[626] | 1 | /* $NetBSD: nodes.c.pat,v 1.12 2004/06/15 22:57:27 dsl Exp $ */
|
---|
| 2 |
|
---|
| 3 | /*-
|
---|
| 4 | * Copyright (c) 1991, 1993
|
---|
| 5 | * The Regents of the University of California. All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * This code is derived from software contributed to Berkeley by
|
---|
| 8 | * Kenneth Almquist.
|
---|
| 9 | *
|
---|
| 10 | * Redistribution and use in source and binary forms, with or without
|
---|
| 11 | * modification, are permitted provided that the following conditions
|
---|
| 12 | * are met:
|
---|
| 13 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer.
|
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 16 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 17 | * documentation and/or other materials provided with the distribution.
|
---|
| 18 | * 3. Neither the name of the University nor the names of its contributors
|
---|
| 19 | * may be used to endorse or promote products derived from this software
|
---|
| 20 | * without specific prior written permission.
|
---|
| 21 | *
|
---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 32 | * SUCH DAMAGE.
|
---|
| 33 | *
|
---|
| 34 | * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | /*
|
---|
| 39 | * Routine for dealing with parsed shell commands.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include "shell.h"
|
---|
| 43 | #include "nodes.h"
|
---|
| 44 | #include "memalloc.h"
|
---|
| 45 | #include "machdep.h"
|
---|
| 46 | #include "mystring.h"
|
---|
[2290] | 47 | #include "shinstance.h"
|
---|
[626] | 48 |
|
---|
[3458] | 49 | #ifndef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[626] | 50 |
|
---|
[2391] | 51 | size_t funcblocksize; /* size of structures in function */
|
---|
| 52 | size_t funcstringsize; /* size of strings in node */
|
---|
[626] | 53 | pointer funcblock; /* block to allocate function from */
|
---|
| 54 | char *funcstring; /* block to allocate strings from */
|
---|
| 55 |
|
---|
| 56 | %SIZES
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | STATIC void calcsize(union node *);
|
---|
| 60 | STATIC void sizenodelist(struct nodelist *);
|
---|
| 61 | STATIC union node *copynode(union node *);
|
---|
| 62 | STATIC struct nodelist *copynodelist(struct nodelist *);
|
---|
| 63 | STATIC char *nodesavestr(char *);
|
---|
| 64 |
|
---|
[3458] | 65 | #endif /* !KASH_SEPARATE_PARSER_ALLOCATOR */
|
---|
[626] | 66 |
|
---|
| 67 |
|
---|
| 68 | /*
|
---|
| 69 | * Make a copy of a parse tree.
|
---|
| 70 | */
|
---|
| 71 |
|
---|
| 72 | union node *
|
---|
[2290] | 73 | copyfunc(psh, n)
|
---|
| 74 | struct shinstance *psh;
|
---|
[626] | 75 | union node *n;
|
---|
| 76 | {
|
---|
[3458] | 77 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
| 78 | if (n != NULL) {
|
---|
| 79 | unsigned refs = pstackretain(n->pblock);
|
---|
| 80 | TRACE2((psh, "copyfunc: %p - %u refs\n", n->pblock, refs)); K_NOREF(refs);
|
---|
| 81 | }
|
---|
| 82 | return n;
|
---|
| 83 | #else
|
---|
[626] | 84 | if (n == NULL)
|
---|
| 85 | return NULL;
|
---|
| 86 | funcblocksize = 0;
|
---|
| 87 | funcstringsize = 0;
|
---|
| 88 | calcsize(n);
|
---|
[2290] | 89 | funcblock = ckmalloc(psh, funcblocksize + funcstringsize);
|
---|
[626] | 90 | funcstring = (char *) funcblock + funcblocksize;
|
---|
| 91 | return copynode(n);
|
---|
[3458] | 92 | #endif
|
---|
[626] | 93 | }
|
---|
| 94 |
|
---|
[3458] | 95 | #ifndef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[626] | 96 |
|
---|
| 97 | STATIC void
|
---|
| 98 | calcsize(n)
|
---|
| 99 | union node *n;
|
---|
| 100 | {
|
---|
| 101 | %CALCSIZE
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | STATIC void
|
---|
| 107 | sizenodelist(lp)
|
---|
| 108 | struct nodelist *lp;
|
---|
| 109 | {
|
---|
| 110 | while (lp) {
|
---|
| 111 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
|
---|
| 112 | calcsize(lp->n);
|
---|
| 113 | lp = lp->next;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | STATIC union node *
|
---|
| 120 | copynode(n)
|
---|
| 121 | union node *n;
|
---|
| 122 | {
|
---|
| 123 | union node *new;
|
---|
| 124 |
|
---|
| 125 | %COPY
|
---|
| 126 | return new;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | STATIC struct nodelist *
|
---|
| 131 | copynodelist(lp)
|
---|
| 132 | struct nodelist *lp;
|
---|
| 133 | {
|
---|
| 134 | struct nodelist *start;
|
---|
| 135 | struct nodelist **lpp;
|
---|
| 136 |
|
---|
| 137 | lpp = &start;
|
---|
| 138 | while (lp) {
|
---|
| 139 | *lpp = funcblock;
|
---|
| 140 | funcblock = (char *) funcblock +
|
---|
| 141 | SHELL_ALIGN(sizeof(struct nodelist));
|
---|
| 142 | (*lpp)->n = copynode(lp->n);
|
---|
| 143 | lp = lp->next;
|
---|
| 144 | lpp = &(*lpp)->next;
|
---|
| 145 | }
|
---|
| 146 | *lpp = NULL;
|
---|
| 147 | return start;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | STATIC char *
|
---|
| 153 | nodesavestr(s)
|
---|
| 154 | char *s;
|
---|
| 155 | {
|
---|
| 156 | register char *p = s;
|
---|
| 157 | register char *q = funcstring;
|
---|
| 158 | char *rtn = funcstring;
|
---|
| 159 |
|
---|
| 160 | while ((*q++ = *p++) != 0)
|
---|
| 161 | continue;
|
---|
| 162 | funcstring = q;
|
---|
| 163 | return rtn;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[3458] | 166 | #endif /* !KASH_SEPARATE_PARSER_ALLOCATOR */
|
---|
[626] | 167 |
|
---|
| 168 |
|
---|
| 169 | /*
|
---|
| 170 | * Free a parse tree.
|
---|
| 171 | */
|
---|
| 172 |
|
---|
| 173 | void
|
---|
[2290] | 174 | freefunc(psh, n)
|
---|
| 175 | shinstance *psh;
|
---|
[626] | 176 | union node *n;
|
---|
| 177 | {
|
---|
[3458] | 178 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
---|
[626] | 179 | if (n)
|
---|
[3458] | 180 | pstackrelease(psh, n->pblock, "freefunc");
|
---|
| 181 | #else
|
---|
| 182 | if (n)
|
---|
[2290] | 183 | ckfree(psh, n);
|
---|
[3458] | 184 | #endif
|
---|
[626] | 185 | }
|
---|