2014-05-28 11:43:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Michael Gehring <mg@ebfe.org>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
// spell-checker:ignore (acronyms/names) Gehring
|
|
|
|
// spell-checker:ignore (rustlang) clippy concat rustlang
|
|
|
|
// spell-checker:ignore (uutils) coreutils uucore uumain uutils sigpipe
|
2020-05-03 22:52:37 +00:00
|
|
|
// spell-checker:ignore (shell) busybox symlinks
|
2020-05-03 20:18:48 +00:00
|
|
|
|
2015-11-22 20:45:04 +00:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/uutils_crates.rs"));
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
extern crate lazy_static;
|
|
|
|
extern crate uucore;
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2014-11-04 06:19:52 +00:00
|
|
|
use std::collections::hash_map::HashMap;
|
2017-12-08 03:40:55 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2018-09-04 12:33:36 +00:00
|
|
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
lazy_static! {
|
2020-05-03 22:52:37 +00:00
|
|
|
static ref BINARY_PATH: std::path::PathBuf = {
|
|
|
|
// support symlinks by using args[0], when possible, with fallback to current_exe()
|
|
|
|
match std::env::args().next() {
|
|
|
|
Some(ref s) if !s.is_empty() => std::path::PathBuf::from(s),
|
|
|
|
_ => std::env::current_exe().unwrap(),
|
|
|
|
}
|
|
|
|
};
|
2020-05-03 20:18:48 +00:00
|
|
|
static ref NAME: &'static str = &*BINARY_PATH.file_stem().unwrap().to_str().unwrap();
|
|
|
|
}
|
|
|
|
|
2015-11-22 20:45:04 +00:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));
|
2015-05-16 17:28:36 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
fn usage(utils: &UtilityMap) {
|
|
|
|
println!("{} {}", *NAME, VERSION);
|
2019-12-28 18:45:11 +00:00
|
|
|
println!();
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("Usage:");
|
2020-05-03 20:18:48 +00:00
|
|
|
println!(" {} [util [arguments...]]\n", *NAME);
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("Currently defined functions:");
|
2019-12-29 17:22:56 +00:00
|
|
|
#[allow(clippy::map_clone)]
|
2020-05-03 20:18:48 +00:00
|
|
|
let mut utils: Vec<&str> = utils.keys().map(|&s| s).collect();
|
2014-06-19 20:05:43 +00:00
|
|
|
utils.sort();
|
2016-01-05 19:42:52 +00:00
|
|
|
for util in utils {
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("\t{}", util);
|
|
|
|
}
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-12-08 03:40:55 +00:00
|
|
|
uucore::panic::install_sigpipe_hook();
|
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
let utils = util_map();
|
2018-07-29 02:19:34 +00:00
|
|
|
let mut args: Vec<String> = uucore::args().collect();
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
let binary = &BINARY_PATH;
|
2018-06-18 06:04:37 +00:00
|
|
|
let binary_as_util = binary.file_stem().unwrap().to_str().unwrap();
|
2014-07-01 15:04:39 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
// binary name equals util name?
|
|
|
|
if let Some(&uumain) = utils.get(binary_as_util) {
|
2016-03-24 11:15:01 +00:00
|
|
|
std::process::exit(uumain(args));
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
// binary name equals prefixed util name?
|
|
|
|
// * prefix/stem may be any string ending in a non-alphanumeric character
|
|
|
|
if let Some(util) = utils.keys().find(|util| {
|
|
|
|
binary_as_util.ends_with(*util)
|
|
|
|
&& !(&binary_as_util[..binary_as_util.len() - (*util).len()])
|
|
|
|
.ends_with(char::is_alphanumeric)
|
|
|
|
}) {
|
|
|
|
// prefixed util => replace 0th (aka, executable name) argument
|
|
|
|
args[0] = (*util).to_owned();
|
2015-12-24 04:14:55 +00:00
|
|
|
} else {
|
2020-05-03 20:18:48 +00:00
|
|
|
// unmatched binary name => regard as multi-binary container and advance argument list
|
|
|
|
args.remove(0);
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
// 0th argument equals util name?
|
2019-12-26 17:00:40 +00:00
|
|
|
if !args.is_empty() {
|
2015-04-16 04:59:33 +00:00
|
|
|
let util = &args[0][..];
|
2014-07-01 15:04:39 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
match utils.get(util) {
|
2014-07-01 15:04:39 +00:00
|
|
|
Some(&uumain) => {
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(uumain(args.clone()));
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
None => {
|
2017-12-07 01:53:36 +00:00
|
|
|
if &args[0][..] == "--help" || &args[0][..] == "-h" {
|
2014-07-01 15:04:39 +00:00
|
|
|
// see if they want help on a specific util
|
|
|
|
if args.len() >= 2 {
|
2015-04-16 04:59:33 +00:00
|
|
|
let util = &args[1][..];
|
2020-05-03 20:18:48 +00:00
|
|
|
match utils.get(util) {
|
2014-07-01 15:04:39 +00:00
|
|
|
Some(&uumain) => {
|
2017-12-08 03:40:55 +00:00
|
|
|
let code = uumain(vec![util.to_owned(), "--help".to_owned()]);
|
|
|
|
std::io::stdout().flush().expect("could not flush stdout");
|
|
|
|
std::process::exit(code);
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
println!("{}: applet not found", util);
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(1);
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-03 20:18:48 +00:00
|
|
|
usage(&utils);
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(0);
|
2014-06-01 20:17:39 +00:00
|
|
|
} else {
|
|
|
|
println!("{}: applet not found", util);
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(1);
|
2014-06-01 20:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
2014-06-01 20:17:39 +00:00
|
|
|
} else {
|
|
|
|
// no arguments provided
|
2020-05-03 20:18:48 +00:00
|
|
|
usage(&utils);
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(0);
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
}
|