xref: /titanic_41/usr/src/cmd/awk/awk.h (revision 7c46fb7fef68117215a0c60a64149aaea1a38578)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #ifndef AWK_H
32 #define	AWK_H
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/types.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <libintl.h>
42 #include <limits.h>
43 
44 typedef double	Awkfloat;
45 typedef	unsigned char uchar;
46 
47 #define	xfree(a)	{ if ((a) != NULL) { free(a); a = NULL; } }
48 
49 #define	DEBUG
50 #ifdef	DEBUG
51 			/* uses have to be doubly parenthesized */
52 #define	dprintf(x)	if (dbg) (void) printf x
53 #else
54 #define	dprintf(x)
55 #endif
56 
57 extern	char	errbuf[200];
58 extern	void	error(int, char *);
59 #define	ERROR	(void) snprintf(errbuf, sizeof (errbuf),
60 /*CSTYLED*/
61 #define	FATAL	), error(1, errbuf)
62 /*CSTYLED*/
63 #define	WARNING	), error(0, errbuf)
64 /*CSTYLED*/
65 #define	SYNTAX	), yyerror(errbuf)
66 /*CSTYLED*/
67 #define	CONT	)
68 
69 extern int	compile_time;	/* 1 if compiling, 0 if running */
70 
71 #define	FLD_INCR	64
72 #define	LINE_INCR	256
73 
74 /* ensure that there is extra 1 byte in the buffer */
75 #define	expand_buf(p, n, r)	\
76 	if (*(n) == 0 || (r) >= (*(n) - 1)) r_expand_buf(p, n, r)
77 
78 extern uchar	**FS;
79 extern uchar	**RS;
80 extern uchar	**ORS;
81 extern uchar	**OFS;
82 extern uchar	**OFMT;
83 extern Awkfloat *NR;
84 extern Awkfloat *FNR;
85 extern Awkfloat *NF;
86 extern uchar	**FILENAME;
87 extern uchar	**SUBSEP;
88 extern Awkfloat *RSTART;
89 extern Awkfloat *RLENGTH;
90 
91 extern uchar	*record;
92 extern size_t	record_size;
93 extern int	errorflag;
94 extern int	donefld;	/* 1 if record broken into fields */
95 extern int	donerec;	/* 1 if record is valid (no fld has changed */
96 
97 extern	uchar	*patbeg;	/* beginning of pattern matched */
98 extern	int	patlen;		/* length.  set in b.c */
99 
100 /* Cell:  all information about a variable or constant */
101 
102 typedef struct Cell {
103 	uchar	ctype;		/* OCELL, OBOOL, OJUMP, etc. */
104 	uchar	csub;		/* CCON, CTEMP, CFLD, etc. */
105 	uchar	*nval;		/* name, for variables only */
106 	uchar	*sval;		/* string value */
107 	Awkfloat fval;		/* value as number */
108 	unsigned tval;
109 		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
110 	struct Cell *cnext;	/* ptr to next if chained */
111 } Cell;
112 
113 typedef struct {		/* symbol table array */
114 	int	nelem;		/* elements in table right now */
115 	int	size;		/* size of tab */
116 	Cell	**tab;		/* hash table pointers */
117 } Array;
118 
119 #define	NSYMTAB	50	/* initial size of a symbol table */
120 extern Array	*symtab, *makesymtab(int);
121 extern Cell	*setsymtab(uchar *, uchar *, Awkfloat, unsigned int, Array *);
122 extern Cell	*lookup(uchar *, Array *);
123 
124 extern Cell	*recloc;	/* location of input record */
125 extern Cell	*nrloc;		/* NR */
126 extern Cell	*fnrloc;	/* FNR */
127 extern Cell	*nfloc;		/* NF */
128 extern Cell	*rstartloc;	/* RSTART */
129 extern Cell	*rlengthloc;	/* RLENGTH */
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 
141 #define	freeable(p)	(!((p)->tval & DONTFREE))
142 
143 extern Awkfloat setfval(Cell *, Awkfloat), getfval(Cell *), r_getfval(Cell *);
144 extern uchar	*setsval(Cell *, uchar *), *getsval(Cell *), *r_getsval(Cell *);
145 extern uchar	*tostring(uchar *), *tokname(int), *qstring(uchar *, int);
146 
147 #define	getfval(p)	\
148 	(((p)->tval & (ARR|FLD|REC|NUM)) == NUM ? (p)->fval : r_getfval(p))
149 #define	getsval(p)	\
150 	(((p)->tval & (ARR|FLD|REC|STR)) == STR ? (p)->sval : r_getsval(p))
151 
152 /* function types */
153 #define	FLENGTH	1
154 #define	FSQRT	2
155 #define	FEXP	3
156 #define	FLOG	4
157 #define	FINT	5
158 #define	FSYSTEM	6
159 #define	FRAND	7
160 #define	FSRAND	8
161 #define	FSIN	9
162 #define	FCOS	10
163 #define	FATAN	11
164 #define	FTOUPPER 12
165 #define	FTOLOWER 13
166 
167 /* Node:  parse tree is made of nodes, with Cell's at bottom */
168 
169 typedef struct Node {
170 	int	ntype;
171 	struct	Node *nnext;
172 	off_t lineno;
173 	int	nobj;
174 	struct Node *narg[1];
175 		/* variable: actual size set by calling malloc */
176 } Node;
177 
178 #define	NIL	((Node *)0)
179 
180 extern Node	*winner;
181 extern Node	*nullstat;
182 extern Node	*nullnode;
183 
184 /* ctypes */
185 #define	OCELL	1
186 #define	OBOOL	2
187 #define	OJUMP	3
188 
189 /* Cell subtypes: csub */
190 #define	CFREE	7
191 #define	CCOPY	6
192 #define	CCON	5
193 #define	CTEMP	4
194 #define	CNAME	3
195 #define	CVAR	2
196 #define	CFLD	1
197 
198 /* bool subtypes */
199 #define	BTRUE	11
200 #define	BFALSE	12
201 
202 /* jump subtypes */
203 #define	JEXIT	21
204 #define	JNEXT	22
205 #define	JBREAK	23
206 #define	JCONT	24
207 #define	JRET	25
208 
209 /* node types */
210 #define	NVALUE	1
211 #define	NSTAT	2
212 #define	NEXPR	3
213 #define	NFIELD	4
214 
215 extern	Cell	*(*proctab[])(Node **, int);
216 extern	Cell	*nullproc(Node **, int);
217 extern	int	pairstack[], paircnt;
218 
219 extern	Node	*stat1(int, Node *), *stat2(int, Node *, Node *);
220 extern	Node	*stat3(int, Node *, Node *, Node *);
221 extern	Node	*stat4(int, Node *, Node *, Node *, Node *);
222 extern	Node	*pa2stat(Node *, Node *, Node *);
223 extern	Node	*op1(int, Node *), *op2(int, Node *, Node *);
224 extern	Node	*op3(int, Node *, Node *, Node *);
225 extern	Node	*op4(int, Node *, Node *, Node *, Node *);
226 extern	Node	*linkum(Node *, Node *), *valtonode(Cell *, int);
227 extern	Node	*rectonode(void), *exptostat(Node *);
228 extern	Node	*makearr(Node *);
229 
230 #define	notlegal(n)	\
231 	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
232 #define	isvalue(n)	((n)->ntype == NVALUE)
233 #define	isexpr(n)	((n)->ntype == NEXPR)
234 #define	isjump(n)	((n)->ctype == OJUMP)
235 #define	isexit(n)	((n)->csub == JEXIT)
236 #define	isbreak(n)	((n)->csub == JBREAK)
237 #define	iscont(n)	((n)->csub == JCONT)
238 #define	isnext(n)	((n)->csub == JNEXT)
239 #define	isret(n)	((n)->csub == JRET)
240 #define	isstr(n)	((n)->tval & STR)
241 #define	isnum(n)	((n)->tval & NUM)
242 #define	isarr(n)	((n)->tval & ARR)
243 #define	isfunc(n)	((n)->tval & FCN)
244 #define	istrue(n)	((n)->csub == BTRUE)
245 #define	istemp(n)	((n)->csub == CTEMP)
246 
247 #define	NCHARS	(256+1)
248 #define	NSTATES	32
249 
250 typedef struct rrow {
251 	int	ltype;
252 	int	lval;
253 	int	*lfollow;
254 } rrow;
255 
256 typedef struct fa {
257 	uchar	*restr;
258 	int	anchor;
259 	int	use;
260 	uchar	gototab[NSTATES][NCHARS];
261 	int	*posns[NSTATES];
262 	uchar	out[NSTATES];
263 	int	initstat;
264 	int	curstat;
265 	int	accept;
266 	int	reset;
267 	struct	rrow re[1];
268 } fa;
269 
270 /* b.c */
271 extern	fa	*makedfa(uchar *, int);
272 extern	int	nematch(fa *, uchar *);
273 extern	int	match(fa *, uchar *);
274 extern	int	pmatch(fa *, uchar *);
275 
276 /* lib.c */
277 extern	int	isclvar(uchar *);
278 extern	int	is_number(uchar *);
279 extern	void	setclvar(uchar *);
280 extern	int	readrec(uchar **, size_t *, FILE *);
281 extern	void	bracecheck(void);
282 extern	void	syminit(void);
283 extern	void	yyerror(char *);
284 extern	void	fldbld(void);
285 extern	void	recbld(void);
286 extern	int	getrec(uchar **, size_t *);
287 extern	Cell	*fieldadr(int);
288 extern	void	newfld(int);
289 extern	Cell	*getfld(int);
290 extern	int	fldidx(Cell *);
291 extern	double	errcheck(double, char *);
292 extern	void	fpecatch(int);
293 extern	void	init_buf(uchar **, size_t *, size_t);
294 extern	void	adjust_buf(uchar **, size_t);
295 extern	void	r_expand_buf(uchar **, size_t *, size_t);
296 
297 extern	int	donefld;
298 extern	int	donerec;
299 extern	uchar	*recdata;
300 extern	uchar	*record;
301 extern	size_t	record_size;
302 
303 /* main.c */
304 extern	int	dbg;
305 extern	uchar	*cmdname;
306 extern	uchar	*lexprog;
307 extern	int	compile_time;
308 extern	char	radixpoint;
309 
310 /* tran.c */
311 extern	void	syminit(void);
312 extern	void	arginit(int, uchar **);
313 extern	void	envinit(uchar **);
314 extern	void	freesymtab(Cell *);
315 extern	void	freeelem(Cell *, uchar *);
316 extern	void	funnyvar(Cell *, char *);
317 extern	int	hash(uchar *, int);
318 extern	Awkfloat *ARGC;
319 
320 /* run.c */
321 extern	void	run(Node *);
322 
323 extern	int	paircnt;
324 extern	Node	*winner;
325 
326 #ifndef input
327 extern	int	input(void);
328 #endif
329 extern	int	yyparse(void);
330 extern	FILE	*yyin;
331 extern	off_t	lineno;
332 
333 /* proc */
334 extern Cell *nullproc(Node **, int);
335 extern Cell *program(Node **, int);
336 extern Cell *boolop(Node **, int);
337 extern Cell *relop(Node **, int);
338 extern Cell *array(Node **, int);
339 extern Cell *indirect(Node **, int);
340 extern Cell *substr(Node **, int);
341 extern Cell *sub(Node **, int);
342 extern Cell *gsub(Node **, int);
343 extern Cell *sindex(Node **, int);
344 extern Cell *asprintf(Node **, int);
345 extern Cell *arith(Node **, int);
346 extern Cell *incrdecr(Node **, int);
347 extern Cell *cat(Node **, int);
348 extern Cell *pastat(Node **, int);
349 extern Cell *dopa2(Node **, int);
350 extern Cell *matchop(Node **, int);
351 extern Cell *intest(Node **, int);
352 extern Cell *aprintf(Node **, int);
353 extern Cell *print(Node **, int);
354 extern Cell *closefile(Node **, int);
355 extern Cell *delete(Node **, int);
356 extern Cell *split(Node **, int);
357 extern Cell *assign(Node **, int);
358 extern Cell *condexpr(Node **, int);
359 extern Cell *ifstat(Node **, int);
360 extern Cell *whilestat(Node **, int);
361 extern Cell *forstat(Node **, int);
362 extern Cell *dostat(Node **, int);
363 extern Cell *instat(Node **, int);
364 extern Cell *jump(Node **, int);
365 extern Cell *bltin(Node **, int);
366 extern Cell *call(Node **, int);
367 extern Cell *arg(Node **, int);
368 extern Cell *getnf(Node **, int);
369 extern Cell *getline(Node **, int);
370 
371 #endif /* AWK_H */
372