Nothing to see here, move along
1#![no_std]
2#![no_main]
3
4use lancer_user::fs;
5use lancer_user::net;
6use lancer_user::syscall;
7use lancer_user::{print, println};
8
9fn print_chunked(data: &[u8]) {
10 data.split_inclusive(|&b| b == b'\n')
11 .for_each(|segment| match core::str::from_utf8(segment) {
12 Ok(s) => print!("{}", s),
13 Err(_) => lancer_user::io::write_bytes(segment),
14 });
15}
16
17#[unsafe(no_mangle)]
18pub extern "C" fn lancer_main() -> ! {
19 let (rx, _tx) = match net::init() {
20 Some(pair) => pair,
21 None => {
22 lancer_user::show!(net, error, "netsock init failed");
23 syscall::exit();
24 }
25 };
26
27 let mut args_buf = [0u8; 256];
28 let args_len = net::recv_args(&rx, &mut args_buf);
29 let args = &args_buf[..args_len];
30
31 let (_cmd, rest) = net::next_token(args);
32 let (path_tok, _) = net::next_token(rest);
33
34 if path_tok.is_empty() {
35 println!("usage: cat <path>");
36 syscall::exit();
37 }
38
39 let mut client = unsafe { fs::init() };
40
41 let handle = match fs::open_path_with_rights(&mut client, 0, path_tok, fs::FsRights::READ) {
42 Ok(h) => h,
43 Err(e) => {
44 println!("cat: {}", e.name());
45 syscall::exit();
46 }
47 };
48
49 let mut buf = [0u8; 4096];
50 fn read_all(client: &mut fs::FsClient, handle: u8, buf: &mut [u8], offset: u64) {
51 match client.read(handle, offset, buf) {
52 Ok(0) => {}
53 Ok(n) => {
54 let data = &buf[..n];
55 print_chunked(data);
56 read_all(client, handle, buf, offset + n as u64)
57 }
58 Err(e) => {
59 println!("cat: read error: {}", e.name());
60 }
61 }
62 }
63 read_all(&mut client, handle, &mut buf, 0);
64
65 let _ = client.close(handle);
66 syscall::exit()
67}