2020-05-27 20:12:11 +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.
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-05-04 04:32:22 +00:00
|
|
|
extern crate textwrap;
|
2020-05-03 20:18:48 +00:00
|
|
|
extern crate uucore;
|
|
|
|
|
2020-06-16 08:10:21 +00:00
|
|
|
use std::cmp;
|
2014-11-04 06:19:52 +00:00
|
|
|
use std::collections::hash_map::HashMap;
|
2020-06-16 08:10:21 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::process;
|
2017-12-08 03:40:55 +00:00
|
|
|
|
2020-06-16 08:10:21 +00:00
|
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
2020-05-03 20:18:48 +00:00
|
|
|
|
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-06-16 08:10:21 +00:00
|
|
|
fn usage<T>(utils: &UtilityMap<T>, name: &str) {
|
|
|
|
println!("{} {} (multi-call binary)\n", name, VERSION);
|
|
|
|
println!("Usage: {} [function [arguments...]]\n", name);
|
2020-05-04 04:32:22 +00:00
|
|
|
println!("Currently defined functions/utilities:\n");
|
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();
|
2020-05-04 04:32:22 +00:00
|
|
|
let display_list = utils.join(", ");
|
2020-06-16 08:10:21 +00:00
|
|
|
let width = cmp::min(textwrap::termwidth(), 100) - 4 * 2; // (opinion/heuristic) max 100 chars wide with 4 character side indentions
|
2020-05-04 04:32:22 +00:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
textwrap::indent(&textwrap::fill(&display_list, width), " ")
|
|
|
|
);
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 08:10:21 +00:00
|
|
|
fn binary_path(args: &mut impl Iterator<Item = OsString>) -> PathBuf {
|
|
|
|
match args.next() {
|
|
|
|
Some(s) if !s.is_empty() => PathBuf::from(s),
|
|
|
|
_ => std::env::current_exe().unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(binary_path: &Path) -> &str {
|
|
|
|
binary_path.file_stem().unwrap().to_str().unwrap()
|
|
|
|
}
|
|
|
|
|
2014-05-28 11:43:37 +00:00
|
|
|
fn main() {
|
2020-05-27 18:49:11 +00:00
|
|
|
uucore::panic::mute_sigpipe_panic();
|
2017-12-08 03:40:55 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
let utils = util_map();
|
2020-06-16 08:10:21 +00:00
|
|
|
let mut args = uucore::args_os();
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-06-16 08:10:21 +00:00
|
|
|
let binary = binary_path(&mut args);
|
|
|
|
let binary_as_util = name(&binary);
|
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) {
|
2020-06-16 08:10:21 +00:00
|
|
|
process::exit(uumain((vec![binary.into()].into_iter()).chain(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
|
2020-06-16 08:10:21 +00:00
|
|
|
let utilname =
|
|
|
|
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
|
|
|
|
Some(OsString::from(*util))
|
|
|
|
} else {
|
|
|
|
// unmatched binary name => regard as multi-binary container and advance argument list
|
|
|
|
args.next()
|
|
|
|
};
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2020-05-03 20:18:48 +00:00
|
|
|
// 0th argument equals util name?
|
2020-06-16 08:10:21 +00:00
|
|
|
if let Some(util_os) = utilname {
|
|
|
|
let util = util_os.as_os_str().to_string_lossy();
|
2014-07-01 15:04:39 +00:00
|
|
|
|
2020-06-16 08:10:21 +00:00
|
|
|
match utils.get(&util[..]) {
|
2014-07-01 15:04:39 +00:00
|
|
|
Some(&uumain) => {
|
2020-06-16 08:10:21 +00:00
|
|
|
process::exit(uumain((vec![util_os].into_iter()).chain(args)));
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
None => {
|
2020-06-16 08:10:21 +00:00
|
|
|
if util == "--help" || util == "-h" {
|
2014-07-01 15:04:39 +00:00
|
|
|
// see if they want help on a specific util
|
2020-06-16 08:10:21 +00:00
|
|
|
if let Some(util_os) = args.next() {
|
|
|
|
let util = util_os.as_os_str().to_string_lossy();
|
|
|
|
|
|
|
|
match utils.get(&util[..]) {
|
2014-07-01 15:04:39 +00:00
|
|
|
Some(&uumain) => {
|
2020-06-16 08:10:21 +00:00
|
|
|
let code = uumain((vec![util_os, OsString::from("--help")].into_iter()).chain(args));
|
|
|
|
io::stdout().flush().expect("could not flush stdout");
|
|
|
|
process::exit(code);
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
None => {
|
2020-05-04 04:32:22 +00:00
|
|
|
println!("{}: function/utility not found", util);
|
2020-06-16 08:10:21 +00:00
|
|
|
process::exit(1);
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 08:10:21 +00:00
|
|
|
usage(&utils, binary_as_util);
|
|
|
|
process::exit(0);
|
2014-06-01 20:17:39 +00:00
|
|
|
} else {
|
2020-05-04 04:32:22 +00:00
|
|
|
println!("{}: function/utility not found", util);
|
2020-06-16 08:10:21 +00:00
|
|
|
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-06-16 08:10:21 +00:00
|
|
|
usage(&utils, binary_as_util);
|
|
|
|
process::exit(0);
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
}
|