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