PageRenderTime 112ms CodeModel.GetById 96ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/src/rt/rust_run_program.cpp

http://github.com/jruderman/rust
C++ | 198 lines | 190 code | 5 blank | 3 comment | 5 complexity | 7c4076639d90e2174bded654ddc77452 MD5 | raw file
  1
  2#include "rust_kernel.h"
  3
  4#ifdef __APPLE__
  5#include <crt_externs.h>
  6#endif
  7
  8#if defined(__WIN32__)
  9
 10#include <process.h>
 11#include <io.h>
 12
 13bool backslash_run_ends_in_quote(char const *c) {
 14    while (*c == '\\') ++c;
 15    return *c == '"';
 16}
 17
 18void append_first_char(char *&buf, char const *c) {
 19    switch (*c) {
 20
 21    case '"':
 22        // Escape quotes.
 23        *buf++ = '\\';
 24        *buf++ = '"';
 25        break;
 26
 27
 28    case '\\':
 29        if (backslash_run_ends_in_quote(c)) {
 30            // Double all backslashes that are in runs before quotes.
 31            *buf++ = '\\';
 32            *buf++ = '\\';
 33        } else {
 34            // Pass other backslashes through unescaped.
 35            *buf++ = '\\';
 36        }
 37        break;
 38
 39    default:
 40        *buf++ = *c;
 41    }
 42}
 43
 44bool contains_whitespace(char const *arg) {
 45    while (*arg) {
 46        switch (*arg++) {
 47        case ' ':
 48        case '\t':
 49            return true;
 50        }
 51    }
 52    return false;
 53}
 54
 55void append_arg(char *& buf, char const *arg, bool last) {
 56    bool quote = contains_whitespace(arg);
 57    if (quote)
 58        *buf++ = '"';
 59    while (*arg)
 60        append_first_char(buf, arg++);
 61    if (quote)
 62        *buf++ = '"';
 63
 64    if (! last) {
 65        *buf++ = ' ';
 66    } else {
 67        *buf++ = '\0';
 68    }
 69}
 70
 71extern "C" CDECL int
 72rust_run_program(const char* argv[],
 73                 void* envp,
 74                 const char* dir,
 75                 int in_fd, int out_fd, int err_fd) {
 76    STARTUPINFO si;
 77    ZeroMemory(&si, sizeof(STARTUPINFO));
 78    si.cb = sizeof(STARTUPINFO);
 79    si.dwFlags = STARTF_USESTDHANDLES;
 80
 81    HANDLE curproc = GetCurrentProcess();
 82    HANDLE origStdin = (HANDLE)_get_osfhandle(in_fd ? in_fd : 0);
 83    if (!DuplicateHandle(curproc, origStdin,
 84        curproc, &si.hStdInput, 0, 1, DUPLICATE_SAME_ACCESS))
 85        return -1;
 86    HANDLE origStdout = (HANDLE)_get_osfhandle(out_fd ? out_fd : 1);
 87    if (!DuplicateHandle(curproc, origStdout,
 88        curproc, &si.hStdOutput, 0, 1, DUPLICATE_SAME_ACCESS))
 89        return -1;
 90    HANDLE origStderr = (HANDLE)_get_osfhandle(err_fd ? err_fd : 2);
 91    if (!DuplicateHandle(curproc, origStderr,
 92        curproc, &si.hStdError, 0, 1, DUPLICATE_SAME_ACCESS))
 93        return -1;
 94
 95    size_t cmd_len = 0;
 96    for (const char** arg = argv; *arg; arg++) {
 97        cmd_len += strlen(*arg);
 98        cmd_len += 3; // Two quotes plus trailing space or \0
 99    }
100    cmd_len *= 2; // Potentially backslash-escape everything.
101
102    char* cmd = (char*)malloc(cmd_len);
103    char* pos = cmd;
104    for (const char** arg = argv; *arg; arg++) {
105        append_arg(pos, *arg, *(arg+1) == NULL);
106    }
107
108    PROCESS_INFORMATION pi;
109    BOOL created = CreateProcess(NULL, cmd, NULL, NULL, TRUE,
110                                 0, envp, dir, &si, &pi);
111
112    CloseHandle(si.hStdInput);
113    CloseHandle(si.hStdOutput);
114    CloseHandle(si.hStdError);
115    free(cmd);
116
117    if (!created) return -1;
118    return (int)pi.hProcess;
119}
120
121extern "C" CDECL int
122rust_process_wait(int proc) {
123    DWORD status;
124    while (true) {
125        if (GetExitCodeProcess((HANDLE)proc, &status) &&
126            status != STILL_ACTIVE)
127            return (int)status;
128        WaitForSingleObject((HANDLE)proc, INFINITE);
129    }
130}
131
132#elif defined(__GNUC__)
133
134#include <sys/file.h>
135#include <signal.h>
136#include <sys/ioctl.h>
137#include <unistd.h>
138#include <termios.h>
139
140#ifdef __FreeBSD__
141extern char **environ;
142#endif
143
144extern "C" CDECL int
145rust_run_program(const char* argv[],
146                 void* envp,
147                 const char* dir,
148                 int in_fd, int out_fd, int err_fd) {
149    int pid = fork();
150    if (pid != 0) return pid;
151
152    sigset_t sset;
153    sigemptyset(&sset);
154    sigprocmask(SIG_SETMASK, &sset, NULL);
155
156    if (in_fd) dup2(in_fd, 0);
157    if (out_fd) dup2(out_fd, 1);
158    if (err_fd) dup2(err_fd, 2);
159    /* Close all other fds. */
160    for (int fd = getdtablesize() - 1; fd >= 3; fd--) close(fd);
161    if (dir) {
162        int result = chdir(dir);
163        // FIXME (#2674): need error handling
164        assert(!result && "chdir failed");
165    }
166
167    if (envp) {
168#ifdef __APPLE__
169        *_NSGetEnviron() = (char **)envp;
170#else
171        environ = (char **)envp;
172#endif
173    }
174
175    execvp(argv[0], (char * const *)argv);
176    exit(1);
177}
178
179extern "C" CDECL int
180rust_process_wait(int proc) {
181    // FIXME: stub; exists to placate linker. (#2692)
182    return 0;
183}
184
185#else
186#error "Platform not supported."
187#endif
188
189//
190// Local Variables:
191// mode: C++
192// fill-column: 78;
193// indent-tabs-mode: nil
194// c-basic-offset: 4
195// buffer-file-coding-system: utf-8-unix
196// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
197// End:
198//