code wiki / _hdl_build / nx_xlsx_view_gate.nx

nx_xlsx_view_gate.nx source

↩ module page · 160 lines · 11108 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" 11 12func xg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n } 13func xg_p(s: *u8) -> i64 { sys_write(1, s, xg_slen(s)); return 0 } 14func 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 } 15func xg_catn(dst: *u8, off: i64, v: i64) -> i64 { 16 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m } 17 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 } 18 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } 19 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k 20} 21func xg_write(path: *u8, content: *u8) -> i64 { 22 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 } 23 sys_write(fd, content, xg_slen(content)); sys_close(fd); return 0 24} 25func xg_readall(path: *u8, szout: *i64) -> *u8 { 26 let fd: i64=sys_openat_rd(path); if fd<0 { szout[0]=0-1; return 0 as *u8 } 27 let buf: *u8=sys_mmap(8388608); var got: i64=0; var n: i64=1 28 while n>0 { n=sys_read(fd,(buf as i64+got) as *u8,131072); if n>0 { got=got+n } } 29 sys_close(fd); szout[0]=got; return buf 30} 31func xg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 { 32 let pid: i64=sys_fork() 33 if pid==0 { 34 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) } } 35 let argv: *i64=sys_mmap(128) as *i64 36 argv[0]=elf as i64; var i: i64=0; var go: i64=1 37 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } } 38 argv[i+1]=0 39 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0 40 sys_execve(elf, argv, envp); sys_exit(127) 41 } 42 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0) 43 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig } 44 return (st[0]>>8)&0xff 45} 46func xg_has(path: *u8, needle: *u8) -> i64 { 47 let szp: *i64=sys_mmap(16) as *i64; let b: *u8=xg_readall(path, szp); let sz: i64=szp[0] 48 let n: i64=xg_slen(needle); if sz<n { return 0 } 49 var i: i64=0 50 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 } 51 return 0 52} 53func xg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 { 54 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 55} 56func xg_row(name: *u8, pass: i64) -> i64 { 57 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 58} 59 60// author the FOREIGN-shaped workbook: t="s" cells + a real sharedStrings part (6 OPC parts) 61func xg_write_foreign(path: *u8) -> i64 { 62 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 63 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 64 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 65 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 66 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 67 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 68 let names: *i64=sys_mmap(64) as *i64 69 let datap: *i64=sys_mmap(64) as *i64 70 let lens: *i64=sys_mmap(64) as *i64 71 names[0]="[Content_Types].xml" as *u8 as i64; datap[0]=ct as i64; lens[0]=xg_slen(ct) 72 names[1]="_rels/.rels" as *u8 as i64; datap[1]=rels as i64; lens[1]=xg_slen(rels) 73 names[2]="xl/workbook.xml" as *u8 as i64; datap[2]=wb as i64; lens[2]=xg_slen(wb) 74 names[3]="xl/_rels/workbook.xml.rels" as *u8 as i64; datap[3]=wbr as i64; lens[3]=xg_slen(wbr) 75 names[4]="xl/worksheets/sheet1.xml" as *u8 as i64; datap[4]=sheet as i64; lens[4]=xg_slen(sheet) 76 names[5]="xl/sharedStrings.xml" as *u8 as i64; datap[5]=sst as i64; lens[5]=xg_slen(sst) 77 return opc_write(path, names, datap, lens, 6) 78} 79 80func main(argc: i64, argv: *i64) -> i64 { 81 xg_p("=== XLSX VIEWER GATE: sheet -> inline table + SHAREDSTRINGS (foreign Excel shape) resolved ===\n" as *u8) 82 let xl: *u8="/tmp/nx_xlsx.sov.elf" as *u8 83 let pr: i64=sys_openat_rd(xl) 84 if pr>=0 { sys_close(pr) } else { 85 xg_p(" instrument missing -> rebuilding\n" as *u8) 86 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) 87 } 88 89 var pass: i64=0; var r: i64=0 90 91 // row 1: our own workbook via fromtsv 92 xg_write("/tmp/xg_t.tsv" as *u8, "Item\tOwner\tCost\nVenue\tAlice\t350\nCatering\tBob & Sons\t480\n" as *u8) 93 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) 94 r=0; if rc1==0 { if xg_has("/tmp/xg_r1.txt" as *u8, "XLSX-WRITE-OK" as *u8)==1 { r=1 } } 95 pass=pass+xg_row("own-write" as *u8, r) 96 97 // row 2: html view of our workbook (header th, data td, '&' escaped) 98 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) 99 r=0 100 if rc2==0 { if xg_has("/tmp/xg_r2.txt" as *u8, "XLSX-HTML-OK" as *u8)==1 { 101 if xg_has("/tmp/xg_own.html" as *u8, "<th>Item</th>" as *u8)==1 { 102 if xg_has("/tmp/xg_own.html" as *u8, "<td>350</td>" as *u8)==1 { 103 if xg_has("/tmp/xg_own.html" as *u8, "<td>Bob &amp; Sons</td>" as *u8)==1 { r=1 } 104 } 105 } 106 } } 107 pass=pass+xg_row("own-html-table" as *u8, r) 108 109 // row 3: FOREIGN sharedStrings workbook -> read resolves INDICES to TEXT 110 xg_write_foreign("/tmp/xg_foreign.xlsx" as *u8) 111 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) 112 r=0 113 if rc3==0 { if xg_has("/tmp/xg_r3.txt" as *u8, "Alpha & Beta\tSecond" as *u8)==1 { 114 if xg_has("/tmp/xg_r3.txt" as *u8, "42\ttail cell" as *u8)==1 { r=1 } 115 } } 116 pass=pass+xg_row("foreign-sharedstrings-resolved" as *u8, r) 117 118 // row 4: html view of the foreign workbook shows the resolved text 119 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) 120 r=0 121 if rc4==0 { if xg_has("/tmp/xg_foreign.html" as *u8, "<th>Alpha &amp; Beta</th>" as *u8)==1 { 122 if xg_has("/tmp/xg_foreign.html" as *u8, "<td>tail cell</td>" as *u8)==1 { r=1 } 123 } } 124 pass=pass+xg_row("foreign-html" as *u8, r) 125 126 // row 5: registry dispatch routes .xlsx (requires the blessed dispatcher + blessed nx_xlsx) 127 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) 128 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 } } } 129 pass=pass+xg_row("registry-dispatch-xlsx" as *u8, r) 130 131 // row 6: outputs sovereign 0-JS 132 r=1 133 if xg_has("/tmp/xg_own.html" as *u8, "<script" as *u8)==1 { r=0 } 134 if xg_has("/tmp/xg_foreign.html" as *u8, "<script" as *u8)==1 { r=0 } 135 pass=pass+xg_row("outputs-0js" as *u8, r) 136 137 // row 7 (neg): missing workbook loud-fails 138 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) 139 r=0; if rc7==1 { if xg_has("/tmp/xg_r7.txt" as *u8, "XLSX-HTML-FAIL" as *u8)==1 { r=1 } } 140 pass=pass+xg_row("loud-fail-missing" as *u8, r) 141 142 let permil: i64=(pass*1000)/7 143 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4) 144 var fdi: i64=0 145 while fdi<2 { 146 var fd: i64=1; if fdi==1 { fd=logfd } 147 if fd>0 { 148 let line: *u8=sys_mmap(256); var o: i64=0 149 o=xg_cat(line,o,"XLSX-VIEW-GATE epoch=" as *u8); o=xg_catn(line,o,sys_now_realtime_sec()) 150 o=xg_cat(line,o," rows=7 pass=" as *u8); o=xg_catn(line,o,pass) 151 o=xg_cat(line,o," permil=" as *u8); o=xg_catn(line,o,permil) 152 if pass==7 { o=xg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=xg_cat(line,o," verdict=RED\n" as *u8) } 153 sys_write(fd, line, o) 154 } 155 fdi=fdi+1 156 } 157 if logfd>0 { sys_close(logfd) } 158 if pass==7 { return 0 } 159 return 1 160}