1 #![allow(dead_code)] // runtime init functions not used during testing
6 use crate::ffi::OsString;
8 use crate::os::windows::prelude::*;
9 use crate::path::PathBuf;
12 use crate::sys::windows::os::current_exe;
17 pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
19 pub unsafe fn cleanup() {}
21 pub fn args() -> Args {
23 let lp_cmd_line = c::GetCommandLineW();
24 let parsed_args_list = parse_lp_cmd_line(lp_cmd_line as *const u16, || {
25 current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new())
28 Args { parsed_args_list: parsed_args_list.into_iter() }
32 /// Implements the Windows command-line argument parsing algorithm.
34 /// Microsoft's documentation for the Windows CLI argument format can be found at
35 /// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.
37 /// Windows includes a function to do this in shell32.dll,
38 /// but linking with that DLL causes the process to be registered as a GUI application.
39 /// GUI applications add a bunch of overhead, even if no windows are drawn. See
40 /// <https://randomascii.wordpress.com/2018/12/03/a-not-called-function-can-cause-a-5x-slowdown/>.
42 /// This function was tested for equivalence to the shell32.dll implementation in
43 /// Windows 10 Pro v1803, using an exhaustive test suite available at
44 /// <https://gist.github.com/notriddle/dde431930c392e428055b2dc22e638f5> or
45 /// <https://paste.gg/p/anonymous/47d6ed5f5bd549168b1c69c799825223>.
46 unsafe fn parse_lp_cmd_line<F: Fn() -> OsString>(
47 lp_cmd_line: *const u16,
50 const BACKSLASH: u16 = '\\' as u16;
51 const QUOTE: u16 = '"' as u16;
52 const TAB: u16 = '\t' as u16;
53 const SPACE: u16 = ' ' as u16;
54 let mut ret_val = Vec::new();
55 if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
56 ret_val.push(exe_name());
61 while *lp_cmd_line.offset(end) != 0 {
64 slice::from_raw_parts(lp_cmd_line, end as usize)
66 // The executable name at the beginning is special.
67 cmd_line = match cmd_line[0] {
68 // The executable name ends at the next quote mark,
72 let mut cut = cmd_line[1..].splitn(2, |&c| c == QUOTE);
73 if let Some(exe) = cut.next() {
74 ret_val.push(OsString::from_wide(exe));
78 if let Some(args) = args {
84 // Implement quirk: when they say whitespace here,
85 // they include the entire ASCII control plane:
86 // "However, if lpCmdLine starts with any amount of whitespace, CommandLineToArgvW
87 // will consider the first argument to be an empty string. Excess whitespace at the
88 // end of lpCmdLine is ignored."
90 ret_val.push(OsString::new());
93 // The executable name ends at the next whitespace,
97 let mut cut = cmd_line.splitn(2, |&c| c > 0 && c <= SPACE);
98 if let Some(exe) = cut.next() {
99 ret_val.push(OsString::from_wide(exe));
103 if let Some(args) = args {
110 let mut cur = Vec::new();
111 let mut in_quotes = false;
112 let mut was_in_quotes = false;
113 let mut backslash_count: usize = 0;
118 backslash_count += 1;
119 was_in_quotes = false;
121 QUOTE if backslash_count % 2 == 0 => {
122 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
125 cur.push('"' as u16);
126 was_in_quotes = false;
128 was_in_quotes = in_quotes;
129 in_quotes = !in_quotes;
132 QUOTE if backslash_count % 2 != 0 => {
133 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
135 was_in_quotes = false;
136 cur.push(b'"' as u16);
138 SPACE | TAB if !in_quotes => {
139 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
140 if !cur.is_empty() || was_in_quotes {
141 ret_val.push(OsString::from_wide(&cur[..]));
145 was_in_quotes = false;
148 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
150 was_in_quotes = false;
155 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
156 // include empty quoted strings at the end of the arguments list
157 if !cur.is_empty() || was_in_quotes || in_quotes {
158 ret_val.push(OsString::from_wide(&cur[..]));
164 parsed_args_list: vec::IntoIter<OsString>,
167 pub struct ArgsInnerDebug<'a> {
171 impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173 self.args.parsed_args_list.as_slice().fmt(f)
178 pub fn inner_debug(&self) -> ArgsInnerDebug<'_> {
179 ArgsInnerDebug { args: self }
183 impl Iterator for Args {
184 type Item = OsString;
185 fn next(&mut self) -> Option<OsString> {
186 self.parsed_args_list.next()
188 fn size_hint(&self) -> (usize, Option<usize>) {
189 self.parsed_args_list.size_hint()
193 impl DoubleEndedIterator for Args {
194 fn next_back(&mut self) -> Option<OsString> {
195 self.parsed_args_list.next_back()
199 impl ExactSizeIterator for Args {
200 fn len(&self) -> usize {
201 self.parsed_args_list.len()