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