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