summaryrefslogtreecommitdiffstats
path: root/bins/cat/src/main.rs
blob: 6683c83c5a12b01fe98a4876f086f7a14e7b401d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! cat — print files or stdin via the exports table.
//!
//! Args: [path ...] — each path is printed as raw text.
//! No args = read from get_line (stdin) until EOF.

#![no_std]
#![no_main]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop { core::hint::spin_loop(); }
}

fn put(exports: &rsapi::Exports, buf: &[u8]) {
    (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: buf.len() });
}

/// Read one file and put_line each chunk.
fn dump_file(exports: &rsapi::Exports, path: &str) -> i32 {
    let h = (exports.fs_resolve)(rsapi::Line { ptr: path.as_ptr(), len: path.len() });
    if h < 0 {
        return -1;
    }
    let mut buf = [0u8; 512];
    let mut pos: u64 = 0;
    loop {
        let n = (exports.fs_read)(h, buf.as_mut_ptr(), pos, 512);
        if n <= 0 { break; }
        if !(exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: n as usize }) {
            break; // pipeline says stop
        }
        pos += n as u64;
    }
    (exports.fs_close)(h);
    0
}

#[unsafe(no_mangle)]
pub extern "C" fn efi_main(exports: &'static rsapi::Exports, argc: usize, argv: *const rsapi::Arg) -> i32 {
    let args = unsafe { core::slice::from_raw_parts(argv, argc) };
    if argc > 0 {
        // Print each path argument
        let mut code = 0;
        for a in args.iter() {
            let path = core::str::from_utf8(unsafe {
                core::slice::from_raw_parts(a.ptr, a.len)
            }).unwrap_or("");
            if dump_file(exports, path) != 0 {
                code = 1;
            }
        }
        code
    } else {
        // No args: read from stdin (get_line) until EOF/Abort
        loop {
            let mut line = rsapi::Line { ptr: core::ptr::null(), len: 0 };
            match (exports.get_line)(&mut line) {
                rsapi::StreamResult::Line => {
                    put(exports, unsafe {
                        core::slice::from_raw_parts(line.ptr, line.len)
                    });
                }
                rsapi::StreamResult::Eof | rsapi::StreamResult::Abort => break,
            }
        }
        0
    }
}