Skip to content

Commit 6759278

Browse files
committed
uefi: Revising function calls and names to better match standard convention
1 parent a21932f commit 6759278

File tree

2 files changed

+46
-55
lines changed

2 files changed

+46
-55
lines changed

uefi-test-runner/src/proto/shell.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ use uefi::proto::shell::Shell;
55
use uefi::{boot, cstr16};
66
use uefi_raw::Status;
77

8-
/// Test `get_env()`, `get_envs()`, and `set_env()`
8+
/// Test `var()`, `vars()`, and `set_var()`
99
pub fn test_env(shell: &ScopedProtocol<Shell>) {
1010
/* Test retrieving list of environment variable names */
11-
let mut cur_env_vec = shell.get_envs();
11+
let mut cur_env_vec = shell.vars();
1212
assert_eq!(cur_env_vec.next().unwrap(), cstr16!("path"),);
1313
// check pre-defined shell variables; see UEFI Shell spec
1414
assert_eq!(cur_env_vec.next().unwrap(), cstr16!("nonesting"),);
15-
let cur_env_vec = shell.get_envs();
15+
let cur_env_vec = shell.vars();
1616
let default_len = cur_env_vec.count();
1717

1818
/* Test setting and getting a specific environment variable */
19-
let cur_env_vec = shell.get_envs();
19+
let cur_env_vec = shell.vars();
2020
let test_var = cstr16!("test_var");
2121
let test_val = cstr16!("test_val");
22-
assert!(shell.get_env(test_var).is_none());
23-
let status = shell.set_env(test_var, test_val, false);
22+
assert!(shell.var(test_var).is_none());
23+
let status = shell.set_var(test_var, test_val, false);
2424
assert_eq!(status, Status::SUCCESS);
2525
let cur_env_str = shell
26-
.get_env(test_var)
26+
.var(test_var)
2727
.expect("Could not get environment variable");
2828
assert_eq!(cur_env_str, test_val);
2929

@@ -34,7 +34,7 @@ pub fn test_env(shell: &ScopedProtocol<Shell>) {
3434
}
3535
}
3636
assert!(!found_var);
37-
let cur_env_vec = shell.get_envs();
37+
let cur_env_vec = shell.vars();
3838
let mut found_var = false;
3939
for env_var in cur_env_vec {
4040
if env_var == test_var {
@@ -43,49 +43,49 @@ pub fn test_env(shell: &ScopedProtocol<Shell>) {
4343
}
4444
assert!(found_var);
4545

46-
let cur_env_vec = shell.get_envs();
46+
let cur_env_vec = shell.vars();
4747
assert_eq!(cur_env_vec.count(), default_len + 1);
4848

4949
/* Test deleting environment variable */
5050
let test_val = cstr16!("");
51-
let status = shell.set_env(test_var, test_val, false);
51+
let status = shell.set_var(test_var, test_val, false);
5252
assert_eq!(status, Status::SUCCESS);
53-
assert!(shell.get_env(test_var).is_none());
53+
assert!(shell.var(test_var).is_none());
5454

55-
let cur_env_vec = shell.get_envs();
55+
let cur_env_vec = shell.vars();
5656
let mut found_var = false;
5757
for env_var in cur_env_vec {
5858
if env_var == test_var {
5959
found_var = true;
6060
}
6161
}
6262
assert!(!found_var);
63-
let cur_env_vec = shell.get_envs();
63+
let cur_env_vec = shell.vars();
6464
assert_eq!(cur_env_vec.count(), default_len);
6565
}
6666

67-
/// Test `get_cur_dir()` and `set_cur_dir()`
67+
/// Test `current_dir()` and `set_current_dir()`
6868
pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
6969
/* Test setting and getting current file system and current directory */
7070
let fs_var = cstr16!("fs0:");
7171
let dir_var = cstr16!("/");
72-
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
72+
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
7373
assert_eq!(status, Status::SUCCESS);
7474

7575
let cur_fs_str = shell
76-
.get_cur_dir(Some(fs_var))
76+
.current_dir(Some(fs_var))
7777
.expect("Could not get the current file system mapping");
7878
let expected_fs_str = cstr16!("FS0:\\");
7979
assert_eq!(cur_fs_str, expected_fs_str);
8080

8181
// Changing current file system
8282
let fs_var = cstr16!("fs1:");
8383
let dir_var = cstr16!("/");
84-
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
84+
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
8585
assert_eq!(status, Status::SUCCESS);
8686

8787
let cur_fs_str = shell
88-
.get_cur_dir(Some(fs_var))
88+
.current_dir(Some(fs_var))
8989
.expect("Could not get the current file system mapping");
9090
assert_ne!(cur_fs_str, expected_fs_str);
9191
let expected_fs_str = cstr16!("FS1:\\");
@@ -94,11 +94,11 @@ pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
9494
// Changing current file system and current directory
9595
let fs_var = cstr16!("fs0:");
9696
let dir_var = cstr16!("efi/");
97-
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
97+
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
9898
assert_eq!(status, Status::SUCCESS);
9999

100100
let cur_fs_str = shell
101-
.get_cur_dir(Some(fs_var))
101+
.current_dir(Some(fs_var))
102102
.expect("Could not get the current file system mapping");
103103
assert_ne!(cur_fs_str, expected_fs_str);
104104
let expected_fs_str = cstr16!("FS0:\\efi");
@@ -108,50 +108,50 @@ pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
108108

109109
// At this point, the current working file system has not been set
110110
// So we expect a NULL output
111-
assert!(shell.get_cur_dir(None).is_none());
111+
assert!(shell.current_dir(None).is_none());
112112

113113
// Setting the current working file system and current working directory
114114
let dir_var = cstr16!("fs0:/");
115-
let status = shell.set_cur_dir(None, Some(dir_var));
115+
let status = shell.set_current_dir(None, Some(dir_var));
116116
assert_eq!(status, Status::SUCCESS);
117117
let cur_fs_str = shell
118-
.get_cur_dir(Some(fs_var))
118+
.current_dir(Some(fs_var))
119119
.expect("Could not get the current file system mapping");
120120
let expected_fs_str = cstr16!("FS0:");
121121
assert_eq!(cur_fs_str, expected_fs_str);
122122

123123
let cur_fs_str = shell
124-
.get_cur_dir(None)
124+
.current_dir(None)
125125
.expect("Could not get the current file system mapping");
126126
assert_eq!(cur_fs_str, expected_fs_str);
127127

128128
// Changing current working directory
129129
let dir_var = cstr16!("/efi");
130-
let status = shell.set_cur_dir(None, Some(dir_var));
130+
let status = shell.set_current_dir(None, Some(dir_var));
131131
assert_eq!(status, Status::SUCCESS);
132132
let cur_fs_str = shell
133-
.get_cur_dir(Some(fs_var))
133+
.current_dir(Some(fs_var))
134134
.expect("Could not get the current file system mapping");
135135
let expected_fs_str = cstr16!("FS0:\\efi");
136136
assert_eq!(cur_fs_str, expected_fs_str);
137137
let cur_fs_str = shell
138-
.get_cur_dir(None)
138+
.current_dir(None)
139139
.expect("Could not get the current file system mapping");
140140
assert_eq!(cur_fs_str, expected_fs_str);
141141

142142
// Changing current directory in a non-current working file system
143143
let fs_var = cstr16!("fs0:");
144144
let dir_var = cstr16!("efi/tools");
145-
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
145+
let status = shell.set_current_dir(Some(fs_var), Some(dir_var));
146146
assert_eq!(status, Status::SUCCESS);
147147
let cur_fs_str = shell
148-
.get_cur_dir(None)
148+
.current_dir(None)
149149
.expect("Could not get the current file system mapping");
150150
assert_ne!(cur_fs_str, expected_fs_str);
151151

152152
let expected_fs_str = cstr16!("FS0:\\efi\\tools");
153153
let cur_fs_str = shell
154-
.get_cur_dir(Some(fs_var))
154+
.current_dir(Some(fs_var))
155155
.expect("Could not get the current file system mapping");
156156
assert_eq!(cur_fs_str, expected_fs_str);
157157
}

