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 / main.om
| 1 | note aienux/pi-register/main.om: CLI entrypoint for pi-register |
| 2 | note Compose with: si/directives/om2.om aienux/pi-register/lib.om aienux/pi-register/main.om |
| 3 | |
| 4 | text pm_flag_reg "--register" |
| 5 | text pm_cmd_status "status" |
| 6 | text pm_cmd_jsonl "jsonl" |
| 7 | text pm_cmd_show "show" |
| 8 | text pm_cmd_filter "filter" |
| 9 | text pm_cmd_check "check" |
| 10 | |
| 11 | text pm_f_owner "owner" |
| 12 | text pm_f_proto "protocol" |
| 13 | text pm_f_status "status" |
| 14 | text pm_f_cat "category" |
| 15 | text pm_f_port "port" |
| 16 | text pm_f_boot "boot" |
| 17 | |
| 18 | text pm_err_open "pi-register: could not open register file\n" |
| 19 | text pm_err_parse "pi-register: registry validation failed (code=" |
| 20 | text pm_err_line ", line=" |
| 21 | text pm_err_close ")\n" |
| 22 | text pm_err_notfound "pi-register: service not found in register\n" |
| 23 | text pm_err_filter "pi-register: unknown filter field\n" |
| 24 | text pm_ok_check "All good: registry parsed and validated (" |
| 25 | text pm_ok_svcs " services).\n" |
| 26 | text pm_usage "Usage: pi-register [--register <path>] <status|jsonl|show <name>|filter <field> <value>|check>\n" |
| 27 | |
| 28 | fn pi_read_fd fd int dst edit uchar * to ulong |
| 29 | cap = len dst |
| 30 | pos = ulong 0 |
| 31 | note bound 65536 caps register input size |
| 32 | while lt pos cap bound 65536 |
| 33 | remn = sub cap pos |
| 34 | k = read fd dst pos remn |
| 35 | if le k long 0 |
| 36 | break |
| 37 | end |
| 38 | pos = add pos ulong k |
| 39 | end |
| 40 | ret pos |
| 41 | end |
| 42 | |
| 43 | fn pi_parse_fkind s view uchar * to ulong |
| 44 | if pi_eq s pm_f_owner |
| 45 | ret FILT_OWNER |
| 46 | end |
| 47 | if pi_eq s pm_f_proto |
| 48 | ret FILT_PROTO |
| 49 | end |
| 50 | if pi_eq s pm_f_status |
| 51 | ret FILT_STATUS |
| 52 | end |
| 53 | if pi_eq s pm_f_cat |
| 54 | ret FILT_CAT |
| 55 | end |
| 56 | if pi_eq s pm_f_port |
| 57 | ret FILT_PORT |
| 58 | end |
| 59 | if pi_eq s pm_f_boot |
| 60 | ret FILT_BOOT |
| 61 | end |
| 62 | ret ulong 99 |
| 63 | end |
| 64 | |
| 65 | fn pi_emit_parse_err info view PiParseInfo to void |
| 66 | ebuf = new uchar 128 0 |
| 67 | p = ulong 0 |
| 68 | p = pi_put_s ebuf p pm_err_parse |
| 69 | p = pi_put_u64 ebuf p get info code |
| 70 | p = pi_put_s ebuf p pm_err_line |
| 71 | p = pi_put_u64 ebuf p get info line |
| 72 | p = pi_put_s ebuf p pm_err_close |
| 73 | w = pi_write_all int 2 part ebuf 0 p |
| 74 | if w |
| 75 | ret |
| 76 | end |
| 77 | end |
| 78 | |
| 79 | fn pi_run_cmd cidx ulong ac ulong to int |
| 80 | outbuf = new uchar 16384 0 |
| 81 | arg0 = new uchar 256 0 |
| 82 | arg1 = new uchar 256 0 |
| 83 | arg2 = new uchar 256 0 |
| 84 | nc = argread cidx arg0 ulong 0 ulong 256 |
| 85 | if le nc long 0 |
| 86 | ret 2 |
| 87 | end |
| 88 | l0 = ulong nc |
| 89 | if pi_eq part arg0 0 l0 pm_cmd_status |
| 90 | n = pi_render_table FILT_ALL part arg1 0 0 outbuf |
| 91 | if pi_write_all int 1 part outbuf 0 n |
| 92 | ret 0 |
| 93 | end |
| 94 | ret 1 |
| 95 | end |
| 96 | if pi_eq part arg0 0 l0 pm_cmd_jsonl |
| 97 | n = pi_render_jsonl FILT_ALL part arg1 0 0 outbuf |
| 98 | if pi_write_all int 1 part outbuf 0 n |
| 99 | ret 0 |
| 100 | end |
| 101 | ret 1 |
| 102 | end |
| 103 | if pi_eq part arg0 0 l0 pm_cmd_check |
| 104 | p = pi_put_s outbuf ulong 0 pm_ok_check |
| 105 | p = pi_put_u64 outbuf p pi_svc_count |
| 106 | p = pi_put_s outbuf p pm_ok_svcs |
| 107 | if pi_write_all int 1 part outbuf 0 p |
| 108 | ret 0 |
| 109 | end |
| 110 | ret 1 |
| 111 | end |
| 112 | if pi_eq part arg0 0 l0 pm_cmd_show |
| 113 | if le ac add cidx ulong 1 |
| 114 | w0 = pi_write_all int 2 pm_usage |
| 115 | if w0 |
| 116 | ret 2 |
| 117 | end |
| 118 | ret 2 |
| 119 | end |
| 120 | r1 = argread add cidx ulong 1 arg1 ulong 0 ulong 256 |
| 121 | if le r1 long 0 |
| 122 | ret 2 |
| 123 | end |
| 124 | idx = pi_find_by_name part arg1 0 ulong r1 |
| 125 | if lt idx long 0 |
| 126 | w = pi_write_all int 2 pm_err_notfound |
| 127 | if w |
| 128 | ret 1 |
| 129 | end |
| 130 | ret 1 |
| 131 | end |
| 132 | n = pi_render_show ulong idx outbuf |
| 133 | if pi_write_all int 1 part outbuf 0 n |
| 134 | ret 0 |
| 135 | end |
| 136 | ret 1 |
| 137 | end |
| 138 | if pi_eq part arg0 0 l0 pm_cmd_filter |
| 139 | if le ac add cidx ulong 2 |
| 140 | w1 = pi_write_all int 2 pm_usage |
| 141 | if w1 |
| 142 | ret 2 |
| 143 | end |
| 144 | ret 2 |
| 145 | end |
| 146 | r1 = argread add cidx ulong 1 arg1 ulong 0 ulong 256 |
| 147 | r2 = argread add cidx ulong 2 arg2 ulong 0 ulong 256 |
| 148 | if or le r1 long 0 le r2 long 0 |
| 149 | ret 2 |
| 150 | end |
| 151 | fk = pi_parse_fkind part arg1 0 ulong r1 |
| 152 | if eq fk ulong 99 |
| 153 | w2 = pi_write_all int 2 pm_err_filter |
| 154 | if w2 |
| 155 | ret 2 |
| 156 | end |
| 157 | ret 2 |
| 158 | end |
| 159 | n = pi_render_jsonl fk part arg2 0 ulong r2 outbuf |
| 160 | if pi_write_all int 1 part outbuf 0 n |
| 161 | ret 0 |
| 162 | end |
| 163 | ret 1 |
| 164 | end |
| 165 | w3 = pi_write_all int 2 pm_usage |
| 166 | if w3 |
| 167 | ret 2 |
| 168 | end |
| 169 | ret 2 |
| 170 | end |
| 171 | |
| 172 | fn pi_load_source use_file bool path view uchar * inp edit uchar * to long |
| 173 | if not use_file |
| 174 | ret long pi_read_fd int 0 inp |
| 175 | end |
| 176 | fdl = fopen path ulong 0 len path int 0 int 0 |
| 177 | if lt fdl long 0 |
| 178 | w = pi_write_all int 2 pm_err_open |
| 179 | if w |
| 180 | ret long -1 |
| 181 | end |
| 182 | ret long -1 |
| 183 | end |
| 184 | nlen = pi_read_fd int fdl inp |
| 185 | cl = fclose int fdl |
| 186 | if lt cl long 0 |
| 187 | ret long -1 |
| 188 | end |
| 189 | ret long nlen |
| 190 | end |
| 191 | |
| 192 | fn pi_load_and_parse cidx_out edit ulong 1 to int |
| 193 | inp = new uchar 32768 0 |
| 194 | arg0 = new uchar 256 0 |
| 195 | arg1 = new uchar 256 0 |
| 196 | info = new PiParseInfo 0 0 0 |
| 197 | ac = argcnt |
| 198 | n0 = argread ulong 1 arg0 ulong 0 ulong 256 |
| 199 | if le n0 long 0 |
| 200 | ret 2 |
| 201 | end |
| 202 | nlen_s = long 0 |
| 203 | if pi_eq part arg0 0 ulong n0 pm_flag_reg |
| 204 | if le ac long 3 |
| 205 | w2 = pi_write_all int 2 pm_usage |
| 206 | if w2 |
| 207 | ret 2 |
| 208 | end |
| 209 | ret 2 |
| 210 | end |
| 211 | np = argread ulong 2 arg1 ulong 0 ulong 256 |
| 212 | if le np long 0 |
| 213 | ret 2 |
| 214 | end |
| 215 | nlen_s = pi_load_source true part arg1 0 ulong np inp |
| 216 | put cidx_out 0 ulong 3 |
| 217 | else |
| 218 | nlen_s = pi_load_source false part arg1 0 0 inp |
| 219 | put cidx_out 0 ulong 1 |
| 220 | end |
| 221 | if lt nlen_s long 0 |
| 222 | ret 1 |
| 223 | end |
| 224 | rc = pi_parse_register part inp 0 ulong nlen_s info |
| 225 | if ne rc PI_OK |
| 226 | pi_emit_parse_err info |
| 227 | ret 1 |
| 228 | end |
| 229 | ret 0 |
| 230 | end |
| 231 | |
| 232 | fn main to int |
| 233 | cidx_box = new ulong 1 0 |
| 234 | ac = argcnt |
| 235 | if le ac long 1 |
| 236 | w = pi_write_all int 1 pm_usage |
| 237 | if w |
| 238 | ret 0 |
| 239 | end |
| 240 | ret 1 |
| 241 | end |
| 242 | lrc = pi_load_and_parse cidx_box |
| 243 | if ne lrc int 0 |
| 244 | ret lrc |
| 245 | end |
| 246 | ret pi_run_cmd get cidx_box 0 ulong ac |
| 247 | end |