]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Unify shell-finding logic
authorTom Tromey <tom@tromey.com>
Fri, 14 Sep 2018 16:35:45 +0000 (10:35 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 27 Oct 2018 17:58:40 +0000 (11:58 -0600)
I noticed several places in gdb that were using getenv("SHELL") and
then falling back to "/bin/sh" if it returned NULL.  This unifies
these into a single function.

gdb/ChangeLog
2018-10-27  Tom Tromey  <tom@tromey.com>

* procfs.c (procfs_target::create_inferior): Use get_shell.
* cli/cli-cmds.c (shell_escape): Use get_shell.
* windows-nat.c (windows_nat_target::create_inferior): Use
get_shell.
* common/pathstuff.c (get_shell): New function.
* nat/fork-inferior.c (SHELL_FILE, get_startup_shell): Remove.
(fork_inferior): Use get_shell.
* common/pathstuff.h (get_shell): Declare.

gdb/ChangeLog
gdb/cli/cli-cmds.c
gdb/common/pathstuff.c
gdb/common/pathstuff.h
gdb/nat/fork-inferior.c
gdb/procfs.c
gdb/windows-nat.c

index e9491358faa1fc08bb179e062926f95e10e5f275..5c5c8b12a6bb99c9cc8f4e7de27254549bff5cb8 100644 (file)
@@ -1,3 +1,14 @@
+2018-10-27  Tom Tromey  <tom@tromey.com>
+
+       * procfs.c (procfs_target::create_inferior): Use get_shell.
+       * cli/cli-cmds.c (shell_escape): Use get_shell.
+       * windows-nat.c (windows_nat_target::create_inferior): Use
+       get_shell.
+       * common/pathstuff.c (get_shell): New function.
+       * nat/fork-inferior.c (SHELL_FILE, get_startup_shell): Remove.
+       (fork_inferior): Use get_shell.
+       * common/pathstuff.h (get_shell): Declare.
+
 2018-10-27  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
 
        * NEWS: Mention changes to 'info [args|functions|locals|variables]'
index b871e476d39bc13ae82233324bb7cc63c6b3a6e9..135f550b80136afc840e03e804c223cc5c352069 100644 (file)
@@ -50,6 +50,7 @@
 #include "cli/cli-utils.h"
 
 #include "extension.h"
+#include "common/pathstuff.h"
 
 #ifdef TUI
 #include "tui/tui.h"   /* For tui_active et.al.  */
@@ -726,13 +727,10 @@ shell_escape (const char *arg, int from_tty)
 
   if ((pid = vfork ()) == 0)
     {
-      const char *p, *user_shell;
+      const char *p, *user_shell = get_shell ();
 
       close_most_fds ();
 
-      if ((user_shell = getenv ("SHELL")) == NULL)
-       user_shell = "/bin/sh";
-
       /* Get the name of the shell for arg0.  */
       p = lbasename (user_shell);
 
index 82905c9e68742c3abe7133f69f43e4023660693b..6d8e53f4e1b124c8027ab4280b26e0b4144aad9d 100644 (file)
@@ -190,3 +190,15 @@ get_standard_cache_dir ()
 
   return {};
 }
+
+/* See common/pathstuff.h.  */
+
+const char *
+get_shell ()
+{
+  const char *ret = getenv ("SHELL");
+  if (ret == NULL)
+    ret = "/bin/sh";
+
+  return ret;
+}
index a43b9636514d26231367561e97a5c6d075135a7c..0a8caeacba773cf1874e8357f73869f43063c8a5 100644 (file)
@@ -64,4 +64,9 @@ extern bool contains_dir_separator (const char *path);
 
 extern std::string get_standard_cache_dir ();
 
+/* Return the file name of the user's shell.  Normally this comes from
+   the SHELL environment variable.  */
+
+extern const char *get_shell ();
+
 #endif /* PATHSTUFF_H */
index 40cd05a0f8fb7f20c486895d83d8904a699adfdf..f1032b43c9edb1e51bdf994c2e9b0cae278718d9 100644 (file)
 #include "target/target.h"
 #include "common-inferior.h"
 #include "common-gdbthread.h"
+#include "common/pathstuff.h"
 #include "signals-state-save-restore.h"
 #include "gdb_tilde_expand.h"
 #include <vector>
 
 extern char **environ;
 
-/* Default shell file to be used if 'startup-with-shell' is set but
-   $SHELL is not.  */
-#define SHELL_FILE "/bin/sh"
-
 /* Build the argument vector for execv(3).  */
 
 class execv_argv
@@ -265,20 +262,6 @@ execv_argv::init_for_shell (const char *exec_file,
   m_argv.push_back (NULL);
 }
 
-/* Return the shell that must be used to startup the inferior.  The
-   first attempt is the environment variable SHELL; if it is not set,
-   then we default to SHELL_FILE.  */
-
-static const char *
-get_startup_shell ()
-{
-  const char *ret = getenv ("SHELL");
-  if (ret == NULL)
-    ret = SHELL_FILE;
-
-  return ret;
-}
-
 /* See nat/fork-inferior.h.  */
 
 pid_t
@@ -316,7 +299,7 @@ fork_inferior (const char *exec_file_arg, const std::string &allargs,
 
       /* Figure out what shell to start up the user program under.  */
       if (shell_file == NULL)
-       shell_file = get_startup_shell ();
+       shell_file = get_shell ();
 
       gdb_assert (shell_file != NULL);
     }
index 6ffe569e690529aa98d2ce26335b090d4642a7ed..ca381a71ae575a1ed7339f60f156ae8662607547 100644 (file)
@@ -3035,11 +3035,11 @@ procfs_target::create_inferior (const char *exec_file,
                                const std::string &allargs,
                                char **env, int from_tty)
 {
-  char *shell_file = getenv ("SHELL");
+  const char *shell_file = get_shell ();
   char *tryname;
   int pid;
 
-  if (shell_file != NULL && strchr (shell_file, '/') == NULL)
+  if (strchr (shell_file, '/') == NULL)
     {
 
       /* We will be looking down the PATH to find shell_file.  If we
index 0047a264189b097feb3ba2897cc335f0a1e4670c..8292cf4212253fdc502d9e9a15ec21b8f9edcf70 100644 (file)
@@ -68,6 +68,7 @@
 #include "complaints.h"
 #include "inf-child.h"
 #include "gdb_tilde_expand.h"
+#include "common/pathstuff.h"
 
 #define AdjustTokenPrivileges          dyn_AdjustTokenPrivileges
 #define DebugActiveProcessStop         dyn_DebugActiveProcessStop
@@ -2578,9 +2579,7 @@ windows_nat_target::create_inferior (const char *exec_file,
     }
   else
     {
-      sh = getenv ("SHELL");
-      if (!sh)
-       sh = "/bin/sh";
+      sh = get_shell ();
       if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0)
        error (_("Error starting executable via shell: %d"), errno);
 #ifdef __USEWIDE