VirtualBox

source: kBuild/trunk/src/kash/generated/nodes.c@ 3443

Last change on this file since 3443 was 3443, checked in by bird, 5 years ago

kash: Need to initialize expfname in the generated function copy code too.

  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1/*
2 * This file was generated by mknodes.sh
3 */
4
5/* $NetBSD: nodes.c.pat,v 1.12 2004/06/15 22:57:27 dsl Exp $ */
6
7/*-
8 * Copyright (c) 1991, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Kenneth Almquist.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95
39 */
40
41#include <stdlib.h>
42/*
43 * Routine for dealing with parsed shell commands.
44 */
45
46#include "shell.h"
47#include "nodes.h"
48#include "memalloc.h"
49#include "machdep.h"
50#include "mystring.h"
51#include "shinstance.h"
52
53
54size_t funcblocksize; /* size of structures in function */
55size_t funcstringsize; /* size of strings in node */
56pointer funcblock; /* block to allocate function from */
57char *funcstring; /* block to allocate strings from */
58
59static const short nodesize[26] = {
60 SHELL_ALIGN(sizeof (struct nbinary)),
61 SHELL_ALIGN(sizeof (struct ncmd)),
62 SHELL_ALIGN(sizeof (struct npipe)),
63 SHELL_ALIGN(sizeof (struct nredir)),
64 SHELL_ALIGN(sizeof (struct nredir)),
65 SHELL_ALIGN(sizeof (struct nredir)),
66 SHELL_ALIGN(sizeof (struct nbinary)),
67 SHELL_ALIGN(sizeof (struct nbinary)),
68 SHELL_ALIGN(sizeof (struct nif)),
69 SHELL_ALIGN(sizeof (struct nbinary)),
70 SHELL_ALIGN(sizeof (struct nbinary)),
71 SHELL_ALIGN(sizeof (struct nfor)),
72 SHELL_ALIGN(sizeof (struct ncase)),
73 SHELL_ALIGN(sizeof (struct nclist)),
74 SHELL_ALIGN(sizeof (struct narg)),
75 SHELL_ALIGN(sizeof (struct narg)),
76 SHELL_ALIGN(sizeof (struct nfile)),
77 SHELL_ALIGN(sizeof (struct nfile)),
78 SHELL_ALIGN(sizeof (struct nfile)),
79 SHELL_ALIGN(sizeof (struct nfile)),
80 SHELL_ALIGN(sizeof (struct nfile)),
81 SHELL_ALIGN(sizeof (struct ndup)),
82 SHELL_ALIGN(sizeof (struct ndup)),
83 SHELL_ALIGN(sizeof (struct nhere)),
84 SHELL_ALIGN(sizeof (struct nhere)),
85 SHELL_ALIGN(sizeof (struct nnot)),
86};
87
88
89STATIC void calcsize(union node *);
90STATIC void sizenodelist(struct nodelist *);
91STATIC union node *copynode(union node *);
92STATIC struct nodelist *copynodelist(struct nodelist *);
93STATIC char *nodesavestr(char *);
94
95
96
97/*
98 * Make a copy of a parse tree.
99 */
100
101union node *
102copyfunc(psh, n)
103 struct shinstance *psh;
104 union node *n;
105{
106 if (n == NULL)
107 return NULL;
108 funcblocksize = 0;
109 funcstringsize = 0;
110 calcsize(n);
111 funcblock = ckmalloc(psh, funcblocksize + funcstringsize);
112 funcstring = (char *) funcblock + funcblocksize;
113 return copynode(n);
114}
115
116
117
118STATIC void
119calcsize(n)
120 union node *n;
121{
122 if (n == NULL)
123 return;
124 funcblocksize += nodesize[n->type];
125 switch (n->type) {
126 case NSEMI:
127 case NAND:
128 case NOR:
129 case NWHILE:
130 case NUNTIL:
131 calcsize(n->nbinary.ch2);
132 calcsize(n->nbinary.ch1);
133 break;
134 case NCMD:
135 calcsize(n->ncmd.redirect);
136 calcsize(n->ncmd.args);
137 break;
138 case NPIPE:
139 sizenodelist(n->npipe.cmdlist);
140 break;
141 case NREDIR:
142 case NBACKGND:
143 case NSUBSHELL:
144 calcsize(n->nredir.redirect);
145 calcsize(n->nredir.n);
146 break;
147 case NIF:
148 calcsize(n->nif.elsepart);
149 calcsize(n->nif.ifpart);
150 calcsize(n->nif.test);
151 break;
152 case NFOR:
153 funcstringsize += strlen(n->nfor.var) + 1;
154 calcsize(n->nfor.body);
155 calcsize(n->nfor.args);
156 break;
157 case NCASE:
158 calcsize(n->ncase.cases);
159 calcsize(n->ncase.expr);
160 break;
161 case NCLIST:
162 calcsize(n->nclist.body);
163 calcsize(n->nclist.pattern);
164 calcsize(n->nclist.next);
165 break;
166 case NDEFUN:
167 case NARG:
168 sizenodelist(n->narg.backquote);
169 funcstringsize += strlen(n->narg.text) + 1;
170 calcsize(n->narg.next);
171 break;
172 case NTO:
173 case NCLOBBER:
174 case NFROM:
175 case NFROMTO:
176 case NAPPEND:
177 calcsize(n->nfile.fname);
178 calcsize(n->nfile.next);
179 break;
180 case NTOFD:
181 case NFROMFD:
182 calcsize(n->ndup.vname);
183 calcsize(n->ndup.next);
184 break;
185 case NHERE:
186 case NXHERE:
187 calcsize(n->nhere.doc);
188 calcsize(n->nhere.next);
189 break;
190 case NNOT:
191 calcsize(n->nnot.com);
192 break;
193 };
194}
195
196
197
198STATIC void
199sizenodelist(lp)
200 struct nodelist *lp;
201{
202 while (lp) {
203 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
204 calcsize(lp->n);
205 lp = lp->next;
206 }
207}
208
209
210
211STATIC union node *
212copynode(n)
213 union node *n;
214{
215 union node *new;
216
217 if (n == NULL)
218 return NULL;
219 new = funcblock;
220 funcblock = (char *) funcblock + nodesize[n->type];
221 switch (n->type) {
222 case NSEMI:
223 case NAND:
224 case NOR:
225 case NWHILE:
226 case NUNTIL:
227 new->nbinary.ch2 = copynode(n->nbinary.ch2);
228 new->nbinary.ch1 = copynode(n->nbinary.ch1);
229 break;
230 case NCMD:
231 new->ncmd.redirect = copynode(n->ncmd.redirect);
232 new->ncmd.args = copynode(n->ncmd.args);
233 new->ncmd.backgnd = n->ncmd.backgnd;
234 break;
235 case NPIPE:
236 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
237 new->npipe.backgnd = n->npipe.backgnd;
238 break;
239 case NREDIR:
240 case NBACKGND:
241 case NSUBSHELL:
242 new->nredir.redirect = copynode(n->nredir.redirect);
243 new->nredir.n = copynode(n->nredir.n);
244 break;
245 case NIF:
246 new->nif.elsepart = copynode(n->nif.elsepart);
247 new->nif.ifpart = copynode(n->nif.ifpart);
248 new->nif.test = copynode(n->nif.test);
249 break;
250 case NFOR:
251 new->nfor.var = nodesavestr(n->nfor.var);
252 new->nfor.body = copynode(n->nfor.body);
253 new->nfor.args = copynode(n->nfor.args);
254 break;
255 case NCASE:
256 new->ncase.cases = copynode(n->ncase.cases);
257 new->ncase.expr = copynode(n->ncase.expr);
258 break;
259 case NCLIST:
260 new->nclist.body = copynode(n->nclist.body);
261 new->nclist.pattern = copynode(n->nclist.pattern);
262 new->nclist.next = copynode(n->nclist.next);
263 break;
264 case NDEFUN:
265 case NARG:
266 new->narg.backquote = copynodelist(n->narg.backquote);
267 new->narg.text = nodesavestr(n->narg.text);
268 new->narg.next = copynode(n->narg.next);
269 break;
270 case NTO:
271 case NCLOBBER:
272 case NFROM:
273 case NFROMTO:
274 case NAPPEND:
275 new->nfile.fname = copynode(n->nfile.fname);
276 new->nfile.fd = n->nfile.fd;
277 new->nfile.next = copynode(n->nfile.next);
278 new->nfile.expfname = NULL;
279 break;
280 case NTOFD:
281 case NFROMFD:
282 new->ndup.vname = copynode(n->ndup.vname);
283 new->ndup.dupfd = n->ndup.dupfd;
284 new->ndup.fd = n->ndup.fd;
285 new->ndup.next = copynode(n->ndup.next);
286 break;
287 case NHERE:
288 case NXHERE:
289 new->nhere.doc = copynode(n->nhere.doc);
290 new->nhere.fd = n->nhere.fd;
291 new->nhere.next = copynode(n->nhere.next);
292 break;
293 case NNOT:
294 new->nnot.com = copynode(n->nnot.com);
295 break;
296 };
297 new->type = n->type;
298 return new;
299}
300
301
302STATIC struct nodelist *
303copynodelist(lp)
304 struct nodelist *lp;
305{
306 struct nodelist *start;
307 struct nodelist **lpp;
308
309 lpp = &start;
310 while (lp) {
311 *lpp = funcblock;
312 funcblock = (char *) funcblock +
313 SHELL_ALIGN(sizeof(struct nodelist));
314 (*lpp)->n = copynode(lp->n);
315 lp = lp->next;
316 lpp = &(*lpp)->next;
317 }
318 *lpp = NULL;
319 return start;
320}
321
322
323
324STATIC char *
325nodesavestr(s)
326 char *s;
327{
328 register char *p = s;
329 register char *q = funcstring;
330 char *rtn = funcstring;
331
332 while ((*q++ = *p++) != 0)
333 continue;
334 funcstring = q;
335 return rtn;
336}
337
338
339
340/*
341 * Free a parse tree.
342 */
343
344void
345freefunc(psh, n)
346 shinstance *psh;
347 union node *n;
348{
349 if (n)
350 ckfree(psh, n);
351}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette