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