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 fn args() -> Args {
19 let lp_cmd_line = c::GetCommandLineW();
20 let parsed_args_list = parse_lp_cmd_line(lp_cmd_line as *const u16, || {
21 current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new())
24 Args { parsed_args_list: parsed_args_list.into_iter() }
28 /// Implements the Windows command-line argument parsing algorithm.
30 /// Microsoft's documentation for the Windows CLI argument format can be found at
31 /// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.
33 /// Windows includes a function to do this in shell32.dll,
34 /// but linking with that DLL causes the process to be registered as a GUI application.
35 /// GUI applications add a bunch of overhead, even if no windows are drawn. See
36 /// <https://randomascii.wordpress.com/2018/12/03/a-not-called-function-can-cause-a-5x-slowdown/>.
38 /// This function was tested for equivalence to the shell32.dll implementation in
39 /// Windows 10 Pro v1803, using an exhaustive test suite available at
40 /// <https://gist.github.com/notriddle/dde431930c392e428055b2dc22e638f5> or
41 /// <https://paste.gg/p/anonymous/47d6ed5f5bd549168b1c69c799825223>.
42 unsafe fn parse_lp_cmd_line<F: Fn() -> OsString>(
43 lp_cmd_line: *const u16,
46 const BACKSLASH: u16 = '\\' as u16;
47 const QUOTE: u16 = '"' as u16;
48 const TAB: u16 = '\t' as u16;
49 const SPACE: u16 = ' ' as u16;
50 let mut ret_val = Vec::new();
51 if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
52 ret_val.push(exe_name());
57 while *lp_cmd_line.offset(end) != 0 {
60 slice::from_raw_parts(lp_cmd_line, end as usize)
62 // The executable name at the beginning is special.
63 cmd_line = match cmd_line[0] {
64 // The executable name ends at the next quote mark,
68 let mut cut = cmd_line[1..].splitn(2, |&c| c == QUOTE);
69 if let Some(exe) = cut.next() {
70 ret_val.push(OsString::from_wide(exe));
74 if let Some(args) = args {
80 // Implement quirk: when they say whitespace here,
81 // they include the entire ASCII control plane:
82 // "However, if lpCmdLine starts with any amount of whitespace, CommandLineToArgvW
83 // will consider the first argument to be an empty string. Excess whitespace at the
84 // end of lpCmdLine is ignored."
86 ret_val.push(OsString::new());
89 // The executable name ends at the next whitespace,
93 let mut cut = cmd_line.splitn(2, |&c| c > 0 && c <= SPACE);
94 if let Some(exe) = cut.next() {
95 ret_val.push(OsString::from_wide(exe));
99 if let Some(args) = args {
106 let mut cur = Vec::new();
107 let mut in_quotes = false;
108 let mut was_in_quotes = false;
109 let mut backslash_count: usize = 0;
114 backslash_count += 1;
115 was_in_quotes = false;
117 QUOTE if backslash_count % 2 == 0 => {
118 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
121 cur.push('"' as u16);
122 was_in_quotes = false;
124 was_in_quotes = in_quotes;
125 in_quotes = !in_quotes;
128 QUOTE if backslash_count % 2 != 0 => {
129 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
131 was_in_quotes = false;
132 cur.push(b'"' as u16);
134 SPACE | TAB if !in_quotes => {
135 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
136 if !cur.is_empty() || was_in_quotes {
137 ret_val.push(OsString::from_wide(&cur[..]));
141 was_in_quotes = false;
144 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
146 was_in_quotes = false;
151 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
152 // include empty quoted strings at the end of the arguments list
153 if !cur.is_empty() || was_in_quotes || in_quotes {
154 ret_val.push(OsString::from_wide(&cur[..]));
160 parsed_args_list: vec::IntoIter<OsString>,
163 impl fmt::Debug for Args {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 self.parsed_args_list.as_slice().fmt(f)
169 impl Iterator for Args {
170 type Item = OsString;
171 fn next(&mut self) -> Option<OsString> {
172 self.parsed_args_list.next()
174 fn size_hint(&self) -> (usize, Option<usize>) {
175 self.parsed_args_list.size_hint()
179 impl DoubleEndedIterator for Args {
180 fn next_back(&mut self) -> Option<OsString> {
181 self.parsed_args_list.next_back()
185 impl ExactSizeIterator for Args {
186 fn len(&self) -> usize {
187 self.parsed_args_list.len()