1 #![allow(dead_code)] // runtime init functions not used during testing
3 use crate::os::windows::prelude::*;
4 use crate::sys::windows::os::current_exe;
6 use crate::ffi::OsString;
10 use crate::path::PathBuf;
14 pub unsafe fn init(_argc: isize, _argv: *const *const u8) { }
16 pub unsafe fn cleanup() { }
18 pub fn args() -> Args {
20 let lp_cmd_line = c::GetCommandLineW();
21 let parsed_args_list = parse_lp_cmd_line(
22 lp_cmd_line as *const u16,
23 || current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new()));
25 Args { parsed_args_list: parsed_args_list.into_iter() }
29 /// Implements the Windows command-line argument parsing algorithm.
31 /// Microsoft's documentation for the Windows CLI argument format can be found at
32 /// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.
34 /// Windows includes a function to do this in shell32.dll,
35 /// but linking with that DLL causes the process to be registered as a GUI application.
36 /// GUI applications add a bunch of overhead, even if no windows are drawn. See
37 /// <https://randomascii.wordpress.com/2018/12/03/a-not-called-function-can-cause-a-5x-slowdown/>.
39 /// This function was tested for equivalence to the shell32.dll implementation in
40 /// Windows 10 Pro v1803, using an exhaustive test suite available at
41 /// <https://gist.github.com/notriddle/dde431930c392e428055b2dc22e638f5> or
42 /// <https://paste.gg/p/anonymous/47d6ed5f5bd549168b1c69c799825223>.
43 unsafe fn parse_lp_cmd_line<F: Fn() -> OsString>(lp_cmd_line: *const u16, exe_name: F)
45 const BACKSLASH: u16 = '\\' as u16;
46 const QUOTE: u16 = '"' as u16;
47 const TAB: u16 = '\t' as u16;
48 const SPACE: u16 = ' ' as u16;
49 let mut ret_val = Vec::new();
50 if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
51 ret_val.push(exe_name());
56 while *lp_cmd_line.offset(end) != 0 {
59 slice::from_raw_parts(lp_cmd_line, end as usize)
61 // The executable name at the beginning is special.
62 cmd_line = match cmd_line[0] {
63 // The executable name ends at the next quote mark,
67 let mut cut = cmd_line[1..].splitn(2, |&c| c == QUOTE);
68 if let Some(exe) = cut.next() {
69 ret_val.push(OsString::from_wide(exe));
73 if let Some(args) = args {
79 // Implement quirk: when they say whitespace here,
80 // they include the entire ASCII control plane:
81 // "However, if lpCmdLine starts with any amount of whitespace, CommandLineToArgvW
82 // will consider the first argument to be an empty string. Excess whitespace at the
83 // end of lpCmdLine is ignored."
85 ret_val.push(OsString::new());
88 // The executable name ends at the next whitespace,
92 let mut cut = cmd_line.splitn(2, |&c| c > 0 && c <= SPACE);
93 if let Some(exe) = cut.next() {
94 ret_val.push(OsString::from_wide(exe));
98 if let Some(args) = args {
105 let mut cur = Vec::new();
106 let mut in_quotes = false;
107 let mut was_in_quotes = false;
108 let mut backslash_count: usize = 0;
113 backslash_count += 1;
114 was_in_quotes = false;
116 QUOTE if backslash_count % 2 == 0 => {
117 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
120 cur.push('"' as u16);
121 was_in_quotes = false;
123 was_in_quotes = in_quotes;
124 in_quotes = !in_quotes;
127 QUOTE if backslash_count % 2 != 0 => {
128 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
130 was_in_quotes = false;
131 cur.push(b'"' as u16);
133 SPACE | TAB if !in_quotes => {
134 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
135 if !cur.is_empty() || was_in_quotes {
136 ret_val.push(OsString::from_wide(&cur[..]));
140 was_in_quotes = false;
143 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
145 was_in_quotes = false;
150 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
151 // include empty quoted strings at the end of the arguments list
152 if !cur.is_empty() || was_in_quotes || in_quotes {
153 ret_val.push(OsString::from_wide(&cur[..]));
159 parsed_args_list: vec::IntoIter<OsString>,
162 pub struct ArgsInnerDebug<'a> {
166 impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 self.args.parsed_args_list.as_slice().fmt(f)
173 pub fn inner_debug(&self) -> ArgsInnerDebug<'_> {
180 impl Iterator for Args {
181 type Item = OsString;
182 fn next(&mut self) -> Option<OsString> { self.parsed_args_list.next() }
183 fn size_hint(&self) -> (usize, Option<usize>) { self.parsed_args_list.size_hint() }
186 impl DoubleEndedIterator for Args {
187 fn next_back(&mut self) -> Option<OsString> { self.parsed_args_list.next_back() }
190 impl ExactSizeIterator for Args {
191 fn len(&self) -> usize { self.parsed_args_list.len() }
196 use crate::sys::windows::args::*;
197 use crate::ffi::OsString;
199 fn chk(string: &str, parts: &[&str]) {
200 let mut wide: Vec<u16> = OsString::from(string).encode_wide().collect();
202 let parsed = unsafe {
203 parse_lp_cmd_line(wide.as_ptr() as *const u16, || OsString::from("TEST.EXE"))
205 let expected: Vec<OsString> = parts.iter().map(|k| OsString::from(k)).collect();
206 assert_eq!(parsed.as_slice(), expected.as_slice());
211 chk("", &["TEST.EXE"]);
212 chk("\0", &["TEST.EXE"]);
217 chk("EXE one_word", &["EXE", "one_word"]);
218 chk("EXE a", &["EXE", "a"]);
219 chk("EXE 😅", &["EXE", "😅"]);
220 chk("EXE 😅🤦", &["EXE", "😅🤦"]);
224 fn official_examples() {
225 chk(r#"EXE "abc" d e"#, &["EXE", "abc", "d", "e"]);
226 chk(r#"EXE a\\\b d"e f"g h"#, &["EXE", r#"a\\\b"#, "de fg", "h"]);
227 chk(r#"EXE a\\\"b c d"#, &["EXE", r#"a\"b"#, "c", "d"]);
228 chk(r#"EXE a\\\\"b c" d e"#, &["EXE", r#"a\\b c"#, "d", "e"]);
232 fn whitespace_behavior() {
233 chk(r#" test"#, &["", "test"]);
234 chk(r#" test"#, &["", "test"]);
235 chk(r#" test test2"#, &["", "test", "test2"]);
236 chk(r#" test test2"#, &["", "test", "test2"]);
237 chk(r#"test test2 "#, &["test", "test2"]);
238 chk(r#"test test2 "#, &["test", "test2"]);
239 chk(r#"test "#, &["test"]);
244 chk(r#"EXE "" """#, &["EXE", "", ""]);
245 chk(r#"EXE "" """"#, &["EXE", "", "\""]);
247 r#"EXE "this is """all""" in the same argument""#,
248 &["EXE", "this is \"all\" in the same argument"]
250 chk(r#"EXE "a"""#, &["EXE", "a\""]);
251 chk(r#"EXE "a"" a"#, &["EXE", "a\"", "a"]);
252 // quotes cannot be escaped in command names
253 chk(r#""EXE" check"#, &["EXE", "check"]);
254 chk(r#""EXE check""#, &["EXE check"]);
255 chk(r#""EXE """for""" check"#, &["EXE ", r#"for""#, "check"]);
256 chk(r#""EXE \"for\" check"#, &[r#"EXE \"#, r#"for""#, "check"]);