[631] | 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"
|
---|
[2290] | 51 | #include "shinstance.h"
|
---|
[631] | 52 |
|
---|
[3458] | 53 | #ifndef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[631] | 54 |
|
---|
[2391] | 55 | size_t funcblocksize; /* size of structures in function */
|
---|
| 56 | size_t funcstringsize; /* size of strings in node */
|
---|
[631] | 57 | pointer funcblock; /* block to allocate function from */
|
---|
| 58 | char *funcstring; /* block to allocate strings from */
|
---|
| 59 |
|
---|
| 60 | static const short nodesize[26] = {
|
---|
| 61 | SHELL_ALIGN(sizeof (struct nbinary)),
|
---|
| 62 | SHELL_ALIGN(sizeof (struct ncmd)),
|
---|
| 63 | SHELL_ALIGN(sizeof (struct npipe)),
|
---|
| 64 | SHELL_ALIGN(sizeof (struct nredir)),
|
---|
| 65 | SHELL_ALIGN(sizeof (struct nredir)),
|
---|
| 66 | SHELL_ALIGN(sizeof (struct nredir)),
|
---|
| 67 | SHELL_ALIGN(sizeof (struct nbinary)),
|
---|
| 68 | SHELL_ALIGN(sizeof (struct nbinary)),
|
---|
| 69 | SHELL_ALIGN(sizeof (struct nif)),
|
---|
| 70 | SHELL_ALIGN(sizeof (struct nbinary)),
|
---|
| 71 | SHELL_ALIGN(sizeof (struct nbinary)),
|
---|
| 72 | SHELL_ALIGN(sizeof (struct nfor)),
|
---|
| 73 | SHELL_ALIGN(sizeof (struct ncase)),
|
---|
| 74 | SHELL_ALIGN(sizeof (struct nclist)),
|
---|
| 75 | SHELL_ALIGN(sizeof (struct narg)),
|
---|
| 76 | SHELL_ALIGN(sizeof (struct narg)),
|
---|
| 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 nfile)),
|
---|
| 82 | SHELL_ALIGN(sizeof (struct ndup)),
|
---|
| 83 | SHELL_ALIGN(sizeof (struct ndup)),
|
---|
| 84 | SHELL_ALIGN(sizeof (struct nhere)),
|
---|
| 85 | SHELL_ALIGN(sizeof (struct nhere)),
|
---|
| 86 | SHELL_ALIGN(sizeof (struct nnot)),
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | STATIC void calcsize(union node *);
|
---|
| 91 | STATIC void sizenodelist(struct nodelist *);
|
---|
| 92 | STATIC union node *copynode(union node *);
|
---|
| 93 | STATIC struct nodelist *copynodelist(struct nodelist *);
|
---|
| 94 | STATIC char *nodesavestr(char *);
|
---|
| 95 |
|
---|
[3458] | 96 | #endif /* !KASH_SEPARATE_PARSER_ALLOCATOR */
|
---|
[631] | 97 |
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Make a copy of a parse tree.
|
---|
| 101 | */
|
---|
| 102 |
|
---|
| 103 | union node *
|
---|
[2290] | 104 | copyfunc(psh, n)
|
---|
| 105 | struct shinstance *psh;
|
---|
[631] | 106 | union node *n;
|
---|
| 107 | {
|
---|
[3458] | 108 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
| 109 | if (n != NULL) {
|
---|
| 110 | unsigned refs = pstackretain(n->pblock);
|
---|
| 111 | TRACE2((psh, "copyfunc: %p - %u refs\n", n->pblock, refs)); K_NOREF(refs);
|
---|
| 112 | }
|
---|
| 113 | return n;
|
---|
| 114 | #else
|
---|
[631] | 115 | if (n == NULL)
|
---|
| 116 | return NULL;
|
---|
| 117 | funcblocksize = 0;
|
---|
| 118 | funcstringsize = 0;
|
---|
| 119 | calcsize(n);
|
---|
[2290] | 120 | funcblock = ckmalloc(psh, funcblocksize + funcstringsize);
|
---|
[631] | 121 | funcstring = (char *) funcblock + funcblocksize;
|
---|
| 122 | return copynode(n);
|
---|
[3458] | 123 | #endif
|
---|
[631] | 124 | }
|
---|
| 125 |
|
---|
[3458] | 126 | #ifndef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[631] | 127 |
|
---|
| 128 | STATIC void
|
---|
| 129 | calcsize(n)
|
---|
| 130 | union node *n;
|
---|
| 131 | {
|
---|
| 132 | if (n == NULL)
|
---|
| 133 | return;
|
---|
| 134 | funcblocksize += nodesize[n->type];
|
---|
| 135 | switch (n->type) {
|
---|
| 136 | case NSEMI:
|
---|
| 137 | case NAND:
|
---|
| 138 | case NOR:
|
---|
| 139 | case NWHILE:
|
---|
| 140 | case NUNTIL:
|
---|
| 141 | calcsize(n->nbinary.ch2);
|
---|
| 142 | calcsize(n->nbinary.ch1);
|
---|
| 143 | break;
|
---|
| 144 | case NCMD:
|
---|
| 145 | calcsize(n->ncmd.redirect);
|
---|
| 146 | calcsize(n->ncmd.args);
|
---|
| 147 | break;
|
---|
| 148 | case NPIPE:
|
---|
| 149 | sizenodelist(n->npipe.cmdlist);
|
---|
| 150 | break;
|
---|
| 151 | case NREDIR:
|
---|
| 152 | case NBACKGND:
|
---|
| 153 | case NSUBSHELL:
|
---|
| 154 | calcsize(n->nredir.redirect);
|
---|
| 155 | calcsize(n->nredir.n);
|
---|
| 156 | break;
|
---|
| 157 | case NIF:
|
---|
| 158 | calcsize(n->nif.elsepart);
|
---|
| 159 | calcsize(n->nif.ifpart);
|
---|
| 160 | calcsize(n->nif.test);
|
---|
| 161 | break;
|
---|
| 162 | case NFOR:
|
---|
| 163 | funcstringsize += strlen(n->nfor.var) + 1;
|
---|
| 164 | calcsize(n->nfor.body);
|
---|
| 165 | calcsize(n->nfor.args);
|
---|
| 166 | break;
|
---|
| 167 | case NCASE:
|
---|
| 168 | calcsize(n->ncase.cases);
|
---|
| 169 | calcsize(n->ncase.expr);
|
---|
| 170 | break;
|
---|
| 171 | case NCLIST:
|
---|
| 172 | calcsize(n->nclist.body);
|
---|
| 173 | calcsize(n->nclist.pattern);
|
---|
| 174 | calcsize(n->nclist.next);
|
---|
| 175 | break;
|
---|
| 176 | case NDEFUN:
|
---|
| 177 | case NARG:
|
---|
| 178 | sizenodelist(n->narg.backquote);
|
---|
| 179 | funcstringsize += strlen(n->narg.text) + 1;
|
---|
| 180 | calcsize(n->narg.next);
|
---|
| 181 | break;
|
---|
| 182 | case NTO:
|
---|
| 183 | case NCLOBBER:
|
---|
| 184 | case NFROM:
|
---|
| 185 | case NFROMTO:
|
---|
| 186 | case NAPPEND:
|
---|
| 187 | calcsize(n->nfile.fname);
|
---|
| 188 | calcsize(n->nfile.next);
|
---|
| 189 | break;
|
---|
| 190 | case NTOFD:
|
---|
| 191 | case NFROMFD:
|
---|
| 192 | calcsize(n->ndup.vname);
|
---|
| 193 | calcsize(n->ndup.next);
|
---|
| 194 | break;
|
---|
| 195 | case NHERE:
|
---|
| 196 | case NXHERE:
|
---|
| 197 | calcsize(n->nhere.doc);
|
---|
| 198 | calcsize(n->nhere.next);
|
---|
| 199 | break;
|
---|
| 200 | case NNOT:
|
---|
| 201 | calcsize(n->nnot.com);
|
---|
| 202 | break;
|
---|
| 203 | };
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | STATIC void
|
---|
| 209 | sizenodelist(lp)
|
---|
| 210 | struct nodelist *lp;
|
---|
| 211 | {
|
---|
| 212 | while (lp) {
|
---|
| 213 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
|
---|
| 214 | calcsize(lp->n);
|
---|
| 215 | lp = lp->next;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 |
|
---|
| 221 | STATIC union node *
|
---|
| 222 | copynode(n)
|
---|
| 223 | union node *n;
|
---|
| 224 | {
|
---|
| 225 | union node *new;
|
---|
| 226 |
|
---|
| 227 | if (n == NULL)
|
---|
| 228 | return NULL;
|
---|
| 229 | new = funcblock;
|
---|
| 230 | funcblock = (char *) funcblock + nodesize[n->type];
|
---|
| 231 | switch (n->type) {
|
---|
| 232 | case NSEMI:
|
---|
| 233 | case NAND:
|
---|
| 234 | case NOR:
|
---|
| 235 | case NWHILE:
|
---|
| 236 | case NUNTIL:
|
---|
| 237 | new->nbinary.ch2 = copynode(n->nbinary.ch2);
|
---|
| 238 | new->nbinary.ch1 = copynode(n->nbinary.ch1);
|
---|
| 239 | break;
|
---|
| 240 | case NCMD:
|
---|
| 241 | new->ncmd.redirect = copynode(n->ncmd.redirect);
|
---|
| 242 | new->ncmd.args = copynode(n->ncmd.args);
|
---|
| 243 | new->ncmd.backgnd = n->ncmd.backgnd;
|
---|
| 244 | break;
|
---|
| 245 | case NPIPE:
|
---|
| 246 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
|
---|
| 247 | new->npipe.backgnd = n->npipe.backgnd;
|
---|
| 248 | break;
|
---|
| 249 | case NREDIR:
|
---|
| 250 | case NBACKGND:
|
---|
| 251 | case NSUBSHELL:
|
---|
| 252 | new->nredir.redirect = copynode(n->nredir.redirect);
|
---|
| 253 | new->nredir.n = copynode(n->nredir.n);
|
---|
| 254 | break;
|
---|
| 255 | case NIF:
|
---|
| 256 | new->nif.elsepart = copynode(n->nif.elsepart);
|
---|
| 257 | new->nif.ifpart = copynode(n->nif.ifpart);
|
---|
| 258 | new->nif.test = copynode(n->nif.test);
|
---|
| 259 | break;
|
---|
| 260 | case NFOR:
|
---|
| 261 | new->nfor.var = nodesavestr(n->nfor.var);
|
---|
| 262 | new->nfor.body = copynode(n->nfor.body);
|
---|
| 263 | new->nfor.args = copynode(n->nfor.args);
|
---|
| 264 | break;
|
---|
| 265 | case NCASE:
|
---|
| 266 | new->ncase.cases = copynode(n->ncase.cases);
|
---|
| 267 | new->ncase.expr = copynode(n->ncase.expr);
|
---|
| 268 | break;
|
---|
| 269 | case NCLIST:
|
---|
| 270 | new->nclist.body = copynode(n->nclist.body);
|
---|
| 271 | new->nclist.pattern = copynode(n->nclist.pattern);
|
---|
| 272 | new->nclist.next = copynode(n->nclist.next);
|
---|
| 273 | break;
|
---|
| 274 | case NDEFUN:
|
---|
| 275 | case NARG:
|
---|
| 276 | new->narg.backquote = copynodelist(n->narg.backquote);
|
---|
| 277 | new->narg.text = nodesavestr(n->narg.text);
|
---|
| 278 | new->narg.next = copynode(n->narg.next);
|
---|
| 279 | break;
|
---|
| 280 | case NTO:
|
---|
| 281 | case NCLOBBER:
|
---|
| 282 | case NFROM:
|
---|
| 283 | case NFROMTO:
|
---|
| 284 | case NAPPEND:
|
---|
| 285 | new->nfile.fname = copynode(n->nfile.fname);
|
---|
[3458] | 286 | new->nfile.next = copynode(n->nfile.next);
|
---|
[631] | 287 | new->nfile.fd = n->nfile.fd;
|
---|
| 288 | break;
|
---|
| 289 | case NTOFD:
|
---|
| 290 | case NFROMFD:
|
---|
| 291 | new->ndup.vname = copynode(n->ndup.vname);
|
---|
| 292 | new->ndup.dupfd = n->ndup.dupfd;
|
---|
[3458] | 293 | new->ndup.next = copynode(n->ndup.next);
|
---|
[631] | 294 | new->ndup.fd = n->ndup.fd;
|
---|
| 295 | break;
|
---|
| 296 | case NHERE:
|
---|
| 297 | case NXHERE:
|
---|
| 298 | new->nhere.doc = copynode(n->nhere.doc);
|
---|
[3458] | 299 | new->nhere.next = copynode(n->nhere.next);
|
---|
[631] | 300 | new->nhere.fd = n->nhere.fd;
|
---|
| 301 | break;
|
---|
| 302 | case NNOT:
|
---|
| 303 | new->nnot.com = copynode(n->nnot.com);
|
---|
| 304 | break;
|
---|
| 305 | };
|
---|
| 306 | new->type = n->type;
|
---|
| 307 | return new;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | STATIC struct nodelist *
|
---|
| 312 | copynodelist(lp)
|
---|
| 313 | struct nodelist *lp;
|
---|
| 314 | {
|
---|
| 315 | struct nodelist *start;
|
---|
| 316 | struct nodelist **lpp;
|
---|
| 317 |
|
---|
| 318 | lpp = &start;
|
---|
| 319 | while (lp) {
|
---|
| 320 | *lpp = funcblock;
|
---|
| 321 | funcblock = (char *) funcblock +
|
---|
| 322 | SHELL_ALIGN(sizeof(struct nodelist));
|
---|
| 323 | (*lpp)->n = copynode(lp->n);
|
---|
| 324 | lp = lp->next;
|
---|
| 325 | lpp = &(*lpp)->next;
|
---|
| 326 | }
|
---|
| 327 | *lpp = NULL;
|
---|
| 328 | return start;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | STATIC char *
|
---|
| 334 | nodesavestr(s)
|
---|
| 335 | char *s;
|
---|
| 336 | {
|
---|
| 337 | register char *p = s;
|
---|
| 338 | register char *q = funcstring;
|
---|
| 339 | char *rtn = funcstring;
|
---|
| 340 |
|
---|
| 341 | while ((*q++ = *p++) != 0)
|
---|
| 342 | continue;
|
---|
| 343 | funcstring = q;
|
---|
| 344 | return rtn;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[3458] | 347 | #endif /* !KASH_SEPARATE_PARSER_ALLOCATOR */
|
---|
[631] | 348 |
|
---|
| 349 |
|
---|
| 350 | /*
|
---|
| 351 | * Free a parse tree.
|
---|
| 352 | */
|
---|
| 353 |
|
---|
| 354 | void
|
---|
[2290] | 355 | freefunc(psh, n)
|
---|
[3458] | 356 | shinstance *psh;
|
---|
[631] | 357 | union node *n;
|
---|
| 358 | {
|
---|
[3458] | 359 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[631] | 360 | if (n)
|
---|
[3458] | 361 | pstackrelease(psh, n->pblock, "freefunc");
|
---|
| 362 | #else
|
---|
| 363 | if (n)
|
---|
[2290] | 364 | ckfree(psh, n);
|
---|
[3458] | 365 | #endif
|
---|
[631] | 366 | }
|
---|