1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #195 from ebfe/multicall

Add a multicall binary
This commit is contained in:
Arcterus 2014-05-29 00:03:12 -07:00
commit fce8606697
42 changed files with 331 additions and 80 deletions

View file

@ -58,6 +58,9 @@ BUILD ?= $(PROGS)
EXES := \
$(sort $(filter $(BUILD),$(filter-out $(DONT_BUILD),$(PROGS))))
CRATES := \
$(sort $(filter $(EXES), $(filter-out md5sum true false, $(EXES))))
# Programs with usable tests
TEST_PROGS := \
cat \
@ -89,6 +92,11 @@ clean_$(1):
endif
endef
define CRATE_BUILD
build/$(2): $(1)/$(1).rs
$(call command,$(RUSTC) $(RUSTCFLAGS) --crate-type rlib $(1)/$(1).rs --out-dir build)
endef
# Test exe built rules
define TEST_BUILD
test_$(1): tmp/$(1)_test build build/$(1)
@ -99,7 +107,14 @@ tmp/$(1)_test: $(1)/test.rs
endef
# Main rules
ifneq ($(MULTICALL), 1)
all: build $(EXES_PATHS)
else
all: build build/uutils
build/uutils: uutils/uutils.rs $(addprefix build/, $(foreach crate,$(CRATES),$(shell $(RUSTC) --crate-type rlib --crate-file-name $(crate)/$(crate).rs)))
$(RUSTC) $(RUSTCFLAGS) -L build/ uutils/uutils.rs -o $@
endif
test: tmp $(addprefix test_,$(TESTS))
$(RM) -rf tmp
@ -117,5 +132,8 @@ tmp:
# Creating necessary rules for each targets
$(foreach exe,$(EXES),$(eval $(call EXE_BUILD,$(exe))))
$(foreach test,$(TESTS),$(eval $(call TEST_BUILD,$(test))))
ifeq ($(MULTICALL), 1)
$(foreach crate,$(CRATES),$(eval $(call CRATE_BUILD,$(crate),$(shell $(RUSTC) --crate-type rlib --crate-file-name --out-dir build $(crate)/$(crate).rs))))
endif
.PHONY: all test clean

View file

