xref: /dragonfly/contrib/awk/awk.h (revision 7485684f)
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 
158 /* Node:  parse tree is made of nodes, with Cell's at bottom */
159 
160 typedef struct Node {
161 	int	ntype;
162 	struct	Node *nnext;
163 	int	lineno;
164 	int	nobj;
165 	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
166 } Node;
167 
168 #define	NIL	((Node *) 0)
169 
170 extern Node	*winner;
171 extern Node	*nullstat;
172 extern Node	*nullnode;
173 
174 /* ctypes */
175 #define OCELL	1
176 #define OBOOL	2
177 #define OJUMP	3
178 
179 /* Cell subtypes: csub */
180 #define	CFREE	7
181 #define CCOPY	6
182 #define CCON	5
183 #define CTEMP	4
184 #define CNAME	3
185 #define CVAR	2
186 #define CFLD	1
187 #define	CUNK	0
188 
189 /* bool subtypes */
190 #define BTRUE	11
191 #define BFALSE	12
192 
193 /* jump subtypes */
194 #define JEXIT	21
195 #define JNEXT	22
196 #define	JBREAK	23
197 #define	JCONT	24
198 #define	JRET	25
199 #define	JNEXTFILE	26
200 
201 /* node types */
202 #define NVALUE	1
203 #define NSTAT	2
204 #define NEXPR	3
205 
206 
207 extern	int	pairstack[], paircnt;
208 
209 #define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
210 #define isvalue(n)	((n)->ntype == NVALUE)
211 #define isexpr(n)	((n)->ntype == NEXPR)
212 #define isjump(n)	((n)->ctype == OJUMP)
213 #define isexit(n)	((n)->csub == JEXIT)
214 #define	isbreak(n)	((n)->csub == JBREAK)
215 #define	iscont(n)	((n)->csub == JCONT)
216 #define	isnext(n)	((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
217 #define	isret(n)	((n)->csub == JRET)
218 #define isrec(n)	((n)->tval & REC)
219 #define isfld(n)	((n)->tval & FLD)
220 #define isstr(n)	((n)->tval & STR)
221 #define isnum(n)	((n)->tval & NUM)
222 #define isarr(n)	((n)->tval & ARR)
223 #define isfcn(n)	((n)->tval & FCN)
224 #define istrue(n)	((n)->csub == BTRUE)
225 #define istemp(n)	((n)->csub == CTEMP)
226 #define	isargument(n)	((n)->nobj == ARG)
227 /* #define freeable(p)	(!((p)->tval & DONTFREE)) */
228 #define freeable(p)	( ((p)->tval & (STR|DONTFREE)) == STR )
229 
230 /* structures used by regular expression matching machinery, mostly b.c: */
231 
232 #define NCHARS	(1256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
233 				/* BUG: some overflows (caught) if we use 256 */
234 				/* watch out in match(), etc. */
235 #define	HAT	(NCHARS+2)	/* matches ^ in regular expr */
236 #define NSTATES	32
237 
238 typedef struct rrow {
239 	long	ltype;	/* long avoids pointer warnings on 64-bit */
240 	union {
241 		int i;
242 		Node *np;
243 		uschar *up;
244 		int *rp; /* rune representation of char class */
245 	} lval;		/* because Al stores a pointer in it! */
246 	int	*lfollow;
247 } rrow;
248 
249 typedef struct gtte { /* gototab entry */
250 	unsigned int ch;
251 	unsigned int state;
252 } gtte;
253 
254 typedef struct gtt {	/* gototab */
255 	size_t	allocated;
256 	size_t	inuse;
257 	gtte	*entries;
258 } gtt;
259 
260 typedef struct fa {
261 	gtt	*gototab;
262 	uschar	*out;
263 	uschar	*restr;
264 	int	**posns;
265 	int	state_count;
266 	bool	anchor;
267 	int	use;
268 	int	initstat;
269 	int	curstat;
270 	int	accept;
271 	struct	rrow re[1];	/* variable: actual size set by calling malloc */
272 } fa;
273 
274 
275 #include "proto.h"
276