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