coreutils/uutils/uutils.rs

104 lines
3 KiB
Rust
Raw Normal View History

2014-05-28 11:43:37 +00:00
#![crate_id(name="uutils", vers="1.0.0", author="Michael Gehring")]
/*
* 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::os;
2014-06-12 04:29:16 +00:00
use std::collections::hashmap::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<&str, fn(Vec<String>) -> int> {
fn uutrue(_: Vec<String>) -> int { 0 }
fn uufalse(_: Vec<String>) -> int { 1 }
2014-05-28 11:43:37 +00:00
2014-07-01 09:20:14 +00:00
let mut map = HashMap::<&str, fn(Vec<String>) -> int>::new();
@UTIL_MAP@
2014-05-28 11:43:37 +00:00
map
}
fn usage(cmap: &HashMap<&str, fn(Vec<String>) -> int>) {
2014-06-19 20:05:43 +00:00
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [util [arguments...]", NAME);
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);
}
println!("");
2014-05-28 11:43:37 +00:00
}
fn main() {
let umap = util_map();
let mut args = os::args();
// try binary name as util name.
let binary = Path::new(args.get(0).as_slice());
let binary_as_util = binary.filename_str().unwrap();
if umap.contains_key(&binary_as_util) {
let &uumain = umap.get(&binary_as_util);
os::set_exit_status(uumain(args));
2014-05-28 11:43:37 +00:00
return
} else if binary_as_util.starts_with("uutils")
|| 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);
os::set_exit_status(1);
return
2014-05-28 11:43:37 +00:00
}
// try first arg as util name.
if args.len() >= 2 {
args.shift();
let util = args.get(0).as_slice();
2014-05-28 11:43:37 +00:00
if umap.contains_key(&util) {
let &uumain = umap.get(&util);
os::set_exit_status(uumain(args.clone()));
2014-05-28 11:43:37 +00:00
return
} else if args.get(0).as_slice() == "--help" {
// see if they want help on a specific util
if args.len() >= 2 {
let util = args.get(1).as_slice();
if umap.contains_key(&util) {
let &uumain = umap.get(&util);
os::set_exit_status(uumain(vec!["--help".to_string()]));
return
} else {
println!("{}: applet not found", util);
os::set_exit_status(1);
return
}
}
usage(&umap);
os::set_exit_status(0);
return
} else {
println!("{}: applet not found", util);
os::set_exit_status(1);
return
2014-05-28 11:43:37 +00:00
}
} else {
// no arguments provided
usage(&umap);
os::set_exit_status(0);
return
2014-05-28 11:43:37 +00:00
}
}