code wiki / _hdl_build / nx_shell_run.nx
nx_shell_run.nx source
↩ module page · 42 lines · 2347 B
1// nx_shell_run.nx -- the DRIVABLE sovereign Nishi shell: reads a script (knowledge/shell/run.nsh) and executes each
2// line via the GENERAL nx_shell_lib's full executor (pipelines | + control flow ; && ||). This is the working bash
3// replacement -- write a .nsh, run this, get output. Exits with the last line's exit code. (lines starting with # = comments)
4// expect_exit: 0 Sovereign: nx_shell_lib (+ nx_syscalls). NEVER-BRICK: spawns userspace, 0 firmware.
5import "nx_shell_lib.nx"
6import "nx_syscalls.nx"
7const K_MAGIC_65536: i64 = 65536
8const K_MAGIC_4096: i64 = 4096
9
10func run_line(buf: *u8, start: i64, end: i64, line: *u8) -> i64 {
11 let len: i64 = end-start
12 if len<=0 { return 0-2 } // empty -> sentinel "skipped"
13 var j: i64=0; while j<len { line[j]=buf[start+j]; j=j+1 } line[len]=0 as u8
14 if line[0]==(35 as u8) { return 0-2 } // '#' comment -> skipped
15 g_puts("$ "); sys_write(1, line, len); g_puts("\n" as *u8)
16 return sh_exec_full(line)
17}
18
19func main(argc: i64, argv: *i64) -> i64 {
20 // S-CLASS 2026-06-25: the script path is ARG-DRIVEN (default knowledge/shell/run.nsh) so ANY .nsh can run
21 // without clobbering the shared default -- the same fixed-path -> arg-driven fix as nx_crawl_web's seed.
22 var script: *u8 = "knowledge/shell/run.nsh" as *u8
23 if argc >= 2 { script = argv[1] as *u8 }
24 g_puts("nx_shell_run (DRIVABLE sovereign Nishi shell; pipelines + control flow) script="); g_puts(script); g_puts("\n" as *u8)
25 let buf: *u8 = sys_mmap(K_MAGIC_65536)
26 let fd: i64 = sys_openat_rd(script)
27 if fd<0 { g_puts(" no script at "); g_puts(script); g_puts(" -- nothing to run\n" as *u8); sys_exit(0); return 0 }
28 var n: i64=0; var go: i64=1
29 while go==1 { let k: i64=sys_read(fd, buf+n as i64, K_MAGIC_65536-n); if k<=0 { go=0 } else { n=n+k; if n>=K_MAGIC_65536 { go=0 } } }
30 sys_close(fd)
31
32 let line: *u8 = sys_mmap(K_MAGIC_4096)
33 var last: i64=0; var start: i64=0; var i: i64=0
34 while i<n {
35 if buf[i]==(10 as u8) { let r: i64=run_line(buf, start, i, line); if r != (0-2) { last=r } start=i+1 }
36 i=i+1
37 }
38 if start<n { let r: i64=run_line(buf, start, n, line); if r != (0-2) { last=r } }
39
40 g_puts("---- nx_shell_run done, last exit = "); g_pn(last); g_puts(" ----\n" as *u8)
41 sys_exit(last); return last
42}