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