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