xref: /minix/external/historical/nawk/dist/awk.h (revision 5ea9e707)
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 
27 typedef double	Awkfloat;
28 
29 /* unsigned char is more trouble than it's worth */
30 
31 typedef	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 
45 extern int	compile_time;	/* 1 if compiling, 0 if running */
46 extern int	safe;		/* 0 => unsafe, 1 => safe */
47 
48 #define	RECSIZE	(8 * 1024)	/* sets limit on records, fields, etc., etc. */
49 extern int	recsize;	/* size of current record, orig RECSIZE */
50 
51 extern char	EMPTY[];
52 extern char	**FS;
53 extern char	**RS;
54 extern char	**ORS;
55 extern char	**OFS;
56 extern char	**OFMT;
57 extern Awkfloat *NR;
58 extern Awkfloat *FNR;
59 extern Awkfloat *NF;
60 extern char	**FILENAME;
61 extern char	**SUBSEP;
62 extern Awkfloat *RSTART;
63 extern Awkfloat *RLENGTH;
64 
65 extern uschar	*record;	/* points to $0 */
66 extern int	lineno;		/* line number in awk program */
67 extern int	errorflag;	/* 1 if error has occurred */
68 extern int	donefld;	/* 1 if record broken into fields */
69 extern int	donerec;	/* 1 if record is valid (no fld has changed */
70 
71 extern int	dbg;
72 
73 extern	uschar	*patbeg;	/* beginning of pattern matched */
74 extern	int	patlen;		/* length of pattern matched.  set in b.c */
75 
76 /* Cell:  all information about a variable or constant */
77 
78 typedef 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 
88 typedef 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 */
95 extern Array	*symtab;
96 
97 extern Cell	*nrloc;		/* NR */
98 extern Cell	*fnrloc;	/* FNR */
99 extern Cell	*nfloc;		/* NF */
100 extern Cell	*rstartloc;	/* RSTART */
101 extern 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 FSYSTIME	15
130 #define FSTRFTIME	16
131 
132 /* Node:  parse tree is made of nodes, with Cell's at bottom */
133 
134 typedef struct Node {
135 	int	ntype;
136 	struct	Node *nnext;
137 	int	lineno;
138 	int	nobj;
139 	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
140 } Node;
141 
142 #define	NIL	((Node *) 0)
143 
144 extern Node	*winner;
145 extern Node	*nullstat;
146 extern Node	*nullnode;
147 
148 /* ctypes */
149 #define OCELL	1
150 #define OBOOL	2
151 #define OJUMP	3
152 
153 /* Cell subtypes: csub */
154 #define	CFREE	7
155 #define CCOPY	6
156 #define CCON	5
157 #define CTEMP	4
158 #define CNAME	3
159 #define CVAR	2
160 #define CFLD	1
161 #define	CUNK	0
162 
163 /* bool subtypes */
164 #define BTRUE	11
165 #define BFALSE	12
166 
167 /* jump subtypes */
168 #define JEXIT	21
169 #define JNEXT	22
170 #define	JBREAK	23
171 #define	JCONT	24
172 #define	JRET	25
173 #define	JNEXTFILE	26
174 
175 /* node types */
176 #define NVALUE	1
177 #define NSTAT	2
178 #define NEXPR	3
179 
180 
181 extern	int	pairstack[], paircnt;
182 
183 #define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
184 #define isvalue(n)	((n)->ntype == NVALUE)
185 #define isexpr(n)	((n)->ntype == NEXPR)
186 #define isjump(n)	((n)->ctype == OJUMP)
187 #define isexit(n)	((n)->csub == JEXIT)
188 #define	isbreak(n)	((n)->csub == JBREAK)
189 #define	iscont(n)	((n)->csub == JCONT)
190 #define	isnext(n)	((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
191 #define	isret(n)	((n)->csub == JRET)
192 #define isrec(n)	((n)->tval & REC)
193 #define isfld(n)	((n)->tval & FLD)
194 #define isstr(n)	((n)->tval & STR)
195 #define isnum(n)	((n)->tval & NUM)
196 #define isarr(n)	((n)->tval & ARR)
197 #define isfcn(n)	((n)->tval & FCN)
198 #define istrue(n)	((n)->csub == BTRUE)
199 #define istemp(n)	((n)->csub == CTEMP)
200 #define	isargument(n)	((n)->nobj == ARG)
201 /* #define freeable(p)	(!((p)->tval & DONTFREE)) */
202 #define freeable(p)	( ((p)->tval & (STR|DONTFREE)) == STR )
203 
204 /* structures used by regular expression matching machinery, mostly b.c: */
205 
206 #define NCHARS	(256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
207 				/* watch out in match(), etc. */
208 typedef struct rrow {
209 	long	ltype;	/* long avoids pointer warnings on 64-bit */
210 	union {
211 		int i;
212 		Node *np;
213 		uschar *up;
214 	} lval;		/* because Al stores a pointer in it! */
215 	int	*lfollow;
216 } rrow;
217 
218 typedef struct fa {
219 	unsigned int	**gototab;
220 	uschar	*out;
221 	uschar	*restr;
222 	int	**posns;
223 	int	state_count;
224 	int	anchor;
225 	int	use;
226 	int	initstat;
227 	int	curstat;
228 	int	accept;
229 	struct	rrow re[1];	/* variable: actual size set by calling malloc */
230 } fa;
231 
232 
233 #include "proto.h"
234