2011-05-12 10:24:54 -05:00
|
|
|
import _str::sbuf;
|
|
|
|
import _vec::vbuf;
|
2011-03-11 06:30:18 -06:00
|
|
|
|
|
|
|
native "rust" mod rustrt {
|
|
|
|
fn rust_run_program(vbuf argv, int in_fd, int out_fd, int err_fd) -> int;
|
|
|
|
}
|
|
|
|
|
2011-05-16 20:21:22 -05:00
|
|
|
fn arg_vec(str prog, vec[str] args) -> vec[sbuf] {
|
|
|
|
auto argptrs = [_str::buf(prog)];
|
2011-03-11 06:30:18 -06:00
|
|
|
for (str arg in args) {
|
2011-05-12 10:24:54 -05:00
|
|
|
_vec::push[sbuf](argptrs, _str::buf(arg));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
_vec::push[sbuf](argptrs, 0 as sbuf);
|
2011-03-11 06:30:18 -06:00
|
|
|
ret argptrs;
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn run_program(str prog, vec[str] args) -> int {
|
2011-05-16 20:21:22 -05:00
|
|
|
auto pid = rustrt::rust_run_program(_vec::buf[sbuf](arg_vec(prog, args)),
|
2011-03-11 06:30:18 -06:00
|
|
|
0, 0, 0);
|
2011-05-12 10:24:54 -05:00
|
|
|
ret os::waitpid(pid);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type program =
|
|
|
|
state obj {
|
|
|
|
fn get_id() -> int;
|
2011-05-12 10:24:54 -05:00
|
|
|
fn input() -> io::writer;
|
|
|
|
fn output() -> io::reader;
|
2011-04-19 15:35:49 -05:00
|
|
|
fn close_input();
|
|
|
|
fn finish() -> int;
|
2011-03-11 06:30:18 -06:00
|
|
|
};
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn start_program(str prog, vec[str] args) -> @program {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto pipe_input = os::pipe();
|
|
|
|
auto pipe_output = os::pipe();
|
|
|
|
auto pid = rustrt::rust_run_program
|
2011-05-16 20:21:22 -05:00
|
|
|
(_vec::buf[sbuf](arg_vec(prog, args)),
|
2011-03-11 06:30:18 -06:00
|
|
|
pipe_input._0, pipe_output._1, 0);
|
|
|
|
if (pid == -1) {fail;}
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::close(pipe_input._0);
|
|
|
|
os::libc::close(pipe_output._1);
|
2011-03-11 06:30:18 -06:00
|
|
|
|
|
|
|
state obj new_program(int pid,
|
|
|
|
int in_fd,
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::FILE out_file,
|
2011-03-11 06:30:18 -06:00
|
|
|
mutable bool finished) {
|
|
|
|
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 {
|
|
|
|
ret io::new_reader(io::FILE_buf_reader(out_file, false));
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-04-19 15:35:49 -05:00
|
|
|
fn close_input() {
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::close(in_fd);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-04-19 15:35:49 -05:00
|
|
|
fn finish() -> int {
|
2011-03-11 06:30:18 -06:00
|
|
|
if (finished) {ret 0;}
|
|
|
|
finished = true;
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::close(in_fd);
|
|
|
|
ret os::waitpid(pid);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
drop {
|
|
|
|
if (!finished) {
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::close(in_fd);
|
|
|
|
os::waitpid(pid);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
os::libc::fclose(out_file);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret @new_program(pid, pipe_input._1,
|
2011-05-12 10:24:54 -05:00
|
|
|
os::fd_FILE(pipe_output._0),
|
2011-03-11 06:30:18 -06:00
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn program_output(str prog, vec[str] args)
|
2011-03-11 06:30:18 -06:00
|
|
|
-> rec(int status, str out) {
|
|
|
|
auto pr = start_program(prog, args);
|
|
|
|
pr.close_input();
|
|
|
|
auto out = pr.output();
|
|
|
|
auto buf = "";
|
|
|
|
while (!out.eof()) {
|
|
|
|
auto bytes = out.read_bytes(4096u);
|
2011-05-12 10:24:54 -05:00
|
|
|
buf += _str::unsafe_from_bytes(bytes);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
ret rec(status=pr.finish(), out=buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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:
|