1/* $NetBSD: nodes.c.pat,v 1.8 1997/04/11 23:03:09 christos 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. 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 52 53int funcblocksize; /* size of structures in function */ 54int funcstringsize; /* size of strings in node */ 55pointer funcblock; /* block to allocate function from */ 56char *funcstring; /* block to allocate strings from */ 57 58%SIZES 59 60 61STATIC void calcsize __P((union node *)); 62STATIC void sizenodelist __P((struct nodelist *)); 63STATIC union node *copynode __P((union node *)); 64STATIC struct nodelist *copynodelist __P((struct nodelist *)); 65STATIC char *nodesavestr __P((char *)); 66 67 68 69/* 70 * Make a copy of a parse tree. 71 */ 72 73union node * 74copyfunc(n) 75 union node *n; 76{ 77 if (n == NULL) 78 return NULL; 79 funcblocksize = 0; 80 funcstringsize = 0; 81 calcsize(n); 82 funcblock = ckmalloc(funcblocksize + funcstringsize); 83 funcstring = (char *) funcblock + funcblocksize; 84 return copynode(n); 85} 86 87 88 89STATIC void 90calcsize(n) 91 union node *n; 92{ 93 %CALCSIZE 94} 95 96 97 98STATIC void 99sizenodelist(lp) 100 struct nodelist *lp; 101{ 102 while (lp) { 103 funcblocksize += ALIGN(sizeof(struct nodelist)); 104 calcsize(lp->n); 105 lp = lp->next; 106 } 107} 108 109 110 111STATIC union node * 112copynode(n) 113 union node *n; 114{ 115 union node *new; 116 117 %COPY 118 return new; 119} 120 121 122STATIC struct nodelist * 123copynodelist(lp) 124 struct nodelist *lp; 125{ 126 struct nodelist *start; 127 struct nodelist **lpp; 128 129 lpp = &start; 130 while (lp) { 131 *lpp = funcblock; 132 funcblock = (char *) funcblock + ALIGN(sizeof(struct nodelist)); 133 (*lpp)->n = copynode(lp->n); 134 lp = lp->next; 135 lpp = &(*lpp)->next; 136 } 137 *lpp = NULL; 138 return start; 139} 140 141 142 143STATIC char * 144nodesavestr(s) 145 char *s; 146{ 147 register char *p = s; 148 register char *q = funcstring; 149 char *rtn = funcstring; 150 151 while ((*q++ = *p++) != '\0') 152 continue; 153 funcstring = q; 154 return rtn; 155} 156 157 158 159/* 160 * Free a parse tree. 161 */ 162 163void 164freefunc(n) 165 union node *n; 166{ 167 if (n) 168 ckfree(n); 169} 170