code wiki / (root) / nx_coindex.nx

nx_coindex.nx source

↩ module page · 65 lines · 3274 B

1// nx_coindex.nx -- CLI of the conflict-free co-edited index (R-ORCH-2). The parallel-safe way for 2// many sessions to maintain ONE shared index (MEMORY.md, the board) with ZERO merge conflicts: 3// each session APPENDS its slug-keyed entry (atomic under the flock floor); the index is DERIVED. 4// usage: 5// nx_coindex append <journal> <slug> <entry...> -- append one entry (conflict-free; never edits peers) 6// nx_coindex emit <journal> -- print the derived index (last-write-wins per slug) 7// lock = <journal>.lock. Exit 0 ok, 2 usage. 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_coindex_core.nx" 11const K_MAGIC_262144: i64 = 262144 12const K_MAGIC_1024: i64 = 1024 13const K_MAGIC_8192: i64 = 8192 14const K_MAGIC_8190: i64 = 8190 15 16func c_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 17func c_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } 18 19func main(argc: i64, argv: *i64) -> i64 { 20 if argc < 3 { c_puts("usage: nx_coindex append <journal> <slug> <entry> | emit <journal>\n" as *u8); return 2 } 21 let sub: *u8 = argv[1] as *u8 22 let journal: *u8 = argv[2] as *u8 23 24 if c_streq(sub, "emit" as *u8) == 1 { 25 let out: *u8 = sys_mmap(K_MAGIC_262144) 26 let n: i64 = ci_emit(journal, out, K_MAGIC_262144) 27 sys_write(1, out, n) 28 return 0 29 } 30 if c_streq(sub, "append" as *u8) == 1 { 31 if argc < 5 { c_puts("usage: nx_coindex append <journal> <slug> <entry>\n" as *u8); return 2 } 32 let lock: *u8 = sys_mmap(K_MAGIC_1024) 33 var o: i64 = 0 34 o = ccz_cat_str(lock, o, journal) 35 o = ccz_cat_str(lock, o, ".lock" as *u8) 36 // entry = argv[4..] joined -- OR `@<file>` reads the entry's FIRST LINE from a file (dodges 37 // every shell/argv quoting-mangle class for unicode + multi-word entries; the file is the API) 38 let entry: *u8 = sys_mmap(K_MAGIC_8192) 39 var eo: i64 = 0 40 let a0: *u8 = argv[4] as *u8 41 if a0[0] == (64 as u8) { 42 let n: i64 = ccz_read((a0 as i64 + 1) as *u8, entry, K_MAGIC_8190) 43 if n <= 0 { c_puts("COINDEX entry-file-unreadable\n" as *u8); return 1 } 44 eo = 0 45 while eo < n { if entry[eo] == (10 as u8) { entry[eo] = 0 as u8; eo = n } else { if entry[eo] == (13 as u8) { entry[eo] = 0 as u8; eo = n } else { eo = eo + 1 } } } 46 entry[K_MAGIC_8190] = 0 as u8 47 } else { 48 var a: i64 = 4 49 while a < argc { 50 if a > 4 { entry[eo] = 32 as u8; eo = eo + 1 } 51 let s: *u8 = argv[a] as *u8 52 var i: i64 = 0 53 while s[i] != (0 as u8) { if eo < K_MAGIC_8190 { entry[eo] = s[i]; eo = eo + 1 } i = i + 1 } 54 a = a + 1 55 } 56 entry[eo] = 0 as u8 57 } 58 let r: i64 = ci_append(journal, lock, argv[3] as *u8, entry) 59 if r < 0 { c_puts("COINDEX append-failed\n" as *u8); return 1 } 60 c_puts("COINDEX appended\n" as *u8) 61 return 0 62 } 63 c_puts("usage: nx_coindex append <journal> <slug> <entry> | emit <journal>\n" as *u8) 64 return 2 65}