1 /*
2    Changes by Gunnar Ritter, Freiburg i. Br., Germany, December 2002.
3 
4    Sccsid @(#)awk.h	1.23 (gritter) 12/25/04>
5  */
6 /* UNIX(R) Regular Expression Tools
7 
8    Copyright (C) 2001 Caldera International, Inc.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to:
22        Free Software Foundation, Inc.
23        59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25 /*	copyright	"%c%"	*/
26 
27 /*	from unixsrc:usr/src/common/cmd/awk/awk.h /main/uw7_nj/1	*/
28 /*	from RCS Header: awk.h 1.2 91/06/25 	*/
29 
30 typedef double	Awkfloat;
31 
32 #define	xfree(a)	{ if ((a) != NULL) { free(a); a = NULL; } }
33 #define MAXLABEL 25
34 
35 extern const char	version[];
36 
37 extern	char	errbuf[200];
38 #define	ERROR	snprintf(errbuf, sizeof errbuf,
39 #define	FATAL	), error(1, errbuf)
40 #define	WARNING	), error(0, errbuf)
41 #define	SYNTAX	), yyerror(errbuf)
42 
43 extern int	compile_time;	/* 1 if compiling, 0 if running */
44 
45 extern int	posix;		/* if POSIX behavior is desired */
46 
47 /*
48  * This is done to prevent redefinition of our own definitions for FS with
49  * those defined in the system's header files. Same of RS (on HP-UX/PA-RISC).
50  */
51 #undef FS
52 #undef RS
53 
54 extern unsigned char	**FS;
55 extern unsigned char	**RS;
56 extern unsigned char	**ORS;
57 extern unsigned char	**OFS;
58 extern unsigned char	**OFMT;
59 extern unsigned char	**CONVFMT;
60 extern Awkfloat *NR;
61 extern Awkfloat *FNR;
62 extern Awkfloat *NF;
63 extern unsigned char	**FILENAME;
64 extern unsigned char	**SUBSEP;
65 extern Awkfloat *RSTART;
66 extern Awkfloat *RLENGTH;
67 
68 #define	CHUNK	512	/* record and string increment */
69 
70 extern unsigned char	*record;
71 extern int	recsize;
72 extern int	dbg;
73 extern int	lineno;
74 extern int	errorflag;
75 extern int	donefld;	/* 1 if record broken into fields */
76 extern int	donerec;	/* 1 if record is valid (no fld has changed */
77 
78 #define	CBUFLEN	5120
79 extern unsigned char	cbuf[CBUFLEN];	/* miscellaneous character collection */
80 
81 extern	unsigned char	*patbeg;	/* beginning of pattern matched */
82 extern	int	patlen;		/* length.  set in b.c */
83 
84 extern	int	mb_cur_max;	/* MB_CUR_MAX, for acceleration purposes */
85 
86 extern const char outofspace[];	/* message */
87 
88 /* Cell:  all information about a variable or constant */
89 
90 typedef struct Cell {
91 	unsigned char	ctype;		/* OCELL, OBOOL, OJUMP, etc. */
92 	unsigned char	csub;		/* CCON, CTEMP, CFLD, etc. */
93 	unsigned char	*nval;		/* name, for variables only */
94 	unsigned char	*sval;		/* string value */
95 	Awkfloat fval;		/* value as number */
96 	unsigned tval;		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
97 	struct Cell *cnext;	/* ptr to next if chained */
98 } Cell;
99 
100 typedef struct {		/* symbol table array */
101 	int	nelem;		/* elements in table right now */
102 	int	size;		/* size of tab */
103 	Cell	**tab;		/* hash table pointers */
104 } Array;
105 
106 #define	NSYMTAB	50	/* initial size of a symbol table */
107 extern Array	*symtab, *makesymtab(int);
108 #define	setsymtab(n, s, f, t, tp)	ssetsymtab((unsigned char *)n, \
109 						(unsigned char *)s, \
110 						f, t, tp)
111 extern Cell	*ssetsymtab(unsigned char *, unsigned char *, Awkfloat,
112 			unsigned, Array *);
113 #define	lookup(s, tp)	slookup((unsigned char *)s, tp)
114 extern Cell	*slookup(unsigned char *, Array *);
115 
116 extern Cell	*recloc;	/* location of input record */
117 extern Cell	*nrloc;		/* NR */
118 extern Cell	*fnrloc;	/* FNR */
119 extern Cell	*fsloc;		/* FS */
120 extern Cell	*nfloc;		/* NF */
121 extern Cell	*rstartloc;	/* RSTART */
122 extern Cell	*rlengthloc;	/* RLENGTH */
123 
124 /* Cell.tval values: */
125 #define	NUM	01	/* number value is valid */
126 #define	STR	02	/* string value is valid */
127 #define DONTFREE 04	/* string space is not freeable */
128 #define	CON	010	/* this is a constant */
129 #define	ARR	020	/* this is an array */
130 #define	FCN	040	/* this is a function name */
131 #define FLD	0100	/* this is a field $1, $2, ... */
132 #define	REC	0200	/* this is $0 */
133 #define CANBENUM 0400	/* tells setsymtab() to try for NUM, too */
134 
135 #define freeable(p)	(!((p)->tval & DONTFREE))
136 
137 #include <stdlib.h>
138 #include <stdio.h>
139 #include <string.h>
140 
141 #ifdef	__GLIBC__
142 #ifdef	_IO_getc_unlocked
143 #undef	getc
144 #define	getc(c)	_IO_getc_unlocked(c)
145 #endif	/* _IO_getc_unlocked */
146 #endif	/* __GLIBC__ */
147 
148 #define	getline	xxgetline	/* avoid glibc _GNU_SOURCE collision */
149 
150 #define	DEBUG
151 #ifdef	DEBUG
152 			/* uses have to be doubly parenthesized */
153 #	define	dprintf(x)	if (dbg) printf x
154 #else
155 #	define	dprintf(x)
156 #endif
157 
158 #ifndef	IN_MAKETAB
159 #include <wchar.h>
160 
161 /*
162  * Get next character from string s and store it in wc; n is set to
163  * the length of the corresponding byte sequence.
164  */
165 #define	next(wc, s, n) (mb_cur_max > 1 && *(s) & 0200 ? \
166 			((n) = mbtowc(&(wc), (char *)(s), mb_cur_max), \
167 			 (n) = ((n) > 0 ? (n) : (n) < 0 ? (wc=WEOF, 1) : 1)) :\
168 		((wc) = *(s), (n) = 1))
169 #endif	/* !IN_MAKETAB */
170 
171 /* function types */
172 #define	FLENGTH	1
173 #define	FSQRT	2
174 #define	FEXP	3
175 #define	FLOG	4
176 #define	FINT	5
177 #define	FSYSTEM	6
178 #define	FRAND	7
179 #define	FSRAND	8
180 #define	FSIN	9
181 #define	FCOS	10
182 #define	FATAN	11
183 #define	FTOUPPER 12
184 #define	FTOLOWER 13
185 #define FCLOSE	14
186 
187 /* Node:  parse tree is made of nodes, with Cell's at bottom */
188 
189 typedef struct Node {
190 	int	ntype;
191 	struct	Node *nnext;
192 	int	lineno;
193 	int	nobj;
194 	struct Node *narg[1];	/* variable: actual size set by calling malloc */
195 } Node;
196 
197 #define	NIL	((Node *) 0)
198 
199 extern Node	*winner;
200 extern Node	*nullstat;
201 extern Node	*nullnode;
202 
203 /* ctypes */
204 #define OCELL	1
205 #define OBOOL	2
206 #define OJUMP	3
207 
208 /* Cell subtypes: csub */
209 #define	CFREE	7
210 #define CCOPY	6
211 #define CCON	5
212 #define CTEMP	4
213 #define CNAME	3
214 #define CVAR	2
215 #define CFLD	1
216 
217 /* bool subtypes */
218 #define BTRUE	11
219 #define BFALSE	12
220 
221 /* jump subtypes */
222 #define JEXIT	21
223 #define JNEXT	22
224 #define	JBREAK	23
225 #define	JCONT	24
226 #define	JRET	25
227 
228 /* node types */
229 #define NVALUE	1
230 #define NSTAT	2
231 #define NEXPR	3
232 #define	NFIELD	4
233 
234 extern	Cell	*(*proctab[])(Node **, int);
235 extern	int	pairstack[];
236 extern	long	paircnt;
237 
238 #define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
239 #define isvalue(n)	((n)->ntype == NVALUE)
240 #define isexpr(n)	((n)->ntype == NEXPR)
241 #define isjump(n)	((n)->ctype == OJUMP)
242 #define isexit(n)	((n)->csub == JEXIT)
243 #define	isbreak(n)	((n)->csub == JBREAK)
244 #define	iscont(n)	((n)->csub == JCONT)
245 #define	isnext(n)	((n)->csub == JNEXT)
246 #define	isret(n)	((n)->csub == JRET)
247 #define isstr(n)	((n)->tval & STR)
248 #define isnum(n)	((n)->tval & NUM)
249 #define isarr(n)	((n)->tval & ARR)
250 #define isfunc(n)	((n)->tval & FCN)
251 #define istrue(n)	((n)->csub == BTRUE)
252 #define istemp(n)	((n)->csub == CTEMP)
253 
254 #include <regex.h>
255 
256 typedef struct fa {
257 	unsigned char	*restr;
258 	int	use;
259 	int	notbol;
260 	regex_t	re;
261 } fa;
262 
263 /* awk.g.c */
264 extern int	yywrap(void);
265 extern int	yyparse(void);
266 /* awk.lx.c */
267 extern int	yylex(void);
268 extern void	startreg(void);
269 extern int	awk_input(void);
270 /* b.c */
271 extern fa	*makedfa(unsigned char *, int);
272 extern int	match(void *, unsigned char *);
273 extern int	pmatch(void *, unsigned char *);
274 extern int	nematch(void *, unsigned char *);
275 /* lib.c */
276 extern void	fldinit(void);
277 extern void	initgetrec(void);
278 extern int	getrec(unsigned char **, int *);
279 extern int	readrec(unsigned char **, int *, FILE *);
280 extern unsigned char	*getargv(int);
281 extern void	setclvar(unsigned char *);
282 extern void	fldbld(void);
283 extern void	newfld(int);
284 extern void	recbld(void);
285 extern Cell	*fieldadr(int);
286 extern void	vyyerror(const char *, ...);
287 extern void	yyerror(char *);
288 extern void	fpecatch(int);
289 extern void	bracecheck(void);
290 extern void	error(int, const char *, ...);
291 extern void	bclass(int);
292 extern double	errcheck(double, unsigned char *);
293 extern void	PUTS(unsigned char *);
294 extern int	isclvar(unsigned char *);
295 extern int	is2number(unsigned char *, Cell *);
296 extern double	awk_atof(const char *);
297 extern unsigned char	*makerec(const unsigned char *, int);
298 /* main.c */
299 extern int	pgetc(void);
300 /* parse.c */
301 extern Node	*nodealloc(int);
302 extern Node	*exptostat(Node *);
303 extern Node	*node1(int, Node *);
304 extern Node	*node2(int, Node *, Node *);
305 extern Node	*node3(int, Node *, Node *, Node *);
306 extern Node	*node4(int, Node *, Node *, Node *, Node *);
307 extern Node	*stat3(int, Node *, Node *, Node *);
308 extern Node	*op2(int, Node *, Node *);
309 extern Node	*op1(int, Node *);
310 extern Node	*stat1(int, Node *);
311 extern Node	*op3(int, Node *, Node *, Node *);
312 extern Node	*op4(int, Node *, Node *, Node *, Node *);
313 extern Node	*stat2(int, Node *, Node *);
314 extern Node	*stat4(int, Node *, Node *, Node *, Node *);
315 extern Node	*valtonode(Cell *, int);
316 extern Node	*rectonode(void);
317 extern Node	*makearr(Node *);
318 extern Node	*pa2stat(Node *, Node *, Node *);
319 extern Node	*linkum(Node *, Node *);
320 extern void	defn(Cell *, Node *, Node *);
321 extern int	isarg(const char *);
322 /* proctab.c */
323 extern unsigned char	*tokname(int);
324 /* run.c */
325 extern int	run(Node *);
326 extern Cell	*r_execute(Node *);
327 extern Cell	*program(Node **, int);
328 extern Cell	*call(Node **, int);
329 extern Cell	*copycell(Cell *);
330 extern Cell	*arg(Node **, int);
331 extern Cell	*jump(Node **, int);
332 extern Cell	*getline(Node **, int);
333 extern Cell	*getnf(Node **, int);
334 extern Cell	*array(Node **, int);
335 extern Cell	*delete(Node **, int);
336 extern Cell	*intest(Node **, int);
337 extern Cell	*matchop(Node **, int);
338 extern Cell	*boolop(Node **, int);
339 extern Cell	*relop(Node **, int);
340 extern Cell	*gettemp(const char *);
341 extern Cell	*indirect(Node **, int);
342 extern Cell	*substr(Node **, int);
343 extern Cell	*sindex(Node **, int);
344 extern int	format(unsigned char **, int *, const unsigned char *, Node *);
345 extern Cell	*awsprintf(Node **, int);
346 extern Cell	*aprintf(Node **, int);
347 extern Cell	*arith(Node **, int);
348 extern double	ipow(double, int);
349 extern Cell	*incrdecr(Node **, int);
350 extern Cell	*assign(Node **, int);
351 extern Cell	*cat(Node **, int);
352 extern Cell	*pastat(Node **, int);
353 extern Cell	*dopa2(Node **, int);
354 extern Cell	*split(Node **, int);
355 extern Cell	*condexpr(Node **, int);
356 extern Cell	*ifstat(Node **, int);
357 extern Cell	*whilestat(Node **, int);
358 extern Cell	*dostat(Node **, int);
359 extern Cell	*forstat(Node **, int);
360 extern Cell	*instat(Node **, int);
361 extern Cell	*bltin(Node **, int);
362 extern Cell	*print(Node **, int);
363 extern Cell	*nullproc(Node **, int);
364 extern FILE	*redirect(int, Node *);
365 extern FILE	*openfile(int, unsigned char *);
366 extern Cell	*sub(Node **, int);
367 extern Cell	*gsub(Node **, int);
368 extern int	chrlen(const unsigned char *);
369 extern int	chrdist(const unsigned char *, const unsigned char *);
370 /* tran.c */
371 extern void	syminit(void);
372 extern void	arginit(int, unsigned char **);
373 extern void	envinit(unsigned char **);
374 extern Array	*makesymtab(int);
375 extern void	freesymtab(Cell *);
376 extern void	freeelem(Cell *, unsigned char *);
377 extern Cell	*ssetsymtab(unsigned char *, unsigned char *,
378 			Awkfloat, unsigned, Array *);
379 extern Cell	*slookup(unsigned char *, Array *);
380 extern Awkfloat	setfval(Cell *, Awkfloat);
381 extern void	funnyvar(Cell *, char *);
382 extern unsigned char	*setsval(Cell *, unsigned char *);
383 extern Awkfloat	r_getfval(Cell *);
384 extern unsigned char	*r_getsval(Cell *);
385 #define	tostring(s)	stostring((unsigned char *)s)
386 extern unsigned char	*stostring(const unsigned char *);
387 extern unsigned char	*qstring(unsigned char *, int);
388