1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
pub struct WindowsCommandArgs<'a> {
command: &'a str,
index: usize,
in_first_argument: bool,
arg: String,
}
impl<'a> WindowsCommandArgs<'a> {
pub fn new(command: &'a str) -> WindowsCommandArgs<'a> {
WindowsCommandArgs {
command,
index: 0,
in_first_argument: false,
arg: String::new(),
}
}
pub fn new_with_program(command: &'a str) -> WindowsCommandArgs<'a> {
WindowsCommandArgs {
command,
index: 0,
in_first_argument: true,
arg: String::new(),
}
}
}
impl<'a> Iterator for WindowsCommandArgs<'a> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
let Self {
command,
index,
in_first_argument,
ref mut arg,
} = *self;
if index > command.len() {
return None;
}
arg.clear();
let mut last_char = ' ';
let mut in_quotes = false;
let mut consecutive_quotes = 0;
let mut backslashes = 0_u32;
fn push_backslashes(arg: &mut String, backslashes: &mut u32, half: bool) {
if *backslashes > 0 {
let n = (*backslashes >> (half as u32)) as usize;
arg.reserve(n);
for _ in 0..n {
arg.push('\\');
}
*backslashes = 0;
}
}
fn push_quotes(arg: &mut String, quotes: &mut u32, in_quotes: &mut bool) {
if *quotes > 0 {
let n = (*quotes >> 1) as usize;
let is_even = *quotes & 1 == 0;
arg.reserve(n);
for _ in 0..n {
arg.push('"');
}
*quotes = 0;
*in_quotes = is_even;
}
}
for (index, c) in (&command[index..]).char_indices() {
match c {
'"' if in_first_argument => {
in_quotes = !in_quotes;
}
'"' => {
let is_backslash_escaped = backslashes % 2 == 1;
push_backslashes(arg, &mut backslashes, true);
if is_backslash_escaped {
arg.push(c);
} else if !in_quotes {
in_quotes = true;
} else {
consecutive_quotes += 1;
}
}
'\\' => {
backslashes += 1;
push_quotes(arg, &mut consecutive_quotes, &mut in_quotes);
}
' ' | '\t' if !in_quotes && matches!(last_char, ' ' | '\t') => {}
' ' | '\t' => {
push_backslashes(arg, &mut backslashes, false);
push_quotes(arg, &mut consecutive_quotes, &mut in_quotes);
if in_quotes {
arg.push(c);
} else {
self.index += index + 1;
self.in_first_argument = false;
let mut result = String::with_capacity(arg.len());
result.clone_from(arg);
return Some(result);
}
}
c => {
push_backslashes(arg, &mut backslashes, false);
push_quotes(arg, &mut consecutive_quotes, &mut in_quotes);
arg.push(c);
}
}
last_char = c;
}
push_backslashes(arg, &mut backslashes, false);
push_quotes(arg, &mut consecutive_quotes, &mut in_quotes);
self.index = command.len() + 1;
if arg.is_empty() {
None
} else {
let mut result = std::mem::take(arg);
result.shrink_to_fit();
Some(result)
}
}
}
pub use shlex::join as join_unix_args;
pub use shlex::quote as quote_unix_arg;
pub use shlex::Shlex as UnixCommandArgs;
#[cfg(windows)]
pub type NativeCommandArgs<'a> = WindowsCommandArgs<'a>;
#[cfg(unix)]
pub type NativeCommandArgs<'a> = UnixCommandArgs<'a>;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn separate_windows_args() {
let cmd = r#"C:\path\\\" a a "/\\//^.. "arg with whitespace" 'abc' '"" "'" "''" ""'""" s " """" \\\\"" \\\" \\\\\" \\\abc "rest a b "#;
let args = WindowsCommandArgs::new_with_program(cmd).collect::<Vec<_>>();
let mut iter = args.iter().map(|s| &s[..]);
assert_eq!(iter.next(), Some(r"C:\path\\\ a a /\\//^.."));
assert_eq!(iter.next(), Some("arg with whitespace"));
assert_eq!(iter.next(), Some("'abc'"));
assert_eq!(iter.next(), Some("'"));
assert_eq!(iter.next(), Some("'"));
assert_eq!(iter.next(), Some("''"));
assert_eq!(iter.next(), Some("'\" s "));
assert_eq!(iter.next(), Some("\""));
assert_eq!(iter.next(), Some(r"\\"));
assert_eq!(iter.next(), Some("\\\""));
assert_eq!(iter.next(), Some(r#"\\""#));
assert_eq!(iter.next(), Some(r#"\\\abc"#));
assert_eq!(iter.next(), Some("rest a b "));
assert_eq!(iter.next(), None);
}
}