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