]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - make_cmd.c
fix for SIGINT in sourced script
[thirdparty/bash.git] / make_cmd.c
index c913c0b1d04280bf653acdd631be796c62afe769..b42e9ff148b22b43c7e087cd79ce2972488d44d6 100644 (file)
@@ -1,29 +1,29 @@
 /* make_cmd.c -- Functions for making instances of the various
    parser constructs. */
 
-/* Copyright (C) 1989 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2009 Free Software Foundation, Inc.
 
-This file is part of GNU Bash, the Bourne Again SHell.
+   This file is part of GNU Bash, the Bourne Again SHell.
 
-Bash is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
-version.
+   Bash is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+   Bash is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "config.h"
 
 #include <stdio.h>
 #include "bashtypes.h"
-#ifndef _MINIX
+#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
 #  include <sys/file.h>
 #endif
 #include "filecntl.h"
@@ -32,37 +32,73 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #  include <unistd.h>
 #endif
 
+#include "bashintl.h"
+
+#include "parser.h"
 #include "syntax.h"
 #include "command.h"
 #include "general.h"
 #include "error.h"
 #include "flags.h"
 #include "make_cmd.h"
+#include "dispose_cmd.h"
 #include "variables.h"
 #include "subst.h"
 #include "input.h"
+#include "ocache.h"
 #include "externs.h"
 
 #if defined (JOB_CONTROL)
 #include "jobs.h"
 #endif
 
-extern int line_number, current_command_line_count;
+#include "shmbutil.h"
+
+extern int line_number, current_command_line_count, parser_state;
 extern int last_command_exit_value;
+extern int shell_initialized;
+
+int here_doc_first_line = 0;
+
+/* Object caching */
+sh_obj_cache_t wdcache = {0, 0, 0};
+sh_obj_cache_t wlcache = {0, 0, 0};
+
+#define WDCACHESIZE    128
+#define WLCACHESIZE    128
 
-static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *));
+static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *, int));
 #if defined (ARITH_FOR_COMMAND)
 static WORD_LIST *make_arith_for_expr __P((char *));
 #endif
 static COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *));
 
+void
+cmd_init ()
+{
+  ocache_create (wdcache, WORD_DESC, WDCACHESIZE);
+  ocache_create (wlcache, WORD_LIST, WLCACHESIZE);
+}
+
+WORD_DESC *
+alloc_word_desc ()
+{
+  WORD_DESC *temp;
+
+  ocache_alloc (wdcache, WORD_DESC, temp);
+  temp->flags = 0;
+  temp->word = 0;
+  return temp;
+}
+
 WORD_DESC *
 make_bare_word (string)
      const char *string;
 {
   WORD_DESC *temp;
 
-  temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
+  temp = alloc_word_desc ();
+
   if (*string)
     temp->word = savestring (string);
   else
@@ -71,7 +107,6 @@ make_bare_word (string)
       temp->word[0] = '\0';
     }
 
-  temp->flags = 0;
   return (temp);
 }
 
@@ -80,11 +115,16 @@ make_word_flags (w, string)
      WORD_DESC *w;
      const char *string;
 {
-  register const char *s;
+  register int i;
+  size_t slen;
+  DECLARE_MBSTATE;
 
-  for (s = string; *s; s++)
-    switch (*s)
-      {
+  i = 0;
+  slen = strlen (string);
+  while (i < slen)
+    {
+      switch (string[i])
+       {
        case '$':
          w->flags |= W_HASDOLLAR;
          break;
@@ -95,7 +135,11 @@ make_word_flags (w, string)
        case '"':
          w->flags |= W_QUOTED;
          break;
-      }
+       }
+
+      ADVANCE_CHAR (string, slen, i);
+    }
+
   return (w);
 }
 
@@ -128,25 +172,13 @@ make_word_list (word, wlink)
 {
   WORD_LIST *temp;
 
-  temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
+  ocache_alloc (wlcache, WORD_LIST, temp);
+
   temp->word = word;
   temp->next = wlink;
   return (temp);
 }
 
-WORD_LIST *
-add_string_to_list (string, list)
-     char *string;
-     WORD_LIST *list;
-{
-  WORD_LIST *temp;
-
-  temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
-  temp->word = make_word (string);
-  temp->next = list;
-  return (temp);
-}
-
 COMMAND *
 make_command (type, pointer)
      enum command_type type;
@@ -177,39 +209,43 @@ command_connect (com1, com2, connector)
 }
 
 static COMMAND *
