From a5212a7a88f845550461b7691337a14ae9643e42 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Thu, 4 Apr 2024 10:52:36 +0200 Subject: [PATCH] term: Fix editor command with args We add support for `EDITOR` being set to a command with arguments, eg. `code --wait`. Fixes dd6861f. --- Cargo.lock | 1 + radicle-term/Cargo.toml | 1 + radicle-term/src/editor.rs | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 5c925f61..21453e8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2595,6 +2595,7 @@ dependencies = [ "libc", "once_cell", "pretty_assertions", + "shlex", "tempfile", "termion 3.0.0", "unicode-display-width", diff --git a/radicle-term/Cargo.toml b/radicle-term/Cargo.toml index 5be3bc6d..b48dd326 100644 --- a/radicle-term/Cargo.toml +++ b/radicle-term/Cargo.toml @@ -17,6 +17,7 @@ anstyle-query = { version = "1.0.0" } inquire = { version = "0.6.2", default-features = false, features = ["termion", "editor"] } libc = { version = "0.2" } once_cell = { version = "1.13" } +shlex = { version = "1.1" } termion = { version = "3" } unicode-display-width = { version = "0.3.0" } unicode-segmentation = { version = "1.7.1" } diff --git a/radicle-term/src/editor.rs b/radicle-term/src/editor.rs index df19d2b8..c4e7bab0 100644 --- a/radicle-term/src/editor.rs +++ b/radicle-term/src/editor.rs @@ -69,6 +69,18 @@ impl Editor { "editor not configured: the `EDITOR` environment variable is not set", )); }; + let Some(parts) = shlex::split(cmd.to_string_lossy().as_ref()) else { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("invalid editor command {cmd:?}"), + )); + }; + let Some((program, args)) = parts.split_first() else { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("invalid editor command {cmd:?}"), + )); + }; // We duplicate the stderr file descriptor to pass it to the child process, otherwise, if // we simply pass the `RawFd` of our stderr, `Command` will close our stderr when the @@ -84,10 +96,11 @@ impl Editor { process::Stdio::from(tty) }; - process::Command::new(&cmd) + process::Command::new(program) .stdout(unsafe { process::Stdio::from_raw_fd(stderr) }) .stderr(process::Stdio::inherit()) .stdin(stdin) + .args(args) .arg(&self.path) .spawn() .map_err(|e| {