code wiki / (root) / nx_compare_staged_capture.nx

nx_compare_staged_capture.nx source

↩ module page · 87 lines · 4414 B

1// Test-only staged matrix capture. No source edits or index publication. 2// WARNING: matrix HTML rendering can refresh public reference assets via rp_publish. 3// Do not run against the real tree without accepting that existing emitter side effect. 4// args: <unique-output-stem> <capture-bytes> <timeout-ms>; all limits are caller supplied. 5import "nx_tool_run.nx" 6import "nx_itoa_lib.nx" 7const CRM_I64_MAX: i64 = 9223372036854775807 8const CRM_PATH_CAP: i64 = 512 9func crm_out(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return sys_write(1, s, n) } 10func crm_decimal(s: *u8) -> i64 { 11 var n: i64 = 0; var i: i64 = 0 12 if s[0] == (0 as u8) { return 0 } 13 while s[i] != (0 as u8) { 14 let c: i64 = s[i] as i64 15 if c < 48 { return 0 }; if c > 57 { return 0 } 16 let d: i64 = c - 48 17 if n > (CRM_I64_MAX - d) / 10 { return 0 } 18 n = n * 10 + d; i = i + 1 19 } 20 return n 21} 22func crm_append(p: *u8, at: i64, s: *u8) -> i64 { 23 var i: i64 = 0; var o: i64 = at 24 while s[i] != (0 as u8) { p[o] = s[i]; o = o + 1; i = i + 1 } 25 p[o] = 0 as u8 26 return o 27} 28func main(argc: i64, argv: *i64) -> i64 { 29 if argc != 4 { crm_out("usage: nx_compare_staged_capture <unique-output-stem> <capture-bytes> <timeout-ms>\n" as *u8); return 2 } 30 let capacity: i64 = crm_decimal(argv[2] as *u8) 31 let deadline: i64 = crm_decimal(argv[3] as *u8) 32 if capacity <= 0 { crm_out("REFUSED invalid capture bytes\n" as *u8); return 2 } 33 if deadline <= 0 { crm_out("REFUSED invalid timeout\n" as *u8); return 2 } 34 let path: *u8 = sys_mmap(CRM_PATH_CAP) 35 if (path as i64) <= 0 { return 3 } 36 let prefix_len: i64 = crm_append(path, 0, "knowledge/status/" as *u8) 37 let name: *u8 = argv[1] as *u8 38 var ni: i64 = 0 39 while name[ni] != (0 as u8) { 40 if prefix_len + ni + 6 >= CRM_PATH_CAP { crm_out("REFUSED output stem too long\n" as *u8); return 2 } 41 let c: i64 = name[ni] as i64 42 var ok: i64 = 0 43 if c >= 97 { if c <= 122 { ok = 1 } } 44 if c >= 48 { if c <= 57 { ok = 1 } } 45 if c == 95 { ok = 1 }; if c == 45 { ok = 1 } 46 if ok == 0 { crm_out("REFUSED invalid output stem\n" as *u8); return 2 } 47 ni = ni + 1 48 } 49 if ni == 0 { return 2 } 50 let no: i64 = crm_append(path, prefix_len, name) 51 crm_append(path, no, ".html" as *u8) 52 if sys_chdir("buildroot" as *u8) != 0 { crm_out("REFUSED cannot chdir buildroot\n" as *u8); return 3 } 53 let prior: i64 = sys_openat_rd(path) 54 if prior >= 0 { sys_close(prior); crm_out("REFUSED output already exists; choose a new test stem\n" as *u8); return 3 } 55 let output: *u8 = sys_mmap(capacity) 56 if (output as i64) <= 0 { crm_out("REFUSED capture allocation failed\n" as *u8); return 3 } 57 let av: *i64 = sys_mmap(32) as *i64 58 av[0] = "_build/nx_swcompare_matrix.sov.elf" as i64 59 av[1] = "deploy" as i64; av[2] = "html" as i64; av[3] = 0 60 let olen: *i64 = sys_mmap(16) as *i64 61 let cut: *i64 = sys_mmap(16) as *i64 62 let rc: i64 = tr_run_capture_tr(av[0] as *u8, av, output, capacity, olen, deadline, cut) 63 crm_out("CAPTURE child_rc=" as *u8); nxi_out(rc); crm_out(" bytes=" as *u8); nxi_out(olen[0]); crm_out(" truncated=" as *u8); nxi_out(cut[0]); crm_out("\n" as *u8) 64 if rc != 0 { return 4 } 65 if cut[0] != TR_FIT { return 4 } 66 if olen[0] <= 0 { return 4 } 67 if tr_contains(output, olen[0], "<html" as *u8) != 1 { return 4 } 68 if tr_contains(output, olen[0], "</html>" as *u8) != 1 { return 4 } 69 let ofd: i64 = sys_openat_wr(path, 384) 70 if ofd < 0 { crm_out("REFUSED cannot create test output\n" as *u8); return 5 } 71 var done: i64 = 0 72 while done < olen[0] { 73 let wrote: i64 = sys_write(ofd, (output as i64 + done) as *u8, olen[0] - done) 74 if wrote <= 0 { sys_close(ofd); crm_out("FAILED short write; partial test artifact retained\n" as *u8); return 5 } 75 done = done + wrote 76 } 77 if sys_close(ofd) != 0 { return 5 } 78 let vlen: *i64 = sys_mmap(16) as *i64 79 let verify: *u8 = sys_read_file(path, vlen) 80 if (verify as i64) == 0 { return 6 } 81 if vlen[0] != olen[0] { return 6 } 82 var vi: i64 = 0 83 while vi < olen[0] { if verify[vi] != output[vi] { return 6 }; vi = vi + 1 } 84 crm_out("CAPTURE-VERIFIED path=buildroot/" as *u8); crm_out(path); crm_out(" bytes=" as *u8); nxi_out(olen[0]); crm_out(" index-publish-invoked=0\n" as *u8) 85 return 0 86} 87