-make_for_or_select (type, name, map_list, action)
+make_for_or_select (type, name, map_list, action, lineno)
      enum command_type type;
      WORD_DESC *name;
      WORD_LIST *map_list;
      COMMAND *action;
+     int lineno;
 {
   FOR_COM *temp;
 
   temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
   temp->flags = 0;
   temp->name = name;
+  temp->line = lineno;
   temp->map_list = map_list;
   temp->action = action;
   return (make_command (type, (SIMPLE_COM *)temp));
 }
 
 COMMAND *
-make_for_command (name, map_list, action)
+make_for_command (name, map_list, action, lineno)
      WORD_DESC *name;
      WORD_LIST *map_list;
      COMMAND *action;
+     int lineno;
 {
-  return (make_for_or_select (cm_for, name, map_list, action));
+  return (make_for_or_select (cm_for, name, map_list, action, lineno));
 }
 
 COMMAND *
-make_select_command (name, map_list, action)
+make_select_command (name, map_list, action, lineno)
      WORD_DESC *name;
      WORD_LIST *map_list;
      COMMAND *action;
+     int lineno;
 {
 #if defined (SELECT_COMMAND)
-  return (make_for_or_select (cm_select, name, map_list, action));
+  return (make_for_or_select (cm_select, name, map_list, action, lineno));
 #else
   last_command_exit_value = 2;
   return ((COMMAND *)NULL);
@@ -222,16 +258,24 @@ make_arith_for_expr (s)
      char *s;
 {
   WORD_LIST *result;
-  WORD_DESC *w;
+  WORD_DESC *wd;
 
   if (s == 0 || *s == '\0')
     return ((WORD_LIST *)NULL);
-  w = make_word (s);
-  result = make_word_list (w, (WORD_LIST *)NULL);
+  wd = make_word (s);
+  wd->flags |= W_NOGLOB|W_NOSPLIT|W_QUOTED|W_DQUOTE;   /* no word splitting or globbing */
+#if defined (PROCESS_SUBSTITUTION)
+  wd->flags |= W_NOPROCSUB;    /* no process substitution */
+#endif
+  result = make_word_list (wd, (WORD_LIST *)NULL);
   return result;
 }
 #endif
 
+/* Note that this function calls dispose_words on EXPRS, since it doesn't
+   use the word list directly.  We free it here rather than at the caller
+   because no other function in this file requires that the caller free
+   any arguments. */
 COMMAND *
 make_arith_for_command (exprs, action, lineno)
      WORD_LIST *exprs;
@@ -242,7 +286,7 @@ make_arith_for_command (exprs, action, lineno)
   ARITH_FOR_COM *temp;
   WORD_LIST *init, *test, *step;
   char *s, *t, *start;
-  int nsemi;
+  int nsemi, i;
 
   init = test = step = (WORD_LIST *)NULL;
   /* Parse the string into the three component sub-expressions. */
@@ -254,10 +298,10 @@ make_arith_for_command (exprs, action, lineno)
        s++;
       start = s;
       /* skip to the semicolon or EOS */
-      while (*s && *s != ';')
-       s++;
+      i = skip_to_delim (start, 0, ";", SD_NOJMP|SD_NOPROCSUB);
+      s = start + i;
 
-      t = (s > start) ? substring (start, 0, s - start) : (char *)NULL;
+      t = (i > 0) ? substring (start, 0, i) : (char *)NULL;
 
       nsemi++;
       switch (nsemi)
@@ -282,10 +326,13 @@ make_arith_for_command (exprs, action, lineno)
   if (nsemi != 3)
     {
       if (nsemi < 3)
-       parser_error (lineno, "syntax error: arithmetic expression required");
+       parser_error (lineno, _("syntax error: arithmetic expression required"));
       else
-       parser_error (lineno, "syntax error: `;' unexpected");
-      parser_error (lineno, "syntax error: `((%s))'", exprs->word->word);
+       parser_error (lineno, _("syntax error: `;' unexpected"));
+      parser_error (lineno, _("syntax error: `((%s))'"), exprs->word->word);
+      free (init);
+      free (test);
+      free (step);
       last_command_exit_value = 2;
       return ((COMMAND *)NULL);
     }
@@ -298,8 +345,10 @@ make_arith_for_command (exprs, action, lineno)
   temp->step = step ? step : make_arith_for_expr ("1");
   temp->action = action;
 
+  dispose_words (exprs);
   return (make_command (cm_arith_for, (SIMPLE_COM *)temp));
 #else
+  dispose_words (exprs);
   last_command_exit_value = 2;
   return ((COMMAND *)NULL);
 #endif /* ARITH_FOR_COMMAND */
@@ -317,14 +366,16 @@ make_group_command (command)
 }
 
 COMMAND *
