+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)