nx_unidoc.nx source
↩ module page · 51 lines · 2722 B
1// nx_unidoc.nx -- CLI over nx_unidoc_lib: the unified document page (rung UD1 of /compare/unidoc).
2//
3// nx_unidoc html <in> <out.html> [download_url] -- the doc_viewers.conf renderer interface
4// nx_unidoc page <in> <out.html> [download_url] [portal_base] -- the same page with the portal's forms
5// nx_unidoc kinds -- the kinds this page understands
6//
7// Exit 0 = page written, 1 = a NAMED refusal (printed), 2 = usage. The refusal name is the whole diagnosis:
8// UNKNOWN-KIND, REFUSED-HTML, RENDERER-FAILED, TOO-BIG, READ-FAILED, OUT-CAP. license_tier: ORIGINAL
9import "nx_unidoc_lib.nx"
10
11const UC_OUT_CAP: i64 = 33554432 // 32 MiB page buffer: 8 MiB of document text cannot exceed this even fully escaped
12const UC_USAGE: i64 = 2
13
14func uc_puts(s: *u8) -> i64 { sys_write(1, s, ud_slen(s)); return 0 }
15func uc_num(v: i64) -> i64 { nxi_out(v); return 0 }
16
17func main(argc: i64, argv: *i64) -> i64 {
18 if argc >= 2 {
19 if ud_streq(argv[1] as *u8, "kinds" as *u8) == 1 {
20 uc_puts("UNIDOC-KINDS docx odt pdf md csv txt (html is REFUSED: a web page is served as itself)\n" as *u8)
21 sys_exit(0); return 0
22 }
23 }
24 if argc < 4 {
25 uc_puts("usage: nx_unidoc html <in> <out.html> [download_url] | page <in> <out.html> [download_url] [portal_base] | kinds\n" as *u8)
26 sys_exit(UC_USAGE); return UC_USAGE
27 }
28 let verb: *u8 = argv[1] as *u8
29 let inp: *u8 = argv[2] as *u8
30 let outp: *u8 = argv[3] as *u8
31 var dlurl: *u8 = "" as *u8
32 var base: *u8 = "" as *u8
33 if argc > 4 { dlurl = argv[4] as *u8 }
34 if ud_streq(verb, "page" as *u8) == 1 { if argc > 5 { base = argv[5] as *u8 } }
35 else { if ud_streq(verb, "html" as *u8) != 1 {
36 uc_puts("nx_unidoc: unknown verb (html|page|kinds)\n" as *u8); sys_exit(UC_USAGE); return UC_USAGE } }
37 let out: *u8 = sys_mmap(UC_OUT_CAP + 16)
38 let kind: *i64 = sys_mmap(16) as *i64
39 kind[0] = 0
40 let n: i64 = ud_render_path(inp, dlurl, base, out, UC_OUT_CAP, kind)
41 if n < 0 {
42 uc_puts("UNIDOC-REFUSED " as *u8); uc_puts(ud_err_name(n)); uc_puts(" kind=" as *u8); uc_puts(ud_kind_name(kind[0])); uc_puts(" in=" as *u8); uc_puts(inp); uc_puts("\n" as *u8)
43 sys_exit(1); return 1
44 }
45 let fd: i64 = sys_openat_wr(outp, MODE_0644)
46 if fd < 0 { uc_puts("UNIDOC-REFUSED cannot-write out=" as *u8); uc_puts(outp); uc_puts("\n" as *u8); sys_exit(1); return 1 }
47 sys_write(fd, out, n)
48 sys_close(fd)
49 uc_puts("UNIDOC-OK kind=" as *u8); uc_puts(ud_kind_name(kind[0])); uc_puts(" bytes=" as *u8); uc_num(n); uc_puts(" out=" as *u8); uc_puts(outp); uc_puts(" script=0 csp=1\n" as *u8)
50 sys_exit(0); return 0
51}