-make_case_command (word, clauses)
+make_case_command (word, clauses, lineno)
      WORD_DESC *word;
      PATTERN_LIST *clauses;
+     int lineno;
 {
   CASE_COM *temp;
 
   temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
   temp->flags = 0;
+  temp->line = lineno;
   temp->word = word;
   temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *);
   return (make_command (cm_case, (SIMPLE_COM *)temp));
@@ -341,6 +392,7 @@ make_pattern_list (patterns, action)
   temp->patterns = REVERSE_LIST (patterns, WORD_LIST *);
   temp->action = action;
   temp->next = NULL;
+  temp->flags = 0;
   return (temp);
 }
 
@@ -486,17 +538,18 @@ make_simple_command (element, command)
   /* If we are starting from scratch, then make the initial command
      structure.  Also note that we have to fill in all the slots, since
      malloc doesn't return zeroed space. */
-  if (!command)
-    command = make_bare_simple_command ();
+  if (command == 0)
+    {
+      command = make_bare_simple_command ();
+      parser_state |= PST_REDIRLIST;
+    }
 
   if (element.word)
     {
-      WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
-      tw->word = element.word;
-      tw->next = command->value.Simple->words;
-      command->value.Simple->words = tw;
+      command->value.Simple->words = make_word_list (element.word, command->value.Simple->words);
+      parser_state &= ~PST_REDIRLIST;
     }
