-#include <signal.h>
-#include <vector>
-
-/* This just gets used as a default if we can't find SHELL. */
-#define SHELL_FILE "/bin/sh"
-
-extern char **environ;
-
-static char *exec_wrapper;
-
-/* Build the argument vector for execv(3). */
-
-class execv_argv
-{
-public:
- /* EXEC_FILE is the file to run. ALLARGS is a string containing the
- arguments to the program. If starting with a shell, SHELL_FILE
- is the shell to run. Otherwise, SHELL_FILE is NULL. */
- execv_argv (const char *exec_file, const std::string &allargs,
- const char *shell_file);
-
- /* Return a pointer to the built argv, in the type expected by
- execv. The result is (only) valid for as long as this execv_argv
- object is live. We return a "char **" because that's the type
- that the execv functions expect. Note that it is guaranteed that
- the execv functions do not modify the argv[] array nor the
- strings to which the array point. */
- char **argv ()
- {
- return const_cast<char **> (&m_argv[0]);
- }
-
-private:
- /* Disable copying. */
- execv_argv (const execv_argv &) = delete;
- void operator= (const execv_argv &) = delete;
-
- /* Helper methods for constructing the argument vector. */
-
- /* Used when building an argv for a straight execv call, without
- going via the shell. */
- void init_for_no_shell (const char *exec_file,
- const std::string &allargs);
-
- /* Used when building an argv for execing a shell that execs the
- child program. */
- void init_for_shell (const char *exec_file,
- const std::string &allargs,
- const char *shell_file);
-
- /* The argument vector built. Holds non-owning pointers. Elements
- either point to the strings passed to the execv_argv ctor, or
- inside M_STORAGE. */
- std::vector<const char *> m_argv;
-
- /* Storage. In the no-shell case, this contains a copy of the
- arguments passed to the ctor, split by '\0'. In the shell case,
- this contains the quoted shell command. I.e., SHELL_COMMAND in
- {"$SHELL" "-c", SHELL_COMMAND, NULL}. */
- std::string m_storage;
-};
-
-/* Create argument vector for straight call to execvp. Breaks up
- ALLARGS into an argument vector suitable for passing to execvp and
- stores it in M_ARGV. E.g., on "run a b c d" this routine would get
- as input the string "a b c d", and as output it would fill in
- M_ARGV with the four arguments "a", "b", "c", "d". Each argument
- in M_ARGV points to a substring of a copy of ALLARGS stored in
- M_STORAGE. */
-
-void
-execv_argv::init_for_no_shell (const char *exec_file,
- const std::string &allargs)
-{
-
- /* Save/work with a copy stored in our storage. The pointers pushed
- to M_ARGV point directly into M_STORAGE, which is modified in
- place with the necessary NULL terminators. This avoids N heap
- allocations and string dups when 1 is sufficient. */
- std::string &args_copy = m_storage = allargs;
-
- m_argv.push_back (exec_file);
-
- for (size_t cur_pos = 0; cur_pos < args_copy.size ();)
- {
- /* Skip whitespace-like chars. */
- std::size_t pos = args_copy.find_first_not_of (" \t\n", cur_pos);
-
- if (pos != std::string::npos)
- cur_pos = pos;
-
- /* Find the position of the next separator. */
- std::size_t next_sep = args_copy.find_first_of (" \t\n", cur_pos);
-
- if (next_sep == std::string::npos)
- {
- /* No separator found, which means this is the last
- argument. */
- next_sep = args_copy.size ();
- }
- else
- {
- /* Replace the separator with a terminator. */
- args_copy[next_sep++] = '\0';
- }
-
- m_argv.push_back (&args_copy[cur_pos]);
-
- cur_pos = next_sep;
- }
-
- /* NULL-terminate the vector. */
- m_argv.push_back (NULL);
-}
-
-/* When executing a command under the given shell, return true if the
- '!' character should be escaped when embedded in a quoted
- command-line argument. */
-
-static bool
-escape_bang_in_quoted_argument (const char *shell_file)
-{
- size_t shell_file_len = strlen (shell_file);
-
- /* Bang should be escaped only in C Shells. For now, simply check
- that the shell name ends with 'csh', which covers at least csh
- and tcsh. This should be good enough for now. */
-
- if (shell_file_len < 3)
- return false;