]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - shell.c
bash-4.4-rc2 release
[thirdparty/bash.git] / shell.c
diff --git a/shell.c b/shell.c
index 9d941c7f786f213a142a60610894e51c25a4bd78..45b77f9ea6be674f4711f0d774f8b5c093c60c91 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -38,7 +38,9 @@
 #include <signal.h>
 #include <errno.h>
 #include "filecntl.h"
-#include <pwd.h>
+#if defined (HAVE_PWD_H)
+#  include <pwd.h>
+#endif
 
 #if defined (HAVE_UNISTD_H)
 #  include <unistd.h>
@@ -57,6 +59,9 @@
 
 #if defined (JOB_CONTROL)
 #include "jobs.h"
+#else
+extern int initialize_job_control __P((int));
+extern int get_tty_state __P((void));
 #endif /* JOB_CONTROL */
 
 #include "input.h"
@@ -160,6 +165,7 @@ int autocd = 0;
    This is a superset of the information provided by interactive_shell.
 */
 int startup_state = 0;
+int reading_shell_script = 0;
 
 /* Special debugging helper. */
 int debugging_login_shell = 0;
@@ -261,7 +267,7 @@ static const struct {
 #if defined (RESTRICTED_SHELL)
   { "restricted", Int, &restricted, (char **)0x0 },
 #endif
-  { "verbose", Int, &echo_input_at_read, (char **)0x0 },
+  { "verbose", Int, &verbose_flag, (char **)0x0 },
   { "version", Int, &do_version, (char **)0x0 },
 #if defined (WORDEXP_OPTION)
   { "wordexp", Int, &wordexp_only, (char **)0x0 },
@@ -390,9 +396,7 @@ main (argc, argv, env)
   xtrace_init ();
 
 #if defined (USING_BASH_MALLOC) && defined (DEBUG) && !defined (DISABLE_MALLOC_WRAPPERS)
-#  if 1
-  malloc_set_register (1);
-#  endif
+  malloc_set_register (1);     /* XXX - change to 1 for malloc debugging */
 #endif
 
   check_dev_tty ();
@@ -461,7 +465,7 @@ main (argc, argv, env)
 
   /* Find full word arguments first. */
   arg_index = parse_long_options (argv, arg_index, argc);
-
+  
   if (want_initial_help)
     {
       show_shell_usage (stdout, 1);
@@ -474,6 +478,8 @@ main (argc, argv, env)
       exit (EXECUTION_SUCCESS);
     }
 
+  echo_input_at_read = verbose_flag;   /* --verbose given */
+
   /* All done with full word options; do standard shell option parsing.*/
   this_command_name = shell_name;      /* for error reporting */
   arg_index = parse_shell_options (argv, arg_index, argc);
@@ -512,8 +518,6 @@ main (argc, argv, env)
     }
   this_command_name = (char *)NULL;
 
