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