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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "bashtypes.h"
27 #if defined (HAVE_UNISTD_H)
41 new_word
= (WORD_DESC
*)xmalloc (sizeof (WORD_DESC
));
42 FASTCOPY ((char *)w
, (char *)new_word
, sizeof (WORD_DESC
));
43 new_word
->word
= savestring (w
->word
);
47 /* Copy the chain of words in LIST. Return a pointer to
53 WORD_LIST
*new_list
, *temp
;
55 for (new_list
= (WORD_LIST
*)NULL
; list
; list
= list
->next
)
57 temp
= (WORD_LIST
*)xmalloc (sizeof (WORD_LIST
));
58 temp
->next
= new_list
;
60 new_list
->word
= copy_word (list
->word
);
62 return (REVERSE_LIST (new_list
, WORD_LIST
*));
66 copy_case_clause (clause
)
69 PATTERN_LIST
*new_clause
;
71 new_clause
= (PATTERN_LIST
*)xmalloc (sizeof (PATTERN_LIST
));
72 new_clause
->patterns
= copy_word_list (clause
->patterns
);
73 new_clause
->action
= copy_command (clause
->action
);
78 copy_case_clauses (clauses
)
79 PATTERN_LIST
*clauses
;
81 PATTERN_LIST
*new_list
, *new_clause
;
83 for (new_list
= (PATTERN_LIST
*)NULL
; clauses
; clauses
= clauses
->next
)
85 new_clause
= copy_case_clause (clauses
);
86 new_clause
->next
= new_list
;
87 new_list
= new_clause
;
89 return (REVERSE_LIST (new_list
, PATTERN_LIST
*));
92 /* Copy a single redirect. */
94 copy_redirect (redirect
)
97 REDIRECT
*new_redirect
;
99 new_redirect
= (REDIRECT
*)xmalloc (sizeof (REDIRECT
));
100 FASTCOPY ((char *)redirect
, (char *)new_redirect
, (sizeof (REDIRECT
)));
101 switch (redirect
->instruction
)
103 case r_reading_until
:
104 case r_deblank_reading_until
:
105 new_redirect
->here_doc_eof
= savestring (redirect
->here_doc_eof
);
108 case r_output_direction
:
109 case r_input_direction
:
110 case r_inputa_direction
:
114 case r_duplicating_input_word
:
115 case r_duplicating_output_word
:
116 new_redirect
->redirectee
.filename
= copy_word (redirect
->redirectee
.filename
);
118 case r_duplicating_input
:
119 case r_duplicating_output
:
123 return (new_redirect
);
127 copy_redirects (list
)
130 REDIRECT
*new_list
, *temp
;
132 for (new_list
= (REDIRECT
*)NULL
; list
; list
= list
->next
)
134 temp
= copy_redirect (list
);
135 temp
->next
= new_list
;
138 return (REVERSE_LIST (new_list
, REDIRECT
*));
142 copy_for_command (com
)
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
);
156 copy_group_command (com
)
159 GROUP_COM
*new_group
;
161 new_group
= (GROUP_COM
*)xmalloc (sizeof (GROUP_COM
));
162 new_group
->command
= copy_command (com
->command
);
167 copy_case_command (com
)
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
);
180 copy_while_command (com
)
183 WHILE_COM
*new_while
;
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
);
193 copy_if_command (com
)
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
);
206 #if defined (DPAREN_ARITHMETIC)
208 copy_arith_command (com
)
211 ARITH_COM
*new_arith
;
213 new_arith
= (ARITH_COM
*)xmalloc (sizeof (ARITH_COM
));
214 new_arith
->flags
= com
->flags
;
215 new_arith
->exp
= copy_word_list (com
->exp
);
216 new_arith
->line
= com
->line
;
222 #if defined (COND_COMMAND)
224 copy_cond_command (com
)
229 new_cond
= (COND_COM
*)xmalloc (sizeof (COND_COM
));
230 new_cond
->flags
= com
->flags
;
231 new_cond
->line
= com
->line
;
232 new_cond
->type
= com
->type
;
233 new_cond
->op
= com
->op
? copy_word (com
->op
) : com
->op
;
234 new_cond
->left
= com
->left
? copy_cond_command (com
->left
) : (COND_COM
*)NULL
;
235 new_cond
->right
= com
->right
? copy_cond_command (com
->right
) : (COND_COM
*)NULL
;
242 copy_simple_command (com
)
245 SIMPLE_COM
*new_simple
;
247 new_simple
= (SIMPLE_COM
*)xmalloc (sizeof (SIMPLE_COM
));
248 new_simple
->flags
= com
->flags
;
249 new_simple
->words
= copy_word_list (com
->words
);
250 new_simple
->redirects
= copy_redirects (com
->redirects
);
251 new_simple
->line
= com
->line
;
255 static FUNCTION_DEF
*
256 copy_function_def (com
)
259 FUNCTION_DEF
*new_def
;
261 new_def
= (FUNCTION_DEF
*)xmalloc (sizeof (FUNCTION_DEF
));
262 new_def
->name
= copy_word (com
->name
);
263 new_def
->command
= copy_command (com
->command
);
267 /* Copy the command structure in COMMAND. Return a pointer to the
268 copy. Don't you forget to dispose_command () on this pointer
271 copy_command (command
)
274 COMMAND
*new_command
;
279 new_command
= (COMMAND
*)xmalloc (sizeof (COMMAND
));
280 FASTCOPY ((char *)command
, (char *)new_command
, sizeof (COMMAND
));
281 new_command
->flags
= command
->flags
;
282 new_command
->line
= command
->line
;
284 if (command
->redirects
)
285 new_command
->redirects
= copy_redirects (command
->redirects
);
287 switch (command
->type
)
290 new_command
->value
.For
= copy_for_command (command
->value
.For
);
293 #if defined (SELECT_COMMAND)
295 new_command
->value
.Select
=
296 (SELECT_COM
*)copy_for_command ((FOR_COM
*)command
->value
.Select
);
301 new_command
->value
.Group
= copy_group_command (command
->value
.Group
);
305 new_command
->value
.Case
= copy_case_command (command
->value
.Case
);
310 new_command
->value
.While
= copy_while_command (command
->value
.While
);
314 new_command
->value
.If
= copy_if_command (command
->value
.If
);
317 #if defined (DPAREN_ARITHMETIC)
319 new_command
->value
.Arith
= copy_arith_command (command
->value
.Arith
);
323 #if defined (COND_COMMAND)
325 new_command
->value
.Cond
= copy_cond_command (command
->value
.Cond
);
330 new_command
->value
.Simple
= copy_simple_command (command
->value
.Simple
);
335 CONNECTION
*new_connection
;
337 new_connection
= (CONNECTION
*)xmalloc (sizeof (CONNECTION
));
338 new_connection
->connector
= command
->value
.Connection
->connector
;
339 new_connection
->first
= copy_command (command
->value
.Connection
->first
);
340 new_connection
->second
= copy_command (command
->value
.Connection
->second
);
341 new_command
->value
.Connection
= new_connection
;
345 case cm_function_def
:
346 new_command
->value
.Function_def
= copy_function_def (command
->value
.Function_def
);
349 return (new_command
);