home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / copy_cmd.c < prev    next >
C/C++ Source or Header  |  1991-12-05  |  7KB  |  280 lines

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