2014-02-07 06:39:07 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Arcterus <arcterus@mail.com>
|
|
|
|
*
|
|
|
|
* 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)+) => ({
|
2015-12-08 02:49:18 +00:00
|
|
|
pipe_write!(&mut ::std::io::stderr(), "{}: error: ", executable!());
|
2015-04-16 04:59:33 +00:00
|
|
|
pipe_writeln!(&mut ::std::io::stderr(), $($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)+) => ({
|
2015-12-08 02:49:18 +00:00
|
|
|
pipe_write!(&mut ::std::io::stderr(), "{}: warning: ", executable!());
|
2015-04-16 04:59:33 +00:00
|
|
|
pipe_writeln!(&mut ::std::io::stderr(), $($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)+) => ({
|
2015-12-08 02:49:18 +00:00
|
|
|
pipe_write!(&mut ::std::io::stderr(), "{}: ", executable!());
|
2015-04-16 04:59:33 +00:00
|
|
|
pipe_writeln!(&mut ::std::io::stderr(), $($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)+) => ({
|
|
|
|
pipe_write!(&mut ::std::io::stderr(), "{}: ", executable!());
|
|
|
|
pipe_writeln!(&mut ::std::io::stderr(), $($args)+);
|
|
|
|
pipe_writeln!(&mut ::std::io::stderr(), "Try '{} --help' for more information.", executable!());
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2014-06-29 19:57:54 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! eprint(
|
2015-04-16 04:59:33 +00:00
|
|
|
($($args:tt)+) => (pipe_write!(&mut ::std::io::stderr(), $($args)+))
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-06-29 19:57:54 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! eprintln(
|
2015-04-16 04:59:33 +00:00
|
|
|
($($args:tt)+) => (pipe_writeln!(&mut ::std::io::stderr(), $($args)+))
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-06-29 19:57:54 +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),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_crash_if_err(
|
|
|
|
($exitcode:expr, $exp:expr) => (
|
|
|
|
match $exp {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(f) => {
|
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
|
|
|
()
|
|
|
|
} else {
|
|
|
|
crash!($exitcode, "{}", f)
|
|
|
|
}
|
|
|
|
},
|
2014-02-27 18:59:51 +00:00
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-02-27 18:59:51 +00:00
|
|
|
|
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-07-25 05:20:03 +00:00
|
|
|
// XXX: should the pipe_* macros return an Err just to show the write failed?
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_print(
|
2015-01-10 00:16:05 +00:00
|
|
|
($($args:tt)+) => (
|
2015-04-16 04:59:33 +00:00
|
|
|
match write!(&mut ::std::io::stdout(), $($args)+) {
|
2014-07-25 05:20:03 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
2015-04-16 04:59:33 +00:00
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
2014-07-25 05:20:03 +00:00
|
|
|
false
|
|
|
|
} else {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("{}", f)
|
2014-07-25 05:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-25 05:20:03 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_println(
|
2015-01-10 00:16:05 +00:00
|
|
|
($($args:tt)+) => (
|
2015-04-16 04:59:33 +00:00
|
|
|
match writeln!(&mut ::std::io::stdout(), $($args)+) {
|
2014-07-25 05:20:03 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
2015-04-16 04:59:33 +00:00
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
2014-07-25 05:20:03 +00:00
|
|
|
false
|
|
|
|
} else {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("{}", f)
|
2014-07-25 05:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-25 05:20:03 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_write(
|
2015-01-10 00:16:05 +00:00
|
|
|
($fd:expr, $($args:tt)+) => (
|
|
|
|
match write!($fd, $($args)+) {
|
2014-07-25 05:20:03 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
2015-04-16 04:59:33 +00:00
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
2014-07-25 05:20:03 +00:00
|
|
|
false
|
|
|
|
} else {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("{}", f)
|
2014-07-25 05:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-25 05:20:03 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_writeln(
|
2015-01-10 00:16:05 +00:00
|
|
|
($fd:expr, $($args:tt)+) => (
|
|
|
|
match writeln!($fd, $($args)+) {
|
2014-07-25 05:20:03 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
2015-04-16 04:59:33 +00:00
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
2014-07-25 05:20:03 +00:00
|
|
|
false
|
|
|
|
} else {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("{}", f)
|
2014-07-25 05:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-21 23:48:23 +00:00
|
|
|
);
|
2014-07-25 05:20:03 +00:00
|
|
|
|
2015-05-23 07:21:53 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pipe_flush(
|
|
|
|
() => (
|
|
|
|
match ::std::io::stdout().flush() {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
panic!("{}", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
($fd:expr) => (
|
|
|
|
match $fd.flush() {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(f) => {
|
|
|
|
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
panic!("{}", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
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
|
|
|
);
|