xref: /illumos-gate/usr/src/cmd/awk/parse.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * Copyright (C) Lucent Technologies 1997
3  * All Rights Reserved
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby
7  * granted, provided that the above copyright notice appear in all
8  * copies and that both that the copyright notice and this
9  * permission notice and warranty disclaimer appear in supporting
10  * documentation, and that the name Lucent Technologies or any of
11  * its entities not be used in advertising or publicity pertaining
12  * to distribution of the software without specific, written prior
13  * permission.
14  *
15  * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17  * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22  * THIS SOFTWARE.
23  */
24 
25 /*
26  * CDDL HEADER START
27  *
28  * The contents of this file are subject to the terms of the
29  * Common Development and Distribution License, Version 1.0 only
30  * (the "License").  You may not use this file except in compliance
31  * with the License.
32  *
33  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
34  * or http://www.opensolaris.org/os/licensing.
35  * See the License for the specific language governing permissions
36  * and limitations under the License.
37  *
38  * When distributing Covered Code, include this CDDL HEADER in each
39  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
40  * If applicable, add the following below this CDDL HEADER, with the
41  * fields enclosed by brackets "[]" replaced with your own identifying
42  * information: Portions Copyright [yyyy] [name of copyright owner]
43  *
44  * CDDL HEADER END
45  */
46 
47 /*
48  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
49  * Use is subject to license terms.
50  */
51 
52 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
53 /*	  All Rights Reserved  	*/
54 
55 #define	DEBUG
56 #include "awk.h"
57 #include "y.tab.h"
58 
59 Node *
60 nodealloc(int n)
61 {
62 	Node *x;
63 
64 	x = (Node *)malloc(sizeof (Node) + (n - 1) * sizeof (Node *));
65 	if (x == NULL)
66 		FATAL("out of space in nodealloc");
67 	x->nnext = NULL;
68 	x->lineno = lineno;
69 	return (x);
70 }
71 
72 Node *
73 exptostat(Node *a)
74 {
75 	a->ntype = NSTAT;
76 	return (a);
77 }
78 
79 Node *
80 node1(int a, Node *b)
81 {
82 	Node *x;
83 
84 	x = nodealloc(1);
85 	x->nobj = a;
86 	x->narg[0] = b;
87 	return (x);
88 }
89 
90 Node *
91 node2(int a, Node *b, Node *c)
92 {
93 	Node *x;
94 
95 	x = nodealloc(2);
96 	x->nobj = a;
97 	x->narg[0] = b;
98 	x->narg[1] = c;
99 	return (x);
100 }
101 
102 Node *
103 node3(int a, Node *b, Node *c, Node *d)
104 {
105 	Node *x;
106 
107 	x = nodealloc(3);
108 	x->nobj = a;
109 	x->narg[0] = b;
110 	x->narg[1] = c;
111 	x->narg[2] = d;
112 	return (x);
113 }
114 
115 Node *
116 node4(int a, Node *b, Node *c, Node *d, Node *e)
117 {
118 	Node *x;
119 
120 	x = nodealloc(4);
121 	x->nobj = a;
122 	x->narg[0] = b;
123 	x->narg[1] = c;
124 	x->narg[2] = d;
125 	x->narg[3] = e;
126 	return (x);
127 }
128 
129 Node *
130 stat1(int a, Node *b)
131 {
132 	Node *x;
133 
134 	x = node1(a, b);
135 	x->ntype = NSTAT;
136 	return (x);
137 }
138 
139 Node *
140 stat2(int a, Node *b, Node *c)
141 {
142 	Node *x;
143 
144 	x = node2(a, b, c);
145 	x->ntype = NSTAT;
146 	return (x);
147 }
148 
149 Node *
150 stat3(int a, Node *b, Node *c, Node *d)
151 {
152 	Node *x;
153 
154 	x = node3(a, b, c, d);
155 	x->ntype = NSTAT;
156 	return (x);
157 }
158 
159 Node *
160 stat4(int a, Node *b, Node *c, Node *d, Node *e)
161 {
162 	Node *x;
163 
164 	x = node4(a, b, c, d, e);
165 	x->ntype = NSTAT;
166 	return (x);
167 }
168 
169 Node *
170 op1(int a, Node *b)
171 {
172 	Node *x;
173 
174 	x = node1(a, b);
175 	x->ntype = NEXPR;
176 	return (x);
177 }
178 
179 Node *
180 op2(int a, Node *b, Node *c)
181 {
182 	Node *x;
183 
184 	x = node2(a, b, c);
185 	x->ntype = NEXPR;
186 	return (x);
187 }
188 
189 Node *
190 op3(int a, Node *b, Node *c, Node *d)
191 {
192 	Node *x;
193 
194 	x = node3(a, b, c, d);
195 	x->ntype = NEXPR;
196 	return (x);
197 }
198 
199 Node *
200 op4(int a, Node *b, Node *c, Node *d, Node *e)
201 {
202 	Node *x;
203 
204 	x = node4(a, b, c, d, e);
205 	x->ntype = NEXPR;
206 	return (x);
207 }
208 
209 Node *
210 celltonode(Cell *a, int b)
211 {
212 	Node *x;
213 
214 	a->ctype = OCELL;
215 	a->csub = b;
216 	x = node1(0, (Node *)a);
217 	x->ntype = NVALUE;
218 	return (x);
219 }
220 
221 Node *
222 rectonode(void)	/* make $0 into a Node */
223 {
224 	extern Cell *literal0;
225 	return (op1(INDIRECT, celltonode(literal0, CUNK)));
226 }
227 
228 Node *
229 makearr(Node *p)
230 {
231 	Cell *cp;
232 
233 	if (isvalue(p)) {
234 		cp = (Cell *)(p->narg[0]);
235 		if (isfcn(cp))
236 			SYNTAX("%s is a function, not an array", cp->nval);
237 		else if (!isarr(cp)) {
238 			xfree(cp->sval);
239 			cp->sval = (char *)makesymtab(NSYMTAB);
240 			cp->tval = ARR;
241 		}
242 	}
243 	return (p);
244 }
245 
246 int	paircnt;	/* number of them in use */
247 int	*pairstack;	/* state of each pat,pat */
248 
249 Node *
250 pa2stat(Node *a, Node *b, Node *c)	/* pat, pat {...} */
251 {
252 	Node *x;
253 
254 	x = node4(PASTAT2, a, b, c, itonp(paircnt));
255 	paircnt++;
256 	x->ntype = NSTAT;
257 	return (x);
258 }
259 
260 Node *
261 linkum(Node *a, Node *b)
262 {
263 	Node *c;
264 
265 	if (errorflag)	/* don't link things that are wrong */
266 		return (a);
267 	if (a == NULL)
268 		return (b);
269 	else if (b == NULL)
270 		return (a);
271 	for (c = a; c->nnext != NULL; c = c->nnext)
272 		;
273 	c->nnext = b;
274 	return (a);
275 }
276 
277 /* turn on FCN bit in definition, */
278 /* body of function, arglist */
279 void
280 defn(Cell *v, Node *vl, Node *st)
281 {
282 	Node *p;
283 	int n;
284 
285 	if (isarr(v)) {
286 		SYNTAX("`%s' is an array name and a function name", v->nval);
287 		return;
288 	}
289 	if (isarg(v->nval) != -1) {
290 		SYNTAX("`%s' is both function name and argument name", v->nval);
291 		return;
292 	}
293 
294 	v->tval = FCN;
295 	v->sval = (char *)st;
296 	n = 0;	/* count arguments */
297 	for (p = vl; p != NULL; p = p->nnext)
298 		n++;
299 	v->fval = n;
300 	dprintf(("defining func %s (%d args)\n", v->nval, n));
301 }
302 
303 /* is s in argument list for current function? */
304 /* return -1 if not, otherwise arg # */
305 int
306 isarg(const char *s)
307 {
308 	extern Node *arglist;
309 	Node *p = arglist;
310 	int n;
311 
312 	for (n = 0; p != NULL; p = p->nnext, n++)
313 		if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
314 			return (n);
315 	return (-1);
316 }
317 
318 int
319 ptoi(void *p)	/* convert pointer to integer */
320 {
321 	return ((int)(long)p);	/* swearing that p fits, of course */
322 }
323 
324 Node *
325 itonp(int i)	/* and vice versa */
326 {
327 	return ((Node *)(long)i);
328 }
329