1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::io::process::{ProcessExit, Command, Process, ProcessOutput};
12 use std::dynamic_lib::DynamicLibrary;
14 fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
15 // Need to be sure to put both the lib_path and the aux path in the dylib
16 // search path for the child.
17 let mut path = DynamicLibrary::search_path();
19 Some(p) => path.insert(0, Path::new(p)),
22 path.insert(0, Path::new(lib_path));
24 // Add the new dylib search path var
25 let var = DynamicLibrary::envvar();
26 let newpath = DynamicLibrary::create_path(path.as_slice());
27 let newpath = String::from_utf8(newpath).unwrap();
28 cmd.env(var.to_string(), newpath);
31 pub struct Result {pub status: ProcessExit, pub out: String, pub err: String}
33 pub fn run(lib_path: &str,
35 aux_path: Option<&str>,
37 env: Vec<(String, String)> ,
38 input: Option<String>) -> Option<Result> {
40 let mut cmd = Command::new(prog);
42 add_target_env(&mut cmd, lib_path, aux_path);
43 for (key, val) in env.move_iter() {
49 for input in input.iter() {
50 process.stdin.as_mut().unwrap().write(input.as_bytes()).unwrap();
52 let ProcessOutput { status, output, error } =
53 process.wait_with_output().unwrap();
57 out: String::from_utf8(output).unwrap(),
58 err: String::from_utf8(error).unwrap()
65 pub fn run_background(lib_path: &str,
67 aux_path: Option<&str>,
69 env: Vec<(String, String)> ,
70 input: Option<String>) -> Option<Process> {
72 let mut cmd = Command::new(prog);
74 add_target_env(&mut cmd, lib_path, aux_path);
75 for (key, val) in env.move_iter() {
81 for input in input.iter() {
82 process.stdin.as_mut().unwrap().write(input.as_bytes()).unwrap();