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