aboutsummaryrefslogtreecommitdiff
path: root/src/rt
diff options
context:
space:
mode:
authorMarijn Haverbeke <[email protected]>2011-03-11 13:30:18 +0100
committerGraydon Hoare <[email protected]>2011-03-14 14:57:13 -0700
commitea5dc54c3f0444fd3f20191fa1b1d94372c74c65 (patch)
tree0a80b98563553f56962cc956e94210462a9d66e2 /src/rt
parentExtend stream functionality (diff)
downloadrust-ea5dc54c3f0444fd3f20191fa1b1d94372c74c65.tar.xz
rust-ea5dc54c3f0444fd3f20191fa1b1d94372c74c65.zip
Add functionality for running external programs to the std lib
See lib/run_program.rs.
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/rust_builtin.cpp3
-rw-r--r--src/rt/rust_internal.h9
-rw-r--r--src/rt/rust_run_program.cpp92
3 files changed, 97 insertions, 7 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 40976214..fcea449d 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -1,7 +1,4 @@
-#include <dirent.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include "rust_internal.h"
/* Native builtins. */
diff --git a/src/rt/rust_internal.h b/src/rt/rust_internal.h
index 42b61801..5bd62d2b 100644
--- a/src/rt/rust_internal.h
+++ b/src/rt/rust_internal.h
@@ -11,9 +11,12 @@
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
-
+#include <sys/types.h>
+#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
+#include <fcntl.h>
+
#include "rust.h"
#include "rand.h"
#include "uthash.h"
@@ -26,12 +29,10 @@ extern "C" {
}
#elif defined(__GNUC__)
#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
#include <dlfcn.h>
#include <pthread.h>
#include <errno.h>
+#include <dirent.h>
#else
#error "Platform not supported."
#endif
diff --git a/src/rt/rust_run_program.cpp b/src/rt/rust_run_program.cpp
new file mode 100644
index 00000000..5919f68e
--- /dev/null
+++ b/src/rt/rust_run_program.cpp
@@ -0,0 +1,92 @@
+#include "rust_internal.h"
+
+#if defined(__WIN32__)
+
+#include <process.h>
+#include <io.h>
+
+extern "C" CDECL int
+rust_run_program(void* task, const char* argv[],
+ int in_fd, int out_fd, int err_fd) {
+ STARTUPINFO si;
+ ZeroMemory(&si, sizeof(STARTUPINFO));
+ si.cb = sizeof(STARTUPINFO);
+ si.dwFlags = STARTF_USESTDHANDLES;
+ si.hStdInput = (HANDLE)_get_osfhandle(in_fd ? in_fd : 0);
+ si.hStdOutput = (HANDLE)_get_osfhandle(out_fd ? out_fd : 1);
+ si.hStdError = (HANDLE)_get_osfhandle(err_fd ? err_fd : 2);
+
+ size_t cmd_len = 0;
+ for (const char** arg = argv; *arg; arg++) {
+ cmd_len += strlen(*arg);
+ cmd_len++; // Space or \0
+ }
+ char* cmd = (char*)malloc(cmd_len);
+ char* pos = cmd;
+ for (const char** arg = argv; *arg; arg++) {
+ strcpy(pos, *arg);
+ pos += strlen(*arg);
+ if (*(arg+1)) *(pos++) = ' ';
+ }
+
+ PROCESS_INFORMATION pi;
+ BOOL created = CreateProcess(NULL, cmd, NULL, NULL, TRUE,
+ 0, NULL, NULL, &si, &pi);
+ free(cmd);
+
+ if (!created) return -1;
+ return (int)pi.hProcess;
+}
+
+extern "C" CDECL int
+rust_process_wait(void* task, int proc) {
+ DWORD status;
+ while (true) {
+ if (GetExitCodeProcess((HANDLE)proc, &status) &&
+ status != STILL_ACTIVE)
+ return (int)status;
+ WaitForSingleObject((HANDLE)proc, INFINITE);
+ }
+}
+
+#elif defined(__GNUC__)
+
+#include <sys/file.h>
+#include <signal.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <termios.h>
+
+extern "C" CDECL int
+rust_run_program(rust_task* task, char* argv[],
+ int in_fd, int out_fd, int err_fd) {
+ int pid = fork();
+ if (pid != 0) return pid;
+
+ sigset_t sset;
+ sigemptyset(&sset);
+ sigprocmask(SIG_SETMASK, &sset, NULL);
+
+ if (in_fd) dup2(in_fd, 0);
+ if (out_fd) dup2(out_fd, 1);
+ if (err_fd) dup2(err_fd, 2);
+ /* Close all other fds. */
+ for (int fd = getdtablesize() - 1; fd >= 3; fd--) close(fd);
+ execvp(argv[0], argv);
+ exit(1);
+}
+
+#else
+#error "Platform not supported."
+#endif
+
+//
+// Local Variables:
+// mode: C++
+// fill-column: 78;
+// indent-tabs-mode: nil
+// c-basic-offset: 4
+// buffer-file-coding-system: utf-8-unix
+// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
+// End:
+//