code wiki / _hdl_build / nx_site_provision_lib.nx
nx_site_provision_lib.nx source
↩ module page · 63 lines · 3255 B
1// nx_site_provision_lib.nx -- shared core for SITE PROVISIONING (operator: "build sites easily that are
2// s-class like our social site"). ONE call turns a .site CONFIG (data the operator owns) into a provisioned
3// s-class site: compose the compliant HTML (via the proven nx_site_compose), emit the sites.conf host->docroot
4// line, and write a provisioning manifest. S-CLASS BY CONSTRUCTION: a non-compliant config is REFUSED (compose_build
5// returns <0 from sg_compliant) -- no partial site, nothing in sites.conf. Composes existing organs, no rebuild.
6// license_tier: ORIGINAL
7import "nx_site_compose.nx"
8const K_MAGIC_262144: i64 = 262144
9const K_MAGIC_262140: i64 = 262140
10
11func sp_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12func sp_w(fd: i64, s: *u8) -> i64 { let n: i64=sp_len(s); sys_write(fd,s,n); return 0 }
13func sp_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
14
15func sp_read(path: *u8, buf: *u8, cap: i64) -> i64 {
16 let fd: i64 = sys_openat_rd(path)
17 if fd < 0 { return 0 }
18 var n: i64 = 0
19 var r: i64 = sys_read(fd, buf, cap - 1)
20 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
21 sys_close(fd)
22 return n
23}
24func sp_writefile(path: *u8, s: *u8) -> i64 {
25 let fd: i64 = sys_openat_wr(path, 0x1a4)
26 if fd < 0 { return 0 - 1 }
27 let n: i64 = sp_len(s)
28 sys_write(fd, s, n)
29 sys_close(fd)
30 return 0
31}
32func sp_contains(buf: *u8, n: i64, pat: *u8) -> i64 {
33 let pl: i64 = sp_len(pat)
34 if pl == 0 { return 0 }
35 var i: i64 = 0
36 while i + pl <= n {
37 var k: i64 = 0
38 var hit: i64 = 1
39 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
40 if hit == 1 { return 1 }
41 i = i + 1
42 }
43 return 0
44}
45
46// PROVISION a site from a .site config. Returns 0 = provisioned (compliant) ; <0 = REFUSED (non-compliant /
47// no-config / io). On compliant: HTML written to outhtml, sites.conf line appended, manifest written. On refuse:
48// nothing useful emitted (sg_compliant rejects before any HTML, and we skip conf+manifest).
49func sp_provision(name: *u8, site_path: *u8, outhtml: *u8, conf_path: *u8, manifest_path: *u8, game_tpl: *u8) -> i64 {
50 let buf: *u8 = sys_mmap(K_MAGIC_262144)
51 let tn: i64 = sp_read(site_path, buf, K_MAGIC_262140)
52 if tn <= 0 { return 0 - 2 }
53 let fd: i64 = sys_openat_wr(outhtml, 0x1a4)
54 if fd < 0 { return 0 - 3 }
55 let r: i64 = compose_build(buf, tn, fd, game_tpl)
56 sys_close(fd)
57 if r < 0 { return r }
58 let cf: i64 = sys_openat_append(conf_path, 0x1a4)
59 if cf >= 0 { sp_w(cf, name); sp_w(cf, "\t" as *u8); sp_w(cf, outhtml); sp_w(cf, "\n" as *u8); sys_close(cf) }
60 let mf: i64 = sys_openat_wr(manifest_path, 0x1a4)
61 if mf >= 0 { sp_w(mf, "PROVISIONED name=" as *u8); sp_w(mf, name); sp_w(mf, " html=" as *u8); sp_w(mf, outhtml); sp_w(mf, " admin=/cms/" as *u8); sp_w(mf, name); sp_w(mf, " compliant=1\n" as *u8); sys_close(mf) }
62 return 0
63}