xref: /illumos-gate/usr/src/cmd/oawk/parse.c (revision 4774dff6a15e3052e75fd8a0fdd519521be2db59)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 #include "awk.def"
26 #include "awk.h"
27 #include "stdio.h"
28 #include "stdint.h"
29 
nodealloc(int n)30 NODE *nodealloc(int n)
31 {
32 	NODE *x;
33 	x = (NODE *) malloc(sizeof (NODE) + n * sizeof (NODE *));
34 	if (x == NULL)
35 		error(FATAL, "out of space in nodealloc");
36 	return (x);
37 }
38 
39 
40 
41 
exptostat(NODE * a)42 NODE *exptostat(NODE *a)
43 {
44 	a->ntype = NSTAT;
45 	return (a);
46 }
47 
48 
49 
50 
node0(int a)51 NODE *node0(int a)
52 {
53 	NODE *x;
54 	x = nodealloc(0);	/* No space for narg */
55 	x->nnext = NULL;
56 	x->nobj = a;
57 	return (x);
58 }
59 
60 
61 
62 
node1(int a,NODE * b)63 NODE *node1(int a, NODE *b)
64 {
65 	NODE *x;
66 	x = nodealloc(1);
67 	x->nnext = NULL;
68 	x->nobj = a;
69 	x->narg[0]=b;
70 	return (x);
71 }
72 
73 
74 
75 
node2(int a,NODE * b,NODE * c)76 NODE *node2(int a, NODE *b, NODE *c)
77 {
78 	NODE *x;
79 	x = nodealloc(2);
80 	x->nnext = NULL;
81 	x->nobj = a;
82 	x->narg[0] = b;
83 	x->narg[1] = c;
84 	return (x);
85 }
86 
87 
88 
89 
node3(int a,NODE * b,NODE * c,NODE * d)90 NODE *node3(int a, NODE *b, NODE *c, NODE *d)
91 {
92 	NODE *x;
93 	x = nodealloc(3);
94 	x->nnext = NULL;
95 	x->nobj = a;
96 	x->narg[0] = b;
97 	x->narg[1] = c;
98 	x->narg[2] = d;
99 	return (x);
100 }
101 
102 
103 
104 
node4(int a,NODE * b,NODE * c,NODE * d,NODE * e)105 NODE *node4(int a, NODE *b, NODE *c, NODE *d, NODE *e)
106 {
107 	NODE *x;
108 	x = nodealloc(4);
109 	x->nnext = NULL;
110 	x->nobj = a;
111 	x->narg[0] = b;
112 	x->narg[1] = c;
113 	x->narg[2] = d;
114 	x->narg[3] = e;
115 	return (x);
116 }
117 
118 
119 
120 
stat3(int a,NODE * b,NODE * c,NODE * d)121 NODE *stat3(int a, NODE *b, NODE *c, NODE *d)
122 {
123 	NODE *x;
124 	x = node3(a, b, c, d);
125 	x->ntype = NSTAT;
126 	return (x);
127 }
128 
129 
130 
131 
op2(int a,NODE * b,NODE * c)132 NODE *op2(int a, NODE *b, NODE *c)
133 {
134 	NODE *x;
135 	x = node2(a, b, c);
136 	x->ntype = NEXPR;
137 	return (x);
138 }
139 
140 
141 
142 
op1(int a,NODE * b)143 NODE *op1(int a, NODE *b)
144 {
145 	NODE *x;
146 	x = node1(a, b);
147 	x->ntype = NEXPR;
148 	return (x);
149 }
150 
151 
152 
153 
stat1(int a,NODE * b)154 NODE *stat1(int a, NODE *b)
155 {
156 	NODE *x;
157 	x = node1(a, b);
158 	x->ntype = NSTAT;
159 	return (x);
160 }
161 
162 
163 
164 
op3(int a,NODE * b,NODE * c,NODE * d)165 NODE *op3(int a, NODE *b, NODE *c, NODE *d)
166 {
167 	NODE *x;
168 	x = node3(a, b, c, d);
169 	x->ntype = NEXPR;
170 	return (x);
171 }
172 
173 
174 
175 
stat2(int a,NODE * b,NODE * c)176 NODE *stat2(int a, NODE *b, NODE *c)
177 {
178 	NODE *x;
179 	x = node2(a, b, c);
180 	x->ntype = NSTAT;
181 	return (x);
182 }
183 
184 
185 
186 
stat4(int a,NODE * b,NODE * c,NODE * d,NODE * e)187 NODE *stat4(int a, NODE *b, NODE *c, NODE *d, NODE *e)
188 {
189 	NODE *x;
190 	x = node4(a, b, c, d, e);
191 	x->ntype = NSTAT;
192 	return (x);
193 }
194 
195 
196 
197 
valtonode(CELL * a,int b)198 NODE *valtonode(CELL *a, int b)
199 {
200 	NODE *x;
201 	x = node0((uintptr_t)a);
202 	x->ntype = NVALUE;
203 	x->subtype = b;
204 	return (x);
205 }
206 
207 
208 
209 
pa2stat(NODE * a,NODE * b,NODE * c)210 NODE *pa2stat(NODE *a, NODE *b, NODE *c)
211 {
212 	NODE *x;
213 	x = node4(PASTAT2, a, b, c, (NODE *) paircnt);
214 	paircnt++;
215 	x->ntype = NSTAT;
216 	return (x);
217 }
218 
219 
220 
221 
linkum(NODE * a,NODE * b)222 NODE *linkum(NODE *a, NODE *b)
223 {
224 	NODE *c;
225 	if (a == NULL) return (b);
226 	else if (b == NULL) return (a);
227 	for (c = a; c->nnext != NULL; c=c->nnext)
228 		;
229 	c->nnext = b;
230 	return (a);
231 }
232 
233 
234 
235 
genprint(void)236 NODE *genprint(void)
237 {
238 	NODE *x;
239 	static wchar_t L_record[] = L"$record";
240 	x = stat2(PRINT, valtonode(lookup(L_record, symtab, 0), CFLD), NULL);
241 	return (x);
242 }
243