]> git.ipfire.org Git - thirdparty/bash.git/blob - dispose_cmd.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / dispose_cmd.c
1 /* dispose_command.c -- dispose of a COMMAND structure. */
2
3 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
7 Bash is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bash is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 #include "config.h"
22
23 #include "bashtypes.h"
24
25 #if defined (HAVE_UNISTD_H)
26 # include <unistd.h>
27 #endif
28
29 #include "bashansi.h"
30 #include "shell.h"
31
32 extern sh_obj_cache_t wdcache, wlcache;
33
34 /* Dispose of the command structure passed. */
35 void
36 dispose_command (command)
37 COMMAND *command;
38 {
39 if (command == 0)
40 return;
41
42 if (command->redirects)
43 dispose_redirects (command->redirects);
44
45 switch (command->type)
46 {
47 case cm_for:
48 #if defined (SELECT_COMMAND)
49 case cm_select:
50 #endif
51 {
52 register FOR_COM *c;
53 #if defined (SELECT_COMMAND)
54 if (command->type == cm_select)
55 c = (FOR_COM *)command->value.Select;
56 else
57 #endif
58 c = command->value.For;
59 dispose_word (c->name);
60 dispose_words (c->map_list);
61 dispose_command (c->action);
62 free (c);
63 break;
64 }
65
66 #if defined (ARITH_FOR_COMMAND)
67 case cm_arith_for:
68 {
69 register ARITH_FOR_COM *c;
70
71 c = command->value.ArithFor;
72 dispose_words (c->init);
73 dispose_words (c->test);
74 dispose_words (c->step);
75 dispose_command (c->action);
76 free (c);
77 break;
78 }
79 #endif /* ARITH_FOR_COMMAND */
80
81 case cm_group:
82 {
83 dispose_command (command->value.Group->command);
84 free (command->value.Group);
85 break;
86 }
87
88 case cm_subshell:
89 {
90 dispose_command (command->value.Subshell->command);
91 free (command->value.Subshell);
92 break;
93 }
94
95 case cm_case:
96 {
97 register CASE_COM *c;
98 PATTERN_LIST *t, *p;
99
100 c = command->value.Case;
101 dispose_word (c->word);
102
103 for (p = c->clauses; p; )
104 {
105 dispose_words (p->patterns);
106 dispose_command (p->action);
107 t = p;
108 p = p->next;
109 free (t);
110 }
111 free (c);
112 break;
113 }
114
115 case cm_until:
116 case cm_while:
117 {
118 register WHILE_COM *c;
119
120 c = command->value.While;
121 dispose_command (c->test);
122 dispose_command (c->action);
123 free (c);
124 break;
125 }
126
127 case cm_if:
128 {
129 register IF_COM *c;
130
131 c = command->value.If;
132 dispose_command (c->test);
133 dispose_command (c->true_case);
134 dispose_command (c->false_case);
135 free (c);
136 break;
137 }
138
139 case cm_simple:
140 {
141 register SIMPLE_COM *c;
142
143 c = command->value.Simple;
144 dispose_words (c->words);
145 dispose_redirects (c->redirects);
146 free (c);
147 break;
148 }
149
150 case cm_connection:
151 {
152 register CONNECTION *c;
153
154 c = command->value.Connection;
155 dispose_command (c->first);
156 dispose_command (c->second);
157 free (c);
158 break;
159 }
160
161 #if defined (DPAREN_ARITHMETIC)
162 case cm_arith:
163 {
164 register ARITH_COM *c;
165
166 c = command->value.Arith;
167 dispose_words (c->exp);
168 free (c);
169 break;
170 }
171 #endif /* DPAREN_ARITHMETIC */
172
173 #if defined (COND_COMMAND)
174 case cm_cond:
175 {
176 register COND_COM *c;
177
178 c = command->value.Cond;
179 dispose_cond_node (c);
180 break;
181 }
182 #endif /* COND_COMMAND */
183
184 case cm_function_def:
185 {
186 register FUNCTION_DEF *c;
187
188 c = command->value.Function_def;
189 dispose_word (c->name);
190 dispose_command (c->command);
191 free (c);
192 break;
193 }
194
195 default:
196 command_error ("dispose_command", CMDERR_BADTYPE, command->type, 0);
197 break;
198 }
199 free (command);
200 }
201
202 #if defined (COND_COMMAND)
203 /* How to free a node in a conditional command. */
204 void
205 dispose_cond_node (cond)
206 COND_COM *cond;
207 {
208 if (cond)
209 {
210 if (cond->left)
211 dispose_cond_node (cond->left);
212 if (cond->right)
213 dispose_cond_node (cond->right);
214 if (cond->op)
215 dispose_word (cond->op);
216 free (cond);
217 }
218 }
219 #endif /* COND_COMMAND */
220
221 /* How to free a WORD_DESC. */
222 void
223 dispose_word (w)
224 WORD_DESC *w;
225 {
226 FREE (w->word);
227 #if 0
228 free (w);
229 #else
230 ocache_free (wdcache, WORD_DESC, w);
231 #endif
232 }
233
234 /* How to get rid of a linked list of words. A WORD_LIST. */
235 void
236 dispose_words (list)
237 WORD_LIST *list;
238 {
239 WORD_LIST *t;
240
241 while (list)
242 {
243 t = list;
244 list = list->next;
245 dispose_word (t->word);
246 #if 0
247 free (t);
248 #else
249 ocache_free (wlcache, WORD_LIST, t);
250 #endif
251 }
252 }
253
254 #ifdef INCLUDE_UNUSED
255 /* How to dispose of an array of pointers to char. This is identical to
256 free_array in stringlib.c. */
257 void
258 dispose_word_array (array)
259 char **array;
260 {
261 register int count;
262
263 if (array == 0)
264 return;
265
266 for (count = 0; array[count]; count++)
267 free (array[count]);
268
269 free (array);
270 }
271 #endif
272
273 /* How to dispose of an list of redirections. A REDIRECT. */
274 void
275 dispose_redirects (list)
276 REDIRECT *list;
277 {
278 register REDIRECT *t;
279
280 while (list)
281 {
282 t = list;
283 list = list->next;
284 switch (t->instruction)
285 {
286 case r_reading_until:
287 case r_deblank_reading_until:
288 free (t->here_doc_eof);
289 /*FALLTHROUGH*/
290 case r_reading_string:
291 case r_output_direction:
292 case r_input_direction:
293 case r_inputa_direction:
294 case r_appending_to:
295 case r_err_and_out:
296 case r_input_output:
297 case r_output_force:
298 case r_duplicating_input_word:
299 case r_duplicating_output_word:
300 case r_move_input_word:
301 case r_move_output_word:
302 dispose_word (t->redirectee.filename);
303 /* FALLTHROUGH */
304 default:
305 break;
306 }
307 free (t);
308 }
309 }