-  cmd_init();          /* initialize the command object caches */
-
   /* First, let the outside world know about our interactive status.
      A shell is interactive if the `-i' flag was given, or if all of
      the following conditions are met:
@@ -569,25 +573,45 @@ main (argc, argv, env)
   set_default_locale_vars ();
 
   /*
-   * M-x term -> TERM=eterm EMACS=22.1 (term:0.96)     (eterm)
-   * M-x shell -> TERM=dumb EMACS=t                    (no line editing)
-   * M-x terminal -> TERM=emacs-em7955 EMACS=          (line editing)
+   * M-x term -> TERM=eterm-color INSIDE_EMACS='251,term:0.96' (eterm)
+   * M-x shell -> TERM='dumb' INSIDE_EMACS='25.1,comint' (no line editing)
+   *
+   * Older versions of Emacs may set EMACS to 't' or to something like
+   * '22.1 (term:0.96)' instead of (or in addition to) setting INSIDE_EMACS.
+   * They may set TERM to 'eterm' instead of 'eterm-color'.  They may have
+   * a now-obsolete command that sets neither EMACS nor INSIDE_EMACS:
+   * M-x terminal -> TERM='emacs-em7955' (line editing)
    */
   if (interactive_shell)
     {
-      char *term, *emacs;
+      char *term, *emacs, *inside_emacs;;
+      int emacs_term, in_emacs;
 
       term = get_string_value ("TERM");
       emacs = get_string_value ("EMACS");
+      inside_emacs = get_string_value ("INSIDE_EMACS");
+
+      if (inside_emacs)
+       {
+         emacs_term = strstr (inside_emacs, ",term:") != 0;
+         in_emacs = 1;
+       }
+      else if (emacs)
+       {
+         /* Infer whether we are in an older Emacs. */
+         emacs_term = strstr (emacs, " (term:") != 0;
+         in_emacs = emacs_term || STREQ (emacs, "t");
+       }
+      else
+       in_emacs = emacs_term = 0;
 
       /* Not sure any emacs terminal emulator sets TERM=emacs any more */
       no_line_editing |= STREQ (term, "emacs");
-      no_line_editing |= emacs && emacs[0] == 't' && emacs[1] == '\0' && STREQ (term, "dumb");
-      no_line_editing |= get_string_value ("INSIDE_EMACS") != 0;
+      no_line_editing |= in_emacs && STREQ (term, "dumb");
 
       /* running_under_emacs == 2 for `eterm' */
-      running_under_emacs = (emacs != 0) || STREQN (term, "emacs", 5);
-      running_under_emacs += STREQN (term, "eterm", 5) && emacs && strstr (emacs, "term");
+      running_under_emacs = in_emacs || STREQN (term, "emacs", 5);
+      running_under_emacs += emacs_term && STREQN (term, "eterm", 5);
 
       if (running_under_emacs)
        gnu_error_format = 1;
@@ -684,6 +708,9 @@ main (argc, argv, env)
     }
 #endif
 
+  cmd_init ();         /* initialize the command object caches */
+  uwp_init ();
+
   if (command_execution_string)
     {
       arg_index = bind_args (argv, arg_index, argc, 0);
@@ -710,20 +737,27 @@ main (argc, argv, env)
       arg_index++;
     }
   else if (interactive == 0)
-    /* In this mode, bash is reading a script from stdin, which is a
-       pipe or redirected file. */
+    {
+      /* In this mode, bash is reading a script from stdin, which is a
+        pipe or redirected file. */
 #if defined (BUFFERED_INPUT)
-    default_buffered_input = fileno (stdin);   /* == 0 */
+      default_buffered_input = fileno (stdin); /* == 0 */
 #else
-    setbuf (default_input, (char *)NULL);
+      setbuf (default_input, (char *)NULL);
 #endif /* !BUFFERED_INPUT */
+      read_from_stdin = 1;
+    }
+  else if (arg_index == argc)
+    /* "If there are no operands and the -c option is not specified, the -s
+       option shall be assumed." */
+    read_from_stdin = 1;
 
   set_bash_input ();
 
   /* Bind remaining args to $1 ... $n */
   arg_index = bind_args (argv, arg_index, argc, 1);
 
-  if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0 && (dollar_vars[1] || interactive_shell == 0))
+  if (debugging_mode && locally_skip_execution == 0 && running_setuid == 0 && (reading_shell_script || interactive_shell == 0))
     start_debugger ();
 
   /* Do the things that should be done only for interactive shells. */
@@ -967,6 +1001,7 @@ sh_exit (s)
 #if defined (MALLOC_DEBUG) && defined (USING_BASH_MALLOC)
   if (malloc_trace_at_exit)
     trace_malloc_stats (get_name_for_error (), (char *)NULL);
+  /* mlocation_write_table (); */
 #endif
 
   exit (s);
