From f104598164591d0d462d8c3dc1e5d07408837b63 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 24 Aug 2024 15:53:58 -0600 Subject: [PATCH] mm(1): updates error-handling functions and uncovers issue with error reporting --- src/mm.rs | 36 +++++++++++++++--------------------- tests/bonsai/mm.mk | 8 +++++++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index 025b0f6..80a9cd7 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -47,20 +47,14 @@ use ArgMode::*; enum ArgMode { In, Out } -fn ioerr(argv0: &String, e: Error) -> ExitCode { +fn err(argv0: &String, e: Error, code: Option) -> ExitCode { eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_IOERR as u8) -} - -#[cfg(target_os="openbsd")] -fn oserr(argv0: &String, e: Error) -> ExitCode { - eprintln!("{}: {}", argv0, e.strerror()); - ExitCode::from(EX_OSERR as u8) + ExitCode::from(code.unwrap_or(1 /* unknown error */)) } fn usage(argv0: &String) -> ExitCode { eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0); - ExitCode::from(EX_USAGE as u8) + ExitCode::from(EX_USAGE) } fn main() -> ExitCode { @@ -69,7 +63,7 @@ fn main() -> ExitCode { #[cfg(target_os="openbsd")] { let promises = Promises::new("cpath rpath stdio unveil wpath"); if let Err(e) = pledge(Some(promises), None) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -124,7 +118,7 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['r']); if let Err(e) = unveil(Some(&input), Some(perms)) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -132,12 +126,12 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['c', 'w']); if let Err(e) = unveil(Some(&output), Some(perms)) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } if let Err(e) = unveil(None, None) { - return oserr(&argv[0], e); + return err(&argv[0], e, Some(EX_OSERR)); } } @@ -162,8 +156,8 @@ fn main() -> ExitCode { match File::open(file) { Ok(f) => f, Err(e) => { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&(argv[0].clone() + ": " + file), e, None); + exit(EX_IOERR.into()); }, } }).collect::>(); @@ -189,8 +183,8 @@ fn main() -> ExitCode { match options { Ok(f) => return f, Err(e) => { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&(argv[0].clone() + ": " + file), e, None); + exit(EX_IOERR.into()); }, }; }).collect::>(); @@ -214,19 +208,19 @@ fn main() -> ExitCode { for file in inputs { for byte in file.bytes().map(|b| { b.unwrap_or_else(|e| { - let _ = ioerr(&argv[0], e); - exit(EX_IOERR); + let _ = err(&argv[0], e, None); + exit(EX_IOERR.into()); }) }) { for out in &mut outputs { if let Err(e) = out.write(&[byte]) { - return ioerr(&argv[0], e); + return err(&argv[0], e, Some(EX_IOERR)); } if u { /* immediately flush the output for -u */ if let Err(e) = out.flush() { - return ioerr(&argv[0], e); + return err(&argv[0], e, Some(EX_IOERR)); } } } diff --git a/tests/bonsai/mm.mk b/tests/bonsai/mm.mk index 124d284..891dae3 100755 --- a/tests/bonsai/mm.mk +++ b/tests/bonsai/mm.mk @@ -6,7 +6,7 @@ # notice are preserved. This file is offered as-is, without any warranty. .PHONY: mm_tests -mm_tests: mm_args mm_help mm_stderr mm_remaining +mm_tests: mm_args mm_help mm_stderr mm_remaining mm_remaining_options .PHONY: mm_none mm_none: $(BIN)/mm @@ -32,3 +32,9 @@ mm_remaining: $(BIN)/mm test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)" $(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1 diff /tmp/mm_test0 /tmp/mm_test1 + +.PHONY: mm_remaining_options +# check to make sure mm -i with trailing arguments interprets -o as one +mm_remaining_options: + ! $(BIN)/mm -i README COPYING -o - 2>&1 | cut -d: -f2 \ + | xargs test " -o" =