/* Start a subprocess with run_command_redirected and register it with the termination handler. Takes care of o.shellexec. Returns the PID of the subprocess or -1 on error. */ static int start_subprocess(char *cmdexec, struct subprocess_info *info) { char *cmdbuf; int pid; if (o.shellexec) { /* Run with cmd.exe. */ const char *shell; size_t cmdlen; shell = get_shell(); cmdlen = strlen(shell) + strlen(cmdexec) + 32; cmdbuf = (char *) safe_malloc(cmdlen); Snprintf(cmdbuf, cmdlen, "%s /C %s", shell, cmdexec); } else { cmdbuf = cmdexec; } if (o.debug) logdebug("Executing: %s\n", cmdbuf); pid = run_command_redirected(cmdbuf, info); if (cmdbuf != cmdexec) free(cmdbuf); if (pid == -1) return -1; if (register_subprocess(info->proc) == -1) { if (o.verbose) logdebug("Couldn't register subprocess with termination handler; not executing.\n"); TerminateProcess(info->proc, 2); subprocess_info_close(info); return -1; } return pid; }
/* Start a subprocess with run_command_redirected and register it with the termination handler. Takes care of o.shellexec. Returns the PID of the subprocess or -1 on error. */ static int start_subprocess(char *cmdexec, struct subprocess_info *info) { char *cmdbuf; int pid; if (o.execmode == EXEC_SHELL) { /* Run with cmd.exe. */ const char *shell; size_t cmdlen; shell = get_shell(); cmdlen = strlen(shell) + strlen(cmdexec) + 32; cmdbuf = (char *) safe_malloc(cmdlen); Snprintf(cmdbuf, cmdlen, "%s /C %s", shell, cmdexec); #ifdef HAVE_LUA } else if (o.execmode == EXEC_LUA) { char exepath[8192]; char *cmdexec_escaped, *exepath_escaped; int n; n = GetModuleFileName(GetModuleHandle(0), exepath, sizeof(exepath)); if (n == 0 || n == sizeof(exepath)) return -1; cmdexec_escaped = escape_windows_command_arg(cmdexec); if (cmdexec_escaped == NULL) return -1; exepath_escaped = escape_windows_command_arg(exepath); if (exepath_escaped == NULL) { free(cmdexec_escaped); return -1; } n = asprintf(&cmdbuf, "%s --lua-exec-internal %s", exepath_escaped, cmdexec_escaped); free(cmdexec_escaped); free(exepath_escaped); if (n < 0) return -1; #endif } else { cmdbuf = cmdexec; } if (o.debug) logdebug("Executing: %s\n", cmdbuf); pid = run_command_redirected(cmdbuf, info); if (cmdbuf != cmdexec) free(cmdbuf); if (pid == -1) return -1; if (register_subprocess(info->proc) == -1) { if (o.verbose) logdebug("Couldn't register subprocess with termination handler; not executing.\n"); TerminateProcess(info->proc, 2); subprocess_info_close(info); return -1; } return pid; }