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.
13 use std::io::process::{ProcessExit, Command, Process, ProcessOutput};
14 use std::dynamic_lib::DynamicLibrary;
16 fn target_env(lib_path: &str, aux_path: Option<&str>) -> Vec<(String, String)> {
17 // Need to be sure to put both the lib_path and the aux path in the dylib
18 // search path for the child.
19 let mut path = DynamicLibrary::search_path();
21 Some(p) => path.insert(0, Path::new(p)),
24 path.insert(0, Path::new(lib_path));
26 // Remove the previous dylib search path var
27 let var = DynamicLibrary::envvar();
28 let mut env: Vec<(String,String)> = os::env();
29 match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
30 Some(i) => { env.remove(i); }
34 // Add the new dylib search path var
35 let newpath = DynamicLibrary::create_path(path.as_slice());
36 let newpath = str::from_utf8(newpath.as_slice()).unwrap().to_string();
37 env.push((var.to_string(), newpath));
41 pub struct Result {pub status: ProcessExit, pub out: String, pub err: String}
43 pub fn run(lib_path: &str,
45 aux_path: Option<&str>,
47 env: Vec<(String, String)> ,
48 input: Option<String>) -> Option<Result> {
50 let env = env.clone().append(target_env(lib_path, aux_path).as_slice());
51 match Command::new(prog).args(args).env(env.as_slice()).spawn() {
53 for input in input.iter() {
54 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
56 let ProcessOutput { status, output, error } =
57 process.wait_with_output().unwrap();
61 out: str::from_utf8(output.as_slice()).unwrap().to_string(),
62 err: str::from_utf8(error.as_slice()).unwrap().to_string()
69 pub fn run_background(lib_path: &str,
71 aux_path: Option<&str>,
73 env: Vec<(String, String)> ,
74 input: Option<String>) -> Option<Process> {
76 let env = env.clone().append(target_env(lib_path, aux_path).as_slice());
77 match Command::new(prog).args(args).env(env.as_slice()).spawn() {
79 for input in input.iter() {
80 process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();