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
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 */
84	int	 tval;		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
85	struct Cell *cnext;	/* ptr to next if chained */
86} Cell;
87
88typedef struct Array {		/* symbol table array */
89	int	nelem;		/* elements in table right now */
90	int	size;		/* size of tab */
91	Cell	**tab;		/* hash table pointers */
92} Array;
93
94#define	NSYMTAB	50	/* initial size of a symbol table */
95extern Array	*symtab;
96
97extern Cell	*nrloc;		/* NR */
98extern Cell	*fnrloc;	/* FNR */
99extern Cell	*nfloc;		/* NF */
100extern Cell	*rstartloc;	/* RSTART */
101extern Cell	*rlengthloc;	/* RLENGTH */
102
103/* Cell.tval values: */
104#define	NUM	01	/* number value is valid */
105#define	STR	02	/* string value is valid */
106#define DONTFREE 04	/* string space is not freeable */
107#define	CON	010	/* this is a constant */
108#define	ARR	020	/* this is an array */
109#define	FCN	040	/* this is a function name */
110#define FLD	0100	/* this is a field $1, $2, ... */
111#define	REC	0200	/* this is $0 */
112
113
114/* function types */
115#define	FLENGTH	1
116#define	FSQRT	2
117#define	FEXP	3
118#define	FLOG	4
119#define	FINT	5
120#define	FSYSTEM	6
121#define	FRAND	7
122#define	FSRAND	8
123#define	FSIN	9
124#define	FCOS	10
125#define	FATAN	11
126#define	FTOUPPER 12
127#define	FTOLOWER 13
128#define	FFLUSH	14
129#define	FAND	15
130#define	FFOR	16
131#define	FXOR	17
132#define	FCOMPL	18
133#define	FLSHIFT	19
134#define	FRSHIFT	20
135
136/* Node:  parse tree is made of nodes, with Cell's at bottom */
137
138typedef struct Node {
139	int	ntype;
140	struct	Node *nnext;
141	int	lineno;
142	int	nobj;
143	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
144} Node;
145
146#define	NIL	((Node *) 0)
147
148extern Node	*winner;
149extern Node	*nullstat;
150extern Node	*nullnode;
151
152/* ctypes */
153#define OCELL	1
154#define OBOOL	2
155#define OJUMP	3
156
157/* Cell subtypes: csub */
158#define	CFREE	7
159#define CCOPY	6
160#define CCON	5
161#define CTEMP	4
162#define CNAME	3
163#define CVAR	2
164#define CFLD	1
165#define	CUNK	0
166
167/* bool subtypes */
168#define BTRUE	11
169#define BFALSE	12
170
171/* jump subtypes */
172#define JEXIT	21
173#define JNEXT	22
174#define	JBREAK	23
175#define	JCONT	24
176#define	JRET	25
177#define	JNEXTFILE	26
178
179/* node types */
180#define NVALUE	1
181#define NSTAT	2
182#define NEXPR	3
183
184
185extern	int	pairstack[], paircnt;
186
187#define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
188#define isvalue(n)	((n)->ntype == NVALUE)
189#define isexpr(n)	((n)->ntype == NEXPR)
190#define isjump(n)	((n)->ctype == OJUMP)
191#define isexit(n)	((n)->csub == JEXIT)
192#define	isbreak(n)	((n)->csub == JBREAK)
193#define	iscont(n)	((n)->csub == JCONT)
194#define	isnext(n)	((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
195#define	isret(n)	((n)->csub == JRET)
196#define isrec(n)	((n)->tval & REC)
197#define isfld(n)	((n)->tval & FLD)
198#define isstr(n)	((n)->tval & STR)
199#define isnum(n)	((n)->tval & NUM)
200#define isarr(n)	((n)->tval & ARR)
201#define isfcn(n)	((n)->tval & FCN)
202#define istrue(n)	((n)->csub == BTRUE)
203#define istemp(n)	((n)->csub == CTEMP)
204#define	isargument(n)	((n)->nobj == ARG)
205/* #define freeable(p)	(!((p)->tval & DONTFREE)) */
206#define freeable(p)	( ((p)->tval & (STR|DONTFREE)) == STR )
207
208/* structures used by regular expression matching machinery, mostly b.c: */
209
210#define NCHARS	(256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
211				/* watch out in match(), etc. */
212#define NSTATES	32
213
214typedef struct rrow {
215	long	ltype;	/* long avoids pointer warnings on 64-bit */
216	union {
217		int i;
218		Node *np;
219		uschar *up;
220	} lval;		/* because Al stores a pointer in it! */
221	int	*lfollow;
222} rrow;
223
224typedef struct fa {
225	uschar	gototab[NSTATES][NCHARS];
226	uschar	out[NSTATES];
227	uschar	*restr;
228	int	*posns[NSTATES];
229	int	anchor;
230	int	use;
231	int	initstat;
232	int	curstat;
233	int	accept;
234	int	reset;
235	struct	rrow re[1];	/* variable: actual size set by calling malloc */
236} fa;
237
238
239#include "proto.h"
240