stdbuf: return UResult from uumain() function

This commit is contained in:
Jeffrey Finkelstein 2021-12-30 22:52:00 -05:00
parent 9f21cd0d37
commit df188258ec

View file

@ -19,6 +19,7 @@ use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use tempfile::tempdir; use tempfile::tempdir;
use tempfile::TempDir; use tempfile::TempDir;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size; use uucore::parse_size::parse_size;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
@ -148,7 +149,8 @@ fn get_preload_env(tmp_dir: &mut TempDir) -> io::Result<(String, PathBuf)> {
Ok((preload.to_owned(), inject_path)) Ok((preload.to_owned(), inject_path))
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
@ -156,37 +158,36 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let matches = uu_app().usage(&usage[..]).get_matches_from(args); let matches = uu_app().usage(&usage[..]).get_matches_from(args);
let options = ProgramOptions::try_from(&matches).unwrap_or_else(|e| { let options = ProgramOptions::try_from(&matches).map_err(|e| UUsageError::new(125, e.0))?;
crash!(
125,
"{}\nTry '{} --help' for more information.",
e.0,
uucore::execution_phrase()
)
});
let mut command_values = matches.values_of::<&str>(options::COMMAND).unwrap(); let mut command_values = matches.values_of::<&str>(options::COMMAND).unwrap();
let mut command = Command::new(command_values.next().unwrap()); let mut command = Command::new(command_values.next().unwrap());
let command_params: Vec<&str> = command_values.collect(); let command_params: Vec<&str> = command_values.collect();
let mut tmp_dir = tempdir().unwrap(); let mut tmp_dir = tempdir().unwrap();
let (preload_env, libstdbuf) = crash_if_err!(1, get_preload_env(&mut tmp_dir)); let (preload_env, libstdbuf) = get_preload_env(&mut tmp_dir).map_err_context(String::new)?;
command.env(preload_env, libstdbuf); command.env(preload_env, libstdbuf);
set_command_env(&mut command, "_STDBUF_I", options.stdin); set_command_env(&mut command, "_STDBUF_I", options.stdin);
set_command_env(&mut command, "_STDBUF_O", options.stdout); set_command_env(&mut command, "_STDBUF_O", options.stdout);
set_command_env(&mut command, "_STDBUF_E", options.stderr); set_command_env(&mut command, "_STDBUF_E", options.stderr);
command.args(command_params); command.args(command_params);
let mut process = match command.spawn() { let mut process = command
Ok(p) => p, .spawn()
Err(e) => crash!(1, "failed to execute process: {}", e), .map_err_context(|| "failed to execute process".to_string())?;
}; let status = process.wait().map_err_context(String::new)?;
match process.wait() { match status.code() {
Ok(status) => match status.code() { Some(i) => {
Some(i) => i, if i == 0 {
None => crash!(1, "process killed by signal {}", status.signal().unwrap()), Ok(())
}, } else {
Err(e) => crash!(1, "{}", e), Err(i.into())
}
}
None => Err(USimpleError::new(
1,
format!("process killed by signal {}", status.signal().unwrap()),
)),
} }
} }