-
- Notifications
You must be signed in to change notification settings - Fork 180
Enhancement/efi shell interface #1679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
6547011
d6db9b5
ad4e343
56602da
595cab2
22df5c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
The UEFI get_env() implementation is used for getting individual environment variable values and also environment variable names. This is not intuitive so this commit splits the function into two dedicated ones: one for accessing values and the other for accessing names. Co-authored-by: Philipp Schuster <phip1611@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -56,64 +56,65 @@ impl Shell { | |
unsafe { (self.0.set_cur_dir)(fs_ptr.cast(), dir_ptr.cast()) }.to_result() | ||
} | ||
| ||
/// Gets the environment variable or list of environment variables | ||
/// Gets the value of the specified environment variable | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `name` - The environment variable name of which to retrieve the | ||
/// value | ||
/// If None, will return all defined shell environment | ||
/// variables | ||
/// value. | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Some(Vec<env_value>)` - Value of the environment variable | ||
/// * `Some(Vec<env_names>)` - Vector of environment variable names | ||
/// * `None` - Environment variable doesn't exist | ||
/// * `Some(<env_value>)` - &CStr16 containing the value of the | ||
/// environment variable | ||
/// * `None` - If environment variable does not exist | ||
#[must_use] | ||
pub fn get_env<'a>(&'a self, name: Option<&CStr16>) -> Option<Vec<&'a CStr16>> { | ||
let mut env_vec = Vec::new(); | ||
match name { | ||
Some(n) => { | ||
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(n).cast(); | ||
let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) }; | ||
if var_val.is_null() { | ||
return None; | ||
} else { | ||
unsafe { env_vec.push(CStr16::from_ptr(var_val.cast())) }; | ||
} | ||
} | ||
None => { | ||
let cur_env_ptr = unsafe { (self.0.get_env)(ptr::null()) }; | ||
pub fn get_env(&self, name: &CStr16) -> Option<&CStr16> { | ||
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast(); | ||
let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) }; | ||
if var_val.is_null() { | ||
None | ||
} else { | ||
unsafe { Some(CStr16::from_ptr(var_val.cast())) } | ||
} | ||
} | ||
| ||
let mut cur_start = cur_env_ptr; | ||
let mut cur_len = 0; | ||
/// Gets the list of environment variables | ||
| ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Vec<env_names>` - Vector of environment variable names | ||
| ||
#[must_use] | ||
pub fn get_envs(&self) -> Vec<&CStr16> { | ||
RenTrieu marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
let mut env_vec: Vec<&CStr16> = Vec::new(); | ||
let cur_env_ptr = unsafe { (self.0.get_env)(ptr::null()) }; | ||
| ||
let mut i = 0; | ||
let mut null_count = 0; | ||
unsafe { | ||
while null_count <= 1 { | ||
if (*(cur_env_ptr.add(i))) == Char16::from_u16_unchecked(0).into() { | ||
if cur_len > 0 { | ||
env_vec.push(CStr16::from_char16_with_nul_unchecked( | ||
&(*ptr::slice_from_raw_parts(cur_start.cast(), cur_len + 1)), | ||
)); | ||
} | ||
cur_len = 0; | ||
null_count += 1; | ||
} else { | ||
if null_count > 0 { | ||
cur_start = cur_env_ptr.add(i); | ||
} | ||
null_count = 0; | ||
cur_len += 1; | ||
} | ||
i += 1; | ||
let mut cur_start = cur_env_ptr; | ||
let mut cur_len = 0; | ||
| ||
let mut i = 0; | ||
let mut null_count = 0; | ||
unsafe { | ||
while null_count <= 1 { | ||
if (*(cur_env_ptr.add(i))) == Char16::from_u16_unchecked(0).into() { | ||
RenTrieu marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
if cur_len > 0 { | ||
env_vec.push(CStr16::from_char16_with_nul( | ||
&(*ptr::slice_from_raw_parts(cur_start.cast(), cur_len + 1)), | ||
).unwrap()); | ||
} | ||
cur_len = 0; | ||
null_count += 1; | ||
} else { | ||
if null_count > 0 { | ||
cur_start = cur_env_ptr.add(i); | ||
} | ||
null_count = 0; | ||
cur_len += 1; | ||
} | ||
i += 1; | ||
} | ||
} | ||
Some(env_vec) | ||
env_vec | ||
} | ||
| ||
/// Sets the environment variable | ||
| @@ -122,12 +123,12 @@ impl Shell { | |
/// | ||
/// * `name` - The environment variable for which to set the value | ||
/// * `value` - The new value of the environment variable | ||
/// * `volatile` - Indicates whether or not the variable is volatile or | ||
/// * `volatile` - Indicates whether the variable is volatile or | ||
/// not | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Status::SUCCESS` The variable was successfully set | ||
/// * `Status::SUCCESS` - The variable was successfully set | ||
pub fn set_env(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status { | ||
| ||
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast(); | ||
| ||
let value_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(value).cast(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In #1679 (comment), it was suggested to use the same name as
std
:var
. I think we should do that here, for several reasons:std::env
will instinctively know how to use this APIget_
prefix