nodes.c.pat revision 21673
10SN/A/*- 216989Srriggs * Copyright (c) 1991, 1993 30SN/A * The Regents of the University of California. All rights reserved. 40SN/A * 50SN/A * This code is derived from software contributed to Berkeley by 60SN/A * Kenneth Almquist. 72362SN/A * 80SN/A * Redistribution and use in source and binary forms, with or without 92362SN/A * modification, are permitted provided that the following conditions 100SN/A * are met: 110SN/A * 1. Redistributions of source code must retain the above copyright 120SN/A * notice, this list of conditions and the following disclaimer. 130SN/A * 2. Redistributions in binary form must reproduce the above copyright 140SN/A * notice, this list of conditions and the following disclaimer in the 150SN/A * documentation and/or other materials provided with the distribution. 160SN/A * 3. All advertising materials mentioning features or use of this software 170SN/A * must display the following acknowledgement: 180SN/A * This product includes software developed by the University of 190SN/A * California, Berkeley and its contributors. 200SN/A * 4. Neither the name of the University nor the names of its contributors 212362SN/A * may be used to endorse or promote products derived from this software 222362SN/A * without specific prior written permission. 232362SN/A * 240SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 250SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 260SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 270SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 280SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 290SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 300SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319716SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 320SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 330SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 340SN/A * SUCH DAMAGE. 350SN/A * 360SN/A * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95 370SN/A * $FreeBSD: head/bin/sh/nodes.c.pat 21673 1997-01-14 07:20:47Z jkh $ 380SN/A */ 390SN/A 400SN/A#include <stdlib.h> 410SN/A/* 420SN/A * Routine for dealing with parsed shell commands. 430SN/A */ 440SN/A 450SN/A#include "shell.h" 460SN/A#include "nodes.h" 470SN/A#include "memalloc.h" 480SN/A#include "machdep.h" 490SN/A#include "mystring.h" 500SN/A 510SN/A 520SN/Aint funcblocksize; /* size of structures in function */ 530SN/Aint funcstringsize; /* size of strings in node */ 540SN/Apointer funcblock; /* block to allocate function from */ 550SN/Achar *funcstring; /* block to allocate strings from */ 560SN/A 570SN/A%SIZES 5812745Smartin 5912745Smartin 600SN/ASTATIC void calcsize __P((union node *)); 610SN/ASTATIC void sizenodelist __P((struct nodelist *)); 620SN/ASTATIC union node *copynode __P((union node *)); 630SN/ASTATIC struct nodelist *copynodelist __P((struct nodelist *)); 645988SN/ASTATIC char *nodesavestr __P((char *)); 650SN/A 660SN/A 670SN/A 680SN/A/* 690SN/A * Make a copy of a parse tree. 705988SN/A */ 715988SN/A 725988SN/Aunion node * 735988SN/Acopyfunc(n) 745988SN/A union node *n; 755988SN/A{ 765988SN/A if (n == NULL) 770SN/A return NULL; 780SN/A funcblocksize = 0; 790SN/A funcstringsize = 0; 800SN/A calcsize(n); 810SN/A funcblock = ckmalloc(funcblocksize + funcstringsize); 820SN/A funcstring = funcblock + funcblocksize; 830SN/A return copynode(n); 840SN/A} 850SN/A 860SN/A 870SN/A 880SN/ASTATIC void 890SN/Acalcsize(n) 900SN/A union node *n; 9112745Smartin{ 920SN/A %CALCSIZE 930SN/A} 9412745Smartin 950SN/A 960SN/A 970SN/ASTATIC void 980SN/Asizenodelist(lp) 990SN/A struct nodelist *lp; 1000SN/A{ 1010SN/A while (lp) { 1020SN/A funcblocksize += ALIGN(sizeof(struct nodelist)); 1030SN/A calcsize(lp->n); 1040SN/A lp = lp->next; 1050SN/A } 1066171SN/A} 1070SN/A 1080SN/A 1090SN/A 1100SN/ASTATIC union node * 1110SN/Acopynode(n) 1120SN/A union node *n; 1130SN/A{ 1140SN/A union node *new; 1150SN/A 1160SN/A %COPY 1170SN/A return new; 1180SN/A} 1196171SN/A 1200SN/A 1210SN/ASTATIC struct nodelist * 1220SN/Acopynodelist(lp) 1230SN/A struct nodelist *lp; 1240SN/A{ 1250SN/A struct nodelist *start; 1260SN/A struct nodelist **lpp; 1270SN/A 1280SN/A lpp = &start; 1290SN/A while (lp) { 1300SN/A *lpp = funcblock; 1310SN/A funcblock += ALIGN(sizeof(struct nodelist)); 1320SN/A (*lpp)->n = copynode(lp->n); 1330SN/A lp = lp->next; 1340SN/A lpp = &(*lpp)->next; 1350SN/A } 1360SN/A *lpp = NULL; 1376171SN/A return start; 1380SN/A} 1390SN/A 1400SN/A 1410SN/A 1420SN/ASTATIC char * 1430SN/Anodesavestr(s) 1440SN/A char *s; 1450SN/A{ 1460SN/A register char *p = s; 1470SN/A register char *q = funcstring; 1480SN/A char *rtn = funcstring; 1490SN/A 1506171SN/A while ((*q++ = *p++) != '\0') 1510SN/A continue; 1520SN/A funcstring = q; 1530SN/A return rtn; 1540SN/A} 1550SN/A 1560SN/A 1570SN/A 1580SN/A/* 1590SN/A * Free a parse tree. 1600SN/A */ 1610SN/A 1620SN/Avoid 1630SN/Afreefunc(n) 1640SN/A union node *n; 1650SN/A{ 1660SN/A if (n) 1670SN/A ckfree(n); 1680SN/A} 1690SN/A