summaryrefslogtreecommitdiffstats
path: root/bins/cat
diff options
context:
space:
mode:
Diffstat (limited to 'bins/cat')
-rw-r--r--bins/cat/Cargo.toml8
-rw-r--r--bins/cat/src/main.rs68
2 files changed, 76 insertions, 0 deletions
diff --git a/bins/cat/Cargo.toml b/bins/cat/Cargo.toml
new file mode 100644
index 0000000..c5990d5
--- /dev/null
+++ b/bins/cat/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2name = "cat"
3version.workspace = true
4edition.workspace = true
5[lints]
6workspace = true
7[dependencies]
8rsapi = { path = "../../rsapi" }
diff --git a/bins/cat/src/main.rs b/bins/cat/src/main.rs
new file mode 100644
index 0000000..6683c83
--- /dev/null
+++ b/bins/cat/src/main.rs
@@ -0,0 +1,68 @@
1//! cat — print files or stdin via the exports table.
2//!
3//! Args: [path ...] — each path is printed as raw text.
4//! No args = read from get_line (stdin) until EOF.
5
6#![no_std]
7#![no_main]
8
9#[panic_handler]
10fn panic(_info: &core::panic::PanicInfo) -> ! {
11 loop { core::hint::spin_loop(); }
12}
13
14fn put(exports: &rsapi::Exports, buf: &[u8]) {
15 (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: buf.len() });
16}
17
18/// Read one file and put_line each chunk.
19fn dump_file(exports: &rsapi::Exports, path: &str) -> i32 {
20 let h = (exports.fs_resolve)(rsapi::Line { ptr: path.as_ptr(), len: path.len() });
21 if h < 0 {
22 return -1;
23 }
24 let mut buf = [0u8; 512];
25 let mut pos: u64 = 0;
26 loop {
27 let n = (exports.fs_read)(h, buf.as_mut_ptr(), pos, 512);
28 if n <= 0 { break; }
29 if !(exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: n as usize }) {
30 break; // pipeline says stop
31 }
32 pos += n as u64;
33 }
34 (exports.fs_close)(h);
35 0
36}
37
38#[unsafe(no_mangle)]
39pub extern "C" fn efi_main(exports: &'static rsapi::Exports, argc: usize, argv: *const rsapi::Arg) -> i32 {
40 let args = unsafe { core::slice::from_raw_parts(argv, argc) };
41 if argc > 0 {
42 // Print each path argument
43 let mut code = 0;
44 for a in args.iter() {
45 let path = core::str::from_utf8(unsafe {
46 core::slice::from_raw_parts(a.ptr, a.len)
47 }).unwrap_or("");
48 if dump_file(exports, path) != 0 {
49 code = 1;
50 }
51 }
52 code
53 } else {
54 // No args: read from stdin (get_line) until EOF/Abort
55 loop {
56 let mut line = rsapi::Line { ptr: core::ptr::null(), len: 0 };
57 match (exports.get_line)(&mut line) {
58 rsapi::StreamResult::Line => {
59 put(exports, unsafe {
60 core::slice::from_raw_parts(line.ptr, line.len)
61 });
62 }
63 rsapi::StreamResult::Eof | rsapi::StreamResult::Abort => break,
64 }
65 }
66 0
67 }
68}