From f8e3013563be18eaf7e8c58ef42041549dbd5fe9 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 14 Feb 2024 22:53:45 -0700 Subject: [PATCH 1/4] fop(1): fixed trimming and handled unwraps (closes #58) --- src/fop.rs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 4f80bd6..04eec7e 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -26,7 +26,7 @@ extern crate sysexits; extern crate getopt; use getopt::{ Opt, Parser }; -use sysexits::{ EX_DATAERR, EX_USAGE }; +use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE }; fn main() { let argv = args().collect::>(); @@ -60,17 +60,24 @@ fn main() { }); let mut buf = String::new(); - stdin().read_to_string(&mut buf).unwrap(); + let _ = stdin().read_to_string(&mut buf); let mut fields = buf.split(d).collect::>(); - let opts = argv.iter().clone().skip(command_arg + 1).collect::>(); + let opts = argv + .iter() + .clone() + .skip(command_arg + 1) + .collect::>(); let mut spawned = Command::new(argv.get(command_arg).unwrap()) .args(opts) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .unwrap(); + .unwrap_or_else( |_| { + eprintln!("{}: {}: Command not found.", argv[0], argv[command_arg]); + exit(EX_UNAVAILABLE); + }); let field = fields.get(index).unwrap_or_else(|| { eprintln!( @@ -82,15 +89,28 @@ fn main() { }); if let Some(mut child_stdin) = spawned.stdin.take() { - child_stdin.write_all(field.as_bytes()).unwrap(); + let _ = child_stdin.write_all(field.as_bytes()); drop(child_stdin); } - let output = spawned.wait_with_output().unwrap(); + let output = spawned.wait_with_output().unwrap_or_else(|| { + eprintln!("{}: {}: Command has no output.", + argv[0], + argv[command_arg] + ); + exit(EX_IOERR); + }); - let new_field = String::from_utf8(output.stdout).unwrap(); + let new_field = String::from_utf8(output.stdout).unwrap_or_else(|_| { + eprintln!( + "{}: {}: Command output is invalid UTF-8.", + argv[0], + argv[command_arg] + ); + exit(EX_IOERR); + }); - fields[index] = &new_field; + fields[index] = &new_field.trim_end(); print!("{}", fields.join(&d.to_string())); } From 4c663bf9dd16c2c6ee1bbbd2f4cc04584e826ddb Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 14 Feb 2024 22:55:28 -0700 Subject: [PATCH 2/4] fop(1): closure fix --- src/fop.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fop.rs b/src/fop.rs index 04eec7e..ba99ae4 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -93,7 +93,7 @@ fn main() { drop(child_stdin); } - let output = spawned.wait_with_output().unwrap_or_else(|| { + let output = spawned.wait_with_output().unwrap_or_else(|_| { eprintln!("{}: {}: Command has no output.", argv[0], argv[command_arg] From 448211bbe293856845ca6915978e2d0b251435c7 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 14 Feb 2024 23:05:39 -0700 Subject: [PATCH 3/4] fop(1): better newline removal (preserves existing newlines) --- src/fop.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index ba99ae4..09cac94 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -101,7 +101,11 @@ fn main() { exit(EX_IOERR); }); - let new_field = String::from_utf8(output.stdout).unwrap_or_else(|_| { + let mut replace = output.stdout.clone(); + + if replace.pop() != Some(b'\n') { replace = output.stdout; } + + let new_field = String::from_utf8(replace).unwrap_or_else(|_| { eprintln!( "{}: {}: Command output is invalid UTF-8.", argv[0], @@ -110,7 +114,7 @@ fn main() { exit(EX_IOERR); }); - fields[index] = &new_field.trim_end(); + fields[index] = &new_field; print!("{}", fields.join(&d.to_string())); } From 3cee3de9005fe071530629c8f56dad69182143d1 Mon Sep 17 00:00:00 2001 From: emma Date: Fri, 23 Feb 2024 21:46:50 -0700 Subject: [PATCH 4/4] fop(1): committing to Rust error messages --- src/fop.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 09cac94..c9a767e 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -18,7 +18,7 @@ use std::{ env::args, - io::{ Read, stdin, Write }, + io::{ Read, stdin, stdout, Write }, process::{ Command, exit, Stdio }, }; @@ -54,8 +54,8 @@ fn main() { exit(EX_USAGE); }); - let index = argv[index_arg].parse::().unwrap_or_else(|_| { - eprintln!("{}: {}: Not an integer.", argv[0], argv[1]); + let index = argv[index_arg].parse::().unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[1], e); exit(EX_DATAERR); }); @@ -74,8 +74,8 @@ fn main() { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .unwrap_or_else( |_| { - eprintln!("{}: {}: Command not found.", argv[0], argv[command_arg]); + .unwrap_or_else( |e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_UNAVAILABLE); }); @@ -83,7 +83,7 @@ fn main() { eprintln!( "{}: {}: No such index in input.", argv[0], - index.to_string() + index.to_string(), ); exit(EX_DATAERR); }); @@ -93,11 +93,8 @@ fn main() { drop(child_stdin); } - let output = spawned.wait_with_output().unwrap_or_else(|_| { - eprintln!("{}: {}: Command has no output.", - argv[0], - argv[command_arg] - ); + let output = spawned.wait_with_output().unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_IOERR); }); @@ -105,16 +102,17 @@ fn main() { if replace.pop() != Some(b'\n') { replace = output.stdout; } - let new_field = String::from_utf8(replace).unwrap_or_else(|_| { - eprintln!( - "{}: {}: Command output is invalid UTF-8.", - argv[0], - argv[command_arg] - ); + let new_field = String::from_utf8(replace).unwrap_or_else(|e| { + eprintln!("{}: {}: {}.", argv[0], argv[command_arg], e); exit(EX_IOERR); }); fields[index] = &new_field; - print!("{}", fields.join(&d.to_string())); + stdout().write_all( + fields.join(&d.to_string()).as_bytes() + ).unwrap_or_else(|e|{ + eprintln!("{}: {}.", argv[0], e); + exit(EX_IOERR); + }); }