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