diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 9fd44b001..6db088ea1 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -292,13 +292,13 @@ fn du( let read = match fs::read_dir(&my_stat.path) { Ok(read) => read, Err(e) => { - safe_writeln!( + writeln!( stderr(), "{}: cannot read directory {}: {}", options.util_name, my_stat.path.quote(), e - ); + ).unwrap(); return Box::new(iter::once(my_stat)); } }; diff --git a/src/uu/join/src/join.rs b/src/uu/join/src/join.rs index 03fe7dcd5..e396d4294 100644 --- a/src/uu/join/src/join.rs +++ b/src/uu/join/src/join.rs @@ -413,7 +413,7 @@ impl<'a> State<'a> { // This is fatal if the check is enabled. if input.check_order == CheckOrder::Enabled { - exit!(1); + std::process::exit(1); } self.has_failed = true; diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index 591dacf25..f745c3bf6 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -410,7 +410,7 @@ fn get_size(size_str_opt: Option) -> Option { util_name(), size_str_opt.unwrap().maybe_quote() ); - exit!(1); + std::process::exit(1); } }; diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index dfc116cb3..423a31892 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -91,7 +91,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if cfg!(windows) { // see https://github.com/rust-lang/rust/issues/29494 show_error!("{} is currently not supported in this platform", OPT_FILTER); - exit!(-1); + std::process::exit(-1); } else { settings.filter = Some(matches.value_of(OPT_FILTER).unwrap().to_owned()); } diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 3d2d867bd..2f8ccce13 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -18,7 +18,6 @@ mod parser; // string parsing modules // * cross-platform modules pub use crate::mods::backup_control; -pub use crate::mods::coreopts; pub use crate::mods::display; pub use crate::mods::error; pub use crate::mods::os; diff --git a/src/uucore/src/lib/macros.rs b/src/uucore/src/lib/macros.rs index 275b0afe7..5c1e24d03 100644 --- a/src/uucore/src/lib/macros.rs +++ b/src/uucore/src/lib/macros.rs @@ -26,9 +26,7 @@ //! - From custom messages: [`show_error!`], [`show_usage_error!`] //! - Print warnings: [`show_warning!`] //! - Terminate util execution -//! - Terminate regularly: [`exit!`], [`return_if_err!`] -//! - Crash program: [`crash!`], [`crash_if_err!`], [`safe_unwrap!`] -//! - Unwrapping result types: [`safe_unwrap!`] +//! - Crash program: [`crash!`], [`crash_if_err!`] // spell-checker:ignore sourcepath targetpath @@ -223,22 +221,10 @@ macro_rules! show_usage_error( }) ); -//==== - -/// Calls [`std::process::exit`] with the provided exit code. -/// -/// Why not call exit directly? -#[macro_export] -macro_rules! exit( - ($exit_code:expr) => ({ - ::std::process::exit($exit_code) - }) -); - /// Display an error and [`exit!`] /// /// Displays the provided error message using [`show_error!`], then invokes -/// [`exit!`] with the provided exit code. +/// [`std::process::exit`] with the provided exit code. /// /// # Examples /// @@ -255,7 +241,7 @@ macro_rules! exit( macro_rules! crash( ($exit_code:expr, $($args:tt)+) => ({ $crate::show_error!($($args)+); - $crate::exit!($exit_code) + std::process::exit($exit_code); }) ); @@ -289,52 +275,6 @@ macro_rules! crash_if_err( ) ); -/// Unwrap some Result, crashing instead of panicking. -/// -/// Drop this in favor of `crash_if_err!` -#[macro_export] -macro_rules! safe_unwrap( - ($exp:expr) => ( - match $exp { - Ok(m) => m, - Err(f) => $crate::crash!(1, "{}", f.to_string()) - } - ) -); - -//==== - -/// Unwraps the Result. Instead of panicking, it shows the error and then -/// returns from the function with the provided exit code. -/// Assumes the current function returns an i32 value. -/// -/// Replace with `crash_if_err`? -#[macro_export] -macro_rules! return_if_err( - ($exit_code:expr, $exp:expr) => ( - match $exp { - Ok(m) => m, - Err(f) => { - $crate::show_error!("{}", f); - return $exit_code; - } - } - ) -); - -//==== - -/// This is used exclusively by du... -#[macro_export] -macro_rules! safe_writeln( - ($fd:expr, $($args:tt)+) => ( - match writeln!($fd, $($args)+) { - Ok(_) => {} - Err(f) => panic!("{}", f) - } - ) -); - //-- message templates //-- message templates : (join utility sub-macros) diff --git a/src/uucore/src/lib/mods.rs b/src/uucore/src/lib/mods.rs index 8f6d14976..bbde696dc 100644 --- a/src/uucore/src/lib/mods.rs +++ b/src/uucore/src/lib/mods.rs @@ -1,7 +1,6 @@ // mods ~ cross-platforms modules (core/bundler file) pub mod backup_control; -pub mod coreopts; pub mod display; pub mod error; pub mod os; diff --git a/src/uucore/src/lib/mods/coreopts.rs b/src/uucore/src/lib/mods/coreopts.rs deleted file mode 100644 index b534ff902..000000000 --- a/src/uucore/src/lib/mods/coreopts.rs +++ /dev/null @@ -1,141 +0,0 @@ -pub struct HelpText<'a> { - pub name: &'a str, - pub version: &'a str, - pub syntax: &'a str, - pub summary: &'a str, - pub long_help: &'a str, - pub display_usage: bool, -} - -pub struct CoreOptions<'a> { - options: getopts::Options, - help_text: HelpText<'a>, -} - -impl<'a> CoreOptions<'a> { - pub fn new(help_text: HelpText<'a>) -> Self { - let mut ret = CoreOptions { - options: getopts::Options::new(), - help_text, - }; - ret.options - .optflag("", "help", "print usage information") - .optflag("", "version", "print name and version number"); - ret - } - pub fn optflagopt( - &mut self, - short_name: &str, - long_name: &str, - desc: &str, - hint: &str, - ) -> &mut CoreOptions<'a> { - self.options.optflagopt(short_name, long_name, desc, hint); - self - } - pub fn optflag( - &mut self, - short_name: &str, - long_name: &str, - desc: &str, - ) -> &mut CoreOptions<'a> { - self.options.optflag(short_name, long_name, desc); - self - } - pub fn optflagmulti( - &mut self, - short_name: &str, - long_name: &str, - desc: &str, - ) -> &mut CoreOptions<'a> { - self.options.optflagmulti(short_name, long_name, desc); - self - } - pub fn optopt( - &mut self, - short_name: &str, - long_name: &str, - desc: &str, - hint: &str, - ) -> &mut CoreOptions<'a> { - self.options.optopt(short_name, long_name, desc, hint); - self - } - pub fn optmulti( - &mut self, - short_name: &str, - long_name: &str, - desc: &str, - hint: &str, - ) -> &mut CoreOptions<'a> { - self.options.optmulti(short_name, long_name, desc, hint); - self - } - pub fn usage(&self, summary: &str) -> String { - self.options.usage(summary) - } - pub fn parse(&mut self, args: Vec) -> getopts::Matches { - let matches = match self.options.parse(&args[1..]) { - Ok(m) => Some(m), - Err(f) => { - eprint!("{}: error: ", self.help_text.name); - eprintln!("{}", f); - ::std::process::exit(1); - } - } - .unwrap(); - if matches.opt_present("help") { - let usage_str = if self.help_text.display_usage { - format!( - "\n {}\n\n Reference\n", - self.options.usage(self.help_text.summary) - ) - .replace("Options:", " Options:") - } else { - String::new() - }; - print!( - " - {0} {1} - - {0} {2} -{3}{4} -", - self.help_text.name, - self.help_text.version, - self.help_text.syntax, - usage_str, - self.help_text.long_help - ); - crate::exit!(0); - } else if matches.opt_present("version") { - println!("{} {}", self.help_text.name, self.help_text.version); - crate::exit!(0); - } - matches - } -} - -#[macro_export] -macro_rules! app { - ($syntax: expr, $summary: expr, $long_help: expr) => { - uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText { - name: uucore::util_name(), - version: env!("CARGO_PKG_VERSION"), - syntax: $syntax, - summary: $summary, - long_help: $long_help, - display_usage: true, - }) - }; - ($syntax: expr, $summary: expr, $long_help: expr, $display_usage: expr) => { - uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText { - name: uucore::util_name(), - version: env!("CARGO_PKG_VERSION"), - syntax: $syntax, - summary: $summary, - long_help: $long_help, - display_usage: $display_usage, - }) - }; -}