Skip to content
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

Fix shellwords delimiter handling #4098

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Changes from 1 commit
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
Next Next commit
Fix shellwords delimiter handling
This allows commands such as `:set statusline.center ["file-type"]` to
work. Before the quotes within the list would mess it up.
Also added a test to ensure correct behavior
  • Loading branch information
A-Walrus committed Oct 4, 2022
commit 4bcbacda234717ccca0320962748081e382691fd
63 changes: 47 additions & 16 deletions helix-core/src/shellwords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::borrow::Cow;
/// Get the vec of escaped / quoted / doublequoted filenames from the input str
pub fn shellwords(input: &str) -> Vec<Cow<'_, str>> {
enum State {
Normal,
NormalEscaped,
Delimiter, // whitespace
A-Walrus marked this conversation as resolved.
Show resolved Hide resolved
Unquoted,
UnquotedEscaped,
Quoted,
QuoteEscaped,
Dquoted,
Expand All @@ -13,7 +14,7 @@ pub fn shellwords(input: &str) -> Vec<Cow<'_, str>> {

use State::*;

let mut state = Normal;
let mut state = Unquoted;
let mut args: Vec<Cow<str>> = Vec::new();
let mut escaped = String::with_capacity(input.len());

Expand All @@ -22,31 +23,47 @@ pub fn shellwords(input: &str) -> Vec<Cow<'_, str>> {

for (i, c) in input.char_indices() {
state = match state {
Normal => match c {
Delimiter => match c {
'"' => {
end = i;
Dquoted
}
'\'' => {
end = i;
Quoted
}
'\\' => {
if cfg!(unix) {
escaped.push_str(&input[start..i]);
start = i + 1;
NormalEscaped
UnquotedEscaped
} else {
Normal
Delimiter
}
}
'"' => {
c if c.is_ascii_whitespace() => {
end = i;
Dquoted
Delimiter
}
'\'' => {
end = i;
Quoted
_ => Unquoted,
},
Unquoted => match c {
'\\' => {
if cfg!(unix) {
escaped.push_str(&input[start..i]);
start = i + 1;
UnquotedEscaped
} else {
Unquoted
}
}
c if c.is_ascii_whitespace() => {
end = i;
Normal
Delimiter
}
_ => Normal,
_ => Unquoted,
},
NormalEscaped => Normal,
UnquotedEscaped => Unquoted,
Quoted => match c {
'\\' => {
if cfg!(unix) {
Expand All @@ -59,7 +76,7 @@ pub fn shellwords(input: &str) -> Vec<Cow<'_, str>> {
}
'\'' => {
end = i;
Normal
Delimiter
}
_ => Quoted,
},
Expand All @@ -76,7 +93,7 @@ pub fn shellwords(input: &str) -> Vec<Cow<'_, str>> {
}
'"' => {
end = i;
Normal
Delimiter
}
_ => Dquoted,
},
Expand Down Expand Up @@ -195,4 +212,18 @@ mod test {
];
assert_eq!(expected, result);
}

#[test]
fn test_lists() {
let input =
r#":set statusline.center ["file-type","file-encoding"] '["list", "in", "qoutes"]'"#;
let result = shellwords(input);
let expected = vec![
Cow::from(":set"),
Cow::from("statusline.center"),
Cow::from(r#"["file-type","file-encoding"]"#),
Cow::from(r#"["list", "in", "qoutes"]"#),
];
assert_eq!(expected, result);
}
}