coreutils/mkuutils.rs

53 lines
2.3 KiB
Rust
Raw Normal View History

2015-02-03 22:49:03 +00:00
#![feature(core, io, os, path)]
2015-01-10 19:31:55 +00:00
2015-01-29 07:29:31 +00:00
use std::old_io::{File, Truncate, Write};
use std::os;
2015-02-05 22:09:38 +00:00
use std::old_path::Path;
fn main() {
let args = os::args();
if args.len() < 3 {
println!("usage: mkuutils <outfile> <crates>");
os::set_exit_status(1);
return;
}
let mut crates = String::new();
let mut util_map = String::new();
let mut hashsum = false;
for prog in args[2..].iter() {
match prog.as_slice() {
"hashsum" | "md5sum" | "sha1sum" | "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => {
if !hashsum {
crates.push_str("extern crate hashsum;\n");
util_map.push_str("map.insert(\"hashsum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"md5sum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"sha1sum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"sha224sum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"sha256sum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"sha384sum\", hashsum::uumain);\n");
util_map.push_str("map.insert(\"sha512sum\", hashsum::uumain);\n");
hashsum = true;
}
}
2015-01-10 18:07:08 +00:00
"true" => util_map.push_str("fn uutrue(_: Vec<String>) -> isize { 0 }\nmap.insert(\"true\", uutrue);\n"),
"false" => util_map.push_str("fn uufalse(_: Vec<String>) -> isize { 1 }\nmap.insert(\"false\", uufalse);\n"),
_ => {
crates.push_str(format!("extern crate \"{0}\" as uu{0};\n", prog).as_slice());
util_map.push_str(format!("map.insert(\"{prog}\", uu{prog}::uumain as fn(Vec<String>) -> isize);\n", prog = prog).as_slice());
}
}
}
2014-07-20 01:13:55 +00:00
let outfile = args[1].as_slice();
// XXX: this all just assumes that the IO works correctly
let mut out = File::open_mode(&Path::new(outfile), Truncate, Write).unwrap();
2014-07-21 03:20:55 +00:00
let mut input = File::open(&Path::new("src/uutils/uutils.rs")).unwrap();
2014-07-09 08:29:50 +00:00
let main = input.read_to_string().unwrap().replace("@CRATES@", crates.as_slice()).replace("@UTIL_MAP@", util_map.as_slice());
2015-01-29 07:45:08 +00:00
match out.write_all(main.as_bytes()) {
2014-10-30 09:06:47 +00:00
Err(e) => panic!("{}", e),
_ => (),
}
}