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 / aienux / pi-register / lib.om

1464 lines · 27008 bytes Raw Blame History
1note aienux/pi-register/lib.om: pure-Omega service registry parser, validator, lookup, and renderer
2note Faithful port of upstream pi-register with port range (1..65535), protocol/status validation,
3note duplicate name/port conflict detection, wildcard ignore matching, filtered lookup,
4note and deterministic plain-text and JSONL rendering.
5note Compose with si/directives/om2.om ahead of this file.
6
7const PI_MAX_SVC ulong 64
8const PI_NAME_SLOT ulong 64
9const PI_PURP_SLOT ulong 128
10const PI_OWN_SLOT ulong 64
11const PI_UNIT_SLOT ulong 64
12const PI_CAT_SLOT ulong 32
13
14const PI_OK ulong 0
15const PI_ERR_SYNTAX ulong 1
16const PI_ERR_MISSING_FIELD ulong 2
17const PI_ERR_BAD_PORT ulong 3
18const PI_ERR_BAD_PROTO ulong 4
19const PI_ERR_BAD_STATUS ulong 5
20const PI_ERR_DUP_NAME ulong 6
21const PI_ERR_DUP_PORT ulong 7
22const PI_ERR_FULL ulong 8
23const PI_ERR_TOO_LONG ulong 9
24
25const PROTO_TCP ulong 1
26const PROTO_UDP ulong 2
27const PROTO_HTTP ulong 3
28const PROTO_HTTPS ulong 4
29const PROTO_UNIX ulong 5
30const PROTO_NONE ulong 6
31
32const STAT_ACTIVE ulong 1
33const STAT_INACTIVE ulong 2
34const STAT_DEGRADED ulong 3
35const STAT_DISABLED ulong 4
36const STAT_MISSING ulong 5
37const STAT_UNKNOWN ulong 6
38
39const FILT_ALL ulong 0
40const FILT_OWNER ulong 1
41const FILT_PROTO ulong 2
42const FILT_STATUS ulong 3
43const FILT_CAT ulong 4
44const FILT_PORT ulong 5
45const FILT_BOOT ulong 6
46
47struct PiParseInfo code ulong line ulong svc_n ulong end
48
49note Global flat service registry tables (up to 64 services)
50global g_pi_n ulong 1
51global g_pi_name uchar 4096
52global g_pi_name_len ulong 64
53global g_pi_purp uchar 8192
54global g_pi_purp_len ulong 64
55global g_pi_own uchar 4096
56global g_pi_own_len ulong 64
57global g_pi_unit uchar 4096
58global g_pi_unit_len ulong 64
59global g_pi_cat uchar 2048
60global g_pi_cat_len ulong 64
61global g_pi_port ulong 64
62global g_pi_proto ulong 64
63global g_pi_status ulong 64
64global g_pi_boot ulong 64
65
66text p_sec_hdr "[[service]]"
67text p_k_name "name"
68text p_k_purpose "purpose"
69text p_k_owner "owner"
70text p_k_port "port"
71text p_k_proto "protocol"
72text p_k_status "status"
73text p_k_unit "unit"
74text p_k_cat "category"
75text p_k_boot "boot"
76text p_k_deps "depends_on"
77text p_k_files "files"
78text p_k_secrets "secrets"
79text p_k_health "health"
80text p_k_notes "notes"
81
82text p_pr_tcp "tcp"
83text p_pr_udp "udp"
84text p_pr_http "http"
85text p_pr_https "https"
86text p_pr_unix "unix"
87text p_pr_none "none"
88
89text p_st_active "active"
90text p_st_inactive "inactive"
91text p_st_degraded "degraded"
92text p_st_disabled "disabled"
93text p_st_missing "missing"
94text p_st_unknown "unknown"
95
96text p_v_true "true"
97text p_v_false "false"
98text p_v_yes "yes"
99text p_v_no "no"
100text p_v_dash "-"
101text p_v_null "null"
102text p_v_general "general"
103
104text p_sfx_svc ".service"
105text p_sfx_sock ".socket"
106text p_sfx_tmr ".timer"
107text p_sfx_tgt ".target"
108text p_sfx_mnt ".mount"
109text p_sfx_pth ".path"
110
111note Base-system ignore patterns from upstream pi-register
112text p_ig_01 "systemd-*"
113text p_ig_02 "getty@*"
114text p_ig_03 "serial-getty@*"
115text p_ig_04 "dbus*"
116text p_ig_05 "ssh.service"
117text p_ig_06 "sshd.service"
118text p_ig_07 "cron.service"
119text p_ig_08 "rsyslog.service"
120text p_ig_09 "networkd-dispatcher.service"
121text p_ig_10 "polkit.service"
122text p_ig_11 "apparmor.service"
123text p_ig_12 "e2scrub*"
124text p_ig_13 "fstrim.*"
125text p_ig_14 "logrotate.*"
126text p_ig_15 "apt-*"
127text p_ig_16 "unattended-upgrades.service"
128text p_ig_17 "ufw.service"
129text p_ig_18 "snapd*"
130text p_ig_19 "lvm2*"
131text p_ig_20 "cloud-*"
132text p_ig_21 "NetworkManager*"
133text p_ig_22 "user@*"
134text p_ig_23 "udev*"
135text p_ig_24 "chrony.service"
136text p_ig_25 "piboot-*"
137text p_ig_26 "containerd.service"
138text p_ig_27 "postfix.service"
139text p_ig_28 "ubuntu-fan.service"
140text p_ig_29 "pi-register*"
141text p_ig_30 "avahi-daemon.*"
142
143text p_tbl_hdr "NAME PORT PROTO OWNER STATUS UNIT BOOT PURPOSE\n"
144text p_sh_name "Name: "
145text p_sh_purp "Purpose: "
146text p_sh_unit "Unit: "
147text p_sh_cat "Category: "
148text p_sh_own "Owner: "
149text p_sh_port "Port: "
150text p_sh_prot "Protocol: "
151text p_sh_stat "Status: "
152text p_sh_boot "Boot: "
153text p_nl "\n"
154
155text p_jl_name "{\"name\":\""
156text p_jl_port "\",\"port\":"
157text p_jl_prot ",\"protocol\":\""
158text p_jl_own "\",\"owner\":\""
159text p_jl_purp "\",\"purpose\":\""
160text p_jl_stat "\",\"status\":\""
161text p_jl_unit "\",\"unit\":\""
162text p_jl_cat "\",\"category\":\""
163text p_jl_boot "\",\"boot\":"
164text p_jl_end "}\n"
165
166note --- Byte, string, and output helpers ---
167
168fn pi_is_ws b uchar to bool
169if eq b uchar 32
170ret true
171end
172if eq b uchar 9
173ret true
174end
175if eq b uchar 13
176ret true
177end
178if eq b uchar 10
179ret true
180end
181ret false
182end
183
184fn pi_trim_l s view uchar * to ulong
185n = len s
186if gt n ulong 65536
187ret n
188end
189i = ulong 0
190while lt i n bound 65536
191if not pi_is_ws get s i
192ret i
193end
194i = add i ulong 1
195end
196ret n
197end
198
199fn pi_trim_r s view uchar * to ulong
200n = len s
201if gt n ulong 65536
202ret ulong 0
203end
204e = n
205while gt e ulong 0 bound 65536
206idx = sub e ulong 1
207if not pi_is_ws get s idx
208ret e
209end
210e = idx
211end
212ret ulong 0
213end
214
215fn pi_eq a view uchar * b view uchar * to bool
216na = len a
217nb = len b
218if ne na nb
219ret false
220end
221if gt na ulong 65536
222ret false
223end
224i = ulong 0
225while lt i na bound 65536
226if ne get a i get b i
227ret false
228end
229i = add i ulong 1
230end
231ret true
232end
233
234fn pi_starts_with s view uchar * pfx view uchar * to bool
235ns = len s
236np = len pfx
237if lt ns np
238ret false
239end
240if gt np ulong 4096
241ret false
242end
243i = ulong 0
244while lt i np bound 4096
245if ne get s i get pfx i
246ret false
247end
248i = add i ulong 1
249end
250ret true
251end
252
253fn pi_ends_with s view uchar * sfx view uchar * to bool
254ns = len s
255np = len sfx
256if lt ns np
257ret false
258end
259if gt np ulong 4096
260ret false
261end
262off = sub ns np
263i = ulong 0
264while lt i np bound 4096
265if ne get s add off i get sfx i
266ret false
267end
268i = add i ulong 1
269end
270ret true
271end
272
273fn pi_write_all fd int buf view uchar * to bool
274n = len buf
275if eq n ulong 0
276ret true
277end
278if gt n ulong 1048576
279ret false
280end
281off = ulong 0
282while lt off n bound 1048576
283remn = sub n off
284k = write fd buf off remn
285if le k long 0
286ret false
287end
288off = add off ulong k
289end
290ret true
291end
292
293fn pi_put_b dst edit uchar * pos ulong b uchar to ulong
294if ge pos len dst
295ret pos
296end
297put dst pos b
298ret add pos ulong 1
299end
300
301fn pi_put_s dst edit uchar * pos ulong src view uchar * to ulong
302ns = len src
303if gt ns ulong 65536
304ret pos
305end
306cap = len dst
307p = pos
308i = ulong 0
309while lt i ns bound 65536
310if ge p cap
311ret p
312end
313put dst p get src i
314p = add p ulong 1
315i = add i ulong 1
316end
317ret p
318end
319
320fn pi_put_u64 dst edit uchar * pos ulong v ulong to ulong
321tmp = new uchar 24 0
322n = ulong 0
323cur = v
324if eq cur ulong 0
325put tmp 0 uchar 48
326n = ulong 1
327else
328while gt cur ulong 0 bound 24
329d = rem cur ulong 10
330put tmp n uchar add d ulong 48
331cur = div cur ulong 10
332n = add n ulong 1
333end
334end
335p = pos
336i = n
337while gt i ulong 0 bound 24
338i = sub i ulong 1
339p = pi_put_b dst p get tmp i
340end
341ret p
342end
343
344fn pi_put_pad dst edit uchar * pos ulong src view uchar * width ulong to ulong
345p = pi_put_s dst pos src
346n = len src
347if lt n width
348pad = sub width n
349if gt pad ulong 64
350pad = ulong 64
351end
352i = ulong 0
353while lt i pad bound 64
354p = pi_put_b dst p uchar 32
355i = add i ulong 1
356end
357else
358p = pi_put_b dst p uchar 32
359end
360ret p
361end
362
363fn pi_put_json_esc dst edit uchar * pos ulong src view uchar * to ulong
364ns = len src
365if gt ns ulong 65536
366ret pos
367end
368p = pos
369i = ulong 0
370while lt i ns bound 65536
371b = get src i
372if eq b uchar 34
373p = pi_put_b dst p uchar 92
374p = pi_put_b dst p uchar 34
375else
376if eq b uchar 92
377p = pi_put_b dst p uchar 92
378p = pi_put_b dst p uchar 92
379else
380if eq b uchar 10
381p = pi_put_b dst p uchar 92
382p = pi_put_b dst p uchar 110
383else
384if eq b uchar 9
385p = pi_put_b dst p uchar 92
386p = pi_put_b dst p uchar 116
387else
388p = pi_put_b dst p b
389end
390end
391end
392end
393i = add i ulong 1
394end
395ret p
396end
397
398note --- Wildcard ignore matching & unit normalization (upstream pi-register) ---
399
400fn pi_pattern_matches pat view uchar * unit view uchar * to bool
401np = len pat
402nu = len unit
403if gt np ulong 256
404ret false
405end
406star = np
407i = ulong 0
408while lt i np bound 256
409if eq get pat i uchar 42
410star = i
411break
412end
413i = add i ulong 1
414end
415if eq star np
416ret pi_eq pat unit
417end
418sfx_len = sub sub np star ulong 1
419if lt nu add star sfx_len
420ret false
421end
422if gt star ulong 0
423if not pi_starts_with unit part pat 0 star
424ret false
425end
426end
427if gt sfx_len ulong 0
428if not pi_ends_with unit part pat add star ulong 1 sfx_len
429ret false
430end
431end
432ret true
433end
434
435fn pi_is_ignored_a unit view uchar * to bool
436if pi_pattern_matches p_ig_01 unit
437ret true
438end
439if pi_pattern_matches p_ig_02 unit
440ret true
441end
442if pi_pattern_matches p_ig_03 unit
443ret true
444end
445if pi_pattern_matches p_ig_04 unit
446ret true
447end
448if pi_pattern_matches p_ig_05 unit
449ret true
450end
451if pi_pattern_matches p_ig_06 unit
452ret true
453end
454if pi_pattern_matches p_ig_07 unit
455ret true
456end
457if pi_pattern_matches p_ig_08 unit
458ret true
459end
460if pi_pattern_matches p_ig_09 unit
461ret true
462end
463if pi_pattern_matches p_ig_10 unit
464ret true
465end
466if pi_pattern_matches p_ig_11 unit
467ret true
468end
469if pi_pattern_matches p_ig_12 unit
470ret true
471end
472if pi_pattern_matches p_ig_13 unit
473ret true
474end
475if pi_pattern_matches p_ig_14 unit
476ret true
477end
478if pi_pattern_matches p_ig_15 unit
479ret true
480end
481ret false
482end
483
484fn pi_is_ignored_b unit view uchar * to bool
485if pi_pattern_matches p_ig_16 unit
486ret true
487end
488if pi_pattern_matches p_ig_17 unit
489ret true
490end
491if pi_pattern_matches p_ig_18 unit
492ret true
493end
494if pi_pattern_matches p_ig_19 unit
495ret true
496end
497if pi_pattern_matches p_ig_20 unit
498ret true
499end
500if pi_pattern_matches p_ig_21 unit
501ret true
502end
503if pi_pattern_matches p_ig_22 unit
504ret true
505end
506if pi_pattern_matches p_ig_23 unit
507ret true
508end
509if pi_pattern_matches p_ig_24 unit
510ret true
511end
512if pi_pattern_matches p_ig_25 unit
513ret true
514end
515if pi_pattern_matches p_ig_26 unit
516ret true
517end
518if pi_pattern_matches p_ig_27 unit
519ret true
520end
521if pi_pattern_matches p_ig_28 unit
522ret true
523end
524if pi_pattern_matches p_ig_29 unit
525ret true
526end
527if pi_pattern_matches p_ig_30 unit
528ret true
529end
530ret false
531end
532
533fn pi_is_ignored unit view uchar * to bool
534if pi_is_ignored_a unit
535ret true
536end
537ret pi_is_ignored_b unit
538end
539
540fn pi_has_unit_suffix unit view uchar * to bool
541if pi_ends_with unit p_sfx_svc
542ret true
543end
544if pi_ends_with unit p_sfx_sock
545ret true
546end
547if pi_ends_with unit p_sfx_tmr
548ret true
549end
550if pi_ends_with unit p_sfx_tgt
551ret true
552end
553if pi_ends_with unit p_sfx_mnt
554ret true
555end
556if pi_ends_with unit p_sfx_pth
557ret true
558end
559ret false
560end
561
562fn pi_full_unit unit view uchar * dst edit uchar * to ulong
563p = pi_put_s dst ulong 0 unit
564if not pi_has_unit_suffix unit
565p = pi_put_s dst p p_sfx_svc
566end
567ret p
568end
569
570note --- Protocol / status / port validation ---
571
572fn pi_parse_proto s view uchar * to ulong
573if pi_eq s p_pr_tcp
574ret PROTO_TCP
575end
576if pi_eq s p_pr_udp
577ret PROTO_UDP
578end
579if pi_eq s p_pr_http
580ret PROTO_HTTP
581end
582if pi_eq s p_pr_https
583ret PROTO_HTTPS
584end
585if pi_eq s p_pr_unix
586ret PROTO_UNIX
587end
588if pi_eq s p_pr_none
589ret PROTO_NONE
590end
591ret ulong 0
592end
593
594fn pi_proto_family proto ulong to ulong
595if eq proto PROTO_TCP
596ret ulong 1
597end
598if eq proto PROTO_HTTP
599ret ulong 1
600end
601if eq proto PROTO_HTTPS
602ret ulong 1
603end
604if eq proto PROTO_UDP
605ret ulong 2
606end
607ret ulong 0
608end
609
610fn pi_put_proto dst edit uchar * pos ulong proto ulong to ulong
611if eq proto PROTO_TCP
612ret pi_put_s dst pos p_pr_tcp
613end
614if eq proto PROTO_UDP
615ret pi_put_s dst pos p_pr_udp
616end
617if eq proto PROTO_HTTP
618ret pi_put_s dst pos p_pr_http
619end
620if eq proto PROTO_HTTPS
621ret pi_put_s dst pos p_pr_https
622end
623if eq proto PROTO_UNIX
624ret pi_put_s dst pos p_pr_unix
625end
626ret pi_put_s dst pos p_pr_none
627end
628
629fn pi_parse_status s view uchar * to ulong
630if pi_eq s p_st_active
631ret STAT_ACTIVE
632end
633if pi_eq s p_st_inactive
634ret STAT_INACTIVE
635end
636if pi_eq s p_st_degraded
637ret STAT_DEGRADED
638end
639if pi_eq s p_st_disabled
640ret STAT_DISABLED
641end
642if pi_eq s p_st_missing
643ret STAT_MISSING
644end
645if pi_eq s p_st_unknown
646ret STAT_UNKNOWN
647end
648ret ulong 0
649end
650
651fn pi_put_status dst edit uchar * pos ulong st ulong to ulong
652if eq st STAT_ACTIVE
653ret pi_put_s dst pos p_st_active
654end
655if eq st STAT_INACTIVE
656ret pi_put_s dst pos p_st_inactive
657end
658if eq st STAT_DEGRADED
659ret pi_put_s dst pos p_st_degraded
660end
661if eq st STAT_DISABLED
662ret pi_put_s dst pos p_st_disabled
663end
664if eq st STAT_MISSING
665ret pi_put_s dst pos p_st_missing
666end
667ret pi_put_s dst pos p_st_unknown
668end
669
670fn pi_parse_port s view uchar * ok edit ulong 1 to ulong
671put ok 0 ulong 0
672n = len s
673if or eq n ulong 0 gt n ulong 5
674ret ulong 0
675end
676acc = ulong 0
677i = ulong 0
678while lt i n bound 6
679b = get s i
680if or lt b uchar 48 gt b uchar 57
681ret ulong 0
682end
683acc = add mul acc ulong 10 ulong sub b uchar 48
684i = add i ulong 1
685end
686if or eq acc ulong 0 gt acc ulong 65535
687ret ulong 0
688end
689put ok 0 ulong 1
690ret acc
691end
692
693note --- Registry table helpers ---
694
695fn pi_reg_reset to void
696put g_pi_n 0 ulong 0
697end
698
699fn pi_svc_count to ulong
700ret get g_pi_n 0
701end
702
703fn pi_init_slot idx ulong to void
704put g_pi_name_len idx ulong 0
705put g_pi_purp_len idx ulong 0
706put g_pi_own_len idx ulong 0
707put g_pi_unit_len idx ulong 0
708put g_pi_cat_len idx ulong 0
709put g_pi_port idx ulong 0
710put g_pi_proto idx PROTO_TCP
711put g_pi_status idx STAT_ACTIVE
712put g_pi_boot idx ulong 0
713end
714
715fn pi_set_name idx ulong s view uchar * to ulong
716n = len s
717if gt n PI_NAME_SLOT
718ret PI_ERR_TOO_LONG
719end
720off = mul idx PI_NAME_SLOT
721i = ulong 0
722while lt i n bound 64
723put g_pi_name add off i get s i
724i = add i ulong 1
725end
726put g_pi_name_len idx n
727ret PI_OK
728end
729
730fn pi_set_purp idx ulong s view uchar * to ulong
731n = len s
732if gt n PI_PURP_SLOT
733ret PI_ERR_TOO_LONG
734end
735off = mul idx PI_PURP_SLOT
736i = ulong 0
737while lt i n bound 128
738put g_pi_purp add off i get s i
739i = add i ulong 1
740end
741put g_pi_purp_len idx n
742ret PI_OK
743end
744
745fn pi_set_own idx ulong s view uchar * to ulong
746n = len s
747if gt n PI_OWN_SLOT
748ret PI_ERR_TOO_LONG
749end
750off = mul idx PI_OWN_SLOT
751i = ulong 0
752while lt i n bound 64
753put g_pi_own add off i get s i
754i = add i ulong 1
755end
756put g_pi_own_len idx n
757ret PI_OK
758end
759
760fn pi_set_unit idx ulong s view uchar * to ulong
761n = len s
762if gt n PI_UNIT_SLOT
763ret PI_ERR_TOO_LONG
764end
765off = mul idx PI_UNIT_SLOT
766i = ulong 0
767while lt i n bound 64
768put g_pi_unit add off i get s i
769i = add i ulong 1
770end
771put g_pi_unit_len idx n
772ret PI_OK
773end
774
775fn pi_set_cat idx ulong s view uchar * to ulong
776n = len s
777if gt n PI_CAT_SLOT
778ret PI_ERR_TOO_LONG
779end
780off = mul idx PI_CAT_SLOT
781i = ulong 0
782while lt i n bound 32
783put g_pi_cat add off i get s i
784i = add i ulong 1
785end
786put g_pi_cat_len idx n
787ret PI_OK
788end
789
790fn pi_get_name idx ulong dst edit uchar * to ulong
791n = get g_pi_name_len idx
792off = mul idx PI_NAME_SLOT
793i = ulong 0
794while lt i n bound 64
795if ge i len dst
796ret i
797end
798put dst i get g_pi_name add off i
799i = add i ulong 1
800end
801ret n
802end
803
804fn pi_get_purp idx ulong dst edit uchar * to ulong
805n = get g_pi_purp_len idx
806off = mul idx PI_PURP_SLOT
807i = ulong 0
808while lt i n bound 128
809if ge i len dst
810ret i
811end
812put dst i get g_pi_purp add off i
813i = add i ulong 1
814end
815ret n
816end
817
818fn pi_get_own idx ulong dst edit uchar * to ulong
819n = get g_pi_own_len idx
820off = mul idx PI_OWN_SLOT
821i = ulong 0
822while lt i n bound 64
823if ge i len dst
824ret i
825end
826put dst i get g_pi_own add off i
827i = add i ulong 1
828end
829ret n
830end
831
832fn pi_get_unit idx ulong dst edit uchar * to ulong
833n = get g_pi_unit_len idx
834off = mul idx PI_UNIT_SLOT
835i = ulong 0
836while lt i n bound 64
837if ge i len dst
838ret i
839end
840put dst i get g_pi_unit add off i
841i = add i ulong 1
842end
843ret n
844end
845
846fn pi_get_cat idx ulong dst edit uchar * to ulong
847n = get g_pi_cat_len idx
848off = mul idx PI_CAT_SLOT
849i = ulong 0
850while lt i n bound 32
851if ge i len dst
852ret i
853end
854put dst i get g_pi_cat add off i
855i = add i ulong 1
856end
857ret n
858end
859
860fn pi_names_eq a ulong b ulong to bool
861la = get g_pi_name_len a
862lb = get g_pi_name_len b
863if ne la lb
864ret false
865end
866oa = mul a PI_NAME_SLOT
867ob = mul b PI_NAME_SLOT
868i = ulong 0
869while lt i la bound 64
870if ne get g_pi_name add oa i get g_pi_name add ob i
871ret false
872end
873i = add i ulong 1
874end
875ret true
876end
877
878fn pi_finalize_slot idx ulong to ulong
879nbuf = new uchar 64 0
880if eq get g_pi_name_len idx ulong 0
881ret PI_ERR_MISSING_FIELD
882end
883if eq get g_pi_purp_len idx ulong 0
884ret PI_ERR_MISSING_FIELD
885end
886if eq get g_pi_own_len idx ulong 0
887ret PI_ERR_MISSING_FIELD
888end
889if eq get g_pi_unit_len idx ulong 0
890nl = pi_get_name idx nbuf
891rc = pi_set_unit idx part nbuf 0 nl
892if ne rc PI_OK
893ret rc
894end
895end
896if eq get g_pi_cat_len idx ulong 0
897rc2 = pi_set_cat idx p_v_general
898if ne rc2 PI_OK
899ret rc2
900end
901end
902note Check duplicate name and duplicate (port, transport family) conflicts against earlier slots
903p_cur = get g_pi_port idx
904f_cur = pi_proto_family get g_pi_proto idx
905j = ulong 0
906while lt j idx bound 64
907if pi_names_eq j idx
908ret PI_ERR_DUP_NAME
909end
910if and gt p_cur ulong 0 gt f_cur ulong 0
911if eq get g_pi_port j p_cur
912if eq pi_proto_family get g_pi_proto j f_cur
913ret PI_ERR_DUP_PORT
914end
915end
916end
917j = add j ulong 1
918end
919ret PI_OK
920end
921
922note --- TOML-style [[service]] parser ---
923
924fn pi_strip_comment line view uchar * to ulong
925n = len line
926if gt n ulong 4096
927ret ulong 0
928end
929in_q = false
930esc = false
931i = ulong 0
932while lt i n bound 4096
933b = get line i
934if esc
935esc = false
936else
937if in_q
938if eq b uchar 92
939esc = true
940else
941if eq b uchar 34
942in_q = false
943end
944end
945else
946if eq b uchar 34
947in_q = true
948else
949if eq b uchar 35
950ret i
951end
952end
953end
954end
955i = add i ulong 1
956end
957ret n
958end
959
960fn pi_unquote raw view uchar * dst edit uchar * ok edit ulong 1 to ulong
961put ok 0 ulong 0
962n = len raw
963if lt n ulong 2
964ret ulong 0
965end
966if gt n ulong 512
967ret ulong 0
968end
969if ne get raw 0 uchar 34
970ret ulong 0
971end
972if ne get raw sub n ulong 1 uchar 34
973ret ulong 0
974end
975p = ulong 0
976cap = len dst
977i = ulong 1
978lim = sub n ulong 1
979while lt i lim bound 512
980b = get raw i
981if eq b uchar 92
982i = add i ulong 1
983if ge i lim
984ret ulong 0
985end
986e = get raw i
987if eq e uchar 110
988b = uchar 10
989else
990if eq e uchar 116
991b = uchar 9
992else
993b = e
994end
995end
996end
997if ge p cap
998ret ulong 0
999end
1000put dst p b
1001p = add p ulong 1
1002i = add i ulong 1
1003end
1004put ok 0 ulong 1
1005ret p
1006end
1007
1008fn pi_apply_kv idx ulong key view uchar * val view uchar * to ulong
1009ubuf = new uchar 256 0
1010ok = new ulong 1 0
1011if pi_eq key p_k_name
1012ul = pi_unquote val ubuf ok
1013if or eq get ok 0 ulong 0 eq ul ulong 0
1014ret PI_ERR_SYNTAX
1015end
1016ret pi_set_name idx part ubuf 0 ul
1017end
1018if pi_eq key p_k_purpose
1019ul = pi_unquote val ubuf ok
1020if or eq get ok 0 ulong 0 eq ul ulong 0
1021ret PI_ERR_SYNTAX
1022end
1023ret pi_set_purp idx part ubuf 0 ul
1024end
1025if pi_eq key p_k_owner
1026ul = pi_unquote val ubuf ok
1027if or eq get ok 0 ulong 0 eq ul ulong 0
1028ret PI_ERR_SYNTAX
1029end
1030ret pi_set_own idx part ubuf 0 ul
1031end
1032if pi_eq key p_k_unit
1033ul = pi_unquote val ubuf ok
1034if or eq get ok 0 ulong 0 eq ul ulong 0
1035ret PI_ERR_SYNTAX
1036end
1037ret pi_set_unit idx part ubuf 0 ul
1038end
1039if pi_eq key p_k_cat
1040ul = pi_unquote val ubuf ok
1041if or eq get ok 0 ulong 0 eq ul ulong 0
1042ret PI_ERR_SYNTAX
1043end
1044ret pi_set_cat idx part ubuf 0 ul
1045end
1046if pi_eq key p_k_port
1047pv = pi_parse_port val ok
1048if eq get ok 0 ulong 0
1049ret PI_ERR_BAD_PORT
1050end
1051put g_pi_port idx pv
1052ret PI_OK
1053end
1054if pi_eq key p_k_proto
1055ul = pi_unquote val ubuf ok
1056if eq get ok 0 ulong 0
1057ret PI_ERR_BAD_PROTO
1058end
1059pr = pi_parse_proto part ubuf 0 ul
1060if eq pr ulong 0
1061ret PI_ERR_BAD_PROTO
1062end
1063put g_pi_proto idx pr
1064ret PI_OK
1065end
1066if pi_eq key p_k_status
1067ul = pi_unquote val ubuf ok
1068if eq get ok 0 ulong 0
1069ret PI_ERR_BAD_STATUS
1070end
1071st = pi_parse_status part ubuf 0 ul
1072if eq st ulong 0
1073ret PI_ERR_BAD_STATUS
1074end
1075put g_pi_status idx st
1076ret PI_OK
1077end
1078if pi_eq key p_k_boot
1079if pi_eq val p_v_true
1080put g_pi_boot idx ulong 1
1081ret PI_OK
1082end
1083if pi_eq val p_v_false
1084put g_pi_boot idx ulong 0
1085ret PI_OK
1086end
1087ret PI_ERR_SYNTAX
1088end
1089if or pi_eq key p_k_deps or pi_eq key p_k_files or pi_eq key p_k_secrets or pi_eq key p_k_health pi_eq key p_k_notes
1090ret PI_OK
1091end
1092ret PI_ERR_SYNTAX
1093end
1094
1095fn pi_find_eq line view uchar * to ulong
1096n = len line
1097if gt n ulong 4096
1098ret n
1099end
1100i = ulong 0
1101while lt i n bound 4096
1102if eq get line i uchar 61
1103ret i
1104end
1105i = add i ulong 1
1106end
1107ret n
1108end
1109
1110fn pi_copy_trimmed s view uchar * dst edit uchar * to ulong
1111ts = pi_trim_l s
1112te = pi_trim_r s
1113if ge ts te
1114ret ulong 0
1115end
1116n = sub te ts
1117if gt n len dst
1118ret ulong 0
1119end
1120i = ulong 0
1121while lt i n bound 256
1122put dst i get s add ts i
1123i = add i ulong 1
1124end
1125ret n
1126end
1127
1128fn pi_parse_kv_line idx ulong line view uchar * to ulong
1129kbuf = new uchar 64 0
1130eqp = pi_find_eq line
1131if or eq eqp ulong 0 ge add eqp ulong 1 len line
1132ret PI_ERR_SYNTAX
1133end
1134kl = pi_copy_trimmed part line 0 eqp kbuf
1135if eq kl ulong 0
1136ret PI_ERR_SYNTAX
1137end
1138voff = add eqp ulong 1
1139vs = pi_trim_l part line voff sub len line voff
1140ve = pi_trim_r part line voff sub len line voff
1141if ge vs ve
1142ret PI_ERR_SYNTAX
1143end
1144ret pi_apply_kv idx part kbuf 0 kl part line add voff vs sub ve vs
1145end
1146
1147fn pi_process_line line view uchar * st edit ulong 2 to ulong
1148ce = pi_strip_comment line
1149ts = pi_trim_l part line 0 ce
1150te = pi_trim_r part line 0 ce
1151if ge ts te
1152ret PI_OK
1153end
1154tlen = sub te ts
1155in_svc = get st 0
1156cur = get st 1
1157if pi_eq part line ts tlen p_sec_hdr
1158if eq in_svc ulong 1
1159fc = pi_finalize_slot cur
1160if ne fc PI_OK
1161ret fc
1162end
1163cur = add cur ulong 1
1164end
1165if ge cur PI_MAX_SVC
1166ret PI_ERR_FULL
1167end
1168pi_init_slot cur
1169put st 0 ulong 1
1170put st 1 cur
1171ret PI_OK
1172end
1173if eq in_svc ulong 0
1174ret PI_ERR_SYNTAX
1175end
1176ret pi_parse_kv_line cur part line ts tlen
1177end
1178
1179fn pi_parse_register src view uchar * info edit PiParseInfo to ulong
1180st = new ulong 2 0
1181pi_reg_reset
1182put info code PI_OK
1183put info line ulong 0
1184put info svc_n ulong 0
1185n = len src
1186if gt n ulong 65536
1187put info code PI_ERR_TOO_LONG
1188ret PI_ERR_TOO_LONG
1189end
1190pos = ulong 0
1191lno = ulong 0
1192while lt pos n bound 65536
1193ls = pos
1194while lt pos n bound 65536
1195if eq get src pos uchar 10
1196break
1197end
1198pos = add pos ulong 1
1199end
1200ll = sub pos ls
1201if lt pos n
1202pos = add pos ulong 1
1203end
1204lno = add lno ulong 1
1205rc = pi_process_line part src ls ll st
1206if ne rc PI_OK
1207put info code rc
1208put info line lno
1209ret rc
1210end
1211end
1212if eq get st 0 ulong 1
1213cur = get st 1
1214fc = pi_finalize_slot cur
1215if ne fc PI_OK
1216put info code fc
1217put info line lno
1218ret fc
1219end
1220put g_pi_n 0 add cur ulong 1
1221end
1222put info svc_n get g_pi_n 0
1223ret PI_OK
1224end
1225
1226note --- Lookup and filtered search ---
1227
1228fn pi_find_by_name name view uchar * to long
1229nbuf = new uchar 64 0
1230cnt = get g_pi_n 0
1231i = ulong 0
1232while lt i cnt bound 64
1233nl = pi_get_name i nbuf
1234if pi_eq part nbuf 0 nl name
1235ret long i
1236end
1237i = add i ulong 1
1238end
1239ret long -1
1240end
1241
1242fn pi_find_by_port port ulong to long
1243if eq port ulong 0
1244ret long -1
1245end
1246cnt = get g_pi_n 0
1247i = ulong 0
1248while lt i cnt bound 64
1249if eq get g_pi_port i port
1250ret long i
1251end
1252i = add i ulong 1
1253end
1254ret long -1
1255end
1256
1257fn pi_matches_filter idx ulong fkind ulong fval view uchar * to bool
1258buf = new uchar 64 0
1259ok = new ulong 1 0
1260if eq fkind FILT_ALL
1261ret true
1262end
1263if eq fkind FILT_OWNER
1264l = pi_get_own idx buf
1265ret pi_eq part buf 0 l fval
1266end
1267if eq fkind FILT_CAT
1268l = pi_get_cat idx buf
1269ret pi_eq part buf 0 l fval
1270end
1271if eq fkind FILT_PROTO
1272pr = pi_parse_proto fval
1273if eq pr ulong 0
1274ret false
1275end
1276ret eq get g_pi_proto idx pr
1277end
1278if eq fkind FILT_STATUS
1279st = pi_parse_status fval
1280if eq st ulong 0
1281ret false
1282end
1283ret eq get g_pi_status idx st
1284end
1285if eq fkind FILT_PORT
1286pv = pi_parse_port fval ok
1287if eq get ok 0 ulong 0
1288ret false
1289end
1290ret eq get g_pi_port idx pv
1291end
1292if eq fkind FILT_BOOT
1293if pi_eq fval p_v_true
1294ret eq get g_pi_boot idx ulong 1
1295end
1296if pi_eq fval p_v_false
1297ret eq get g_pi_boot idx ulong 0
1298end
1299ret false
1300end
1301ret false
1302end
1303
1304fn pi_count_matching fkind ulong fval view uchar * to ulong
1305cnt = get g_pi_n 0
1306hits = ulong 0
1307i = ulong 0
1308while lt i cnt bound 64
1309if pi_matches_filter i fkind fval
1310hits = add hits ulong 1
1311end
1312i = add i ulong 1
1313end
1314ret hits
1315end
1316
1317note --- Deterministic plain-text table, show, and JSONL rendering ---
1318
1319fn pi_render_table_row idx ulong dst edit uchar * pos ulong to ulong
1320buf = new uchar 128 0
1321fub = new uchar 80 0
1322p = pos
1323n = pi_get_name idx buf
1324p = pi_put_pad dst p part buf 0 n ulong 17
1325pv = get g_pi_port idx
1326if eq pv ulong 0
1327p = pi_put_pad dst p p_v_dash ulong 7
1328else
1329n = pi_put_u64 buf ulong 0 pv
1330p = pi_put_pad dst p part buf 0 n ulong 7
1331end
1332n = pi_put_proto buf ulong 0 get g_pi_proto idx
1333p = pi_put_pad dst p part buf 0 n ulong 7
1334n = pi_get_own idx buf
1335p = pi_put_pad dst p part buf 0 n ulong 17
1336n = pi_put_status buf ulong 0 get g_pi_status idx
1337p = pi_put_pad dst p part buf 0 n ulong 11
1338n = pi_get_unit idx buf
1339ful = pi_full_unit part buf 0 n fub
1340p = pi_put_pad dst p part fub 0 ful ulong 21
1341if eq get g_pi_boot idx ulong 1
1342p = pi_put_pad dst p p_v_yes ulong 6
1343else
1344p = pi_put_pad dst p p_v_no ulong 6
1345end
1346n = pi_get_purp idx buf
1347p = pi_put_s dst p part buf 0 n
1348p = pi_put_b dst p uchar 10
1349ret p
1350end
1351
1352fn pi_render_table fkind ulong fval view uchar * dst edit uchar * to ulong
1353p = pi_put_s dst ulong 0 p_tbl_hdr
1354cnt = get g_pi_n 0
1355i = ulong 0
1356while lt i cnt bound 64
1357if pi_matches_filter i fkind fval
1358p = pi_render_table_row i dst p
1359end
1360i = add i ulong 1
1361end
1362ret p
1363end
1364
1365fn pi_render_jsonl_row idx ulong dst edit uchar * pos ulong to ulong
1366buf = new uchar 128 0
1367fub = new uchar 80 0
1368p = pos
1369p = pi_put_s dst p p_jl_name
1370n = pi_get_name idx buf
1371p = pi_put_json_esc dst p part buf 0 n
1372p = pi_put_s dst p p_jl_port
1373pv = get g_pi_port idx
1374if eq pv ulong 0
1375p = pi_put_s dst p p_v_null
1376else
1377p = pi_put_u64 dst p pv
1378end
1379p = pi_put_s dst p p_jl_prot
1380p = pi_put_proto dst p get g_pi_proto idx
1381p = pi_put_s dst p p_jl_own
1382n = pi_get_own idx buf
1383p = pi_put_json_esc dst p part buf 0 n
1384p = pi_put_s dst p p_jl_purp
1385n = pi_get_purp idx buf
1386p = pi_put_json_esc dst p part buf 0 n
1387p = pi_put_s dst p p_jl_stat
1388p = pi_put_status dst p get g_pi_status idx
1389p = pi_put_s dst p p_jl_unit
1390n = pi_get_unit idx buf
1391ful = pi_full_unit part buf 0 n fub
1392p = pi_put_json_esc dst p part fub 0 ful
1393p = pi_put_s dst p p_jl_cat
1394n = pi_get_cat idx buf
1395p = pi_put_json_esc dst p part buf 0 n
1396p = pi_put_s dst p p_jl_boot
1397if eq get g_pi_boot idx ulong 1
1398p = pi_put_s dst p p_v_true
1399else
1400p = pi_put_s dst p p_v_false
1401end
1402p = pi_put_s dst p p_jl_end
1403ret p
1404end
1405
1406fn pi_render_jsonl fkind ulong fval view uchar * dst edit uchar * to ulong
1407p = ulong 0
1408cnt = get g_pi_n 0
1409i = ulong 0
1410while lt i cnt bound 64
1411if pi_matches_filter i fkind fval
1412p = pi_render_jsonl_row i dst p
1413end
1414i = add i ulong 1
1415end
1416ret p
1417end
1418
1419fn pi_render_show idx ulong dst edit uchar * to ulong
1420buf = new uchar 128 0
1421fub = new uchar 80 0
1422p = ulong 0
1423p = pi_put_s dst p p_sh_name
1424n = pi_get_name idx buf
1425p = pi_put_s dst p part buf 0 n
1426p = pi_put_s dst p p_nl
1427p = pi_put_s dst p p_sh_purp
1428n = pi_get_purp idx buf
1429p = pi_put_s dst p part buf 0 n
1430p = pi_put_s dst p p_nl
1431p = pi_put_s dst p p_sh_unit
1432n = pi_get_unit idx buf
1433ful = pi_full_unit part buf 0 n fub
1434p = pi_put_s dst p part fub 0 ful
1435p = pi_put_s dst p p_nl
1436p = pi_put_s dst p p_sh_cat
1437n = pi_get_cat idx buf
1438p = pi_put_s dst p part buf 0 n
1439p = pi_put_s dst p p_nl
1440p = pi_put_s dst p p_sh_own
1441n = pi_get_own idx buf
1442p = pi_put_s dst p part buf 0 n
1443p = pi_put_s dst p p_nl
1444pv = get g_pi_port idx
1445if gt pv ulong 0
1446p = pi_put_s dst p p_sh_port
1447p = pi_put_u64 dst p pv
1448p = pi_put_s dst p p_nl
1449end
1450p = pi_put_s dst p p_sh_prot
1451p = pi_put_proto dst p get g_pi_proto idx
1452p = pi_put_s dst p p_nl
1453p = pi_put_s dst p p_sh_stat
1454p = pi_put_status dst p get g_pi_status idx
1455p = pi_put_s dst p p_nl
1456p = pi_put_s dst p p_sh_boot
1457if eq get g_pi_boot idx ulong 1
1458p = pi_put_s dst p p_v_yes
1459else
1460p = pi_put_s dst p p_v_no
1461end
1462p = pi_put_s dst p p_nl
1463ret p
1464end