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