2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "uutils"]
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-11-22 20:45:04 +00:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/uutils_crates.rs"));
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2014-11-04 06:19:52 +00:00
|
|
|
use std::collections::hash_map::HashMap;
|
2015-04-16 04:59:33 +00:00
|
|
|
use std::path::Path;
|
2015-11-22 20:45:04 +00:00
|
|
|
use std::env;
|
2017-12-08 03:40:55 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
extern crate uucore;
|
2014-05-28 11:43:37 +00:00
|
|
|
|
|
|
|
static NAME: &'static str = "uutils";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-05-28 11:43:37 +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
|
|
|
|
|
|
|
fn usage(cmap: &UtilityMap) {
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2014-07-19 21:25:28 +00:00
|
|
|
println!(" {} [util [arguments...]]\n", NAME);
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("Currently defined functions:");
|
2015-11-27 06:54:18 +00:00
|
|
|
let mut utils: Vec<&str> = cmap.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();
|
|
|
|
|
2014-05-28 11:43:37 +00:00
|
|
|
let umap = util_map();
|
2018-03-12 08:20:58 +00:00
|
|
|
let mut args: Vec<String> = env::args().collect();
|
2014-05-28 11:43:37 +00:00
|
|
|
|
|
|
|
// try binary name as util name.
|
2015-04-16 04:59:33 +00:00
|
|
|
let args0 = args[0].clone();
|
|
|
|
let binary = Path::new(&args0[..]);
|
|
|
|
let binary_as_util = binary.file_name().unwrap().to_str().unwrap();
|
2014-07-01 15:04:39 +00:00
|
|
|
|
2016-03-24 11:15:01 +00:00
|
|
|
if let Some(&uumain) = umap.get(binary_as_util) {
|
|
|
|
std::process::exit(uumain(args));
|
2014-07-01 15:04:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 08:20:58 +00:00
|
|
|
if binary_as_util.ends_with("uutils") || binary_as_util.starts_with("uutils")
|
|
|
|
|| binary_as_util.ends_with("busybox") || binary_as_util.starts_with("busybox")
|
|
|
|
{
|
2015-12-24 04:14:55 +00:00
|
|
|
args.remove(0);
|
|
|
|
} else {
|
|
|
|
let mut found = false;
|
|
|
|
for util in umap.keys() {
|
|
|
|
if binary_as_util.ends_with(util) {
|
2016-03-24 11:15:01 +00:00
|
|
|
args[0] = (*util).to_owned();
|
2015-12-24 04:14:55 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 08:20:58 +00:00
|
|
|
if !found {
|
2015-12-24 04:14:55 +00:00
|
|
|
println!("{}: applet not found", binary_as_util);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try first arg as util name.
|
2015-12-24 04:14:55 +00:00
|
|
|
if args.len() >= 1 {
|
2015-04-16 04:59:33 +00:00
|
|
|
let util = &args[0][..];
|
2014-07-01 15:04:39 +00:00
|
|
|
|
2015-11-27 06:54:18 +00:00
|
|
|
match umap.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][..];
|
2015-11-27 06:54:18 +00:00
|
|
|
match umap.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usage(&umap);
|
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
|
|
|
|
usage(&umap);
|
2015-05-19 01:22:51 +00:00
|
|
|
std::process::exit(0);
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
}
|