xref: /openbsd-src/usr.bin/awk/awk.h (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: awk.h,v 1.27 2020/08/28 16:29:16 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)	{ if ((a) != NULL) { 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 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 char	*record;	/* points to $0 */
84 extern int	lineno;		/* line number in awk program */
85 extern int	errorflag;	/* 1 if error has occurred */
86 extern bool	donefld;	/* true if record broken into fields */
87 extern bool	donerec;	/* true if record is valid (no fld has changed */
88 extern int	dbg;
89 
90 extern const char *patbeg;	/* beginning of pattern matched */
91 extern	int	patlen;		/* length of pattern matched.  set in b.c */
92 
93 /* Cell:  all information about a variable or constant */
94 
95 typedef struct Cell {
96 	uschar	ctype;		/* OCELL, OBOOL, OJUMP, etc. */
97 	uschar	csub;		/* CCON, CTEMP, CFLD, etc. */
98 	char	*nval;		/* name, for variables only */
99 	char	*sval;		/* string value */
100 	Awkfloat fval;		/* value as number */
101 	int	 tval;		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
102 	char	*fmt;		/* CONVFMT/OFMT value used to convert from number */
103 	struct Cell *cnext;	/* ptr to next if chained */
104 } Cell;
105 
106 typedef struct Array {		/* symbol table array */
107 	int	nelem;		/* elements in table right now */
108 	int	size;		/* size of tab */
109 	Cell	**tab;		/* hash table pointers */
110 } Array;
111 
112 #define	NSYMTAB	50	/* initial size of a symbol table */
113 extern Array	*symtab;
114 
115 extern Cell	*nrloc;		/* NR */
116 extern Cell	*fnrloc;	/* FNR */
117 extern Cell	*fsloc;		/* FS */
118 extern Cell	*nfloc;		/* NF */
119 extern Cell	*ofsloc;	/* OFS */
120 extern Cell	*orsloc;	/* ORS */
121 extern Cell	*rsloc;		/* RS */
122 extern Cell	*rstartloc;	/* RSTART */
123 extern Cell	*rlengthloc;	/* RLENGTH */
124 extern Cell	*subseploc;	/* SUBSEP */
125 extern Cell	*symtabloc;	/* SYMTAB */
126 
127 /* Cell.tval values: */
128 #define	NUM	01	/* number value is valid */
129 #define	STR	02	/* string value is valid */
130 #define DONTFREE 04	/* string space is not freeable */
131 #define	CON	010	/* this is a constant */
132 #define	ARR	020	/* this is an array */
133 #define	FCN	040	/* this is a function name */
134 #define FLD	0100	/* this is a field $1, $2, ... */
135 #define	REC	0200	/* this is $0 */
136 #define CONVC	0400	/* string was converted from number via CONVFMT */
137 #define CONVO	01000	/* string was converted from number via OFMT */
138 
139 
140 /* function types */
141 #define	FLENGTH	1
142 #define	FSQRT	2
143 #define	FEXP	3
144 #define	FLOG	4
145 #define	FINT	5
146 #define	FSYSTEM	6
147 #define	FRAND	7
148 #define	FSRAND	8
149 #define	FSIN	9
150 #define	FCOS	10
151 #define	FATAN	11
152 #define	FTOUPPER 12
153 #define	FTOLOWER 13
154 #define	FFLUSH	14
155 #define FAND	15
156 #define FFOR	16
157 #define FXOR	17
158 #define FCOMPL	18
159 #define FLSHIFT	19
160 #define FRSHIFT	20
161 #define FSYSTIME	21
162 #define FSTRFTIME	22
163 #define FMKTIME	23
164 
165 /* Node:  parse tree is made of nodes, with Cell's at bottom */
166 
167 typedef struct Node {
168 	int	ntype;
169 	struct	Node *nnext;
170 	int	lineno;
171 	int	nobj;
172 	struct	Node *narg[1];	/* variable: actual size set by calling malloc */
173 } Node;
174 
175 #define	NIL	((Node *) 0)
176 
177 extern Node	*winner;
178 extern Node	*nullstat;
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	(256+3)		/* 256 handles 8-bit chars; 128 does 7-bit */
240 				/* watch out in match(), etc. */
241 #define	HAT	(NCHARS+2)	/* matches ^ in regular expr */
242 #define NSTATES	32
243 
244 typedef struct rrow {
245 	long	ltype;	/* long avoids pointer warnings on 64-bit */
246 	union {
247 		int i;
248 		Node *np;
249 		uschar *up;
250 	} lval;		/* because Al stores a pointer in it! */
251 	int	*lfollow;
252 } rrow;
253 
254 typedef struct fa {
255 	unsigned int	**gototab;
256 	uschar	*out;
257 	uschar	*restr;
258 	int	**posns;
259 	int	state_count;
260 	bool	anchor;
261 	int	use;
262 	int	initstat;
263 	int	curstat;
264 	int	accept;
265 	struct	rrow re[1];	/* variable: actual size set by calling malloc */
266 } fa;
267 
268 
269 #include "proto.h"
270