@ -35,8 +35,7 @@ mod util;
static NAME: &'static str = "base64";
fn main() {
let args = os::args();
pub fn uumain(args: Vec<String>) {
let opts = ~[
optflag("d", "decode", "decode data"),
optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"),
@ -91,6 +90,9 @@ fn main() {
}
}
#[allow(dead_code)]
fn main() { uumain(os::args()); }
fn decode(input: &mut Reader, ignore_garbage: bool) {
let mut to_decode = match input.read_to_str() {
Ok(m) => m,

View file

@ -23,8 +23,10 @@ mod util;
static NAME: &'static str = "basename";
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = strip_dir(args.get(0).as_slice());
//

View file

@ -19,8 +19,10 @@ use std::io::{print, File};
use std::io::stdio::{stdout_raw, stdin_raw};
use std::io::{BufferedWriter};
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = ~[
getopts::optflag("A", "show-all", "equivalent to -vET"),

View file

@ -77,8 +77,10 @@ fn open_file(name: &str) -> IoResult<Box<Reader>> {
}
}
pub fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = [
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),

View file

@ -87,8 +87,10 @@ fn open_file(name: &str) -> IoResult<Box<Buffer>> {
}
}
pub fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = [
getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"),
getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"),

View file

@ -31,8 +31,10 @@ pub enum Mode {
Version,
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = ~[
optflag("h", "help", "display this help and exit"),
optflag("", "version", "output version information and exit"),

View file

@ -16,8 +16,10 @@ use std::io::print;
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("z", "zero", "separate output with NUL rather than newline"),

View file

@ -90,8 +90,10 @@ fn du(path: &Path, mut my_stat: Stat,
return stats;
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = ~[
// In task

View file

@ -69,8 +69,10 @@ fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
return (to_char(&bytes, base), max_digits)
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("n", "", "do not output the trailing newline"),

8
env/env.rs vendored
View file

@ -13,6 +13,8 @@
#![allow(non_camel_case_types)]
use std::os;
struct options {
ignore_env: bool,
null: bool,
@ -51,8 +53,10 @@ fn print_env(null: bool) {
}
}
fn main() {
let args = std::os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let prog = args.get(0).as_slice();
// to handle arguments the same way than GNU env, we can't use getopts

View file

@ -26,9 +26,11 @@ mod util;
static NAME: &'static str = "fold";
static VERSION: &'static str = "1.0.0";
fn main() {
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let args = os::args();
let (args, obs_width) = handle_obsolete(args.as_slice());
let program = args.get(0).clone();

View file

@ -25,8 +25,10 @@ use c_types::{get_pw_from_args, group};
static NAME: &'static str = "groups";
fn main () {
let args = os::args();
#[allow(dead_code)]
fn main () { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let options = [
optflag("h", "", "Help")
];

View file

@ -22,11 +22,14 @@ use getopts::{optopt, optflag, getopts, usage};
static PROGRAM: &'static str = "head";
fn main () {
#[allow(dead_code)]
fn main () { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let mut line_count = 10u;
// handle obsolete -number syntax
let options = match obsolete(os::args().tail()) {
let options = match obsolete(args.tail()) {
(args, Some(n)) => { line_count = n; args },
(args, None) => args
};

View file

@ -49,8 +49,10 @@ extern {
pub fn gethostid() -> c_long;
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = [
optflag("", "help", "display this help and exit"),

View file

@ -23,8 +23,10 @@ extern {
fn sethostname(name: *libc::c_char, namelen: libc::c_int) -> libc::c_int;
}
fn main () {
let args = os::args();
#[allow(dead_code)]
fn main () { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0);
let options = [

View file

@ -87,8 +87,10 @@ extern {
static NAME: &'static str = "id";
fn main () {
let args = os::args();
#[allow(dead_code)]
fn main () { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let args_t = args.tail();
let options = [

View file

@ -52,9 +52,10 @@ pub enum Mode {
Version,
}
#[allow(dead_code)]
fn main() { uumain(os::args()); }
fn main() {
let args = os::args();
pub fn uumain(args: Vec<String>) {
let opts = ~[
optflag("h", "help", "display this help and exit"),

View file

@ -43,8 +43,10 @@ fn version() {
println!("{} {}", NAME, VERSION);
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
//

View file

@ -28,8 +28,10 @@ static VERSION: &'static str = "1.0.0";
/**
* Handles option parsing
*/
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = ~[
// Linux-specific options, not implemented

View file

@ -23,8 +23,10 @@ mod util;
static NAME: &'static str = "paste";
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[

View file

@ -24,8 +24,10 @@ mod util;
static NAME: &'static str = "printenv";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"),

View file

@ -23,8 +23,10 @@ mod util;
static NAME: &'static str = "pwd";
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("", "help", "display this help and exit"),

View file

@ -29,8 +29,10 @@ enum InteractiveMode {
static NAME: &'static str = "rm";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
// TODO: make getopts support -R in addition to -r

View file

@ -22,8 +22,10 @@ mod util;
static NAME: &'static str = "rmdir";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[

View file

@ -33,8 +33,10 @@ fn escape_sequences(s: &str) -> String {
replace("\\t", "\t")
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = ~[
getopts::optopt("s", "separator", "Separator character (defaults to \\n)", ""),
getopts::optopt("t", "terminator", "Terminator character (defaults to separator)", ""),

View file

@ -23,8 +23,10 @@ mod util;
static NAME: &'static str = "sleep";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[

View file

@ -76,8 +76,10 @@ fn open(name: &str) -> IoResult<Box<Reader>> {
}
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = [
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),

View file

@ -23,8 +23,10 @@ mod util;
static NAME: &'static str = "tac";
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[

View file

@ -24,8 +24,11 @@ use getopts::{getopts, optflag, usage};
static NAME: &'static str = "tee";
static VERSION: &'static str = "1.0.0";
fn main() {
match options(args().as_slice()).and_then(exec) {
#[allow(dead_code)]
fn main() { uumain(args()); }
pub fn uumain(args: Vec<String>) {
match options(args.as_slice()).and_then(exec) {
Ok(_) => set_exit_status(0),
Err(_) => set_exit_status(1)
}

View file

@ -22,8 +22,10 @@ mod util;
static NAME: &'static str = "touch";
static VERSION: &'static str = "1.0.0";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = ~[
getopts::optflag("a", "", "change only the access time"),
getopts::optflag("c", "no-create", "do not create any files"),

View file

@ -146,8 +146,10 @@ fn usage(opts: &[OptGroup]) {
print(getopts::usage("Translate or delete characters.", opts).as_slice());
}
pub fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let opts = [
getopts::optflag("c", "complement", "use the complement of SET1"),
getopts::optflag("C", "", "same as -c"),

View file

@ -46,8 +46,10 @@ enum TruncateMode {
static NAME: &'static str = "truncate";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[

View file

@ -34,9 +34,10 @@ extern {
static NAME: &'static str = "tty";
fn main () {
let args = os::args();
#[allow(dead_code)]
fn main () { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let options = [
optflag("s", "silent", "print nothing, only return an exit status")
];

View file

@ -51,8 +51,10 @@ unsafe fn getuname() -> utsrust {
static NAME: &'static str = "uname";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = ~[
getopts::optflag("h", "help", "display this help and exit"),

View file

@ -26,8 +26,10 @@ mod util;
static NAME: &'static str = "unlink";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("h", "help", "display this help and exit"),

View file

@ -47,8 +47,10 @@ extern {
fn utmpxname(file: *c_char) -> c_int;
}
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("v", "version", "output version information and exit"),

View file

@ -47,8 +47,10 @@ extern {
static NAME: &'static str = "users";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = ~[
getopts::optflag("h", "help", "display this help and exit"),

151
uutils/uutils.rs Normal file
View file

@ -0,0 +1,151 @@
#![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 collections;
extern crate getopts;
extern crate base64;
extern crate basename;
extern crate cat;
extern crate cksum;
extern crate comm;
extern crate cp;
extern crate dirname;
extern crate du;
extern crate echo;
extern crate env;
extern crate fold;
extern crate groups;
extern crate head;
extern crate hostid;
extern crate hostname;
extern crate id;
extern crate kill;
extern crate logname;
extern crate mkdir;
extern crate paste;
extern crate printenv;
extern crate pwd;
extern crate rm;
extern crate rmdir;
extern crate seq;
extern crate sleep;
extern crate sum;
extern crate tac;
extern crate tee;
extern crate touch;
extern crate tr;
extern crate truncate;
extern crate tty;
extern crate uname;
extern crate unlink;
extern crate uptime;
extern crate users;
extern crate wc;
extern crate whoami;
extern crate yes;
use std::os;
use collections::hashmap::HashMap;
static NAME: &'static str = "uutils";
static VERSION: &'static str = "1.0.0";
fn util_map() -> HashMap<&str, fn(Vec<String>)> {
fn uutrue(_: Vec<String>) { os::set_exit_status(0); }
fn uufalse(_: Vec<String>) { os::set_exit_status(1); }
let mut map = HashMap::new();
map.insert("base64", base64::uumain);
map.insert("basename", basename::uumain);
map.insert("cat", cat::uumain);
map.insert("cksum", cksum::uumain);
map.insert("comm", comm::uumain);
map.insert("cp", cp::uumain);
map.insert("dirname", dirname::uumain);
map.insert("du", du::uumain);
map.insert("echo", echo::uumain);
map.insert("env", env::uumain);
map.insert("false", uufalse);
map.insert("fold", fold::uumain);
map.insert("groups", groups::uumain);
map.insert("head", head::uumain);
map.insert("hostid", hostid::uumain);
map.insert("hostname", hostname::uumain);
map.insert("id", id::uumain);
map.insert("kill", kill::uumain);
map.insert("logname", logname::uumain);
map.insert("mkdir", mkdir::uumain);
map.insert("paste", paste::uumain);
map.insert("printenv", printenv::uumain);
map.insert("pwd", pwd::uumain);
map.insert("rm", rm::uumain);
map.insert("rmdir", rmdir::uumain);
map.insert("seq", seq::uumain);
map.insert("sleep", sleep::uumain);
map.insert("sum", sum::uumain);
map.insert("tac", tac::uumain);
map.insert("tee", tee::uumain);
map.insert("touch", touch::uumain);
map.insert("tr", tr::uumain);
map.insert("true", uutrue);
map.insert("truncate", truncate::uumain);
map.insert("tty", tty::uumain);
map.insert("uname", uname::uumain);
map.insert("unlink", unlink::uumain);
map.insert("uptime", uptime::uumain);
map.insert("users", users::uumain);
map.insert("wc", wc::uumain);
map.insert("whoami", whoami::uumain);
map.insert("yes", yes::uumain);
map
}
fn usage(cmap: &HashMap<&str, fn(Vec<String>)>) {
println!("{} {}", NAME, VERSION);
println!("");
println!("Usage:");
println!(" {} [util [arguments...]", NAME);
println!("Utils:");
let mut utils: Vec<&str> = cmap.keys().map(|&s| s).collect();
utils.sort();
for util in utils.iter() {
println!("\t{}", util);
}
}
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 util = binary.filename_str().unwrap();
if umap.contains_key(&util) {
let &uumain = umap.get(&util);
uumain(args);
return
}
// try first arg as util name.
if args.len() >= 2 {
args.shift();
let util = args.get(0).as_slice().clone();
if umap.contains_key(&util) {
let &uumain = umap.get(&util);
uumain(args.clone());
return
}
}
usage(&umap);
os::set_exit_status(1);
}

View file

@ -33,8 +33,10 @@ struct Result {
static NAME: &'static str = "wc";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("c", "bytes", "print the byte counts"),

View file

@ -41,8 +41,10 @@ unsafe fn getusername() -> String {
static NAME: &'static str = "whoami";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).as_slice();
let opts = ~[
getopts::optflag("h", "help", "display this help and exit"),

View file

@ -24,8 +24,10 @@ mod util;
static NAME: &'static str = "yes";
fn main() {
let args = os::args();
#[allow(dead_code)]
fn main() { uumain(os::args()); }
pub fn uumain(args: Vec<String>) {
let program = args.get(0).clone();
let opts = ~[
getopts::optflag("h", "help", "display this help and exit"),