Windows: Implement a custom spawnve().
[gitweb.git] / compat / mingw.c
index d184c582eb9d2898bc0037c40dbcca924a93168a..1ef2a4caf2be92acebd847a9707c78e41d26eb50 100644 (file)
@@ -1,4 +1,5 @@
 #include "../git-compat-util.h"
+#include "../strbuf.h"
 
 unsigned int _CRT_fmode = _O_BINARY;
 
@@ -186,6 +187,65 @@ char *mingw_getcwd(char *pointer, int len)
        return ret;
 }
 
+/*
+ * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
+ * (Parsing C++ Command-Line Arguments)
+ */
+static const char *quote_arg(const char *arg)
+{
+       /* count chars to quote */
+       int len = 0, n = 0;
+       int force_quotes = 0;
+       char *q, *d;
+       const char *p = arg;
+       if (!*p) force_quotes = 1;
+       while (*p) {
+               if (isspace(*p) || *p == '*' || *p == '?' || *p == '{')
+                       force_quotes = 1;
+               else if (*p == '"')
+                       n++;
+               else if (*p == '\\') {
+                       int count = 0;
+                       while (*p == '\\') {
+                               count++;
+                               p++;
+                               len++;
+                       }
+                       if (*p == '"')
+                               n += count*2 + 1;
+                       continue;
+               }
+               len++;
+               p++;
+       }
+       if (!force_quotes && n == 0)
+               return arg;
+
+       /* insert \ where necessary */
+       d = q = xmalloc(len+n+3);
+       *d++ = '"';
+       while (*arg) {
+               if (*arg == '"')
+                       *d++ = '\\';
+               else if (*arg == '\\') {
+                       int count = 0;
+                       while (*arg == '\\') {
+                               count++;
+                               *d++ = *arg++;
+                       }
+                       if (*arg == '"') {
+                               while (count-- > 0)
+                                       *d++ = '\\';
+                               *d++ = '\\';
+                       }
+               }
+               *d++ = *arg++;
+       }
+       *d++ = '"';
+       *d++ = 0;
+       return q;
+}
+
 static const char *parse_interpreter(const char *cmd)
 {
        static char buf[100];
@@ -307,6 +367,138 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
        return prog;
 }
 
