mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 14:52:41 +00:00
change/uutils ~ allow (almost) any name for the multi-binary container
- final multi-binary will now function correctly with any binary/executable name - multi-binary container acts as the specified util IF EITHER ... 1. the binary/executable name exactly matches the name of an applet/util 2. the binary/exectuable name matches <PREFIX><UTIL_NAME> pattern * where the PREFIX is any string ending in a non-alphanumeric character
This commit is contained in:
parent
100fc8d1e0
commit
bebf36bffb
2 changed files with 42 additions and 41 deletions
|
@ -214,7 +214,9 @@ feat_os_windows_legacy = [
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
uucore = "0.0.2"
|
lazy_static = { version="1.3" }
|
||||||
|
uucore = { version="0.0.2" }
|
||||||
|
# * uutils
|
||||||
arch = { optional=true, path="src/uu/arch" }
|
arch = { optional=true, path="src/uu/arch" }
|
||||||
base32 = { optional=true, path="src/uu/base32" }
|
base32 = { optional=true, path="src/uu/base32" }
|
||||||
base64 = { optional=true, path="src/uu/base64" }
|
base64 = { optional=true, path="src/uu/base64" }
|
||||||
|
@ -315,7 +317,6 @@ _backtrace = { version=">= 0.3.3, <= 0.3.30", package="backtrace" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
filetime = "0.2"
|
filetime = "0.2"
|
||||||
lazy_static = "1.3"
|
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
regex = "1.0"
|
regex = "1.0"
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#![crate_name = "uutils"]
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
*
|
*
|
||||||
|
@ -9,27 +7,37 @@
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// spell-checker:ignore (acronyms/names) Gehring
|
||||||
|
// spell-checker:ignore (rustlang) clippy concat rustlang
|
||||||
|
// spell-checker:ignore (uutils) coreutils uucore uumain uutils sigpipe
|
||||||
|
// spell-checker:ignore (shell) busybox
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/uutils_crates.rs"));
|
include!(concat!(env!("OUT_DIR"), "/uutils_crates.rs"));
|
||||||
|
|
||||||
use std::collections::hash_map::HashMap;
|
extern crate lazy_static;
|
||||||
use std::io::Write;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
static NAME: &str = "uutils";
|
use lazy_static::lazy_static;
|
||||||
|
use std::collections::hash_map::HashMap;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref BINARY_PATH: std::path::PathBuf = std::env::current_exe().unwrap();
|
||||||
|
static ref NAME: &'static str = &*BINARY_PATH.file_stem().unwrap().to_str().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));
|
include!(concat!(env!("OUT_DIR"), "/uutils_map.rs"));
|
||||||
|
|
||||||
fn usage(cmap: &UtilityMap) {
|
fn usage(utils: &UtilityMap) {
|
||||||
println!("{} {}", NAME, VERSION);
|
println!("{} {}", *NAME, VERSION);
|
||||||
println!();
|
println!();
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {} [util [arguments...]]\n", NAME);
|
println!(" {} [util [arguments...]]\n", *NAME);
|
||||||
println!("Currently defined functions:");
|
println!("Currently defined functions:");
|
||||||
#[allow(clippy::map_clone)]
|
#[allow(clippy::map_clone)]
|
||||||
let mut utils: Vec<&str> = cmap.keys().map(|&s| s).collect();
|
let mut utils: Vec<&str> = utils.keys().map(|&s| s).collect();
|
||||||
utils.sort();
|
utils.sort();
|
||||||
for util in utils {
|
for util in utils {
|
||||||
println!("\t{}", util);
|
println!("\t{}", util);
|
||||||
|
@ -39,44 +47,36 @@ fn usage(cmap: &UtilityMap) {
|
||||||
fn main() {
|
fn main() {
|
||||||
uucore::panic::install_sigpipe_hook();
|
uucore::panic::install_sigpipe_hook();
|
||||||
|
|
||||||
let umap = util_map();
|
let utils = util_map();
|
||||||
let mut args: Vec<String> = uucore::args().collect();
|
let mut args: Vec<String> = uucore::args().collect();
|
||||||
|
|
||||||
// try binary name as util name.
|
let binary = &BINARY_PATH;
|
||||||
let args0 = args[0].clone();
|
|
||||||
let binary = Path::new(&args0[..]);
|
|
||||||
let binary_as_util = binary.file_stem().unwrap().to_str().unwrap();
|
let binary_as_util = binary.file_stem().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
if let Some(&uumain) = umap.get(binary_as_util) {
|
// binary name equals util name?
|
||||||
|
if let Some(&uumain) = utils.get(binary_as_util) {
|
||||||
std::process::exit(uumain(args));
|
std::process::exit(uumain(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
if binary_as_util.ends_with("uutils")
|
// binary name equals prefixed util name?
|
||||||
|| binary_as_util.starts_with("uutils")
|
// * prefix/stem may be any string ending in a non-alphanumeric character
|
||||||
|| binary_as_util.ends_with("busybox")
|
if let Some(util) = utils.keys().find(|util| {
|
||||||
|| binary_as_util.starts_with("busybox")
|
binary_as_util.ends_with(*util)
|
||||||
{
|
&& !(&binary_as_util[..binary_as_util.len() - (*util).len()])
|
||||||
args.remove(0);
|
.ends_with(char::is_alphanumeric)
|
||||||
|
}) {
|
||||||
|
// prefixed util => replace 0th (aka, executable name) argument
|
||||||
|
args[0] = (*util).to_owned();
|
||||||
} else {
|
} else {
|
||||||
let mut found = false;
|
// unmatched binary name => regard as multi-binary container and advance argument list
|
||||||
for util in umap.keys() {
|
args.remove(0);
|
||||||
if binary_as_util.ends_with(util) {
|
|
||||||
args[0] = (*util).to_owned();
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
println!("{}: applet not found", binary_as_util);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try first arg as util name.
|
// 0th argument equals util name?
|
||||||
if !args.is_empty() {
|
if !args.is_empty() {
|
||||||
let util = &args[0][..];
|
let util = &args[0][..];
|
||||||
|
|
||||||
match umap.get(util) {
|
match utils.get(util) {
|
||||||
Some(&uumain) => {
|
Some(&uumain) => {
|
||||||
std::process::exit(uumain(args.clone()));
|
std::process::exit(uumain(args.clone()));
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ fn main() {
|
||||||
// see if they want help on a specific util
|
// see if they want help on a specific util
|
||||||
if args.len() >= 2 {
|
if args.len() >= 2 {
|
||||||
let util = &args[1][..];
|
let util = &args[1][..];
|
||||||
match umap.get(util) {
|
match utils.get(util) {
|
||||||
Some(&uumain) => {
|
Some(&uumain) => {
|
||||||
let code = uumain(vec![util.to_owned(), "--help".to_owned()]);
|
let code = uumain(vec![util.to_owned(), "--help".to_owned()]);
|
||||||
std::io::stdout().flush().expect("could not flush stdout");
|
std::io::stdout().flush().expect("could not flush stdout");
|
||||||
|
@ -97,7 +97,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
usage(&umap);
|
usage(&utils);
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
} else {
|
} else {
|
||||||
println!("{}: applet not found", util);
|
println!("{}: applet not found", util);
|
||||||
|
@ -107,7 +107,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no arguments provided
|
// no arguments provided
|
||||||
usage(&umap);
|
usage(&utils);
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue