blob: 5a5530141c661daa9dbc0cb6fda43fd8c01aaf1e [file] [log] [blame]
Brian Kernighan87b94932012-12-22 10:35:39 -05001/****************************************************************
2Copyright (C) Lucent Technologies 1997
3All Rights Reserved
4
5Permission to use, copy, modify, and distribute this software and
6its documentation for any purpose and without fee is hereby
7granted, provided that the above copyright notice appear in all
8copies and that both that the copyright notice and this
9permission notice and warranty disclaimer appear in supporting
10documentation, and that the name Lucent Technologies or any of
11its entities not be used in advertising or publicity pertaining
12to distribution of the software without specific, written prior
13permission.
14
15LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22THIS SOFTWARE.
23****************************************************************/
24
25#include <assert.h>
Arnold D. Robbins961eec12019-10-24 09:42:51 -040026#include <stdint.h>
Arnold D. Robbins108224b2019-11-10 21:19:18 +020027#include <stdbool.h>
Arnold D. Robbins11074372020-05-15 15:12:15 +030028#if __STDC_VERSION__ <= 199901L
Arnold D. Robbins93e5dd82020-04-16 20:56:49 +030029#define noreturn
30#else
awkfan77bb538fe2020-04-05 19:10:52 +010031#include <stdnoreturn.h>
Arnold D. Robbins93e5dd82020-04-16 20:56:49 +030032#endif
Brian Kernighan87b94932012-12-22 10:35:39 -050033
34typedef double Awkfloat;
35
36/* unsigned char is more trouble than it's worth */
37
38typedef unsigned char uschar;
39
zoulasc65892082019-10-24 09:40:15 -040040#define xfree(a) { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
41/*
42 * We sometimes cheat writing read-only pointers to NUL-terminate them
43 * and then put back the original value
44 */
45#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
Brian Kernighan87b94932012-12-22 10:35:39 -050046
Arnold D. Robbins795a06b2019-07-28 05:51:52 -060047#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for dprintf
Brian Kernighan87b94932012-12-22 10:35:39 -050048*/
49#define DEBUG
50#ifdef DEBUG
51 /* uses have to be doubly parenthesized */
52# define dprintf(x) if (dbg) printf x
53#else
54# define dprintf(x)
55#endif
56
Arnold D. Robbins108224b2019-11-10 21:19:18 +020057extern enum compile_states {
58 RUNNING,
59 COMPILING,
60 ERROR_PRINTING
61} compile_time;
62
63extern bool safe; /* false => unsafe, true => safe */
Brian Kernighan87b94932012-12-22 10:35:39 -050064
65#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
66extern int recsize; /* size of current record, orig RECSIZE */
67
Arnold D. Robbins4d9b1292020-01-24 11:15:30 +020068extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
Brian Kernighan87b94932012-12-22 10:35:39 -050069extern char **FS;
70extern char **RS;
71extern char **ORS;
72extern char **OFS;
73extern char **OFMT;
74extern Awkfloat *NR;
75extern Awkfloat *FNR;
76extern Awkfloat *NF;
77extern char **FILENAME;
78extern char **SUBSEP;
79extern Awkfloat *RSTART;
80extern Awkfloat *RLENGTH;
81
82extern char *record; /* points to $0 */
83extern int lineno; /* line number in awk program */
84extern int errorflag; /* 1 if error has occurred */
Arnold D. Robbins108224b2019-11-10 21:19:18 +020085extern bool donefld; /* true if record broken into fields */
86extern bool donerec; /* true if record is valid (no fld has changed */
Brian Kernighan87b94932012-12-22 10:35:39 -050087extern int dbg;
88
zoulasc65892082019-10-24 09:40:15 -040089extern const char *patbeg; /* beginning of pattern matched */
Brian Kernighan87b94932012-12-22 10:35:39 -050090extern int patlen; /* length of pattern matched. set in b.c */
91
92/* Cell: all information about a variable or constant */
93
94typedef struct Cell {
95 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */
96 uschar csub; /* CCON, CTEMP, CFLD, etc. */
97 char *nval; /* name, for variables only */
98 char *sval; /* string value */
99 Awkfloat fval; /* value as number */
Arnold D. Robbins32093f52018-08-22 20:40:26 +0300100 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
101 char *fmt; /* CONVFMT/OFMT value used to convert from number */
Brian Kernighan87b94932012-12-22 10:35:39 -0500102 struct Cell *cnext; /* ptr to next if chained */
103} Cell;
104
105typedef struct Array { /* symbol table array */
106 int nelem; /* elements in table right now */
107 int size; /* size of tab */
108 Cell **tab; /* hash table pointers */
109} Array;
110
111#define NSYMTAB 50 /* initial size of a symbol table */
112extern Array *symtab;
113
114extern Cell *nrloc; /* NR */
115extern Cell *fnrloc; /* FNR */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700116extern Cell *fsloc; /* FS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500117extern Cell *nfloc; /* NF */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700118extern Cell *ofsloc; /* OFS */
119extern Cell *orsloc; /* ORS */
120extern Cell *rsloc; /* RS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500121extern Cell *rstartloc; /* RSTART */
122extern Cell *rlengthloc; /* RLENGTH */
Cody Peter Mello97a4b7e2018-09-17 11:59:04 -0700123extern Cell *subseploc; /* SUBSEP */
Cody Melloae99b752019-06-17 10:08:54 -0900124extern Cell *symtabloc; /* SYMTAB */
Brian Kernighan87b94932012-12-22 10:35:39 -0500125
126/* Cell.tval values: */
127#define NUM 01 /* number value is valid */
128#define STR 02 /* string value is valid */
129#define DONTFREE 04 /* string space is not freeable */
130#define CON 010 /* this is a constant */
131#define ARR 020 /* this is an array */
132#define FCN 040 /* this is a function name */
133#define FLD 0100 /* this is a field $1, $2, ... */
134#define REC 0200 /* this is $0 */
Arnold D. Robbins32093f52018-08-22 20:40:26 +0300135#define CONVC 0400 /* string was converted from number via CONVFMT */
136#define CONVO 01000 /* string was converted from number via OFMT */
Brian Kernighan87b94932012-12-22 10:35:39 -0500137
138
139/* function types */
140#define FLENGTH 1
141#define FSQRT 2
142#define FEXP 3
143#define FLOG 4
144#define FINT 5
145#define FSYSTEM 6
146#define FRAND 7
147#define FSRAND 8
148#define FSIN 9
149#define FCOS 10
150#define FATAN 11
151#define FTOUPPER 12
152#define FTOLOWER 13
153#define FFLUSH 14
154
155/* Node: parse tree is made of nodes, with Cell's at bottom */
156
157typedef struct Node {
158 int ntype;
159 struct Node *nnext;
160 int lineno;
161 int nobj;
162 struct Node *narg[1]; /* variable: actual size set by calling malloc */
163} Node;
164
165#define NIL ((Node *) 0)
166
167extern Node *winner;
168extern Node *nullstat;
169extern Node *nullnode;
170
171/* ctypes */
172#define OCELL 1
173#define OBOOL 2
174#define OJUMP 3
175
176/* Cell subtypes: csub */
177#define CFREE 7
178#define CCOPY 6
179#define CCON 5
180#define CTEMP 4
Arnold D. Robbins795a06b2019-07-28 05:51:52 -0600181#define CNAME 3
Brian Kernighan87b94932012-12-22 10:35:39 -0500182#define CVAR 2
183#define CFLD 1
184#define CUNK 0
185
186/* bool subtypes */
187#define BTRUE 11
188#define BFALSE 12
189
190/* jump subtypes */
191#define JEXIT 21
192#define JNEXT 22
193#define JBREAK 23
194#define JCONT 24
195#define JRET 25
196#define JNEXTFILE 26
197
198/* node types */
199#define NVALUE 1
200#define NSTAT 2
201#define NEXPR 3
202
203
204extern int pairstack[], paircnt;
205
206#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
207#define isvalue(n) ((n)->ntype == NVALUE)
208#define isexpr(n) ((n)->ntype == NEXPR)
209#define isjump(n) ((n)->ctype == OJUMP)
210#define isexit(n) ((n)->csub == JEXIT)
211#define isbreak(n) ((n)->csub == JBREAK)
212#define iscont(n) ((n)->csub == JCONT)
213#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
214#define isret(n) ((n)->csub == JRET)
215#define isrec(n) ((n)->tval & REC)
216#define isfld(n) ((n)->tval & FLD)
217#define isstr(n) ((n)->tval & STR)
218#define isnum(n) ((n)->tval & NUM)
219#define isarr(n) ((n)->tval & ARR)
220#define isfcn(n) ((n)->tval & FCN)
221#define istrue(n) ((n)->csub == BTRUE)
222#define istemp(n) ((n)->csub == CTEMP)
223#define isargument(n) ((n)->nobj == ARG)
224/* #define freeable(p) (!((p)->tval & DONTFREE)) */
225#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
226
227/* structures used by regular expression matching machinery, mostly b.c: */
228
229#define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
230 /* watch out in match(), etc. */
Alexander Richardsoncbf92432019-09-10 07:54:11 +0100231#define HAT (NCHARS+2) /* matches ^ in regular expr */
Brian Kernighan87b94932012-12-22 10:35:39 -0500232#define NSTATES 32
233
234typedef struct rrow {
235 long ltype; /* long avoids pointer warnings on 64-bit */
236 union {
237 int i;
238 Node *np;
239 uschar *up;
240 } lval; /* because Al stores a pointer in it! */
241 int *lfollow;
242} rrow;
243
244typedef struct fa {
zoulascc16e8692019-10-17 13:04:46 -0400245 unsigned int **gototab;
246 uschar *out;
Brian Kernighan87b94932012-12-22 10:35:39 -0500247 uschar *restr;
zoulascc16e8692019-10-17 13:04:46 -0400248 int **posns;
249 int state_count;
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200250 bool anchor;
Brian Kernighan87b94932012-12-22 10:35:39 -0500251 int use;
252 int initstat;
253 int curstat;
254 int accept;
Brian Kernighan87b94932012-12-22 10:35:39 -0500255 struct rrow re[1]; /* variable: actual size set by calling malloc */
256} fa;
257
258
259#include "proto.h"