coreutils/src/uutils/uutils.rs

113 lines
3.3 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "uutils"]
2015-03-08 18:09:41 +00:00
#![feature(core, exit_status, old_path, rustc_private)]
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.
*/
extern crate getopts;
@CRATES@
2014-05-28 11:43:37 +00:00
use std::env;
2014-11-04 06:19:52 +00:00
use std::collections::hash_map::HashMap;
2014-05-28 11:43:37 +00:00
static NAME: &'static str = "uutils";
static VERSION: &'static str = "1.0.0";
fn util_map() -> HashMap<&'static str, fn(Vec<String>) -> i32> {
2014-07-01 15:04:39 +00:00
let mut map = HashMap::new();
@UTIL_MAP@
2014-05-28 11:43:37 +00:00
map
}
fn usage(cmap: &HashMap<&'static str, fn(Vec<String>) -> i32>) {
2014-06-19 20:05:43 +00:00
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [util [arguments...]]\n", NAME);
2014-06-19 20:05:43 +00:00
println!("Currently defined functions:");
let mut utils: Vec<&str> = cmap.keys().map(|&s| s).collect();
utils.sort();
for util in utils.iter() {
println!("\t{}", util);
}
2014-05-28 11:43:37 +00:00
}
fn main() {
let umap = util_map();
2015-02-13 19:57:47 +00:00
let mut args : Vec<String> = env::args().collect();
2014-05-28 11:43:37 +00:00
// try binary name as util name.
2014-07-20 01:13:55 +00:00
let binary = Path::new(args[0].as_slice());
let binary_as_util = binary.filename_str().unwrap();
2014-07-01 15:04:39 +00:00
match umap.get(binary_as_util) {
2014-07-01 15:04:39 +00:00
Some(&uumain) => {
env::set_exit_status(uumain(args));
2014-07-01 15:04:39 +00:00
return
}
None => (),
}
2014-07-25 05:20:03 +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") {
// uutils can be called as either "uutils", "busybox"
// "uutils-suffix" or "busybox-suffix". Not sure
// what busybox uses the -suffix pattern for.
} else {
println!("{}: applet not found", binary_as_util);
env::set_exit_status(1);
return
2014-05-28 11:43:37 +00:00
}
// try first arg as util name.
if args.len() >= 2 {
2014-07-29 06:52:58 +00:00
args.remove(0);
2014-07-20 01:13:55 +00:00
let util = args[0].as_slice();
2014-07-01 15:04:39 +00:00
match umap.get(util) {
2014-07-01 15:04:39 +00:00
Some(&uumain) => {
env::set_exit_status(uumain(args.clone()));
2014-07-01 15:04:39 +00:00
return
}
None => {
2014-07-20 01:13:55 +00:00
if args[0].as_slice() == "--help" {
2014-07-01 15:04:39 +00:00
// see if they want help on a specific util
if args.len() >= 2 {
2014-07-20 01:13:55 +00:00
let util = args[1].as_slice();
match umap.get(util) {
2014-07-01 15:04:39 +00:00
Some(&uumain) => {
env::set_exit_status(uumain(vec![util.to_string(), "--help".to_string()]));
2014-07-01 15:04:39 +00:00
return
}
None => {
println!("{}: applet not found", util);
env::set_exit_status(1);
2014-07-01 15:04:39 +00:00
return
}
}
}
usage(&umap);
env::set_exit_status(0);
return
} else {
println!("{}: applet not found", util);
env::set_exit_status(1);
return
}
}
2014-05-28 11:43:37 +00:00
}
} else {
// no arguments provided
usage(&umap);
env::set_exit_status(0);
return
2014-05-28 11:43:37 +00:00
}
}