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