Imported from ../bash-1.14.7.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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 #include <stdio.h>
24
25 #if defined (HAVE_STRING_H)
26 #  include <string.h>
27 #else /* !HAVE_STRING_H */
28 #  include <strings.h>
29 #endif /* !HAVE_STRING_H */
30
31 #include "shell.h"
32
33 WORD_DESC *
34 copy_word (word)
35      WORD_DESC *word;
36 {
37   WORD_DESC *new_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
38   FASTCOPY ((char *)word, (char *)new_word, sizeof (WORD_DESC));
39   new_word->word = savestring (word->word);
40   return (new_word);
41 }
42
43 /* Copy the chain of words in LIST.  Return a pointer to 
44    the new chain. */
45 WORD_LIST *
46 copy_word_list (list)
47      WORD_LIST *list;
48 {
49   WORD_LIST *new_list = NULL;
50
51   while (list)
52     {
53       WORD_LIST *temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
54       temp->next = new_list;
55       new_list = temp;
56       new_list->word = copy_word (list->word);
57       list = list->next;
58     }
59   return (REVERSE_LIST (new_list, WORD_LIST *));
60 }
61
62 static PATTERN_LIST *
63 copy_case_clause (clause)
64      PATTERN_LIST *clause;
65 {
66   PATTERN_LIST *new_clause = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
67   new_clause->patterns = copy_word_list (clause->patterns);
68   new_clause->action = copy_command (clause->action);
69   return (new_clause);
70 }
71
72 static PATTERN_LIST *
73 copy_case_clauses (clauses)
74      PATTERN_LIST *clauses;
75 {
76   PATTERN_LIST *new_list = (PATTERN_LIST *)NULL;
77
78   while (clauses)
79     {
80       PATTERN_LIST *new_clause = copy_case_clause (clauses);
81       new_clause->next = new_list;
82       new_list = new_clause;
83       clauses = clauses->next;
84     }
85   return (REVERSE_LIST (new_list, PATTERN_LIST *));
86 }
87
88 /* Copy a single redirect. */
89 REDIRECT *
90 copy_redirect (redirect)
91      REDIRECT *redirect;
92 {
93   REDIRECT *new_redirect = (REDIRECT *)xmalloc (sizeof (REDIRECT));
94   FASTCOPY ((char *)redirect, (char *)new_redirect, (sizeof (REDIRECT)));
95   switch (redirect->instruction)
96     {
97     case r_reading_until:
98     case r_deblank_reading_until:
99       new_redirect->here_doc_eof = savestring (redirect->here_doc_eof);
100       /* There is NO BREAK HERE ON PURPOSE!!!! */
101     case r_appending_to:
102     case r_output_direction:
103     case r_input_direction:
104     case r_inputa_direction:
105     case r_err_and_out:
106     case r_input_output:
107     case r_output_force:
108     case r_duplicating_input_word:
109     case r_duplicating_output_word:
110       new_redirect->redirectee.filename =
111         copy_word (redirect->redirectee.filename);
112       break;
113     }
114   return (new_redirect);
115 }
116   
117 REDIRECT *
118 copy_redirects (list)
119      REDIRECT *list;
120 {
121   REDIRECT *new_list = NULL;
122
123   while (list)
124     {
125       REDIRECT *temp = copy_redirect (list);
126       temp->next = new_list;
127       new_list = temp;
128       list = list->next;
129     }
130   return (REVERSE_LIST (new_list, REDIRECT *));
131 }
132   
133 static FOR_COM *
134 copy_for_command (com)
135      FOR_COM *com;
136 {
137   FOR_COM *new_for = (FOR_COM *)xmalloc (sizeof (FOR_COM));
138   new_for->flags = com->flags;
139   new_for->name = copy_word (com->name);
140   new_for->map_list = copy_word_list (com->map_list);
141   new_for->action = copy_command (com->action);
142   return (new_for);
143 }
144
145 #if defined (SELECT_COMMAND)
146 static SELECT_COM *
147 copy_select_command (com)
148      SELECT_COM *com;
149 {
150   SELECT_COM *new_select = (SELECT_COM *)xmalloc (sizeof (SELECT_COM));
151   new_select->flags = com->flags;
152   new_select->name = copy_word (com->name);
153   new_select->map_list = copy_word_list (com->map_list);
154   new_select->action = copy_command (com->action);
155   return (new_select);
156 }
157 #endif /* SELECT_COMMAND */
158
159 static GROUP_COM *
160 copy_group_command (com)
161      GROUP_COM *com;
162 {
163   GROUP_COM *new_group = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
164
165   new_group->command = copy_command (com->command);
166   return (new_group);
167 }
168
169 static CASE_COM *
170 copy_case_command (com)
171      CASE_COM *com;
172 {
173   CASE_COM *new_case = (CASE_COM *)xmalloc (sizeof (CASE_COM));
174
175   new_case->flags = com->flags;
176   new_case->word = copy_word (com->word);
177   new_case->clauses = copy_case_clauses (com->clauses);
178   return (new_case);
179 }
180
181 static WHILE_COM *
182 copy_while_command (com)
183      WHILE_COM *com;
184 {
185   WHILE_COM *new_while = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
186
187   new_while->flags = com->flags;
188   new_while->test = copy_command (com->test);
189   new_while->action = copy_command (com->action);
190   return (new_while);
191 }
192
193 static IF_COM *
194 copy_if_command (com)
195      IF_COM *com;
196 {
197   IF_COM *new_if = (IF_COM *)xmalloc (sizeof (IF_COM));
198
199   new_if->flags = com->flags;
200   new_if->test = copy_command (com->test);
201   new_if->true_case = copy_command (com->true_case);
202   new_if->false_case = copy_command (com->false_case);
203   return (new_if);
204 }
205
206 static SIMPLE_COM *
207 copy_simple_command (com)
208      SIMPLE_COM *com;
209 {
210   SIMPLE_COM *new_simple = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
211
212   new_simple->flags = com->flags;
213   new_simple->words = copy_word_list (com->words);
214   new_simple->redirects = copy_redirects (com->redirects);
215   new_simple->line = com->line;
216   return (new_simple);
217 }
218
219 static FUNCTION_DEF *
220 copy_function_def (com)
221      FUNCTION_DEF *com;
222 {
223   FUNCTION_DEF *new_def = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
224
225   new_def->name = copy_word (com->name);
226   new_def->command = copy_command (com->command);
227   return (new_def);
228 }
229
230 /* Copy the command structure in COMMAND.  Return a pointer to the
231    copy.  Don't you forget to dispose_command () on this pointer
232    later! */
233 COMMAND *
234 copy_command (command)
235      COMMAND *command;
236 {
237   COMMAND *new_command = (COMMAND *)NULL;
238
239   if (command)
240     {
241       new_command = (COMMAND *)xmalloc (sizeof (COMMAND));
242       FASTCOPY ((char *)command, (char *)new_command, sizeof (COMMAND));
243       new_command->flags = command->flags;
244       new_command->line = command->line;
245
246       if (command->redirects)
247         new_command->redirects = copy_redirects (command->redirects);
248
249       switch (command->type)
250         {
251         case cm_for:
252           new_command->value.For = copy_for_command (command->value.For);
253           break;
254
255 #if defined (SELECT_COMMAND)
256         case cm_select:
257           new_command->value.Select = copy_select_command (command->value.Select);
258           break;
259 #endif
260
261         case cm_group:
262           new_command->value.Group = copy_group_command (command->value.Group);
263           break;
264
265         case cm_case:
266           new_command->value.Case = copy_case_command (command->value.Case);
267           break;
268       
269         case cm_until:
270         case cm_while:
271           new_command->value.While = copy_while_command (command->value.While);
272           break;
273       
274         case cm_if:
275           new_command->value.If = copy_if_command (command->value.If);
276           break;
277       
278         case cm_simple:
279           new_command->value.Simple = copy_simple_command (command->value.Simple);
280           break;
281       
282         case cm_connection:
283           {
284             CONNECTION *new_connection;
285
286             new_connection = (CONNECTION *)xmalloc (sizeof (CONNECTION));
287             new_connection->connector = command->value.Connection->connector;
288             new_connection->first =
289               copy_command (command->value.Connection->first);
290             new_connection->second =
291               copy_command (command->value.Connection->second);
292             new_command->value.Connection = new_connection;
293             break;
294           }
295       
296           /* Pathological case.  I'm not even sure that you can have a
297              function definition as part of a function definition. */
298         case cm_function_def:
299           new_command->value.Function_def =
300             copy_function_def (command->value.Function_def);
301           break;
302         }
303     }
304   return (new_command);
305 }