blob: 68654380638edd2d63664d75204fe2b64301a859 [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>
Brian Kernighan87b94932012-12-22 10:35:39 -050028
29typedef double Awkfloat;
30
31/* unsigned char is more trouble than it's worth */
32
33typedef unsigned char uschar;
34
zoulasc65892082019-10-24 09:40:15 -040035#define xfree(a) { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
36/*
37 * We sometimes cheat writing read-only pointers to NUL-terminate them
38 * and then put back the original value
39 */
40#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
Brian Kernighan87b94932012-12-22 10:35:39 -050041
Arnold D. Robbins795a06b2019-07-28 05:51:52 -060042#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for dprintf
Brian Kernighan87b94932012-12-22 10:35:39 -050043*/
44#define DEBUG
45#ifdef DEBUG
46 /* uses have to be doubly parenthesized */
47# define dprintf(x) if (dbg) printf x
48#else
49# define dprintf(x)
50#endif
51
Arnold D. Robbins108224b2019-11-10 21:19:18 +020052extern enum compile_states {
53 RUNNING,
54 COMPILING,
55 ERROR_PRINTING
56} compile_time;
57
58extern bool safe; /* false => unsafe, true => safe */
Brian Kernighan87b94932012-12-22 10:35:39 -050059
60#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
61extern int recsize; /* size of current record, orig RECSIZE */
62
Arnold D. Robbins4d9b1292020-01-24 11:15:30 +020063extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
Brian Kernighan87b94932012-12-22 10:35:39 -050064extern char **FS;
65extern char **RS;
66extern char **ORS;
67extern char **OFS;
68extern char **OFMT;
69extern Awkfloat *NR;
70extern Awkfloat *FNR;
71extern Awkfloat *NF;
72extern char **FILENAME;
73extern char **SUBSEP;
74extern Awkfloat *RSTART;
75extern Awkfloat *RLENGTH;
76
77extern char *record; /* points to $0 */
78extern int lineno; /* line number in awk program */
79extern int errorflag; /* 1 if error has occurred */
Arnold D. Robbins108224b2019-11-10 21:19:18 +020080extern bool donefld; /* true if record broken into fields */
81extern bool donerec; /* true if record is valid (no fld has changed */
Brian Kernighan87b94932012-12-22 10:35:39 -050082extern int dbg;
83
zoulasc65892082019-10-24 09:40:15 -040084extern const char *patbeg; /* beginning of pattern matched */
Brian Kernighan87b94932012-12-22 10:35:39 -050085extern int patlen; /* length of pattern matched. set in b.c */
86
87/* Cell: all information about a variable or constant */
88
89typedef struct Cell {
90 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */
91 uschar csub; /* CCON, CTEMP, CFLD, etc. */
92 char *nval; /* name, for variables only */
93 char *sval; /* string value */
94 Awkfloat fval; /* value as number */
Arnold D. Robbins32093f52018-08-22 20:40:26 +030095 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
96 char *fmt; /* CONVFMT/OFMT value used to convert from number */
Brian Kernighan87b94932012-12-22 10:35:39 -050097 struct Cell *cnext; /* ptr to next if chained */
98} Cell;
99
100typedef struct Array { /* symbol table array */
101 int nelem; /* elements in table right now */
102 int size; /* size of tab */
103 Cell **tab; /* hash table pointers */
104} Array;
105
106#define NSYMTAB 50 /* initial size of a symbol table */
107extern Array *symtab;
108
109extern Cell *nrloc; /* NR */
110extern Cell *fnrloc; /* FNR */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700111extern Cell *fsloc; /* FS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500112extern Cell *nfloc; /* NF */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700113extern Cell *ofsloc; /* OFS */
114extern Cell *orsloc; /* ORS */
115extern Cell *rsloc; /* RS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500116extern Cell *rstartloc; /* RSTART */
117extern Cell *rlengthloc; /* RLENGTH */
Cody Peter Mello97a4b7e2018-09-17 11:59:04 -0700118extern Cell *subseploc; /* SUBSEP */
Cody Melloae99b752019-06-17 10:08:54 -0900119extern Cell *symtabloc; /* SYMTAB */
Brian Kernighan87b94932012-12-22 10:35:39 -0500120
121/* Cell.tval values: */
122#define NUM 01 /* number value is valid */
123#define STR 02 /* string value is valid */
124#define DONTFREE 04 /* string space is not freeable */
125#define CON 010 /* this is a constant */
126#define ARR 020 /* this is an array */
127#define FCN 040 /* this is a function name */
128#define FLD 0100 /* this is a field $1, $2, ... */
129#define REC 0200 /* this is $0 */
Arnold D. Robbins32093f52018-08-22 20:40:26 +0300130#define CONVC 0400 /* string was converted from number via CONVFMT */
131#define CONVO 01000 /* string was converted from number via OFMT */
Brian Kernighan87b94932012-12-22 10:35:39 -0500132
133
134/* function types */
135#define FLENGTH 1
136#define FSQRT 2
137#define FEXP 3
138#define FLOG 4
139#define FINT 5
140#define FSYSTEM 6
141#define FRAND 7
142#define FSRAND 8
143#define FSIN 9
144#define FCOS 10
145#define FATAN 11
146#define FTOUPPER 12
147#define FTOLOWER 13
148#define FFLUSH 14
149
150/* Node: parse tree is made of nodes, with Cell's at bottom */
151
152typedef struct Node {
153 int ntype;
154 struct Node *nnext;
155 int lineno;
156 int nobj;
157 struct Node *narg[1]; /* variable: actual size set by calling malloc */
158} Node;
159
160#define NIL ((Node *) 0)
161
162extern Node *winner;
163extern Node *nullstat;
164extern Node *nullnode;
165
166/* ctypes */
167#define OCELL 1
168#define OBOOL 2
169#define OJUMP 3
170
171/* Cell subtypes: csub */
172#define CFREE 7
173#define CCOPY 6
174#define CCON 5
175#define CTEMP 4
Arnold D. Robbins795a06b2019-07-28 05:51:52 -0600176#define CNAME 3
Brian Kernighan87b94932012-12-22 10:35:39 -0500177#define CVAR 2
178#define CFLD 1
179#define CUNK 0
180
181/* bool subtypes */
182#define BTRUE 11
183#define BFALSE 12
184
185/* jump subtypes */
186#define JEXIT 21
187#define JNEXT 22
188#define JBREAK 23
189#define JCONT 24
190#define JRET 25
191#define JNEXTFILE 26
192
193/* node types */
194#define NVALUE 1
195#define NSTAT 2
196#define NEXPR 3
197
198
199extern int pairstack[], paircnt;
200
201#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
202#define isvalue(n) ((n)->ntype == NVALUE)
203#define isexpr(n) ((n)->ntype == NEXPR)
204#define isjump(n) ((n)->ctype == OJUMP)
205#define isexit(n) ((n)->csub == JEXIT)
206#define isbreak(n) ((n)->csub == JBREAK)
207#define iscont(n) ((n)->csub == JCONT)
208#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
209#define isret(n) ((n)->csub == JRET)
210#define isrec(n) ((n)->tval & REC)
211#define isfld(n) ((n)->tval & FLD)
212#define isstr(n) ((n)->tval & STR)
213#define isnum(n) ((n)->tval & NUM)
214#define isarr(n) ((n)->tval & ARR)
215#define isfcn(n) ((n)->tval & FCN)
216#define istrue(n) ((n)->csub == BTRUE)
217#define istemp(n) ((n)->csub == CTEMP)
218#define isargument(n) ((n)->nobj == ARG)
219/* #define freeable(p) (!((p)->tval & DONTFREE)) */
220#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
221
222/* structures used by regular expression matching machinery, mostly b.c: */
223
224#define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
225 /* watch out in match(), etc. */
Alexander Richardsoncbf92432019-09-10 07:54:11 +0100226#define HAT (NCHARS+2) /* matches ^ in regular expr */
Brian Kernighan87b94932012-12-22 10:35:39 -0500227#define NSTATES 32
228
229typedef struct rrow {
230 long ltype; /* long avoids pointer warnings on 64-bit */
231 union {
232 int i;
233 Node *np;
234 uschar *up;
235 } lval; /* because Al stores a pointer in it! */
236 int *lfollow;
237} rrow;
238
239typedef struct fa {
zoulascc16e8692019-10-17 13:04:46 -0400240 unsigned int **gototab;
241 uschar *out;
Brian Kernighan87b94932012-12-22 10:35:39 -0500242 uschar *restr;
zoulascc16e8692019-10-17 13:04:46 -0400243 int **posns;
244 int state_count;
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200245 bool anchor;
Brian Kernighan87b94932012-12-22 10:35:39 -0500246 int use;
247 int initstat;
248 int curstat;
249 int accept;
Brian Kernighan87b94932012-12-22 10:35:39 -0500250 struct rrow re[1]; /* variable: actual size set by calling malloc */
251} fa;
252
253
254#include "proto.h"