code wiki / _hdl_build / nx_board.nx
nx_board.nx source
↩ module page · 60 lines · 2564 B
1// nx_board.nx -- shared BASE CLASS for the _board organ family (10 organs, each rolling its own HTML page
2// scaffold + JSON envelope + atomic publish; worst nx_maturity_board = 70 local emit refs; NONE shared a base
3// until now). COMPOSES nx_estr (base-on-base: a board inherits the emit primitives es_cat/es_catn AND the
4// board scaffold here) -- the OO consolidation the atlas family census surfaced. Import this + emit a board in
5// a handful of calls instead of reimplementing the skeleton. license_tier: ORIGINAL No hw writes (Rule 26).
6import "nx_syscalls.nx"
7import "nx_estr.nx"
8
9// ---- HTML page scaffold (write into buf at offset o, return new offset) ----
10func bd_page_open(buf: *u8, o: i64, title: *u8) -> i64 {
11 var p: i64 = es_cat(buf, o, "<!doctype html><html><head><meta charset=\"utf-8\"><title>" as *u8)
12 p = es_cat(buf, p, title)
13 p = es_cat(buf, p, "</title></head><body>" as *u8)
14 return p
15}
16func bd_page_close(buf: *u8, o: i64) -> i64 { return es_cat(buf, o, "</body></html>" as *u8) }
17// <h{level}>text</h{level}>
18func bd_h(buf: *u8, o: i64, level: i64, text: *u8) -> i64 {
19 var p: i64 = es_cat(buf, o, "<h" as *u8)
20 p = es_catn(buf, p, level)
21 buf[p] = 62 as u8; p = p + 1
22 p = es_cat(buf, p, text)
23 p = es_cat(buf, p, "</h" as *u8)
24 p = es_catn(buf, p, level)
25 buf[p] = 62 as u8; p = p + 1
26 return p
27}
28
29// ---- JSON envelope (the machine-readable board every board also emits) ----
30// {"v":1,"domain":"<domain>","title":"<title>" (caller appends fields via bd_json_kvn, then closes with '}')
31func bd_json_open(buf: *u8, o: i64, domain: *u8, title: *u8) -> i64 {
32 var p: i64 = es_cat(buf, o, "{\"v\":1,\"domain\":\"" as *u8)
33 p = es_cat(buf, p, domain)
34 p = es_cat(buf, p, "\",\"title\":\"" as *u8)
35 p = es_cat(buf, p, title)
36 buf[p] = 34 as u8; p = p + 1
37 return p
38}
39// append ,"<key>":<intval>
40func bd_json_kvn(buf: *u8, o: i64, key: *u8, v: i64) -> i64 {
41 var p: i64 = es_cat(buf, o, ",\"" as *u8)
42 p = es_cat(buf, p, key)
43 p = es_cat(buf, p, "\":" as *u8)
44 p = es_catn(buf, p, v)
45 return p
46}
47
48// ---- the publish primitive: atomic tmp+rename write (every board reimplements this) ----
49func bd_write_atomic(path: *u8, buf: *u8, n: i64) -> i64 {
50 let tmp: *u8 = sys_mmap(512)
51 var t: i64 = es_cat(tmp, 0, path)
52 t = es_cat(tmp, t, ".tmp" as *u8)
53 tmp[t] = 0 as u8
54 let fd: i64 = sys_openat_wr(tmp, 0x1a4)
55 if fd < 0 { return 0 - 1 }
56 sys_write(fd, buf, n)
57 sys_close(fd)
58 if sys_renameat(tmp, path) != 0 { return 0 - 2 }
59 return 0
60}