Skip to content

Commit df8e560

Browse files
aero_shell: update to use std
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent b5b72a3 commit df8e560

File tree

1 file changed

+31
-46
lines changed
  • userland/apps/aero_shell/src

1 file changed

+31
-46
lines changed

userland/apps/aero_shell/src/main.rs

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use core::sync::atomic::{AtomicU32, Ordering};
20+
use std::sync::atomic::{AtomicU32, Ordering};
21+
22+
use std::error::Error;
23+
use std::io::{BufRead, BufReader};
24+
25+
use std::fs;
26+
use std::fs::File;
2127

2228
use aero_ipc::SystemService;
2329
use aero_syscall::signal::*;
@@ -86,13 +92,13 @@ fn repl(history: &mut Vec<String>) -> Result<(), AeroSyscallError> {
8692
let message = args.collect::<Vec<_>>().join(" ");
8793
let message = message.replace(
8894
"$?",
89-
LAST_EXIT_CODE.load(Ordering::Relaxed).to_string().as_str(),
95+
LAST_EXIT_CODE.load(Ordering::SeqCst).to_string().as_str(),
9096
);
9197

9298
println!("{}", message);
9399
}
94100

95-
"ls" => list_directory(args.next().unwrap_or("."))?,
101+
"ls" => list_directory(args.next().unwrap_or(".")).unwrap(),
96102
"pwd" => println!("{}", pwd),
97103
"cd" => {
98104
sys_chdir(args.next().unwrap_or(".."))?;
@@ -116,9 +122,9 @@ fn repl(history: &mut Vec<String>) -> Result<(), AeroSyscallError> {
116122
},
117123
None => sys_exit(0),
118124
},
119-
"cat" => cat_file(args.next())?,
125+
"cat" => cat_file(args.next()).unwrap(),
120126
"clear" => print!("{esc}[2J{esc}[1;1H", esc = 27 as char),
121-
"dmsg" => print_kernel_log()?,
127+
"dmsg" => print_kernel_log().unwrap(),
122128
"uwufetch" => uwufetch()?,
123129
"uname" => uname()?,
124130
"history" => {
@@ -255,67 +261,46 @@ fn repl(history: &mut Vec<String>) -> Result<(), AeroSyscallError> {
255261
Ok(())
256262
}
257263

258-
fn list_directory(path: &str) -> Result<(), AeroSyscallError> {
259-
let dir_fd = sys_open(path, OpenFlags::O_DIRECTORY)?;
260-
261-
loop {
262-
let mut dir_ents_buffer = [0; 1024];
263-
264-
let size = sys_getdents(dir_fd, &mut dir_ents_buffer)?;
265-
266-
if size == 0 {
267-
break;
268-
}
269-
270-
let dir_entry = unsafe { &*(dir_ents_buffer.as_ptr() as *const SysDirEntry) };
264+
fn list_directory(path: &str) -> Result<(), Box<dyn Error>> {
265+
let rdir = fs::read_dir(path)?;
271266

272-
let name_start = core::mem::size_of::<SysDirEntry>();
273-
let name_end = dir_entry.reclen;
267+
for file in rdir {
268+
let name = file?.file_name();
269+
let name_str = name.to_str().ok_or("ls: invalid filename")?;
274270

275-
let name =
276-
unsafe { core::str::from_utf8_unchecked(&dir_ents_buffer[name_start..name_end]) };
277-
278-
print!("{} ", name);
271+
print!("{name_str} ");
279272
}
280273

281274
println!();
282-
283275
Ok(())
284276
}
285277

286-
fn cat_file(path: Option<&str>) -> Result<(), AeroSyscallError> {
287-
// On the `None` arm we default to 0 to take input from stdin.
288-
// This is the behaviour of `cat` that comes with any modern Linux distro.
289-
let fd = match path {
290-
Some(path) => sys_open(path, OpenFlags::O_RDONLY)?,
291-
None => 0,
292-
};
278+
fn cat_file(path: Option<&str>) -> Result<(), Box<dyn Error>> {
279+
let file = path
280+
.map(|e| File::open(e))
281+
// On the `None` arm, we take input from stdin until we get interrupted. This is the
282+
// behaviour of `cat` that comes with any modern Linux distro.
283+
.unwrap_or_else(|| File::open("/dev/tty"))?;
293284

294-
sys_seek(fd, 0, SeekWhence::SeekSet)?;
295-
296-
let mut buffer = [0; 1024];
285+
let mut reader = BufReader::new(file);
297286

298287
loop {
299-
let length = sys_read(fd, &mut buffer)?;
288+
let mut line = String::new();
289+
let size = reader.read_line(&mut line)?;
300290

301-
if length == 0 {
291+
// We have reached the end of the file.
292+
if size == 0 {
302293
break;
303294
}
304295

305-
let contents = unsafe { core::str::from_utf8_unchecked(&buffer[0..length]) };
306-
307-
print!("{}", contents);
308-
}
309-
310-
if fd != 0 {
311-
sys_close(fd)?;
296+
print!("{line}");
312297
}
313298

314-
print!("\n");
299+
println!();
315300
Ok(())
316301
}
317302

318-
fn print_kernel_log() -> Result<(), AeroSyscallError> {
303+
fn print_kernel_log() -> Result<(), Box<dyn Error>> {
319304
// dmsg is just a wrapper around `cat /dev/kmsg`
320305
// TODO: Add colored output back :^)
321306

0 commit comments

Comments
 (0)