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. */
5 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
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)
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.
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. */
25 #include "bashtypes.h"
27 #if defined (HAVE_UNISTD_H)
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 *));
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 *));
49 #if defined (COND_COMMAND)
50 static COND_COM *copy_cond_command __P((COND_COM *));
52 static SIMPLE_COM *copy_simple_command __P((SIMPLE_COM *));
53 static FUNCTION_DEF *copy_function_def __P((FUNCTION_DEF *));
61 new_word = make_bare_word (w->word);
62 new_word->flags = w->flags;
66 /* Copy the chain of words in LIST. Return a pointer to
74 for (new_list = (WORD_LIST *)NULL; list; list = list->next)
75 new_list = make_word_list (copy_word (list->word), new_list);
77 return (REVERSE_LIST (new_list, WORD_LIST *));
81 copy_case_clause (clause)
84 PATTERN_LIST *new_clause;
86 new_clause = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
87 new_clause->patterns = copy_word_list (clause->patterns);
88 new_clause->action = copy_command (clause->action);
93 copy_case_clauses (clauses)
94 PATTERN_LIST *clauses;
96 PATTERN_LIST *new_list, *new_clause;
98 for (new_list = (PATTERN_LIST *)NULL; clauses; clauses = clauses->next)
100 new_clause = copy_case_clause (clauses);
101 new_clause->next = new_list;
102 new_list = new_clause;
104 return (REVERSE_LIST (new_list, PATTERN_LIST *));
107 /* Copy a single redirect. */
109 copy_redirect (redirect)
112 REDIRECT *new_redirect;
114 new_redirect = (REDIRECT *)xmalloc (sizeof (REDIRECT));
115 FASTCOPY ((char *)redirect, (char *)new_redirect, (sizeof (REDIRECT)));
116 switch (redirect->instruction)
118 case r_reading_until:
119 case r_deblank_reading_until:
120 new_redirect->here_doc_eof = savestring (redirect->here_doc_eof);
122 case r_reading_string:
124 case r_output_direction:
125 case r_input_direction:
126 case r_inputa_direction:
130 case r_duplicating_input_word:
131 case r_duplicating_output_word:
132 case r_move_input_word:
133 case r_move_output_word:
134 new_redirect->redirectee.filename = copy_word (redirect->redirectee.filename);
136 case r_duplicating_input:
137 case r_duplicating_output:
143 return (new_redirect);
147 copy_redirects (list)
150 REDIRECT *new_list, *temp;
152 for (new_list = (REDIRECT *)NULL; list; list = list->next)
154 temp = copy_redirect (list);
155 temp->next = new_list;
158 return (REVERSE_LIST (new_list, REDIRECT *));
162 copy_for_command (com)
167 new_for = (FOR_COM *)xmalloc (sizeof (FOR_COM));
168 new_for->flags = com->flags;
169 new_for->name = copy_word (com->name);
170 new_for->map_list = copy_word_list (com->map_list);
171 new_for->action = copy_command (com->action);
175 #if defined (ARITH_FOR_COMMAND)
176 static ARITH_FOR_COM *
177 copy_arith_for_command (com)
180 ARITH_FOR_COM *new_arith_for;
182 new_arith_for = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM));
183 new_arith_for->flags = com->flags;
184 new_arith_for->line = com->line;
185 new_arith_for->init = copy_word_list (com->init);
186 new_arith_for->test = copy_word_list (com->test);
187 new_arith_for->step = copy_word_list (com->step);
188 new_arith_for->action = copy_command (com->action);
189 return (new_arith_for);
191 #endif /* ARITH_FOR_COMMAND */
194 copy_group_command (com)
197 GROUP_COM *new_group;
199 new_group = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
200 new_group->command = copy_command (com->command);
204 static SUBSHELL_COM *
205 copy_subshell_command (com)
208 SUBSHELL_COM *new_subshell;
210 new_subshell = (SUBSHELL_COM *)xmalloc (sizeof (SUBSHELL_COM));
211 new_subshell->command = copy_command (com->command);
212 new_subshell->flags = com->flags;
213 return (new_subshell);
217 copy_case_command (com)
222 new_case = (CASE_COM *)xmalloc (sizeof (CASE_COM));
223 new_case->flags = com->flags;
224 new_case->word = copy_word (com->word);
225 new_case->clauses = copy_case_clauses (com->clauses);
230 copy_while_command (com)
233 WHILE_COM *new_while;
235 new_while = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
236 new_while->flags = com->flags;
237 new_while->test = copy_command (com->test);
238 new_while->action = copy_command (com->action);
243 copy_if_command (com)
248 new_if = (IF_COM *)xmalloc (sizeof (IF_COM));
249 new_if->flags = com->flags;
250 new_if->test = copy_command (com->test);
251 new_if->true_case = copy_command (com->true_case);
252 new_if->false_case = com->false_case ? copy_command (com->false_case) : com->false_case;
256 #if defined (DPAREN_ARITHMETIC)
258 copy_arith_command (com)
261 ARITH_COM *new_arith;
263 new_arith = (ARITH_COM *)xmalloc (sizeof (ARITH_COM));
264 new_arith->flags = com->flags;
265 new_arith->exp = copy_word_list (com->exp);
266 new_arith->line = com->line;
272 #if defined (COND_COMMAND)
274 copy_cond_command (com)
279 new_cond = (COND_COM *)xmalloc (sizeof (COND_COM));
280 new_cond->flags = com->flags;
281 new_cond->line = com->line;
282 new_cond->type = com->type;
283 new_cond->op = com->op ? copy_word (com->op) : com->op;
284 new_cond->left = com->left ? copy_cond_command (com->left) : (COND_COM *)NULL;
285 new_cond->right = com->right ? copy_cond_command (com->right) : (COND_COM *)NULL;
292 copy_simple_command (com)
295 SIMPLE_COM *new_simple;
297 new_simple = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
298 new_simple->flags = com->flags;
299 new_simple->words = copy_word_list (com->words);
300 new_simple->redirects = com->redirects ? copy_redirects (com->redirects) : (REDIRECT *)NULL;
301 new_simple->line = com->line;
305 static FUNCTION_DEF *
306 copy_function_def (com)
309 FUNCTION_DEF *new_def;
311 new_def = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
312 new_def->name = copy_word (com->name);
313 new_def->command = copy_command (com->command);
314 new_def->flags = com->flags;
315 new_def->line = com->line;
319 /* Copy the command structure in COMMAND. Return a pointer to the
320 copy. Don't you forget to dispose_command () on this pointer
323 copy_command (command)
326 COMMAND *new_command;
331 new_command = (COMMAND *)xmalloc (sizeof (COMMAND));
332 FASTCOPY ((char *)command, (char *)new_command, sizeof (COMMAND));
333 new_command->flags = command->flags;
334 new_command->line = command->line;
336 if (command->redirects)
337 new_command->redirects = copy_redirects (command->redirects);
339 switch (command->type)
342 new_command->value.For = copy_for_command (command->value.For);
345 #if defined (ARITH_FOR_COMMAND)
347 new_command->value.ArithFor = copy_arith_for_command (command->value.ArithFor);
351 #if defined (SELECT_COMMAND)
353 new_command->value.Select =
354 (SELECT_COM *)copy_for_command ((FOR_COM *)command->value.Select);
359 new_command->value.Group = copy_group_command (command->value.Group);
363 new_command->value.Subshell = copy_subshell_command (command->value.Subshell);
367 new_command->value.Case = copy_case_command (command->value.Case);
372 new_command->value.While = copy_while_command (command->value.While);
376 new_command->value.If = copy_if_command (command->value.If);
379 #if defined (DPAREN_ARITHMETIC)
381 new_command->value.Arith = copy_arith_command (command->value.Arith);
385 #if defined (COND_COMMAND)
387 new_command->value.Cond = copy_cond_command (command->value.Cond);
392 new_command->value.Simple = copy_simple_command (command->value.Simple);
397 CONNECTION *new_connection;
399 new_connection = (CONNECTION *)xmalloc (sizeof (CONNECTION));
400 new_connection->connector = command->value.Connection->connector;
401 new_connection->first = copy_command (command->value.Connection->first);
402 new_connection->second = copy_command (command->value.Connection->second);
403 new_command->value.Connection = new_connection;
407 case cm_function_def:
408 new_command->value.Function_def = copy_function_def (command->value.Function_def);
411 return (new_command);