| [3228] | 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
|---|
| 22 |
|
|---|
| 23 | #include "config.h"
|
|---|
| 24 |
|
|---|
| 25 | #include "bashtypes.h"
|
|---|
| 26 |
|
|---|
| 27 | #if defined (HAVE_UNISTD_H)
|
|---|
| 28 | # include <unistd.h>
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | #include <stdio.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "shell.h"
|
|---|
| 34 |
|
|---|
| 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 *));
|
|---|
| 40 | #endif
|
|---|
| 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 *));
|
|---|
| 48 | #endif
|
|---|
| 49 | #if defined (COND_COMMAND)
|
|---|
| 50 | static COND_COM *copy_cond_command __P((COND_COM *));
|
|---|
| 51 | #endif
|
|---|
| 52 | static SIMPLE_COM *copy_simple_command __P((SIMPLE_COM *));
|
|---|
| 53 |
|
|---|
| 54 | WORD_DESC *
|
|---|
| 55 | copy_word (w)
|
|---|
| 56 | WORD_DESC *w;
|
|---|
| 57 | {
|
|---|
| 58 | WORD_DESC *new_word;
|
|---|
| 59 |
|
|---|
| 60 | new_word = make_bare_word (w->word);
|
|---|
| 61 | new_word->flags = w->flags;
|
|---|
| 62 | return (new_word);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* Copy the chain of words in LIST. Return a pointer to
|
|---|
| 66 | the new chain. */
|
|---|
| 67 | WORD_LIST *
|
|---|
| 68 | copy_word_list (list)
|
|---|
| 69 | WORD_LIST *list;
|
|---|
| 70 | {
|
|---|
| 71 | WORD_LIST *new_list;
|
|---|
| 72 |
|
|---|
| 73 | for (new_list = (WORD_LIST *)NULL; list; list = list->next)
|
|---|
| 74 | new_list = make_word_list (copy_word (list->word), new_list);
|
|---|
| 75 |
|
|---|
| 76 | return (REVERSE_LIST (new_list, WORD_LIST *));
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | static PATTERN_LIST *
|
|---|
| 80 | copy_case_clause (clause)
|
|---|
| 81 | PATTERN_LIST *clause;
|
|---|
| 82 | {
|
|---|
| 83 | PATTERN_LIST *new_clause;
|
|---|
| 84 |
|
|---|
| 85 | new_clause = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
|
|---|
| 86 | new_clause->patterns = copy_word_list (clause->patterns);
|
|---|
| 87 | new_clause->action = copy_command (clause->action);
|
|---|
| 88 | return (new_clause);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static PATTERN_LIST *
|
|---|
| 92 | copy_case_clauses (clauses)
|
|---|
| 93 | PATTERN_LIST *clauses;
|
|---|
| 94 | {
|
|---|
| 95 | PATTERN_LIST *new_list, *new_clause;
|
|---|
| 96 |
|
|---|
| 97 | for (new_list = (PATTERN_LIST *)NULL; clauses; clauses = clauses->next)
|
|---|
| 98 | {
|
|---|
| 99 | new_clause = copy_case_clause (clauses);
|
|---|
| 100 | new_clause->next = new_list;
|
|---|
| 101 | new_list = new_clause;
|
|---|
| 102 | }
|
|---|
| 103 | return (REVERSE_LIST (new_list, PATTERN_LIST *));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /* Copy a single redirect. */
|
|---|
| 107 | REDIRECT *
|
|---|
| 108 | copy_redirect (redirect)
|
|---|
| 109 | REDIRECT *redirect;
|
|---|
| 110 | {
|
|---|
| 111 | REDIRECT *new_redirect;
|
|---|
| 112 |
|
|---|
| 113 | new_redirect = (REDIRECT *)xmalloc (sizeof (REDIRECT));
|
|---|
| 114 | FASTCOPY ((char *)redirect, (char *)new_redirect, (sizeof (REDIRECT)));
|
|---|
| 115 | switch (redirect->instruction)
|
|---|
| 116 | {
|
|---|
| 117 | case r_reading_until:
|
|---|
| 118 | case r_deblank_reading_until:
|
|---|
| 119 | new_redirect->here_doc_eof = savestring (redirect->here_doc_eof);
|
|---|
| 120 | /*FALLTHROUGH*/
|
|---|
| 121 | case r_reading_string:
|
|---|
| 122 | case r_appending_to:
|
|---|
| 123 | case r_output_direction:
|
|---|
| 124 | case r_input_direction:
|
|---|
| 125 | case r_inputa_direction:
|
|---|
| 126 | case r_err_and_out:
|
|---|
| 127 | case r_input_output:
|
|---|
| 128 | case r_output_force:
|
|---|
| 129 | case r_duplicating_input_word:
|
|---|
| 130 | case r_duplicating_output_word:
|
|---|
| 131 | case r_move_input_word:
|
|---|
| 132 | case r_move_output_word:
|
|---|
| 133 | new_redirect->redirectee.filename = copy_word (redirect->redirectee.filename);
|
|---|
| 134 | break;
|
|---|
| 135 | case r_duplicating_input:
|
|---|
| 136 | case r_duplicating_output:
|
|---|
| 137 | case r_move_input:
|
|---|
| 138 | case r_move_output:
|
|---|
| 139 | case r_close_this:
|
|---|
| 140 | break;
|
|---|
| 141 | }
|
|---|
| 142 | return (new_redirect);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | REDIRECT *
|
|---|
| 146 | copy_redirects (list)
|
|---|
| 147 | REDIRECT *list;
|
|---|
| 148 | {
|
|---|
| 149 | REDIRECT *new_list, *temp;
|
|---|
| 150 |
|
|---|
| 151 | for (new_list = (REDIRECT *)NULL; list; list = list->next)
|
|---|
| 152 | {
|
|---|
| 153 | temp = copy_redirect (list);
|
|---|
| 154 | temp->next = new_list;
|
|---|
| 155 | new_list = temp;
|
|---|
| 156 | }
|
|---|
| 157 | return (REVERSE_LIST (new_list, REDIRECT *));
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | static FOR_COM *
|
|---|
| 161 | copy_for_command (com)
|
|---|
| 162 | FOR_COM *com;
|
|---|
| 163 | {
|
|---|
| 164 | FOR_COM *new_for;
|
|---|
| 165 |
|
|---|
| 166 | new_for = (FOR_COM *)xmalloc (sizeof (FOR_COM));
|
|---|
| 167 | new_for->flags = com->flags;
|
|---|
| 168 | new_for->line = com->line;
|
|---|
| 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);
|
|---|
| 172 | return (new_for);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | #if defined (ARITH_FOR_COMMAND)
|
|---|
| 176 | static ARITH_FOR_COM *
|
|---|
| 177 | copy_arith_for_command (com)
|
|---|
| 178 | ARITH_FOR_COM *com;
|
|---|
| 179 | {
|
|---|
| 180 | ARITH_FOR_COM *new_arith_for;
|
|---|
| 181 |
|
|---|
| 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);
|
|---|
| 190 | }
|
|---|
| 191 | #endif /* ARITH_FOR_COMMAND */
|
|---|
| 192 |
|
|---|
| 193 | static GROUP_COM *
|
|---|
| 194 | copy_group_command (com)
|
|---|
| 195 | GROUP_COM *com;
|
|---|
| |
|---|