| 1 | /*- |
| 2 | * Copyright (c) 1991 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * %sccs.include.redist.c% |
| 9 | * |
| 10 | * @(#)nodes.c.pat 5.1 (Berkeley) %G% |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Routine for dealing with parsed shell commands. |
| 15 | */ |
| 16 | |
| 17 | #include "shell.h" |
| 18 | #include "nodes.h" |
| 19 | #include "memalloc.h" |
| 20 | #include "machdep.h" |
| 21 | #include "mystring.h" |
| 22 | |
| 23 | |
| 24 | int funcblocksize; /* size of structures in function */ |
| 25 | int funcstringsize; /* size of strings in node */ |
| 26 | pointer funcblock; /* block to allocate function from */ |
| 27 | char *funcstring; /* block to allocate strings from */ |
| 28 | |
| 29 | %SIZES |
| 30 | |
| 31 | |
| 32 | #ifdef __STDC__ |
| 33 | STATIC void calcsize(union node *); |
| 34 | STATIC void sizenodelist(struct nodelist *); |
| 35 | STATIC union node *copynode(union node *); |
| 36 | STATIC struct nodelist *copynodelist(struct nodelist *); |
| 37 | STATIC char *nodesavestr(char *); |
| 38 | #else |
| 39 | STATIC void calcsize(); |
| 40 | STATIC void sizenodelist(); |
| 41 | STATIC union node *copynode(); |
| 42 | STATIC struct nodelist *copynodelist(); |
| 43 | STATIC char *nodesavestr(); |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * Make a copy of a parse tree. |
| 50 | */ |
| 51 | |
| 52 | union node * |
| 53 | copyfunc(n) |
| 54 | union node *n; |
| 55 | { |
| 56 | if (n == NULL) |
| 57 | return NULL; |
| 58 | funcblocksize = 0; |
| 59 | funcstringsize = 0; |
| 60 | calcsize(n); |
| 61 | funcblock = ckmalloc(funcblocksize + funcstringsize); |
| 62 | funcstring = funcblock + funcblocksize; |
| 63 | return copynode(n); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | STATIC void |
| 69 | calcsize(n) |
| 70 | union node *n; |
| 71 | { |
| 72 | %CALCSIZE |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | STATIC void |
| 78 | sizenodelist(lp) |
| 79 | struct nodelist *lp; |
| 80 | { |
| 81 | while (lp) { |
| 82 | funcblocksize += ALIGN(sizeof (struct nodelist)); |
| 83 | calcsize(lp->n); |
| 84 | lp = lp->next; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | STATIC union node * |
| 91 | copynode(n) |
| 92 | union node *n; |
| 93 | { |
| 94 | union node *new; |
| 95 | |
| 96 | %COPY |
| 97 | return new; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | STATIC struct nodelist * |
| 102 | copynodelist(lp) |
| 103 | struct nodelist *lp; |
| 104 | { |
| 105 | struct nodelist *start; |
| 106 | struct nodelist **lpp; |
| 107 | |
| 108 | lpp = &start; |
| 109 | while (lp) { |
| 110 | *lpp = funcblock; |
| 111 | funcblock += ALIGN(sizeof (struct nodelist)); |
| 112 | (*lpp)->n = copynode(lp->n); |
| 113 | lp = lp->next; |
| 114 | lpp = &(*lpp)->next; |
| 115 | } |
| 116 | *lpp = NULL; |
| 117 | return start; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | STATIC char * |
| 123 | nodesavestr(s) |
| 124 | char *s; |
| 125 | { |
| 126 | register char *p = s; |
| 127 | register char *q = funcstring; |
| 128 | char *rtn = funcstring; |
| 129 | |
| 130 | while (*q++ = *p++); |
| 131 | funcstring = q; |
| 132 | return rtn; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Free a parse tree. |
| 139 | */ |
| 140 | |
| 141 | void |
| 142 | freefunc(n) |
| 143 | union node *n; |
| 144 | { |
| 145 | if (n) |
| 146 | ckfree(n); |
| 147 | } |