2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-06-30 21:36:55 -05:00
|
|
|
use std::os;
|
|
|
|
use std::str;
|
2014-05-05 16:33:55 -05:00
|
|
|
use std::io::process::{ProcessExit, Command, Process, ProcessOutput};
|
2014-05-15 12:28:46 -05:00
|
|
|
use std::unstable::dynamic_lib::DynamicLibrary;
|
2012-08-03 20:57:43 -05:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn target_env(lib_path: &str, prog: &str) -> Vec<(String, String)> {
|
2014-05-15 12:28:46 -05:00
|
|
|
let prog = if cfg!(windows) {prog.slice_to(prog.len() - 4)} else {prog};
|
2014-05-25 05:17:19 -05:00
|
|
|
let mut aux_path = prog.to_string();
|
2014-05-20 01:19:56 -05:00
|
|
|
aux_path.push_str(".libaux");
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2014-05-15 12:28:46 -05:00
|
|
|
// Need to be sure to put both the lib_path and the aux path in the dylib
|
|
|
|
// search path for the child.
|
|
|
|
let mut path = DynamicLibrary::search_path();
|
|
|
|
path.insert(0, Path::new(aux_path));
|
|
|
|
path.insert(0, Path::new(lib_path));
|
2012-06-01 20:24:55 -05:00
|
|
|
|
2014-05-15 12:28:46 -05:00
|
|
|
// Remove the previous dylib search path var
|
|
|
|
let var = DynamicLibrary::envvar();
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut env: Vec<(String,String)> =
|
2014-05-25 05:17:19 -05:00
|
|
|
os::env().move_iter().map(|(a,b)|(a.to_string(), b.to_string())).collect();
|
2014-05-15 12:28:46 -05:00
|
|
|
match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
|
|
|
|
Some(i) => { env.remove(i); }
|
|
|
|
None => {}
|
2012-02-07 20:55:02 -06:00
|
|
|
}
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2014-05-15 12:28:46 -05:00
|
|
|
// Add the new dylib search path var
|
|
|
|
let newpath = DynamicLibrary::create_path(path.as_slice());
|
2014-05-25 05:17:19 -05:00
|
|
|
env.push((var.to_string(),
|
|
|
|
str::from_utf8(newpath.as_slice()).unwrap().to_string()));
|
2014-02-21 18:32:49 -06:00
|
|
|
return env;
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub struct Result {pub status: ProcessExit, pub out: String, pub err: String}
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2013-05-11 21:45:28 -05:00
|
|
|
pub fn run(lib_path: &str,
|
|
|
|
prog: &str,
|
2014-05-22 18:57:53 -05:00
|
|
|
args: &[String],
|
|
|
|
env: Vec<(String, String)> ,
|
|
|
|
input: Option<String>) -> Option<Result> {
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2014-03-30 22:53:26 -05:00
|
|
|
let env = env.clone().append(target_env(lib_path, prog).as_slice());
|
2014-05-05 16:33:55 -05:00
|
|
|
match Command::new(prog).args(args).env(env.as_slice()).spawn() {
|
2014-05-05 18:58:42 -05:00
|
|
|
Ok(mut process) => {
|
2013-12-12 08:07:43 -06:00
|
|
|
for input in input.iter() {
|
2014-02-18 14:04:51 -06:00
|
|
|
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2014-05-05 18:58:42 -05:00
|
|
|
let ProcessOutput { status, output, error } =
|
|
|
|
process.wait_with_output().unwrap();
|
2011-07-30 23:11:14 -05:00
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
Some(Result {
|
|
|
|
status: status,
|
2014-05-25 05:17:19 -05:00
|
|
|
out: str::from_utf8(output.as_slice()).unwrap().to_string(),
|
|
|
|
err: str::from_utf8(error.as_slice()).unwrap().to_string()
|
2013-12-12 08:07:43 -06:00
|
|
|
})
|
|
|
|
},
|
2014-01-30 16:37:10 -06:00
|
|
|
Err(..) => None
|
2011-07-31 17:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
|
|
|
|
pub fn run_background(lib_path: &str,
|
|
|
|
prog: &str,
|
2014-05-22 18:57:53 -05:00
|
|
|
args: &[String],
|
|
|
|
env: Vec<(String, String)> ,
|
|
|
|
input: Option<String>) -> Option<Process> {
|
2013-11-04 00:53:01 -06:00
|
|
|
|
2014-03-30 22:53:26 -05:00
|
|
|
let env = env.clone().append(target_env(lib_path, prog).as_slice());
|
2014-05-05 16:33:55 -05:00
|
|
|
match Command::new(prog).args(args).env(env.as_slice()).spawn() {
|
2014-01-30 16:37:10 -06:00
|
|
|
Ok(mut process) => {
|
2013-12-12 08:07:43 -06:00
|
|
|
for input in input.iter() {
|
2014-02-18 14:04:51 -06:00
|
|
|
process.stdin.get_mut_ref().write(input.as_bytes()).unwrap();
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
|
2013-12-12 08:07:43 -06:00
|
|
|
Some(process)
|
|
|
|
},
|
2014-01-30 16:37:10 -06:00
|
|
|
Err(..) => None
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2013-11-04 00:53:01 -06:00
|
|
|
}
|