@@ -10,75 +10,76 @@ use uefi_raw::Status;
10
10
11
11
use core:: ptr;
12
12
13
- pub use uefi_raw:: protocol:: shell:: ShellProtocol ;
13
+ use uefi_raw:: protocol:: shell:: ShellProtocol ;
14
14
15
15
use crate :: { CStr16 , Char16 } ;
16
16
17
17
/// Shell Protocol
18
18
#[ derive( Debug ) ]
19
19
#[ repr( transparent) ]
20
- #[ unsafe_protocol( uefi_raw :: protocol :: shell :: ShellProtocol :: GUID ) ]
21
- pub struct Shell ( uefi_raw :: protocol :: shell :: ShellProtocol ) ;
20
+ #[ unsafe_protocol( ShellProtocol :: GUID ) ]
21
+ pub struct Shell ( ShellProtocol ) ;
22
22
23
23
impl Shell {
24
- /// Gets the environment variable or list of environment variables
24
+ /// Gets the value of the specified environment variable
25
25
///
26
26
/// # Arguments
27
27
///
28
28
/// * `name` - The environment variable name of which to retrieve the
29
- /// value
30
- /// If None, will return all defined shell environment
31
- /// variables
29
+ /// value.
32
30
///
33
31
/// # Returns
34
32
///
35
- /// * `Some(Vec <env_value>)` - Value of the environment variable
36
- /// * `Some(Vec<env_names>)` - Vector of environment variable names
37
- /// * `None` - Environment variable doesn't exist
33
+ /// * `Some(<env_value>)` - &CStr16 containing the value of the
34
+ /// environment variable
35
+ /// * `None` - If environment variable does not exist
38
36
#[ must_use]
39
- pub fn get_env < ' a > ( & ' a self , name : Option < & CStr16 > ) -> Option < Vec < & ' a CStr16 > > {
40
- let mut env_vec = Vec :: new ( ) ;
41
- match name {
42
- Some ( n) => {
43
- let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( n) . cast ( ) ;
44
- let var_val = unsafe { ( self . 0 . get_env ) ( name_ptr. cast ( ) ) } ;
45
- if var_val. is_null ( ) {
46
- return None ;
47
- } else {
48
- unsafe { env_vec. push ( CStr16 :: from_ptr ( var_val. cast ( ) ) ) } ;
49
- }
50
- }
51
- None => {
52
- let cur_env_ptr = unsafe { ( self . 0 . get_env ) ( ptr:: null ( ) ) } ;
37
+ pub fn get_env ( & self , name : & CStr16 ) -> Option < & CStr16 > {
38
+ let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( name) . cast ( ) ;
39
+ let var_val = unsafe { ( self . 0 . get_env ) ( name_ptr. cast ( ) ) } ;
40
+ if var_val. is_null ( ) {
41
+ None
42
+ } else {
43
+ unsafe { Some ( CStr16 :: from_ptr ( var_val. cast ( ) ) ) }
44
+ }
45
+ }
53
46
54
- let mut cur_start = cur_env_ptr;
55
- let mut cur_len = 0 ;
47
+ /// Gets the list of environment variables
48
+ ///
49
+ /// # Returns
50
+ ///
51
+ /// * `Vec<env_names>` - Vector of environment variable names
52
+ #[ must_use]
53
+ pub fn get_envs ( & self ) -> Vec < & CStr16 > {
54
+ let mut env_vec: Vec < & CStr16 > = Vec :: new ( ) ;
55
+ let cur_env_ptr = unsafe { ( self . 0 . get_env ) ( ptr:: null ( ) ) } ;
56
+
57
+ let mut cur_start = cur_env_ptr;
58
+ let mut cur_len = 0 ;
56
59
57
- let mut i = 0 ;
58
- let mut null_count = 0 ;
59
- unsafe {
60
- while null_count <= 1 {
61
- if ( * ( cur_env_ptr. add ( i) ) ) == Char16 :: from_u16_unchecked ( 0 ) . into ( ) {
62
- if cur_len > 0 {
63
- env_vec. push ( CStr16 :: from_char16_with_nul_unchecked (
64
- & ( * ptr:: slice_from_raw_parts ( cur_start. cast ( ) , cur_len + 1 ) ) ,
65
- ) ) ;
66
- }
67
- cur_len = 0 ;
68
- null_count += 1 ;
69
- } else {
70
- if null_count > 0 {
71
- cur_start = cur_env_ptr. add ( i) ;
72
- }
73
- null_count = 0 ;
74
- cur_len += 1 ;
75
- }
76
- i += 1 ;
60
+ let mut i = 0 ;
61
+ let mut null_count = 0 ;
62
+ unsafe {
63
+ while null_count <= 1 {
64
+ if ( * ( cur_env_ptr. add ( i) ) ) == Char16 :: from_u16_unchecked ( 0 ) . into ( ) {
65
+ if cur_len > 0 {
66
+ env_vec. push ( CStr16 :: from_char16_with_nul (
67
+ & ( * ptr:: slice_from_raw_parts ( cur_start. cast ( ) , cur_len + 1 ) ) ,
68
+ ) . unwrap ( ) ) ;
77
69
}
70
+ cur_len = 0 ;
71
+ null_count += 1 ;
72
+ } else {
73
+ if null_count > 0 {
74
+ cur_start = cur_env_ptr. add ( i) ;
75
+ }
76
+ null_count = 0 ;
77
+ cur_len += 1 ;
78
78
}
79
+ i += 1 ;
79
80
}
80
81
}
81
- Some ( env_vec)
82
+ env_vec
82
83
}
83
84
84
85
/// Sets the environment variable
@@ -87,12 +88,12 @@ impl Shell {
87
88
///
88
89
/// * `name` - The environment variable for which to set the value
89
90
/// * `value` - The new value of the environment variable
90
- /// * `volatile` - Indicates whether or not the variable is volatile or
91
+ /// * `volatile` - Indicates whether the variable is volatile or
91
92
/// not
92
93
///
93
94
/// # Returns
94
95
///
95
- /// * `Status::SUCCESS` The variable was successfully set
96
+ /// * `Status::SUCCESS` - The variable was successfully set
96
97
pub fn set_env ( & self , name : & CStr16 , value : & CStr16 , volatile : bool ) -> Status {
97
98
let name_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( name) . cast ( ) ;
98
99
let value_ptr: * const Char16 = core:: ptr:: from_ref :: < CStr16 > ( value) . cast ( ) ;
@@ -105,12 +106,13 @@ impl Shell {
105
106
///
106
107
/// * `file_system_mapping` - The file system mapping for which to get
107
108
/// the current directory
109
+ ///
108
110
/// # Returns
109
111
///
110
112
/// * `Some(cwd)` - CStr16 containing the current working directory
111
113
/// * `None` - Could not retrieve current directory
112
114
#[ must_use]
113
- pub fn get_cur_dir < ' a > ( & ' a self , file_system_mapping : Option < & CStr16 > ) -> Option < & ' a CStr16 > {
115
+ pub fn get_cur_dir ( & self , file_system_mapping : Option < & CStr16 > ) -> Option < & CStr16 > {
114
116
let mapping_ptr: * const Char16 = file_system_mapping. map_or ( ptr:: null ( ) , |x| ( x. as_ptr ( ) ) ) ;
115
117
let cur_dir = unsafe { ( self . 0 . get_cur_dir ) ( mapping_ptr. cast ( ) ) } ;
116
118
if cur_dir. is_null ( ) {
@@ -127,13 +129,14 @@ impl Shell {
127
129
/// * `file_system` - Pointer to the file system's mapped name.
128
130
/// * `directory` - Points to the directory on the device specified by
129
131
/// `file_system`.
132
+ ///
130
133
/// # Returns
131
134
///
132
- /// * `Status::SUCCESS` The directory was successfully set
135
+ /// * `Status::SUCCESS` - The directory was successfully set
133
136
///
134
137
/// # Errors
135
138
///
136
- /// * `Status::EFI_NOT_FOUND` The directory does not exist
139
+ /// * `Status::EFI_NOT_FOUND` - The directory does not exist
137
140
pub fn set_cur_dir ( & self , file_system : Option < & CStr16 > , directory : Option < & CStr16 > ) -> Status {
138
141
let fs_ptr: * const Char16 = file_system. map_or ( ptr:: null ( ) , |x| ( x. as_ptr ( ) ) ) ;
139
142
let dir_ptr: * const Char16 = directory. map_or ( ptr:: null ( ) , |x| ( x. as_ptr ( ) ) ) ;
0 commit comments