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