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