code wiki / _hdl_build / nx_xlsx_view_gate.nx

nx_xlsx_view_gate.nx source

↩ module page · 168 lines · 11657 B

1// nx_xlsx_view_gate.nx -- gate for the .xlsx VIEWER (nx_xlsx html) + the SHAREDSTRINGS close. Two fixtures: 2// (a) our own fromtsv workbook (inlineStr+numeric) -> html table with escaped '&' round-trip; 3// (b) a FOREIGN-shaped workbook the gate authors DIRECTLY via nx_opc -- t="s" cells indexing a real 4// xl/sharedStrings.xml part (the shape Excel/Sheets actually emit; previously an HONEST LIMITATION that 5// yielded raw indices) -> `read` must resolve the INDICES to TEXT, and `html` must show the text. 6// Rows: 1 own-write 2 own-html(th/td + &-escape) 3 FOREIGN sharedStrings resolved in read 4 foreign html 7// 5 registry-dispatch(.xlsx via nx_doc_view) 6 outputs-0js 7 loud-fail-missing[neg] 8// license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_opc.nx" 11import "nx_gate_verdict.nx" 12 13func xg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n } 14func xg_p(s: *u8) -> i64 { sys_write(1, s, xg_slen(s)); return 0 } 15func xg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off+i]=s[i]; i=i+1 } return off+i } 16func xg_catn(dst: *u8, off: i64, v: i64) -> i64 { 17 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m } 18 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 } 19 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 20 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k 21} 22func xg_write(path: *u8, content: *u8) -> i64 { 23 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 } 24 sys_write(fd, content, xg_slen(content)); sys_close(fd); return 0 25} 26func xg_readall(path: *u8, szout: *i64) -> *u8 { 27 let fd: i64=sys_openat_rd(path); if fd<0 { szout[0]=0-1; return 0 as *u8 } 28 let buf: *u8=sys_mmap(8388608); var got: i64=0; var n: i64=1 29 while n>0 { n=sys_read(fd,(buf as i64+got) as *u8,131072); if n>0 { got=got+n } } 30 sys_close(fd); szout[0]=got; return buf 31} 32func xg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 33 let pid: i64=sys_fork() 34 if pid==0 { 35 if (outpath as i64)!=0 { let ofd: i64=sys_openat_wr(outpath,0x1a4); if ofd>=0 { sys_dup3(ofd,1,0); sys_dup3(ofd,2,0) } } 36 let argv: *i64=sys_mmap(128) as *i64 37 argv[0]=elf as i64; var i: i64=0; var go: i64=1 38 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } } 39 argv[i+1]=0 40 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0 41 sys_execve(elf, argv, envp); sys_exit(127) 42 } 43 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0) 44 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig } 45 return (st[0]>>8)&0xff 46} 47func xg_has(path: *u8, needle: *u8) -> i64 { 48 let szp: *i64=sys_mmap(16) as *i64; let b: *u8=xg_readall(path, szp); let sz: i64=szp[0] 49 let n: i64=xg_slen(needle); if sz<n { return 0 } 50 var i: i64=0 51 while i+n<=sz { var ok: i64=1; var j: i64=0; while j<n { if b[i+j]!=needle[j] { ok=0; j=n } else { j=j+1 } } if ok==1 { return 1 } i=i+1 } 52 return 0 53} 54func xg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 { 55 let a: *i64=sys_mmap(64) as *i64; a[0]=a1 as i64; a[1]=a2 as i64; a[2]=a3 as i64; a[3]=a4 as i64; a[4]=0; return a 56} 57func xg_row(name: *u8, pass: i64) -> i64 { 58 xg_p("ROW " as *u8); xg_p(name); if pass==1 { xg_p(" PASS\n" as *u8) } else { xg_p(" FAIL\n" as *u8) } return pass 59} 60 61// author the FOREIGN-shaped workbook: t="s" cells + a real sharedStrings part (6 OPC parts) 62func xg_write_foreign(path: *u8) -> i64 { 63 let ct: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/><Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/><Override PartName=\"/xl/worksheets/sheet1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/><Override PartName=\"/xl/sharedStrings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml\"/></Types>" as *u8 64 let rels: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/></Relationships>" as *u8 65 let wb: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<workbook xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"><sheets><sheet name=\"Sheet1\" sheetId=\"1\" r:id=\"rId1\"/></sheets></workbook>" as *u8 66 let wbr: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet\" Target=\"worksheets/sheet1.xml\"/></Relationships>" as *u8 67 let sheet: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><sheetData><row r=\"1\"><c r=\"A1\" t=\"s\"><v>0</v></c><c r=\"B1\" t=\"s\"><v>1</v></c></row><row r=\"2\"><c r=\"A2\"><v>42</v></c><c r=\"B2\" t=\"s\"><v>2</v></c></row></sheetData></worksheet>" as *u8 68 let sst: *u8="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"3\" uniqueCount=\"3\"><si><t>Alpha &amp; Beta</t></si><si><t>Second</t></si><si><t xml:space=\"preserve\">tail cell</t></si></sst>" as *u8 69 let names: *i64=sys_mmap(64) as *i64 70 let datap: *i64=sys_mmap(64) as *i64 71 let lens: *i64=sys_mmap(64) as *i64 72 names[0]="[Content_Types].xml" as *u8 as i64; datap[0]=ct as i64; lens[0]=xg_slen(ct) 73 names[1]="_rels/.rels" as *u8 as i64; datap[1]=rels as i64; lens[1]=xg_slen(rels) 74 names[2]="xl/workbook.xml" as *u8 as i64; datap[2]=wb as i64; lens[2]=xg_slen(wb) 75 names[3]="xl/_rels/workbook.xml.rels" as *u8 as i64; datap[3]=wbr as i64; lens[3]=xg_slen(wbr) 76 names[4]="xl/worksheets/sheet1.xml" as *u8 as i64; datap[4]=sheet as i64; lens[4]=xg_slen(sheet) 77 names[5]="xl/sharedStrings.xml" as *u8 as i64; datap[5]=sst as i64; lens[5]=xg_slen(sst) 78 return opc_write(path, names, datap, lens, 6) 79} 80 81func main(argc: i64, argv: *i64) -> i64 { 82 xg_p("=== XLSX VIEWER GATE: sheet -> inline table + SHAREDSTRINGS (foreign Excel shape) resolved ===\n" as *u8) 83 let xl: *u8="buildroot/_build/nx_xlsx.sov.elf" as *u8 84 let pr: i64=sys_openat_rd(xl) 85 if pr>=0 { sys_close(pr) } else { 86 xg_p(" instrument missing -> rebuilding\n" as *u8) 87 xg_runv("_offc/nx_sov_build_run.elf" as *u8, xg_args4("nx_xlsx" as *u8, "--build-only" as *u8, 0 as *u8, 0 as *u8), "/tmp/xg_rebuild.out" as *u8) 88 } 89 90 var pass: i64=0; var r: i64=0 91 92 // row 1: our own workbook via fromtsv 93 xg_write("/tmp/xg_t.tsv" as *u8, "Item\tOwner\tCost\nVenue\tAlice\t350\nCatering\tBob & Sons\t480\n" as *u8) 94 let rc1: i64=xg_runv(xl, xg_args4("fromtsv" as *u8, "/tmp/xg_t.tsv" as *u8, "/tmp/xg_own.xlsx" as *u8, 0 as *u8), "/tmp/xg_r1.txt" as *u8) 95 r=0; if rc1==0 { if xg_has("/tmp/xg_r1.txt" as *u8, "XLSX-WRITE-OK" as *u8)==1 { r=1 } } 96 pass=pass+xg_row("own-write" as *u8, r) 97 98 // row 2: html view of our workbook (header th, data td, '&' escaped) 99 let rc2: i64=xg_runv(xl, xg_args4("html" as *u8, "/tmp/xg_own.xlsx" as *u8, "/tmp/xg_own.html" as *u8, "/sample.xlsx" as *u8), "/tmp/xg_r2.txt" as *u8) 100 r=0 101 if rc2==0 { if xg_has("/tmp/xg_r2.txt" as *u8, "XLSX-HTML-OK" as *u8)==1 { 102 if xg_has("/tmp/xg_own.html" as *u8, "<th>Item</th>" as *u8)==1 { 103 if xg_has("/tmp/xg_own.html" as *u8, "<td>350</td>" as *u8)==1 { 104 if xg_has("/tmp/xg_own.html" as *u8, "<td>Bob &amp; Sons</td>" as *u8)==1 { r=1 } 105 } 106 } 107 } } 108 pass=pass+xg_row("own-html-table" as *u8, r) 109 110 // row 3: FOREIGN sharedStrings workbook -> read resolves INDICES to TEXT 111 xg_write_foreign("/tmp/xg_foreign.xlsx" as *u8) 112 let rc3: i64=xg_runv(xl, xg_args4("read" as *u8, "/tmp/xg_foreign.xlsx" as *u8, 0 as *u8, 0 as *u8), "/tmp/xg_r3.txt" as *u8) 113 r=0 114 if rc3==0 { if xg_has("/tmp/xg_r3.txt" as *u8, "Alpha & Beta\tSecond" as *u8)==1 { 115 if xg_has("/tmp/xg_r3.txt" as *u8, "42\ttail cell" as *u8)==1 { r=1 } 116 } } 117 pass=pass+xg_row("foreign-sharedstrings-resolved" as *u8, r) 118 119 // row 4: html view of the foreign workbook shows the resolved text 120 let rc4: i64=xg_runv(xl, xg_args4("html" as *u8, "/tmp/xg_foreign.xlsx" as *u8, "/tmp/xg_foreign.html" as *u8, 0 as *u8), "/tmp/xg_r4.txt" as *u8) 121 r=0 122 if rc4==0 { if xg_has("/tmp/xg_foreign.html" as *u8, "<th>Alpha &amp; Beta</th>" as *u8)==1 { 123 if xg_has("/tmp/xg_foreign.html" as *u8, "<td>tail cell</td>" as *u8)==1 { r=1 } 124 } } 125 pass=pass+xg_row("foreign-html" as *u8, r) 126 127 // row 5: registry dispatch routes .xlsx (requires the blessed dispatcher + blessed nx_xlsx) 128 let rc5: i64=xg_runv("_offc/nx_doc_view.elf" as *u8, xg_args4("/tmp/xg_own.xlsx" as *u8, "/tmp/xg_dv.html" as *u8, "/sample.xlsx" as *u8, 0 as *u8), "/tmp/xg_r5.txt" as *u8) 129 r=0; if rc5==0 { if xg_has("/tmp/xg_r5.txt" as *u8, "DOC-VIEW-OK ext=xlsx" as *u8)==1 { if xg_has("/tmp/xg_dv.html" as *u8, "<th>Item</th>" as *u8)==1 { r=1 } } } 130 pass=pass+xg_row("registry-dispatch-xlsx" as *u8, r) 131 132 // row 6: outputs sovereign 0-JS 133 r=1 134 if xg_has("/tmp/xg_own.html" as *u8, "<script" as *u8)==1 { r=0 } 135 if xg_has("/tmp/xg_foreign.html" as *u8, "<script" as *u8)==1 { r=0 } 136 pass=pass+xg_row("outputs-0js" as *u8, r) 137 138 // row 7 (neg): missing workbook loud-fails 139 let rc7: i64=xg_runv(xl, xg_args4("html" as *u8, "/tmp/xg_nope.xlsx" as *u8, "/tmp/xg_x.html" as *u8, 0 as *u8), "/tmp/xg_r7.txt" as *u8) 140 r=0; if rc7==1 { if xg_has("/tmp/xg_r7.txt" as *u8, "XLSX-HTML-FAIL" as *u8)==1 { r=1 } } 141 pass=pass+xg_row("loud-fail-missing" as *u8, r) 142 143 let permil: i64=(pass*1000)/7 144 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 145 var fdi: i64=0 146 while fdi<2 { 147 var fd: i64=1; if fdi==1 { fd=logfd } 148 if fd>0 { 149 let line: *u8=sys_mmap(256); var o: i64=0 150 o=xg_cat(line,o,"XLSX-VIEW-GATE epoch=" as *u8); o=xg_catn(line,o,sys_now_realtime_sec()) 151 o=xg_cat(line,o," rows=7 pass=" as *u8); o=xg_catn(line,o,pass) 152 o=xg_cat(line,o," permil=" as *u8); o=xg_catn(line,o,permil) 153 if pass==7 { o=xg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=xg_cat(line,o," verdict=RED\n" as *u8) } 154 sys_write(fd, line, o) 155 } 156 fdi=fdi+1 157 } 158 if logfd>0 { sys_close(logfd) } 159 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 160 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 161 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 162 let ctr__dry: *i64 = gv_ctr() 163 ctr__dry[0] = pass 164 ctr__dry[1] = 7 165 let rc__dry: i64 = gv_verdict("XLSX-VIEW-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 166 sys_exit(rc__dry) 167 return rc__dry 168}