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