82 lines
1.9 KiB
Rust
82 lines
1.9 KiB
Rust
import std::run;
|
|
import std::run::spawn_process;
|
|
import std::io;
|
|
import std::os;
|
|
import io::writer_util;
|
|
import ctypes::{pid_t, fd_t};
|
|
|
|
export run;
|
|
|
|
#[cfg(target_os = "win32")]
|
|
fn target_env(lib_path: str, prog: str) -> option<[(str,str)]> {
|
|
|
|
let env = std::generic_os::env();
|
|
|
|
env = vec::map(env) {|pair|
|
|
let (k,v) = pair;
|
|
if k == "PATH" { ("PATH", v + ";" + lib_path) }
|
|
else { (k,v) }
|
|
};
|
|
if str::ends_with(prog, "rustc.exe") {
|
|
env += [("RUST_THREADS", "1")]
|
|
}
|
|
ret some(env);
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
#[cfg(target_os = "macos")]
|
|
#[cfg(target_os = "freebsd")]
|
|
fn target_env(lib_path: str, prog: str) -> option<[(str,str)]> {
|
|
none
|
|
}
|
|
|
|
|
|
fn run(lib_path: str, prog: str, args: [str],
|
|
input: option<str>) -> {status: int, out: str, err: str} {
|
|
|
|
let pipe_in = os::pipe();
|
|
let pipe_out = os::pipe();
|
|
let pipe_err = os::pipe();
|
|
let pid = spawn_process(prog, args, target_env(lib_path, prog), none,
|
|
pipe_in.in, pipe_out.out, pipe_err.out);
|
|
|
|
os::close(pipe_in.in);
|
|
os::close(pipe_out.out);
|
|
os::close(pipe_err.out);
|
|
if pid == -1i32 {
|
|
os::close(pipe_in.out);
|
|
os::close(pipe_out.in);
|
|
os::close(pipe_err.in);
|
|
fail;
|
|
}
|
|
|
|
|
|
writeclose(pipe_in.out, input);
|
|
let output = readclose(pipe_out.in);
|
|
let errput = readclose(pipe_err.in);
|
|
let status = run::waitpid(pid);
|
|
ret {status: status, out: output, err: errput};
|
|
}
|
|
|
|
fn writeclose(fd: fd_t, s: option<str>) {
|
|
if option::is_some(s) {
|
|
let writer = io::fd_writer(fd, false);
|
|
writer.write_str(option::get(s));
|
|
}
|
|
|
|
os::close(fd);
|
|
}
|
|
|
|
fn readclose(fd: fd_t) -> str {
|
|
// Copied from run::program_output
|
|
let file = os::fd_FILE(fd);
|
|
let reader = io::FILE_reader(file, false);
|
|
let buf = "";
|
|
while !reader.eof() {
|
|
let bytes = reader.read_bytes(4096u);
|
|
buf += str::from_bytes(bytes);
|
|
}
|
|
os::fclose(file);
|
|
ret buf;
|
|
}
|