optimizations #161

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

View File

@ -18,8 +18,8 @@
use std::{
env::args,
io::{ Read, stdin, stdout, Write },
process::{ Command, exit, Stdio },
io::{ Error, Read, Write, stdin, stdout },
process::{ Command, ExitCode, Stdio, exit },
};
extern crate getopt;
@ -34,7 +34,16 @@ use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
#[cfg(target_os="openbsd")] extern crate openbsd;
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
fn main() {
fn err(argv0: &String, e: Error) {
eprintln!("{}: {}", argv0, e.strerror());
}
fn usage(argv0: &String) -> i32 {
eprintln!("Usage: {} [-d delimiter] index command [args...]", argv0);
EX_USAGE
}
fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
let mut d = '\u{1E}'.to_string(); /* ASCII record separator */
let mut optind = 1;
@ -42,16 +51,11 @@ fn main() {
#[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio proc exec");
if let Err(e) = pledge(Some(promises), None) {
eprintln!("{}: {}", argv[0], e.strerror());
err(&argv[0], e);
exit(EX_OSERR);
}
}
let usage = format!(
"Usage: {} [-d delimiter] index command [args...]",
argv[0],
);
while let Some(opt) = argv.getopt("d:") {
match opt.opt() {
Ok("d") => {
@ -60,8 +64,7 @@ fn main() {
optind = opt.ind();
},
_ => {
eprintln!("{}", usage);
exit(EX_USAGE);
return ExitCode::from(usage(&argv[0]) as u8);
}
};
}
@ -77,14 +80,13 @@ fn main() {
/* argv[0] of the operator command */
let operator = argv.get(command_arg).unwrap_or_else(|| {
eprintln!("{}", usage);
exit(EX_USAGE);
exit(usage(&argv[0]));
});
/* read entire standard input into memory */
let mut buf = String::new();
if let Err(e) = stdin().read_to_string(&mut buf) {
eprintln!("{}: {}", argv[0], e.strerror());
err(&argv[0], e);
exit(EX_IOERR);
};
@ -105,17 +107,13 @@ fn main() {
.stdout(Stdio::piped()) /* piped stdout to handle output ourselves */
.spawn()
.unwrap_or_else( |e| {
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror());
err(&argv[0], e);
exit(EX_UNAVAILABLE);
});
/* 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.to_string(),
);
eprintln!("{}: {}: no such index in input", argv[0], index);
exit(EX_DATAERR);
});
@ -126,7 +124,7 @@ fn main() {
}
let output = spawned.wait_with_output().unwrap_or_else(|e| {
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e.strerror());
err(&argv[0], e);
exit(EX_IOERR);
});
@ -143,7 +141,7 @@ fn main() {
/* convert the output of the program to UTF-8 */
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e);
eprintln!("{}: {}", argv[0], e);
exit(EX_IOERR);
});
@ -154,7 +152,9 @@ fn main() {
stdout().write_all(
fields.join(&d.to_string()).as_bytes()
).unwrap_or_else(|e| {
eprintln!("{}: {}", argv[0], e.strerror());
err(&argv[0], e);
exit(EX_IOERR);
});
ExitCode::SUCCESS
}