Skip to content

Fix argument tokenization #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "README.md"


[dependencies]
comma = "1.0"
nix = "0.14"
regex = "1"
tempfile = "3"
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ pub enum Error {

#[error(transparent)]
Regex(#[from] regex::Error),

#[error("The provided program arguments cannot be parsed")]
BadProgramArguments,
}
25 changes: 13 additions & 12 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,8 @@ impl PtySession {

/// Turn e.g. "prog arg1 arg2" into ["prog", "arg1", "arg2"]
/// Also takes care of single and double quotes
fn tokenize_command(program: &str) -> Vec<String> {
let re = Regex::new(r#""[^"]+"|'[^']+'|[^'" ]+"#).unwrap();
let mut res = vec![];
for cap in re.captures_iter(program) {
res.push(cap[0].to_string());
}
res
fn tokenize_command(program: &str) -> Result<Vec<String>, Error> {
comma::parse_command(program).ok_or(Error::BadProgramArguments)
}

/// Start command in background in a pty session (pty fork) and return a struct
Expand All @@ -237,7 +232,7 @@ pub fn spawn(program: &str, timeout_ms: Option<u64>) -> Result<PtySession, Error
return Err(Error::EmptyProgramName);
}

let mut parts = tokenize_command(program);
let mut parts = tokenize_command(program)?;
let prog = parts.remove(0);
let mut command = Command::new(prog);
command.args(parts);
Expand Down Expand Up @@ -579,15 +574,21 @@ mod tests {
#[test]
fn test_tokenize_command() {
let res = tokenize_command("prog arg1 arg2");
assert_eq!(vec!["prog", "arg1", "arg2"], res);
assert_eq!(vec!["prog", "arg1", "arg2"], res.unwrap());

let res = tokenize_command("prog -k=v");
assert_eq!(vec!["prog", "-k=v"], res);
assert_eq!(vec!["prog", "-k=v"], res.unwrap());

let res = tokenize_command("prog 'my text'");
assert_eq!(vec!["prog", "'my text'"], res);
assert_eq!(vec!["prog", "my text"], res.unwrap());

let res = tokenize_command(r#"prog "my text""#);
assert_eq!(vec!["prog", r#""my text""#], res);
assert_eq!(vec!["prog", r#"my text"#], res.unwrap());

let res = tokenize_command(r#"prog "my text with quote'""#);
assert_eq!(vec!["prog", r#"my text with quote'"#], res.unwrap());

let res = tokenize_command(r#"prog "my broken text"#);
assert!(res.is_err());
}
}