nodes.c.pat revision 196483
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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95 33 * $FreeBSD: head/bin/sh/nodes.c.pat 196483 2009-08-23 21:09:46Z jilles $ 34 */ 35 36#include <sys/param.h> 37#include <stdlib.h> 38#include <stddef.h> 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 "mystring.h" 47 48 49STATIC int funcblocksize; /* size of structures in function */ 50STATIC int funcstringsize; /* size of strings in node */ 51STATIC pointer funcblock; /* block to allocate function from */ 52STATIC char *funcstring; /* block to allocate strings from */ 53 54%SIZES 55 56 57STATIC void calcsize(union node *); 58STATIC void sizenodelist(struct nodelist *); 59STATIC union node *copynode(union node *); 60STATIC struct nodelist *copynodelist(struct nodelist *); 61STATIC char *nodesavestr(char *); 62 63 64 65/* 66 * Make a copy of a parse tree. 67 */ 68 69struct funcdef * 70copyfunc(union node *n) 71{ 72 struct funcdef *fn; 73 74 if (n == NULL) 75 return NULL; 76 funcblocksize = offsetof(struct funcdef, n); 77 funcstringsize = 0; 78 calcsize(n); 79 fn = ckmalloc(funcblocksize + funcstringsize); 80 fn->refcount = 1; 81 funcblock = (char *)fn + offsetof(struct funcdef, n); 82 funcstring = (char *)fn + funcblocksize; 83 copynode(n); 84 return fn; 85} 86 87 88 89STATIC void 90calcsize(union node *n) 91{ 92 %CALCSIZE 93} 94 95 96 97STATIC void 98sizenodelist(struct nodelist *lp) 99{ 100 while (lp) { 101 funcblocksize += ALIGN(sizeof(struct nodelist)); 102 calcsize(lp->n); 103 lp = lp->next; 104 } 105} 106 107 108 109STATIC union node * 110copynode(union node *n) 111{ 112 union node *new; 113 114 %COPY 115 return new; 116} 117 118 119STATIC struct nodelist * 120copynodelist(struct nodelist *lp) 121{ 122 struct nodelist *start; 123 struct nodelist **lpp; 124 125 lpp = &start; 126 while (lp) { 127 *lpp = funcblock; 128 funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist)); 129 (*lpp)->n = copynode(lp->n); 130 lp = lp->next; 131 lpp = &(*lpp)->next; 132 } 133 *lpp = NULL; 134 return start; 135} 136 137 138 139STATIC char * 140nodesavestr(char *s) 141{ 142 char *p = s; 143 char *q = funcstring; 144 char *rtn = funcstring; 145 146 while ((*q++ = *p++) != '\0') 147 continue; 148 funcstring = q; 149 return rtn; 150} 151 152 153void 154reffunc(struct funcdef *fn) 155{ 156 fn->refcount++; 157} 158 159 160/* 161 * Decrement the reference count of a function definition, freeing it 162 * if it falls to 0. 163 */ 164 165void 166unreffunc(struct funcdef *fn) 167{ 168 if (fn) { 169 fn->refcount--; 170 if (fn->refcount > 0) 171 return; 172 ckfree(fn); 173 } 174} 175