xref: /openbsd/usr.bin/awk/awk.h (revision a6445c1d)
1 /*	$OpenBSD: awk.h,v 1.13 2008/10/06 20:38:33 millert Exp $	*/
2 /****************************************************************
3 Copyright (C) Lucent Technologies 1997
4 All Rights Reserved
5 
6 Permission to use, copy, modify, and distribute this software and
7 its documentation for any purpose and without fee is hereby
8 granted, provided that the above copyright notice appear in all
9 copies and that both that the copyright notice and this
10 permission notice and warranty disclaimer appear in supporting
11 documentation, and that the name Lucent Technologies or any of
12 its entities not be used in advertising or publicity pertaining
13 to distribution of the software without specific, written prior
14 permission.
15 
16 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23 THIS SOFTWARE.
24 ****************************************************************/
25 
26 #include <assert.h>
27 
28 typedef double	Awkfloat;
29 
30 /* unsigned char is more trouble than it's worth */
31 
32 typedef	unsigned char uschar;
33 
34 #define	xfree(a)	{ if ((a) != NULL) { free((void *) (a)); (a) = NULL; } }
35 
36 #define	NN(p)	((p) ? (p) : "(null)")	/* guaranteed non-null for dprintf
37 */
38 #define	DEBUG
39 #ifdef	DEBUG
40 			/* uses have to be doubly parenthesized */
41 #	define	dprintf(x)	if (dbg) printf x
42 #else
43 #	define	dprintf(x)
44 #endif
45 
46 extern int	compile_time;	/* 1 if compiling, 0 if running */
47 extern int	safe;		/* 0 => unsafe, 1 => safe */
48 
49 #define	RECSIZE	(8 * 1024)	/* sets limit on records, fields, etc., etc. */
50 extern int	recsize;	/* size of current record, orig RECSIZE */
51 
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 char	*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 extern char	inputFS[];	/* FS at time of input, for field splitting */
71 
72 extern int	dbg;
73 
74 extern	char	*patbeg;	/* beginning of pattern matched */
75 extern	int	patlen;		/* length of pattern matched.  set in b.c */
76 
77 /* Cell:  all information about a variable or constant */
78 
79 typedef 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 
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	*nfloc;		/* NF */
101 extern Cell	*rstartloc;	/* RSTART */
102 extern 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 #define FAND	15
131 #define FFOR	16
132 #define FXOR	17
133 #define FCOMPL	18
134 #define FLSHIFT	19
135 #define FRSHIFT	20
136 
137 /* Node:  parse tree is made of nodes, with Cell's at bottom */
138 
139 typedef struct Node {
140 	int	ntype;
141 	struct	Node *nnext;
142 	int	lineno;
143 	int	nobj;
144 	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
145 } Node;
146 
147 #define	NIL	((Node *) 0)
148 
149 extern Node	*winner;
150 extern Node	*nullstat;
151 extern Node	*nullnode;
152 
153 /* ctypes */
154 #define OCELL	1
155 #define OBOOL	2
156 #define OJUMP	3
157 
158 /* Cell subtypes: csub */
159 #define	CFREE	7
160 #define CCOPY	6
161 #define CCON	5
162 #define CTEMP	4
163 #define CNAME	3
164 #define CVAR	2
165 #define CFLD	1
166 #define	CUNK	0
167 
168 /* bool subtypes */
169 #define BTRUE	11
170 #define BFALSE	12
171 
172 /* jump subtypes */
173 #define JEXIT	21
174 #define JNEXT	22
175 #define	JBREAK	23
176 #define	JCONT	24
177 #define	JRET	25
178 #define	JNEXTFILE	26
179 
180 /* node types */
181 #define NVALUE	1
182 #define NSTAT	2
183 #define NEXPR	3
184 
185 
186 extern	int	pairstack[], paircnt;
187 
188 #define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
189 #define isvalue(n)	((n)->ntype == NVALUE)
190 #define isexpr(n)	((n)->ntype == NEXPR)
191 #define isjump(n)	((n)->ctype == OJUMP)
192 #define isexit(n)	((n)->csub == JEXIT)
193 #define	isbreak(n)	((n)->csub == JBREAK)
194 #define	iscont(n)	((n)->csub == JCONT)
195 #define	isnext(n)	((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
196 #define	isret(n)	((n)->csub == JRET)
197 #define isrec(n)	((n)->tval & REC)
198 #define isfld(n)	((n)->tval & FLD)
199 #define isstr(n)	((n)->tval & STR)
200 #define isnum(n)	((n)->tval & NUM)
201 #define isarr(n)	((n)->tval & ARR)
202 #define isfcn(n)	((n)->tval & FCN)
203 #define istrue(n)	((n)->csub == BTRUE)
204 #define istemp(n)	((n)->csub == CTEMP)
205 #define	isargument(n)	((n)->nobj == ARG)
206 /* #define freeable(p)	(!((p)->tval & DONTFREE)) */
207 #define freeable(p)	( ((p)->tval & (STR|DONTFREE)) == STR )
208 
209 /* structures used by regular expression matching machinery, mostly b.c: */
210 
211 #define NCHARS	(256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
212 				/* watch out in match(), etc. */
213 #define NSTATES	32
214 
215 typedef struct rrow {
216 	long	ltype;	/* long avoids pointer warnings on 64-bit */
217 	union {
218 		int i;
219 		Node *np;
220 		uschar *up;
221 	} lval;		/* because Al stores a pointer in it! */
222 	int	*lfollow;
223 } rrow;
224 
225 typedef struct fa {
226 	uschar	gototab[NSTATES][NCHARS];
227 	uschar	out[NSTATES];
228 	uschar	*restr;
229 	int	*posns[NSTATES];
230 	int	anchor;
231 	int	use;
232 	int	initstat;
233 	int	curstat;
234 	int	accept;
235 	int	reset;
236 	struct	rrow re[1];	/* variable: actual size set by calling malloc */
237 } fa;
238 
239 
240 #include "proto.h"
241