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