]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/args.rs
Merge branch 'master' into dedup
[rust.git] / library / std / src / sys / windows / args.rs
1 #![allow(dead_code)] // runtime init functions not used during testing
2
3 #[cfg(test)]
4 mod tests;
5
6 use crate::ffi::OsString;
7 use crate::fmt;
8 use crate::os::windows::prelude::*;
9 use crate::path::PathBuf;
10 use crate::slice;
11 use crate::sys::c;
12 use crate::sys::windows::os::current_exe;
13 use crate::vec;
14
15 use core::iter;
16
17 pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
18
19 pub unsafe fn cleanup() {}
20
21 pub fn args() -> Args {
22     unsafe {
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())
26         });
27
28         Args { parsed_args_list: parsed_args_list.into_iter() }
29     }
30 }
31
32 /// Implements the Windows command-line argument parsing algorithm.
33 ///
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)>.
36 ///
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/>.
41 ///
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,
48     exe_name: F,
49 ) -> Vec<OsString> {
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());
57         return ret_val;
58     }
59     let mut cmd_line = {
60         let mut end = 0;
61         while *lp_cmd_line.offset(end) != 0 {
62             end += 1;
63         }
64         slice::from_raw_parts(lp_cmd_line, end as usize)
65     };
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,
69         // no matter what.
70         QUOTE => {
71             let args = {
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));
75                 }
76                 cut.next()
77             };
78             if let Some(args) = args {
79                 args
80             } else {
81                 return ret_val;
82             }
83         }
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."
89         0..=SPACE => {
90             ret_val.push(OsString::new());
91             &cmd_line[1..]
92         }
93         // The executable name ends at the next whitespace,
94         // no matter what.
95         _ => {
96             let args = {
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));
100                 }
101                 cut.next()
102             };
103             if let Some(args) = args {
104                 args
105             } else {
106                 return ret_val;
107             }
108         }
109     };
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;
114     for &c in cmd_line {
115         match c {
116             // backslash
117             BACKSLASH => {
118                 backslash_count += 1;
119                 was_in_quotes = false;
120             }
121             QUOTE if backslash_count % 2 == 0 => {
122                 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
123                 backslash_count = 0;
124                 if was_in_quotes {
125                     cur.push('"' as u16);
126                     was_in_quotes = false;
127                 } else {
128                     was_in_quotes = in_quotes;
129                     in_quotes = !in_quotes;
130                 }
131             }
132             QUOTE if backslash_count % 2 != 0 => {
133                 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
134                 backslash_count = 0;
135                 was_in_quotes = false;
136                 cur.push(b'"' as u16);
137             }
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[..]));
142                     cur.truncate(0);
143                 }
144                 backslash_count = 0;
145                 was_in_quotes = false;
146             }
147             _ => {
148                 cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
149                 backslash_count = 0;
150                 was_in_quotes = false;
151                 cur.push(c);
152             }
153         }
154     }
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[..]));
159     }
160     ret_val
161 }
162
163 pub struct Args {
164     parsed_args_list: vec::IntoIter<OsString>,
165 }
166
167 pub struct ArgsInnerDebug<'a> {
168     args: &'a Args,
169 }
170
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)
174     }
175 }
176
177 impl Args {
178     pub fn inner_debug(&self) -> ArgsInnerDebug<'_> {
179         ArgsInnerDebug { args: self }
180     }
181 }
182
183 impl Iterator for Args {
184     type Item = OsString;
185     fn next(&mut self) -> Option<OsString> {
186         self.parsed_args_list.next()
187     }
188     fn size_hint(&self) -> (usize, Option<usize>) {
189         self.parsed_args_list.size_hint()
190     }
191 }
192
193 impl DoubleEndedIterator for Args {
194     fn next_back(&mut self) -> Option<OsString> {
195         self.parsed_args_list.next_back()
196     }
197 }
198
199 impl ExactSizeIterator for Args {
200     fn len(&self) -> usize {
201         self.parsed_args_list.len()
202     }
203 }