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