1/****************************************************************
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>
26#include <stdint.h>
27
28#ifndef bool
29#define bool int
30#define true 1
31#define false 0
32#endif
33
34#if __STDC_VERSION__ <= 199901L
35#define noreturn
36#else
37#include <stdnoreturn.h>
38#endif
39
40typedef double Awkfloat;
41
42/* unsigned char is more trouble than it's worth */
43
44typedef unsigned char uschar;
45
46#define xfree(a) { free((void *)(intptr_t)(a)); (a) = NULL; }
47/*
48 * We sometimes cheat writing read-only pointers to NUL-terminate them
49 * and then put back the original value
50 */
51#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
52
53#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF
54*/
55#define DEBUG
56#ifdef DEBUG
57# define DPRINTF(...) if (dbg) printf(__VA_ARGS__)
58#else
59# define DPRINTF(...)
60#endif
61
62extern enum compile_states {
63 RUNNING,
64 COMPILING,
65 ERROR_PRINTING
66} compile_time;
67
68extern bool safe; /* false => unsafe, true => safe */
69
70#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
71extern int recsize; /* size of current record, orig RECSIZE */
72
73extern size_t awk_mb_cur_max; /* max size of a multi-byte character */
74
75extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
76extern char **FS;
77extern char **RS;
78extern char **ORS;
79extern char **OFS;
80extern char **OFMT;
81extern Awkfloat *NR;
82extern Awkfloat *FNR;
83extern Awkfloat *NF;
84extern char **FILENAME;
85extern char **SUBSEP;
86extern Awkfloat *RSTART;
87extern Awkfloat *RLENGTH;
88
89extern bool CSV; /* true for csv input */
90
91extern char *record; /* points to $0 */
92extern int lineno; /* line number in awk program */
93extern int errorflag; /* 1 if error has occurred */
94extern bool donefld; /* true if record broken into fields */
95extern bool donerec; /* true if record is valid (no fld has changed */
96extern int dbg;
97
98extern const char *patbeg; /* beginning of pattern matched */
99extern int patlen; /* length of pattern matched. set in b.c */
100
101/* Cell: all information about a variable or constant */
102
103typedef struct Cell {
104 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */
105 uschar csub; /* CCON, CTEMP, CFLD, etc. */
106 char *nval; /* name, for variables only */
107 char *sval; /* string value */
108 Awkfloat fval; /* value as number */
109 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
110 char *fmt; /* CONVFMT/OFMT value used to convert from number */
111 struct Cell *cnext; /* ptr to next if chained */
112} Cell;
113
114typedef struct Array { /* symbol table array */
115 int nelem; /* elements in table right now */
116 int size; /* size of tab */
117 Cell **tab; /* hash table pointers */
118} Array;
119
120#define NSYMTAB 50 /* initial size of a symbol table */
121extern Array *symtab;
122
123extern Cell *nrloc; /* NR */
124extern Cell *fnrloc; /* FNR */
125extern Cell *fsloc; /* FS */
126extern Cell *nfloc; /* NF */
127extern Cell *ofsloc; /* OFS */
128extern Cell *orsloc; /* ORS */
129extern Cell *rsloc; /* RS */
130extern Cell *rstartloc; /* RSTART */
131extern Cell *rlengthloc; /* RLENGTH */
132extern Cell *subseploc; /* SUBSEP */
133extern Cell *symtabloc; /* SYMTAB */
134
135/* Cell.tval values: */
136#define NUM 01 /* number value is valid */
137#define STR 02 /* string value is valid */
138#define DONTFREE 04 /* string space is not freeable */
139#define CON 010 /* this is a constant */
140#define ARR 020 /* this is an array */
141#define FCN 040 /* this is a function name */
142#define FLD 0100 /* this is a field $1, $2, ... */
143#define REC 0200 /* this is $0 */
144#define CONVC 0400 /* string was converted from number via CONVFMT */
145#define CONVO 01000 /* string was converted from number via OFMT */
146
147
148/* function types */
149#define FLENGTH 1
150#define FSQRT 2
151#define FEXP 3
152#define FLOG 4
153#define FINT 5
154#define FSYSTEM 6
155#define FRAND 7
156#define FSRAND 8
157#define FSIN 9
158#define FCOS 10
159#define FATAN 11
160#define FTOUPPER 12
161#define FTOLOWER 13
162#define FFLUSH 14
163
164/* Node: parse tree is made of nodes, with Cell's at bottom */
165
166typedef struct Node {
167 int ntype;
168 struct Node *nnext;
169 int lineno;
170 int nobj;
171 struct Node *narg[1]; /* variable: actual size set by calling malloc */
172} Node;
173
174#define NIL ((Node *) 0)
175
176extern Node *winner;
177extern Node *nullnode;
178
179/* ctypes */
180#define OCELL 1
181#define OBOOL 2
182#define OJUMP 3
183
184/* Cell subtypes: csub */
185#define CFREE 7
186#define CCOPY 6
187#define CCON 5
188#define CTEMP 4
189#define CNAME 3
190#define CVAR 2
191#define CFLD 1
192#define CUNK 0
193
194/* bool subtypes */
195#define BTRUE 11
196#define BFALSE 12
197
198/* jump subtypes */
199#define JEXIT 21
200#define JNEXT 22
201#define JBREAK 23
202#define JCONT 24
203#define JRET 25
204#define JNEXTFILE 26
205
206/* node types */
207#define NVALUE 1
208#define NSTAT 2
209#define NEXPR 3
210
211
212extern int pairstack[], paircnt;
213
214#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
215#define isvalue(n) ((n)->ntype == NVALUE)
216#define isexpr(n) ((n)->ntype == NEXPR)
217#define isjump(n) ((n)->ctype == OJUMP)
218#define isexit(n) ((n)->csub == JEXIT)
219#define isbreak(n) ((n)->csub == JBREAK)
220#define iscont(n) ((n)->csub == JCONT)
221#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
222#define isret(n) ((n)->csub == JRET)
223#define isrec(n) ((n)->tval & REC)
224#define isfld(n) ((n)->tval & FLD)
225#define isstr(n) ((n)->tval & STR)
226#define isnum(n) ((n)->tval & NUM)
227#define isarr(n) ((n)->tval & ARR)
228#define isfcn(n) ((n)->tval & FCN)
229#define istrue(n) ((n)->csub == BTRUE)
230#define istemp(n) ((n)->csub == CTEMP)
231#define isargument(n) ((n)->nobj == ARG)
232/* #define freeable(p) (!((p)->tval & DONTFREE)) */
233#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
234
235/* structures used by regular expression matching machinery, mostly b.c: */
236
237#define NCHARS (1256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
238 /* BUG: some overflows (caught) if we use 256 */
239 /* watch out in match(), etc. */
240#define HAT (NCHARS+2) /* matches ^ in regular expr */
241#define NSTATES 32
242
243typedef struct rrow {
244 long ltype; /* long avoids pointer warnings on 64-bit */
245 union {
246 int i;
247 Node *np;
248 uschar *up;
249 int *rp; /* rune representation of char class */
250 } lval; /* because Al stores a pointer in it! */
251 int *lfollow;
252} rrow;
253
254typedef struct gtte { /* gototab entry */
255 unsigned int ch;
256 unsigned int state;
257} gtte;
258
259#define GOTO_DIRECT 128 /* size of ASCII direct-index fast path */
260
261typedef struct gtt { /* gototab */
262 size_t allocated;
263 size_t inuse;
264 gtte *entries;
265 int direct[GOTO_DIRECT]; /* fast path for ASCII; 0 == miss */
266} gtt;
267
268typedef struct fa {
269 gtt *gototab;
270 uschar *out;
271 uschar *restr;
272 int **posns;
273 int state_count;
274 bool anchor;
275 int use;
276 int initstat;
277 int curstat;
278 int accept;
279 struct rrow re[1]; /* variable: actual size set by calling malloc */
280} fa;
281
282
283#include "proto.h"