Commit | Line | Data |
---|---|---|
e83ac6ec KB |
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 | * | |
b775bf58 | 10 | * @(#)nodes.c.pat 5.2 (Berkeley) %G% |
e83ac6ec KB |
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 */ | |
b775bf58 | 26 | #ifdef __STDC__ |
e83ac6ec | 27 | pointer funcblock; /* block to allocate function from */ |
b775bf58 KB |
28 | #else |
29 | char *funcblock; /* block to allocate function from */ | |
30 | #endif | |
e83ac6ec KB |
31 | char *funcstring; /* block to allocate strings from */ |
32 | ||
33 | %SIZES | |
34 | ||
35 | ||
36 | #ifdef __STDC__ | |
37 | STATIC void calcsize(union node *); | |
38 | STATIC void sizenodelist(struct nodelist *); | |
39 | STATIC union node *copynode(union node *); | |
40 | STATIC struct nodelist *copynodelist(struct nodelist *); | |
41 | STATIC char *nodesavestr(char *); | |
42 | #else | |
43 | STATIC void calcsize(); | |
44 | STATIC void sizenodelist(); | |
45 | STATIC union node *copynode(); | |
46 | STATIC struct nodelist *copynodelist(); | |
47 | STATIC char *nodesavestr(); | |
48 | #endif | |
49 | ||
50 | ||
51 | ||
52 | /* | |
53 | * Make a copy of a parse tree. | |
54 | */ | |
55 | ||
56 | union node * | |
57 | copyfunc(n) | |
58 | union node *n; | |
59 | { | |
60 | if (n == NULL) | |
61 | return NULL; | |
62 | funcblocksize = 0; | |
63 | funcstringsize = 0; | |
64 | calcsize(n); | |
65 | funcblock = ckmalloc(funcblocksize + funcstringsize); | |
66 | funcstring = funcblock + funcblocksize; | |
67 | return copynode(n); | |
68 | } | |
69 | ||
70 | ||
71 | ||
72 | STATIC void | |
73 | calcsize(n) | |
74 | union node *n; | |
75 | { | |
76 | %CALCSIZE | |
77 | } | |
78 | ||
79 | ||
80 | ||
81 | STATIC void | |
82 | sizenodelist(lp) | |
83 | struct nodelist *lp; | |
84 | { | |
85 | while (lp) { | |
86 | funcblocksize += ALIGN(sizeof (struct nodelist)); | |
87 | calcsize(lp->n); | |
88 | lp = lp->next; | |
89 | } | |
90 | } | |
91 | ||
92 | ||
93 | ||
94 | STATIC union node * | |
95 | copynode(n) | |
96 | union node *n; | |
97 | { | |
98 | union node *new; | |
99 | ||
100 | %COPY | |
101 | return new; | |
102 | } | |
103 | ||
104 | ||
105 | STATIC struct nodelist * | |
106 | copynodelist(lp) | |
107 | struct nodelist *lp; | |
108 | { | |
109 | struct nodelist *start; | |
110 | struct nodelist **lpp; | |
111 | ||
112 | lpp = &start; | |
113 | while (lp) { | |
114 | *lpp = funcblock; | |
115 | funcblock += ALIGN(sizeof (struct nodelist)); | |
116 | (*lpp)->n = copynode(lp->n); | |
117 | lp = lp->next; | |
118 | lpp = &(*lpp)->next; | |
119 | } | |
120 | *lpp = NULL; | |
121 | return start; | |
122 | } | |
123 | ||
124 | ||
125 | ||
126 | STATIC char * | |
127 | nodesavestr(s) | |
128 | char *s; | |
129 | { | |
130 | register char *p = s; | |
131 | register char *q = funcstring; | |
132 | char *rtn = funcstring; | |
133 | ||
134 | while (*q++ = *p++); | |
135 | funcstring = q; | |
136 | return rtn; | |
137 | } | |
138 | ||
139 | ||
140 | ||
141 | /* | |
142 | * Free a parse tree. | |
143 | */ | |
144 | ||
145 | void | |
146 | freefunc(n) | |
147 | union node *n; | |
148 | { | |
149 | if (n) | |
150 | ckfree(n); | |
151 | } |