xref: /onnv-gate/usr/src/cmd/awk/awk.h (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * Copyright (c) 1996, 2001 by Sun Microsystems, Inc.
27*0Sstevel@tonic-gate  * All rights reserved.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 2.13	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <limits.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate typedef double	Awkfloat;
36*0Sstevel@tonic-gate typedef	unsigned char uchar;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #define	xfree(a)	{ if ((a) != NULL) { free(a); a = NULL; } }
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #define	DEBUG
41*0Sstevel@tonic-gate #ifdef	DEBUG
42*0Sstevel@tonic-gate 			/* uses have to be doubly parenthesized */
43*0Sstevel@tonic-gate #	define	dprintf(x)	if (dbg) printf x
44*0Sstevel@tonic-gate #else
45*0Sstevel@tonic-gate #	define	dprintf(x)
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate extern	char	errbuf[200];
49*0Sstevel@tonic-gate #define	ERROR	sprintf(errbuf,
50*0Sstevel@tonic-gate #define	FATAL	), error(1, errbuf)
51*0Sstevel@tonic-gate #define	WARNING	), error(0, errbuf)
52*0Sstevel@tonic-gate #define	SYNTAX	), yyerror(errbuf)
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate extern int	compile_time;	/* 1 if compiling, 0 if running */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /* The standards (SUSV2) requires that Record size be atleast LINE_MAX.
57*0Sstevel@tonic-gate  * LINE_MAX is standard variable defined in limits.h.
58*0Sstevel@tonic-gate  * Though nawk is not standards compliant, we let RECSIZE
59*0Sstevel@tonic-gate  * grow with LINE_MAX instead of magic number 1024.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate #define	RECSIZE	(3 * LINE_MAX)	/* sets limit on records, fields, etc., etc. */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #define	MAXFLD	500
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate extern uchar	**FS;
66*0Sstevel@tonic-gate extern uchar	**RS;
67*0Sstevel@tonic-gate extern uchar	**ORS;
68*0Sstevel@tonic-gate extern uchar	**OFS;
69*0Sstevel@tonic-gate extern uchar	**OFMT;
70*0Sstevel@tonic-gate extern Awkfloat *NR;
71*0Sstevel@tonic-gate extern Awkfloat *FNR;
72*0Sstevel@tonic-gate extern Awkfloat *NF;
73*0Sstevel@tonic-gate extern uchar	**FILENAME;
74*0Sstevel@tonic-gate extern uchar	**SUBSEP;
75*0Sstevel@tonic-gate extern Awkfloat *RSTART;
76*0Sstevel@tonic-gate extern Awkfloat *RLENGTH;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate extern uchar	*record;
79*0Sstevel@tonic-gate extern int	dbg;
80*0Sstevel@tonic-gate extern off_t	lineno;
81*0Sstevel@tonic-gate extern int	errorflag;
82*0Sstevel@tonic-gate extern int	donefld;	/* 1 if record broken into fields */
83*0Sstevel@tonic-gate extern int	donerec;	/* 1 if record is valid (no fld has changed */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate extern uchar	cbuf[RECSIZE];	/* miscellaneous character collection */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate extern	uchar	*patbeg;	/* beginning of pattern matched */
88*0Sstevel@tonic-gate extern	int	patlen;		/* length.  set in b.c */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /* Cell:  all information about a variable or constant */
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate typedef struct Cell {
93*0Sstevel@tonic-gate 	uchar	ctype;		/* OCELL, OBOOL, OJUMP, etc. */
94*0Sstevel@tonic-gate 	uchar	csub;		/* CCON, CTEMP, CFLD, etc. */
95*0Sstevel@tonic-gate 	uchar	*nval;		/* name, for variables only */
96*0Sstevel@tonic-gate 	uchar	*sval;		/* string value */
97*0Sstevel@tonic-gate 	Awkfloat fval;		/* value as number */
98*0Sstevel@tonic-gate 	unsigned tval;		/* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
99*0Sstevel@tonic-gate 	struct Cell *cnext;	/* ptr to next if chained */
100*0Sstevel@tonic-gate } Cell;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate typedef struct {		/* symbol table array */
103*0Sstevel@tonic-gate 	int	nelem;		/* elements in table right now */
104*0Sstevel@tonic-gate 	int	size;		/* size of tab */
105*0Sstevel@tonic-gate 	Cell	**tab;		/* hash table pointers */
106*0Sstevel@tonic-gate } Array;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate #define	NSYMTAB	50	/* initial size of a symbol table */
109*0Sstevel@tonic-gate extern Array	*symtab, *makesymtab();
110*0Sstevel@tonic-gate extern Cell	*setsymtab(), *lookup();
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate extern Cell	*recloc;	/* location of input record */
113*0Sstevel@tonic-gate extern Cell	*nrloc;		/* NR */
114*0Sstevel@tonic-gate extern Cell	*fnrloc;	/* FNR */
115*0Sstevel@tonic-gate extern Cell	*nfloc;		/* NF */
116*0Sstevel@tonic-gate extern Cell	*rstartloc;	/* RSTART */
117*0Sstevel@tonic-gate extern Cell	*rlengthloc;	/* RLENGTH */
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /* Cell.tval values: */
120*0Sstevel@tonic-gate #define	NUM	01	/* number value is valid */
121*0Sstevel@tonic-gate #define	STR	02	/* string value is valid */
122*0Sstevel@tonic-gate #define DONTFREE 04	/* string space is not freeable */
123*0Sstevel@tonic-gate #define	CON	010	/* this is a constant */
124*0Sstevel@tonic-gate #define	ARR	020	/* this is an array */
125*0Sstevel@tonic-gate #define	FCN	040	/* this is a function name */
126*0Sstevel@tonic-gate #define FLD	0100	/* this is a field $1, $2, ... */
127*0Sstevel@tonic-gate #define	REC	0200	/* this is $0 */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate #define freeable(p)	(!((p)->tval & DONTFREE))
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate Awkfloat setfval(), getfval();
132*0Sstevel@tonic-gate uchar	*setsval(), *getsval();
133*0Sstevel@tonic-gate uchar	*tostring(), *tokname(), *qstring();
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate double	log(), sqrt(), exp(), atof();
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /* function types */
138*0Sstevel@tonic-gate #define	FLENGTH	1
139*0Sstevel@tonic-gate #define	FSQRT	2
140*0Sstevel@tonic-gate #define	FEXP	3
141*0Sstevel@tonic-gate #define	FLOG	4
142*0Sstevel@tonic-gate #define	FINT	5
143*0Sstevel@tonic-gate #define	FSYSTEM	6
144*0Sstevel@tonic-gate #define	FRAND	7
145*0Sstevel@tonic-gate #define	FSRAND	8
146*0Sstevel@tonic-gate #define	FSIN	9
147*0Sstevel@tonic-gate #define	FCOS	10
148*0Sstevel@tonic-gate #define	FATAN	11
149*0Sstevel@tonic-gate #define	FTOUPPER 12
150*0Sstevel@tonic-gate #define	FTOLOWER 13
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /* Node:  parse tree is made of nodes, with Cell's at bottom */
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate typedef struct Node {
155*0Sstevel@tonic-gate 	int	ntype;
156*0Sstevel@tonic-gate 	struct	Node *nnext;
157*0Sstevel@tonic-gate 	off_t lineno;
158*0Sstevel@tonic-gate 	int	nobj;
159*0Sstevel@tonic-gate 	struct Node *narg[1];	/* variable: actual size set by calling malloc */
160*0Sstevel@tonic-gate } Node;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate #define	NIL	((Node *) 0)
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate extern Node	*winner;
165*0Sstevel@tonic-gate extern Node	*nullstat;
166*0Sstevel@tonic-gate extern Node	*nullnode;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /* ctypes */
169*0Sstevel@tonic-gate #define OCELL	1
170*0Sstevel@tonic-gate #define OBOOL	2
171*0Sstevel@tonic-gate #define OJUMP	3
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /* Cell subtypes: csub */
174*0Sstevel@tonic-gate #define	CFREE	7
175*0Sstevel@tonic-gate #define CCOPY	6
176*0Sstevel@tonic-gate #define CCON	5
177*0Sstevel@tonic-gate #define CTEMP	4
178*0Sstevel@tonic-gate #define CNAME	3
179*0Sstevel@tonic-gate #define CVAR	2
180*0Sstevel@tonic-gate #define CFLD	1
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /* bool subtypes */
183*0Sstevel@tonic-gate #define BTRUE	11
184*0Sstevel@tonic-gate #define BFALSE	12
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /* jump subtypes */
187*0Sstevel@tonic-gate #define JEXIT	21
188*0Sstevel@tonic-gate #define JNEXT	22
189*0Sstevel@tonic-gate #define	JBREAK	23
190*0Sstevel@tonic-gate #define	JCONT	24
191*0Sstevel@tonic-gate #define	JRET	25
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /* node types */
194*0Sstevel@tonic-gate #define NVALUE	1
195*0Sstevel@tonic-gate #define NSTAT	2
196*0Sstevel@tonic-gate #define NEXPR	3
197*0Sstevel@tonic-gate #define	NFIELD	4
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate extern	Cell	*(*proctab[])();
200*0Sstevel@tonic-gate extern	Cell	*nullproc();
201*0Sstevel@tonic-gate extern	int	pairstack[], paircnt;
202*0Sstevel@tonic-gate extern	Cell	*fieldadr();
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate extern	Node	*stat1(), *stat2(), *stat3(), *stat4(), *pa2stat();
205*0Sstevel@tonic-gate extern	Node	*op1(), *op2(), *op3(), *op4();
206*0Sstevel@tonic-gate extern	Node	*linkum(), *valtonode(), *rectonode(), *exptostat();
207*0Sstevel@tonic-gate extern	Node	*makearr();
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate #define notlegal(n)	(n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
210*0Sstevel@tonic-gate #define isvalue(n)	((n)->ntype == NVALUE)
211*0Sstevel@tonic-gate #define isexpr(n)	((n)->ntype == NEXPR)
212*0Sstevel@tonic-gate #define isjump(n)	((n)->ctype == OJUMP)
213*0Sstevel@tonic-gate #define isexit(n)	((n)->csub == JEXIT)
214*0Sstevel@tonic-gate #define	isbreak(n)	((n)->csub == JBREAK)
215*0Sstevel@tonic-gate #define	iscont(n)	((n)->csub == JCONT)
216*0Sstevel@tonic-gate #define	isnext(n)	((n)->csub == JNEXT)
217*0Sstevel@tonic-gate #define	isret(n)	((n)->csub == JRET)
218*0Sstevel@tonic-gate #define isstr(n)	((n)->tval & STR)
219*0Sstevel@tonic-gate #define isnum(n)	((n)->tval & NUM)
220*0Sstevel@tonic-gate #define isarr(n)	((n)->tval & ARR)
221*0Sstevel@tonic-gate #define isfunc(n)	((n)->tval & FCN)
222*0Sstevel@tonic-gate #define istrue(n)	((n)->csub == BTRUE)
223*0Sstevel@tonic-gate #define istemp(n)	((n)->csub == CTEMP)
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate #define NCHARS	(256+1)
226*0Sstevel@tonic-gate #define NSTATES	32
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate typedef struct rrow {
229*0Sstevel@tonic-gate 	int	ltype;
230*0Sstevel@tonic-gate 	int	lval;
231*0Sstevel@tonic-gate 	int	*lfollow;
232*0Sstevel@tonic-gate } rrow;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate typedef struct fa {
235*0Sstevel@tonic-gate 	uchar	*restr;
236*0Sstevel@tonic-gate 	int	anchor;
237*0Sstevel@tonic-gate 	int	use;
238*0Sstevel@tonic-gate 	uchar	gototab[NSTATES][NCHARS];
239*0Sstevel@tonic-gate 	int	*posns[NSTATES];
240*0Sstevel@tonic-gate 	uchar	out[NSTATES];
241*0Sstevel@tonic-gate 	int	initstat;
242*0Sstevel@tonic-gate 	int	curstat;
243*0Sstevel@tonic-gate 	int	accept;
244*0Sstevel@tonic-gate 	int	reset;
245*0Sstevel@tonic-gate 	struct	rrow re[1];
246*0Sstevel@tonic-gate } fa;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate extern	fa	*makedfa();
249