--- /dev/null
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+// Author: Andy Rushton\r
+// Copyright: (c) Southampton University 1999-2004\r
+// (c) Andy Rushton 2004-2009\r
+// License: BSD License, see ../docs/license.html\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+// Bug fix by Alistair Low: kill on Windows now kills grandchild processes as\r
+// well as the child process. This is done using jobs - which has to be\r
+// enabled by stating that the version of Windows is at least 5.0\r
+#if defined(_WIN32) || defined(_WIN32_WCE)\r
+#define _WIN32_WINNT 0x0500\r
+#endif\r
+\r
+#include "subprocesses.hpp"\r
+#include "file_system.hpp"\r
+#include "dprintf.hpp"\r
+#include <ctype.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+\r
+#ifdef MSWINDOWS\r
+#else\r
+#include <signal.h>\r
+#include <errno.h>\r
+#include <sys/wait.h>\r
+#include <unistd.h>\r
+#include <fcntl.h>\r
+#endif\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // argument-vector related stuff\r
+\r
+ static void skip_white (const std::string& command, unsigned& i)\r
+ {\r
+ while(i < command.size() && isspace(command[i]))\r
+ i++;\r
+ }\r
+\r
+ // get_argument is the main function for breaking a string down into separate command arguments\r
+ // it mimics the way shells break down a command into an argv[] and unescapes the escaped characters on the way\r
+\r
+ static std::string get_argument (const std::string& command, unsigned& i)\r
+ {\r
+ std::string result;\r
+#ifdef MSWINDOWS\r
+\r
+ // as far as I know, there is only double-quoting and no escape character in DOS\r
+ // so, how do you include a double-quote in an argument???\r
+\r
+ bool dquote = false;\r
+ for ( ; i < command.size(); i++)\r
+ {\r
+ char ch = command[i];\r
+ if (!dquote && isspace(ch)) break;\r
+ if (dquote)\r
+ {\r
+ if (ch == '\"')\r
+ dquote = false;\r
+ else\r
+ result += ch;\r
+ }\r
+ else if (ch == '\"')\r
+ dquote = true;\r
+ else\r
+ result += ch;\r
+ }\r
+#else\r
+ bool squote = false;\r
+ bool dquote = false;\r
+ bool escaped = false;\r
+ for ( ; i < command.size(); i++)\r
+ {\r
+ char ch = command[i];\r
+ if (!squote && !dquote && !escaped && isspace(ch)) break;\r
+ if (escaped)\r
+ {\r
+ result += ch;\r
+ escaped = false;\r
+ }\r
+ else if (squote)\r
+ {\r
+ if (ch == '\'')\r
+ squote = false;\r
+ else\r
+ result += ch;\r
+ }\r
+ else if (ch == '\\')\r
+ escaped = true;\r
+ else if (dquote)\r
+ {\r
+ if (ch == '\"')\r
+ dquote = false;\r
+ else\r
+ result += ch;\r
+ }\r
+ else if (ch == '\'')\r
+ squote = true;\r
+ else if (ch == '\"')\r
+ dquote = true;\r
+ else\r
+ result += ch;\r
+ }\r
+#endif\r
+\r
+ return result;\r
+ }\r
+\r
+\r
+ // this function performs the reverse of the above on a single argument\r
+ // it escapes special characters and quotes the argument if necessary ready for shell interpretation\r
+\r
+ static std::string make_argument (const std::string& arg)\r
+ {\r
+ std::string result;\r
+ bool needs_quotes = false;\r
+\r
+ for (unsigned i = 0; i < arg.size(); i++)\r
+ {\r
+ switch (arg[i])\r
+ {\r
+ // set of characters requiring escapes\r
+#ifdef MSWINDOWS\r
+#else\r
+ case '\\': case '\'': case '\"': case '`': case '(': case ')': \r
+ case '&': case '|': case '<': case '>': case '*': case '?': case '!':\r
+ result += '\\';\r
+ result += arg[i];\r
+ break;\r
+#endif\r
+ // set of whitespace characters that force quoting\r
+ case ' ':\r
+ result += arg[i];\r
+ needs_quotes = true;\r
+ break;\r
+ default:\r
+ result += arg[i];\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (needs_quotes)\r
+ {\r
+ result.insert(result.begin(), '"');\r
+ result += '"';\r
+ }\r
+ return result;\r
+ }\r
+\r
+ static char* copy_string (const char* str)\r
+ {\r
+ char* result = new char[strlen(str)+1];\r
+ strcpy(result,str);\r
+ return result;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ arg_vector::arg_vector (void)\r
+ {\r
+ m_argv = 0;\r
+ }\r
+\r
+ arg_vector::arg_vector (const arg_vector& a)\r
+ {\r
+ m_argv = 0;\r
+ *this = a;\r
+ }\r
+\r
+ arg_vector::arg_vector (char** a)\r
+ {\r
+ m_argv = 0;\r
+ *this = a;\r
+ }\r
+\r
+ arg_vector::arg_vector (const std::string& command)\r
+ {\r
+ m_argv = 0;\r
+ *this = command;\r
+ }\r
+\r
+ arg_vector::arg_vector (const char* command)\r
+ {\r
+ m_argv = 0;\r
+ *this = command;\r
+ }\r
+\r
+ arg_vector::~arg_vector (void)\r
+ {\r
+ clear();\r
+ }\r
+\r
+ arg_vector& arg_vector::operator = (const arg_vector& a)\r
+ {\r
+ return *this = a.m_argv;\r
+ }\r
+\r
+ arg_vector& arg_vector::operator = (char** argv)\r
+ {\r
+ clear();\r
+ for (unsigned i = 0; argv[i]; i++)\r
+ operator += (argv[i]);\r
+ return *this;\r
+ }\r
+\r
+ arg_vector& arg_vector::operator = (const std::string& command)\r
+ {\r
+ clear();\r
+ for (unsigned i = 0; i < command.size(); )\r
+ {\r
+ std::string argument = get_argument(command, i);\r
+ operator += (argument);\r
+ skip_white(command, i);\r
+ }\r
+ return *this;\r
+ }\r
+\r
+ arg_vector& arg_vector::operator = (const char* command)\r
+ {\r
+ return operator = (std::string(command));\r
+ }\r
+\r
+ arg_vector& arg_vector::operator += (const std::string& str)\r
+ {\r
+ insert(size(), str);\r
+ return *this;\r
+ }\r
+\r
+ arg_vector& arg_vector::operator -= (const std::string& str)\r
+ {\r
+ insert(0, str);\r
+ return *this;\r
+ }\r
+\r
+ void arg_vector::insert (unsigned index, const std::string& str) throw(std::out_of_range)\r
+ {\r
+ if (index > size()) throw std::out_of_range("arg_vector::insert");\r
+ // copy up to but not including index, then add the new argument, then copy the rest\r
+ char** new_argv = new char*[size()+2];\r
+ unsigned i = 0;\r
+ for ( ; i < index; i++)\r
+ new_argv[i] = copy_string(m_argv[i]);\r
+ new_argv[index] = copy_string(str.c_str());\r
+ for ( ; i < size(); i++)\r
+ new_argv[i+1] = copy_string(m_argv[i]);\r
+ new_argv[i+1] = 0;\r
+ clear();\r
+ m_argv = new_argv;\r
+ }\r
+\r
+ void arg_vector::clear (unsigned index) throw(std::out_of_range)\r
+ {\r
+ if (index >= size()) throw std::out_of_range("arg_vector::clear");\r
+ // copy up to index, skip it, then copy the rest\r
+ char** new_argv = new char*[size()];\r
+ unsigned i = 0;\r
+ for ( ; i < index; i++)\r
+ new_argv[i] = copy_string(m_argv[i]);\r
+ i++;\r
+ for ( ; i < size(); i++)\r
+ new_argv[i-1] = copy_string(m_argv[i]);\r
+ new_argv[i-1] = 0;\r
+ clear();\r
+ m_argv = new_argv;\r
+ }\r
+\r
+ void arg_vector::clear(void)\r
+ {\r
+ if (m_argv)\r
+ {\r
+ for (unsigned i = 0; m_argv[i]; i++)\r
+ delete[] m_argv[i];\r
+ delete[] m_argv;\r
+ m_argv = 0;\r
+ }\r
+ }\r
+\r
+ unsigned arg_vector::size (void) const\r
+ {\r
+ unsigned i = 0;\r
+ if (m_argv)\r
+ while (m_argv[i])\r
+ i++;\r
+ return i;\r
+ }\r
+\r
+ arg_vector::operator char** (void) const\r
+ {\r
+ return m_argv;\r
+ }\r
+\r
+ char** arg_vector::argv (void) const\r
+ {\r
+ return m_argv;\r
+ }\r
+\r
+ char* arg_vector::operator [] (unsigned index) const throw(std::out_of_range)\r
+ {\r
+ if (index >= size()) throw std::out_of_range("arg_vector::operator[]");\r
+ return m_argv[index];\r
+ }\r
+\r
+ char* arg_vector::argv0 (void) const throw(std::out_of_range)\r
+ {\r
+ return operator [] (0);\r
+ }\r
+\r
+ std::string arg_vector::image (void) const\r
+ {\r
+ std::string result;\r
+ for (unsigned i = 0; i < size(); i++)\r
+ {\r
+ if (i) result += ' ';\r
+ result += make_argument(m_argv[i]);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // environment-vector\r
+\r
+ // Windoze environment is a single string containing null-terminated\r
+ // name=value strings and the whole terminated by a null\r
+\r
+ // Unix environment is a null-terminated vector of pointers to null-terminated strings\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // platform specifics\r
+\r
+#ifdef MSWINDOWS\r
+ // Windows utilities\r
+\r
+ // Windows environment variables are case-insensitive and I do comparisons by converting to lowercase\r
+ static std::string lowercase(const std::string& val)\r
+ {\r
+ std::string text = val;\r
+ for (unsigned i = 0; i < text.size(); i++)\r
+ text[i] = tolower(text[i]);\r
+ return text;\r
+ }\r
+\r
+ static unsigned envp_size(const char* envp)\r
+ {\r
+ unsigned size = 0;\r
+ while (envp[size] || (size > 0 && envp[size-1])) size++;\r
+ size++;\r
+ return size;\r
+ }\r
+\r
+ static void envp_extract(std::string& name, std::string& value, const char* envp, unsigned& envi)\r
+ {\r
+ name.erase();\r
+ value.erase();\r
+ if (!envp[envi]) return;\r
+ // some special variables start with '=' so ensure at least one character in the name\r
+ name += envp[envi++];\r
+ while(envp[envi] != '=')\r
+ name += envp[envi++];\r
+ envi++;\r
+ while(envp[envi])\r
+ value += envp[envi++];\r
+ envi++;\r
+ }\r
+\r
+ static void envp_append(const std::string& name, const std::string& value, char* envp, unsigned& envi)\r
+ {\r
+ for (unsigned i = 0; i < name.size(); i++)\r
+ envp[envi++] = name[i];\r
+ envp[envi++] = '=';\r
+ for (unsigned j = 0; j < value.size(); j++)\r
+ envp[envi++] = value[j];\r
+ envp[envi++] = '\0';\r
+ envp[envi] = '\0';\r
+ }\r
+\r
+ static char* envp_copy(const char* envp)\r
+ {\r
+ unsigned size = envp_size(envp);\r
+ char* result = new char[size];\r
+ result[0] = '\0';\r
+ unsigned i = 0;\r
+ unsigned j = 0;\r
+ while(envp[i])\r
+ {\r
+ std::string name;\r
+ std::string value;\r
+ envp_extract(name, value, envp, i);\r
+ envp_append(name, value, result, j);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ static void envp_clear(char*& envp)\r
+ {\r
+ if (envp)\r
+ {\r
+ delete[] envp;\r
+ envp = 0;\r
+ }\r
+ }\r
+\r
+ static bool envp_equal(const std::string& left, const std::string& right)\r
+ {\r
+ return lowercase(left) == lowercase(right);\r
+ }\r
+\r
+ static bool envp_less(const std::string& left, const std::string& right)\r
+ {\r
+ return lowercase(left) < lowercase(right);\r
+ }\r
+\r
+#else\r
+ // Unix utilities\r
+\r
+ extern char** environ;\r
+\r
+ static unsigned envp_size(char* const* envp)\r
+ {\r
+ unsigned size = 0;\r
+ while(envp[size]) size++;\r
+ size++;\r
+ return size;\r
+ }\r
+\r
+ static void envp_extract(std::string& name, std::string& value, char* const* envp, unsigned& envi)\r
+ {\r
+ name = "";\r
+ value = "";\r
+ if (!envp[envi]) return;\r
+ unsigned i = 0;\r
+ while(envp[envi][i] != '=')\r
+ name += envp[envi][i++];\r
+ i++;\r
+ while(envp[envi][i])\r
+ value += envp[envi][i++];\r
+ envi++;\r
+ }\r
+\r
+ static void envp_append(const std::string& name, const std::string& value, char** envp, unsigned& envi)\r
+ {\r
+ std::string entry = name + "=" + value;\r
+ envp[envi] = copy_string(entry.c_str());\r
+ envi++;\r
+ envp[envi] = 0;\r
+ }\r
+\r
+ static char** envp_copy(char* const* envp)\r
+ {\r
+ unsigned size = envp_size(envp);\r
+ char** result = new char*[size];\r
+ unsigned i = 0;\r
+ unsigned j = 0;\r
+ while(envp[i])\r
+ {\r
+ std::string name;\r
+ std::string value;\r
+ envp_extract(name, value, envp, i);\r
+ envp_append(name, value, result, j);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ static void envp_clear(char**& envp)\r
+ {\r
+ if (envp)\r
+ {\r
+ for (unsigned i = 0; envp[i]; i++)\r
+ delete[] envp[i];\r
+ delete[] envp;\r
+ envp = 0;\r
+ }\r
+ }\r
+\r
+ static bool envp_equal(const std::string& left, const std::string& right)\r
+ {\r
+ return left == right;\r
+ }\r
+\r
+ static bool envp_less(const std::string& left, const std::string& right)\r
+ {\r
+ return left < right;\r
+ }\r
+\r
+#endif\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ env_vector::env_vector(void)\r
+ {\r
+#ifdef MSWINDOWS\r
+ char* env = (char*)GetEnvironmentStringsA();\r
+ m_env = envp_copy(env);\r
+ FreeEnvironmentStringsA(env);\r
+#else\r
+ m_env = envp_copy(::environ);\r
+#endif\r
+ }\r
+\r
+ env_vector::env_vector (const env_vector& a)\r
+ {\r
+ m_env = 0;\r
+ *this = a;\r
+ }\r
+\r
+ env_vector::~env_vector (void)\r
+ {\r
+ clear();\r
+ }\r
+\r
+ env_vector& env_vector::operator = (const env_vector& a)\r
+ {\r
+ clear();\r
+ m_env = envp_copy(a.m_env);\r
+ return *this;\r
+ }\r
+\r
+ void env_vector::clear(void)\r
+ {\r
+ envp_clear(m_env);\r
+ }\r
+\r
+ void env_vector::add(const std::string& name, const std::string& value)\r
+ {\r
+ // the trick is to add the value in alphabetic order\r
+ // this is done by copying the existing environment string to a new\r
+ // string, inserting the new value when a name greater than it is found\r
+ unsigned size = envp_size(m_env);\r
+#ifdef MSWINDOWS\r
+ unsigned new_size = size + name.size() + value.size() + 2;\r
+ char* new_v = new char[new_size];\r
+ new_v[0] = '\0';\r
+#else\r
+ unsigned new_size = size + 1;\r
+ char** new_v = new char*[new_size];\r
+ new_v[0] = 0;\r
+#endif\r
+ // now extract each name=value pair and check the ordering\r
+ bool added = false;\r
+ unsigned i = 0;\r
+ unsigned j = 0;\r
+ while(m_env[i])\r
+ {\r
+ std::string current_name;\r
+ std::string current_value;\r
+ envp_extract(current_name, current_value, m_env, i);\r
+ if (envp_equal(name,current_name))\r
+ {\r
+ // replace an existing value\r
+ envp_append(name, value, new_v, j);\r
+ }\r
+ else if (!added && envp_less(name,current_name))\r
+ {\r
+ // add the new value first, then the existing one\r
+ envp_append(name, value, new_v, j);\r
+ envp_append(current_name, current_value, new_v, j);\r
+ added = true;\r
+ }\r
+ else\r
+ {\r
+ // just add the existing value\r
+ envp_append(current_name, current_value, new_v, j);\r
+ }\r
+ }\r
+ if (!added)\r
+ envp_append(name, value, new_v, j);\r
+ envp_clear(m_env);\r
+ m_env = new_v;\r
+ }\r
+\r
+\r
+ bool env_vector::remove (const std::string& name)\r
+ {\r
+ bool result = false;\r
+ // this is done by copying the existing environment string to a new string, but excluding the specified name\r
+ unsigned size = envp_size(m_env);\r
+#ifdef MSWINDOWS\r
+ char* new_v = new char[size];\r
+ new_v[0] = '\0';\r
+#else\r
+ char** new_v = new char*[size];\r
+ new_v[0] = 0;\r
+#endif\r
+ unsigned i = 0;\r
+ unsigned j = 0;\r
+ while(m_env[i])\r
+ {\r
+ std::string current_name;\r
+ std::string current_value;\r
+ envp_extract(current_name, current_value, m_env, i);\r
+ if (envp_equal(name,current_name))\r
+ result = true;\r
+ else\r
+ envp_append(current_name, current_value, new_v, j);\r
+ }\r
+ envp_clear(m_env);\r
+ m_env = new_v;\r
+ return result;\r
+ }\r
+\r
+ std::string env_vector::operator [] (const std::string& name) const\r
+ {\r
+ return get(name);\r
+ }\r
+\r
+ std::string env_vector::get (const std::string& name) const\r
+ {\r
+ unsigned i = 0;\r
+ while(m_env[i])\r
+ {\r
+ std::string current_name;\r
+ std::string current_value;\r
+ envp_extract(current_name, current_value, m_env, i);\r
+ if (envp_equal(name,current_name))\r
+ return current_value;\r
+ }\r
+ return std::string();\r
+ }\r
+\r
+ unsigned env_vector::size (void) const\r
+ {\r
+ unsigned i = 0;\r
+#ifdef MSWINDOWS\r
+ unsigned offset = 0;\r
+ while(m_env[offset])\r
+ {\r
+ std::string current_name;\r
+ std::string current_value;\r
+ envp_extract(current_name, current_value, m_env, offset);\r
+ i++;\r
+ }\r
+#else\r
+ while(m_env[i])\r
+ i++;\r
+#endif\r
+\r
+ return i;\r
+ }\r
+\r
+ std::pair<std::string,std::string> env_vector::operator [] (unsigned index) const throw(std::out_of_range)\r
+ {\r
+ return get(index);\r
+ }\r
+\r
+ std::pair<std::string,std::string> env_vector::get (unsigned index) const throw(std::out_of_range)\r
+ {\r
+ if (index >= size()) throw std::out_of_range("arg_vector::get");\r
+ unsigned j = 0;\r
+ for (unsigned i = 0; i < index; i++)\r
+ {\r
+ std::string current_name;\r
+ std::string current_value;\r
+ envp_extract(current_name, current_value, m_env, j);\r
+ }\r
+ std::string name;\r
+ std::string value;\r
+ envp_extract(name, value, m_env, j);\r
+ return std::make_pair(name,value);\r
+ }\r
+\r
+ ENVIRON_TYPE env_vector::envp (void) const\r
+ {\r
+ return m_env;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // Synchronous subprocess\r
+ // Win32 implementation mostly cribbed from MSDN examples and then made (much) more readable\r
+ // Unix implementation mostly from man pages and bitter experience\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ subprocess::subprocess(void)\r
+ {\r
+ m_pid.hProcess = 0;\r
+ m_job = 0;\r
+ m_child_in = 0;\r
+ m_child_out = 0;\r
+ m_child_err = 0;\r
+ m_err = 0;\r
+ m_status = 0;\r
+ }\r
+\r
+#else\r
+\r
+ subprocess::subprocess(void)\r
+ {\r
+ m_pid = -1;\r
+ m_child_in = -1;\r
+ m_child_out = -1;\r
+ m_child_err = -1;\r
+ m_err = 0;\r
+ m_status = 0;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ subprocess::~subprocess(void)\r
+ {\r
+ if (m_pid.hProcess != 0)\r
+ {\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ kill();\r
+ WaitForSingleObject(m_pid.hProcess, INFINITE);\r
+ CloseHandle(m_pid.hThread);\r
+ CloseHandle(m_pid.hProcess);\r
+ CloseHandle(m_job);\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ subprocess::~subprocess(void)\r
+ {\r
+ if (m_pid != -1)\r
+ {\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ kill();\r
+ for (;;)\r
+ {\r
+ int wait_status = 0;\r
+ int wait_ret_val = waitpid(m_pid, &wait_status, 0);\r
+ if (wait_ret_val != -1 || errno != EINTR) break;\r
+ }\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+ void subprocess::add_variable(const std::string& name, const std::string& value)\r
+ {\r
+ m_env.add(name, value);\r
+ }\r
+\r
+ bool subprocess::remove_variable(const std::string& name)\r
+ {\r
+ return m_env.remove(name);\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ bool subprocess::spawn(const std::string& path, const arg_vector& argv,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ bool result = true;\r
+ // first create the pipes to be used to connect to the child stdin/out/err\r
+ // If no pipes requested, then connect to the parent stdin/out/err\r
+ // for some reason you have to create a pipe handle, then duplicate it\r
+ // This is not well explained in MSDN but seems to work\r
+ PIPE_TYPE parent_stdin = 0;\r
+ if (!connect_stdin)\r
+ parent_stdin = GetStdHandle(STD_INPUT_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&parent_stdin, &tmp, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_in, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ PIPE_TYPE parent_stdout = 0;\r
+ if (!connect_stdout)\r
+ parent_stdout = GetStdHandle(STD_OUTPUT_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&tmp, &parent_stdout, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_out, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ PIPE_TYPE parent_stderr = 0;\r
+ if (!connect_stderr)\r
+ parent_stderr = GetStdHandle(STD_ERROR_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&tmp, &parent_stderr, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_err, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ // Now create the subprocess\r
+ // The horrible trick of creating a console window and hiding it seems to be required for the pipes to work\r
+ // Note that the child will inherit a copy of the pipe handles\r
+ STARTUPINFOA startup = {sizeof(STARTUPINFO),0,0,0,0,0,0,0,0,0,0,\r
+ STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW,SW_HIDE,0,0,\r
+ parent_stdin,parent_stdout,parent_stderr};\r
+ bool created = CreateProcessA(path.c_str(),(char*)argv.image().c_str(),0,0,TRUE,CREATE_SUSPENDED,m_env.envp(),0,&startup,&m_pid) != 0;\r
+ // close the parent copy of the pipe handles so that the pipes will be closed when the child releases them\r
+ if (connect_stdin) CloseHandle(parent_stdin);\r
+ if (connect_stdout) CloseHandle(parent_stdout);\r
+ if (connect_stderr) CloseHandle(parent_stderr);\r
+ if (!created)\r
+ {\r
+ m_err = GetLastError();\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ result = false;\r
+ }\r
+ else\r
+ {\r
+ m_job = CreateJobObject(NULL, NULL);\r
+ AssignProcessToJobObject(m_job, m_pid.hProcess);\r
+ ResumeThread(m_pid.hThread);\r
+\r
+ // The child process is now running so call the user's callback\r
+ // The convention is that the callback can return false, in which case this will kill the child (if its still running)\r
+ if (!callback())\r
+ {\r
+ result = false;\r
+ kill();\r
+ }\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ // wait for the child to finish\r
+ // TODO - kill the child if a timeout happens\r
+ WaitForSingleObject(m_pid.hProcess, INFINITE);\r
+ DWORD exit_status = 0;\r
+ if (!GetExitCodeProcess(m_pid.hProcess, &exit_status))\r
+ {\r
+ m_err = GetLastError();\r
+ result = false;\r
+ }\r
+ else if (exit_status != 0)\r
+ result = false;\r
+ m_status = (int)exit_status;\r
+ CloseHandle(m_pid.hThread);\r
+ CloseHandle(m_pid.hProcess);\r
+ CloseHandle(m_job);\r
+ }\r
+ m_pid.hProcess = 0;\r
+ return result;\r
+ }\r
+\r
+#else\r
+\r
+ bool subprocess::spawn(const std::string& path, const arg_vector& argv,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ bool result = true;\r
+ // first create the pipes to be used to connect to the child stdin/out/err\r
+\r
+ int stdin_pipe [2] = {-1, -1};\r
+ if (connect_stdin)\r
+ pipe(stdin_pipe);\r
+\r
+ int stdout_pipe [2] = {-1, -1};\r
+ if (connect_stdout)\r
+ pipe(stdout_pipe);\r
+\r
+ int stderr_pipe [2] = {-1, -1};\r
+ if (connect_stderr)\r
+ pipe(stderr_pipe);\r
+\r
+ // now create the subprocess\r
+ // In Unix, this is done by forking (creating two copies of the parent), then overwriting the child copy using exec\r
+ m_pid = ::fork();\r
+ switch(m_pid)\r
+ {\r
+ case -1: // failed to fork\r
+ m_err = errno;\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[0]);\r
+ ::close(stdin_pipe[1]);\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[0]);\r
+ ::close(stdout_pipe[1]);\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[0]);\r
+ ::close(stderr_pipe[1]);\r
+ }\r
+ result = false;\r
+ break;\r
+ case 0: // in child;\r
+ {\r
+ // for each pipe, close the end of the duplicated pipe that is being used by the parent\r
+ // and connect the child's end of the pipe to the appropriate standard I/O device\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[1]);\r
+ dup2(stdin_pipe[0],STDIN_FILENO);\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[0]);\r
+ dup2(stdout_pipe[1],STDOUT_FILENO);\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[0]);\r
+ dup2(stderr_pipe[1],STDERR_FILENO);\r
+ }\r
+ execve(path.c_str(), argv.argv(), m_env.envp());\r
+ // will only ever get here if the exec() failed completely - *must* now exit the child process\r
+ // by using errno, the parent has some chance of diagnosing the cause of the problem\r
+ exit(errno);\r
+ }\r
+ break;\r
+ default: // in parent\r
+ {\r
+ // for each pipe, close the end of the duplicated pipe that is being used by the child\r
+ // and connect the parent's end of the pipe to the class members so that they are visible to the parent() callback\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[0]);\r
+ m_child_in = stdin_pipe[1];\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[1]);\r
+ m_child_out = stdout_pipe[0];\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[1]);\r
+ m_child_err = stderr_pipe[0];\r
+ }\r
+ // call the user's callback\r
+ if (!callback())\r
+ {\r
+ result = false;\r
+ kill();\r
+ }\r
+ // close the pipes and wait for the child to finish\r
+ // wait exits on a signal which may be the child signalling its exit or may be an interrupt\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ int wait_status = 0;\r
+ for (;;)\r
+ {\r
+ int wait_ret_val = waitpid(m_pid, &wait_status, 0);\r
+ if (wait_ret_val != -1 || errno != EINTR) break;\r
+ }\r
+ // establish whether an error occurred\r
+ if (WIFSIGNALED(wait_status))\r
+ {\r
+ // set_error(errno);\r
+ m_status = WTERMSIG(wait_status);\r
+ result = false;\r
+ }\r
+ else if (WIFEXITED(wait_status))\r
+ {\r
+ m_status = WEXITSTATUS(wait_status);\r
+ if (m_status != 0)\r
+ result = false;\r
+ }\r
+ m_pid = -1;\r
+ }\r
+ break;\r
+ }\r
+ return result;\r
+ }\r
+\r
+#endif\r
+\r
+ bool subprocess::spawn(const std::string& command_line,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ arg_vector arguments = command_line;\r
+ if (arguments.size() == 0) return false;\r
+ std::string path = path_lookup(arguments.argv0());\r
+ if (path.empty()) return false;\r
+ return spawn(path, arguments, connect_stdin, connect_stdout, connect_stderr);\r
+ }\r
+\r
+ bool subprocess::callback(void)\r
+ {\r
+ return true;\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ bool subprocess::kill (void)\r
+ {\r
+ if (!m_pid.hProcess) return false;\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ if (!TerminateJobObject(m_job, (UINT)-1))\r
+ {\r
+ m_err = GetLastError();\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+#else\r
+\r
+ bool subprocess::kill (void)\r
+ {\r
+ if (m_pid == -1) return false;\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ if (::kill(m_pid, SIGINT) == -1)\r
+ {\r
+ m_err = errno;\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int subprocess::write_stdin (std::string& buffer)\r
+ {\r
+ if (m_child_in == 0) return -1;\r
+ // do a blocking write of the whole buffer\r
+ DWORD bytes = 0;\r
+ if (!WriteFile(m_child_in, buffer.c_str(), (DWORD)buffer.size(), &bytes, 0))\r
+ {\r
+ m_err = GetLastError();\r
+ close_stdin();\r
+ return -1;\r
+ }\r
+ // now discard that part of the buffer that was written\r
+ if (bytes > 0)\r
+ buffer.erase(0, bytes);\r
+ return bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int subprocess::write_stdin (std::string& buffer)\r
+ {\r
+ if (m_child_in == -1) return -1;\r
+ // do a blocking write of the whole buffer\r
+ int bytes = write(m_child_in, buffer.c_str(), buffer.size());\r
+ if (bytes == -1)\r
+ {\r
+ m_err = errno;\r
+ close_stdin();\r
+ return -1;\r
+ }\r
+ // now discard that part of the buffer that was written\r
+ if (bytes > 0)\r
+ buffer.erase(0, bytes);\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int subprocess::read_stdout (std::string& buffer)\r
+ {\r
+ if (m_child_out == 0) return -1;\r
+ DWORD bytes = 0;\r
+ DWORD buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ if (!ReadFile(m_child_out, tmp, buffer_size, &bytes, 0))\r
+ {\r
+ if (GetLastError() != ERROR_BROKEN_PIPE)\r
+ m_err = GetLastError();\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return (int)bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int subprocess::read_stdout (std::string& buffer)\r
+ {\r
+ if (m_child_out == -1) return -1;\r
+ int buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ int bytes = read(m_child_out, tmp, buffer_size);\r
+ if (bytes == -1)\r
+ {\r
+ m_err = errno;\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int subprocess::read_stderr(std::string& buffer)\r
+ {\r
+ if (m_child_err == 0) return -1;\r
+ DWORD bytes = 0;\r
+ DWORD buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ if (!ReadFile(m_child_err, tmp, buffer_size, &bytes, 0))\r
+ {\r
+ if (GetLastError() != ERROR_BROKEN_PIPE)\r
+ m_err = GetLastError();\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return (int)bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int subprocess::read_stderr (std::string& buffer)\r
+ {\r
+ if (m_child_err == -1) return -1;\r
+ int buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ int bytes = read(m_child_err, tmp, buffer_size);\r
+ if (bytes == -1)\r
+ {\r
+ m_err = errno;\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void subprocess::close_stdin (void)\r
+ {\r
+ if (m_child_in)\r
+ {\r
+ CloseHandle(m_child_in);\r
+ m_child_in = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void subprocess::close_stdin (void)\r
+ {\r
+ if (m_child_in != -1)\r
+ {\r
+ ::close(m_child_in);\r
+ m_child_in = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void subprocess::close_stdout (void)\r
+ {\r
+ if (m_child_out)\r
+ {\r
+ CloseHandle(m_child_out);\r
+ m_child_out = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void subprocess::close_stdout (void)\r
+ {\r
+ if (m_child_out != -1)\r
+ {\r
+ ::close(m_child_out);\r
+ m_child_out = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void subprocess::close_stderr (void)\r
+ {\r
+ if (m_child_err)\r
+ {\r
+ CloseHandle(m_child_err);\r
+ m_child_err = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void subprocess::close_stderr (void)\r
+ {\r
+ if (m_child_err != -1)\r
+ {\r
+ ::close(m_child_err);\r
+ m_child_err = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+ bool subprocess::error(void) const\r
+ {\r
+ return m_err != 0;\r
+ }\r
+\r
+ int subprocess::error_number(void) const\r
+ {\r
+ return m_err;\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ std::string subprocess::error_text(void) const\r
+ {\r
+ if (m_err == 0) return std::string();\r
+ char* message;\r
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,\r
+ 0,\r
+ m_err,\r
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
+ (LPTSTR)&message,\r
+ 0,0);\r
+ std::string result = message;\r
+ LocalFree(message);\r
+ // the error message is for some perverse reason newline terminated - remove this\r
+ if (result[result.size()-1] == '\n')\r
+ result.erase(result.end()-1);\r
+ if (result[result.size()-1] == '\r')\r
+ result.erase(result.end()-1);\r
+ return result;\r
+ }\r
+\r
+#else\r
+\r
+ std::string subprocess::error_text(void) const\r
+ {\r
+ if (m_err == 0) return std::string();\r
+ char* text = strerror(m_err);\r
+ if (text) return std::string(text);\r
+ return "error number " + dformat("%d",m_err);\r
+ }\r
+\r
+#endif\r
+\r
+ int subprocess::exit_status(void) const\r
+ {\r
+ return m_status;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // backtick subprocess and operations\r
+\r
+ backtick_subprocess::backtick_subprocess(void) : subprocess()\r
+ {\r
+ }\r
+\r
+ bool backtick_subprocess::callback(void)\r
+ {\r
+ for (;;)\r
+ {\r
+ std::string buffer;\r
+ int read_size = read_stdout(buffer);\r
+ if (read_size < 0) break;\r
+ m_text += buffer;\r
+ }\r
+ return !error();\r
+ }\r
+\r
+ bool backtick_subprocess::spawn(const std::string& path, const arg_vector& argv)\r
+ {\r
+ return subprocess::spawn(path, argv, false, true, false);\r
+ }\r
+\r
+ bool backtick_subprocess::spawn(const std::string& command_line)\r
+ {\r
+ return subprocess::spawn(command_line, false, true, false);\r
+ }\r
+\r
+ std::vector<std::string> backtick_subprocess::text(void) const\r
+ {\r
+ std::vector<std::string> result;\r
+ // convert the raw text into a vector of strings, each corresponding to a line\r
+ // in the process, strip out platform-specific line-endings\r
+ for (unsigned i = 0; i < m_text.size(); i++)\r
+ {\r
+ // handle any kind of line-ending - Dos, Unix or MacOS\r
+ switch(m_text[i])\r
+ {\r
+ case '\xd': // carriage-return - optionally followed by linefeed\r
+ {\r
+ // discard optional following linefeed\r
+ if ((i+1 < m_text.size()) && (m_text[i+1] == '\xa'))\r
+ i++;\r
+ // add a new line to the end of the vector\r
+ result.push_back(std::string());\r
+ break;\r
+ }\r
+ case '\xa': // linefeed\r
+ {\r
+ // add a new line to the end of the vector\r
+ result.push_back(std::string());\r
+ break;\r
+ }\r
+ default:\r
+ {\r
+ result.back() += m_text[i];\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ // tidy up - if the last line ended with a newline, the vector will end with an empty string - discard this\r
+ if ((result.size()) > 0 && result.back().empty())\r
+ result.erase(result.end()-1);\r
+ return result;\r
+ }\r
+\r
+ std::vector<std::string> backtick(const std::string& path, const arg_vector& argv)\r
+ {\r
+ backtick_subprocess sub;\r
+ sub.spawn(path, argv);\r
+ return sub.text();\r
+ }\r
+\r
+ std::vector<std::string> backtick(const std::string& command_line)\r
+ {\r
+ backtick_subprocess sub;\r
+ sub.spawn(command_line);\r
+ return sub.text();\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // Asynchronous subprocess\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ async_subprocess::async_subprocess(void)\r
+ {\r
+ m_pid.hProcess = 0;\r
+ m_job = 0;\r
+ m_child_in = 0;\r
+ m_child_out = 0;\r
+ m_child_err = 0;\r
+ m_err = 0;\r
+ m_status = 0;\r
+ }\r
+\r
+#else\r
+\r
+ async_subprocess::async_subprocess(void)\r
+ {\r
+ m_pid = -1;\r
+ m_child_in = -1;\r
+ m_child_out = -1;\r
+ m_child_err = -1;\r
+ m_err = 0;\r
+ m_status = 0;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ async_subprocess::~async_subprocess(void)\r
+ {\r
+ if (m_pid.hProcess != 0)\r
+ {\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ kill();\r
+ WaitForSingleObject(m_pid.hProcess, INFINITE);\r
+ CloseHandle(m_pid.hThread);\r
+ CloseHandle(m_pid.hProcess);\r
+ CloseHandle(m_job);\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ async_subprocess::~async_subprocess(void)\r
+ {\r
+ if (m_pid != -1)\r
+ {\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ kill();\r
+ for (;;)\r
+ {\r
+ int wait_status = 0;\r
+ int wait_ret_val = waitpid(m_pid, &wait_status, 0);\r
+ if (wait_ret_val != -1 || errno != EINTR) break;\r
+ }\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+ void async_subprocess::set_error(int e)\r
+ {\r
+ m_err = e;\r
+ }\r
+\r
+ void async_subprocess::add_variable(const std::string& name, const std::string& value)\r
+ {\r
+ m_env.add(name, value);\r
+ }\r
+\r
+ bool async_subprocess::remove_variable(const std::string& name)\r
+ {\r
+ return m_env.remove(name);\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ bool async_subprocess::spawn(const std::string& path, const arg_vector& argv,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ bool result = true;\r
+ // first create the pipes to be used to connect to the child stdin/out/err\r
+ // If no pipes requested, then connect to the parent stdin/out/err\r
+ // for some reason you have to create a pipe handle, then duplicate it\r
+ // This is not well explained in MSDN but seems to work\r
+ PIPE_TYPE parent_stdin = 0;\r
+ if (!connect_stdin)\r
+ parent_stdin = GetStdHandle(STD_INPUT_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&parent_stdin, &tmp, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_in, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ PIPE_TYPE parent_stdout = 0;\r
+ if (!connect_stdout)\r
+ parent_stdout = GetStdHandle(STD_OUTPUT_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&tmp, &parent_stdout, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_out, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ PIPE_TYPE parent_stderr = 0;\r
+ if (!connect_stderr)\r
+ parent_stderr = GetStdHandle(STD_ERROR_HANDLE);\r
+ else\r
+ {\r
+ PIPE_TYPE tmp = 0;\r
+ SECURITY_ATTRIBUTES inherit_handles = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};\r
+ CreatePipe(&tmp, &parent_stderr, &inherit_handles, 0);\r
+ DuplicateHandle(GetCurrentProcess(), tmp, GetCurrentProcess(), &m_child_err, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);\r
+ }\r
+\r
+ // Now create the subprocess\r
+ // The horrible trick of creating a console window and hiding it seems to be required for the pipes to work\r
+ // Note that the child will inherit a copy of the pipe handles\r
+ STARTUPINFOA startup = {sizeof(STARTUPINFO),0,0,0,0,0,0,0,0,0,0,\r
+ STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW,SW_HIDE,0,0,\r
+ parent_stdin,parent_stdout,parent_stderr};\r
+ bool created = CreateProcessA(path.c_str(),(char*)argv.image().c_str(),0,0,TRUE,CREATE_SUSPENDED,m_env.envp(),0,&startup,&m_pid) != 0;\r
+ // close the parent copy of the pipe handles so that the pipes will be closed when the child releases them\r
+ if (connect_stdin) CloseHandle(parent_stdin);\r
+ if (connect_stdout) CloseHandle(parent_stdout);\r
+ if (connect_stderr) CloseHandle(parent_stderr);\r
+ if (!created)\r
+ {\r
+ set_error(GetLastError());\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ result = false;\r
+ }\r
+ else\r
+ {\r
+ m_job = CreateJobObject(NULL, NULL);\r
+ AssignProcessToJobObject(m_job, m_pid.hProcess);\r
+ ResumeThread(m_pid.hThread);\r
+ }\r
+ return result;\r
+ }\r
+\r
+#else\r
+\r
+ bool async_subprocess::spawn(const std::string& path, const arg_vector& argv,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ bool result = true;\r
+ // first create the pipes to be used to connect to the child stdin/out/err\r
+\r
+ int stdin_pipe [2] = {-1, -1};\r
+ if (connect_stdin)\r
+ pipe(stdin_pipe);\r
+\r
+ int stdout_pipe [2] = {-1, -1};\r
+ if (connect_stdout)\r
+ pipe(stdout_pipe);\r
+\r
+ int stderr_pipe [2] = {-1, -1};\r
+ if (connect_stderr)\r
+ pipe(stderr_pipe);\r
+\r
+ // now create the subprocess\r
+ // In Unix, this is done by forking (creating two copies of the parent), then overwriting the child copy using exec\r
+ m_pid = ::fork();\r
+ switch(m_pid)\r
+ {\r
+ case -1: // failed to fork\r
+ set_error(errno);\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[0]);\r
+ ::close(stdin_pipe[1]);\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[0]);\r
+ ::close(stdout_pipe[1]);\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[0]);\r
+ ::close(stderr_pipe[1]);\r
+ }\r
+ result = false;\r
+ break;\r
+ case 0: // in child;\r
+ {\r
+ // for each pipe, close the end of the duplicated pipe that is being used by the parent\r
+ // and connect the child's end of the pipe to the appropriate standard I/O device\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[1]);\r
+ dup2(stdin_pipe[0],STDIN_FILENO);\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[0]);\r
+ dup2(stdout_pipe[1],STDOUT_FILENO);\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[0]);\r
+ dup2(stderr_pipe[1],STDERR_FILENO);\r
+ }\r
+ execve(path.c_str(), argv.argv(), m_env.envp());\r
+ // will only ever get here if the exec() failed completely - *must* now exit the child process\r
+ // by using errno, the parent has some chance of diagnosing the cause of the problem\r
+ exit(errno);\r
+ }\r
+ break;\r
+ default: // in parent\r
+ {\r
+ // for each pipe, close the end of the duplicated pipe that is being used by the child\r
+ // and connect the parent's end of the pipe to the class members so that they are visible to the parent() callback\r
+ if (connect_stdin)\r
+ {\r
+ ::close(stdin_pipe[0]);\r
+ m_child_in = stdin_pipe[1];\r
+ if (fcntl(m_child_in, F_SETFL, O_NONBLOCK) == -1)\r
+ {\r
+ set_error(errno);\r
+ result = false;\r
+ }\r
+ }\r
+ if (connect_stdout)\r
+ {\r
+ ::close(stdout_pipe[1]);\r
+ m_child_out = stdout_pipe[0];\r
+ if (fcntl(m_child_out, F_SETFL, O_NONBLOCK) == -1)\r
+ {\r
+ set_error(errno);\r
+ result = false;\r
+ }\r
+ }\r
+ if (connect_stderr)\r
+ {\r
+ ::close(stderr_pipe[1]);\r
+ m_child_err = stderr_pipe[0];\r
+ if (fcntl(m_child_err, F_SETFL, O_NONBLOCK) == -1)\r
+ {\r
+ set_error(errno);\r
+ result = false;\r
+ }\r
+ }\r
+ }\r
+ break;\r
+ }\r
+ return result;\r
+ }\r
+\r
+#endif\r
+\r
+ bool async_subprocess::spawn(const std::string& command_line,\r
+ bool connect_stdin, bool connect_stdout, bool connect_stderr)\r
+ {\r
+ arg_vector arguments = command_line;\r
+ if (arguments.size() == 0) return false;\r
+ std::string path = path_lookup(arguments.argv0());\r
+ if (path.empty()) return false;\r
+ return spawn(path, arguments, connect_stdin, connect_stdout, connect_stderr);\r
+ }\r
+\r
+ bool async_subprocess::callback(void)\r
+ {\r
+ return true;\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ bool async_subprocess::tick(void)\r
+ {\r
+ bool result = true;\r
+ if (!callback())\r
+ kill();\r
+ DWORD exit_status = 0;\r
+ if (!GetExitCodeProcess(m_pid.hProcess, &exit_status))\r
+ {\r
+ set_error(GetLastError());\r
+ result = false;\r
+ }\r
+ else if (exit_status != STILL_ACTIVE)\r
+ {\r
+ CloseHandle(m_pid.hThread);\r
+ CloseHandle(m_pid.hProcess);\r
+ CloseHandle(m_job);\r
+ m_pid.hProcess = 0;\r
+ result = false;\r
+ }\r
+ m_status = (int)exit_status;\r
+ return result;\r
+ }\r
+\r
+#else\r
+\r
+ bool async_subprocess::tick(void)\r
+ {\r
+ bool result = true;\r
+ if (!callback())\r
+ kill();\r
+ int wait_status = 0;\r
+ int wait_ret_val = waitpid(m_pid, &wait_status, WNOHANG);\r
+ if (wait_ret_val == -1 && errno != EINTR)\r
+ {\r
+ set_error(errno);\r
+ result = false;\r
+ }\r
+ else if (wait_ret_val != 0)\r
+ {\r
+ // the only states that indicate a terminated child are WIFSIGNALLED and WIFEXITED\r
+ if (WIFSIGNALED(wait_status))\r
+ {\r
+ // set_error(errno);\r
+ m_status = WTERMSIG(wait_status);\r
+ result = false;\r
+ }\r
+ else if (WIFEXITED(wait_status))\r
+ {\r
+ // child has exited\r
+ m_status = WEXITSTATUS(wait_status);\r
+ result = false;\r
+ }\r
+ }\r
+ if (!result)\r
+ m_pid = -1;\r
+ return result;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ bool async_subprocess::kill(void)\r
+ {\r
+ if (!m_pid.hProcess) return false;\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ if (!TerminateJobObject(m_job, (UINT)-1))\r
+ {\r
+ set_error(GetLastError());\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+#else\r
+\r
+ bool async_subprocess::kill(void)\r
+ {\r
+ if (m_pid == -1) return false;\r
+ close_stdin();\r
+ close_stdout();\r
+ close_stderr();\r
+ if (::kill(m_pid, SIGINT) == -1)\r
+ {\r
+ set_error(errno);\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int async_subprocess::write_stdin (std::string& buffer)\r
+ {\r
+ if (m_child_in == 0) return -1;\r
+ // there doesn't seem to be a way of doing non-blocking writes under Windoze\r
+ DWORD bytes = 0;\r
+ if (!WriteFile(m_child_in, buffer.c_str(), (DWORD)buffer.size(), &bytes, 0))\r
+ {\r
+ set_error(GetLastError());\r
+ close_stdin();\r
+ return -1;\r
+ }\r
+ // now discard that part of the buffer that was written\r
+ if (bytes > 0)\r
+ buffer.erase(0, bytes);\r
+ return (int)bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int async_subprocess::write_stdin (std::string& buffer)\r
+ {\r
+ if (m_child_in == -1) return -1;\r
+ // relies on the pipe being non-blocking\r
+ // This does block under Windoze\r
+ int bytes = write(m_child_in, buffer.c_str(), buffer.size());\r
+ if (bytes == -1 && errno == EAGAIN)\r
+ {\r
+ // not ready\r
+ return 0;\r
+ }\r
+ if (bytes == -1)\r
+ {\r
+ // error on write - close the pipe and give up\r
+ set_error(errno);\r
+ close_stdin();\r
+ return -1;\r
+ }\r
+ // successful write\r
+ // now discard that part of the buffer that was written\r
+ if (bytes > 0)\r
+ buffer.erase(0, bytes);\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int async_subprocess::read_stdout (std::string& buffer)\r
+ {\r
+ if (m_child_out == 0) return -1;\r
+ // peek at the buffer to see how much data there is in the first place\r
+ DWORD buffer_size = 0;\r
+ if (!PeekNamedPipe(m_child_out, 0, 0, 0, &buffer_size, 0))\r
+ {\r
+ if (GetLastError() != ERROR_BROKEN_PIPE)\r
+ set_error(GetLastError());\r
+ close_stdout();\r
+ return -1;\r
+ }\r
+ if (buffer_size == 0) return 0;\r
+ DWORD bytes = 0;\r
+ char* tmp = new char[buffer_size];\r
+ if (!ReadFile(m_child_out, tmp, buffer_size, &bytes, 0))\r
+ {\r
+ set_error(GetLastError());\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return (int)bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int async_subprocess::read_stdout (std::string& buffer)\r
+ {\r
+ if (m_child_out == -1) return -1;\r
+ // rely on the pipe being non-blocking\r
+ int buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ int bytes = read(m_child_out, tmp, buffer_size);\r
+ if (bytes == -1 && errno == EAGAIN)\r
+ {\r
+ // not ready\r
+ delete[] tmp;\r
+ return 0;\r
+ }\r
+ if (bytes == -1)\r
+ {\r
+ // error\r
+ set_error(errno);\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stdout();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ // successful read\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ int async_subprocess::read_stderr (std::string& buffer)\r
+ {\r
+ if (m_child_err == 0) return -1;\r
+ // peek at the buffer to see how much data there is in the first place\r
+ DWORD buffer_size = 0;\r
+ if (!PeekNamedPipe(m_child_err, 0, 0, 0, &buffer_size, 0))\r
+ {\r
+ if (GetLastError() != ERROR_BROKEN_PIPE)\r
+ set_error(GetLastError());\r
+ close_stderr();\r
+ return -1;\r
+ }\r
+ if (buffer_size == 0) return 0;\r
+ DWORD bytes = 0;\r
+ char* tmp = new char[buffer_size];\r
+ if (!ReadFile(m_child_err, tmp, buffer_size, &bytes, 0))\r
+ {\r
+ set_error(GetLastError());\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return (int)bytes;\r
+ }\r
+\r
+#else\r
+\r
+ int async_subprocess::read_stderr (std::string& buffer)\r
+ {\r
+ if (m_child_err == -1) return -1;\r
+ // rely on the pipe being non-blocking\r
+ int buffer_size = 256;\r
+ char* tmp = new char[buffer_size];\r
+ int bytes = read(m_child_err, tmp, buffer_size);\r
+ if (bytes == -1 && errno == EAGAIN)\r
+ {\r
+ // not ready\r
+ delete[] tmp;\r
+ return 0;\r
+ }\r
+ if (bytes == -1)\r
+ {\r
+ // error\r
+ set_error(errno);\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ if (bytes == 0)\r
+ {\r
+ // EOF\r
+ close_stderr();\r
+ delete[] tmp;\r
+ return -1;\r
+ }\r
+ // successful read\r
+ buffer.append(tmp, bytes);\r
+ delete[] tmp;\r
+ return bytes;\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void async_subprocess::close_stdin (void)\r
+ {\r
+ if (m_child_in)\r
+ {\r
+ CloseHandle(m_child_in);\r
+ m_child_in = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void async_subprocess::close_stdin (void)\r
+ {\r
+ if (m_child_in != -1)\r
+ {\r
+ ::close(m_child_in);\r
+ m_child_in = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void async_subprocess::close_stdout (void)\r
+ {\r
+ if (m_child_out)\r
+ {\r
+ CloseHandle(m_child_out);\r
+ m_child_out = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void async_subprocess::close_stdout (void)\r
+ {\r
+ if (m_child_out != -1)\r
+ {\r
+ ::close(m_child_out);\r
+ m_child_out = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ void async_subprocess::close_stderr (void)\r
+ {\r
+ if (m_child_err)\r
+ {\r
+ CloseHandle(m_child_err);\r
+ m_child_err = 0;\r
+ }\r
+ }\r
+\r
+#else\r
+\r
+ void async_subprocess::close_stderr (void)\r
+ {\r
+ if (m_child_err != -1)\r
+ {\r
+ ::close(m_child_err);\r
+ m_child_err = -1;\r
+ }\r
+ }\r
+\r
+#endif\r
+\r
+ bool async_subprocess::error(void) const\r
+ {\r
+ return m_err != 0;\r
+ }\r
+\r
+ int async_subprocess::error_number(void) const\r
+ {\r
+ return m_err;\r
+ }\r
+\r
+#ifdef MSWINDOWS\r
+\r
+ std::string async_subprocess::error_text(void) const\r
+ {\r
+ if (m_err == 0) return std::string();\r
+ char* message;\r
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,\r
+ 0,\r
+ m_err,\r
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
+ (LPTSTR)&message,\r
+ 0,0);\r
+ std::string result = message;\r
+ LocalFree(message);\r
+ // the error message is for some perverse reason newline terminated - remove this\r
+ if (result[result.size()-1] == '\n')\r
+ result.erase(result.end()-1);\r
+ if (result[result.size()-1] == '\r')\r
+ result.erase(result.end()-1);\r
+ return result;\r
+ }\r
+\r
+#else\r
+\r
+ std::string async_subprocess::error_text(void) const\r
+ {\r
+ if (m_err == 0) return std::string();\r
+ char* text = strerror(m_err);\r
+ if (text) return std::string(text);\r
+ return "error number " + dformat("%d",m_err);\r
+ }\r
+\r
+#endif\r
+\r
+ int async_subprocess::exit_status(void) const\r
+ {\r
+ return m_status;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r