2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
import str::sbuf;
|
|
|
|
import vec::vbuf;
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2011-07-18 23:07:33 -05:00
|
|
|
export program;
|
|
|
|
export run_program;
|
|
|
|
export start_program;
|
|
|
|
export program_output;
|
2011-07-25 23:25:32 -05:00
|
|
|
export spawn_process;
|
2011-07-18 23:07:33 -05:00
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
native "rust" mod rustrt {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn rust_run_program(argv: vbuf, in_fd: int, out_fd: int, err_fd: int) ->
|
|
|
|
int;
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn arg_vec(prog: str, args: vec[str]) -> vec[sbuf] {
|
|
|
|
let argptrs = [str::buf(prog)];
|
|
|
|
for arg: str in args { vec::push[sbuf](argptrs, str::buf(arg)); }
|
2011-05-17 13:41:41 -05:00
|
|
|
vec::push[sbuf](argptrs, 0 as sbuf);
|
2011-03-11 06:30:18 -06:00
|
|
|
ret argptrs;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn spawn_process(prog: str, args: vec[str], in_fd: int, out_fd: int,
|
|
|
|
err_fd: int) -> int {
|
2011-07-18 23:03:28 -05:00
|
|
|
// Note: we have to hold on to this vector reference while we hold a
|
|
|
|
// pointer to its buffer
|
2011-07-27 07:19:39 -05:00
|
|
|
let argv = arg_vec(prog, args);
|
|
|
|
let pid = rustrt::rust_run_program(vec::buf(argv), in_fd, out_fd, err_fd);
|
2011-07-18 23:07:33 -05:00
|
|
|
ret pid;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn run_program(prog: str, args: vec[str]) -> int {
|
2011-07-18 23:07:33 -05:00
|
|
|
ret os::waitpid(spawn_process(prog, args, 0, 0, 0));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type program =
|
2011-06-15 13:19:50 -05:00
|
|
|
obj {
|
|
|
|
fn get_id() -> int ;
|
|
|
|
fn input() -> io::writer ;
|
|
|
|
fn output() -> io::reader ;
|
2011-07-28 23:15:56 -05:00
|
|
|
fn err() -> io::reader ;
|
2011-06-15 13:19:50 -05:00
|
|
|
fn close_input() ;
|
|
|
|
fn finish() -> int ;
|
2011-03-11 06:30:18 -06:00
|
|
|
};
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn start_program(prog: str, args: vec[str]) -> @program {
|
|
|
|
let pipe_input = os::pipe();
|
|
|
|
let pipe_output = os::pipe();
|
2011-07-28 23:15:56 -05:00
|
|
|
let pipe_err = os::pipe();
|
|
|
|
let pid = spawn_process(prog, args, pipe_input.in, pipe_output.out,
|
|
|
|
pipe_err.out);
|
2011-07-18 23:07:33 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
if pid == -1 { fail; }
|
2011-07-26 07:06:02 -05:00
|
|
|
os::libc::close(pipe_input.in);
|
|
|
|
os::libc::close(pipe_output.out);
|
2011-07-28 23:15:56 -05:00
|
|
|
os::libc::close(pipe_err.out);
|
2011-07-27 07:19:39 -05:00
|
|
|
obj new_program(pid: int,
|
|
|
|
mutable in_fd: int,
|
|
|
|
out_file: os::libc::FILE,
|
2011-07-28 23:15:56 -05:00
|
|
|
err_file: os::libc::FILE,
|
2011-07-27 07:19:39 -05:00
|
|
|
mutable finished: bool) {
|
2011-06-15 13:19:50 -05:00
|
|
|
fn get_id() -> int { ret pid; }
|
2011-05-12 10:24:54 -05:00
|
|
|
fn input() -> io::writer {
|
|
|
|
ret io::new_writer(io::fd_buf_writer(in_fd, false));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
fn output() -> io::reader {
|
2011-07-29 03:55:03 -05:00
|
|
|
ret io::new_reader(io::FILE_buf_reader(out_file, option::none));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-07-28 23:15:56 -05:00
|
|
|
fn err() -> io::reader {
|
2011-07-29 03:55:03 -05:00
|
|
|
ret io::new_reader(io::FILE_buf_reader(err_file, option::none));
|
2011-07-28 23:15:56 -05:00
|
|
|
}
|
2011-07-19 16:00:15 -05:00
|
|
|
fn close_input() {
|
2011-07-27 07:19:39 -05:00
|
|
|
let invalid_fd = -1;
|
|
|
|
if in_fd != invalid_fd {
|
2011-07-19 16:00:15 -05:00
|
|
|
os::libc::close(in_fd);
|
|
|
|
in_fd = invalid_fd;
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 15:35:49 -05:00
|
|
|
fn finish() -> int {
|
2011-07-27 07:19:39 -05:00
|
|
|
if finished { ret 0; }
|
2011-03-11 06:30:18 -06:00
|
|
|
finished = true;
|
2011-07-19 16:00:15 -05:00
|
|
|
self.close_input();
|
2011-05-12 10:24:54 -05:00
|
|
|
ret os::waitpid(pid);
|
2011-07-27 09:52:49 -05:00
|
|
|
}
|
|
|
|
drop {
|
|
|
|
self.close_input();
|
|
|
|
if !finished { os::waitpid(pid); }
|
|
|
|
os::libc::fclose(out_file);
|
2011-07-28 23:15:56 -05:00
|
|
|
os::libc::fclose(err_file);
|
2011-07-27 09:52:49 -05:00
|
|
|
}
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-07-28 23:15:56 -05:00
|
|
|
ret @new_program(pid,
|
|
|
|
pipe_input.out,
|
|
|
|
os::fd_FILE(pipe_output.in),
|
|
|
|
os::fd_FILE(pipe_err.in),
|
|
|
|
false);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
2011-07-28 23:15:56 -05:00
|
|
|
fn read_all(rd: &io::reader) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let buf = "";
|
2011-07-28 23:15:56 -05:00
|
|
|
while !rd.eof() {
|
|
|
|
let bytes = rd.read_bytes(4096u);
|
2011-05-17 13:41:41 -05:00
|
|
|
buf += str::unsafe_from_bytes(bytes);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-07-28 23:15:56 -05:00
|
|
|
ret buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn program_output(prog: str, args: vec[str])
|
|
|
|
-> {status: int, out: str, err: str} {
|
|
|
|
let pr = start_program(prog, args);
|
|
|
|
pr.close_input();
|
|
|
|
ret {status: pr.finish(),
|
|
|
|
out: read_all(pr.output()),
|
|
|
|
err: read_all(pr.err())};
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-05-16 20:21:22 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-11 06:30:18 -06:00
|
|
|
// End:
|