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