2016-09-29 22:00:44 +00:00
|
|
|
#![allow(dead_code)] // runtime init functions not used during testing
|
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ffi::OsString;
|
|
|
|
|
use crate::fmt;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::os::windows::prelude::*;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::path::PathBuf;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::slice;
|
|
|
|
|
use crate::sys::c;
|
|
|
|
|
use crate::sys::windows::os::current_exe;
|
|
|
|
|
use crate::vec;
|
2019-02-11 04:23:21 +09:00
|
|
|
|
2018-12-06 11:26:11 -07:00
|
|
|
use core::iter;
|
2016-09-29 22:00:44 +00:00
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
|
2016-09-29 22:00:44 +00:00
|
|
|
|
|
|
|
|
pub fn args() -> Args {
|
|
|
|
|
unsafe {
|
2018-12-06 11:26:11 -07:00
|
|
|
let lp_cmd_line = c::GetCommandLineW();
|
2019-11-27 10:28:39 -08:00
|
|
|
let parsed_args_list = parse_lp_cmd_line(lp_cmd_line as *const u16, || {
|
|
|
|
|
current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new())
|
|
|
|
|
});
|
2018-12-06 11:26:11 -07:00
|
|
|
|
2018-12-10 18:31:53 -07:00
|
|
|
Args { parsed_args_list: parsed_args_list.into_iter() }
|
2016-09-29 22:00:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 15:37:50 -07:00
|
|
|
/// Implements the Windows command-line argument parsing algorithm.
|
|
|
|
|
///
|
|
|
|
|
/// Microsoft's documentation for the Windows CLI argument format can be found at
|
2018-12-06 11:26:11 -07:00
|
|
|
/// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.
|
|
|
|
|
///
|
|
|
|
|
/// Windows includes a function to do this in shell32.dll,
|
|
|
|
|
/// but linking with that DLL causes the process to be registered as a GUI application.
|
|
|
|
|
/// GUI applications add a bunch of overhead, even if no windows are drawn. See
|
|
|
|
|
/// <https://randomascii.wordpress.com/2018/12/03/a-not-called-function-can-cause-a-5x-slowdown/>.
|
2018-12-10 15:37:50 -07:00
|
|
|
///
|
|
|
|
|
/// This function was tested for equivalence to the shell32.dll implementation in
|
|
|
|
|
/// Windows 10 Pro v1803, using an exhaustive test suite available at
|
|
|
|
|
/// <https://gist.github.com/notriddle/dde431930c392e428055b2dc22e638f5> or
|
|
|
|
|
/// <https://paste.gg/p/anonymous/47d6ed5f5bd549168b1c69c799825223>.
|
2019-11-27 10:28:39 -08:00
|
|
|
unsafe fn parse_lp_cmd_line<F: Fn() -> OsString>(
|
|
|
|
|
lp_cmd_line: *const u16,
|
|
|
|
|
exe_name: F,
|
|
|
|
|
) -> Vec<OsString> {
|
2018-12-06 11:26:11 -07:00
|
|
|
const BACKSLASH: u16 = '\\' as u16;
|
|
|
|
|
const QUOTE: u16 = '"' as u16;
|
|
|
|
|
const TAB: u16 = '\t' as u16;
|
|
|
|
|
const SPACE: u16 = ' ' as u16;
|
2018-12-10 13:12:47 -07:00
|
|
|
let mut ret_val = Vec::new();
|
2018-12-06 11:26:11 -07:00
|
|
|
if lp_cmd_line.is_null() || *lp_cmd_line == 0 {
|
2018-12-10 13:12:47 -07:00
|
|
|
ret_val.push(exe_name());
|
2018-12-10 18:31:53 -07:00
|
|
|
return ret_val;
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
2018-12-11 11:55:02 -07:00
|
|
|
let mut cmd_line = {
|
|
|
|
|
let mut end = 0;
|
|
|
|
|
while *lp_cmd_line.offset(end) != 0 {
|
|
|
|
|
end += 1;
|
|
|
|
|
}
|
|
|
|
|
slice::from_raw_parts(lp_cmd_line, end as usize)
|
|
|
|
|
};
|
2018-12-06 11:26:11 -07:00
|
|
|
// The executable name at the beginning is special.
|
2018-12-11 11:55:02 -07:00
|
|
|
cmd_line = match cmd_line[0] {
|
2018-12-06 11:26:11 -07:00
|
|
|
// The executable name ends at the next quote mark,
|
|
|
|
|
// no matter what.
|
|
|
|
|
QUOTE => {
|
2018-12-11 11:55:02 -07:00
|
|
|
let args = {
|
|
|
|
|
let mut cut = cmd_line[1..].splitn(2, |&c| c == QUOTE);
|
|
|
|
|
if let Some(exe) = cut.next() {
|
|
|
|
|
ret_val.push(OsString::from_wide(exe));
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
2018-12-11 11:55:02 -07:00
|
|
|
cut.next()
|
|
|
|
|
};
|
|
|
|
|
if let Some(args) = args {
|
|
|
|
|
args
|
|
|
|
|
} else {
|
|
|
|
|
return ret_val;
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Implement quirk: when they say whitespace here,
|
|
|
|
|
// they include the entire ASCII control plane:
|
|
|
|
|
// "However, if lpCmdLine starts with any amount of whitespace, CommandLineToArgvW
|
|
|
|
|
// will consider the first argument to be an empty string. Excess whitespace at the
|
|
|
|
|
// end of lpCmdLine is ignored."
|
2019-02-11 04:23:21 +09:00
|
|
|
0..=SPACE => {
|
2018-12-10 13:12:47 -07:00
|
|
|
ret_val.push(OsString::new());
|
2018-12-11 11:55:02 -07:00
|
|
|
&cmd_line[1..]
|
2019-11-27 10:28:39 -08:00
|
|
|
}
|
2018-12-10 13:12:47 -07:00
|
|
|
// The executable name ends at the next whitespace,
|
2018-12-06 11:26:11 -07:00
|
|
|
// no matter what.
|
|
|
|
|
_ => {
|
2018-12-11 11:55:02 -07:00
|
|
|
let args = {
|
|
|
|
|
let mut cut = cmd_line.splitn(2, |&c| c > 0 && c <= SPACE);
|
|
|
|
|
if let Some(exe) = cut.next() {
|
|
|
|
|
ret_val.push(OsString::from_wide(exe));
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
2018-12-11 11:55:02 -07:00
|
|
|
cut.next()
|
|
|
|
|
};
|
|
|
|
|
if let Some(args) = args {
|
|
|
|
|
args
|
|
|
|
|
} else {
|
|
|
|
|
return ret_val;
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-11 11:55:02 -07:00
|
|
|
};
|
|
|
|
|
let mut cur = Vec::new();
|
|
|
|
|
let mut in_quotes = false;
|
|
|
|
|
let mut was_in_quotes = false;
|
|
|
|
|
let mut backslash_count: usize = 0;
|
|
|
|
|
for &c in cmd_line {
|
2018-12-06 11:26:11 -07:00
|
|
|
match c {
|
|
|
|
|
// backslash
|
|
|
|
|
BACKSLASH => {
|
|
|
|
|
backslash_count += 1;
|
|
|
|
|
was_in_quotes = false;
|
2019-11-27 10:28:39 -08:00
|
|
|
}
|
2018-12-06 11:26:11 -07:00
|
|
|
QUOTE if backslash_count % 2 == 0 => {
|
|
|
|
|
cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
|
|
|
|
|
backslash_count = 0;
|
|
|
|
|
if was_in_quotes {
|
|
|
|
|
cur.push('"' as u16);
|
|
|
|
|
was_in_quotes = false;
|
|
|
|
|
} else {
|
|
|
|
|
was_in_quotes = in_quotes;
|
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QUOTE if backslash_count % 2 != 0 => {
|
|
|
|
|
cur.extend(iter::repeat(b'\\' as u16).take(backslash_count / 2));
|
|
|
|
|
backslash_count = 0;
|
|
|
|
|
was_in_quotes = false;
|
|
|
|
|
cur.push(b'"' as u16);
|
|
|
|
|
}
|
|
|
|
|
SPACE | TAB if !in_quotes => {
|
|
|
|
|
cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
|
|
|
|
|
if !cur.is_empty() || was_in_quotes {
|
2018-12-10 13:12:47 -07:00
|
|
|
ret_val.push(OsString::from_wide(&cur[..]));
|
2018-12-06 11:26:11 -07:00
|
|
|
cur.truncate(0);
|
|
|
|
|
}
|
|
|
|
|
backslash_count = 0;
|
|
|
|
|
was_in_quotes = false;
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
|
|
|
|
|
backslash_count = 0;
|
|
|
|
|
was_in_quotes = false;
|
|
|
|
|
cur.push(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-11 11:55:02 -07:00
|
|
|
}
|
|
|
|
|
cur.extend(iter::repeat(b'\\' as u16).take(backslash_count));
|
|
|
|
|
// include empty quoted strings at the end of the arguments list
|
|
|
|
|
if !cur.is_empty() || was_in_quotes || in_quotes {
|
|
|
|
|
ret_val.push(OsString::from_wide(&cur[..]));
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
2018-12-10 18:31:53 -07:00
|
|
|
ret_val
|
2018-12-06 11:26:11 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-29 22:00:44 +00:00
|
|
|
pub struct Args {
|
2018-12-10 13:12:47 -07:00
|
|
|
parsed_args_list: vec::IntoIter<OsString>,
|
2016-09-29 22:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 15:40:45 +03:00
|
|
|
pub struct ArgsInnerDebug<'a> {
|
|
|
|
|
args: &'a Args,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
|
2019-03-01 09:34:11 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-10 18:31:53 -07:00
|
|
|
self.args.parsed_args_list.as_slice().fmt(f)
|
2017-06-21 15:40:45 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Args {
|
2019-03-01 09:34:11 +01:00
|
|
|
pub fn inner_debug(&self) -> ArgsInnerDebug<'_> {
|
2019-11-27 10:28:39 -08:00
|
|
|
ArgsInnerDebug { args: self }
|
2017-06-21 15:40:45 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 22:00:44 +00:00
|
|
|
impl Iterator for Args {
|
|
|
|
|
type Item = OsString;
|
2019-11-27 10:28:39 -08:00
|
|
|
fn next(&mut self) -> Option<OsString> {
|
|
|
|
|
self.parsed_args_list.next()
|
|
|
|
|
}
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.parsed_args_list.size_hint()
|
|
|
|
|
}
|
2016-09-29 22:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DoubleEndedIterator for Args {
|
2019-11-27 10:28:39 -08:00
|
|
|
fn next_back(&mut self) -> Option<OsString> {
|
|
|
|
|
self.parsed_args_list.next_back()
|
|
|
|
|
}
|
2016-09-29 22:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ExactSizeIterator for Args {
|
2019-11-27 10:28:39 -08:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
|
self.parsed_args_list.len()
|
|
|
|
|
}
|
2016-09-29 22:00:44 +00:00
|
|
|
}
|