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 */
47extern int	Unix2003_compat;
48
49#define	RECSIZE	(8 * 1024)	/* sets limit on records, fields, etc., etc. */
50extern int	recsize;	/* size of current record, orig RECSIZE */
51
52extern char	**FS;
53extern char	**RS;
54extern char	**ORS;
55extern char	**OFS;
56extern char	**OFMT;
57extern Awkfloat *NR;
58extern Awkfloat *FNR;
59extern Awkfloat *NF;
60extern char	**FILENAME;
61extern char	**SUBSEP;
62extern Awkfloat *RSTART;
63extern Awkfloat *RLENGTH;
64
65extern char	*record;	/* points to $0 */
66extern int	lineno;		/* line number in awk program */
67extern int	errorflag;	/* 1 if error has occurred */
68extern int	donefld;	/* 1 if record broken into fields */
69extern int	donerec;	/* 1 if record is valid (no fld has changed */
70extern char	inputFS[];	/* FS at time of input, for field splitting */
71
72extern int	dbg;
73
74extern	char	*patbeg;	/* beginning of pattern matched */
75extern	int	patlen;		/* length of pattern matched.  set in b.c */
76
77/* Cell:  all information about a variable or constant */
78
79typedef struct Cell {
80	uschar	ctype;		/* OCELL, OBOOL, OJUMP, etc. */
81	uschar	csub;		/* CCON, CTEMP, CFLD, etc. */
82	char	*nval;		/* name, for variables only */
83	char	*sval;		/* string value */
84	Awkfloat fval;		/* value as number */
85	int	 tval;		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
86	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 */
100extern Cell	*nfloc;		/* NF */
101extern Cell	*rstartloc;	/* RSTART */
102extern Cell	*rlengthloc;	/* RLENGTH */
103
104/* Cell.tval values: */
105#define	NUM	01	/* number value is valid */
106#define	STR	02	/* string value is valid */
107#define DONTFREE 04	/* string space is not freeable */
108#define	CON	010	/* this is a constant */
109#define	ARR	020	/* this is an array */
110#define	FCN	040	/* this is a function name */
111#define FLD	0100	/* this is a field $1, $2, ... */
112#define	REC	0200	/* this is $0 */
113
114
115/* function types */
116#define	FLENGTH	1
117#define	FSQRT	2
118#define	FEXP	3
119#define	FLOG	4
120#define	FINT	5
121#define	FSYSTEM	6
122#define	FRAND	7
123#define	FSRAND	8
124#define	FSIN	9
125#define	FCOS	10
126#define	FATAN	11
127#define	FTOUPPER 12
128#define	FTOLOWER 13
129#define	FFLUSH	14
130
131/* Node:  parse tree is made of nodes, with Cell's at bottom */
132
133typedef struct Node {
134	int	ntype;
135	struct	Node *nnext;
136	int	lineno;
137	int	nobj;
138	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
139} Node;
140
141#define	NIL	((Node *) 0)
142
143extern Node	*winner;
144extern Node	*nullstat;
145extern Node	*nullnode;
146
147/* ctypes */
148#define OCELL	1
149#define OBOOL	2
150#define OJUMP	3
151
152/* Cell subtypes: csub */
153#define	CFREE	7
154#define CCOPY	6
155#define CCON	5
156#define CTEMP	4
157#define CNAME	3
158#define CVAR	2
159#define CFLD	1
160#define	CUNK	0
161
162/* bool subtypes */
163#define BTRUE	11
164#define BFALSE	12
165
166/* jump subtypes */
167#define JEXIT	21
168#define JNEXT	22
169#define	JBREAK	23
170#define	JCONT	24
171#define	JRET	25
172#define	JNEXTFILE	26
173
174/* node types */
175#define NVALUE	1
176#define NSTAT	2
177#define NEXPR	3
178
179
180extern	int	pairstack[], paircnt;
181
182#define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
183#define isvalue(n)	((n)->ntype == NVALUE)
184#define isexpr(n)	((n)->ntype == NEXPR)
185#define isjump(n)	((n)->ctype == OJUMP)
186#define isexit(n)	((n)->csub == JEXIT)
187#define	isbreak(n)	((n)->csub == JBREAK)
188#define	iscont(n)	((n)->csub == JCONT)
189#define	isnext(n)	((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
190#define	isret(n)	((n)->csub == JRET)
191#define isrec(n)	((n)->tval & REC)
192#define isfld(n)	((n)->tval & FLD)
193#define isstr(n)	((n)->tval & STR)
194#define isnum(n)	((n)->tval & NUM)
195#define isarr(n)	((n)->tval & ARR)
196#define isfcn(n)	((n)->tval & FCN)
197#define istrue(n)	((n)->csub == BTRUE)
198#define istemp(n)	((n)->csub == CTEMP)
199#define	isargument(n)	((n)->nobj == ARG)
200/* #define freeable(p)	(!((p)->tval & DONTFREE)) */
201#define freeable(p)	( ((p)->tval & (STR|DONTFREE)) == STR )
202
203/* structures used by regular expression matching machinery, mostly b.c: */
204
205#define NCHARS	(256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
206				/* watch out in match(), etc. */
207#define NSTATES	32
208
209typedef struct rrow {
210	long	ltype;	/* long avoids pointer warnings on 64-bit */
211	union {
212		int i;
213		Node *np;
214		uschar *up;
215	} lval;		/* because Al stores a pointer in it! */
216	int	*lfollow;
217} rrow;
218
219typedef struct fa {
220	uschar	gototab[NSTATES][NCHARS];
221	uschar	out[NSTATES];
222	uschar	*restr;
223	int	*posns[NSTATES];
224	int	anchor;
225	int	use;
226	int	initstat;
227	int	curstat;
228	int	accept;
229	int	reset;
230	struct	rrow re[1];	/* variable: actual size set by calling malloc */
231} fa;
232
233
234#include "proto.h"
235