@@ -1420,11 +1455,13 @@ start_debugger ()
   r = force_execute_file (DEBUGGER_START_FILE, 1);
   if (r < 0)
     {
-      internal_warning ("cannot start debugger; debugging mode disabled");
-      debugging_mode = function_trace_mode = 0;
+      internal_warning (_("cannot start debugger; debugging mode disabled"));
+      debugging_mode = 0;
     }
-  else
-    function_trace_mode = 1;
+  error_trace_mode = function_trace_mode = debugging_mode;
+
+  set_shellopts ();
+  set_bashopts ();
 
   exit_immediately_on_error += old_errexit;
 #endif
@@ -1467,7 +1504,15 @@ open_shell_script (script_name)
     {
       e = errno;
       file_error (filename);
-      exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
+      sh_exit ((e == ENOENT) ? EX_NOTFOUND : EX_NOINPUT);
+    }
+
+  free (dollar_vars[0]);
+  dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name);
+  if (exec_argv0)
+    {
+      free (exec_argv0);
+      exec_argv0 = (char *)NULL;
     }
 
   if (file_isdir (filename))
@@ -1478,15 +1523,7 @@ open_shell_script (script_name)
       errno = EINVAL;
 #endif
       file_error (filename);
-      exit (EX_NOINPUT);
-    }
-
-  free (dollar_vars[0]);
-  dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name);
-  if (exec_argv0)
-    {
-      free (exec_argv0);
-      exec_argv0 = (char *)NULL;
+      sh_exit (EX_NOINPUT);
     }
 
 #if defined (ARRAY_VARS)
@@ -1521,7 +1558,14 @@ open_shell_script (script_name)
        {
          e = errno;
          if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
-           internal_error (_("%s: is a directory"), filename);
+           {
+#if defined (EISDIR)
+             errno = EISDIR;
+             file_error (filename);
+#else        
+             internal_error (_("%s: Is a directory"), filename);
+#endif
+           }
          else
            {
              errno = e;
@@ -1581,6 +1625,8 @@ open_shell_script (script_name)
     init_interactive_script ();
 
   free (filename);
+
+  reading_shell_script = 1;
   return (fd);
 }
 
@@ -1674,6 +1720,9 @@ init_interactive ()
 {
   expand_aliases = interactive_shell = startup_state = 1;
   interactive = 1;
+#if defined (HISTORY)
+  remember_on_history = enable_history_list = 1;       /* XXX */
+#endif
 }
 
 static void
@@ -1697,6 +1746,9 @@ init_interactive_script ()
 {
   init_noninteractive ();
   expand_aliases = interactive_shell = startup_state = 1;
+#if defined (HISTORY)
+  remember_on_history = enable_history_list = 1;       /* XXX */
+#endif
 }
 
 void
@@ -1727,7 +1779,9 @@ get_current_user_info ()
          current_user.shell = savestring ("/bin/sh");
          current_user.home_dir = savestring ("/");
        }
+#if defined (HAVE_GETPWENT)
       endpwent ();
+#endif
     }
 }
 
@@ -1839,7 +1893,7 @@ shell_reinitialize ()
   /* XXX - should we set jobs_m_flag to 0 here? */
 
 #if defined (HISTORY)
-  bash_history_reinit (0);
+  bash_history_reinit (enable_history_list = 0);
 #endif /* HISTORY */
 
 #if defined (RESTRICTED_SHELL)
@@ -1905,6 +1959,9 @@ show_shell_usage (fp, extra)
       fprintf (fp, _("Type `%s -c \"help set\"' for more information about shell options.\n"), shell_name);
       fprintf (fp, _("Type `%s -c help' for more information about shell builtin commands.\n"), shell_name);
       fprintf (fp, _("Use the `bashbug' command to report bugs.\n"));
+      fprintf (fp, "\n");
+      fprintf (fp, _("bash home page: <http://www.gnu.org/software/bash>\n"));
+      fprintf (fp, _("General help using GNU software: <http://www.gnu.org/gethelp/>\n"));
     }
 }