blob: cc302493a00414e36f62d06ee70fb9d889fa0dc4 [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
Todd C. Miller292d39f2020-06-25 12:32:34 -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
Todd C. Miller292d39f2020-06-25 12:32:34 -060051# define DPRINTF(...) if (dbg) printf(__VA_ARGS__)
Brian Kernighan87b94932012-12-22 10:35:39 -050052#else
Todd C. Miller292d39f2020-06-25 12:32:34 -060053# define DPRINTF(...)
Brian Kernighan87b94932012-12-22 10:35:39 -050054#endif
55
Arnold D. Robbins108224b2019-11-10 21:19:18 +020056extern enum compile_states {
57 RUNNING,
58 COMPILING,
59 ERROR_PRINTING
60} compile_time;
61
62extern bool safe; /* false => unsafe, true => safe */
Brian Kernighan87b94932012-12-22 10:35:39 -050063
64#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
65extern int recsize; /* size of current record, orig RECSIZE */
66
Arnold D. Robbins4d9b1292020-01-24 11:15:30 +020067extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
Brian Kernighan87b94932012-12-22 10:35:39 -050068extern char **FS;
69extern char **RS;
70extern char **ORS;
71extern char **OFS;
72extern char **OFMT;
73extern Awkfloat *NR;
74extern Awkfloat *FNR;
75extern Awkfloat *NF;
76extern char **FILENAME;
77extern char **SUBSEP;
78extern Awkfloat *RSTART;
79extern Awkfloat *RLENGTH;
80
81extern char *record; /* points to $0 */
82extern int lineno; /* line number in awk program */
83extern int errorflag; /* 1 if error has occurred */
Arnold D. Robbins108224b2019-11-10 21:19:18 +020084extern bool donefld; /* true if record broken into fields */
85extern bool donerec; /* true if record is valid (no fld has changed */
Brian Kernighan87b94932012-12-22 10:35:39 -050086extern int dbg;
87
zoulasc65892082019-10-24 09:40:15 -040088extern const char *patbeg; /* beginning of pattern matched */
Brian Kernighan87b94932012-12-22 10:35:39 -050089extern int patlen; /* length of pattern matched. set in b.c */
90
91/* Cell: all information about a variable or constant */
92
93typedef struct Cell {
94 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */
95 uschar csub; /* CCON, CTEMP, CFLD, etc. */
96 char *nval; /* name, for variables only */
97 char *sval; /* string value */
98 Awkfloat fval; /* value as number */
Arnold D. Robbins32093f52018-08-22 20:40:26 +030099 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
100 char *fmt; /* CONVFMT/OFMT value used to convert from number */
Brian Kernighan87b94932012-12-22 10:35:39 -0500101 struct Cell *cnext; /* ptr to next if chained */
102} Cell;
103
104typedef struct Array { /* symbol table array */
105 int nelem; /* elements in table right now */
106 int size; /* size of tab */
107 Cell **tab; /* hash table pointers */
108} Array;
109
110#define NSYMTAB 50 /* initial size of a symbol table */
111extern Array *symtab;
112
113extern Cell *nrloc; /* NR */
114extern Cell *fnrloc; /* FNR */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700115extern Cell *fsloc; /* FS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500116extern Cell *nfloc; /* NF */
Cody Peter Mello52566c02018-09-18 15:45:55 -0700117extern Cell *ofsloc; /* OFS */
118extern Cell *orsloc; /* ORS */
119extern Cell *rsloc; /* RS */
Brian Kernighan87b94932012-12-22 10:35:39 -0500120extern Cell *rstartloc; /* RSTART */
121extern Cell *rlengthloc; /* RLENGTH */
Cody Peter Mello97a4b7e2018-09-17 11:59:04 -0700122extern Cell *subseploc; /* SUBSEP */
Cody Melloae99b752019-06-17 10:08:54 -0900123extern Cell *symtabloc; /* SYMTAB */
Brian Kernighan87b94932012-12-22 10:35:39 -0500124
125/* Cell.tval values: */
126#define NUM 01 /* number value is valid */
127#define STR 02 /* string value is valid */
128#define DONTFREE 04 /* string space is not freeable */
129#define CON 010 /* this is a constant */
130#define ARR 020 /* this is an array */
131#define FCN 040 /* this is a function name */
132#define FLD 0100 /* this is a field $1, $2, ... */
133#define REC 0200 /* this is $0 */
Arnold D. Robbins32093f52018-08-22 20:40:26 +0300134#define CONVC 0400 /* string was converted from number via CONVFMT */
135#define CONVO 01000 /* string was converted from number via OFMT */
Brian Kernighan87b94932012-12-22 10:35:39 -0500136
137
138/* function types */
139#define FLENGTH 1
140#define FSQRT 2
141#define FEXP 3
142#define FLOG 4
143#define FINT 5
144#define FSYSTEM 6
145#define FRAND 7
146#define FSRAND 8
147#define FSIN 9
148#define FCOS 10
149#define FATAN 11
150#define FTOUPPER 12
151#define FTOLOWER 13
152#define FFLUSH 14
153
154/* Node: parse tree is made of nodes, with Cell's at bottom */
155
156typedef struct Node {
157 int ntype;
158 struct Node *nnext;
159 int lineno;
160 int nobj;
161 struct Node *narg[1]; /* variable: actual size set by calling malloc */
162} Node;
163
164#define NIL ((Node *) 0)
165
166extern Node *winner;
167extern Node *nullstat;
168extern Node *nullnode;
169
170/* ctypes */
171#define OCELL 1
172#define OBOOL 2
173#define OJUMP 3
174
175/* Cell subtypes: csub */
176#define CFREE 7
177#define CCOPY 6
178#define CCON 5
179#define CTEMP 4
Arnold D. Robbins795a06b2019-07-28 05:51:52 -0600180#define CNAME 3
Brian Kernighan87b94932012-12-22 10:35:39 -0500181#define CVAR 2
182#define CFLD 1
183#define CUNK 0
184
185/* bool subtypes */
186#define BTRUE 11
187#define BFALSE 12
188
189/* jump subtypes */
190#define JEXIT 21
191#define JNEXT 22
192#define JBREAK 23
193#define JCONT 24
194#define JRET 25
195#define JNEXTFILE 26
196
197/* node types */
198#define NVALUE 1
199#define NSTAT 2
200#define NEXPR 3
201
202
203extern int pairstack[], paircnt;
204
205#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
206#define isvalue(n) ((n)->ntype == NVALUE)
207#define isexpr(n) ((n)->ntype == NEXPR)
208#define isjump(n) ((n)->ctype == OJUMP)
209#define isexit(n) ((n)->csub == JEXIT)
210#define isbreak(n) ((n)->csub == JBREAK)
211#define iscont(n) ((n)->csub == JCONT)
212#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
213#define isret(n) ((n)->csub == JRET)
214#define isrec(n) ((n)->tval & REC)
215#define isfld(n) ((n)->tval & FLD)
216#define isstr(n) ((n)->tval & STR)
217#define isnum(n) ((n)->tval & NUM)
218#define isarr(n) ((n)->tval & ARR)
219#define isfcn(n) ((n)->tval & FCN)
220#define istrue(n) ((n)->csub == BTRUE)
221#define istemp(n) ((n)->csub == CTEMP)
222#define isargument(n) ((n)->nobj == ARG)
223/* #define freeable(p) (!((p)->tval & DONTFREE)) */
224#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
225
226/* structures used by regular expression matching machinery, mostly b.c: */
227
228#define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
229 /* watch out in match(), etc. */
Alexander Richardsoncbf92432019-09-10 07:54:11 +0100230#define HAT (NCHARS+2) /* matches ^ in regular expr */
Brian Kernighan87b94932012-12-22 10:35:39 -0500231#define NSTATES 32
232
233typedef struct rrow {
234 long ltype; /* long avoids pointer warnings on 64-bit */
235 union {
236 int i;
237 Node *np;
238 uschar *up;
239 } lval; /* because Al stores a pointer in it! */
240 int *lfollow;
241} rrow;
242
243typedef struct fa {
zoulascc16e8692019-10-17 13:04:46 -0400244 unsigned int **gototab;
245 uschar *out;
Brian Kernighan87b94932012-12-22 10:35:39 -0500246 uschar *restr;
zoulascc16e8692019-10-17 13:04:46 -0400247 int **posns;
248 int state_count;
Arnold D. Robbins108224b2019-11-10 21:19:18 +0200249 bool anchor;
Brian Kernighan87b94932012-12-22 10:35:39 -0500250 int use;
251 int initstat;
252 int curstat;
253 int accept;
Brian Kernighan87b94932012-12-22 10:35:39 -0500254 struct rrow re[1]; /* variable: actual size set by calling malloc */
255} fa;
256
257
258#include "proto.h"