blob: 826e0c3a4650b1833888176d50c93d62a2ce9d4e [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* 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
Jari Aalto31859422009-01-12 13:36:28 +00005/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00006
7 This file is part of GNU Bash, the Bourne Again SHell.
8
Jari Aalto31859422009-01-12 13:36:28 +00009 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000013
Jari Aalto31859422009-01-12 13:36:28 +000014 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000018
19 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000020 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21*/
Jari Aalto726f6381996-08-26 18:22:31 +000022
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023#include "config.h"
24
Jari Aaltocce855b1998-04-17 19:52:44 +000025#include "bashtypes.h"
Jari Aalto726f6381996-08-26 18:22:31 +000026
Jari Aaltoccc6cda1996-12-23 17:02:34 +000027#if defined (HAVE_UNISTD_H)
28# include <unistd.h>
29#endif
30
Jari Aaltocce855b1998-04-17 19:52:44 +000031#include <stdio.h>
32
Jari Aalto726f6381996-08-26 18:22:31 +000033#include "shell.h"
34
Jari Aaltof73dda02001-11-13 17:56:06 +000035static PATTERN_LIST *copy_case_clause __P((PATTERN_LIST *));
36static PATTERN_LIST *copy_case_clauses __P((PATTERN_LIST *));
37static FOR_COM *copy_for_command __P((FOR_COM *));
38#if defined (ARITH_FOR_COMMAND)
39static ARITH_FOR_COM *copy_arith_for_command __P((ARITH_FOR_COM *));
40#endif
41static GROUP_COM *copy_group_command __P((GROUP_COM *));
42static SUBSHELL_COM *copy_subshell_command __P((SUBSHELL_COM *));
Jari Aalto31859422009-01-12 13:36:28 +000043static COPROC_COM *copy_coproc_command __P((COPROC_COM *));
Jari Aaltof73dda02001-11-13 17:56:06 +000044static CASE_COM *copy_case_command __P((CASE_COM *));
45static WHILE_COM *copy_while_command __P((WHILE_COM *));
46static IF_COM *copy_if_command __P((IF_COM *));
47#if defined (DPAREN_ARITHMETIC)
48static ARITH_COM *copy_arith_command __P((ARITH_COM *));
49#endif
50#if defined (COND_COMMAND)
51static COND_COM *copy_cond_command __P((COND_COM *));
52#endif
53static SIMPLE_COM *copy_simple_command __P((SIMPLE_COM *));
Jari Aaltof73dda02001-11-13 17:56:06 +000054
Jari Aalto726f6381996-08-26 18:22:31 +000055WORD_DESC *
Jari Aaltocce855b1998-04-17 19:52:44 +000056copy_word (w)
57 WORD_DESC *w;
Jari Aalto726f6381996-08-26 18:22:31 +000058{
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059 WORD_DESC *new_word;
60
Jari Aalto7117c2d2002-07-17 14:10:11 +000061 new_word = make_bare_word (w->word);
Jari Aalto28ef6c32001-04-06 19:14:31 +000062 new_word->flags = w->flags;
Jari Aalto726f6381996-08-26 18:22:31 +000063 return (new_word);
64}
65
Jari Aaltoccc6cda1996-12-23 17:02:34 +000066/* Copy the chain of words in LIST. Return a pointer to
Jari Aalto726f6381996-08-26 18:22:31 +000067 the new chain. */
68WORD_LIST *
69copy_word_list (list)
70 WORD_LIST *list;
71{
Jari Aalto7117c2d2002-07-17 14:10:11 +000072 WORD_LIST *new_list;
Jari Aalto726f6381996-08-26 18:22:31 +000073
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074 for (new_list = (WORD_LIST *)NULL; list; list = list->next)
Jari Aalto7117c2d2002-07-17 14:10:11 +000075 new_list = make_word_list (copy_word (list->word), new_list);
76
Jari Aalto726f6381996-08-26 18:22:31 +000077 return (REVERSE_LIST (new_list, WORD_LIST *));
78}
79
80static PATTERN_LIST *
81copy_case_clause (clause)
82 PATTERN_LIST *clause;
83{
Jari Aaltoccc6cda1996-12-23 17:02:34 +000084 PATTERN_LIST *new_clause;
85
86 new_clause = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
Jari Aalto726f6381996-08-26 18:22:31 +000087 new_clause->patterns = copy_word_list (clause->patterns);
88 new_clause->action = copy_command (clause->action);
Jari Aalto31859422009-01-12 13:36:28 +000089 new_clause->flags = clause->flags;
Jari Aalto726f6381996-08-26 18:22:31 +000090 return (new_clause);
91}
92
93static PATTERN_LIST *
94copy_case_clauses (clauses)
95 PATTERN_LIST *clauses;
96{
Jari Aaltoccc6cda1996-12-23 17:02:34 +000097 PATTERN_LIST *new_list, *new_clause;
Jari Aalto726f6381996-08-26 18:22:31 +000098
Jari Aaltoccc6cda1996-12-23 17:02:34 +000099 for (new_list = (PATTERN_LIST *)NULL; clauses; clauses = clauses->next)
Jari Aalto726f6381996-08-26 18:22:31 +0000100 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000101 new_clause = copy_case_clause (clauses);
Jari Aalto726f6381996-08-26 18:22:31 +0000102 new_clause->next = new_list;
103 new_list = new_clause;
Jari Aalto726f6381996-08-26 18:22:31 +0000104 }
105 return (REVERSE_LIST (new_list, PATTERN_LIST *));
106}
107
108/* Copy a single redirect. */
109REDIRECT *
110copy_redirect (redirect)
111 REDIRECT *redirect;
112{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000113 REDIRECT *new_redirect;
114
115 new_redirect = (REDIRECT *)xmalloc (sizeof (REDIRECT));
Chet Ramey00018032011-11-21 20:51:19 -0500116#if 0
Jari Aalto726f6381996-08-26 18:22:31 +0000117 FASTCOPY ((char *)redirect, (char *)new_redirect, (sizeof (REDIRECT)));
Chet Ramey00018032011-11-21 20:51:19 -0500118#else
119 *new_redirect = *redirect; /* let the compiler do the fast structure copy */
120#endif
121
122 if (redirect->rflags & REDIR_VARASSIGN)
123 new_redirect->redirector.filename = copy_word (redirect->redirector.filename);
124
Jari Aalto726f6381996-08-26 18:22:31 +0000125 switch (redirect->instruction)
126 {
127 case r_reading_until:
128 case r_deblank_reading_until:
Chet Rameya417a512014-10-02 22:50:19 -0400129 new_redirect->here_doc_eof = redirect->here_doc_eof ? savestring (redirect->here_doc_eof) : 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000130 /*FALLTHROUGH*/
Jari Aalto7117c2d2002-07-17 14:10:11 +0000131 case r_reading_string:
Jari Aalto726f6381996-08-26 18:22:31 +0000132 case r_appending_to:
133 case r_output_direction:
134 case r_input_direction:
135 case r_inputa_direction:
136 case r_err_and_out:
Jari Aalto31859422009-01-12 13:36:28 +0000137 case r_append_err_and_out:
Jari Aalto726f6381996-08-26 18:22:31 +0000138 case r_input_output:
139 case r_output_force:
140 case r_duplicating_input_word:
141 case r_duplicating_output_word:
Jari Aalto7117c2d2002-07-17 14:10:11 +0000142 case r_move_input_word:
143 case r_move_output_word:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000144 new_redirect->redirectee.filename = copy_word (redirect->redirectee.filename);
Jari Aalto726f6381996-08-26 18:22:31 +0000145 break;
Jari Aaltod166f041997-06-05 14:59:13 +0000146 case r_duplicating_input:
147 case r_duplicating_output:
Jari Aalto7117c2d2002-07-17 14:10:11 +0000148 case r_move_input:
149 case r_move_output:
Jari Aaltod166f041997-06-05 14:59:13 +0000150 case r_close_this:
151 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000152 }
153 return (new_redirect);
154}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000155
Jari Aalto726f6381996-08-26 18:22:31 +0000156REDIRECT *
157copy_redirects (list)
158 REDIRECT *list;
159{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000160 REDIRECT *new_list, *temp;
Jari Aalto726f6381996-08-26 18:22:31 +0000161
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000162 for (new_list = (REDIRECT *)NULL; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +0000163 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000164 temp = copy_redirect (list);
Jari Aalto726f6381996-08-26 18:22:31 +0000165 temp->next = new_list;
166 new_list = temp;
Jari Aalto726f6381996-08-26 18:22:31 +0000167 }
168 return (REVERSE_LIST (new_list, REDIRECT *));
169}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000170
Jari Aalto726f6381996-08-26 18:22:31 +0000171static FOR_COM *
172copy_for_command (com)
173 FOR_COM *com;
174{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000175 FOR_COM *new_for;
176
177 new_for = (FOR_COM *)xmalloc (sizeof (FOR_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000178 new_for->flags = com->flags;
Jari Aaltob80f6442004-07-27 13:29:18 +0000179 new_for->line = com->line;
Jari Aalto726f6381996-08-26 18:22:31 +0000180 new_for->name = copy_word (com->name);
181 new_for->map_list = copy_word_list (com->map_list);
182 new_for->action = copy_command (com->action);
183 return (new_for);
184}
185
Jari Aaltobb706242000-03-17 21:46:59 +0000186#if defined (ARITH_FOR_COMMAND)
187static ARITH_FOR_COM *
188copy_arith_for_command (com)
189 ARITH_FOR_COM *com;
190{
191 ARITH_FOR_COM *new_arith_for;
192
193 new_arith_for = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
194 new_arith_for->flags = com->flags;
195 new_arith_for->line = com->line;
196 new_arith_for->init = copy_word_list (com->init);
197 new_arith_for->test = copy_word_list (com->test);
198 new_arith_for->step = copy_word_list (com->step);
199 new_arith_for->action = copy_command (com->action);
200 return (new_arith_for);
201}
202#endif /* ARITH_FOR_COMMAND */
203
Jari Aalto726f6381996-08-26 18:22:31 +0000204static GROUP_COM *
205copy_group_command (com)
206 GROUP_COM *com;
207{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000208 GROUP_COM *new_group;
Jari Aalto726f6381996-08-26 18:22:31 +0000209
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000210 new_group = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000211 new_group->command = copy_command (com->command);
212 return (new_group);
213}
214
Jari Aaltobb706242000-03-17 21:46:59 +0000215static SUBSHELL_COM *
216copy_subshell_command (com)
217 SUBSHELL_COM *com;
218{
219 SUBSHELL_COM *new_subshell;
220
221 new_subshell = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
222 new_subshell->command = copy_command (com->command);
223 new_subshell->flags = com->flags;
224 return (new_subshell);
225}
226
Jari Aalto31859422009-01-12 13:36:28 +0000227static COPROC_COM *
228copy_coproc_command (com)
229 COPROC_COM *com;
230{
231 COPROC_COM *new_coproc;
232
233 new_coproc = (COPROC_COM *)xmalloc (sizeof (COPROC_COM));
234 new_coproc->name = savestring (com->name);
235 new_coproc->command = copy_command (com->command);
236 new_coproc->flags = com->flags;
237 return (new_coproc);
238}
239
Jari Aalto726f6381996-08-26 18:22:31 +0000240static CASE_COM *
241copy_case_command (com)
242 CASE_COM *com;
243{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000244 CASE_COM *new_case;
Jari Aalto726f6381996-08-26 18:22:31 +0000245
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000246 new_case = (CASE_COM *)xmalloc (sizeof (CASE_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000247 new_case->flags = com->flags;
Jari Aaltob80f6442004-07-27 13:29:18 +0000248 new_case->line = com->line;
Jari Aalto726f6381996-08-26 18:22:31 +0000249 new_case->word = copy_word (com->word);
250 new_case->clauses = copy_case_clauses (com->clauses);
251 return (new_case);
252}
253
254static WHILE_COM *
255copy_while_command (com)
256 WHILE_COM *com;
257{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000258 WHILE_COM *new_while;
Jari Aalto726f6381996-08-26 18:22:31 +0000259
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000260 new_while = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000261 new_while->flags = com->flags;
262 new_while->test = copy_command (com->test);
263 new_while->action = copy_command (com->action);
264 return (new_while);
265}
266
267static IF_COM *
268copy_if_command (com)
269 IF_COM *com;
270{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000271 IF_COM *new_if;
Jari Aalto726f6381996-08-26 18:22:31 +0000272
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000273 new_if = (IF_COM *)xmalloc (sizeof (IF_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000274 new_if->flags = com->flags;
275 new_if->test = copy_command (com->test);
276 new_if->true_case = copy_command (com->true_case);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000277 new_if->false_case = com->false_case ? copy_command (com->false_case) : com->false_case;
Jari Aalto726f6381996-08-26 18:22:31 +0000278 return (new_if);
279}
280
Jari Aaltocce855b1998-04-17 19:52:44 +0000281#if defined (DPAREN_ARITHMETIC)
282static ARITH_COM *
283copy_arith_command (com)
284 ARITH_COM *com;
285{
286 ARITH_COM *new_arith;
287
288 new_arith = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
289 new_arith->flags = com->flags;
290 new_arith->exp = copy_word_list (com->exp);
291 new_arith->line = com->line;
292
293 return (new_arith);
294}
295#endif
296
297#if defined (COND_COMMAND)
298static COND_COM *
299copy_cond_command (com)
300 COND_COM *com;
301{
302 COND_COM *new_cond;
303
304 new_cond = (COND_COM *)xmalloc (sizeof (COND_COM));
305 new_cond->flags = com->flags;
306 new_cond->line = com->line;
Jari Aaltobc4cd231998-07-23 14:37:54 +0000307 new_cond->type = com->type;
308 new_cond->op = com->op ? copy_word (com->op) : com->op;
Jari Aaltocce855b1998-04-17 19:52:44 +0000309 new_cond->left = com->left ? copy_cond_command (com->left) : (COND_COM *)NULL;
310 new_cond->right = com->right ? copy_cond_command (com->right) : (COND_COM *)NULL;
311
312 return (new_cond);
313}
314#endif
315
Jari Aalto726f6381996-08-26 18:22:31 +0000316static SIMPLE_COM *
317copy_simple_command (com)
318 SIMPLE_COM *com;
319{
Jari Aaltocce855b1998-04-17 19:52:44 +0000320 SIMPLE_COM *new_simple;
Jari Aalto726f6381996-08-26 18:22:31 +0000321
Jari Aaltocce855b1998-04-17 19:52:44 +0000322 new_simple = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
Jari Aalto726f6381996-08-26 18:22:31 +0000323 new_simple->flags = com->flags;
324 new_simple->words = copy_word_list (com->words);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000325 new_simple->redirects = com->redirects ? copy_redirects (com->redirects) : (REDIRECT *)NULL;
Jari Aalto726f6381996-08-26 18:22:31 +0000326 new_simple->line = com->line;
327 return (new_simple);
328}
329
Jari Aaltob80f6442004-07-27 13:29:18 +0000330FUNCTION_DEF *
331copy_function_def_contents (old, new_def)
332 FUNCTION_DEF *old, *new_def;
333{
334 new_def->name = copy_word (old->name);
335 new_def->command = old->command ? copy_command (old->command) : old->command;
336 new_def->flags = old->flags;
337 new_def->line = old->line;
338 new_def->source_file = old->source_file ? savestring (old->source_file) : old->source_file;
339 return (new_def);
340}
341
342FUNCTION_DEF *
Jari Aalto726f6381996-08-26 18:22:31 +0000343copy_function_def (com)
344 FUNCTION_DEF *com;
345{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000346 FUNCTION_DEF *new_def;
Jari Aalto726f6381996-08-26 18:22:31 +0000347
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000348 new_def = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
Jari Aaltob80f6442004-07-27 13:29:18 +0000349 new_def = copy_function_def_contents (com, new_def);
Jari Aalto726f6381996-08-26 18:22:31 +0000350 return (new_def);
351}
352
353/* Copy the command structure in COMMAND. Return a pointer to the
354 copy. Don't you forget to dispose_command () on this pointer
355 later! */
356COMMAND *
357copy_command (command)
358 COMMAND *command;
359{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000360 COMMAND *new_command;
Jari Aalto726f6381996-08-26 18:22:31 +0000361
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000362 if (command == NULL)
363 return (command);
364
365 new_command = (COMMAND *)xmalloc (sizeof (COMMAND));
366 FASTCOPY ((char *)command, (char *)new_command, sizeof (COMMAND));
367 new_command->flags = command->flags;
368 new_command->line = command->line;
369
370 if (command->redirects)
371 new_command->redirects = copy_redirects (command->redirects);
372
373 switch (command->type)
Jari Aalto726f6381996-08-26 18:22:31 +0000374 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000375 case cm_for:
376 new_command->value.For = copy_for_command (command->value.For);
377 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000378
Jari Aaltobb706242000-03-17 21:46:59 +0000379#if defined (ARITH_FOR_COMMAND)
380 case cm_arith_for:
381 new_command->value.ArithFor = copy_arith_for_command (command->value.ArithFor);
382 break;
383#endif
384
Jari Aalto726f6381996-08-26 18:22:31 +0000385#if defined (SELECT_COMMAND)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000386 case cm_select:
387 new_command->value.Select =
388 (SELECT_COM *)copy_for_command ((FOR_COM *)command->value.Select);
389 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000390#endif
391
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000392 case cm_group:
393 new_command->value.Group = copy_group_command (command->value.Group);
394 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000395
Jari Aaltobb706242000-03-17 21:46:59 +0000396 case cm_subshell:
Jari Aalto28ef6c32001-04-06 19:14:31 +0000397 new_command->value.Subshell = copy_subshell_command (command->value.Subshell);
398 break;
Jari Aaltobb706242000-03-17 21:46:59 +0000399
Jari Aalto31859422009-01-12 13:36:28 +0000400 case cm_coproc:
401 new_command->value.Coproc = copy_coproc_command (command->value.Coproc);
402 break;
403
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000404 case cm_case:
405 new_command->value.Case = copy_case_command (command->value.Case);
406 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000407
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000408 case cm_until:
409 case cm_while:
410 new_command->value.While = copy_while_command (command->value.While);
411 break;
412
413 case cm_if:
414 new_command->value.If = copy_if_command (command->value.If);
415 break;
416
Jari Aaltocce855b1998-04-17 19:52:44 +0000417#if defined (DPAREN_ARITHMETIC)
418 case cm_arith:
Jari Aalto28ef6c32001-04-06 19:14:31 +0000419 new_command->value.Arith = copy_arith_command (command->value.Arith);
420 break;
Jari Aaltocce855b1998-04-17 19:52:44 +0000421#endif
422
423#if defined (COND_COMMAND)
424 case cm_cond:
425 new_command->value.Cond = copy_cond_command (command->value.Cond);
426 break;
427#endif
428
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000429 case cm_simple:
430 new_command->value.Simple = copy_simple_command (command->value.Simple);
431 break;
432
433 case cm_connection:
434 {
435 CONNECTION *new_connection;
436
437 new_connection = (CONNECTION *)xmalloc (sizeof (CONNECTION));
438 new_connection->connector = command->value.Connection->connector;
439 new_connection->first = copy_command (command->value.Connection->first);
440 new_connection->second = copy_command (command->value.Connection->second);
441 new_command->value.Connection = new_connection;
Jari Aalto726f6381996-08-26 18:22:31 +0000442 break;
443 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000444
445 case cm_function_def:
446 new_command->value.Function_def = copy_function_def (command->value.Function_def);
447 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000448 }
449 return (new_command);
450}