1 #![allow(dead_code)] // runtime init functions not used during testing
3 use crate::ffi::OsString;
5 use crate::os::windows::prelude::*;
6 use crate::path::PathBuf;
9 use crate::sys::windows::os::current_exe;
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(lp_cmd_line as *const u16, || {
22 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>(
44 lp_cmd_line: *const u16,
47 const BACKSLASH: u16 = '\\' as u16;
48 const QUOTE: u16 = '"' as u16;
49 const TAB: u16 = '\t' as u16;
50 const SPACE: u16 = ' ' as u16;
51 let mut ret_val = Vec::new();
52 if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
53 ret_val.push(exe_name());
58 while *lp_cmd_line.offset(end) != 0 {
61 slice::from_raw_parts(lp_cmd_line, end as usize)
63 // The executable name at the beginning is special.
64 cmd_line = match cmd_line[0] {
65 // The executable name ends at the next quote mark,
69 let mut cut = cmd_line[1..].splitn(2, |&c| c == QUOTE);
70 if let Some(exe) = cut.next() {
71 ret_val.push(OsString::from_wide(exe));
75 if let Some(args) = args {
81 // Implement quirk: when they say whitespace here,
82 // they include the entire ASCII control plane:
83 // "However, if lpCmdLine starts with any amount of whitespace, CommandLineToArgvW
84 // will consider the first argument to be an empty string. Excess whitespace at the
85 // end of lpCmdLine is ignored."
87 ret_val.push(OsString::new());
90 // The executable name ends at the next whitespace,
94 let mut cut = cmd_line.splitn(2, |&c| c > 0 && c <= SPACE);
95 if let Some(exe) = cut.next() {
96 ret_val.push(OsString::from_wide(exe));
100 if let Some(args) = args {
107 let mut cur = Vec::new();
108 let mut in_quotes = false;
109 let mut was_in_quotes = false;
110 let mut backslash_count: usize = 0;
115 backslash_count += 1;
116 was_in_quotes = false;
118 QUOTE if backslash_count % 2 == 0 => {
119 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
122 cur.push('"' as u16);
123 was_in_quotes = false;
125 was_in_quotes = in_quotes;
126 in_quotes = !in_quotes;
129 QUOTE if backslash_count % 2 != 0 => {
130 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
132 was_in_quotes = false;
133 cur.push(b'"' as u16);
135 SPACE | TAB if !in_quotes => {
136 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
137 if !cur.is_empty() || was_in_quotes {
138 ret_val.push(OsString::from_wide(&cur[..]));
142 was_in_quotes = false;
145 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
147 was_in_quotes = false;
152 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
153 // include empty quoted strings at the end of the arguments list
154 if !cur.is_empty() || was_in_quotes || in_quotes {
155 ret_val.push(OsString::from_wide(&cur[..]));
161 parsed_args_list: vec::IntoIter<OsString>,
164 pub struct ArgsInnerDebug<'a> {
168 impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 self.args.parsed_args_list.as_slice().fmt(f)
175 pub fn inner_debug(&self) -> ArgsInnerDebug<'_> {
176 ArgsInnerDebug { args: self }
180 impl Iterator for Args {
181 type Item = OsString;
182 fn next(&mut self) -> Option<OsString> {
183 self.parsed_args_list.next()
185 fn size_hint(&self) -> (usize, Option<usize>) {
186 self.parsed_args_list.size_hint()
190 impl DoubleEndedIterator for Args {
191 fn next_back(&mut self) -> Option<OsString> {
192 self.parsed_args_list.next_back()
196 impl ExactSizeIterator for Args {
197 fn len(&self) -> usize {
198 self.parsed_args_list.len()
204 use crate::ffi::OsString;
205 use crate::sys::windows::args::*;
207 fn chk(string: &str, parts: &[&str]) {
208 let mut wide: Vec<u16> = OsString::from(string).encode_wide().collect();
210 let parsed = unsafe {
211 parse_lp_cmd_line(wide.as_ptr() as *const u16, || OsString::from("TEST.EXE"))
213 let expected: Vec<OsString> = parts.iter().map(|k| OsString::from(k)).collect();
214 assert_eq!(parsed.as_slice(), expected.as_slice());
219 chk("", &["TEST.EXE"]);
220 chk("\0", &["TEST.EXE"]);
225 chk("EXE one_word", &["EXE", "one_word"]);
226 chk("EXE a", &["EXE", "a"]);
227 chk("EXE 😅", &["EXE", "😅"]);
228 chk("EXE 😅🤦", &["EXE", "😅🤦"]);
232 fn official_examples() {
233 chk(r#"EXE "abc" d e"#, &["EXE", "abc", "d", "e"]);
234 chk(r#"EXE a\\\b d"e f"g h"#, &["EXE", r#"a\\\b"#, "de fg", "h"]);
235 chk(r#"EXE a\\\"b c d"#, &["EXE", r#"a\"b"#, "c", "d"]);
236 chk(r#"EXE a\\\\"b c" d e"#, &["EXE", r#"a\\b c"#, "d", "e"]);
240 fn whitespace_behavior() {
241 chk(r#" test"#, &["", "test"]);
242 chk(r#" test"#, &["", "test"]);
243 chk(r#" test test2"#, &["", "test", "test2"]);
244 chk(r#" test test2"#, &["", "test", "test2"]);
245 chk(r#"test test2 "#, &["test", "test2"]);
246 chk(r#"test test2 "#, &["test", "test2"]);
247 chk(r#"test "#, &["test"]);
252 chk(r#"EXE "" """#, &["EXE", "", ""]);
253 chk(r#"EXE "" """"#, &["EXE", "", "\""]);
255 r#"EXE "this is """all""" in the same argument""#,
256 &["EXE", "this is \"all\" in the same argument"],
258 chk(r#"EXE "a"""#, &["EXE", "a\""]);
259 chk(r#"EXE "a"" a"#, &["EXE", "a\"", "a"]);
260 // quotes cannot be escaped in command names
261 chk(r#""EXE" check"#, &["EXE", "check"]);
262 chk(r#""EXE check""#, &["EXE check"]);
263 chk(r#""EXE """for""" check"#, &["EXE ", r#"for""#, "check"]);
264 chk(r#""EXE \"for\" check"#, &[r#"EXE \"#, r#"for""#, "check"]);