uefi/src/proto/shell/mod.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,12 @@ impl<'a> Iterator for Vars<'a> {
3232
// We iterate a list of NUL terminated CStr16s.
3333
// The list is terminated with a double NUL.
3434
fn next(&mut self) -> Option<Self::Item> {
35-
let cur_start = self.inner;
36-
let mut cur_len = 0;
37-
unsafe {
38-
if *(cur_start) == Char16::from_u16_unchecked(0) {
39-
return None;
40-
}
41-
while *(cur_start.add(cur_len)) != Char16::from_u16_unchecked(0) {
42-
cur_len += 1;
43-
}
44-
self.inner = self.inner.add(cur_len + 1);
45-
Some(CStr16::from_ptr(cur_start))
35+
let s = unsafe { CStr16::from_ptr(self.inner) };
36+
if s.is_empty() {
37+
None
38+
} else {
39+
self.inner = unsafe { self.inner.add(s.num_chars() + 1) };
40+
Some(s)
4641
}
4742
}
4843
}
@@ -61,8 +56,8 @@ impl Shell {
6156
/// environment variable
6257
/// * `None` - If environment variable does not exist
6358
#[must_use]
64-
pub fn get_env(&self, name: &CStr16) -> Option<&CStr16> {
65-
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast();
59+
pub fn var(&self, name: &CStr16) -> Option<&CStr16> {
60+
let name_ptr: *const Char16 = name.as_ptr();
6661
let var_val = unsafe { (self.0.get_env)(name_ptr.cast()) };
6762
if var_val.is_null() {
6863
None
@@ -71,13 +66,9 @@ impl Shell {
7166
}
7267
}
7368

74-
/// Gets the list of environment variables
75-
///
76-
/// # Returns
77-
///
78-
/// * `Vec<env_names>` - Vector of environment variable names
69+
/// Gets an iterator over the names of all environment variables
7970
#[must_use]
80-
pub fn get_envs(&self) -> Vars {
71+
pub fn vars(&self) -> Vars {
8172
let env_ptr = unsafe { (self.0.get_env)(ptr::null()) };
8273
Vars {
8374
inner: env_ptr.cast::<Char16>(),
@@ -97,9 +88,9 @@ impl Shell {
9788
/// # Returns
9889
///
9990
/// * `Status::SUCCESS` - The variable was successfully set
100-
pub fn set_env(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status {
101-
let name_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(name).cast();
102-
let value_ptr: *const Char16 = core::ptr::from_ref::<CStr16>(value).cast();
91+
pub fn set_var(&self, name: &CStr16, value: &CStr16, volatile: bool) -> Status {
92+
let name_ptr: *const Char16 = name.as_ptr();
93+
let value_ptr: *const Char16 = value.as_ptr();
10394
unsafe { (self.0.set_env)(name_ptr.cast(), value_ptr.cast(), volatile) }
10495
}
10596

@@ -115,8 +106,8 @@ impl Shell {
115106
/// * `Some(cwd)` - CStr16 containing the current working directory
116107
/// * `None` - Could not retrieve current directory
117108
#[must_use]
118-
pub fn get_cur_dir(&self, file_system_mapping: Option<&CStr16>) -> Option<&CStr16> {
119-
let mapping_ptr: *const Char16 = file_system_mapping.map_or(ptr::null(), |x| (x.as_ptr()));
109+
pub fn current_dir(&self, file_system_mapping: Option<&CStr16>) -> Option<&CStr16> {
110+
let mapping_ptr: *const Char16 = file_system_mapping.map_or(ptr::null(), CStr16::as_ptr);
120111
let cur_dir = unsafe { (self.0.get_cur_dir)(mapping_ptr.cast()) };
121112
if cur_dir.is_null() {
122113
None
@@ -140,7 +131,7 @@ impl Shell {
140131
/// # Errors
141132
///
142133
/// * `Status::EFI_NOT_FOUND` - The directory does not exist
143-
pub fn set_cur_dir(&self, file_system: Option<&CStr16>, directory: Option<&CStr16>) -> Status {
134+
pub fn set_current_dir(&self, file_system: Option<&CStr16>, directory: Option<&CStr16>) -> Status {
144135
let fs_ptr: *const Char16 = file_system.map_or(ptr::null(), |x| (x.as_ptr()));
145136
let dir_ptr: *const Char16 = directory.map_or(ptr::null(), |x| (x.as_ptr()));
146137
unsafe { (self.0.set_cur_dir)(fs_ptr.cast(), dir_ptr.cast()) }

0 commit comments

Comments
 (0)