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.
|
|
|
|
|
2014-06-08 22:12:10 -05:00
|
|
|
use std::dynamic_lib::DynamicLibrary;
|
2015-03-17 15:33:26 -05:00
|
|
|
use std::io::prelude::*;
|
2015-03-27 15:22:26 -05:00
|
|
|
use std::path::PathBuf;
|
2015-03-17 15:33:26 -05:00
|
|
|
use std::process::{ExitStatus, Command, Child, Output, Stdio};
|
2012-08-03 20:57:43 -05:00
|
|
|
|
2014-07-02 15:50:45 -05:00
|
|
|
fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
|
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();
|
2014-06-11 16:52:38 -05:00
|
|
|
match aux_path {
|
2015-03-27 15:22:26 -05:00
|
|
|
Some(p) => path.insert(0, PathBuf::from(p)),
|
2014-06-11 16:52:38 -05:00
|
|
|
None => {}
|
|
|
|
}
|
2015-03-27 15:22:26 -05:00
|
|
|
path.insert(0, PathBuf::from(lib_path));
|
2012-06-01 20:24:55 -05:00
|
|
|
|
2014-05-15 12:28:46 -05:00
|
|
|
// Add the new dylib search path var
|
2014-07-02 15:50:45 -05:00
|
|
|
let var = DynamicLibrary::envvar();
|
2015-02-01 20:53:25 -06:00
|
|
|
let newpath = DynamicLibrary::create_path(&path);
|
2015-03-27 15:22:26 -05:00
|
|
|
let newpath = newpath.to_str().unwrap().to_string();
|
2015-02-26 23:00:43 -06:00
|
|
|
cmd.env(var, &newpath);
|
2011-07-30 23:11:14 -05:00
|
|
|
}
|
|
|
|
|
2015-02-26 23:00:43 -06:00
|
|
|
pub struct Result {pub status: ExitStatus, 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-06-11 16:52:38 -05:00
|
|
|
aux_path: Option<&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-07-02 15:50:45 -05:00
|
|
|
let mut cmd = Command::new(prog);
|
2015-02-26 23:00:43 -06:00
|
|
|
cmd.args(args)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped());
|
2014-07-02 15:50:45 -05:00
|
|
|
add_target_env(&mut cmd, lib_path, aux_path);
|
2015-01-31 19:03:04 -06:00
|
|
|
for (key, val) in env {
|
2015-02-26 23:00:43 -06:00
|
|
|
cmd.env(&key, &val);
|
2014-07-02 15:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
match cmd.spawn() {
|
2014-05-05 18:58:42 -05:00
|
|
|
Ok(mut process) => {
|
2015-01-31 11:20:46 -06:00
|
|
|
if let Some(input) = input {
|
2015-01-23 12:46:14 -06:00
|
|
|
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
|
2013-12-12 08:07:43 -06:00
|
|
|
}
|
2015-02-26 23:00:43 -06:00
|
|
|
let Output { status, stdout, stderr } =
|
2014-05-05 18:58:42 -05:00
|
|
|
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,
|
2015-02-26 23:00:43 -06:00
|
|
|
out: String::from_utf8(stdout).unwrap(),
|
|
|
|
err: String::from_utf8(stderr).unwrap()
|
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-06-11 16:52:38 -05:00
|
|
|
aux_path: Option<&str>,
|
2014-05-22 18:57:53 -05:00
|
|
|
args: &[String],
|
|
|
|
env: Vec<(String, String)> ,
|
2015-02-26 23:00:43 -06:00
|
|
|
input: Option<String>) -> Option<Child> {
|
2013-11-04 00:53:01 -06:00
|
|
|
|
2014-07-02 15:50:45 -05:00
|
|
|
let mut cmd = Command::new(prog);
|
2015-02-26 23:00:43 -06:00
|
|
|
cmd.args(args)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped());
|
2014-07-02 15:50:45 -05:00
|
|
|
add_target_env(&mut cmd, lib_path, aux_path);
|
2015-01-31 19:03:04 -06:00
|
|
|
for (key, val) in env {
|
2015-02-26 23:00:43 -06:00
|
|
|
cmd.env(&key, &val);
|
2014-07-02 15:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
match cmd.spawn() {
|
2014-01-30 16:37:10 -06:00
|
|
|
Ok(mut process) => {
|
2015-01-31 11:20:46 -06:00
|
|
|
if let Some(input) = input {
|
2015-01-23 12:46:14 -06:00
|
|
|
process.stdin.as_mut().unwrap().write_all(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
|
|
|
}
|