-  else
+  else if (element.redirect)
     {
       REDIRECT *r = element.redirect;
       /* Due to the way <> is implemented, there may be more than a single
@@ -507,6 +560,7 @@ make_simple_command (element, command)
       r->next = command->value.Simple->redirects;
       command->value.Simple->redirects = element.redirect;
     }
+
   return (command);
 }
 
@@ -516,8 +570,9 @@ make_simple_command (element, command)
    the redirectee.word with the new input text.  If <<- is on,
    then remove leading TABS from each line. */
 void
-make_here_document (temp)
+make_here_document (temp, lineno)
      REDIRECT *temp;
+     int lineno;
 {
   int kill_leading, redir_len;
   char *redir_word, *document, *full_line;
@@ -526,7 +581,7 @@ make_here_document (temp)
   if (temp->instruction != r_deblank_reading_until &&
       temp->instruction != r_reading_until)
     {
-      internal_error ("make_here_document: bad instruction type %d", temp->instruction);
+      internal_error (_("make_here_document: bad instruction type %d"), temp->instruction);
       return;
     }
 
@@ -570,9 +625,15 @@ make_here_document (temp)
       register char *line;
       int len;
 
+      here_doc_first_line = 0;
       line = full_line;
       line_number++;
 
+      /* If set -v is in effect, echo the line read.  read_secondary_line/
+        read_a_line leaves the newline at the end, so don't print another. */
+      if (echo_input_at_read)
+       fprintf (stderr, "%s", line);
+
       if (kill_leading && *line)
        {
          /* Hack:  To be compatible with some Bourne shells, we
@@ -604,6 +665,9 @@ make_here_document (temp)
       document_index += len;
     }
 
+  if (full_line == 0)
+    internal_warning (_("here-document at line %d delimited by end-of-file (wanted `%s')"), lineno, redir_word);
+
 document_done:
   if (document)
     document[document_index] = '\0';
@@ -613,24 +677,33 @@ document_done:
       document[0] = '\0';
     }
   temp->redirectee.filename->word = document;
+  here_doc_first_line = 0;
 }
 
 /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION.
    INSTRUCTION is the instruction type, SOURCE is a file descriptor,
    and DEST is a file descriptor or a WORD_DESC *. */
 REDIRECT *
-make_redirection (source, instruction, dest_and_filename)
-     int source;
+make_redirection (source, instruction, dest_and_filename, flags)
+     REDIRECTEE source;
      enum r_instruction instruction;
      REDIRECTEE dest_and_filename;
+     int flags;
 {
-  REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
+  REDIRECT *temp;
+  WORD_DESC *w;
+  int wlen;
+  intmax_t lfd;
+
+  temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
 
   /* First do the common cases. */
   temp->redirector = source;
   temp->redirectee = dest_and_filename;
+  temp->here_doc_eof = 0;
   temp->instruction = instruction;
   temp->flags = 0;
+  temp->rflags = flags;
   temp->next = (REDIRECT *)NULL;
 
   switch (instruction)
@@ -638,11 +711,12 @@ make_redirection (source, instruction, dest_and_filename)
 
     case r_output_direction:           /* >foo */
     case r_output_force:               /* >| foo */
-    case r_err_and_out:                        /* command &>filename */
+    case r_err_and_out:                        /* &>filename */
       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
       break;
 
     case r_appending_to:               /* >>foo */
+    case r_append_err_and_out:         /* &>> filename */
       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
       break;
 
@@ -657,15 +731,41 @@ make_redirection (source, instruction, dest_and_filename)
 
     case r_deblank_reading_until:      /* <<-foo */
     case r_reading_until:              /* << foo */
+    case r_reading_string:             /* <<< foo */
     case r_close_this:                 /* <&- */
     case r_duplicating_input:          /* 1<&2 */
     case r_duplicating_output:         /* 1>&2 */
+      break;
+
+    /* the parser doesn't pass these. */
+    case r_move_input:                 /* 1<&2- */
+    case r_move_output:                        /* 1>&2- */
+    case r_move_input_word:            /* 1<&$foo- */
+    case r_move_output_word:           /* 1>&$foo- */
+      break;
+
+    /* The way the lexer works we have to do this here. */
     case r_duplicating_input_word:     /* 1<&$foo */
     case r_duplicating_output_word:    /* 1>&$foo */
+      w = dest_and_filename.filename;
+      wlen = strlen (w->word) - 1;
+      if (w->word[wlen] == '-')                /* Yuck */
+        {
+          w->word[wlen] = '\0';
+         if (all_digits (w->word) && legal_number (w->word, &lfd) && lfd == (int)lfd)
+           {
+             dispose_word (w);
+             temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input : r_move_output;
+             temp->redirectee.dest = lfd;
+           }
+         else
+           temp->instruction = (instruction == r_duplicating_input_word) ? r_move_input_word : r_move_output_word;
+        }
+          
       break;
 
     default:
-      programming_error ("make_redirection: redirection instruction `%d' out of range", instruction);
+      programming_error (_("make_redirection: redirection instruction `%d' out of range"), instruction);
       abort ();
       break;
     }
@@ -679,6 +779,10 @@ make_function_def (name, command, lineno, lstart)
      int lineno, lstart;
 {
   FUNCTION_DEF *temp;
+#if defined (ARRAY_VARS)
+  SHELL_VAR *bash_source_v;
+  ARRAY *bash_source_a;
+#endif
 
   temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
   temp->command = command;
@@ -686,6 +790,26 @@ make_function_def (name, command, lineno, lstart)
   temp->line = lineno;
   temp->flags = 0;
   command->line = lstart;
+
+  /* Information used primarily for debugging. */
+  temp->source_file = 0;
+#if defined (ARRAY_VARS)
+  GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
+  if (bash_source_a && array_num_elements (bash_source_a) > 0)
+    temp->source_file = array_reference (bash_source_a, 0);
+#endif
+  /* Assume that shell functions without a source file before the shell is
+     initialized come from the environment.  Otherwise default to "main"
+     (usually functions being defined interactively) */
+  if (temp->source_file == 0)
+    temp->source_file = shell_initialized ? "main" : "environment";
+
+#if defined (DEBUGGER)
+  bind_function_def (name->word, temp);
+#endif
+
+  temp->source_file = temp->source_file ? savestring (temp->source_file) : 0;
+
   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
 }
 
@@ -701,6 +825,20 @@ make_subshell_command (command)
   return (make_command (cm_subshell, (SIMPLE_COM *)temp));
 }
 
+COMMAND *
+make_coproc_command (name, command)
+     char *name;
+     COMMAND *command;
+{
+  COPROC_COM *temp;
+
+  temp = (COPROC_COM *)xmalloc (sizeof (COPROC_COM));
+  temp->name = savestring (name);
+  temp->command = command;
+  temp->flags = CMD_WANT_SUBSHELL|CMD_COPROC_SUBSHELL;
+  return (make_command (cm_coproc, (SIMPLE_COM *)temp));
+}
+
 /* Reverse the word list and redirection list in the simple command
    has just been parsed.  It seems simpler to do this here the one
    time then by any other method that I can think of. */
@@ -718,6 +856,7 @@ clean_simple_command (command)
        REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
     }
 
+  parser_state &= ~PST_REDIRLIST;
   return (command);
 }