code wiki / (root) / nx_lane_conf.nx

nx_lane_conf.nx source

↩ module page · 88 lines · 3521 B

1// nx_lane_conf.nx -- shared hot-read key/value conf getter (rule 11: thresholds, limits, timeouts 2// and weights live in CONFIG, never in code; rule 15: three organs were about to hand-roll this). 3// Format: `key<TAB or SPACE>value` rows, `#` comments. Re-read on EVERY call (files are ~1KB; the 4// gen_steps.conf precedent) so an edit takes effect with no rebuild and no restart. 5// Resolution: CWD-relative first, then the estate-absolute fallback -- the same dual-path the 6// gen orchestrator proved, because daemons run from three different roots (the CWD-artifact class). 7// license_tier: ORIGINAL module: nishi-core.lib.laneconf 8import "nx_syscalls.nx" 9 10const LC_BUF: i64 = 65536 11 12func lc_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 13 14// read whole file; returns bytes or 0. 15func lc_read(path: *u8, buf: *u8) -> i64 { 16 let fd: i64 = sys_openat_rd(path) 17 if fd < 0 { return 0 } 18 var total: i64 = 0 19 var r: i64 = 1 20 while r > 0 { 21 r = sys_read(fd, ((buf as i64)+total) as *u8, LC_BUF-1-total) 22 if r > 0 { total = total + r } 23 if total >= LC_BUF-1 { r = 0 } 24 } 25 sys_close(fd) 26 buf[total] = 0 as u8 27 return total 28} 29 30// integer value for key in conf at path (CWD then estate-absolute fallback); dflt when the file, 31// the key, or a parseable value is absent. NEVER errors -- config absence must not break a lane. 32func lc_geti(relpath: *u8, abspath: *u8, key: *u8, dflt: i64) -> i64 { 33 let buf: *u8 = sys_mmap(LC_BUF) 34 var n: i64 = lc_read(relpath, buf) 35 if n <= 0 { n = lc_read(abspath, buf) } 36 if n <= 0 { return dflt } 37 let kl: i64 = lc_slen(key) 38 var i: i64 = 0 39 while i < n { 40 // line start: match key then a separator 41 if buf[i] != (35 as u8) { 42 var k: i64 = 0 43 var ok: i64 = 1 44 while k < kl { 45 if i+k >= n { ok = 0; k = kl } else { 46 if buf[i+k] != key[k] { ok = 0; k = kl } else { k = k + 1 } 47 } 48 } 49 if ok == 1 { 50 var j: i64 = i + kl 51 var sep: i64 = 0 52 if buf[j] == (9 as u8) { sep = 1 } 53 if buf[j] == (32 as u8) { sep = 1 } 54 if sep == 1 { 55 while buf[j] == (9 as u8) { j = j + 1 } 56 while buf[j] == (32 as u8) { j = j + 1 } 57 var v: i64 = 0 58 var got: i64 = 0 59 var run: i64 = 1 60 while run == 1 { 61 if buf[j] < (48 as u8) { run = 0 } else { 62 if buf[j] > (57 as u8) { run = 0 } else { 63 v = v*10 + ((buf[j] as i64) - 48); got = 1; j = j + 1 64 } 65 } 66 } 67 if got == 1 { return v } 68 return dflt 69 } 70 } 71 } 72 // skip to next line (NUL-safe: stop, never loop) 73 var lrun: i64 = 1 74 while lrun == 1 { 75 if i >= n { lrun = 0 } else { 76 if buf[i] == (10 as u8) { lrun = 0; i = i + 1 } else { i = i + 1 } 77 } 78 } 79 } 80 return dflt 81} 82 83// the write lane's conf paths, shared by its organs so no caller re-spells them. 84func lc_write_lane(key: *u8, dflt: i64) -> i64 { 85 return lc_geti("knowledge/write_lane.conf" as *u8, 86 "/volume1/homes/elderwesto/nishihost/knowledge/write_lane.conf" as *u8, 87 key, dflt) 88}