interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / node / tests / nodedrv.om

1723 lines · 43657 bytes Raw Blame History
1note seed/node/tests/nodedrv.om: helpers for the Omega Node acceptance drivers (view-prep.om,
2note view-accept.om): the DRA-291 fixture (prep: what view-prep.py did), node and fake-serve
3note processes, omnode-op calls and their JSON answers, output lines, the journal and the
4note listening check. Composition: om2, seed/json/json.om, seed/node/tests/drv.om, this unit, the
5note driver's main unit. Names: nd_* functions, nd* globals and texts.
6note The context buffer cx (65536 bytes) holds path slots, slot i at i*4096 with its length in
7note ndl[i]: [0] BUILD, [1] OMC, [2] ROUND40, [3] OUTDIR, [4] BASE (the scenario directory), [5]
8note scratch path, [6] the node command "PROGRAM [MODE...]" (nd_set; nd_node), [7] the op capture
9note file. Bytes 32768..65535 hold the prepared objectives, one newline-terminated line each:
10note ndl[8] their byte count, ndl[9] their number. Limits: 6 argument slots per function (an
11note unsized view/edit costs 2, a sized one 1), so js is passed as ndjsz-sized where needed; this driver owns that width, not the JSON library.
12
13const ndjsz ulong 983040
14
15global ndl ulong 16
16note ndpid, ndpst: tracked child processes (at most 16, ndpn[0] of them): the pid, and its status
17note (-1 while it runs; dv_wstat code once reaped, so a finished process keeps its status).
18global ndpid long 16
19global ndpst long 16
20global ndpn ulong 2
21note ndr: the last op call: [0] its exit status (-110 at the 30 s deadline, other negative values:
22note spawn errors), [1] 1 when its stdout parsed as JSON onto the caller's js and tp (root token 0).
23global ndr long 4
24note ndoa, ndon[0]: the op numbers after the port and word (nd_args, nd_argx): ndon[0] of ndoa.
25global ndoa long 16
26global ndon ulong 2
27note ndbad[0]: set to 1 when an accessor meets a missing or mistyped value; callers clear it.
28global ndbad ulong 1
29note ndport: [0] the last fake-serve port, [1] the last node's view port, [2] 1 when that node's
30note view came up (its CWV line seen while it still ran).
31global ndport long 4
32note ndob: the prepared objectives' original values, four each: kind code (1 add, 2 mul, 3 loop,
33note 0 other), k, n, a. ndrun: their run tokens in the fixture (at most 128), ndrn[0] the count.
34global ndob long 512
35global ndrun ulong 128
36global ndrn ulong 1
37note ndw: the buffered writer: [0] bytes buffered, [1] descriptor + 1 (0 none), [2] counting mode
38note (1: nd_wb counts into [3] and writes nothing), [3] the count. ndwe[0]: the first write error.
39global ndw ulong 4
40global ndwe long 1
41note ndlc[0]: listening entries counted by nd_lsnf.
42global ndlc ulong 1
43note ndev, ndevn[0]: a list of up to 4096 events of four numbers each (the full scenario's own
44note job events, or any caller's list).
45global ndev long 16384
46global ndevn ulong 1
47
48text ndsel "final-selected.json"
49text ndkjob "job"
50text ndkpath "path"
51text ndkflt "fault"
52text ndkkind "kind"
53text ndkstyl "style"
54text ndkk "k"
55text ndkn "n"
56text ndka "a"
57text ndkbrok "broken"
58text ndkturn "turns"
59text ndkraw "raw"
60text ndkstat "state"
61text ndkprom "prompt"
62text ndkpass "pass"
63text ndmiss "missing"
64text ndadd "add"
65text ndmul "mul"
66text ndloop "loop"
67text ndroot "root"
68text ndrjobs "root/jobs"
69text ndrsl "/root/"
70text ndwork "work"
71text ndjour "journal"
72text ndjfile "journal/journal"
73text ndobjs "objs.txt"
74text ndexp "expect.json"
75text ndmap "map"
76text ndflog "fake.log"
77text ndfake "fake-serve"
78text ndinfer "infer"
79text ndtool "tool"
80text ndop "op"
81text ndopout ".op.out"
82text ndportw "PORT"
83text ndcwv "CWV "
84text ndview "view"
85text ndcmd "cmd"
86text ndwx "x"
87text ndout ".out"
88text nderr ".err"
89text ndnone "None"
90text ndtcp "/proc/net/tcp"
91text ndtcp6 "/proc/net/tcp6"
92note Keys and values of omnode-op answers (seed/node/view.om).
93text ndklay "layout"
94text ndkinc "incarnation"
95text ndkver "version"
96text ndksnce "since"
97text ndkevs "events"
98text ndkauth "authority"
99text ndkpaus "paused"
100text ndkq "queue"
101text ndkwait "waiting"
102text ndkrdy "ready"
103text ndkrun "running"
104text ndklim "limit"
105text ndkref "refused"
106text ndkcmds "commands"
107text ndkcomm "committed"
108text ndklast "last_id"
109text ndkwkrs "workers"
110text ndkunc "uncertain"
111text ndkobs "objectives"
112text ndkbud "budget"
113text ndkph "phase"
114text ndkres "result"
115text ndkid "id"
116text ndkst "status"
117text ndsnap "snapshot"
118text nddelta "delta"
119text ndstale "stale"
120text ndinval "invalid"
121text nddup "duplicate"
122note expect.json framing (json.dump indent=1) and the map answer around the raw text.
123text ndx1 "{\n \"objs\": [\n "
124text ndx2 ",\n "
125text ndx3 "\n ],\n \"recs\": [\n {\n \"path\": "
126text ndx4 "\n },\n {\n \"path\": "
127text ndx5 ",\n \"obj\": "
128text ndx6 ",\n \"turns\": ["
129text ndx7 "\n {\n \"state\": "
130text ndx8 ",\n \"prompt\": "
131text ndx9 ",\n \"raw\": "
132text ndx10 "\n }"
133text ndx11 ","
134text ndx12 "\n ]"
135text ndx13 "]"
136text ndx14 ",\n \"pass_\": "
137text ndx15 ",\n \"state\": "
138text ndx16 "\n }\n ]\n}"
139text ndtrue "true"
140text ndfalse "false"
141text ndmv1 "{\"object\": \"text_completion\", \"choices\": [{\"index\": 0, \"text\": "
142text ndmv2 ", \"finish_reason\": \"stop\"}], \"usage\": {\"prompt_tokens\": 1, \"completion_tokens\": 1}}"
143note ndsty1, ndsty2: repair.py STYLES 0..25 and 26..50, one per line (as tool-replay.om).
144text ndsty1 "Repair this Omega file and verify it.\nUse tools to fix this Omega program, then test it.\nCheck this Omega file, correct any fault and confirm the result.\nPlease get this Omega file working according to its goal and check your work.\nInvestigate this Omega file and make its behavior match the goal.\nDiagnose this Omega file, repair it as needed, and verify the result.\nRepair the calculation in this Omega file and verify the result.\nFind and repair the mistake in this Omega file, then confirm its behavior.\nMake this Omega file satisfy its stated goal and verify the repaired result.\nFix this Omega file.\nCheck and repair the Omega program.\nMake the program match its goal and test it.\nUse tools to repair the file and check it.\nFind the wrong calculation, fix it, and test it.\nInspect this program and fix the error.\nRead the file, check it, repair it, and test again.\nPlease correct the Omega calculation and verify it.\nRepair this file to meet the stated goal.\nWork through this Omega repair and check the result.\nCheck this Omega file, find the error, repair it and verify the result.\nPlease inspect this Omega program to repair the calculation and verify the result.\nUse the goal to fix this Omega file and verify the changes.\nPlease repair this Omega program to do what the goal asks.\nGet this Omega file to match the goal, then check the result.\nPlease carefully repair the calculation using tools and confirm that the tests pass.\nPlease carefully examine this Omega file and repair any calculation that does not match its goal.\n"
145text ndsty2 "Find the incorrect behavior in this Omega file, fix it, and test the result.\nInspect and repair this Omega file, then verify it.\nCorrect this Omega program according to its goal.\nFix the error in this Omega program and check the result.\nCheck this file and repair it to meet its goal.\nPlease fix this program to match its goal.\nCorrect the calculation in this Omega file.\nRead this program and repair its calculation.\nPlease check the code and fix the mistake.\nRepair the program according to the stated goal.\nUse the goal to correct this Omega calculation.\nFix the calculation and test the corrected program.\nPlease inspect the file and correct the error.\nCheck the calculation, make the repair, and verify it.\nMake this file behave as the goal describes.\nPlease correct this program and check your work.\nRepair this calculation, then confirm the result.\nRepair this file and confirm it meets the goal.\nCheck the Omega program and correct its calculation.\nInspect this file, fix its calculation, and verify it.\nMake this Omega program meet the goal, then test it.\nRepair the file and verify its goal.\nCheck the code, repair its fault, and test it.\nFix the error in this file and confirm its goal.\nInspect the code and repair it to satisfy the goal.\n"
146
147note ======== paths
148
149note nd_sub cx s q t: path slot s joined with t (os.path.join) into slot q; its length or dvnone.
150fn nd_sub cx edit uchar 65536 s ulong q ulong t view uchar * to ulong
151a = new uchar 4096 0
152n = dv_cat a 0 view part cx mul s 4096 get ndl s
153if eq n dvnone
154ret dvnone
155end
156r = dv_join part cx mul q 4096 4096 view part a 0 n t
157if ne r dvnone
158put ndl q r
159end
160ret r
161end
162
163note nd_absj o a: a joined to the working directory (os.path.join: a itself when absolute) into
164note o; the length, or dvnone when the directory is unknown or o is too short.
165fn nd_absj o edit uchar 4096 a view uchar * to ulong
166w = new uchar 4096 0
167m = cwd w 0 4096
168if lt m 2
169ret dvnone
170end
171ret dv_join o view part w 0 sub ulong m 1 a
172end
173
174note nd_abs cx s i: command-line argument i made absolute (joined to the working directory when
175note relative; not normalised) into slot s: 0, or -22 empty, -105 too long or no directory.
176fn nd_abs cx edit uchar 65536 s ulong i ulong to long
177a = new uchar 4096 0
178b = new uchar 4096 0
179n = dv_arg i a
180if lt n 1
181if eq n 0
182ret neg long 22
183end
184ret n
185end
186r = nd_absj b view part a 0 ulong n
187if eq r dvnone
188ret neg long 105
189end
190x = nd_set cx s view part b 0 r
191ret 0
192end
193
194note nd_base cx t: BASE (slot 4) = OUTDIR/t, and the op capture file (slot 7) = OUTDIR/.op.out;
195note true when both fit.
196fn nd_base cx edit uchar 65536 t view uchar * to bool
197if eq nd_sub cx 3 4 t dvnone
198ret false
199end
200ret ne nd_sub cx 3 7 ndopout dvnone
201end
202
203note ======== numbers and lines
204
205note nd_w64 b o: the little-endian 64-bit word at b[o].
206fn nd_w64 b view uchar * o ulong to ulong
207v = ulong 0
208for i ulong 8
209v = or v shl ulong get b add o i mul i 8
210end
211ret v
212end
213
214note nd_lend b s: the end of the line that starts at b[s] (its newline's index, or len b).
215fn nd_lend b view uchar * s ulong to ulong
216i = s
217while lt i len b bound 16777217
218if eq get b i 10
219ret i
220end
221i = add i 1
222end
223ret len b
224end
225
226note nd_pfx b s e t: true when b[s..e) starts with t.
227fn nd_pfx b view uchar * s ulong e ulong t view uchar * to bool
228if lt sub e s len t
229ret false
230end
231ret dv_eq view part b s len t t
232end
233
234note nd_hasl b t: true when some line of b is exactly t.
235fn nd_hasl b view uchar * t view uchar * to bool
236s = ulong 0
237while lt s len b bound 16777217
238e = nd_lend b s
239if eq sub e s len t
240if dv_eq view part b s len t t
241ret true
242end
243end
244s = add e 1
245end
246ret false
247end
248
249note nd_hasp b t: true when some line of b starts with t.
250fn nd_hasp b view uchar * t view uchar * to bool
251s = ulong 0
252while lt s len b bound 16777217
253e = nd_lend b s
254if nd_pfx b s e t
255ret true
256end
257s = add e 1
258end
259ret false
260end
261
262note nd_lastl o p b: the last line of b (Python splitlines()[-1]; empty when b is) appended at o[p].
263fn nd_lastl o edit uchar * p ulong b view uchar * to ulong
264n = len b
265if gt n 0
266if eq get b sub n 1 10
267n = sub n 1
268end
269end
270s = n
271while gt s 0 bound 16777217
272if eq get b sub s 1 10
273break
274end
275s = sub s 1
276end
277ret dv_cat o p part b s sub n s
278end
279
280note nd_pfxl o p b t: the Python repr of the list of lines of b starting with t (['l1', 'l2'])
281note appended at o[p] (o is a 1024-byte detail buffer); lines are taken verbatim between quotes.
282fn nd_pfxl o edit uchar 1024 p ulong b view uchar * t view uchar * to ulong
283q = dv_byte o p 91
284c = ulong 0
285s = ulong 0
286while lt s len b bound 16777217
287e = nd_lend b s
288if nd_pfx b s e t
289if gt c 0
290q = dv_byte o q 44
291q = dv_byte o q 32
292end
293q = dv_byte o q 39
294q = dv_cat o q part b s sub e s
295q = dv_byte o q 39
296c = add c 1
297end
298s = add e 1
299end
300ret dv_byte o q 93
301end
302
303note nd_line o p t k: line k of the newline-terminated lines t appended at o[p]; the end or dvnone.
304fn nd_line o edit uchar * p ulong t view uchar * k ulong to ulong
305i = ulong 0
306c = ulong 0
307s = ulong 0
308while lt i len t bound 4096
309if eq get t i 10
310if eq c k
311ret dv_cat o p part t s sub i s
312end
313c = add c 1
314s = add i 1
315end
316i = add i 1
317end
318ret dvnone
319end
320
321note nd_style o p s: repair.py STYLES[s] appended at o[p]; dvnone for s outside 0..50.
322fn nd_style o edit uchar * p ulong s long to ulong
323if or lt s 0 gt s 50
324ret dvnone
325end
326if lt s 26
327ret nd_line o p ndsty1 ulong s
328end
329ret nd_line o p ndsty2 sub ulong s 26
330end
331
332note ======== the buffered writer and the JSON string encoder (Python json.dumps, ensure_ascii)
333
334note nd_wopen pa: open pa (created or truncated) for the writer: 0 or the negative fopen error.
335fn nd_wopen pa view uchar * to long
336fd = fopen pa 0 len pa 557633 420
337if lt fd 0
338ret fd
339end
340put ndw 0 0
341put ndw 1 add ulong fd 1
342put ndw 2 0
343put ndw 3 0
344put ndwe 0 0
345ret 0
346end
347
348note nd_wflush ob: write out the bytes buffered in ob.
349fn nd_wflush ob edit uchar * to void
350n = get ndw 0
351if eq n 0
352ret
353end
354f = get ndw 1
355if gt f 0
356r = dv_wall int sub f 1 view part ob 0 n
357if lt r 0
358if eq get ndwe 0 0
359put ndwe 0 r
360end
361end
362end
363put ndw 0 0
364end
365
366note nd_wclose ob: flush and close the writer: 0 or the first write error.
367fn nd_wclose ob edit uchar * to long
368nd_wflush ob
369f = get ndw 1
370if gt f 0
371x = fclose int sub f 1
372end
373put ndw 1 0
374ret get ndwe 0
375end
376
377note nd_wb ob c: byte c through the writer buffer ob (or counted, in counting mode).
378fn nd_wb ob edit uchar * c uchar to void
379if eq get ndw 2 1
380put ndw 3 add get ndw 3 1
381ret
382end
383if ge get ndw 0 len ob
384nd_wflush ob
385end
386put ob get ndw 0 c
387put ndw 0 add get ndw 0 1
388end
389
390note nd_wt ob t: the bytes of t through the writer.
391fn nd_wt ob edit uchar * t view uchar * to void
392for i ulong len t
393nd_wb ob get t i
394end
395end
396
397note nd_wu ob v: v in decimal through the writer.
398fn nd_wu ob edit uchar * v ulong to void
399d = new uchar 24 0
400n = dv_fmtu d 0 v
401nd_wt ob view part d 0 n
402end
403
404note nd_whex ob v: \u and v as four lowercase hex digits through the writer.
405fn nd_whex ob edit uchar * v ulong to void
406nd_wb ob 92
407nd_wb ob 117
408for i ulong 4
409d = and shr v mul sub 3 i 4 15
410if lt d 10
411nd_wb ob uchar add d 48
412else
413nd_wb ob uchar add d 87
414end
415end
416end
417
418note nd_wch ob c: code point c JSON-escaped as Python's ensure_ascii encoder does.
419fn nd_wch ob edit uchar * c ulong to void
420e = ulong 0
421if eq c 34
422e = 34
423end
424if eq c 92
425e = 92
426end
427if eq c 10
428e = 110
429end
430if eq c 13
431e = 114
432end
433if eq c 9
434e = 116
435end
436if eq c 8
437e = 98
438end
439if eq c 12
440e = 102
441end
442if ne e 0
443nd_wb ob 92
444nd_wb ob uchar e
445ret
446end
447if and ge c 32 le c 126
448nd_wb ob uchar c
449ret
450end
451if lt c 65536
452nd_whex ob c
453ret
454end
455v = sub c 65536
456nd_whex ob add 55296 shr v 10
457nd_whex ob add 56320 and v 1023
458end
459
460note nd_u8 s i: the UTF-8 sequence at s[i]: its code point, or'd with its byte length << 32
461note (a stray or cut byte decodes as itself, or as U+FFFD at the end, with length 1).
462fn nd_u8 s view uchar * i ulong to ulong
463b = ulong get s i
464k = ulong 1
465c = b
466if ge b 240
467k = 4
468c = and b 7
469else
470if ge b 224
471k = 3
472c = and b 15
473else
474if ge b 192
475k = 2
476c = and b 31
477end
478end
479end
480if gt add i k len s
481ret or 65533 shl ulong 1 32
482end
483j = ulong 1
484while lt j k bound 4
485c = or shl c 6 and ulong get s add i j 63
486j = add j 1
487end
488ret or c shl k 32
489end
490
491note nd_wesc ob s: the UTF-8 bytes s as a JSON string (quotes included) through the writer.
492fn nd_wesc ob edit uchar * s view uchar * to void
493nd_wb ob 34
494i = ulong 0
495while lt i len s bound 16777217
496r = nd_u8 s i
497nd_wch ob and r 2097151
498i = add i shr r 32
499end
500nd_wb ob 34
501end
502
503note nd_wtk ob js tp t: JSON string token t re-encoded through the writer: 0, or the negative
504note dv_jstr error (not a string, or longer than 65536 bytes decoded).
505fn nd_wtk ob edit uchar * js view uchar * tp view uint dvtcap t ulong to long
506d = new uchar 65536 0
507n = dv_jstr js tp t d
508if lt n 0
509ret n
510end
511nd_wesc ob view part d 0 ulong n
512ret 0
513end
514
515note nd_elen ob js tp t: the byte length nd_wtk would write for t, or its negative error.
516fn nd_elen ob edit uchar * js view uchar * tp view uint dvtcap t ulong to long
517put ndw 2 1
518put ndw 3 0
519r = nd_wtk ob js tp t
520put ndw 2 0
521if lt r 0
522ret r
523end
524ret long get ndw 3
525end
526
527note ======== the DRA-291 fixture (view-prep)
528
529note nd_jint js tp t key: integer member key of object token t; ndbad set and 0 when it is no
530note integer.
531fn nd_jint js view uchar * tp view uint dvtcap t ulong key view uchar * to long
532v = dv_jkey js tp t key
533if not dv_jisl js tp v
534put ndbad 0 1
535ret 0
536end
537ret dv_jnum js tp v
538end
539
540note nd_find js tp w: the first run of the fixture array whose job.path contains w, or jsnone.
541fn nd_find js view uchar * tp view uint dvtcap w view uchar * to ulong
542d = new uchar 4096 0
543r = js_first tp 0
544while ne r jsnone bound 16777217
545j = dv_jkey js tp r ndkjob
546n = dv_jstr js tp dv_jkey js tp j ndkpath d
547if ge n 0
548if ne dv_find view part d 0 ulong n w 0 dvnone
549ret r
550end
551end
552r = js_enext tp r
553end
554ret jsnone
555end
556
557note nd_rpath cx js tp j: BASE/root/ followed by job j's path into slot 5; its length or dvnone.
558fn nd_rpath cx edit uchar 65536 js view uchar * tp view uint dvtcap j ulong to ulong
559a = new uchar 4096 0
560d = new uchar 4096 0
561n = dv_jstr js tp dv_jkey js tp j ndkpath d
562if lt n 0
563ret dvnone
564end
565p = dv_cat a 0 view part cx 16384 get ndl 4
566p = dv_cat a p ndrsl
567p = dv_cat a p view part d 0 ulong n
568if eq p dvnone
569ret dvnone
570end
571ret nd_set cx 5 view part a 0 p
572end
573
574note nd_brok cx js tp j: write job j's broken text to BASE/root/PATH unless its fault is
575note "missing": 0 or a negative error.
576fn nd_brok cx edit uchar 65536 js view uchar * tp view uint dvtcap j ulong to long
577b = new uchar 65536 0
578if dv_jsteq js tp dv_jkey js tp j ndkflt ndmiss
579ret 0
580end
581n = dv_jstr js tp dv_jkey js tp j ndkbrok b
582if lt n 0
583ret n
584end
585q = nd_rpath cx js tp j
586if eq q dvnone
587ret neg long 36
588end
589ret dv_spit view part cx 20480 q view part b 0 ulong n
590end
591
592note nd_objv js tp j i: job j's kind code, k, n (0 when absent) and a stored as objective i in
593note ndob: 0, or -22 for a field that is no integer.
594fn nd_objv js view uchar * tp view uint dvtcap j ulong i ulong to long
595put ndbad 0 0
596o = mul i 4
597c = long 0
598kd = dv_jkey js tp j ndkkind
599if dv_jsteq js tp kd ndadd
600c = 1
601end
602if dv_jsteq js tp kd ndmul
603c = 2
604end
605if dv_jsteq js tp kd ndloop
606c = 3
607end
608put ndob o c
609put ndob add o 1 nd_jint js tp j ndkk
610n = long 0
611if ne dv_jkey js tp j ndkn jsnone
612n = nd_jint js tp j ndkn
613end
614put ndob add o 2 n
615put ndob add o 3 nd_jint js tp j ndka
616if eq get ndbad 0 1
617ret neg long 22
618end
619ret 0
620end
621
622note nd_obj o js tp j i: "kind,k,n," of job j (objective i in ndob) written at o[0]; the end or dvnone.
623fn nd_obj o edit uchar 4096 js view uchar * tp view uint dvtcap j ulong i ulong to ulong
624kd = new uchar 64 0
625o4 = mul i 4
626n = dv_jstr js tp dv_jkey js tp j ndkkind kd
627if lt n 0
628ret dvnone
629end
630q = dv_cat o 0 view part kd 0 ulong n
631q = dv_byte o q 44
632q = dv_fmtl o q get ndob add o4 1
633q = dv_byte o q 44
634q = dv_fmtl o q get ndob add o4 2
635ret dv_byte o q 44
636end
637
638note nd_objt o js tp j i a: objective i's line "kind,k,n,a,path,STYLE\n" (job j, argument a)
639note written at o[0]; its length, or dvnone.
640fn nd_objt o edit uchar 4096 js view uchar ndjsz tp view uint dvtcap j ulong i ulong a long to ulong
641d = new uchar 4096 0
642p = nd_obj o js tp j i
643p = dv_fmtl o p a
644p = dv_byte o p 44
645n = dv_jstr js tp dv_jkey js tp j ndkpath d
646if lt n 0
647ret dvnone
648end
649p = dv_cat o p view part d 0 ulong n
650p = dv_byte o p 44
651p = nd_style o p nd_jint js tp j ndkstyl
652ret dv_byte o p 10
653end
654
655note nd_addl cx o n: the line o[0..n) appended to the prepared objectives in cx: 0, or -7 when
656note they would pass 32768 bytes.
657fn nd_addl cx edit uchar 65536 o view uchar 4096 n ulong to long
658e = get ndl 8
659if gt n sub 32768 e
660ret neg long 7
661end
662for i ulong n
663put cx add add 32768 e i get o i
664end
665put ndl 8 add e n
666put ndl 9 add get ndl 9 1
667ret 0
668end
669
670note nd_objl cx js tp j i wrong: objective i's line (nd_objt; a = k+n+a+1 for the first objective
671note when wrong) appended to the objectives in cx: 0 or a negative error.
672fn nd_objl cx edit uchar 65536 js view uchar ndjsz tp view uint dvtcap j ulong i ulong wrong bool to long
673o = new uchar 4096 0
674o4 = mul i 4
675a = get ndob add o4 3
676if wrong
677if eq i 0
678k = get ndob add o4 1
679k = add k get ndob add o4 2
680a = add add k a 1
681end
682end
683p = nd_objt o js tp j i a
684if eq p dvnone
685ret neg long 22
686end
687ret nd_addl cx o p
688end
689
690note nd_prep1 cx js tp w wrong: prepare the first run whose path contains w: 0 or negative.
691fn nd_prep1 cx edit uchar 65536 js view uchar ndjsz tp view uint dvtcap w view uchar * wrong bool to long
692r = nd_find js tp w
693if eq r jsnone
694ret neg long 2
695end
696i = get ndrn 0
697if ge i 128
698ret neg long 7
699end
700put ndrun i r
701put ndrn 0 add i 1
702j = dv_jkey js tp r ndkjob
703x = nd_brok cx js tp j
704if lt x 0
705ret x
706end
707x = nd_objv js tp j i
708if lt x 0
709ret x
710end
711ret nd_objl cx js tp j i wrong
712end
713
714note nd_tree cx: BASE removed and recreated with root/jobs and work: 0 or a negative error.
715fn nd_tree cx edit uchar 65536 to long
716r = dv_rmrf view part cx 16384 get ndl 4
717q = nd_sub cx 4 5 ndrjobs
718if eq q dvnone
719ret neg long 36
720end
721r = dv_mkdirp view part cx 20480 q
722if lt r 0
723ret r
724end
725q = nd_sub cx 4 5 ndwork
726if eq q dvnone
727ret neg long 36
728end
729ret dv_mkdirp view part cx 20480 q
730end
731
732note nd_wturn ob js tp u: turn object u written as an expect.json turn: 0 or negative.
733fn nd_wturn ob edit uchar * js view uchar * tp view uint dvtcap u ulong to long
734nd_wt ob ndx7
735r = nd_wtk ob js tp dv_jkey js tp u ndkstat
736nd_wt ob ndx8
737x = nd_wtk ob js tp dv_jkey js tp u ndkprom
738r = or r x
739nd_wt ob ndx9
740x = nd_wtk ob js tp dv_jkey js tp u ndkraw
741r = or r x
742nd_wt ob ndx10
743if ne r 0
744ret neg long 22
745end
746ret 0
747end
748
749note nd_wobj ob js tp i: prepared run i's "kind,k,n,a" (its original a) written as a JSON string.
750fn nd_wobj ob edit uchar * js view uchar * tp view uint dvtcap i ulong to long
751o = new uchar 4096 0
752j = dv_jkey js tp get ndrun i ndkjob
753p = nd_obj o js tp j i
754p = dv_fmtl o p get ndob add mul i 4 3
755if eq p dvnone
756ret neg long 22
757end
758nd_wesc ob view part o 0 p
759ret 0
760end
761
762note nd_wtrns ob js tp ts: the turns array ts written as expect.json turns ([] when empty).
763fn nd_wtrns ob edit uchar * js view uchar * tp view uint dvtcap ts ulong to long
764if ne js_type tp ts jsarr
765ret neg long 22
766end
767e = long 0
768u = js_first tp ts
769c = ulong 0
770while ne u jsnone bound 16777217
771if gt c 0
772nd_wt ob ndx11
773end
774x = nd_wturn ob js tp u
775e = or e x
776c = add c 1
777u = js_enext tp u
778end
779if gt c 0
780nd_wt ob ndx12
781else
782nd_wt ob ndx13
783end
784ret e
785end
786
787note nd_wrec ob js tp i: prepared run i written as an expect.json rec: 0 or negative.
788fn nd_wrec ob edit uchar * js view uchar * tp view uint dvtcap i ulong to long
789r = get ndrun i
790j = dv_jkey js tp r ndkjob
791e = nd_wtk ob js tp dv_jkey js tp j ndkpath
792nd_wt ob ndx5
793x = nd_wobj ob js tp i
794e = or e x
795nd_wt ob ndx6
796x = nd_wtrns ob js tp dv_jkey js tp r ndkturn
797e = or e x
798nd_wt ob ndx14
799u = dv_jkey js tp r ndkpass
800if eq js_type tp u jstrue
801nd_wt ob ndtrue
802else
803if eq js_type tp u jsfalse
804nd_wt ob ndfalse
805else
806e = neg long 22
807end
808end
809nd_wt ob ndx15
810x = nd_wtk ob js tp dv_jkey js tp r ndkstat
811e = or e x
812if ne e 0
813ret neg long 22
814end
815ret 0
816end
817
818note nd_wobjs ob b: each line of b (the prepared objectives) written as a JSON string, ", "-joined.
819fn nd_wobjs ob edit uchar * b view uchar * to void
820s = ulong 0
821c = ulong 0
822while lt s len b bound 16777217
823t = nd_lend b s
824if gt c 0
825nd_wt ob ndx2
826end
827nd_wesc ob view part b s sub t s
828c = add c 1
829s = add t 1
830end
831end
832
833note nd_wexp cx js tp: BASE/expect.json, byte for byte what view-prep.py's json.dump(indent=1)
834note wrote: 0 or a negative error.
835fn nd_wexp cx edit uchar 65536 js view uchar * tp view uint dvtcap to long
836ob = new uchar 65536 0
837q = nd_sub cx 4 5 ndexp
838if eq q dvnone
839ret neg long 36
840end
841e = nd_wopen view part cx 20480 q
842if lt e 0
843ret e
844end
845nd_wt ob ndx1
846nd_wobjs ob view part cx 32768 get ndl 8
847for i ulong get ndrn 0
848if eq i 0
849nd_wt ob ndx3
850else
851nd_wt ob ndx4
852end
853x = nd_wrec ob js tp i
854if lt x 0
855e = x
856end
857end
858nd_wt ob ndx16
859x = nd_wclose ob
860if lt x 0
861ret x
862end
863ret e
864end
865
866note nd_wpair ob js tp u: turn u as a map record "KLEN VLEN\n" KEY VALUE: 0 or negative.
867fn nd_wpair ob edit uchar * js view uchar * tp view uint dvtcap u ulong to long
868p = dv_jkey js tp u ndkprom
869w = dv_jkey js tp u ndkraw
870kl = nd_elen ob js tp p
871vl = nd_elen ob js tp w
872if or lt kl 0 lt vl 0
873ret neg long 22
874end
875nd_wu ob ulong kl
876nd_wb ob 32
877nd_wu ob add add ulong vl len ndmv1 len ndmv2
878nd_wb ob 10
879x = nd_wtk ob js tp p
880nd_wt ob ndmv1
881x = nd_wtk ob js tp w
882nd_wt ob ndmv2
883ret 0
884end
885
886note nd_wmap cx js tp: BASE/map, the fake-serve map of every prepared turn (prompt to raw),
887note byte for byte what view-prep.py wrote: 0 or a negative error.
888fn nd_wmap cx edit uchar 65536 js view uchar * tp view uint dvtcap to long
889ob = new uchar 65536 0
890q = nd_sub cx 4 5 ndmap
891if eq q dvnone
892ret neg long 36
893end
894e = nd_wopen view part cx 20480 q
895if lt e 0
896ret e
897end
898for i ulong get ndrn 0
899u = js_first tp dv_jkey js tp get ndrun i ndkturn
900while ne u jsnone bound 16777217
901x = nd_wpair ob js tp u
902if lt x 0
903e = x
904end
905u = js_enext tp u
906end
907end
908x = nd_wclose ob
909if lt x 0
910ret x
911end
912ret e
913end
914
915note nd_prepl cx js tp ids wrong: nd_prep1 for each comma-separated id of ids: 0 or negative.
916fn nd_prepl cx edit uchar 65536 js view uchar ndjsz tp view uint dvtcap ids view uchar * wrong bool to long
917s = ulong 0
918i = ulong 0
919while le i len ids bound 16777217
920c = true
921if lt i len ids
922c = eq get ids i 44
923end
924if c
925r = nd_prep1 cx js tp view part ids s sub i s wrong
926if lt r 0
927ret r
928end
929s = add i 1
930end
931i = add i 1
932end
933ret 0
934end
935
936note nd_wobjf cx: the prepared objective lines written to BASE/objs.txt: 0 or negative.
937fn nd_wobjf cx edit uchar 65536 to long
938b = new uchar 32768 0
939m = get ndl 8
940for i ulong m
941put b i get cx add 32768 i
942end
943n = nd_sub cx 4 5 ndobjs
944if eq n dvnone
945ret neg long 36
946end
947ret dv_spit view part cx 20480 n view part b 0 m
948end
949
950note nd_prep cx js tp ids wrong: view-prep: load ROUND40/final-selected.json (slot 2) onto js and
951note tp, rebuild BASE (slot 4) and prepare, for each comma-separated id of ids, the first run whose
952note job path contains it: its broken file under BASE/root (unless the fault is "missing"), its
953note objective line (in cx and BASE/objs.txt), its expect.json rec and its map records; with wrong
954note the first objective's a is k+n+a+1. The number of objectives, or a negative error.
955fn nd_prep cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap ids view uchar * wrong bool to long
956put ndl 8 0
957put ndl 9 0
958put ndrn 0 0
959n = nd_sub cx 2 5 ndsel
960if eq n dvnone
961ret neg long 36
962end
963k = dv_jload view part cx 20480 n js tp
964if lt k 0
965ret k
966end
967if ne js_type tp 0 jsarr
968ret neg long 22
969end
970r = nd_tree cx
971if lt r 0
972ret r
973end
974r = nd_prepl cx js tp ids wrong
975if lt r 0
976ret r
977end
978r = nd_wobjf cx
979if lt r 0
980ret r
981end
982r = nd_wexp cx js tp
983if lt r 0
984ret r
985end
986r = nd_wmap cx js tp
987if lt r 0
988ret r
989end
990ret long get ndl 9
991end
992
993note ======== processes
994
995note nd_track pid: a slot for child pid (its status -1, running), or dvnone when the table is
996note full (the child is then killed and reaped).
997fn nd_track pid long to ulong
998i = get ndpn 0
999if ge i 16
1000x = dv_killg pid 9
1001x = dv_kill pid 9
1002x = dv_reap pid
1003ret dvnone
1004end
1005put ndpid i pid
1006put ndpst i neg long 1
1007put ndpn 0 add i 1
1008ret i
1009end
1010
1011note nd_poll i: tracked process i's status without blocking: -1 while it runs, its dv_wstat
1012note status once finished (kept), -10 for no such slot.
1013fn nd_poll i ulong to long
1014if ge i get ndpn 0
1015ret neg long 10
1016end
1017s = get ndpst i
1018if ne s -1
1019ret s
1020end
1021r = dv_poll get ndpid i
1022if ne r -1
1023put ndpst i r
1024end
1025ret r
1026end
1027
1028note nd_fin i sec: wait for tracked process i up to sec seconds: its status, or -110 when it was
1029note killed (group and pid) at the deadline.
1030fn nd_fin i ulong sec ulong to long
1031if ge i get ndpn 0
1032ret neg long 10
1033end
1034s = get ndpst i
1035if ne s -1
1036ret s
1037end
1038r = dv_wait get ndpid i mul sec 1000000000
1039put ndpst i r
1040ret r
1041end
1042
1043note nd_sig i sig: signal sig to tracked process i alone (not its group) while it runs.
1044fn nd_sig i ulong sig int to long
1045if ge i get ndpn 0
1046ret neg long 10
1047end
1048if ne get ndpst i -1
1049ret 0
1050end
1051ret dv_kill get ndpid i sig
1052end
1053
1054note nd_reap i: wait (blocking) for tracked process i: its status.
1055fn nd_reap i ulong to long
1056if ge i get ndpn 0
1057ret neg long 10
1058end
1059s = get ndpst i
1060if ne s -1
1061ret s
1062end
1063r = dv_reap get ndpid i
1064put ndpst i r
1065ret r
1066end
1067
1068note nd_clean: SIGKILL every tracked process's group and every one still running, reap them,
1069note and empty the table (the end of each scenario).
1070fn nd_clean to void
1071for i ulong get ndpn 0
1072x = dv_killg get ndpid i 9
1073if eq get ndpst i -1
1074x = dv_kill get ndpid i 9
1075x = dv_reap get ndpid i
1076put ndpst i x
1077end
1078end
1079put ndpn 0 0
1080end
1081
1082note ======== spawning fake-serve and the node
1083
1084note nd_pkn pk p v: the decimal v appended to the packed list (dv_pkadd; pk is 4096 bytes).
1085fn nd_pkn pk edit uchar 4096 p ulong v long to ulong
1086d = new uchar 24 0
1087q = dv_fmtl d 0 v
1088ret dv_pkadd pk p view part d 0 q
1089end
1090
1091note nd_pks cx pk p s t: path slot s joined with t appended to the packed list.
1092fn nd_pks cx edit uchar 65536 pk edit uchar 4096 p ulong s ulong t view uchar * to ulong
1093n = nd_sub cx s 5 t
1094if eq n dvnone
1095ret dvnone
1096end
1097ret dv_pkadd pk p view part cx 20480 n
1098end
1099
1100note nd_pkw pk p t: each space-separated word of t appended to the packed list.
1101fn nd_pkw pk edit uchar 4096 p ulong t view uchar * to ulong
1102q = p
1103s = ulong 0
1104i = ulong 0
1105while le i len t bound 16777217
1106c = true
1107if lt i len t
1108c = eq get t i 32
1109end
1110if c
1111if gt i s
1112q = dv_pkadd pk q view part t s sub i s
1113end
1114s = add i 1
1115end
1116i = add i 1
1117end
1118ret q
1119end
1120
1121note nd_pkobj pk p b: each line of b (the prepared objectives, cx[32768..+ndl[8])) appended to the
1122note packed list.
1123fn nd_pkobj pk edit uchar 4096 p ulong b view uchar * to ulong
1124q = p
1125s = ulong 0
1126while lt s len b bound 16777217
1127e = nd_lend b s
1128q = dv_pkadd pk q view part b s sub e s
1129s = add e 1
1130end
1131ret q
1132end
1133
1134note nd_wtext pa t l0 sec: true once file pa holds t at or after offset l0, polled every 20 ms
1135note for up to sec seconds.
1136fn nd_wtext pa view uchar * t view uchar * l0 ulong sec ulong to bool
1137b = new uchar 262144 0
1138t0 = ulong monons
1139while true bound 9223372036854775807
1140n = dv_slurp pa b
1141if ge n 0
1142if ne dv_find view part b 0 ulong n t l0 dvnone
1143ret true
1144end
1145end
1146if ge sub ulong monons t0 mul sec 1000000000
1147ret false
1148end
1149x = sleepns 20000000
1150end
1151ret false
1152end
1153
1154note nd_flen pa: the length of file pa (0 when it is missing or longer than 262144 bytes).
1155fn nd_flen pa view uchar * to ulong
1156b = new uchar 262144 0
1157n = dv_slurp pa b
1158if lt n 0
1159ret 0
1160end
1161ret ulong n
1162end
1163
1164note nd_fake cx dl: start BUILD/fake-serve on a free port (ndport[0]) with BASE/map and delay dl
1165note ms, stdout and stderr appended to BASE/fake.log, and wait up to 5 s for its new PORT line:
1166note its process slot, or dvnone.
1167fn nd_fake cx edit uchar 65536 dl ulong to ulong
1168pk = new uchar 4096 0
1169lg = new uchar 4096 0
1170pt = dv_port
1171if lt pt 0
1172ret dvnone
1173end
1174put ndport 0 pt
1175n = nd_sub cx 4 5 ndflog
1176m = dv_cat lg 0 view part cx 20480 n
1177l0 = nd_flen view part lg 0 m
1178p = dv_pkinit pk
1179p = nd_pks cx pk p 0 ndfake
1180p = nd_pkn pk p pt
1181p = nd_pks cx pk p 4 ndmap
1182p = nd_pkn pk p long dl
1183if or eq p dvnone eq m dvnone
1184ret dvnone
1185end
1186pid = dv_spawna view part pk 0 p view part lg 0 m
1187if lt pid 0
1188ret dvnone
1189end
1190i = nd_track pid
1191x = nd_wtext view part lg 0 m ndportw l0 5
1192ret i
1193end
1194
1195note nd_tagp cx o tg sx: BASE/ followed by tg and sx into o; the length or dvnone.
1196fn nd_tagp cx edit uchar 65536 o edit uchar 4096 tg view uchar * sx view uchar * to ulong
1197a = new uchar 4096 0
1198n = dv_cat a 0 tg
1199n = dv_cat a n sx
1200if eq n dvnone
1201ret dvnone
1202end
1203ret dv_join o view part cx 16384 get ndl 4 view part a 0 n
1204end
1205
1206note nd_set cx s t: text t copied into path slot s; its length or dvnone.
1207fn nd_set cx edit uchar 65536 s ulong t view uchar * to ulong
1208n = dv_cat part cx mul s 4096 4096 0 t
1209if ne n dvnone
1210put ndl s n
1211end
1212ret n
1213end
1214
1215note nd_pknode cx pk fp vp: the node argument list: BUILD/PROGRAM and the mode words (slot 6,
1216note "PROGRAM [MODE...]"), BUILD/infer, BUILD/tool, OMC, BASE/root, BASE/work, BASE/journal, fp,
1217note vp, then the objective lines.
1218fn nd_pknode cx edit uchar 65536 pk edit uchar 4096 fp long vp long to ulong
1219w = new uchar 4096 0
1220n = dv_cat w 0 view part cx 24576 get ndl 6
1221e = ulong 0
1222while lt e n bound 4097
1223if eq get w e 32
1224break
1225end
1226e = add e 1
1227end
1228p = dv_pkinit pk
1229p = nd_pks cx pk p 0 view part w 0 e
1230p = nd_pkw pk p view part w e sub n e
1231p = nd_pks cx pk p 0 ndinfer
1232p = nd_pks cx pk p 0 ndtool
1233p = dv_pkadd pk p view part cx 4096 get ndl 1
1234p = nd_pks cx pk p 4 ndroot
1235p = nd_pks cx pk p 4 ndwork
1236p = nd_pks cx pk p 4 ndjour
1237p = nd_pkn pk p fp
1238p = nd_pkn pk p vp
1239ret nd_pkobj pk p view part cx 32768 get ndl 8
1240end
1241
1242note nd_fhas pa t l0: true when file pa (read up to 256 KiB) holds t at or after offset l0.
1243fn nd_fhas pa view uchar * t view uchar * l0 ulong to bool
1244b = new uchar 262144 0
1245n = dv_slurp pa b
1246if lt n 0
1247ret false
1248end
1249ret ne dv_find view part b 0 ulong n t l0 dvnone
1250end
1251
1252note nd_nodeup fo vp i: wait up to 10 s for "CWV vp" in file fo or for process i to exit: true
1253note when the line was seen and the process still runs.
1254fn nd_nodeup fo view uchar * vp long i ulong to bool
1255w = new uchar 32 0
1256q = dv_cat w 0 ndcwv
1257q = dv_fmtl w q vp
1258q = dv_byte w q 10
1259t0 = ulong monons
1260while true bound 9223372036854775807
1261f = nd_fhas fo view part w 0 q 0
1262r = nd_poll i
1263if or f ne r -1
1264ret and f eq r -1
1265end
1266if ge sub ulong monons t0 10000000000
1267ret false
1268end
1269x = sleepns 20000000
1270end
1271ret false
1272end
1273
1274note nd_nsp pk p fo fe n: spawn the packed list pk[0..p) with stdout to fo[0..n) and stderr to
1275note fe[0..n): its pid, or a negative error.
1276fn nd_nsp pk view uchar 4096 p ulong fo view uchar 4096 fe view uchar 4096 n ulong to long
1277ret dv_spawn view part pk 0 p view part fo 0 n view part fe 0 n
1278end
1279
1280note nd_nodew cx tg vp i: nd_nodeup on BASE/tg.out.
1281fn nd_nodew cx edit uchar 65536 tg view uchar * vp long i ulong to bool
1282fo = new uchar 4096 0
1283nl = nd_tagp cx fo tg ndout
1284if eq nl dvnone
1285ret false
1286end
1287ret nd_nodeup view part fo 0 nl vp i
1288end
1289
1290note nd_node cx tg fp: start the node of slot 6 ("PROGRAM [MODE...]", nd_set; e.g. "code-main" or
1291note "code-test overload 1", PROGRAM under BUILD) with fake-serve port fp, the prepared objectives
1292note and a free view port (ndport[1]), stdout to BASE/tg.out and stderr to BASE/tg.err, and wait for
1293note its view (ndport[2] 1 when up): its process slot, or dvnone when it could not start.
1294fn nd_node cx edit uchar 65536 tg view uchar * fp long to ulong
1295pk = new uchar 4096 0
1296fo = new uchar 4096 0
1297fe = new uchar 4096 0
1298put ndport 2 0
1299vp = dv_port
1300put ndport 1 vp
1301if lt vp 0
1302ret dvnone
1303end
1304p = nd_pknode cx pk fp vp
1305if eq p dvnone
1306ret dvnone
1307end
1308n = nd_tagp cx fo tg ndout
1309x = nd_tagp cx fe tg nderr
1310if or eq n dvnone ne x n
1311ret dvnone
1312end
1313pid = nd_nsp pk p fo fe n
1314if lt pid 0
1315ret dvnone
1316end
1317i = nd_track pid
1318if nd_nodew cx tg vp i
1319put ndport 2 1
1320end
1321ret i
1322end
1323
1324note nd_outl cx tg b: file BASE/tg.out read into b: its length, or the negative dv_slurp error.
1325fn nd_outl cx edit uchar 65536 tg view uchar * b edit uchar * to long
1326fo = new uchar 4096 0
1327n = nd_tagp cx fo tg ndout
1328if eq n dvnone
1329ret neg long 36
1330end
1331ret dv_slurp view part fo 0 n b
1332end
1333
1334note ======== omnode-op calls
1335
1336note nd_pkop pk p w: the op word w (1 view, 2 cmd, 3 the usage control "view x") and the ndon[0]
1337note numbers of ndoa appended.
1338fn nd_pkop pk edit uchar 4096 p ulong w ulong to ulong
1339q = p
1340if eq w 2
1341q = dv_pkadd pk q ndcmd
1342else
1343q = dv_pkadd pk q ndview
1344end
1345if eq w 3
1346q = dv_pkadd pk q ndwx
1347end
1348for i ulong get ndon 0
1349q = nd_pkn pk q get ndoa i
1350end
1351ret q
1352end
1353
1354note nd_args n a b c d e: the first n (at most 5) of a b c d e as the op numbers (ndoa, ndon[0]).
1355fn nd_args n ulong a long b long c long d long e long to void
1356put ndoa 0 a
1357put ndoa 1 b
1358put ndoa 2 c
1359put ndoa 3 d
1360put ndoa 4 e
1361put ndon 0 n
1362end
1363
1364note nd_argx n a b c d: the first n (at most 4) of a b c d appended to the op numbers.
1365fn nd_argx n ulong a long b long c long d long to void
1366k = get ndon 0
1367if gt add k n 16
1368ret
1369end
1370if gt n 0
1371put ndoa k a
1372end
1373if gt n 1
1374put ndoa add k 1 b
1375end
1376if gt n 2
1377put ndoa add k 2 c
1378end
1379if gt n 3
1380put ndoa add k 3 d
1381end
1382put ndon 0 add k n
1383end
1384
1385note nd_op cx js tp pt w: run BUILD/op pt WORD NUMBERS... (word w as nd_pkop, the op numbers) with
1386note stdout captured to slot 7 and a 30 s deadline; its stdout parsed onto js and tp. Sets ndr;
1387note returns its status.
1388fn nd_op cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long w ulong to long
1389pk = new uchar 4096 0
1390put ndr 0 neg long 7
1391put ndr 1 0
1392p = dv_pkinit pk
1393p = nd_pks cx pk p 0 ndop
1394p = nd_pkn pk p pt
1395p = nd_pkop pk p w
1396if eq p dvnone
1397ret neg long 7
1398end
1399pid = dv_spawn view part pk 0 p view part cx 28672 get ndl 7 dvnull
1400if lt pid 0
1401put ndr 0 pid
1402ret pid
1403end
1404r = dv_wait pid 30000000000
1405put ndr 0 r
1406k = dv_jload view part cx 28672 get ndl 7 js tp
1407if gt k 0
1408put ndr 1 1
1409end
1410ret r
1411end
1412
1413note nd_view cx js tp pt: op pt view (a snapshot): its status.
1414fn nd_view cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long to long
1415put ndon 0 0
1416ret nd_op cx js tp pt 1
1417end
1418
1419note nd_viewd cx js tp pt sn inc: op pt view sn inc (a delta since version sn): its status.
1420fn nd_viewd cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long sn long inc long to long
1421nd_args 2 sn inc 0 0 0
1422ret nd_op cx js tp pt 1
1423end
1424
1425note nd_vctl cx js tp pt: the usage control op pt view x 1: its status.
1426fn nd_vctl cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long to long
1427nd_args 1 1 0 0 0 0
1428ret nd_op cx js tp pt 3
1429end
1430
1431note nd_cmd cx js tp pt: op pt cmd NUMBERS (set by nd_args and nd_argx: cid opn tg inc ver, then
1432note any operation arguments): its status.
1433fn nd_cmd cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long to long
1434ret nd_op cx js tp pt 2
1435end
1436
1437note ======== answers of the last op call (all jsnone/missing-safe)
1438
1439note nd_tk js tp t key: member key of object token t of the last answer; jsnone when it did not
1440note parse, t is jsnone or no object, or key is missing.
1441fn nd_tk js view uchar * tp view uint dvtcap t ulong key view uchar * to ulong
1442if or eq get ndr 1 0 eq t jsnone
1443ret jsnone
1444end
1445ret dv_jkey js tp t key
1446end
1447
1448note nd_ti js tp t key: integer member key of token t (nd_tk); 0 with ndbad set when missing.
1449fn nd_ti js view uchar * tp view uint dvtcap t ulong key view uchar * to long
1450v = nd_tk js tp t key
1451if not dv_jisl js tp v
1452put ndbad 0 1
1453ret 0
1454end
1455ret dv_jnum js tp v
1456end
1457
1458note nd_el tp t i: element i of array token t, or jsnone.
1459fn nd_el tp view uint dvtcap t ulong i ulong to ulong
1460if or eq get ndr 1 0 eq t jsnone
1461ret jsnone
1462end
1463ret dv_jidx tp t i
1464end
1465
1466note nd_ei js tp t i: integer element i of array token t; 0 with ndbad set when missing.
1467fn nd_ei js view uchar * tp view uint dvtcap t ulong i ulong to long
1468v = nd_el tp t i
1469if not dv_jisl js tp v
1470put ndbad 0 1
1471ret 0
1472end
1473ret dv_jnum js tp v
1474end
1475
1476note nd_n tp t: the element count of array token t; 0 with ndbad set when t is no array.
1477fn nd_n tp view uint dvtcap t ulong to ulong
1478if or eq get ndr 1 0 eq t jsnone
1479put ndbad 0 1
1480ret 0
1481end
1482if ne js_type tp t jsarr
1483put ndbad 0 1
1484ret 0
1485end
1486ret dv_jlen tp t
1487end
1488
1489note nd_status js tp s: true when the last answer parsed as an object whose status is s.
1490fn nd_status js view uchar * tp view uint dvtcap s view uchar * to bool
1491v = nd_tk js tp 0 ndkst
1492if eq v jsnone
1493ret false
1494end
1495ret dv_jsteq js tp v s
1496end
1497
1498note nd_dj o p js tp t: token t's JSON text as the last answer had it (compact), or None when the
1499note answer did not parse or t is jsnone, appended at o[p] (o is a 1024-byte detail buffer).
1500fn nd_dj o edit uchar 1024 p ulong js view uchar * tp view uint dvtcap t ulong to ulong
1501if or eq get ndr 1 0 eq t jsnone
1502ret dv_cat o p ndnone
1503end
1504ret dv_cat o p part js js_off tp t js_span tp t
1505end
1506
1507note nd_frarg sv n js tp: the op numbers sv[0..3) INC VER sv[3..n), with INC and VER (incarnation,
1508note version) of the last answer (a view).
1509fn nd_frarg sv view long 16 n ulong js view uchar ndjsz tp view uint dvtcap to void
1510inc = nd_ti js tp 0 ndkinc
1511ver = nd_ti js tp 0 ndkver
1512nd_args 5 get sv 0 get sv 1 get sv 2 inc ver
1513for i ulong sub n 3
1514put ndoa add i 5 get sv add i 3
1515end
1516put ndon 0 add n 2
1517end
1518
1519note nd_fresh cx js tp pt: with the op numbers cid opn tg [ARGS...] (nd_args, nd_argx), up to 20
1520note times: view, then cmd cid opn tg INC VER ARGS... with that view's incarnation and version,
1521note until the answer is not stale. Its status; ndr[1] 0 and no answer when a view did not parse.
1522fn nd_fresh cx edit uchar 65536 js edit uchar ndjsz tp edit uint dvtcap pt long to long
1523sv = new long 16 0
1524n = get ndon 0
1525if or lt n 3 gt n 14
1526ret neg long 22
1527end
1528for i ulong n
1529put sv i get ndoa i
1530end
1531r = long 0
1532for k ulong 20
1533r = nd_view cx js tp pt
1534if eq get ndr 1 0
1535ret r
1536end
1537nd_frarg sv n js tp
1538r = nd_cmd cx js tp pt
1539if not nd_status js tp ndstale
1540ret r
1541end
1542end
1543ret r
1544end
1545
1546note ======== the journal and the listening check
1547
1548note nd_jset b m o: true when a settled DONE record (type 4, word 7 non-zero) among the m records
1549note of journal b carries the operation words 3..6 of the record at b[o].
1550fn nd_jset b view uchar * m ulong o ulong to bool
1551for i ulong m
1552r = add 64 mul i 128
1553if eq nd_w64 b r 4
1554if ne nd_w64 b add r 56 0
1555c = true
1556for w ulong 4
1557if ne nd_w64 b add r mul add w 3 8 nd_w64 b add o mul add w 3 8
1558c = false
1559end
1560end
1561if c
1562ret true
1563end
1564end
1565end
1566end
1567ret false
1568end
1569
1570note nd_jstart cx jid: true when BASE/journal/journal holds a START record (type 3) for job jid
1571note whose operation no settled DONE record carries (view-accept.py journal_started).
1572fn nd_jstart cx edit uchar 65536 jid ulong to bool
1573b = new uchar 1048576 0
1574q = nd_sub cx 4 5 ndjfile
1575if eq q dvnone
1576ret false
1577end
1578n = dv_slurp view part cx 20480 q b
1579if lt n 64
1580ret false
1581end
1582m = div sub ulong n 64 128
1583for i ulong m
1584o = add 64 mul i 128
1585if eq nd_w64 b o 3
1586if eq nd_w64 b add o 64 jid
1587if not nd_jset view part b 0 ulong n m o
1588ret true
1589end
1590end
1591end
1592end
1593ret false
1594end
1595
1596note nd_hexv b s e: the hexadecimal number b[s..e) (dvnone for another byte).
1597fn nd_hexv b view uchar * s ulong e ulong to ulong
1598v = ulong 0
1599for i ulong sub e s
1600c = ulong get b add s i
1601d = dvnone
1602if and ge c 48 le c 57
1603d = sub c 48
1604end
1605if and ge c 65 le c 70
1606d = sub c 55
1607end
1608if and ge c 97 le c 102
1609d = sub c 87
1610end
1611if eq d dvnone
1612ret dvnone
1613end
1614v = or shl v 4 d
1615end
1616ret v
1617end
1618
1619note nd_lsnl l pt: 1 when /proc/net/tcp line l is a LISTEN (st 0A) entry on local port pt.
1620fn nd_lsnl l view uchar * pt long to ulong
1621f = new ulong 8 0
1622nf = ulong 0
1623s = ulong 0
1624inf = false
1625for i ulong len l
1626sp = eq get l i 32
1627if and not sp not inf
1628s = i
1629inf = true
1630end
1631if and sp inf
1632if lt nf 4
1633put f mul nf 2 s
1634put f add mul nf 2 1 i
1635end
1636nf = add nf 1
1637inf = false
1638end
1639end
1640if and inf lt nf 4
1641put f mul nf 2 s
1642put f add mul nf 2 1 len l
1643nf = add nf 1
1644end
1645if lt nf 4
1646ret 0
1647end
1648if ne sub get f 7 get f 6 2
1649ret 0
1650end
1651if ne nd_hexv l get f 6 get f 7 10
1652ret 0
1653end
1654e = get f 3
1655c = e
1656while gt c get f 2 bound 64
1657if eq get l sub c 1 58
1658break
1659end
1660c = sub c 1
1661end
1662if eq c get f 2
1663ret 0
1664end
1665if eq nd_hexv l c e ulong pt
1666ret 1
1667end
1668ret 0
1669end
1670
1671note nd_lsnb b pt: count (into ndlc) the LISTEN entries on pt among the complete lines of b:
1672note the bytes consumed (through the last newline).
1673fn nd_lsnb b view uchar * pt long to ulong
1674s = ulong 0
1675i = ulong 0
1676while lt i len b bound 16777217
1677if eq get b i 10
1678put ndlc 0 add get ndlc 0 nd_lsnl view part b s sub i s pt
1679s = add i 1
1680end
1681i = add i 1
1682end
1683ret s
1684end
1685
1686note nd_lsnf pa pt: the number of LISTEN entries on port pt in table pa (/proc/net/tcp or tcp6),
1687note read in 65536-byte chunks; 0 when it cannot be opened.
1688fn nd_lsnf pa view uchar * pt long to ulong
1689b = new uchar 65536 0
1690put ndlc 0 0
1691fd = fopen pa 0 len pa 524288 0
1692if lt fd 0
1693ret 0
1694end
1695n = ulong 0
1696while true bound 16777217
1697k = read int fd b n sub 65536 n
1698if le k 0
1699break
1700end
1701n = add n ulong k
1702s = nd_lsnb view part b 0 n pt
1703for j ulong sub n s
1704put b j get b add s j
1705end
1706n = sub n s
1707if ge n 65536
1708n = 0
1709end
1710end
1711if gt n 0
1712put ndlc 0 add get ndlc 0 nd_lsnl view part b 0 n pt
1713end
1714x = fclose int fd
1715ret get ndlc 0
1716end
1717
1718note nd_lsn pt: true when some TCP socket (IPv4 or IPv6) listens on port pt (ss -ltn's answer).
1719fn nd_lsn pt long to bool
1720a = nd_lsnf ndtcp pt
1721b = nd_lsnf ndtcp6 pt
1722ret gt add a b 0
1723end