nodes.c.pat revision 127958
1146773Ssam/*- 2146773Ssam * Copyright (c) 1991, 1993 3146773Ssam * The Regents of the University of California. All rights reserved. 4146773Ssam * 5146773Ssam * This code is derived from software contributed to Berkeley by 6146773Ssam * Kenneth Almquist. 7146773Ssam * 8146773Ssam * Redistribution and use in source and binary forms, with or without 9146773Ssam * modification, are permitted provided that the following conditions 10146773Ssam * are met: 11146773Ssam * 1. Redistributions of source code must retain the above copyright 12146773Ssam * notice, this list of conditions and the following disclaimer. 13146773Ssam * 2. Redistributions in binary form must reproduce the above copyright 14146773Ssam * notice, this list of conditions and the following disclaimer in the 15146773Ssam * documentation and/or other materials provided with the distribution. 16146773Ssam * 4. Neither the name of the University nor the names of its contributors 17146773Ssam * may be used to endorse or promote products derived from this software 18146773Ssam * without specific prior written permission. 19146773Ssam * 20146773Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21146773Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22146773Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23146773Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24146773Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25146773Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26146773Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27146773Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28146773Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29146773Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30146773Ssam * SUCH DAMAGE. 31146773Ssam * 32146773Ssam * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95 33146773Ssam * $FreeBSD: head/bin/sh/nodes.c.pat 127958 2004-04-06 20:06:54Z markm $ 34146773Ssam */ 35146773Ssam 36146773Ssam#include <sys/param.h> 37146773Ssam#include <stdlib.h> 38146773Ssam/* 39146773Ssam * Routine for dealing with parsed shell commands. 40146773Ssam */ 41146773Ssam 42146773Ssam#include "shell.h" 43146773Ssam#include "nodes.h" 44146773Ssam#include "memalloc.h" 45146773Ssam#include "mystring.h" 46146773Ssam 47146773Ssam 48146773SsamSTATIC int funcblocksize; /* size of structures in function */ 49146773SsamSTATIC int funcstringsize; /* size of strings in node */ 50146773SsamSTATIC pointer funcblock; /* block to allocate function from */ 51146773SsamSTATIC char *funcstring; /* block to allocate strings from */ 52146773Ssam 53146773Ssam%SIZES 54146773Ssam 55146773Ssam 56146773SsamSTATIC void calcsize(union node *); 57146773SsamSTATIC void sizenodelist(struct nodelist *); 58146773SsamSTATIC union node *copynode(union node *); 59146773SsamSTATIC struct nodelist *copynodelist(struct nodelist *); 60146773SsamSTATIC char *nodesavestr(char *); 61146773Ssam 62146773Ssam 63146773Ssam 64146773Ssam/* 65146773Ssam * Make a copy of a parse tree. 66146773Ssam */ 67146773Ssam 68146773Ssamunion node * 69146773Ssamcopyfunc(union node *n) 70146773Ssam{ 71146773Ssam if (n == NULL) 72146773Ssam return NULL; 73146773Ssam funcblocksize = 0; 74146773Ssam funcstringsize = 0; 75146773Ssam calcsize(n); 76146773Ssam funcblock = ckmalloc(funcblocksize + funcstringsize); 77146773Ssam funcstring = (char *)funcblock + funcblocksize; 78146773Ssam return copynode(n); 79146773Ssam} 80146773Ssam 81146773Ssam 82146773Ssam 83146773SsamSTATIC void 84146773Ssamcalcsize(union node *n) 85146773Ssam{ 86146773Ssam %CALCSIZE 87146773Ssam} 88146773Ssam 89146773Ssam 90146773Ssam 91146773SsamSTATIC void 92146773Ssamsizenodelist(struct nodelist *lp) 93146773Ssam{ 94146773Ssam while (lp) { 95146773Ssam funcblocksize += ALIGN(sizeof(struct nodelist)); 96146773Ssam calcsize(lp->n); 97146773Ssam lp = lp->next; 98146773Ssam } 99146773Ssam} 100146773Ssam 101146773Ssam 102146773Ssam 103146773SsamSTATIC union node * 104146773Ssamcopynode(union node *n) 105146773Ssam{ 106146773Ssam union node *new; 107146773Ssam 108146773Ssam %COPY 109146773Ssam return new; 110146773Ssam} 111146773Ssam 112146773Ssam 113146773SsamSTATIC struct nodelist * 114146773Ssamcopynodelist(struct nodelist *lp) 115146773Ssam{ 116146773Ssam struct nodelist *start; 117146773Ssam struct nodelist **lpp; 118146773Ssam 119146773Ssam lpp = &start; 120146773Ssam while (lp) { 121146773Ssam *lpp = funcblock; 122146773Ssam funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist)); 123146773Ssam (*lpp)->n = copynode(lp->n); 124146773Ssam lp = lp->next; 125146773Ssam lpp = &(*lpp)->next; 126146773Ssam } 127146773Ssam *lpp = NULL; 128146773Ssam return start; 129146773Ssam} 130146773Ssam 131146773Ssam 132146773Ssam 133146773SsamSTATIC char * 134146773Ssamnodesavestr(char *s) 135146773Ssam{ 136146773Ssam char *p = s; 137146773Ssam char *q = funcstring; 138146773Ssam char *rtn = funcstring; 139146773Ssam 140146773Ssam while ((*q++ = *p++) != '\0') 141146773Ssam continue; 142146773Ssam funcstring = q; 143146773Ssam return rtn; 144146773Ssam} 145146773Ssam 146146773Ssam 147146773Ssam 148146773Ssam/* 149146773Ssam * Free a parse tree. 150146773Ssam */ 151146773Ssam 152146773Ssamvoid 153146773Ssamfreefunc(union node *n) 154146773Ssam{ 155146773Ssam if (n) 156146773Ssam ckfree(n); 157146773Ssam} 158146773Ssam