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