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