home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / make_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-11  |  15.6 KB  |  667 lines

  1. /* make_command.c --
  2.    Functions for making instances of the various parser constructs. */
  3.  
  4. /* Copyright (C) 1989 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/file.h>
  25.  
  26. extern int errno;
  27. extern char *strerror ();
  28.  
  29. #if defined (HAVE_VPRINTF)
  30. #include <varargs.h>
  31. #endif
  32.  
  33. #include "shell.h"
  34. #include "flags.h"
  35. #include "filecntl.h"
  36.  
  37. #if defined (JOB_CONTROL)
  38. #include "jobs.h"
  39. #endif
  40.  
  41. WORD_DESC *
  42. make_word (string)
  43.      char *string;
  44. {
  45.   WORD_DESC *temp;
  46.  
  47.   temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  48.   temp->word = savestring (string);
  49.   temp->quoted = temp->dollar_present = temp->assignment = 0;
  50.  
  51.   while (*string)
  52.     {
  53.       if (*string == '$') temp->dollar_present = 1;
  54.  
  55.       if (member (*string, "'`\\\""))
  56.     {
  57.       temp->quoted = 1;
  58.       if (*string == '\\')
  59.         string++;
  60.     }
  61.  
  62.       if (*string)
  63.     (string++);
  64.     }
  65.   return (temp);
  66. }
  67.  
  68. WORD_DESC *
  69. make_word_from_token (token)
  70.      int token;
  71. {
  72.   char tokenizer[2];
  73.  
  74.   tokenizer[0] = token;
  75.   tokenizer[1] = '\0';
  76.  
  77.   return (make_word (tokenizer));
  78. }
  79.  
  80. WORD_LIST *
  81. make_word_list (word, link)
  82.      WORD_DESC *word;
  83.      WORD_LIST *link;
  84. {
  85.   WORD_LIST *temp;
  86.  
  87.   temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  88.   temp->word = word;
  89.   temp->next = link;
  90.   return (temp);
  91. }
  92.  
  93. WORD_LIST *
  94. add_string_to_list (string, list)
  95.      char *string;
  96.      WORD_LIST *list;
  97. {
  98.   WORD_LIST *temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  99.   temp->word = make_word (string);
  100.   temp->next = list;
  101.   return (temp);
  102. }
  103.  
  104. WORD_DESC *
  105. coerce_to_word (number)
  106.      int number;
  107. {
  108.   char *string;
  109.  
  110.   string = (char *)alloca (24);
  111.   sprintf (string, "%d", number);
  112.   return (make_word (string));
  113. }
  114.  
  115. COMMAND *
  116. make_command (type, pointer)
  117.      enum command_type type;
  118.      SIMPLE_COM *pointer;
  119. {
  120.   COMMAND *temp;
  121.  
  122.   temp = (COMMAND *)xmalloc (sizeof (COMMAND));
  123.   temp->type = type;
  124.   temp->value.Simple = pointer;
  125.   temp->value.Simple->flags = 0;
  126.   temp->flags = 0;
  127.   temp->redirects = (REDIRECT *)NULL;
  128.   return (temp);
  129. }
  130.  
  131. COMMAND *
  132. command_connect (com1, com2, connector)
  133.      COMMAND *com1, *com2;
  134.      int connector;
  135. {
  136.   CONNECTION *temp;
  137.  
  138.   temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
  139.   temp->connector = connector;
  140.   temp->first = com1;
  141.   temp->second = com2;
  142.   return (make_command (cm_connection, (SIMPLE_COM *)temp));
  143. }
  144.  
  145. COMMAND *
  146. make_for_command (name, map_list, action)
  147.      WORD_DESC *name;
  148.      WORD_LIST *map_list;
  149.      COMMAND *action;
  150. {
  151.   FOR_COM *temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
  152.  
  153.   temp->flags = 0;
  154.   temp->name = name;
  155.   temp->map_list = map_list;
  156.   temp->action = action;
  157.   return (make_command (cm_for, (SIMPLE_COM *)temp));
  158. }
  159.  
  160. COMMAND *
  161. make_group_command (command)
  162.      COMMAND *command;
  163. {
  164.   GROUP_COM *temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
  165.  
  166.   temp->command = command;
  167.   return (make_command (cm_group, (SIMPLE_COM *)temp));
  168. }
  169.  
  170. COMMAND *
  171. make_case_command (word, clauses)
  172.      WORD_DESC *word;
  173.      PATTERN_LIST *clauses;
  174. {
  175.   extern GENERIC_LIST *reverse_list ();
  176.   CASE_COM *temp;
  177.  
  178.   temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
  179.   temp->flags = 0;
  180.   temp->word = word;
  181.   temp->clauses = (PATTERN_LIST *)reverse_list (clauses);
  182.   return (make_command (cm_case, (SIMPLE_COM *)temp));
  183. }
  184.  
  185. PATTERN_LIST *
  186. make_pattern_list (patterns, action)
  187.      WORD_LIST *patterns;
  188.      COMMAND *action;
  189. {
  190.   PATTERN_LIST *temp;
  191.  
  192.   temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
  193.   temp->patterns = patterns;
  194.   temp->action = action;
  195.   temp->next = NULL;
  196.   return (temp);
  197. }
  198.  
  199. COMMAND *
  200. make_if_command (test, true_case, false_case)
  201.      COMMAND *test, *true_case, *false_case;
  202. {
  203.   IF_COM *temp;
  204.  
  205.   temp = (IF_COM *)xmalloc (sizeof (IF_COM));
  206.   temp->flags = 0;
  207.   temp->test = test;
  208.   temp->true_case = true_case;
  209.   temp->false_case = false_case;
  210.   return (make_command (cm_if, (SIMPLE_COM *)temp));
  211. }
  212.  
  213. COMMAND *
  214. make_until_or_while (test, action, which)
  215.      COMMAND *test, *action;
  216.      enum command_type which;
  217. {
  218.   WHILE_COM *temp;
  219.  
  220.   temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
  221.   temp->flags = 0;
  222.   temp->test = test;
  223.   temp->action = action;
  224.   return (make_command (which, (SIMPLE_COM *)temp));
  225. }
  226.  
  227. COMMAND *
  228. make_while_command (test, action)
  229.      COMMAND *test, *action;
  230. {
  231.   return (make_until_or_while (test, action, cm_while));
  232. }
  233.  
  234. COMMAND *
  235. make_until_command (test, action)
  236.      COMMAND *test, *action;
  237. {
  238.   return (make_until_or_while (test, action, cm_until));
  239. }
  240.  
  241. COMMAND *
  242. make_bare_simple_command ()
  243. {
  244.   COMMAND *command;
  245.   SIMPLE_COM *temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
  246.  
  247.   temp->flags = 0;
  248.   temp->words = (WORD_LIST *)NULL;
  249.   temp->redirects = (REDIRECT *)NULL;
  250.   command = (COMMAND *)xmalloc (sizeof (COMMAND));
  251.   command->type = cm_simple;
  252.   command->redirects = (REDIRECT *)NULL;
  253.   command->flags = 0;
  254.   command->value.Simple = temp;
  255.   return (command);
  256. }
  257.  
  258. /* Return a command which is the connection of the word or redirection
  259.    in ELEMENT, and the command * or NULL in COMMAND. */
  260. COMMAND *
  261. make_simple_command (element, command)
  262.      ELEMENT element;
  263.      COMMAND *command;
  264. {
  265.   /* If we are starting from scratch, then make the initial command
  266.      structure.  Also note that we have to fill in all the slots, since
  267.      malloc doesn't return zeroed space. */
  268.   if (!command)
  269.     command = make_bare_simple_command ();
  270.  
  271.   if (element.word)
  272.     {
  273.       WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  274.       tw->word = element.word;
  275.       tw->next = command->value.Simple->words;
  276.       command->value.Simple->words = tw;
  277.     }
  278.   else
  279.     {
  280.       REDIRECT *r = element.redirect;
  281.       /* Due to the way <> is implemented, there may be more than a single
  282.      redirection in element.redirect.  We just follow the chain as far
  283.      as it goes, and hook onto the end. */
  284.       while (r->next)
  285.     r = r->next;
  286.       r->next = command->value.Simple->redirects;
  287.       command->value.Simple->redirects = element.redirect;
  288.     }
  289.   return (command);
  290. }
  291.  
  292. #define POSIX_HERE_DOCUMENTS
  293. make_here_document (temp)
  294.      REDIRECT *temp;
  295. {
  296.   int kill_leading = 0;
  297.  
  298.   switch (temp->instruction)
  299.     {
  300.       /* Because we are Bourne compatible, we read the input for this
  301.      << or <<- redirection now, from wherever input is coming from.
  302.      We store the input read into a WORD_DESC.  Replace the text of
  303.      the redirectee.word with the new input text.  If <<- is on,
  304.      then remove leading whitespace from each line. */
  305.  
  306.       case r_deblank_reading_until:    /* <<-foo */
  307.     kill_leading++;
  308.     /* ... */
  309.       case r_reading_until:        /* <<foo */
  310.     {
  311.       extern char *redirection_expand ();
  312.       extern char *string_quote_removal ();
  313.       char *redirectee_word;
  314.       int len;
  315.  
  316.       char *document = (char *)NULL;
  317.       int document_index = 0, document_size = 0;
  318.  
  319. #if !defined (POSIX_HERE_DOCUMENTS)
  320.       /* Because of Bourne shell semantics, we turn off globbing, but
  321.          only for this style of redirection.  I feel a little ill.  */
  322.       {
  323.         extern int disallow_filename_globbing;
  324.         int old_value = disallow_filename_globbing;
  325.         disallow_filename_globbing = 1;
  326.  
  327.         redirectee_word = redirection_expand (temp->redirectee.filename);
  328.  
  329.         disallow_filename_globbing = old_value;
  330.       }
  331. #else /* POSIX_HERE_DOCUMENTS */
  332.       /* Quote removal is the only expansion performed on the delimiter
  333.          for here documents, making it an extremely special case.  I
  334.          still feel ill. */
  335.       redirectee_word =
  336.         string_quote_removal (temp->redirectee.filename->word, 0);
  337. #endif /* POSIX_HERE_DOCUMENTS */
  338.  
  339.       /* redirection_expand will return NULL if the expansion results in
  340.          multiple words or no words.  Check for that here, and just abort
  341.          this here document if it does. */
  342.       if (redirectee_word)
  343.         len = strlen (redirectee_word);
  344.       else
  345.         {
  346.           temp->here_doc_eof = savestring ("");
  347.           goto document_done;
  348.         }
  349.  
  350.       free (temp->redirectee.filename->word);
  351.       temp->here_doc_eof = redirectee_word;
  352.  
  353.       /* Read lines from wherever lines are coming from.
  354.          For each line read, if kill_leading, then kill the
  355.          leading whitespace.
  356.          If the line matches redirectee_word exactly, then we have
  357.          manufactured the document.  Otherwise, add the line to the
  358.          list of lines in the document. */
  359.       {
  360.         extern char *read_secondary_line ();
  361.         char *line;
  362.         int l;
  363.  
  364.         while (line = read_secondary_line ())
  365.           {
  366.         if (!line)
  367.           goto document_done;
  368.  
  369.         if (kill_leading)
  370.           {
  371.             register int i;
  372.  
  373.             /* Hack:  To be compatible with some Bourne shells, we 
  374.                check the word before stripping the whitespace.  This
  375.                is a hack, though. */
  376.             if ((strncmp (line, redirectee_word, len) == 0) &&
  377.             line[len] == '\n')
  378.               goto document_done;
  379.  
  380.             for (i = 0; whitespace (line[i]); i++)
  381.               ;
  382.  
  383.             if (i)
  384.               strcpy (&line[0], &line[i]);
  385.           }
  386.  
  387.         if ((strncmp (line, redirectee_word, len) == 0) &&
  388.             line[len] == '\n')
  389.           goto document_done;
  390.  
  391.         l = strlen (line);
  392.         if (l + document_index >= document_size)
  393.           {
  394.             document = (char *)
  395.               xrealloc (document, (document_size += (10 * l)));
  396.           }
  397.  
  398.         if (l != 0)
  399.           {
  400.             strcpy (&document[document_index], line);
  401.             free (line);
  402.             document_index += l;
  403.           }
  404.           }
  405.   document_done:
  406.         if (!document)
  407.           document = savestring ("");
  408.         temp->redirectee.filename->word = document;
  409.       }
  410.     }
  411.     }
  412. }
  413.    
  414. /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION. 
  415.    INSTRUCTION is the instruction type, SOURCE is an INT,
  416.    and DEST is an INT or a WORD_DESC *. */
  417. REDIRECT *
  418. make_redirection (source, instruction, dest)
  419.      enum r_instruction instruction;
  420. {
  421.   REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
  422.  
  423.   /* First do the common cases. */
  424.   temp->redirector = source;
  425.   temp->redirectee.dest = dest;
  426.   temp->instruction = instruction;
  427.   temp->next = (REDIRECT *)NULL;
  428.  
  429.   switch (instruction)
  430.     {
  431.  
  432.     case r_output_direction:    /* >foo */
  433.     case r_output_force:    /* >| foo */
  434.       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  435.       break;
  436.  
  437.     case r_input_direction:    /* <foo */
  438.     case r_inputa_direction:    /* foo & makes this. */
  439.       temp->flags = O_RDONLY;
  440.       break;
  441.  
  442.     case r_appending_to:    /* >>foo */
  443.       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
  444.       break;
  445.  
  446.     case r_deblank_reading_until: /* <<-foo */
  447.     case r_reading_until:    /* << foo */
  448.       break;
  449.  
  450.     case r_duplicating:        /* 1<&2 */
  451.     case r_close_this:        /* <&- */
  452.       break;
  453.     
  454.     case r_err_and_out:        /* command &>filename */
  455.       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  456.       break;
  457.  
  458.     case r_input_output:
  459.       temp->flags = O_RDWR;
  460.       break;
  461.  
  462.     default:
  463.       programming_error ("Redirection instruction from yyparse () '%d' is\n\
  464. out of range in make_redirection ().", instruction);
  465.       abort ();
  466.       break;
  467.     }
  468.   return (temp);
  469. }
  470.  
  471. COMMAND *
  472. make_function_def (name, command)
  473.      WORD_DESC *name;
  474.      COMMAND *command;
  475. {
  476.   FUNCTION_DEF *temp;
  477.  
  478.   temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
  479.   temp->command = command;
  480.   temp->name = name;
  481.   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
  482. }
  483.  
  484. /* Reverse the word list and redirection list in the simple command
  485.    has just been parsed.  It seems simpler to do this here the one
  486.    time then by any other method that I can think of. */
  487. COMMAND *
  488. clean_simple_command (command)
  489.      COMMAND *command;
  490. {
  491.   extern GENERIC_LIST *reverse_list ();
  492.  
  493.   if (command->type != cm_simple)
  494.     {
  495.       programming_error
  496.     ("clean_simple_command () got a command with type %d.", command->type);
  497.     }
  498.   else
  499.     {
  500.       command->value.Simple->words =
  501.     (WORD_LIST *)reverse_list (command->value.Simple->words);
  502.       command->value.Simple->redirects = 
  503.     (REDIRECT *)reverse_list (command->value.Simple->redirects);
  504.     }
  505.  
  506.   return (command);
  507. }
  508.  
  509. /* Cons up a new array of words.  The words are taken from LIST,
  510.    which is a WORD_LIST *.  Absolutely everything is malloc'ed,
  511.    so you should free everything in this array when you are done.
  512.    The array is NULL terminated. */
  513. char **
  514. make_word_array (list)
  515.      WORD_LIST *list;
  516. {
  517.   int count = list_length (list);
  518.   char **array = (char **)xmalloc ((1 + count) * sizeof (char *));
  519.  
  520.   for (count = 0; list; count++)
  521.     {
  522.       array[count] = (char *)xmalloc (1 + strlen (list->word->word));
  523.       strcpy (array[count], list->word->word);
  524.       list = list->next;
  525.     }
  526.   array[count] = (char *)NULL;
  527.   return (array);
  528. }
  529.  
  530. /* Report an error having to do with FILENAME. */
  531. file_error (filename)
  532.      char *filename;
  533. {
  534.   report_error ("%s: %s", filename, strerror (errno));
  535. }
  536.  
  537. #if !defined (HAVE_VPRINTF)
  538. programming_error (reason, arg1, arg2, arg3, arg4, arg5)
  539.      char *reason;
  540. {
  541.   extern char *the_current_maintainer;
  542.  
  543. #if defined (JOB_CONTROL)
  544.   {
  545.     extern pid_t shell_pgrp;
  546.     give_terminal_to (shell_pgrp);
  547.   }
  548. #endif /* JOB_CONTROL */
  549.  
  550.   report_error (reason, arg1, arg2);
  551.   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  552.  
  553. #if defined (MAKE_BUG_REPORTS)
  554.   if (1)
  555.     {
  556.       fprintf (stderr, "Mailing a bug report...");
  557.       fflush (stderr);
  558.       make_bug_report (reason, arg1, arg2, arg3, arg4, arg5);
  559.       fprintf (stderr, "done.\n");
  560.     }
  561. #endif /* MAKE_BUG_REPORTS */
  562.  
  563.   fprintf (stderr, "Stopping myself...");
  564.   fflush (stderr);
  565.   abort ();
  566. }
  567.  
  568. report_error (format, arg1, arg2, arg3, arg4, arg5)
  569.      char *format;
  570. {
  571. #if defined (NOTDEF)
  572.   extern char *shell_name, *base_pathname ();
  573.  
  574.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  575. #endif /* NOTDEF */
  576.  
  577.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  578.   fprintf (stderr, "\n");
  579.   if (exit_immediately_on_error)
  580.     exit (EXECUTION_FAILURE);
  581. }  
  582.  
  583. fatal_error (format, arg1, arg2, arg3, arg4, arg5)
  584.      char *format;
  585. {
  586.   report_error (format, arg1, arg2, arg3, arg4, arg5);
  587.   exit (2);
  588. }
  589.  
  590. #else /* We have VARARGS support, so use it. */
  591.  
  592. programming_error (va_alist)
  593.      va_dcl
  594. {
  595.   extern char *the_current_maintainer, *shell_name;
  596.   extern char *base_pathname ();
  597.   va_list args;
  598.   char *format;
  599.  
  600. #if defined (JOB_CONTROL)
  601.   {
  602.     extern pid_t shell_pgrp;
  603.     give_terminal_to (shell_pgrp);
  604.   }
  605. #endif /* JOB_CONTROL */
  606.  
  607.   va_start (args);
  608.   format = va_arg (args, char *);
  609.   vfprintf (stderr, format, args);
  610.   fprintf (stderr, "\n");
  611.   va_end (args);
  612.  
  613.   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  614.  
  615. #if defined (MAKE_BUG_REPORTS)
  616.   if (1)
  617.     {
  618.       fprintf (stderr, "Mailing a bug report...");
  619.       fflush (stderr);
  620.       make_bug_report (va_alist);
  621.       fprintf (stderr, "done.\n");
  622.     }
  623. #endif
  624.   fprintf (stderr, "Stopping myself...");
  625.   fflush (stderr);
  626.   abort ();
  627. }
  628.  
  629. report_error (va_alist)
  630.      va_dcl
  631. {
  632.   va_list args;
  633.   char *format;
  634.  
  635. #if defined (NOTDEF)
  636.   extern char *shell_name, *base_pathname ();
  637.  
  638.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  639. #endif /* NOTDEF */
  640.   va_start (args);
  641.   format = va_arg (args, char *);
  642.   vfprintf (stderr, format, args);
  643.   fprintf (stderr, "\n");
  644.  
  645.   va_end (args);
  646.   if (exit_immediately_on_error)
  647.     exit (EXECUTION_FAILURE);
  648. }
  649.  
  650. fatal_error (va_alist)
  651.      va_dcl
  652. {
  653.   va_list args;
  654.   char *format;
  655.   extern char *shell_name, *base_pathname ();
  656.  
  657.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  658.   va_start (args);
  659.   format = va_arg (args, char *);
  660.   vfprintf (stderr, format, args);
  661.   fprintf (stderr, "\n");
  662.  
  663.   va_end (args);
  664.   exit (2);
  665. }
  666. #endif /* HAVE_VPRINTF */
  667.