nodes.c.pat (4398B)
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. 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 33 #include <sys/param.h> 34 #include <stdlib.h> 35 #include <stddef.h> 36 /* 37 * Routine for dealing with parsed shell commands. 38 */ 39 40 #include "shell.h" 41 #include "nodes.h" 42 #include "memalloc.h" 43 #include "mystring.h" 44 45 46 struct nodesize { 47 int blocksize; /* size of structures in function */ 48 int stringsize; /* size of strings in node */ 49 }; 50 51 struct nodecopystate { 52 pointer block; /* block to allocate function from */ 53 char *string; /* block to allocate strings from */ 54 }; 55 56 %SIZES 57 58 59 static void calcsize(union node *, struct nodesize *); 60 static void sizenodelist(struct nodelist *, struct nodesize *); 61 static union node *copynode(union node *, struct nodecopystate *); 62 static struct nodelist *copynodelist(struct nodelist *, struct nodecopystate *); 63 static char *nodesavestr(const char *, struct nodecopystate *); 64 65 66 struct funcdef { 67 unsigned int refcount; 68 union node n; 69 }; 70 71 /* 72 * Make a copy of a parse tree. 73 */ 74 75 struct funcdef * 76 copyfunc(union node *n) 77 { 78 struct nodesize sz; 79 struct nodecopystate st; 80 struct funcdef *fn; 81 82 if (n == NULL) 83 return NULL; 84 sz.blocksize = offsetof(struct funcdef, n); 85 sz.stringsize = 0; 86 calcsize(n, &sz); 87 fn = ckmalloc(sz.blocksize + sz.stringsize); 88 fn->refcount = 1; 89 st.block = (char *)fn + offsetof(struct funcdef, n); 90 st.string = (char *)fn + sz.blocksize; 91 copynode(n, &st); 92 return fn; 93 } 94 95 96 union node * 97 getfuncnode(struct funcdef *fn) 98 { 99 return fn == NULL ? NULL : &fn->n; 100 } 101 102 103 static void 104 calcsize(union node *n, struct nodesize *result) 105 { 106 %CALCSIZE 107 } 108 109 110 111 static void 112 sizenodelist(struct nodelist *lp, struct nodesize *result) 113 { 114 while (lp) { 115 result->blocksize += ALIGN(sizeof(struct nodelist)); 116 calcsize(lp->n, result); 117 lp = lp->next; 118 } 119 } 120 121 122 123 static union node * 124 copynode(union node *n, struct nodecopystate *state) 125 { 126 union node *new; 127 128 %COPY 129 return new; 130 } 131 132 133 static struct nodelist * 134 copynodelist(struct nodelist *lp, struct nodecopystate *state) 135 { 136 struct nodelist *start; 137 struct nodelist **lpp; 138 139 lpp = &start; 140 while (lp) { 141 *lpp = state->block; 142 state->block = (char *)state->block + 143 ALIGN(sizeof(struct nodelist)); 144 (*lpp)->n = copynode(lp->n, state); 145 lp = lp->next; 146 lpp = &(*lpp)->next; 147 } 148 *lpp = NULL; 149 return start; 150 } 151 152 153 154 static char * 155 nodesavestr(const char *s, struct nodecopystate *state) 156 { 157 const char *p = s; 158 char *q = state->string; 159 char *rtn = state->string; 160 161 while ((*q++ = *p++) != '\0') 162 continue; 163 state->string = q; 164 return rtn; 165 } 166 167 168 void 169 reffunc(struct funcdef *fn) 170 { 171 if (fn) 172 fn->refcount++; 173 } 174 175 176 /* 177 * Decrement the reference count of a function definition, freeing it 178 * if it falls to 0. 179 */ 180 181 void 182 unreffunc(struct funcdef *fn) 183 { 184 if (fn) { 185 fn->refcount--; 186 if (fn->refcount > 0) 187 return; 188 ckfree(fn); 189 } 190 }