From e63da0b7e09250caf2a808ec4975087a18858aed Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 2 Jul 2020 02:11:03 -0400 Subject: [PATCH] check if $EDITOR contains flags before calling If it contains flags, split by whitespace and pass them in as arguments to std::process::Command. Otherwise, just pass it in as normal. This was causing a panic/abort. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/ed.rs | 45 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e1f5c90..ec5221b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,7 +103,7 @@ dependencies = [ [[package]] name = "clinte" -version = "2.1.2" +version = "2.1.3" dependencies = [ "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 730199a..9ef7b03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clinte" -version = "2.1.2" +version = "2.1.3" authors = ["Ben Morrison "] edition = "2018" description = "CLI note posting system for shared UNIX boxes." diff --git a/src/ed.rs b/src/ed.rs index 468e008..1a70511 100644 --- a/src/ed.rs +++ b/src/ed.rs @@ -12,6 +12,8 @@ use crate::error; use crate::user; +// Creates a temporary file to call $EDITOR on. Returns the +// path to the file on success. fn create_tmp_file() -> Result { let the_time = Utc::now().to_rfc3339(); let file_name = format!("/tmp/clinte_ed_{}_{}", *user::NAME, the_time); @@ -21,6 +23,7 @@ fn create_tmp_file() -> Result { } } +// This calls $EDITOR and pre-fills it with the provided text. #[cfg(not(test))] pub fn call(body: &str) -> String { // If they don't have $EDITOR set, just default to nano @@ -28,9 +31,9 @@ pub fn call(body: &str) -> String { let editor = match env::var("EDITOR") { Ok(ed) => { if &ed == "" { - "nano".into() + "nano".trim().to_string() } else { - ed + ed.trim().to_string() } } Err(_) => { @@ -46,14 +49,36 @@ pub fn call(body: &str) -> String { "Couldn't populate tempfile with message", ); - error::helper( - process::Command::new(editor) - .arg(&tmp_loc) - .stdin(process::Stdio::inherit()) - .stdout(process::Stdio::inherit()) - .output(), - "Couldn't call editor", - ); + // Check if $EDITOR contains flags. Change the way we call $EDITOR if so, + // because otherwise it will explode. + if editor.contains(" ") { + let ed_split = editor.split_whitespace().collect::>(); + let mut args = vec![]; + ed_split.iter().enumerate().for_each(|(i, e)| { + if i == 0 { + return; + } + args.push(e.to_string()); + }); + args.push(tmp_loc.clone()); + error::helper( + process::Command::new(ed_split[0]) + .args(&args) + .stdin(process::Stdio::inherit()) + .stdout(process::Stdio::inherit()) + .output(), + "Couldn't call editor", + ); + } else { + error::helper( + process::Command::new(editor) + .arg(&tmp_loc) + .stdin(process::Stdio::inherit()) + .stdout(process::Stdio::inherit()) + .output(), + "Couldn't call editor", + ); + } let body = error::helper( fs::read_to_string(&tmp_loc),