|
| 1 | +//! `ShellParams` protocol |
| 2 | +
|
| 3 | +use crate::proto::unsafe_protocol; |
| 4 | +use crate::Char16; |
| 5 | +use core::ffi::c_void; |
| 6 | +use core::slice::from_raw_parts; |
| 7 | + |
| 8 | +#[cfg(feature = "alloc")] |
| 9 | +use crate::CStr16; |
| 10 | +#[cfg(feature = "alloc")] |
| 11 | +use alloc::string::String; |
| 12 | +#[cfg(feature = "alloc")] |
| 13 | +use alloc::string::ToString; |
| 14 | + |
| 15 | +type ShellFileHandle = *const c_void; |
| 16 | + |
| 17 | +/// The ShellParameters protocol. |
| 18 | +#[derive(Debug)] |
| 19 | +#[repr(transparent)] |
| 20 | +#[unsafe_protocol("752f3136-4e16-4fdc-a22a-e5f46812f4ca")] |
| 21 | +pub struct ShellParameters { |
| 22 | + /// Pointer to a list of arguments |
| 23 | + pub argv: *const *const Char16, |
| 24 | + /// Number of arguments |
| 25 | + pub argc: usize, |
| 26 | + /// Handle of the standard input |
| 27 | + std_in: ShellFileHandle, |
| 28 | + /// Handle of the standard output |
| 29 | + std_out: ShellFileHandle, |
| 30 | + /// Handle of the standard error output |
| 31 | + std_err: ShellFileHandle, |
| 32 | +} |
| 33 | + |
| 34 | +impl ShellParameters { |
| 35 | + /// Get an iterator of the shell parameter arguments |
| 36 | + #[cfg(feature = "alloc")] |
| 37 | + pub fn get_args(&self) -> impl Iterator<Item = String> { |
| 38 | + unsafe { |
| 39 | + from_raw_parts(self.argv, self.argc) |
| 40 | + .iter() |
| 41 | + .map(|x| CStr16::from_ptr(*x).to_string()) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Get a slice of the args, as Char16 pointers |
| 46 | + #[must_use] |
| 47 | + pub fn get_args_slice(&self) -> &[*const Char16] { |
| 48 | + unsafe { from_raw_parts(self.argv, self.argc) } |
| 49 | + } |
| 50 | +} |
0 commit comments