nx_barfresh.nx source
↩ module page · 123 lines · 5761 B
1// nx_barfresh.nx -- CLI over nx_barfresh_lib: is every bar on a /compare board the CURRENT MONTH'S, or attested this month
2// as the newest the field has? Prints one line per bar, the partition, and a verdict LAST (positional readers take it).
3// nx_barfresh <domain> [YYYY-MM]
4// The optional month replaces the clock, so a seat can ask "would this board pass next month" and a probe can show the
5// refusal firing on a live board without planting anything. exit: 0 FRESH | 1 STALE (the hard fail) | 2 usage |
6// 3 NO-BAR (no bar rows) or NO-PLAN (unreadable in both trees)
7// license_tier: ORIGINAL No hw writes (Rule 26).
8import "nx_syscalls.nx"
9import "nx_barfresh_lib.nx"
10
11const BFC_ARG_DOMAIN: i64 = 1
12const BFC_ARG_MONTH: i64 = 2
13const BFC_EXIT_USAGE: i64 = 2
14const BFC_NUM_CAP: i64 = 24
15const BFC_YM_CAP: i64 = 12
16const BFC_STDOUT: i64 = 1
17
18func bfc_w(s: *u8) -> i64 {
19 return sys_write(BFC_STDOUT, s, bf_slen(s))
20}
21func bfc_wn(v: i64) -> i64 {
22 let b: *u8 = sys_mmap(BFC_NUM_CAP)
23 var x: i64 = v
24 var neg: i64 = 0
25 if x < 0 { neg = 1; x = 0 - x }
26 var o: i64 = BFC_NUM_CAP - 1
27 b[o] = 0 as u8
28 o = o - 1
29 if x == 0 { b[o] = BF_DIGIT0 as u8; o = o - 1 }
30 while x > 0 { b[o] = (x - (x / 10) * 10 + BF_DIGIT0) as u8; o = o - 1; x = x / 10 }
31 if neg == 1 { b[o] = BF_DASH as u8; o = o - 1 }
32 return bfc_w((b as i64 + o + 1) as *u8)
33}
34func bfc_wym(ix: i64) -> i64 {
35 if ix == BF_NONE { return bfc_w("-" as *u8) }
36 let b: *u8 = sys_mmap(BFC_YM_CAP)
37 bf_ym_write(b, 0, ix)
38 return bfc_w(b)
39}
40// a span of the buffer, written verbatim
41func bfc_wspan(buf: *u8, off: i64, len: i64) -> i64 {
42 if len <= 0 { return 0 }
43 return sys_write(BFC_STDOUT, (buf as i64 + off) as *u8, len)
44}
45
46func main(argc: i64, argv: *i64) -> i64 {
47 if argc <= BFC_ARG_DOMAIN {
48 bfc_w("usage: nx_barfresh <domain> [YYYY-MM] -- exit 0 FRESH | 1 STALE | 3 NO-BAR or NO-PLAN\n" as *u8)
49 return BFC_EXIT_USAGE
50 }
51 let dom: *u8 = argv[BFC_ARG_DOMAIN] as *u8
52 var cur: i64 = BF_NONE
53 var cursrc: *u8 = "clock" as *u8
54 if argc > BFC_ARG_MONTH {
55 let ma: *u8 = argv[BFC_ARG_MONTH] as *u8
56 cur = bf_parse_ym(ma, bf_slen(ma))
57 if cur == BF_NONE { bfc_w("usage: the month must read YYYY-MM\n" as *u8); return BFC_EXIT_USAGE }
58 cursrc = "argv" as *u8
59 } else { cur = bf_now_ym() }
60 let lenp: *i64 = sys_mmap(BF_I64) as *i64
61 let which: *i64 = sys_mmap(BF_I64) as *i64
62 lenp[0] = 0
63 let buf: *u8 = bf_read_plan(ct_first_published(), ct_second_published(), dom, lenp, which)
64 bfc_w("BARFRESH domain=" as *u8); bfc_w(dom)
65 bfc_w(" cur=" as *u8); bfc_wym(cur); bfc_w(" cur_src=" as *u8); bfc_w(cursrc)
66 if lenp[0] <= 0 {
67 bfc_w(" plan=UNREADABLE-IN-BOTH-TREES bars=0 verdict=NO-PLAN\n" as *u8)
68 return BF_EXIT_NOBAR
69 }
70 bfc_w(" plan_bytes=" as *u8); bfc_wn(lenp[0])
71 bfc_w(" tree=" as *u8)
72 if which[0] == CT_TREE_PRIMARY { bfc_w(ct_first_published()) } else { bfc_w(ct_second_published()) }
73 bfc_w("\n" as *u8)
74 let n: i64 = lenp[0]
75 let nbars: i64 = bf_count_rows(buf, n, BF_BAR_TAG)
76 let off: *i64 = sys_mmap((nbars + 1) * BF_I64) as *i64
77 let ym: *i64 = sys_mmap((nbars + 1) * BF_I64) as *i64
78 let st: *i64 = sys_mmap((nbars + 1) * BF_I64) as *i64
79 let seen: *i64 = sys_mmap((nbars + 1) * BF_I64) as *i64
80 let c: *i64 = sys_mmap(BF_C_N * BF_I64) as *i64
81 let nb: i64 = bf_classify(buf, n, cur, off, ym, st, seen, c)
82 let fo: *i64 = sys_mmap(BF_I64) as *i64
83 var i: i64 = 0
84 while i < nb {
85 let p: i64 = off[i]
86 let e: i64 = bf_line_end(buf, n, p)
87 bfc_w(" BAR " as *u8)
88 var l: i64 = bf_field(buf, p, e, BF_F_BAR_ID, fo); bfc_wspan(buf, fo[0], l)
89 bfc_w(" ym=" as *u8); bfc_wym(ym[i])
90 bfc_w(" ref=" as *u8)
91 l = bf_field(buf, p, e, BF_F_BAR_REF, fo); bfc_wspan(buf, fo[0], l)
92 bfc_w(" state=" as *u8); bfc_w(bf_state_name(st[i]))
93 bfc_w(" newest_seen_this_month=" as *u8); bfc_wym(seen[i])
94 bfc_w(" | " as *u8)
95 l = bf_field(buf, p, e, BF_F_BAR_SUBJECT, fo); bfc_wspan(buf, fo[0], l)
96 bfc_w("\n" as *u8)
97 i = i + 1
98 }
99 let v: i64 = bf_verdict(c)
100 if v == BF_EXIT_STALE {
101 bfc_w("REFUSAL-RULE: " as *u8); bfc_w(bf_refusal_rule(c)); bfc_w("\n" as *u8)
102 if c[BF_C_DUPLICATE] + c[BF_C_SCANS_DUP] > 0 {
103 bfc_w("REMEDY: a sotabar id or a same-day barscan for one ref is declared twice on this board; remove the repeated row (a later bar with a repeated id reads DUPLICATE above) so one declaration remains.\n" as *u8)
104 } else {
105 bfc_w("REMEDY: a bar older than the current month is a hard fail. Fetch the current month's listing (nx_research_fetch, mirrored and pinned as a .refs row), READ it, then either land a sotabar| row dated " as *u8)
106 bfc_wym(cur)
107 bfc_w(" or a barscan|<today>|<ref>|newest=<month> attestation for the bar's own ref proving the current month has nothing newer.\n" as *u8)
108 }
109 }
110 bfc_w("BARFRESH bars=" as *u8); bfc_wn(c[BF_C_BARS])
111 bfc_w(" fresh=" as *u8); bfc_wn(c[BF_C_FRESH])
112 bfc_w(" attested=" as *u8); bfc_wn(c[BF_C_ATTESTED])
113 bfc_w(" stale=" as *u8); bfc_wn(c[BF_C_STALE])
114 bfc_w(" unattested=" as *u8); bfc_wn(c[BF_C_UNATTESTED])
115 bfc_w(" malformed=" as *u8); bfc_wn(c[BF_C_MALFORMED])
116 bfc_w(" duplicate=" as *u8); bfc_wn(c[BF_C_DUPLICATE])
117 bfc_w(" sum=" as *u8); bfc_wn(bf_partition_sum(c))
118 bfc_w(" scans=" as *u8); bfc_wn(c[BF_C_SCANS])
119 bfc_w(" scans_this_month=" as *u8); bfc_wn(c[BF_C_SCANS_CUR])
120 bfc_w(" scans_dup=" as *u8); bfc_wn(c[BF_C_SCANS_DUP])
121 bfc_w(" verdict=" as *u8); bfc_w(bf_verdict_name(v)); bfc_w("\n" as *u8)
122 return v
123}