+static int env_compare(const void *a, const void *b)
+{
+       char *const *ea = a;
+       char *const *eb = b;
+       return strcasecmp(*ea, *eb);
+}
+
+static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env,
+                          int prepend_cmd)
+{
+       STARTUPINFO si;
+       PROCESS_INFORMATION pi;
+       struct strbuf envblk, args;
+       unsigned flags;
+       BOOL ret;
+
+       /* Determine whether or not we are associated to a console */
+       HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
+                       FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                       FILE_ATTRIBUTE_NORMAL, NULL);
+       if (cons == INVALID_HANDLE_VALUE) {
+               /* There is no console associated with this process.
+                * Since the child is a console process, Windows
+                * would normally create a console window. But
+                * since we'll be redirecting std streams, we do
+                * not need the console.
+                */
+               flags = CREATE_NO_WINDOW;
+       } else {
+               /* There is already a console. If we specified
+                * CREATE_NO_WINDOW here, too, Windows would
+                * disassociate the child from the console.
+                * Go figure!
+                */
+               flags = 0;
+               CloseHandle(cons);
+       }
+       memset(&si, 0, sizeof(si));
+       si.cb = sizeof(si);
+       si.dwFlags = STARTF_USESTDHANDLES;
+       si.hStdInput = (HANDLE) _get_osfhandle(0);
+       si.hStdOutput = (HANDLE) _get_osfhandle(1);
+       si.hStdError = (HANDLE) _get_osfhandle(2);
+
+       /* concatenate argv, quoting args as we go */
+       strbuf_init(&args, 0);
+       if (prepend_cmd) {
+               char *quoted = (char *)quote_arg(cmd);
+               strbuf_addstr(&args, quoted);
+               if (quoted != cmd)
+                       free(quoted);
+       }
+       for (; *argv; argv++) {
+               char *quoted = (char *)quote_arg(*argv);
+               if (*args.buf)
+                       strbuf_addch(&args, ' ');
+               strbuf_addstr(&args, quoted);
+               if (quoted != *argv)
+                       free(quoted);
+       }
+
+       if (env) {
+               int count = 0;
+               char **e, **sorted_env;
+
+               for (e = env; *e; e++)
+                       count++;
+
+               /* environment must be sorted */
+               sorted_env = xmalloc(sizeof(*sorted_env) * (count + 1));
+               memcpy(sorted_env, env, sizeof(*sorted_env) * (count + 1));
+               qsort(sorted_env, count, sizeof(*sorted_env), env_compare);
+
+               strbuf_init(&envblk, 0);
+               for (e = sorted_env; *e; e++) {
+                       strbuf_addstr(&envblk, *e);
+                       strbuf_addch(&envblk, '\0');
+               }
+               free(sorted_env);
+       }
+
+       memset(&pi, 0, sizeof(pi));
+       ret = CreateProcess(cmd, args.buf, NULL, NULL, TRUE, flags,
+               env ? envblk.buf : NULL, NULL, &si, &pi);
+
+       if (env)
+               strbuf_release(&envblk);
+       strbuf_release(&args);
+
+       if (!ret) {
+               errno = ENOENT;
+               return -1;
+       }
+       CloseHandle(pi.hThread);
+       return (pid_t)pi.hProcess;
+}
+
+pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env)
+{
+       pid_t pid;
+       char **path = get_path_split();
+       char *prog = path_lookup(cmd, path, 0);
+
+       if (!prog) {
+               errno = ENOENT;
+               pid = -1;
+       }
+       else {
+               const char *interpr = parse_interpreter(prog);
+
+               if (interpr) {
+                       const char *argv0 = argv[0];
+                       char *iprog = path_lookup(interpr, path, 1);
+                       argv[0] = prog;
+                       if (!iprog) {
+                               errno = ENOENT;
+                               pid = -1;
+                       }
+                       else {
+                               pid = mingw_spawnve(iprog, argv, env, 1);
+                               free(iprog);
+                       }
+                       argv[0] = argv0;
+               }
+               else
+                       pid = mingw_spawnve(prog, argv, env, 0);
+               free(prog);
+       }
+       free_path_split(path);
+       return pid;
+}
+
 static int try_shell_exec(const char *cmd, char *const *argv, char **env)
 {
        const char *interpr = parse_interpreter(cmd);
@@ -322,11 +514,10 @@ static int try_shell_exec(const char *cmd, char *const *argv, char **env)
                int argc = 0;
                const char **argv2;
                while (argv[argc]) argc++;
-               argv2 = xmalloc(sizeof(*argv) * (argc+2));
-               argv2[0] = (char *)interpr;
-               argv2[1] = (char *)cmd; /* full path to the script file */
-               memcpy(&argv2[2], &argv[1], sizeof(*argv) * argc);
-               pid = spawnve(_P_NOWAIT, prog, argv2, (const char **)env);
+               argv2 = xmalloc(sizeof(*argv) * (argc+1));
+               argv2[0] = (char *)cmd; /* full path to the script file */
+               memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
+               pid = mingw_spawnve(prog, argv2, env, 1);
                if (pid >= 0) {
                        int status;
                        if (waitpid(pid, &status, 0) < 0)
@@ -347,7 +538,7 @@ static void mingw_execve(const char *cmd, char *const *argv, char *const *env)
        if (!try_shell_exec(cmd, argv, (char **)env)) {
                int pid, status;
 
-               pid = spawnve(_P_NOWAIT, cmd, (const char **)argv, (const char **)env);
+               pid = mingw_spawnve(cmd, (const char **)argv, (char **)env, 0);
                if (pid < 0)
                        return;
                if (waitpid(pid, &status, 0) < 0)