code wiki / _hdl_build / nx_shell.nx
nx_shell.nx source
↩ module page · 93 lines · 7054 B
1// nx_shell.nx -- the SOVEREIGN NISHI SHELL gate (R0-R6), now on the GENERAL nx_shell_lib (rule 15: one capability,
2// not copies). Proves every rung; the drivable tool is nx_shell_run. NEVER-BRICK: spawns userspace, 0 firmware.
3// R0 process / R1 tokenize / R2 exec(runs a real organ) / R3 builtins / R4 capture+grep / R5 control / R6 pipeline.
4// expect_exit: 0 Sovereign: nx_shell_lib (+ nx_syscalls).
5import "nx_shell_lib.nx"
6import "nx_syscalls.nx"
7const K_MAGIC_4096: i64 = 4096
8
9func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
10func sh_fork_exit(code: i64) -> i64 { let pid: i64=sys_fork(); if pid==0 { sys_exit(code); return 0 } let st: *i64=sys_mmap(8); sys_wait4(pid, st, 0); return wait_exit_code(st[0]) }
11
12func main() -> i64 {
13 g_puts("nx_shell (SOVEREIGN Nishi shell gate, on nx_shell_lib: R0 process / R1 tokenize / R2 exec / R3 builtins / R4 capture+grep / R5 control / R6 pipeline)\n" as *u8)
14 var pass: i64=0; var total: i64=0
15
16 let r0: i64 = sh_fork_exit(42)
17 g_puts(" R0 fork->child exit(42)->wait = "); g_pn(r0); g_puts("\n" as *u8)
18 var t1: i64=0; if r0==42 { t1=1 }
19 pass=pass+ck("R0: fork a child, reap it, extract exit code" as *u8, t1); total=total+1
20
21 let copy: *u8 = sys_mmap(256); let argv: *i64 = sys_mmap(32*8) as *i64
22 let argc: i64 = sh_tokenize("echo hi there" as *u8, copy, argv, 32)
23 let a0: *u8 = argv[0] as *u8; let a2: *u8 = argv[2] as *u8
24 var tokok: i64=0
25 if argc==3 { if a0[0]==(101 as u8) { if a0[4]==(0 as u8) { if a2[0]==(116 as u8) { tokok=1 } } } }
26 g_puts(" R1 tokenize -> argc="); g_pn(argc); g_puts(" argv[0]="); g_puts(a0); g_puts(" argv[2]="); g_puts(a2); g_puts("\n" as *u8)
27 pass=pass+ck("R1: command line -> null-terminated argv (argc=3, echo, there)" as *u8, tokok); total=total+1
28
29 g_puts(" R2 running a Nishi organ through the shell: _offc/nx_sov_build_run.elf nx_riscv_gate ...\n" as *u8)
30 let rcopy: *u8 = sys_mmap(256); let rargv: *i64 = sys_mmap(32*8) as *i64
31 let rargc: i64 = sh_tokenize("_offc/nx_sov_build_run.elf nx_riscv_gate" as *u8, rcopy, rargv, 32)
32 let rc: i64 = sh_run("_offc/nx_sov_build_run.elf" as *u8, rargv)
33 g_puts(" R2 child exit = "); g_pn(rc); g_puts("\n" as *u8)
34 var t3: i64=0; if rc==0 { t3=1 }
35 pass=pass+ck("R2: shell fork+execve'd a REAL Nishi organ and reaped exit 0 -- runs the ecosystem" as *u8, t3); total=total+1
36
37 let bc: *u8 = sys_mmap(64); let bav: *i64 = sys_mmap(8*8) as *i64
38 let n_t: i64 = sh_tokenize("true" as *u8, bc, bav, 8); let rt_t: i64 = sh_builtin(bav, n_t)
39 let bc2: *u8 = sys_mmap(64); let bav2: *i64 = sys_mmap(8*8) as *i64
40 let n_f: i64 = sh_tokenize("false" as *u8, bc2, bav2, 8); let rt_f: i64 = sh_builtin(bav2, n_f)
41 let bc3: *u8 = sys_mmap(64); let bav3: *i64 = sys_mmap(8*8) as *i64
42 let n_e: i64 = sh_tokenize("echo nishi shell" as *u8, bc3, bav3, 8); g_puts(" R3 echo builtin -> "); let rt_e: i64 = sh_builtin(bav3, n_e)
43 var t4: i64=0; if rt_t==0 { if rt_f==1 { if rt_e==0 { t4=1 } } }
44 pass=pass+ck("R3: builtins in-process (true->0, false->1, echo printed)" as *u8, t4); total=total+1
45
46 g_puts(" R4 capture a child's stdout via pipe, then sovereign grep...\n" as *u8)
47 let pb: *i64 = sys_mmap(16); sys_pipe2(pb, 0)
48 let rfd: i64 = pb[0] & 0xFFFFFFFF; let wfd: i64 = (pb[0] >> 32) & 0xFFFFFFFF
49 let cpid: i64 = sys_fork()
50 if cpid==0 { sys_dup3(wfd,1,0); sys_close(rfd); sys_close(wfd); g_puts("STATUS GREEN 6 OK\n" as *u8); sys_exit(0); return 0 }
51 sys_close(wfd)
52 let cb: *u8 = sys_mmap(512); var ct: i64=0; var cgo: i64=1
53 while cgo==1 { let k: i64=sys_read(rfd, cb+ct as i64, 512-ct); if k<=0 { cgo=0 } else { ct=ct+k } }
54 sys_close(rfd); let st2: *i64=sys_mmap(8); sys_wait4(cpid, st2, 0)
55 let found: i64 = sh_grep(cb, ct, "GREEN" as *u8); let notfound: i64 = sh_grep(cb, ct, "RED" as *u8)
56 g_puts(" R4 captured "); g_pn(ct); g_puts(" bytes; grep GREEN="); g_pn(found); g_puts(" grep RED="); g_pn(notfound); g_puts("\n" as *u8)
57 var t5: i64=0; if found==1 { if notfound==0 { t5=1 } }
58 pass=pass+ck("R4: pipe-capture + sovereign grep (replaces 'run | grep')" as *u8, t5); total=total+1
59
60 g_puts(" R5 control flow (; && ||):\n" as *u8)
61 let e1: i64 = sh_exec_line("true && echo ran" as *u8)
62 let e2: i64 = sh_exec_line("false && echo SHOULD-NOT" as *u8)
63 let e3: i64 = sh_exec_line("false || echo recovered" as *u8)
64 let e4: i64 = sh_exec_line("true ; false" as *u8)
65 g_puts(" e1="); g_pn(e1); g_puts(" e2="); g_pn(e2); g_puts(" e3="); g_pn(e3); g_puts(" e4="); g_pn(e4); g_puts("\n" as *u8)
66 var t6: i64=0; if e1==0 { if e2==1 { if e3==0 { if e4==1 { t6=1 } } } }
67 pass=pass+ck("R5: && runs-on-success, || runs-on-failure, ; sequences" as *u8, t6); total=total+1
68
69 g_puts(" R6 pipeline: <producer 4 lines> | grep GREEN ...\n" as *u8)
70 let pa: *i64 = sys_mmap(16); sys_pipe2(pa, 0); let aw: i64 = (pa[0]>>32)&0xFFFFFFFF; let ar: i64 = pa[0]&0xFFFFFFFF
71 let pbp: *i64 = sys_mmap(16); sys_pipe2(pbp, 0); let bw: i64 = (pbp[0]>>32)&0xFFFFFFFF; let br: i64 = pbp[0]&0xFFFFFFFF
72 let s1: i64 = sys_fork()
73 if s1==0 { sys_dup3(aw,1,0); sys_close(ar); sys_close(aw); sys_close(br); sys_close(bw); g_puts("x1 line\nGREEN a\nx2 line\nGREEN b\n" as *u8); sys_exit(0); return 0 }
74 let s2: i64 = sys_fork()
75 if s2==0 { sys_dup3(ar,0,0); sys_dup3(bw,1,0); sys_close(aw); sys_close(ar); sys_close(bw); sys_close(br); sh_grep_stream("GREEN" as *u8); sys_exit(0); return 0 }
76 sys_close(aw); sys_close(ar); sys_close(bw)
77 let ob: *u8 = sys_mmap(K_MAGIC_4096); var on: i64=0; var pgo: i64=1
78 while pgo==1 { let k: i64=sys_read(br, ob+on as i64, K_MAGIC_4096-on); if k<=0 { pgo=0 } else { on=on+k } }
79 sys_close(br); let ps1: *i64=sys_mmap(8); sys_wait4(s1, ps1, 0); let ps2: *i64=sys_mmap(8); sys_wait4(s2, ps2, 0)
80 g_puts(" R6 final ("); g_pn(on); g_puts(" bytes):\n" as *u8); sys_write(1, ob, on)
81 let fa: i64=sh_grep(ob,on,"GREEN a" as *u8); let fb: i64=sh_grep(ob,on,"GREEN b" as *u8); let nx1: i64=sh_grep(ob,on,"x1" as *u8); let nx2: i64=sh_grep(ob,on,"x2" as *u8)
82 var t7: i64=0; if fa==1 { if fb==1 { if nx1==0 { if nx2==0 { t7=1 } } } }
83 pass=pass+ck("R6: 2-stage pipeline producer|grep wired via pipes, filtered output correct" as *u8, t7); total=total+1
84
85 var okall: i64=0; if pass==total { okall=1 }
86 g_puts("---- nx_shell: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
87 if okall==1 {
88 let logf: i64=sys_openat_append("knowledge/status/shell.log" as *u8, 420)
89 if logf>=0 { let z: i64=sys_write(logf,"NXSHELL R0-R6 GREEN on nx_shell_lib: process/tokenize/exec/builtins/capture+grep/control/pipeline (bash replacement)\n" as *u8,116); sys_close(logf) }
90 g_puts("verdict=GREEN (sovereign Nishi shell R0-R6 on the GENERAL nx_shell_lib; runs the ecosystem; NO bash)\n" as *u8); sys_exit(0); return 0
91 }
92 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
93}