7a9f2027d6176d0ac9557c4e79cb56fd48e06401
[platform/upstream/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 /* Dispose of the command structure passed. */
33 void
34 dispose_command (command)
35      COMMAND *command;
36 {
37   if (command == 0)
38     return;
39
40   if (command->redirects)
41     dispose_redirects (command->redirects);
42
43   switch (command->type)
44     {
45     case cm_for:
46 #if defined (SELECT_COMMAND)
47     case cm_select:
48 #endif
49       {
50         register FOR_COM *c;
51 #if defined (SELECT_COMMAND)
52         if (command->type == cm_select)
53           c = (FOR_COM *)command->value.Select;
54         else
55 #endif
56         c = command->value.For;
57         dispose_word (c->name);
58         dispose_words (c->map_list);
59         dispose_command (c->action);
60         free (c);
61         break;
62       }
63
64 #if defined (ARITH_FOR_COMMAND)
65     case cm_arith_for:
66       {
67         register ARITH_FOR_COM *c;
68
69         c = command->value.ArithFor;
70         dispose_words (c->init);
71         dispose_words (c->test);
72         dispose_words (c->step);
73         dispose_command (c->action);
74         free (c);
75         break;
76       }
77 #endif /* ARITH_FOR_COMMAND */
78
79     case cm_group:
80       {
81         dispose_command (command->value.Group->command);
82         free (command->value.Group);
83         break;
84       }
85
86     case cm_subshell:
87       {
88         dispose_command (command->value.Subshell->command);
89         free (command->value.Subshell);
90         break;
91       }
92
93     case cm_case:
94       {
95         register CASE_COM *c;
96         PATTERN_LIST *t, *p;
97
98         c = command->value.Case;
99         dispose_word (c->word);
100
101         for (p = c->clauses; p; )
102           {
103             dispose_words (p->patterns);
104             dispose_command (p->action);
105             t = p;
106             p = p->next;
107             free (t);
108           }
109         free (c);
110         break;
111       }
112
113     case cm_until:
114     case cm_while:
115       {
116         register WHILE_COM *c;
117
118         c = command->value.While;
119         dispose_command (c->test);
120         dispose_command (c->action);
121         free (c);
122         break;
123       }
124
125     case cm_if:
126       {
127         register IF_COM *c;
128
129         c = command->value.If;
130         dispose_command (c->test);
131         dispose_command (c->true_case);
132         dispose_command (c->false_case);
133         free (c);
134         break;
135       }
136
137     case cm_simple:
138       {
139         register SIMPLE_COM *c;
140
141         c = command->value.Simple;
142         dispose_words (c->words);
143         dispose_redirects (c->redirects);
144         free (c);
145         break;
146       }
147
148     case cm_connection:
149       {
150         register CONNECTION *c;
151
152         c = command->value.Connection;
153         dispose_command (c->first);
154         dispose_command (c->second);
155         free (c);
156         break;
157       }
158
159 #if defined (DPAREN_ARITHMETIC)
160     case cm_arith:
161       {
162         register ARITH_COM *c;
163
164         c = command->value.Arith;
165         dispose_words (c->exp);
166         free (c);
167         break;
168       }
169 #endif /* DPAREN_ARITHMETIC */
170
171 #if defined (COND_COMMAND)
172     case cm_cond:
173       {
174         register COND_COM *c;
175
176         c = command->value.Cond;
177         dispose_cond_node (c);
178         break;
179       }
180 #endif /* COND_COMMAND */
181
182     case cm_function_def:
183       {
184         register FUNCTION_DEF *c;
185
186         c = command->value.Function_def;
187         dispose_word (c->name);
188         dispose_command (c->command);
189         free (c);
190         break;
191       }
192
193     default:
194       command_error ("dispose_command", CMDERR_BADTYPE, command->type, 0);
195       break;
196     }
197   free (command);
198 }
199
200 #if defined (COND_COMMAND)
201 /* How to free a node in a conditional command. */
202 void
203 dispose_cond_node (cond)
204      COND_COM *cond;
205 {
206   if (cond)
207     {
208       if (cond->left)
209         dispose_cond_node (cond->left);
210       if (cond->right)
211         dispose_cond_node (cond->right);
212       if (cond->op)
213         dispose_word (cond->op);
214       free (cond);
215     }
216 }
217 #endif /* COND_COMMAND */
218
219 /* How to free a WORD_DESC. */
220 void
221 dispose_word (w)
222      WORD_DESC *w;
223 {
224   FREE (w->word);
225   free (w);
226 }
227
228 /* How to get rid of a linked list of words.  A WORD_LIST. */
229 void
230 dispose_words (list)
231      WORD_LIST *list;
232 {
233   WORD_LIST *t;
234
235   while (list)
236     {
237       t = list;
238       list = list->next;
239       dispose_word (t->word);
240       free (t);
241     }
242 }
243
244 #ifdef INCLUDE_UNUSED
245 /* How to dispose of an array of pointers to char.  This is identical to
246    free_array in stringlib.c. */
247 void
248 dispose_word_array (array)
249      char **array;
250 {
251   register int count;
252
253   if (array == 0)
254     return;
255
256   for (count = 0; array[count]; count++)
257     free (array[count]);
258
259   free (array);
260 }
261 #endif
262
263 /* How to dispose of an list of redirections.  A REDIRECT. */
264 void
265 dispose_redirects (list)
266      REDIRECT *list;
267 {
268   register REDIRECT *t;
269
270   while (list)
271     {
272       t = list;
273       list = list->next;
274       switch (t->instruction)
275         {
276         case r_reading_until:
277         case r_deblank_reading_until:
278           free (t->here_doc_eof);
279         /*FALLTHROUGH*/
280         case r_output_direction:
281         case r_input_direction:
282         case r_inputa_direction:
283         case r_appending_to:
284         case r_err_and_out:
285         case r_input_output:
286         case r_output_force:
287         case r_duplicating_input_word:
288         case r_duplicating_output_word:
289           dispose_word (t->redirectee.filename);
290           /* FALLTHROUGH */
291         default:
292           break;
293         }
294       free (t);
295     }
296 }