2014-02-07 06:39:07 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
2017-12-11 04:57:39 +00:00
|
|
|
* (c) Alex Lyon <arcterus@mail.com>
|
2014-02-07 06:39:07 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-12-08 02:49:18 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! executable(
|
|
|
|
() => ({
|
|
|
|
let module = module_path!();
|
|
|
|
if &module[0..3] == "uu_" {
|
|
|
|
&module[3..]
|
|
|
|
} else {
|
|
|
|
module
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_export]
|
2014-06-09 03:26:51 +00:00
|
|
|
macro_rules! show_error(
|
2015-01-10 00:16:05 +00:00
|
|
|
($($args:tt)+) => ({
|
2017-12-11 04:57:39 +00:00
|
|
|
eprint!("{}: error: ", executable!());
|
|
|
|
eprintln!($($args)+);
|
2014-06-08 08:37:02 +00:00
|
|
|
})
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-06-08 08:37:02 +00:00
|
|
|
|
2014-07-10 16:10:09 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! show_warning(
|
2015-01-10 00:16:05 +00:00
|
|
|
($($args:tt)+) => ({
|
2017-12-11 04:57:39 +00:00
|
|
|
eprint!("{}: warning: ", executable!());
|
|
|
|
eprintln!($($args)+);
|
2014-07-10 16:10:09 +00:00
|
|
|
})
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-10 16:10:09 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! show_info(
|
2015-01-10 00:16:05 +00:00
|
|
|
($($args:tt)+) => ({
|
2017-12-11 04:57:39 +00:00
|
|
|
eprint!("{}: ", executable!());
|
|
|
|
eprintln!($($args)+);
|
2014-07-10 16:10:09 +00:00
|
|
|
})
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-10 16:10:09 +00:00
|
|
|
|
2016-05-21 10:19:13 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! disp_err(
|
|
|
|
($($args:tt)+) => ({
|
2017-12-11 04:57:39 +00:00
|
|
|
eprint!("{}: ", executable!());
|
|
|
|
eprintln!($($args)+);
|
|
|
|
eprintln!("Try '{} --help' for more information.", executable!());
|
2016-05-21 10:19:13 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! crash(
|
2015-01-10 00:16:05 +00:00
|
|
|
($exitcode:expr, $($args:tt)+) => ({
|
|
|
|
show_error!($($args)+);
|
2015-07-20 00:25:48 +00:00
|
|
|
::std::process::exit($exitcode)
|
2014-04-03 13:59:58 +00:00
|
|
|
})
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-04-03 13:59:58 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! exit(
|
|
|
|
($exitcode:expr) => ({
|
2015-07-20 00:25:48 +00:00
|
|
|
::std::process::exit($exitcode)
|
2014-02-07 06:39:07 +00:00
|
|
|
})
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-02-07 06:39:07 +00:00
|
|
|
|
2014-02-27 18:59:51 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! crash_if_err(
|
|
|
|
($exitcode:expr, $exp:expr) => (
|
|
|
|
match $exp {
|
|
|
|
Ok(m) => m,
|
2015-05-12 01:08:32 +00:00
|
|
|
Err(f) => crash!($exitcode, "{}", f),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-07-22 01:50:53 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! return_if_err(
|
|
|
|
($exitcode:expr, $exp:expr) => (
|
|
|
|
match $exp {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
|
|
|
show_error!("{}", f);
|
|
|
|
return $exitcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-22 01:50:53 +00:00
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! safe_write(
|
2015-01-10 00:16:05 +00:00
|
|
|
($fd:expr, $($args:tt)+) => (
|
|
|
|
match write!($fd, $($args)+) {
|
2014-02-07 06:39:07 +00:00
|
|
|
Ok(_) => {}
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(f) => panic!(f.to_string())
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-02-07 06:39:07 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! safe_writeln(
|
2015-01-10 00:16:05 +00:00
|
|
|
($fd:expr, $($args:tt)+) => (
|
|
|
|
match writeln!($fd, $($args)+) {
|
2014-02-07 06:39:07 +00:00
|
|
|
Ok(_) => {}
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(f) => panic!(f.to_string())
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-02-19 01:10:32 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! safe_unwrap(
|
|
|
|
($exp:expr) => (
|
|
|
|
match $exp {
|
|
|
|
Ok(m) => m,
|
2014-07-09 08:29:50 +00:00
|
|
|
Err(f) => crash!(1, "{}", f.to_string())
|
2014-02-19 01:10:32 +00:00
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2016-08-08 07:05:36 +00:00
|
|
|
|
|
|
|
//-- message templates
|
|
|
|
|
|
|
|
//-- message templates : general
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! snippet_list_join_oxford {
|
|
|
|
($conjunction:expr, $valOne:expr, $valTwo:expr) => (
|
|
|
|
format!("{}, {} {}", $valOne, $conjunction, $valTwo)
|
|
|
|
);
|
|
|
|
($conjunction:expr, $valOne:expr, $valTwo:expr $(, $remainingVals:expr)*) => (
|
|
|
|
format!("{}, {}", $valOne, snippet_list_join_inner!($conjunction, $valTwo $(, $remainingVals)*))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! snippet_list_join_or {
|
|
|
|
($valOne:expr, $valTwo:expr) => (
|
|
|
|
format!("{} or {}", $valOne, $valTwo)
|
|
|
|
);
|
|
|
|
($valOne:expr, $valTwo:expr $(, $remainingVals:expr)*) => (
|
|
|
|
format!("{}, {}", $valOne, snippet_list_join_oxford!("or", $valTwo $(, $remainingVals)*))
|
2016-08-09 16:16:06 +00:00
|
|
|
);
|
2016-08-08 07:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- message templates : invalid input
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_invalid_input { ($reason: expr) => (
|
|
|
|
format!("invalid input: {}", $reason) ); }
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! snippet_no_file_at_path { ($path:expr) => (
|
|
|
|
format!("nonexistent path {}", $path) ); }
|
|
|
|
|
|
|
|
// -- message templates : invalid input : flag
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_invalid_opt_use {
|
|
|
|
($about:expr, $flag:expr) => (
|
|
|
|
msg_invalid_input!(format!("The '{}' option {}", $flag, $about))
|
|
|
|
);
|
|
|
|
($about:expr, $longflag:expr, $shortflag:expr) => {
|
|
|
|
msg_invalid_input!(format!("The '{}' ('{}') option {}", $longflag, $shortflag, $about))
|
|
|
|
};
|
|
|
|
}
|
2016-08-09 16:16:06 +00:00
|
|
|
|
2016-08-08 07:05:36 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_opt_only_usable_if {
|
|
|
|
($clause:expr, $flag:expr) => (
|
|
|
|
msg_invalid_opt_use!(format!("only usable if {}", $clause), $flag)
|
|
|
|
);
|
|
|
|
($clause:expr, $longflag:expr, $shortflag:expr) => (
|
|
|
|
msg_invalid_opt_use!(format!("only usable if {}", $clause), $longflag, $shortflag)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_opt_invalid_should_be {
|
|
|
|
($expects:expr, $received:expr, $flag:expr) => (
|
|
|
|
msg_invalid_opt_use!(format!("expects {}, but was provided {}", $expects, $received), $flag)
|
|
|
|
);
|
|
|
|
($expects:expr, $received:expr, $longflag:expr, $shortflag:expr) => (
|
|
|
|
msg_invalid_opt_use!(format!("expects {}, but was provided {}", $expects, $received), $longflag, $shortflag)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- message templates : invalid input : args
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_arg_invalid_value { ($expects:expr, $received:expr) => (
|
|
|
|
msg_invalid_input!(format!("expects its argument to be {}, but was provided {}", $expects, $received)) ); }
|
|
|
|
|
|
|
|
#[macro_export]
|
2016-08-10 05:59:30 +00:00
|
|
|
macro_rules! msg_args_invalid_value {
|
|
|
|
($expects:expr, $received:expr) => (
|
|
|
|
msg_invalid_input!(format!("expects its arguments to be {}, but was provided {}", $expects, $received))
|
|
|
|
);
|
|
|
|
($msg:expr) => (
|
|
|
|
msg_invalid_input!($msg)
|
|
|
|
);
|
|
|
|
}
|
2016-08-08 07:05:36 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_args_nonexistent_file { ($received:expr) => (
|
2016-08-09 16:16:06 +00:00
|
|
|
msg_args_invalid_value!("paths to files", snippet_no_file_at_path!($received)));}
|
2016-08-08 07:05:36 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2016-08-15 02:49:27 +00:00
|
|
|
macro_rules! msg_wrong_number_of_arguments {
|
|
|
|
() => (
|
|
|
|
msg_args_invalid_value!("wrong number of arguments")
|
|
|
|
);
|
|
|
|
($min:expr, $max:expr) => (
|
|
|
|
msg_args_invalid_value!(format!("expects {}-{} arguments", $min, $max))
|
|
|
|
);
|
|
|
|
($exact:expr) => (
|
|
|
|
if $exact == 1 {
|
|
|
|
msg_args_invalid_value!("expects 1 argument")
|
|
|
|
} else {
|
|
|
|
msg_args_invalid_value!(format!("expects {} arguments", $exact))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-08-09 16:16:06 +00:00
|
|
|
|
|
|
|
// -- message templates : invalid input : input combinations
|
2016-08-08 07:05:36 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_expects_one_of {
|
|
|
|
($valOne:expr $(, $remainingVals:expr)*) => (
|
2016-08-09 16:16:06 +00:00
|
|
|
msg_invalid_input!(format!("expects one of {}", snippet_list_join_or!($valOne $(, $remainingVals)*)))
|
|
|
|
);
|
2016-08-08 07:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg_expects_no_more_than_one_of {
|
|
|
|
($valOne:expr $(, $remainingVals:expr)*) => (
|
|
|
|
msg_invalid_input!(format!("expects no more than one of {}", snippet_list_join_or!($valOne $(, $remainingVals)*))) ;
|
2016-08-09 16:16:06 +00:00
|
|
|
);
|
2016-08-08 07:05:36 +00:00
|
|
|
}
|