Skip to content

Commit f89fceb

Browse files
committed
Fixes after rebase
1. Another set of fixes needed due to rebase 2. Code Refactoring Signed-off-by: Ayush Singh <[email protected]>
1 parent 77283ae commit f89fceb

File tree

16 files changed

+169
-252
lines changed

16 files changed

+169
-252
lines changed

Cargo.lock

+6-7
Original file line numberDiff line numberDiff line change
@@ -5240,9 +5240,9 @@ checksum = "aa7c7f42dea4b1b99439786f5633aeb9c14c1b53f75e282803c2ec2ad545873c"
52405240

52415241
[[package]]
52425242
name = "tracing"
5243-
version = "0.1.37"
5243+
version = "0.1.35"
52445244
source = "registry+https://github.com/rust-lang/crates.io-index"
5245-
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
5245+
checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160"
52465246
dependencies = [
52475247
"cfg-if 1.0.0",
52485248
"pin-project-lite",
@@ -5252,9 +5252,9 @@ dependencies = [
52525252

52535253
[[package]]
52545254
name = "tracing-attributes"
5255-
version = "0.1.23"
5255+
version = "0.1.22"
52565256
source = "registry+https://github.com/rust-lang/crates.io-index"
5257-
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
5257+
checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2"
52585258
dependencies = [
52595259
"proc-macro2",
52605260
"quote",
@@ -5263,9 +5263,9 @@ dependencies = [
52635263

52645264
[[package]]
52655265
name = "tracing-core"
5266-
version = "0.1.30"
5266+
version = "0.1.28"
52675267
source = "registry+https://github.com/rust-lang/crates.io-index"
5268-
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
5268+
checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7"
52695269
dependencies = [
52705270
"once_cell",
52715271
"valuable",
@@ -5866,4 +5866,3 @@ dependencies = [
58665866
"syn",
58675867
"synstructure",
58685868
]
5869-

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
522522
let param_handle = bx.get_param(0);
523523
let param_system_table = bx.get_param(1);
524524
let arg_argc = bx.const_int(cx.type_isize(), 2);
525-
let arg_argv = bx.array_alloca(cx.type_i8p(), bx.const_int(cx.type_int(), 2), Align::ONE);
525+
let arg_argv = bx.alloca(cx.type_array(cx.type_i8p(), 2), Align::ONE);
526526
bx.store(param_handle, arg_argv, Align::ONE);
527527
let arg_argv_el1 =
528528
bx.gep(cx.type_ptr_to(cx.type_i8()), arg_argv, &[bx.const_int(cx.type_int(), 1)]);

compiler/rustc_target/src/spec/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1648,12 +1648,6 @@ pub struct TargetOptions {
16481648
/// `argc` and `argv` values.
16491649
pub main_needs_argc_argv: bool,
16501650

1651-
// The name of entry function.
1652-
pub entry_name: StaticCow<str>,
1653-
1654-
// The ABI of entry function.
1655-
pub entry_abi: Conv,
1656-
16571651
/// Flag indicating whether #[thread_local] is available for this target.
16581652
pub has_thread_local: bool,
16591653
/// This is mainly for easy compatibility with emscripten.

library/std/src/os/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ pub mod redox;
143143
pub mod solaris;
144144
#[cfg(target_os = "solid_asp3")]
145145
pub mod solid;
146-
#[cfg(target_os = "vxworks")]
147-
pub mod vxworks;
148-
#[cfg(target_os = "watchos")]
149-
pub(crate) mod watchos;
150146
#[cfg(target_os = "uefi")]
151147
pub mod uefi;
152148
#[cfg(target_os = "vxworks")]
153149
pub mod vxworks;
150+
#[cfg(target_os = "watchos")]
151+
pub(crate) mod watchos;
154152

155153
#[cfg(any(unix, target_os = "wasi", doc))]
156154
pub mod fd;

library/std/src/os/uefi/env.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,21 @@ pub fn system_table() -> NonNull<c_void> {
3737
try_system_table().unwrap()
3838
}
3939

40-
/// Get the SystemTable Pointer.
41-
/// This function is mostly intended for places where panic is not an option
42-
#[unstable(feature = "uefi_std", issue = "100499")]
43-
pub fn try_system_table() -> Option<NonNull<c_void>> {
44-
NonNull::new(GLOBAL_SYSTEM_TABLE.load(Ordering::Acquire))
45-
}
46-
4740
/// Get the SystemHandle Pointer.
4841
/// Note: This function panics if the System Table and Image Handle is Not initialized
4942
#[unstable(feature = "uefi_std", issue = "100499")]
5043
pub fn image_handle() -> NonNull<c_void> {
5144
try_image_handle().unwrap()
5245
}
5346

47+
/// Get the SystemTable Pointer.
48+
/// This function is mostly intended for places where panic is not an option
49+
pub(crate) fn try_system_table() -> Option<NonNull<crate::ffi::c_void>> {
50+
NonNull::new(GLOBAL_SYSTEM_TABLE.load(Ordering::Acquire))
51+
}
52+
5453
/// Get the SystemHandle Pointer.
5554
/// This function is mostly intended for places where panic is not an option
56-
#[unstable(feature = "uefi_std", issue = "100499")]
57-
pub fn try_image_handle() -> Option<NonNull<c_void>> {
55+
pub(crate) fn try_image_handle() -> Option<NonNull<crate::ffi::c_void>> {
5856
NonNull::new(GLOBAL_IMAGE_HANDLE.load(Ordering::Acquire))
5957
}

library/std/src/sys/uefi/args.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ffi::OsString;
66
use crate::fmt;
77
use crate::path::PathBuf;
88
use crate::sync::OnceLock;
9-
use crate::sys_common::args::{parse_lp_cmd_line, WStrUnits};
9+
use crate::sys_common::wstr::WStrUnits;
1010
use crate::vec;
1111
use r_efi::efi::protocols::loaded_image;
1212

@@ -35,6 +35,13 @@ pub fn args() -> Args {
3535
Args { parsed_args_list: vec_args.clone().into_iter() }
3636
}
3737

38+
pub(crate) fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
39+
_lp_cmd_line: Option<WStrUnits<'a>>,
40+
_exe_name: F,
41+
) -> Vec<OsString> {
42+
todo!()
43+
}
44+
3845
impl fmt::Debug for Args {
3946
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4047
self.parsed_args_list.as_slice().fmt(f)

library/std/src/sys/uefi/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Contains most of the shared UEFI specific stuff. Some of this might be moved to `std::os::uefi`
2+
//! if needed but no point in adding extra public API when there is not Std support for UEFI in the
3+
//! first place
4+
15
use r_efi::efi::{EventNotify, Guid, Tpl};
26

37
use crate::alloc::{AllocError, Allocator, Global, Layout};

library/std/src/sys/uefi/io.rs

+5
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ struct UefiBuf {
7979
pub len: u32,
8080
pub buf: *mut c_void,
8181
}
82+
83+
// FIXME: Maybe add std::os::io APIs from Windows.
84+
pub fn is_terminal<T>(_: T) -> bool {
85+
false
86+
}

library/std/src/sys/uefi/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) fn device_path_to_path(path: &mut device_path::Protocol) -> io::Resul
9191
r_efi::efi::Boolean::FALSE,
9292
)
9393
};
94-
let ucs2_iter = match unsafe { crate::sys_common::args::WStrUnits::new(path_ucs2) } {
94+
let ucs2_iter = match unsafe { crate::sys_common::wstr::WStrUnits::new(path_ucs2) } {
9595
None => break,
9696
Some(x) => x,
9797
};

library/std/src/sys/windows/args.rs

+122
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,128 @@ pub fn args() -> Args {
4444
}
4545
}
4646

47+
/// Implements the Windows command-line argument parsing algorithm.
48+
///
49+
/// Microsoft's documentation for the Windows CLI argument format can be found at
50+
/// <https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160#parsing-c-command-line-arguments>
51+
///
52+
/// A more in-depth explanation is here:
53+
/// <https://daviddeley.com/autohotkey/parameters/parameters.htm#WIN>
54+
///
55+
/// Windows includes a function to do command line parsing in shell32.dll.
56+
/// However, this is not used for two reasons:
57+
///
58+
/// 1. Linking with that DLL causes the process to be registered as a GUI application.
59+
/// GUI applications add a bunch of overhead, even if no windows are drawn. See
60+
/// <https://randomascii.wordpress.com/2018/12/03/a-not-called-function-can-cause-a-5x-slowdown/>.
61+
///
62+
/// 2. It does not follow the modern C/C++ argv rules outlined in the first two links above.
63+
///
64+
/// This function was tested for equivalence to the C/C++ parsing rules using an
65+
/// extensive test suite available at
66+
/// <https://github.com/ChrisDenton/winarg/tree/std>.
67+
fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
68+
lp_cmd_line: Option<WStrUnits<'a>>,
69+
exe_name: F,
70+
) -> Vec<OsString> {
71+
const BACKSLASH: NonZeroU16 = non_zero_u16(b'\\' as u16);
72+
const QUOTE: NonZeroU16 = non_zero_u16(b'"' as u16);
73+
const TAB: NonZeroU16 = non_zero_u16(b'\t' as u16);
74+
const SPACE: NonZeroU16 = non_zero_u16(b' ' as u16);
75+
76+
let mut ret_val = Vec::new();
77+
// If the cmd line pointer is null or it points to an empty string then
78+
// return the name of the executable as argv[0].
79+
if lp_cmd_line.as_ref().and_then(|cmd| cmd.peek()).is_none() {
80+
ret_val.push(exe_name());
81+
return ret_val;
82+
}
83+
let mut code_units = lp_cmd_line.unwrap();
84+
85+
// The executable name at the beginning is special.
86+
let mut in_quotes = false;
87+
let mut cur = Vec::new();
88+
for w in &mut code_units {
89+
match w {
90+
// A quote mark always toggles `in_quotes` no matter what because
91+
// there are no escape characters when parsing the executable name.
92+
QUOTE => in_quotes = !in_quotes,
93+
// If not `in_quotes` then whitespace ends argv[0].
94+
SPACE | TAB if !in_quotes => break,
95+
// In all other cases the code unit is taken literally.
96+
_ => cur.push(w.get()),
97+
}
98+
}
99+
// Skip whitespace.
100+
code_units.advance_while(|w| w == SPACE || w == TAB);
101+
ret_val.push(OsString::from_wide(&cur));
102+
103+
// Parse the arguments according to these rules:
104+
// * All code units are taken literally except space, tab, quote and backslash.
105+
// * When not `in_quotes`, space and tab separate arguments. Consecutive spaces and tabs are
106+
// treated as a single separator.
107+
// * A space or tab `in_quotes` is taken literally.
108+
// * A quote toggles `in_quotes` mode unless it's escaped. An escaped quote is taken literally.
109+
// * A quote can be escaped if preceded by an odd number of backslashes.
110+
// * If any number of backslashes is immediately followed by a quote then the number of
111+
// backslashes is halved (rounding down).
112+
// * Backslashes not followed by a quote are all taken literally.
113+
// * If `in_quotes` then a quote can also be escaped using another quote
114+
// (i.e. two consecutive quotes become one literal quote).
115+
let mut cur = Vec::new();
116+
let mut in_quotes = false;
117+
while let Some(w) = code_units.next() {
118+
match w {
119+
// If not `in_quotes`, a space or tab ends the argument.
120+
SPACE | TAB if !in_quotes => {
121+
ret_val.push(OsString::from_wide(&cur[..]));
122+
cur.truncate(0);
123+
124+
// Skip whitespace.
125+
code_units.advance_while(|w| w == SPACE || w == TAB);
126+
}
127+
// Backslashes can escape quotes or backslashes but only if consecutive backslashes are followed by a quote.
128+
BACKSLASH => {
129+
let backslash_count = code_units.advance_while(|w| w == BACKSLASH) + 1;
130+
if code_units.peek() == Some(QUOTE) {
131+
cur.extend(iter::repeat(BACKSLASH.get()).take(backslash_count / 2));
132+
// The quote is escaped if there are an odd number of backslashes.
133+
if backslash_count % 2 == 1 {
134+
code_units.next();
135+
cur.push(QUOTE.get());
136+
}
137+
} else {
138+
// If there is no quote on the end then there is no escaping.
139+
cur.extend(iter::repeat(BACKSLASH.get()).take(backslash_count));
140+
}
141+
}
142+
// If `in_quotes` and not backslash escaped (see above) then a quote either
143+
// unsets `in_quote` or is escaped by another quote.
144+
QUOTE if in_quotes => match code_units.peek() {
145+
// Two consecutive quotes when `in_quotes` produces one literal quote.
146+
Some(QUOTE) => {
147+
cur.push(QUOTE.get());
148+
code_units.next();
149+
}
150+
// Otherwise set `in_quotes`.
151+
Some(_) => in_quotes = false,
152+
// The end of the command line.
153+
// Push `cur` even if empty, which we do by breaking while `in_quotes` is still set.
154+
None => break,
155+
},
156+
// If not `in_quotes` and not BACKSLASH escaped (see above) then a quote sets `in_quote`.
157+
QUOTE => in_quotes = true,
158+
// Everything else is always taken literally.
159+
_ => cur.push(w.get()),
160+
}
161+
}
162+
// Push the final argument, if any.
163+
if !cur.is_empty() || in_quotes {
164+
ret_val.push(OsString::from_wide(&cur[..]));
165+
}
166+
ret_val
167+
}
168+
47169
pub struct Args {
48170
parsed_args_list: vec::IntoIter<OsString>,
49171
}

0 commit comments

Comments
 (0)