xref: /onnv-gate/usr/src/cmd/awk/parse.c (revision 289:9e020dd19eb9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*289Snakanon 
23*289Snakanon /*
24*289Snakanon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*289Snakanon  * Use is subject to license terms.
26*289Snakanon  */
27*289Snakanon 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
31*289Snakanon #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33*289Snakanon #define	DEBUG
340Sstevel@tonic-gate #include "awk.h"
350Sstevel@tonic-gate #include "y.tab.h"
360Sstevel@tonic-gate 
37*289Snakanon Node *
nodealloc(int n)38*289Snakanon nodealloc(int n)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	register Node *x;
41*289Snakanon 
42*289Snakanon 	x = (Node *)malloc(sizeof (Node) + (n - 1) * sizeof (Node *));
430Sstevel@tonic-gate 	if (x == NULL)
440Sstevel@tonic-gate 		ERROR "out of space in nodealloc" FATAL;
450Sstevel@tonic-gate 	x->nnext = NULL;
460Sstevel@tonic-gate 	x->lineno = lineno;
47*289Snakanon 	return (x);
480Sstevel@tonic-gate }
490Sstevel@tonic-gate 
50*289Snakanon Node *
exptostat(Node * a)51*289Snakanon exptostat(Node *a)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	a->ntype = NSTAT;
54*289Snakanon 	return (a);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
57*289Snakanon Node *
node1(int a,Node * b)58*289Snakanon node1(int a, Node *b)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	register Node *x;
61*289Snakanon 
620Sstevel@tonic-gate 	x = nodealloc(1);
630Sstevel@tonic-gate 	x->nobj = a;
64*289Snakanon 	x->narg[0] = b;
65*289Snakanon 	return (x);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
68*289Snakanon Node *
node2(int a,Node * b,Node * c)69*289Snakanon node2(int a, Node *b, Node *c)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	register Node *x;
72*289Snakanon 
730Sstevel@tonic-gate 	x = nodealloc(2);
740Sstevel@tonic-gate 	x->nobj = a;
750Sstevel@tonic-gate 	x->narg[0] = b;
760Sstevel@tonic-gate 	x->narg[1] = c;
77*289Snakanon 	return (x);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
80*289Snakanon Node *
node3(int a,Node * b,Node * c,Node * d)81*289Snakanon node3(int a, Node *b, Node *c, Node *d)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	register Node *x;
84*289Snakanon 
850Sstevel@tonic-gate 	x = nodealloc(3);
860Sstevel@tonic-gate 	x->nobj = a;
870Sstevel@tonic-gate 	x->narg[0] = b;
880Sstevel@tonic-gate 	x->narg[1] = c;
890Sstevel@tonic-gate 	x->narg[2] = d;
90*289Snakanon 	return (x);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
93*289Snakanon Node *
node4(int a,Node * b,Node * c,Node * d,Node * e)94*289Snakanon node4(int a, Node *b, Node *c, Node *d, Node *e)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	register Node *x;
970Sstevel@tonic-gate 	x = nodealloc(4);
980Sstevel@tonic-gate 	x->nobj = a;
990Sstevel@tonic-gate 	x->narg[0] = b;
1000Sstevel@tonic-gate 	x->narg[1] = c;
1010Sstevel@tonic-gate 	x->narg[2] = d;
1020Sstevel@tonic-gate 	x->narg[3] = e;
103*289Snakanon 	return (x);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
106*289Snakanon Node *
stat3(int a,Node * b,Node * c,Node * d)107*289Snakanon stat3(int a, Node *b, Node *c, Node *d)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	register Node *x;
110*289Snakanon 
111*289Snakanon 	x = node3(a, b, c, d);
1120Sstevel@tonic-gate 	x->ntype = NSTAT;
113*289Snakanon 	return (x);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
116*289Snakanon Node *
op2(int a,Node * b,Node * c)117*289Snakanon op2(int a, Node *b, Node *c)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	register Node *x;
120*289Snakanon 
121*289Snakanon 	x = node2(a, b, c);
1220Sstevel@tonic-gate 	x->ntype = NEXPR;
123*289Snakanon 	return (x);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
126*289Snakanon Node *
op1(int a,Node * b)127*289Snakanon op1(int a, Node *b)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	register Node *x;
130*289Snakanon 
131*289Snakanon 	x = node1(a, b);
1320Sstevel@tonic-gate 	x->ntype = NEXPR;
133*289Snakanon 	return (x);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
136*289Snakanon Node *
stat1(int a,Node * b)137*289Snakanon stat1(int a, Node *b)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	register Node *x;
140*289Snakanon 
141*289Snakanon 	x = node1(a, b);
1420Sstevel@tonic-gate 	x->ntype = NSTAT;
143*289Snakanon 	return (x);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
146*289Snakanon Node *
op3(int a,Node * b,Node * c,Node * d)147*289Snakanon op3(int a, Node *b, Node *c, Node *d)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	register Node *x;
150*289Snakanon 
151*289Snakanon 	x = node3(a, b, c, d);
1520Sstevel@tonic-gate 	x->ntype = NEXPR;
153*289Snakanon 	return (x);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
156*289Snakanon Node *
op4(int a,Node * b,Node * c,Node * d,Node * e)157*289Snakanon op4(int a, Node *b, Node *c, Node *d, Node *e)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	register Node *x;
160*289Snakanon 
161*289Snakanon 	x = node4(a, b, c, d, e);
1620Sstevel@tonic-gate 	x->ntype = NEXPR;
163*289Snakanon 	return (x);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
166*289Snakanon Node *
stat2(int a,Node * b,Node * c)167*289Snakanon stat2(int a, Node *b, Node *c)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	register Node *x;
170*289Snakanon 
171*289Snakanon 	x = node2(a, b, c);
1720Sstevel@tonic-gate 	x->ntype = NSTAT;
173*289Snakanon 	return (x);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
176*289Snakanon Node *
stat4(int a,Node * b,Node * c,Node * d,Node * e)177*289Snakanon stat4(int a, Node *b, Node *c, Node *d, Node *e)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	register Node *x;
180*289Snakanon 
181*289Snakanon 	x = node4(a, b, c, d, e);
1820Sstevel@tonic-gate 	x->ntype = NSTAT;
183*289Snakanon 	return (x);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
186*289Snakanon Node *
valtonode(Cell * a,int b)187*289Snakanon valtonode(Cell *a, int b)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	register Node *x;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	a->ctype = OCELL;
1920Sstevel@tonic-gate 	a->csub = b;
193*289Snakanon 	x = node1(0, (Node *)a);
1940Sstevel@tonic-gate 	x->ntype = NVALUE;
195*289Snakanon 	return (x);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
198*289Snakanon Node *
rectonode(void)199*289Snakanon rectonode(void)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	/* return valtonode(lookup("$0", symtab), CFLD); */
202*289Snakanon 	return (valtonode(recloc, CFLD));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
205*289Snakanon Node *
makearr(Node * p)206*289Snakanon makearr(Node *p)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	Cell *cp;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (isvalue(p)) {
211*289Snakanon 		cp = (Cell *)(p->narg[0]);
2120Sstevel@tonic-gate 		if (isfunc(cp))
2130Sstevel@tonic-gate 			ERROR "%s is a function, not an array", cp->nval SYNTAX;
2140Sstevel@tonic-gate 		else if (!isarr(cp)) {
2150Sstevel@tonic-gate 			xfree(cp->sval);
216*289Snakanon 			cp->sval = (uchar *)makesymtab(NSYMTAB);
2170Sstevel@tonic-gate 			cp->tval = ARR;
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 	}
220*289Snakanon 	return (p);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
223*289Snakanon Node *
pa2stat(Node * a,Node * b,Node * c)224*289Snakanon pa2stat(Node *a, Node *b, Node *c)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	register Node *x;
227*289Snakanon 
228*289Snakanon 	x = node4(PASTAT2, a, b, c, (Node *)paircnt);
2290Sstevel@tonic-gate 	paircnt++;
2300Sstevel@tonic-gate 	x->ntype = NSTAT;
231*289Snakanon 	return (x);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
234*289Snakanon Node *
linkum(Node * a,Node * b)235*289Snakanon linkum(Node *a, Node *b)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	register Node *c;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (errorflag)	/* don't link things that are wrong */
240*289Snakanon 		return (a);
241*289Snakanon 	if (a == NULL)
242*289Snakanon 		return (b);
243*289Snakanon 	else if (b == NULL)
244*289Snakanon 		return (a);
2450Sstevel@tonic-gate 	for (c = a; c->nnext != NULL; c = c->nnext)
2460Sstevel@tonic-gate 		;
2470Sstevel@tonic-gate 	c->nnext = b;
248*289Snakanon 	return (a);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
251*289Snakanon void
defn(Cell * v,Node * vl,Node * st)252*289Snakanon defn(Cell *v, Node *vl, Node *st)	/* turn on FCN bit in definition */
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	Node *p;
2550Sstevel@tonic-gate 	int n;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (isarr(v)) {
258*289Snakanon 		ERROR "`%s' is an array name and a function name",
259*289Snakanon 		    v->nval SYNTAX;
2600Sstevel@tonic-gate 		return;
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	v->tval = FCN;
263*289Snakanon 	v->sval = (uchar *)st;
2640Sstevel@tonic-gate 	n = 0;	/* count arguments */
2650Sstevel@tonic-gate 	for (p = vl; p; p = p->nnext)
2660Sstevel@tonic-gate 		n++;
2670Sstevel@tonic-gate 	v->fval = n;
268*289Snakanon 	dprintf(("defining func %s (%d args)\n", v->nval, n));
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
271*289Snakanon int
isarg(uchar * s)272*289Snakanon isarg(uchar *s)	/* is s in argument list for current function? */
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	extern Node *arglist;
2750Sstevel@tonic-gate 	Node *p = arglist;
2760Sstevel@tonic-gate 	int n;
2770Sstevel@tonic-gate 
278*289Snakanon 	for (n = 0; p != 0; p = p->nnext, n++) {
279*289Snakanon 		if (strcmp((char *)((Cell *)(p->narg[0]))->nval,
280*289Snakanon 		    (char *)s) == 0) {
281*289Snakanon 			return (n);
282*289Snakanon 		}
283*289Snakanon 	}
284*289Snakanon 	return (-1);
2850Sstevel@tonic-gate }
286