Merge pull request #333 from ebfe/uutils

uutils: fix hashmap lifetime
This commit is contained in:
Arcterus 2014-07-01 13:11:38 -07:00
commit 8328ba2e0a

View file

@ -19,16 +19,16 @@ use std::collections::hashmap::HashMap;
static NAME: &'static str = "uutils"; static NAME: &'static str = "uutils";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
fn util_map() -> HashMap<&str, fn(Vec<String>) -> int> { fn util_map() -> HashMap<&'static str, fn(Vec<String>) -> int> {
fn uutrue(_: Vec<String>) -> int { 0 } fn uutrue(_: Vec<String>) -> int { 0 }
fn uufalse(_: Vec<String>) -> int { 1 } fn uufalse(_: Vec<String>) -> int { 1 }
let mut map = HashMap::<&str, fn(Vec<String>) -> int>::new(); let mut map = HashMap::new();
@UTIL_MAP@ @UTIL_MAP@
map map
} }
fn usage(cmap: &HashMap<&str, fn(Vec<String>) -> int>) { fn usage(cmap: &HashMap<&'static str, fn(Vec<String>) -> int>) {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
println!(""); println!("");
println!("Usage:"); println!("Usage:");
@ -49,11 +49,16 @@ fn main() {
// try binary name as util name. // try binary name as util name.
let binary = Path::new(args.get(0).as_slice()); let binary = Path::new(args.get(0).as_slice());
let binary_as_util = binary.filename_str().unwrap(); let binary_as_util = binary.filename_str().unwrap();
if umap.contains_key(&binary_as_util) {
let &uumain = umap.get(&binary_as_util); match umap.find_equiv(&binary_as_util) {
os::set_exit_status(uumain(args)); Some(&uumain) => {
return os::set_exit_status(uumain(args));
} else if binary_as_util.starts_with("uutils") return
}
None => (),
}
if binary_as_util.starts_with("uutils")
|| binary_as_util.starts_with("busybox") { || binary_as_util.starts_with("busybox") {
// uutils can be called as either "uutils", "busybox" // uutils can be called as either "uutils", "busybox"
// "uutils-suffix" or "busybox-suffix". Not sure // "uutils-suffix" or "busybox-suffix". Not sure
@ -68,17 +73,31 @@ fn main() {
if args.len() >= 2 { if args.len() >= 2 {
args.shift(); args.shift();
let util = args.get(0).as_slice(); let util = args.get(0).as_slice();
if umap.contains_key(&util) {
let &uumain = umap.get(&util); match umap.find_equiv(&util) {
os::set_exit_status(uumain(args.clone())); Some(&uumain) => {
return os::set_exit_status(uumain(args.clone()));
} else if args.get(0).as_slice() == "--help" { return
// see if they want help on a specific util }
if args.len() >= 2 { None => {
let util = args.get(1).as_slice(); if args.get(0).as_slice() == "--help" {
if umap.contains_key(&util) { // see if they want help on a specific util
let &uumain = umap.get(&util); if args.len() >= 2 {
os::set_exit_status(uumain(vec!["--help".to_string()])); let util = args.get(1).as_slice();
match umap.find_equiv(&util) {
Some(&uumain) => {
os::set_exit_status(uumain(vec![util.to_string(), "--help".to_string()]));
return
}
None => {
println!("{}: applet not found", util);
os::set_exit_status(1);
return
}
}
}
usage(&umap);
os::set_exit_status(0);
return return
} else { } else {
println!("{}: applet not found", util); println!("{}: applet not found", util);
@ -86,13 +105,6 @@ fn main() {
return return
} }
} }
usage(&umap);
os::set_exit_status(0);
return
} else {
println!("{}: applet not found", util);
os::set_exit_status(1);
return
} }
} else { } else {
// no arguments provided // no arguments provided