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