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