optimizations #161

Open
emma wants to merge 44 commits from optimizations into main
Showing only changes of commit 928dce0234 - Show all commits

View File

@ -38,7 +38,7 @@ fn err(argv0: &String, e: Error) {
eprintln!("{}: {}", argv0, e.strerror());
}
fn usage(argv0: &String) -> i32 {
fn usage(argv0: &String) -> u8 {
eprintln!("Usage: {} [-d delimiter] index command [args...]", argv0);
EX_USAGE
}
@ -52,7 +52,7 @@ fn main() -> ExitCode {
let promises = Promises::new("stdio proc exec");
if let Err(e) = pledge(Some(promises), None) {
err(&argv[0], e);
exit(EX_OSERR);
return ExitCode::from(EX_OSERR);
}
}
@ -64,7 +64,7 @@ fn main() -> ExitCode {
optind = opt.ind();
},
_ => {
return ExitCode::from(usage(&argv[0]) as u8);
return ExitCode::from(usage(&argv[0]));
}
};
}
@ -72,7 +72,7 @@ fn main() -> ExitCode {
/* parse the specified index as a number we can use */
let index = argv[optind].parse::<usize>().unwrap_or_else(|e| {
eprintln!("{}: {}: {}", argv[0], argv[1], e);
exit(EX_DATAERR);
exit(EX_DATAERR as i32);
});
/* index of the argv[0] for the operator command */
@ -80,14 +80,14 @@ fn main() -> ExitCode {
/* argv[0] of the operator command */
let operator = argv.get(command_arg).unwrap_or_else(|| {
exit(usage(&argv[0]));
exit(usage(&argv[0]) as i32);
});
/* read entire standard input into memory */
let mut buf = String::new();
if let Err(e) = stdin().read_to_string(&mut buf) {
err(&argv[0], e);
exit(EX_IOERR);
exit(EX_IOERR as i32);
};
/* split the buffer by the delimiter (by default, '\u{1E}') */
@ -108,13 +108,13 @@ fn main() -> ExitCode {
.spawn()
.unwrap_or_else( |e| {
err(&argv[0], e);
exit(EX_UNAVAILABLE);
exit(EX_UNAVAILABLE as i32);
});
/* get field we want to pipe into spawned program */
let field = fields.get(index).unwrap_or_else(|| {
eprintln!("{}: {}: no such index in input", argv[0], index);
exit(EX_DATAERR);
exit(EX_DATAERR as i32);
});
/* get the stdin of the newly spawned program and feed it the field val */
@ -125,7 +125,7 @@ fn main() -> ExitCode {
let output = spawned.wait_with_output().unwrap_or_else(|e| {
err(&argv[0], e);
exit(EX_IOERR);
exit(EX_IOERR as i32);
});
/* get the output with which the original field will be replaced */
@ -142,7 +142,7 @@ fn main() -> ExitCode {
/* convert the output of the program to UTF-8 */
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {
eprintln!("{}: {}", argv[0], e);
exit(EX_IOERR);
exit(EX_IOERR as i32);
});
/* store the new field in the old fields vector */
@ -153,7 +153,7 @@ fn main() -> ExitCode {
fields.join(&d.to_string()).as_bytes()
).unwrap_or_else(|e| {
err(&argv[0], e);
exit(EX_IOERR);
exit(EX_IOERR as i32);
});
ExitCode::SUCCESS