Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / copy_cmd.c
1 /* copy_command.c -- copy a COMMAND structure.  This is needed
2    primarily for making function definitions, but I'm not sure
3    that anyone else will need it.  */
4
5 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
6
7    This file is part of GNU Bash, the Bourne Again SHell.
8
9    Bash is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13
14    Bash is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Bash; see the file COPYING.  If not, write to the Free
21    Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 #include "config.h"
24
25 #include "bashtypes.h"
26
27 #if defined (HAVE_UNISTD_H)
28 #  include <unistd.h>
29 #endif
30
31 #include <stdio.h>
32
33 #include "shell.h"
34
35 static PATTERN_LIST *copy_case_clause __P((PATTERN_LIST *));
36 static PATTERN_LIST *copy_case_clauses __P((PATTERN_LIST *));
37 static FOR_COM *copy_for_command __P((FOR_COM *));
38 #if defined (ARITH_FOR_COMMAND)
39 static ARITH_FOR_COM *copy_arith_for_command __P((ARITH_FOR_COM *));
40 #endif
41 static GROUP_COM *copy_group_command __P((GROUP_COM *));
42 static SUBSHELL_COM *copy_subshell_command __P((SUBSHELL_COM *));
43 static CASE_COM *copy_case_command __P((CASE_COM *));
44 static WHILE_COM *copy_while_command __P((WHILE_COM *));
45 static IF_COM *copy_if_command __P((IF_COM *));
46 #if defined (DPAREN_ARITHMETIC)
47 static ARITH_COM *copy_arith_command __P((ARITH_COM *));
48 #endif
49 #if defined (COND_COMMAND)
50 static COND_COM *copy_cond_command __P((COND_COM *));
51 #endif
52 static SIMPLE_COM *copy_simple_command __P((SIMPLE_COM *));
53 static FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *));
54
55 WORD_DESC *
56 copy_word (w)
57      WORD_DESC *w;
58 {
59   WORD_DESC *new_word;
60
61   new_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
62 #if 1
63   new_word->flags = w->flags;
64 #else
65   FASTCOPY ((char *)w, (char *)new_word, sizeof (WORD_DESC));
66 #endif
67   new_word->word = savestring (w->word);
68   return (new_word);
69 }
70
71 /* Copy the chain of words in LIST.  Return a pointer to
72    the new chain. */
73 WORD_LIST *
74 copy_word_list (list)
75      WORD_LIST *list;
76 {
77   WORD_LIST *new_list, *temp;
78
79   for (new_list = (WORD_LIST *)NULL; list; list = list->next)
80     {
81       temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
82       temp->next = new_list;
83       new_list = temp;
84       new_list->word = copy_word (list->word);
85     }
86   return (REVERSE_LIST (new_list, WORD_LIST *));
87 }
88
89 static PATTERN_LIST *
90 copy_case_clause (clause)
91      PATTERN_LIST *clause;
92 {
93   PATTERN_LIST *new_clause;
94
95   new_clause = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
96   new_clause->patterns = copy_word_list (clause->patterns);
97   new_clause->action = copy_command (clause->action);
98   return (new_clause);
99 }
100
101 static PATTERN_LIST *
102 copy_case_clauses (clauses)
103      PATTERN_LIST *clauses;
104 {
105   PATTERN_LIST *new_list, *new_clause;
106
107   for (new_list = (PATTERN_LIST *)NULL; clauses; clauses = clauses->next)
108     {
109       new_clause = copy_case_clause (clauses);
110       new_clause->next = new_list;
111       new_list = new_clause;
112     }
113   return (REVERSE_LIST (new_list, PATTERN_LIST *));
114 }
115
116 /* Copy a single redirect. */
117 REDIRECT *
118 copy_redirect (redirect)
119      REDIRECT *redirect;
120 {
121   REDIRECT *new_redirect;
122
123   new_redirect = (REDIRECT *)xmalloc (sizeof (REDIRECT));
124   FASTCOPY ((char *)redirect, (char *)new_redirect, (sizeof (REDIRECT)));
125   switch (redirect->instruction)
126     {
127     case r_reading_until:
128     case r_deblank_reading_until:
129       new_redirect->here_doc_eof = savestring (redirect->here_doc_eof);
130       /*FALLTHROUGH*/
131     case r_appending_to:
132     case r_output_direction:
133     case r_input_direction:
134     case r_inputa_direction:
135     case r_err_and_out:
136     case r_input_output:
137     case r_output_force:
138     case r_duplicating_input_word:
139     case r_duplicating_output_word:
140       new_redirect->redirectee.filename = copy_word (redirect->redirectee.filename);
141       break;
142     case r_duplicating_input:
143     case r_duplicating_output:
144     case r_close_this:
145       break;
146     }
147   return (new_redirect);
148 }
149
150 REDIRECT *
151 copy_redirects (list)
152      REDIRECT *list;
153 {
154   REDIRECT *new_list, *temp;
155
156   for (new_list = (REDIRECT *)NULL; list; list = list->next)
157     {
158       temp = copy_redirect (list);
159       temp->next = new_list;
160       new_list = temp;
161     }
162   return (REVERSE_LIST (new_list, REDIRECT *));
163 }
164
165 static FOR_COM *
166 copy_for_command (com)
167      FOR_COM *com;
168 {
169   FOR_COM *new_for;
170
171   new_for = (FOR_COM *)xmalloc (sizeof (FOR_COM));
172   new_for->flags = com->flags;
173   new_for->name = copy_word (com->name);
174   new_for->map_list = copy_word_list (com->map_list);
175   new_for->action = copy_command (com->action);
176   return (new_for);
177 }
178
179 #if defined (ARITH_FOR_COMMAND)
180 static ARITH_FOR_COM *
181 copy_arith_for_command (com)
182      ARITH_FOR_COM *com;
183 {
184   ARITH_FOR_COM *new_arith_for;
185
186   new_arith_for = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
187   new_arith_for->flags = com->flags;
188   new_arith_for->line = com->line;
189   new_arith_for->init = copy_word_list (com->init);
190   new_arith_for->test = copy_word_list (com->test);
191   new_arith_for->step = copy_word_list (com->step);
192   new_arith_for->action = copy_command (com->action);
193   return (new_arith_for);
194 }
195 #endif /* ARITH_FOR_COMMAND */
196
197 static GROUP_COM *
198 copy_group_command (com)
199      GROUP_COM *com;
200 {
201   GROUP_COM *new_group;
202
203   new_group = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
204   new_group->command = copy_command (com->command);
205   return (new_group);
206 }
207
208 static SUBSHELL_COM *
209 copy_subshell_command (com)
210      SUBSHELL_COM *com;
211 {
212   SUBSHELL_COM *new_subshell;
213
214   new_subshell = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
215   new_subshell->command = copy_command (com->command);
216   new_subshell->flags = com->flags;
217   return (new_subshell);
218 }
219
220 static CASE_COM *
221 copy_case_command (com)
222      CASE_COM *com;
223 {
224   CASE_COM *new_case;
225
226   new_case = (CASE_COM *)xmalloc (sizeof (CASE_COM));
227   new_case->flags = com->flags;
228   new_case->word = copy_word (com->word);
229   new_case->clauses = copy_case_clauses (com->clauses);
230   return (new_case);
231 }
232
233 static WHILE_COM *
234 copy_while_command (com)
235      WHILE_COM *com;
236 {
237   WHILE_COM *new_while;
238
239   new_while = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
240   new_while->flags = com->flags;
241   new_while->test = copy_command (com->test);
242   new_while->action = copy_command (com->action);
243   return (new_while);
244 }
245
246 static IF_COM *
247 copy_if_command (com)
248      IF_COM *com;
249 {
250   IF_COM *new_if;
251
252   new_if = (IF_COM *)xmalloc (sizeof (IF_COM));
253   new_if->flags = com->flags;
254   new_if->test = copy_command (com->test);
255   new_if->true_case = copy_command (com->true_case);
256   new_if->false_case = copy_command (com->false_case);
257   return (new_if);
258 }
259
260 #if defined (DPAREN_ARITHMETIC)
261 static ARITH_COM *
262 copy_arith_command (com)
263      ARITH_COM *com;
264 {
265   ARITH_COM *new_arith;
266
267   new_arith = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
268   new_arith->flags = com->flags;
269   new_arith->exp = copy_word_list (com->exp);
270   new_arith->line = com->line;
271
272   return (new_arith);
273 }
274 #endif
275
276 #if defined (COND_COMMAND)
277 static COND_COM *
278 copy_cond_command (com)
279      COND_COM *com;
280 {
281   COND_COM *new_cond;
282
283   new_cond = (COND_COM *)xmalloc (sizeof (COND_COM));
284   new_cond->flags = com->flags;
285   new_cond->line = com->line;
286   new_cond->type = com->type;
287   new_cond->op = com->op ? copy_word (com->op) : com->op;
288   new_cond->left = com->left ? copy_cond_command (com->left) : (COND_COM *)NULL;
289   new_cond->right = com->right ? copy_cond_command (com->right) : (COND_COM *)NULL;
290
291   return (new_cond);
292 }
293 #endif
294
295 static SIMPLE_COM *
296 copy_simple_command (com)
297      SIMPLE_COM *com;
298 {
299   SIMPLE_COM *new_simple;
300
301   new_simple = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
302   new_simple->flags = com->flags;
303   new_simple->words = copy_word_list (com->words);
304   new_simple->redirects = copy_redirects (com->redirects);
305   new_simple->line = com->line;
306   return (new_simple);
307 }
308
309 static FUNCTION_DEF *
310 copy_function_def (com)
311      FUNCTION_DEF *com;
312 {
313   FUNCTION_DEF *new_def;
314
315   new_def = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
316   new_def->name = copy_word (com->name);
317   new_def->command = copy_command (com->command);
318   new_def->flags = com->flags;
319   new_def->line = com->line;
320   return (new_def);
321 }
322
323 /* Copy the command structure in COMMAND.  Return a pointer to the
324    copy.  Don't you forget to dispose_command () on this pointer
325    later! */
326 COMMAND *
327 copy_command (command)
328      COMMAND *command;
329 {
330   COMMAND *new_command;
331
332   if (command == NULL)
333     return (command);
334
335   new_command = (COMMAND *)xmalloc (sizeof (COMMAND));
336   FASTCOPY ((char *)command, (char *)new_command, sizeof (COMMAND));
337   new_command->flags = command->flags;
338   new_command->line = command->line;
339
340   if (command->redirects)
341     new_command->redirects = copy_redirects (command->redirects);
342
343   switch (command->type)
344     {
345       case cm_for:
346         new_command->value.For = copy_for_command (command->value.For);
347         break;
348
349 #if defined (ARITH_FOR_COMMAND)
350       case cm_arith_for:
351         new_command->value.ArithFor = copy_arith_for_command (command->value.ArithFor);
352         break;
353 #endif
354
355 #if defined (SELECT_COMMAND)
356       case cm_select:
357         new_command->value.Select =
358           (SELECT_COM *)copy_for_command ((FOR_COM *)command->value.Select);
359         break;
360 #endif
361
362       case cm_group:
363         new_command->value.Group = copy_group_command (command->value.Group);
364         break;
365
366       case cm_subshell:
367         new_command->value.Subshell = copy_subshell_command (command->value.Subshell);
368         break;
369
370       case cm_case:
371         new_command->value.Case = copy_case_command (command->value.Case);
372         break;
373
374       case cm_until:
375       case cm_while:
376         new_command->value.While = copy_while_command (command->value.While);
377         break;
378
379       case cm_if:
380         new_command->value.If = copy_if_command (command->value.If);
381         break;
382
383 #if defined (DPAREN_ARITHMETIC)
384       case cm_arith:
385         new_command->value.Arith = copy_arith_command (command->value.Arith);
386         break;
387 #endif
388
389 #if defined (COND_COMMAND)
390       case cm_cond:
391         new_command->value.Cond = copy_cond_command (command->value.Cond);
392         break;
393 #endif
394
395       case cm_simple:
396         new_command->value.Simple = copy_simple_command (command->value.Simple);
397         break;
398
399       case cm_connection:
400         {
401           CONNECTION *new_connection;
402
403           new_connection = (CONNECTION *)xmalloc (sizeof (CONNECTION));
404           new_connection->connector = command->value.Connection->connector;
405           new_connection->first = copy_command (command->value.Connection->first);
406           new_connection->second = copy_command (command->value.Connection->second);
407           new_command->value.Connection = new_connection;
408           break;
409         }
410
411       case cm_function_def:
412         new_command->value.Function_def = copy_function_def (command->value.Function_def);
413         break;
414     }
415   return (new_command);
416 }