1 /*-
2  * This code contains changes by
3  *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4  *
5  * Conditions 1, 2, and 4 and the no-warranty notice below apply
6  * to these changes.
7  *
8  *
9  * Copyright (c) 1991
10  * 	The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  * 	This product includes software developed by the University of
23  * 	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *
41  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *   Redistributions of source code and documentation must retain the
47  *    above copyright notice, this list of conditions and the following
48  *    disclaimer.
49  *   Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *   All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed or owned by Caldera
55  *      International, Inc.
56  *   Neither the name of Caldera International, Inc. nor the names of
57  *    other contributors may be used to endorse or promote products
58  *    derived from this software without specific prior written permission.
59  *
60  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
65  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
68  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
70  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
71  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 /*	from 4.4BSD /usr/src/old/awk/parse.c	4.3 (Berkeley) 4/17/91	*/
75 /*	Sccsid @(#)parse.c	1.5 (gritter) 4/21/04>	*/
76 
77 #include "awk.def"
78 #include "awk.h"
79 #include "stdio.h"
ALLOC(int n)80 node *ALLOC(int n)
81 {
82 	register node *x;
83 	x = (node *) malloc(sizeof(node) + (n-1)*sizeof(node *));
84 	if (x == NULL)
85 		error(FATAL, "out of space in ALLOC");
86 	return(x);
87 }
exptostat(node * a)88 node *exptostat(node *a)
89 {
90 	a->ntype = NSTAT;
91 	return(a);
92 }
93 node	*nullstat;
node0(intptr_t a)94 node *node0(intptr_t a)
95 {
96 	register node *x;
97 	x=ALLOC(0);
98 	x->nnext = NULL;
99 	x->nobj=a;
100 	return(x);
101 }
node1(intptr_t a,node * b)102 node *node1(intptr_t a,node *b)
103 {
104 	register node *x;
105 	x=ALLOC(1);
106 	x->nnext = NULL;
107 	x->nobj=a;
108 	x->narg[0]=b;
109 	return(x);
110 }
node2(intptr_t a,node * b,node * c)111 node *node2(intptr_t a,node *b,node *c)
112 {
113 	register node *x;
114 	x = ALLOC(2);
115 	x->nnext = NULL;
116 	x->nobj = a;
117 	x->narg[0] = b;
118 	x->narg[1] = c;
119 	return(x);
120 }
node3(intptr_t a,node * b,node * c,node * d)121 node *node3(intptr_t a,node *b,node *c,node *d)
122 {
123 	register node *x;
124 	x = ALLOC(3);
125 	x->nnext = NULL;
126 	x->nobj = a;
127 	x->narg[0] = b;
128 	x->narg[1] = c;
129 	x->narg[2] = d;
130 	return(x);
131 }
node4(intptr_t a,node * b,node * c,node * d,node * e)132 node *node4(intptr_t a,node *b,node *c,node *d,node *e)
133 {
134 	register node *x;
135 	x = ALLOC(4);
136 	x->nnext = NULL;
137 	x->nobj = a;
138 	x->narg[0] = b;
139 	x->narg[1] = c;
140 	x->narg[2] = d;
141 	x->narg[3] = e;
142 	return(x);
143 }
stat3(intptr_t a,node * b,node * c,node * d)144 node *stat3(intptr_t a,node *b,node *c,node *d)
145 {
146 	register node *x;
147 	x = node3(a,b,c,d);
148 	x->ntype = NSTAT;
149 	return(x);
150 }
op2(intptr_t a,node * b,node * c)151 node *op2(intptr_t a,node *b,node *c)
152 {
153 	register node *x;
154 	x = node2(a,b,c);
155 	x->ntype = NEXPR;
156 	return(x);
157 }
op1(intptr_t a,node * b)158 node *op1(intptr_t a,node *b)
159 {
160 	register node *x;
161 	x = node1(a,b);
162 	x->ntype = NEXPR;
163 	return(x);
164 }
stat1(intptr_t a,node * b)165 node *stat1(intptr_t a,node *b)
166 {
167 	register node *x;
168 	x = node1(a,b);
169 	x->ntype = NSTAT;
170 	return(x);
171 }
op3(intptr_t a,node * b,node * c,node * d)172 node *op3(intptr_t a,node *b,node *c,node *d)
173 {
174 	register node *x;
175 	x = node3(a,b,c,d);
176 	x->ntype = NEXPR;
177 	return(x);
178 }
stat2(intptr_t a,node * b,node * c)179 node *stat2(intptr_t a,node *b,node *c)
180 {
181 	register node *x;
182 	x = node2(a,b,c);
183 	x->ntype = NSTAT;
184 	return(x);
185 }
stat4(intptr_t a,node * b,node * c,node * d,node * e)186 node *stat4(intptr_t a,node *b,node *c,node *d,node *e)
187 {
188 	register node *x;
189 	x = node4(a,b,c,d,e);
190 	x->ntype = NSTAT;
191 	return(x);
192 }
valtonode(cell * a,int b)193 node *valtonode(cell *a, int b)
194 {
195 	register node *x;
196 	x = node0((intptr_t)a);
197 	x->ntype = NVALUE;
198 	x->subtype = b;
199 	return(x);
200 }
pa2stat(node * a,node * b,node * c)201 node *pa2stat(node *a,node *b,node *c)
202 {
203 	register node *x;
204 	x = node3(paircnt++, a, b, c);
205 	x->ntype = NPA2;
206 	return(x);
207 }
linkum(node * a,node * b)208 node *linkum(node *a,node *b)
209 {
210 	register node *c;
211 	if(a == NULL) return(b);
212 	else if(b == NULL) return(a);
213 	for(c=a; c->nnext != NULL; c=c->nnext);
214 	c->nnext = b;
215 	return(a);
216 }
genprint(void)217 node *genprint(void)
218 {
219 	register node *x;
220 	x = stat2(PRINT,valtonode(lookup("$record", symtab, 0), CFLD), nullstat);
221 	return(x);
222 }
223