4bf2c04295b32a095a7310ee9c64e2c033ae3d6d
[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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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     case cm_group:
65       {
66         dispose_command (command->value.Group->command);
67         free (command->value.Group);
68         break;
69       }
70
71     case cm_case:
72       {
73         register CASE_COM *c;
74         PATTERN_LIST *t, *p;
75
76         c = command->value.Case;
77         dispose_word (c->word);
78
79         for (p = c->clauses; p; )
80           {
81             dispose_words (p->patterns);
82             dispose_command (p->action);
83             t = p;
84             p = p->next;
85             free (t);
86           }
87         free (c);
88         break;
89       }
90
91     case cm_until:
92     case cm_while:
93       {
94         register WHILE_COM *c;
95
96         c = command->value.While;
97         dispose_command (c->test);
98         dispose_command (c->action);
99         free (c);
100         break;
101       }
102
103     case cm_if:
104       {
105         register IF_COM *c;
106
107         c = command->value.If;
108         dispose_command (c->test);
109         dispose_command (c->true_case);
110         dispose_command (c->false_case);
111         free (c);
112         break;
113       }
114
115     case cm_simple:
116       {
117         register SIMPLE_COM *c;
118
119         c = command->value.Simple;
120         dispose_words (c->words);
121         dispose_redirects (c->redirects);
122         free (c);
123         break;
124       }
125
126     case cm_connection:
127       {
128         register CONNECTION *c;
129
130         c = command->value.Connection;
131         dispose_command (c->first);
132         dispose_command (c->second);
133         free (c);
134         break;
135       }
136
137 #if defined (DPAREN_ARITHMETIC)
138     case cm_arith:
139       {
140         register ARITH_COM *c;
141
142         c = command->value.Arith;
143         dispose_words (c->exp);
144         free (c);
145         break;
146       }
147 #endif /* DPAREN_ARITHMETIC */
148
149 #if defined (COND_COMMAND)
150     case cm_cond:
151       {
152         register COND_COM *c;
153
154         c = command->value.Cond;
155         dispose_cond_node (c);
156         break;
157       }
158 #endif /* COND_COMMAND */
159
160     case cm_function_def:
161       {
162         register FUNCTION_DEF *c;
163
164         c = command->value.Function_def;
165         dispose_word (c->name);
166         dispose_command (c->command);
167         free (c);
168         break;
169       }
170
171     default:
172       programming_error ("dispose_command: bad command type `%d'", command->type);
173       break;
174     }
175   free (command);
176 }
177
178 #if defined (COND_COMMAND)
179 /* How to free a node in a conditional command. */
180 void
181 dispose_cond_node (cond)
182      COND_COM *cond;
183 {
184   if (cond)
185     {
186       if (cond->left)
187         dispose_cond_node (cond->left);
188       if (cond->right)
189         dispose_cond_node (cond->right);
190       if (cond->op)
191         dispose_word (cond->op);
192       free (cond);
193     }
194 }
195 #endif /* COND_COMMAND */
196
197 /* How to free a WORD_DESC. */
198 void
199 dispose_word (w)
200      WORD_DESC *w;
201 {
202   FREE (w->word);
203   free (w);
204 }
205
206 /* How to get rid of a linked list of words.  A WORD_LIST. */
207 void
208 dispose_words (list)
209      WORD_LIST *list;
210 {
211   WORD_LIST *t;
212
213   while (list)
214     {
215       t = list;
216       list = list->next;
217       dispose_word (t->word);
218       free (t);
219     }
220 }
221
222 #ifdef INCLUDE_UNUSED
223 /* How to dispose of an array of pointers to char.  This is identical to
224    free_array in stringlib.c. */
225 void
226 dispose_word_array (array)
227      char **array;
228 {
229   register int count;
230
231   if (array == 0)
232     return;
233
234   for (count = 0; array[count]; count++)
235     free (array[count]);
236
237   free (array);
238 }
239 #endif
240
241 /* How to dispose of an list of redirections.  A REDIRECT. */
242 void
243 dispose_redirects (list)
244      REDIRECT *list;
245 {
246   register REDIRECT *t;
247
248   while (list)
249     {
250       t = list;
251       list = list->next;
252       switch (t->instruction)
253         {
254         case r_reading_until:
255         case r_deblank_reading_until:
256           free (t->here_doc_eof);
257         /*FALLTHROUGH*/
258         case r_output_direction:
259         case r_input_direction:
260         case r_inputa_direction:
261         case r_appending_to:
262         case r_err_and_out:
263         case r_input_output:
264         case r_output_force:
265         case r_duplicating_input_word:
266         case r_duplicating_output_word:
267           dispose_word (t->redirectee.filename);
268           /* FALLTHROUGH */
269         default:
270           break;
271         }
272       free (t);
273     }
274 }