aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / tools / candidate / check_candidate.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note check_candidate: validate a candidate manifest (docs/qualification/CANDIDATE.md) and its |
| 3 | note amendments. Seeded native Omega port of scripts/check_candidate.sh (aien-architecture #316). |
| 4 | note usage: check_candidate <manifest.toml> exit 0 = valid, 1 = invalid |
| 5 | note Same lines on stdout/stderr and the same exit code as the bash it replaces, including the |
| 6 | note order of the FAIL lines: keys are kept in a table that walks like a bash 5.2 associative |
| 7 | note array (FNV-1 hash, 1024 buckets, x4 growth, newest first in a bucket). sha existence is |
| 8 | note checked with `gh api repos/aien-dev/<repo>/commits/<sha> -q .sha`, or with the command in |
| 9 | note CAND_SHA_CHECK (run as: $CAND_SHA_CHECK <repo> <sha>). Children run through /bin/sh with |
| 10 | note this process's environment replayed from a memfd, so PATH and the stub variables reach them. |
| 11 | note Amendments: every <stem>.amendment-<n>.toml next to the manifest is read and checked too. |
| 12 | note Capacities (exit 70 beyond them): a file of 999000 bytes, 4096 keys per file, keys and |
| 13 | note section names of 8191 bytes, 1024 amendment files, a child argument list of 4096 bytes. |
| 14 | note See tools/candidate/README.md for the parsed subset and the named limits. |
| 15 | |
| 16 | text tsh "set -a\n. \"$1\"\nset +a\nshift\nexec \"$@\"\n" |
| 17 | text tm "usage: \n <manifest.toml>\nFAIL: \nmissing field: \nschema must be CandidateManifestV1\nstatus must be 'draft, not frozen', 'frozen' or 'superseded'\nmissing commit: \ncommit for \n is not 40 lowercase hex: \ncommit \n not found in aien-dev/\n is UNBUILT but status is not draft\n digest is not 64 lowercase hex\n digest is all zero (placeholder, not a real digest)\nno [executables] entries\n: schema must be CandidateAmendmentV1\n: candidate '\n' is not the manifest id '\n'\n: amendment '\n' does not match its file name (\n)\n: date and reason are required\n: pins manifest sha256 '\n' but \n is \n (frozen manifests are not edited)\n: unknown top-level key '\n cannot be amended (it would change what the candidate is)\n is already in the manifest; an amendment only adds keys\n is not a real sha256\n: adds no keys\nOK: \n (amendments: \n)\n: \ncheck_candidate: input over capacity\n" |
| 18 | text tk "schema\nid\nstatus\ncreated\ncommits.omega\ncommits.aienos\ncommits.aien-sovereign-core\ncommits.aien-protocols\ncommits.aien-architecture\ncontracts.crumb-spec\ncontracts.spark-crumbs\ncommits.physics\ncommits.interplane\nCandidateManifestV1\ndraft, not frozen\nfrozen\nsuperseded\nexecutables.\nUNBUILT\ndraft\nCandidateAmendmentV1\ncandidate\namendment\ndate\nreason\nmanifest-sha256\ncommits.\ncontracts.\nconsumed_pins.\n-sha256\n.amendment-\n.toml\nCAND_SHA_CHECK\n/bin/sh\n-c\nsh\n/proc/self/fd/\ngh\napi\nrepos/aien-dev/\n/commits/\n-q\n.sha\n/dev/null\n.\nenvfile\n" |
| 19 | |
| 20 | const CAP ulong 999000 |
| 21 | const NREQ ulong 7 |
| 22 | const NOPT ulong 2 |
| 23 | const MASK ulong 4294967295 |
| 24 | |
| 25 | global src uchar 999128 |
| 26 | global srcn ulong 1 |
| 27 | global pool uchar 1998000 |
| 28 | global pooln ulong 1 |
| 29 | global poolm ulong 1 |
| 30 | global kof ulong 8192 |
| 31 | global kln ulong 8192 |
| 32 | global vof ulong 8192 |
| 33 | global vln ulong 8192 |
| 34 | global khs ulong 8192 |
| 35 | global knx ulong 8192 |
| 36 | global kbk ulong 8192 |
| 37 | global kb2 ulong 4096 |
| 38 | global nen ulong 2 |
| 39 | global nbk ulong 2 |
| 40 | global secb uchar 8192 |
| 41 | global secl ulong 1 |
| 42 | global arg uchar 8192 |
| 43 | global argl ulong 1 |
| 44 | global obuf uchar 1100000 |
| 45 | global obl ulong 1 |
| 46 | global fpath uchar 4096 |
| 47 | global fpl ulong 1 |
| 48 | global a0 uchar 4096 |
| 49 | global a0l ulong 1 |
| 50 | global apath uchar 8192 |
| 51 | global apl ulong 1 |
| 52 | global dirp uchar 4096 |
| 53 | global dirl ulong 1 |
| 54 | global apre uchar 4096 |
| 55 | global aprl ulong 1 |
| 56 | global anum uchar 4096 |
| 57 | global anml ulong 1 |
| 58 | global apool uchar 262144 |
| 59 | global apn ulong 1 |
| 60 | global ano ulong 1024 |
| 61 | global anl ulong 1024 |
| 62 | global aix ulong 1024 |
| 63 | global acnt ulong 1 |
| 64 | global dbuf uchar 65536 |
| 65 | global stb uchar 128 |
| 66 | global rp uchar 8192 |
| 67 | global rpl ulong 1 |
| 68 | global mfh uchar 64 |
| 69 | global failf ulong 1 |
| 70 | global namend ulong 1 |
| 71 | global pk uchar 4096 |
| 72 | global pkn ulong 1 |
| 73 | global pkc ulong 1 |
| 74 | global ebuf uchar 65600 |
| 75 | global ewb uchar 131200 |
| 76 | global ev uchar 65600 |
| 77 | global evl ulong 1 |
| 78 | global efd long 1 |
| 79 | global epath uchar 32 |
| 80 | global epl ulong 1 |
| 81 | global status uchar 4 |
| 82 | global rmode ulong 1 |
| 83 | global nbuf uchar 24 |
| 84 | global shv ulong 8 |
| 85 | global swt ulong 64 |
| 86 | global skt ulong 64 |
| 87 | global sst ulong 8 |
| 88 | |
| 89 | note ---- text tables ---- |
| 90 | |
| 91 | fn om i ulong to void |
| 92 | s = ulong 0 |
| 93 | k = ulong 0 |
| 94 | while lt k i bound 8192 |
| 95 | if eq get tm s 10 |
| 96 | k = add k 1 |
| 97 | end |
| 98 | s = add s 1 |
| 99 | end |
| 100 | n = get obl 0 |
| 101 | while ne get tm s 10 bound 8191 |
| 102 | put obuf n get tm s |
| 103 | n = add n 1 |
| 104 | s = add s 1 |
| 105 | end |
| 106 | put obl 0 n |
| 107 | end |
| 108 | |
| 109 | fn kp i ulong to void |
| 110 | s = ulong 0 |
| 111 | k = ulong 0 |
| 112 | while lt k i bound 8192 |
| 113 | if eq get tk s 10 |
| 114 | k = add k 1 |
| 115 | end |
| 116 | s = add s 1 |
| 117 | end |
| 118 | n = ulong 0 |
| 119 | while ne get tk add s n 10 bound 8191 |
| 120 | put arg n get tk add s n |
| 121 | n = add n 1 |
| 122 | end |
| 123 | put argl 0 n |
| 124 | end |
| 125 | |
| 126 | fn over to void |
| 127 | put obl 0 0 |
| 128 | om 36 |
| 129 | oc 10 |
| 130 | oflush 2 |
| 131 | exit 70 |
| 132 | end |
| 133 | |
| 134 | note ---- output line ---- |
| 135 | |
| 136 | fn oc c uchar to void |
| 137 | n = get obl 0 |
| 138 | put obuf n c |
| 139 | put obl 0 add n 1 |
| 140 | end |
| 141 | |
| 142 | fn oflush fd int to void |
| 143 | r = write fd obuf 0 get obl 0 |
| 144 | put obl 0 0 |
| 145 | end |
| 146 | |
| 147 | fn opool o ulong n ulong to void |
| 148 | b = get obl 0 |
| 149 | for i ulong n |
| 150 | put obuf add b i get pool add o i |
| 151 | end |
| 152 | put obl 0 add b n |
| 153 | end |
| 154 | |
| 155 | fn oarg to void |
| 156 | b = get obl 0 |
| 157 | for i ulong get argl 0 |
| 158 | put obuf add b i get arg i |
| 159 | end |
| 160 | put obl 0 add b get argl 0 |
| 161 | end |
| 162 | |
| 163 | fn ofile to void |
| 164 | b = get obl 0 |
| 165 | for i ulong get fpl 0 |
| 166 | put obuf add b i get fpath i |
| 167 | end |
| 168 | put obl 0 add b get fpl 0 |
| 169 | end |
| 170 | |
| 171 | fn oapath to void |
| 172 | b = get obl 0 |
| 173 | for i ulong get apl 0 |
| 174 | put obuf add b i get apath i |
| 175 | end |
| 176 | put obl 0 add b get apl 0 |
| 177 | end |
| 178 | |
| 179 | fn oanum to void |
| 180 | b = get obl 0 |
| 181 | for i ulong get anml 0 |
| 182 | put obuf add b i get anum i |
| 183 | end |
| 184 | put obl 0 add b get anml 0 |
| 185 | end |
| 186 | |
| 187 | fn omfh to void |
| 188 | b = get obl 0 |
| 189 | for i ulong 64 |
| 190 | put obuf add b i get mfh i |
| 191 | end |
| 192 | put obl 0 add b 64 |
| 193 | end |
| 194 | |
| 195 | fn okey x ulong to void |
| 196 | opool get kof x get kln x |
| 197 | end |
| 198 | |
| 199 | fn oval e long to void |
| 200 | if ge e 0 |
| 201 | opool get vof ulong e get vln ulong e |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | fn onum v ulong to void |
| 206 | d = ulong 0 |
| 207 | while gt v 0 bound 24 |
| 208 | put nbuf d uchar rem v 10 |
| 209 | v = div v 10 |
| 210 | d = add d 1 |
| 211 | end |
| 212 | if eq d 0 |
| 213 | put nbuf 0 0 |
| 214 | d = ulong 1 |
| 215 | end |
| 216 | for i ulong d |
| 217 | oc add 48 get nbuf sub sub d 1 i |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | fn fail0 to void |
| 222 | put obl 0 0 |
| 223 | om 2 |
| 224 | end |
| 225 | |
| 226 | fn fail1 to void |
| 227 | oc 10 |
| 228 | oflush 2 |
| 229 | put failf 0 1 |
| 230 | end |
| 231 | |
| 232 | fn afail i ulong to void |
| 233 | fail0 |
| 234 | oapath |
| 235 | om i |
| 236 | end |
| 237 | |
| 238 | note ---- key table: bash 5.2 associative array order ---- |
| 239 | |
| 240 | fn hashk to ulong |
| 241 | h = ulong 2166136261 |
| 242 | for j ulong get argl 0 |
| 243 | h = and mul h 16777619 MASK |
| 244 | h = xor h ulong get arg j |
| 245 | end |
| 246 | ret h |
| 247 | end |
| 248 | |
| 249 | fn treset t ulong to void |
| 250 | put nbk t 1024 |
| 251 | put nen t 0 |
| 252 | for b ulong 1024 |
| 253 | put kbk add mul t 4096 b 0 |
| 254 | end |
| 255 | end |
| 256 | |
| 257 | fn keyeq x ulong to bool |
| 258 | n = get argl 0 |
| 259 | if ne get kln x n |
| 260 | ret false |
| 261 | end |
| 262 | o = get kof x |
| 263 | for i ulong n |
| 264 | if ne get pool add o i get arg i |
| 265 | ret false |
| 266 | end |
| 267 | end |
| 268 | ret true |
| 269 | end |
| 270 | |
| 271 | fn tfind t ulong to long |
| 272 | h = hashk |
| 273 | b = and h sub get nbk t 1 |
| 274 | e = get kbk add mul t 4096 b |
| 275 | while ne e 0 bound 8192 |
| 276 | e = sub e 1 |
| 277 | if eq get khs e h |
| 278 | if keyeq e |
| 279 | ret long e |
| 280 | end |
| 281 | end |
| 282 | e = get knx e |
| 283 | end |
| 284 | ret -1 |
| 285 | end |
| 286 | |
| 287 | fn pcopy o ulong n ulong to void |
| 288 | p = get pooln 0 |
| 289 | if gt add p n len pool |
| 290 | over |
| 291 | end |
| 292 | for i ulong n |
| 293 | put pool add p i get src add o i |
| 294 | end |
| 295 | put pooln 0 add p n |
| 296 | end |
| 297 | |
| 298 | fn acopy to void |
| 299 | p = get pooln 0 |
| 300 | n = get argl 0 |
| 301 | if gt add p n len pool |
| 302 | over |
| 303 | end |
| 304 | for i ulong n |
| 305 | put pool add p i get arg i |
| 306 | end |
| 307 | put pooln 0 add p n |
| 308 | end |
| 309 | |
| 310 | fn tlink t ulong x ulong h ulong to void |
| 311 | b = add mul t 4096 and h sub get nbk t 1 |
| 312 | put knx x get kbk b |
| 313 | put kbk b add x 1 |
| 314 | end |
| 315 | |
| 316 | fn rehash t ulong j ulong to void |
| 317 | e = get kb2 j |
| 318 | while ne e 0 bound 8192 |
| 319 | x = sub e 1 |
| 320 | n = get knx x |
| 321 | tlink t x get khs x |
| 322 | e = n |
| 323 | end |
| 324 | end |
| 325 | |
| 326 | fn tgrow t ulong to void |
| 327 | nb = get nbk t |
| 328 | if gt mul nb 4 4096 |
| 329 | over |
| 330 | end |
| 331 | tb = mul t 4096 |
| 332 | for b ulong nb |
| 333 | put kb2 b get kbk add tb b |
| 334 | put kbk add tb b 0 |
| 335 | end |
| 336 | put nbk t mul nb 4 |
| 337 | for j ulong nb |
| 338 | rehash t j |
| 339 | end |
| 340 | end |
| 341 | |
| 342 | fn tset t ulong vs ulong vn ulong to void |
| 343 | e = tfind t |
| 344 | if ge e 0 |
| 345 | x = ulong e |
| 346 | put vof x get pooln 0 |
| 347 | put vln x vn |
| 348 | pcopy vs vn |
| 349 | ret |
| 350 | end |
| 351 | if ge get nen t mul get nbk t 2 |
| 352 | tgrow t |
| 353 | end |
| 354 | n = get nen t |
| 355 | if ge n 4096 |
| 356 | over |
| 357 | end |
| 358 | x = add mul t 4096 n |
| 359 | put kof x get pooln 0 |
| 360 | put kln x get argl 0 |
| 361 | acopy |
| 362 | put vof x get pooln 0 |
| 363 | put vln x vn |
| 364 | pcopy vs vn |
| 365 | h = hashk |
| 366 | put khs x h |
| 367 | tlink t x h |
| 368 | put nen t add n 1 |
| 369 | end |
| 370 | |
| 371 | note ---- the line parser: key = "value" [# comment], [section] [# comment], the rest ignored ---- |
| 372 | |
| 373 | fn isws c uchar to bool |
| 374 | ret or eq c 32 and ge c 9 le c 13 |
| 375 | end |
| 376 | |
| 377 | fn iskc c uchar to bool |
| 378 | d = or and ge c 48 le c 57 and ge c 65 le c 90 |
| 379 | d = or d and ge c 97 le c 122 |
| 380 | ret or d or eq c 95 or eq c 46 eq c 45 |
| 381 | end |
| 382 | |
| 383 | fn skipws i ulong e ulong to ulong |
| 384 | while lt i e bound 999999 |
| 385 | if not isws get src i |
| 386 | ret i |
| 387 | end |
| 388 | i = add i 1 |
| 389 | end |
| 390 | ret i |
| 391 | end |
| 392 | |
| 393 | fn skipkc i ulong e ulong to ulong |
| 394 | while lt i e bound 999999 |
| 395 | if not iskc get src i |
| 396 | ret i |
| 397 | end |
| 398 | i = add i 1 |
| 399 | end |
| 400 | ret i |
| 401 | end |
| 402 | |
| 403 | fn notq i ulong e ulong to bool |
| 404 | ret and lt i e ne get src i 34 |
| 405 | end |
| 406 | |
| 407 | fn skipnq i ulong e ulong to ulong |
| 408 | while notq i e bound 999999 |
| 409 | i = add i 1 |
| 410 | end |
| 411 | ret i |
| 412 | end |
| 413 | |
| 414 | fn mkkey ks ulong kn ulong to void |
| 415 | n = get secl 0 |
| 416 | if gt add n kn 8191 |
| 417 | over |
| 418 | end |
| 419 | for i ulong n |
| 420 | put arg i get secb i |
| 421 | end |
| 422 | for i ulong kn |
| 423 | put arg add n i get src add ks i |
| 424 | end |
| 425 | put argl 0 add n kn |
| 426 | end |
| 427 | |
| 428 | fn kvline t ulong ls ulong z ulong to bool |
| 429 | i = skipws ls z |
| 430 | ks = i |
| 431 | i = skipkc i z |
| 432 | kn = sub i ks |
| 433 | if eq kn 0 |
| 434 | ret false |
| 435 | end |
| 436 | i = skipws i z |
| 437 | if ge i z |
| 438 | ret false |
| 439 | end |
| 440 | if ne get src i 61 |
| 441 | ret false |
| 442 | end |
| 443 | i = skipws add i 1 z |
| 444 | if ge i z |
| 445 | ret false |
| 446 | end |
| 447 | if ne get src i 34 |
| 448 | ret false |
| 449 | end |
| 450 | vs = add i 1 |
| 451 | i = skipnq vs z |
| 452 | if ge i z |
| 453 | ret false |
| 454 | end |
| 455 | vn = sub i vs |
| 456 | i = skipws add i 1 z |
| 457 | if lt i z |
| 458 | if ne get src i 35 |
| 459 | ret false |
| 460 | end |
| 461 | end |
| 462 | mkkey ks kn |
| 463 | tset t vs vn |
| 464 | ret true |
| 465 | end |
| 466 | |
| 467 | fn setsec s ulong j ulong to void |
| 468 | n = sub j s |
| 469 | if gt n 8191 |
| 470 | over |
| 471 | end |
| 472 | for i ulong n |
| 473 | put secb i get src add s add 1 i |
| 474 | end |
| 475 | put secb sub n 1 46 |
| 476 | put secl 0 n |
| 477 | end |
| 478 | |
| 479 | fn secline ls ulong z ulong to void |
| 480 | s = skipws ls z |
| 481 | if ge s z |
| 482 | ret |
| 483 | end |
| 484 | if eq get src s 35 |
| 485 | ret |
| 486 | end |
| 487 | e = z |
| 488 | while gt e s bound 999999 |
| 489 | if not isws get src sub e 1 |
| 490 | break |
| 491 | end |
| 492 | e = sub e 1 |
| 493 | end |
| 494 | if ne get src s 91 |
| 495 | ret |
| 496 | end |
| 497 | j = skipkc add s 1 e |
| 498 | if eq j add s 1 |
| 499 | ret |
| 500 | end |
| 501 | if ge j e |
| 502 | ret |
| 503 | end |
| 504 | if ne get src j 93 |
| 505 | ret |
| 506 | end |
| 507 | i = skipws add j 1 e |
| 508 | if lt i e |
| 509 | if ne get src i 35 |
| 510 | ret |
| 511 | end |
| 512 | end |
| 513 | setsec s j |
| 514 | end |
| 515 | |
| 516 | fn parse t ulong to void |
| 517 | put secl 0 0 |
| 518 | ls = ulong 0 |
| 519 | n = get srcn 0 |
| 520 | while lt ls n bound 999999 |
| 521 | z = ls |
| 522 | while and lt z n ne get src z 10 bound 999999 |
| 523 | z = add z 1 |
| 524 | end |
| 525 | if ge z n |
| 526 | break |
| 527 | end |
| 528 | if not kvline t ls z |
| 529 | secline ls z |
| 530 | end |
| 531 | ls = add z 1 |
| 532 | end |
| 533 | end |
| 534 | |
| 535 | note ---- files ---- |
| 536 | |
| 537 | fn slurpf fd int to void |
| 538 | t = ulong 0 |
| 539 | while true bound 999999 |
| 540 | room = sub CAP t |
| 541 | if eq room 0 |
| 542 | over |
| 543 | end |
| 544 | r = read fd src t room |
| 545 | if lt r 0 |
| 546 | break |
| 547 | end |
| 548 | if eq r 0 |
| 549 | break |
| 550 | end |
| 551 | t = add t ulong r |
| 552 | end |
| 553 | put srcn 0 t |
| 554 | end |
| 555 | |
| 556 | fn dropnul to void |
| 557 | n = get srcn 0 |
| 558 | w = ulong 0 |
| 559 | for i ulong n |
| 560 | c = get src i |
| 561 | if ne c 0 |
| 562 | put src w c |
| 563 | w = add w 1 |
| 564 | end |
| 565 | end |
| 566 | put srcn 0 w |
| 567 | end |
| 568 | |
| 569 | fn loadm to void |
| 570 | put srcn 0 0 |
| 571 | fd = fopen fpath 0 get fpl 0 0 0 |
| 572 | if lt fd 0 |
| 573 | ret |
| 574 | end |
| 575 | slurpf int fd |
| 576 | r = fclose int fd |
| 577 | end |
| 578 | |
| 579 | fn loada to void |
| 580 | put srcn 0 0 |
| 581 | fd = fopen apath 0 get apl 0 0 0 |
| 582 | if lt fd 0 |
| 583 | ret |
| 584 | end |
| 585 | slurpf int fd |
| 586 | r = fclose int fd |
| 587 | dropnul |
| 588 | end |
| 589 | |
| 590 | fn isregf to bool |
| 591 | r = stat fpath 0 get fpl 0 stb 0 128 |
| 592 | if lt r 0 |
| 593 | ret false |
| 594 | end |
| 595 | m = ulong get stb 16 |
| 596 | m = or m shl ulong get stb 17 8 |
| 597 | ret eq and m 61440 32768 |
| 598 | end |
| 599 | |
| 600 | fn isrega to bool |
| 601 | r = stat apath 0 get apl 0 stb 0 128 |
| 602 | if lt r 0 |
| 603 | ret false |
| 604 | end |
| 605 | m = ulong get stb 16 |
| 606 | m = or m shl ulong get stb 17 8 |
| 607 | ret eq and m 61440 32768 |
| 608 | end |
| 609 | |
| 610 | note ---- SHA-256 of src[0..srcn) into mfh (hex), FIPS 180-4 ---- |
| 611 | |
| 612 | fn initk to void |
| 613 | put skt 0 1116352408 |
| 614 | put skt 1 1899447441 |
| 615 | put skt 2 3049323471 |
| 616 | put skt 3 3921009573 |
| 617 | put skt 4 961987163 |
| 618 | put skt 5 1508970993 |
| 619 | put skt 6 2453635748 |
| 620 | put skt 7 2870763221 |
| 621 | put skt 8 3624381080 |
| 622 | put skt 9 310598401 |
| 623 | put skt 10 607225278 |
| 624 | put skt 11 1426881987 |
| 625 | put skt 12 1925078388 |
| 626 | put skt 13 2162078206 |
| 627 | put skt 14 2614888103 |
| 628 | put skt 15 3248222580 |
| 629 | put skt 16 3835390401 |
| 630 | put skt 17 4022224774 |
| 631 | put skt 18 264347078 |
| 632 | put skt 19 604807628 |
| 633 | put skt 20 770255983 |
| 634 | put skt 21 1249150122 |
| 635 | put skt 22 1555081692 |
| 636 | put skt 23 1996064986 |
| 637 | put skt 24 2554220882 |
| 638 | put skt 25 2821834349 |
| 639 | put skt 26 2952996808 |
| 640 | put skt 27 3210313671 |
| 641 | put skt 28 3336571891 |
| 642 | put skt 29 3584528711 |
| 643 | put skt 30 113926993 |
| 644 | put skt 31 338241895 |
| 645 | put skt 32 666307205 |
| 646 | put skt 33 773529912 |
| 647 | put skt 34 1294757372 |
| 648 | put skt 35 1396182291 |
| 649 | put skt 36 1695183700 |
| 650 | put skt 37 1986661051 |
| 651 | put skt 38 2177026350 |
| 652 | put skt 39 2456956037 |
| 653 | put skt 40 2730485921 |
| 654 | put skt 41 2820302411 |
| 655 | put skt 42 3259730800 |
| 656 | put skt 43 3345764771 |
| 657 | put skt 44 3516065817 |
| 658 | put skt 45 3600352804 |
| 659 | put skt 46 4094571909 |
| 660 | put skt 47 275423344 |
| 661 | put skt 48 430227734 |
| 662 | put skt 49 506948616 |
| 663 | put skt 50 659060556 |
| 664 | put skt 51 883997877 |
| 665 | put skt 52 958139571 |
| 666 | put skt 53 1322822218 |
| 667 | put skt 54 1537002063 |
| 668 | put skt 55 1747873779 |
| 669 | put skt 56 1955562222 |
| 670 | put skt 57 2024104815 |
| 671 | put skt 58 2227730452 |
| 672 | put skt 59 2361852424 |
| 673 | put skt 60 2428436474 |
| 674 | put skt 61 2756734187 |
| 675 | put skt 62 3204031479 |
| 676 | put skt 63 3329325298 |
| 677 | end |
| 678 | |
| 679 | fn inith to void |
| 680 | put shv 0 1779033703 |
| 681 | put shv 1 3144134277 |
| 682 | put shv 2 1013904242 |
| 683 | put shv 3 2773480762 |
| 684 | put shv 4 1359893119 |
| 685 | put shv 5 2600822924 |
| 686 | put shv 6 528734635 |
| 687 | put shv 7 1541459225 |
| 688 | end |
| 689 | |
| 690 | fn rotr x ulong r ulong to ulong |
| 691 | ret and or shr x r shl x sub 32 r MASK |
| 692 | end |
| 693 | |
| 694 | fn bsig0 x ulong to ulong |
| 695 | ret xor rotr x 2 xor rotr x 13 rotr x 22 |
| 696 | end |
| 697 | |
| 698 | fn bsig1 x ulong to ulong |
| 699 | ret xor rotr x 6 xor rotr x 11 rotr x 25 |
| 700 | end |
| 701 | |
| 702 | fn ssig0 x ulong to ulong |
| 703 | ret xor rotr x 7 xor rotr x 18 shr x 3 |
| 704 | end |
| 705 | |
| 706 | fn ssig1 x ulong to ulong |
| 707 | ret xor rotr x 17 xor rotr x 19 shr x 10 |
| 708 | end |
| 709 | |
| 710 | fn ch x ulong y ulong z ulong to ulong |
| 711 | ret xor and x y and and not x MASK z |
| 712 | end |
| 713 | |
| 714 | fn maj x ulong y ulong z ulong to ulong |
| 715 | ret xor and x y xor and x z and y z |
| 716 | end |
| 717 | |
| 718 | fn sched b ulong to void |
| 719 | for i ulong 16 |
| 720 | o = add b mul i 4 |
| 721 | v = ulong get src o |
| 722 | v = or shl v 8 ulong get src add o 1 |
| 723 | v = or shl v 8 ulong get src add o 2 |
| 724 | v = or shl v 8 ulong get src add o 3 |
| 725 | put swt i v |
| 726 | end |
| 727 | for j ulong 48 |
| 728 | i = add j 16 |
| 729 | s = add ssig1 get swt sub i 2 get swt sub i 7 |
| 730 | s = add s ssig0 get swt sub i 15 |
| 731 | s = add s get swt sub i 16 |
| 732 | put swt i and s MASK |
| 733 | end |
| 734 | end |
| 735 | |
| 736 | fn round i ulong to void |
| 737 | a = get sst 0 |
| 738 | e = get sst 4 |
| 739 | t1 = add get sst 7 bsig1 e |
| 740 | t1 = add t1 ch e get sst 5 get sst 6 |
| 741 | t1 = add t1 get skt i |
| 742 | t1 = and add t1 get swt i MASK |
| 743 | t2 = and add bsig0 a maj a get sst 1 get sst 2 MASK |
| 744 | put sst 7 get sst 6 |
| 745 | put sst 6 get sst 5 |
| 746 | put sst 5 e |
| 747 | put sst 4 and add get sst 3 t1 MASK |
| 748 | put sst 3 get sst 2 |
| 749 | put sst 2 get sst 1 |
| 750 | put sst 1 a |
| 751 | put sst 0 and add t1 t2 MASK |
| 752 | end |
| 753 | |
| 754 | fn block b ulong to void |
| 755 | sched b |
| 756 | for i ulong 8 |
| 757 | put sst i get shv i |
| 758 | end |
| 759 | for i ulong 64 |
| 760 | round i |
| 761 | end |
| 762 | for i ulong 8 |
| 763 | put shv i and add get shv i get sst i MASK |
| 764 | end |
| 765 | end |
| 766 | |
| 767 | fn pad to ulong |
| 768 | sz = get srcn 0 |
| 769 | put src sz 128 |
| 770 | p = add sz 1 |
| 771 | while ne rem p 64 56 bound 64 |
| 772 | put src p 0 |
| 773 | p = add p 1 |
| 774 | end |
| 775 | bits = mul sz 8 |
| 776 | for i ulong 8 |
| 777 | put src add p i uchar and shr bits mul sub 7 i 8 255 |
| 778 | end |
| 779 | ret add p 8 |
| 780 | end |
| 781 | |
| 782 | fn hexd v ulong to uchar |
| 783 | if lt v 10 |
| 784 | ret uchar add v 48 |
| 785 | end |
| 786 | ret uchar add v 87 |
| 787 | end |
| 788 | |
| 789 | fn sha256 to void |
| 790 | initk |
| 791 | inith |
| 792 | total = pad |
| 793 | b = ulong 0 |
| 794 | while lt b total bound 262145 |
| 795 | block b |
| 796 | b = add b 64 |
| 797 | end |
| 798 | for i ulong 8 |
| 799 | v = get shv i |
| 800 | for j ulong 8 |
| 801 | d = and shr v mul sub 7 j 4 15 |
| 802 | put mfh add mul i 8 j hexd d |
| 803 | end |
| 804 | end |
| 805 | end |
| 806 | |
| 807 | note ---- children: /bin/sh replays the environment from a memfd, then exec "$@" ---- |
| 808 | |
| 809 | fn pk0 to void |
| 810 | put pkn 0 2 |
| 811 | put pkc 0 0 |
| 812 | end |
| 813 | |
| 814 | fn pka to void |
| 815 | n = get pkn 0 |
| 816 | m = get argl 0 |
| 817 | if gt add n add 2 m 4096 |
| 818 | over |
| 819 | end |
| 820 | put pk n uchar rem m 256 |
| 821 | put pk add n 1 uchar div m 256 |
| 822 | for i ulong m |
| 823 | put pk add n add 2 i get arg i |
| 824 | end |
| 825 | put pkn 0 add n add 2 m |
| 826 | c = add get pkc 0 1 |
| 827 | put pkc 0 c |
| 828 | put pk 0 uchar rem c 256 |
| 829 | put pk 1 uchar div c 256 |
| 830 | end |
| 831 | |
| 832 | fn pkt i ulong to void |
| 833 | kp i |
| 834 | pka |
| 835 | end |
| 836 | |
| 837 | fn pksh to void |
| 838 | pk0 |
| 839 | pkt 33 |
| 840 | pkt 34 |
| 841 | for i ulong len tsh |
| 842 | put arg i get tsh i |
| 843 | end |
| 844 | put argl 0 len tsh |
| 845 | pka |
| 846 | pkt 35 |
| 847 | for i ulong get epl 0 |
| 848 | put arg i get epath i |
| 849 | end |
| 850 | put argl 0 get epl 0 |
| 851 | pka |
| 852 | end |
| 853 | |
| 854 | fn envname l ulong to bool |
| 855 | if eq l 0 |
| 856 | ret false |
| 857 | end |
| 858 | for i ulong l |
| 859 | c = get ebuf i |
| 860 | ok = or and ge c 65 le c 90 and ge c 97 le c 122 |
| 861 | ok = or ok eq c 95 |
| 862 | if gt i 0 |
| 863 | ok = or ok and ge c 48 le c 57 |
| 864 | end |
| 865 | if not ok |
| 866 | ret false |
| 867 | end |
| 868 | end |
| 869 | ret true |
| 870 | end |
| 871 | |
| 872 | fn envone l ulong to void |
| 873 | e = ulong 0 |
| 874 | while and lt e l ne get ebuf e 61 bound 65536 |
| 875 | e = add e 1 |
| 876 | end |
| 877 | if eq e l |
| 878 | ret |
| 879 | end |
| 880 | if not envname e |
| 881 | ret |
| 882 | end |
| 883 | for i ulong e |
| 884 | put ewb i get ebuf i |
| 885 | end |
| 886 | n = e |
| 887 | put ewb n 61 |
| 888 | put ewb add n 1 39 |
| 889 | n = add n 2 |
| 890 | i = add e 1 |
| 891 | while lt i l bound 65536 |
| 892 | c = get ebuf i |
| 893 | if eq c 39 |
| 894 | put ewb n 39 |
| 895 | put ewb add n 1 92 |
| 896 | put ewb add n 2 39 |
| 897 | put ewb add n 3 39 |
| 898 | n = add n 4 |
| 899 | else |
| 900 | put ewb n c |
| 901 | n = add n 1 |
| 902 | end |
| 903 | i = add i 1 |
| 904 | end |
| 905 | put ewb n 39 |
| 906 | put ewb add n 1 10 |
| 907 | n = add n 2 |
| 908 | r = write int get efd 0 ewb 0 n |
| 909 | end |
| 910 | |
| 911 | fn envinit to void |
| 912 | kp 45 |
| 913 | f = memfd arg 0 get argl 0 0 |
| 914 | if lt f 0 |
| 915 | over |
| 916 | end |
| 917 | put efd 0 f |
| 918 | for i ulong ulong envcnt |
| 919 | l = envread i ebuf 0 65536 |
| 920 | if gt l 0 |
| 921 | envone ulong l |
| 922 | end |
| 923 | end |
| 924 | kp 36 |
| 925 | n = get argl 0 |
| 926 | for i ulong n |
| 927 | put epath i get arg i |
| 928 | end |
| 929 | v = ulong f |
| 930 | d = ulong 0 |
| 931 | while gt v 0 bound 24 |
| 932 | put nbuf d uchar rem v 10 |
| 933 | v = div v 10 |
| 934 | d = add d 1 |
| 935 | end |
| 936 | if eq d 0 |
| 937 | put nbuf 0 0 |
| 938 | d = ulong 1 |
| 939 | end |
| 940 | for i ulong d |
| 941 | put epath add n i add 48 get nbuf sub sub d 1 i |
| 942 | end |
| 943 | put epl 0 add n d |
| 944 | end |
| 945 | |
| 946 | note the value of the environment variable named by arg, into ev; -1 when unset |
| 947 | fn getenv to long |
| 948 | n = get argl 0 |
| 949 | for i ulong ulong envcnt |
| 950 | l = envread i ebuf 0 65536 |
| 951 | if gt l long n |
| 952 | if envpre n |
| 953 | ret envtake n ulong l |
| 954 | end |
| 955 | end |
| 956 | end |
| 957 | ret -1 |
| 958 | end |
| 959 | |
| 960 | fn envtake n ulong l ulong to long |
| 961 | m = sub l add n 1 |
| 962 | o = add n 1 |
| 963 | for j ulong m |
| 964 | put ev j get ebuf add o j |
| 965 | end |
| 966 | put evl 0 m |
| 967 | ret long m |
| 968 | end |
| 969 | |
| 970 | fn envpre n ulong to bool |
| 971 | for i ulong n |
| 972 | if ne get ebuf i get arg i |
| 973 | ret false |
| 974 | end |
| 975 | end |
| 976 | ret eq get ebuf n 61 |
| 977 | end |
| 978 | |
| 979 | note split ev on space, tab and newline, one packet record per word |
| 980 | fn pkwords to void |
| 981 | i = ulong 0 |
| 982 | n = get evl 0 |
| 983 | while lt i n bound 65536 |
| 984 | c = get ev i |
| 985 | if or eq c 32 or eq c 9 eq c 10 |
| 986 | i = add i 1 |
| 987 | else |
| 988 | w = ulong 0 |
| 989 | while and lt i n wordc get ev i bound 65536 |
| 990 | put arg w get ev i |
| 991 | w = add w 1 |
| 992 | i = add i 1 |
| 993 | end |
| 994 | put argl 0 w |
| 995 | pka |
| 996 | end |
| 997 | end |
| 998 | end |
| 999 | |
| 1000 | fn wordc c uchar to bool |
| 1001 | ret not or eq c 32 or eq c 9 eq c 10 |
| 1002 | end |
| 1003 | |
| 1004 | fn child to void |
| 1005 | if eq get rmode 0 1 |
| 1006 | kp 43 |
| 1007 | fd = fopen arg 0 get argl 0 1 0 |
| 1008 | if lt fd 0 |
| 1009 | exit 126 |
| 1010 | end |
| 1011 | r = dupfd int fd 1 0 |
| 1012 | r = dupfd int fd 2 0 |
| 1013 | end |
| 1014 | r = execprog pk 0 get pkn 0 |
| 1015 | exit 127 |
| 1016 | end |
| 1017 | |
| 1018 | fn run to long |
| 1019 | pid = fork |
| 1020 | if lt pid 0 |
| 1021 | ret -1 |
| 1022 | end |
| 1023 | if eq pid 0 |
| 1024 | child |
| 1025 | end |
| 1026 | r = waitpid int pid status 0 4 0 |
| 1027 | if ne r pid |
| 1028 | ret -1 |
| 1029 | end |
| 1030 | s = long get status 0 |
| 1031 | s = or s shl long get status 1 8 |
| 1032 | if eq and s 127 0 |
| 1033 | ret shr s 8 |
| 1034 | end |
| 1035 | ret add 128 and s 127 |
| 1036 | end |
| 1037 | |
| 1038 | note ---- value helpers ---- |
| 1039 | |
| 1040 | fn vlen e long to ulong |
| 1041 | if lt e 0 |
| 1042 | ret 0 |
| 1043 | end |
| 1044 | ret get vln ulong e |
| 1045 | end |
| 1046 | |
| 1047 | fn veq e long i ulong to bool |
| 1048 | if lt e 0 |
| 1049 | ret false |
| 1050 | end |
| 1051 | kp i |
| 1052 | x = ulong e |
| 1053 | if ne get vln x get argl 0 |
| 1054 | ret false |
| 1055 | end |
| 1056 | o = get vof x |
| 1057 | for j ulong get argl 0 |
| 1058 | if ne get pool add o j get arg j |
| 1059 | ret false |
| 1060 | end |
| 1061 | end |
| 1062 | ret true |
| 1063 | end |
| 1064 | |
| 1065 | fn vpre e long i ulong to bool |
| 1066 | if lt e 0 |
| 1067 | ret false |
| 1068 | end |
| 1069 | kp i |
| 1070 | x = ulong e |
| 1071 | if lt get vln x get argl 0 |
| 1072 | ret false |
| 1073 | end |
| 1074 | o = get vof x |
| 1075 | for j ulong get argl 0 |
| 1076 | if ne get pool add o j get arg j |
| 1077 | ret false |
| 1078 | end |
| 1079 | end |
| 1080 | ret true |
| 1081 | end |
| 1082 | |
| 1083 | fn vveq e long f long to bool |
| 1084 | n = vlen e |
| 1085 | if ne n vlen f |
| 1086 | ret false |
| 1087 | end |
| 1088 | if lt e 0 |
| 1089 | ret true |
| 1090 | end |
| 1091 | o = get vof ulong e |
| 1092 | p = get vof ulong f |
| 1093 | for j ulong n |
| 1094 | if ne get pool add o j get pool add p j |
| 1095 | ret false |
| 1096 | end |
| 1097 | end |
| 1098 | ret true |
| 1099 | end |
| 1100 | |
| 1101 | fn vnum e long to bool |
| 1102 | n = vlen e |
| 1103 | if ne n get anml 0 |
| 1104 | ret false |
| 1105 | end |
| 1106 | if lt e 0 |
| 1107 | ret true |
| 1108 | end |
| 1109 | o = get vof ulong e |
| 1110 | for j ulong n |
| 1111 | if ne get pool add o j get anum j |
| 1112 | ret false |
| 1113 | end |
| 1114 | end |
| 1115 | ret true |
| 1116 | end |
| 1117 | |
| 1118 | fn mfeq e long to bool |
| 1119 | if ne vlen e 64 |
| 1120 | ret false |
| 1121 | end |
| 1122 | o = get vof ulong e |
| 1123 | for j ulong 64 |
| 1124 | if ne get pool add o j get mfh j |
| 1125 | ret false |
| 1126 | end |
| 1127 | end |
| 1128 | ret true |
| 1129 | end |
| 1130 | |
| 1131 | fn ishex c uchar to bool |
| 1132 | ret or and ge c 48 le c 57 and ge c 97 le c 102 |
| 1133 | end |
| 1134 | |
| 1135 | fn hexn e long n ulong to bool |
| 1136 | if ne vlen e n |
| 1137 | ret false |
| 1138 | end |
| 1139 | o = get vof ulong e |
| 1140 | for j ulong n |
| 1141 | if not ishex get pool add o j |
| 1142 | ret false |
| 1143 | end |
| 1144 | end |
| 1145 | ret true |
| 1146 | end |
| 1147 | |
| 1148 | fn zero64 e long to bool |
| 1149 | if ne vlen e 64 |
| 1150 | ret false |
| 1151 | end |
| 1152 | o = get vof ulong e |
| 1153 | for j ulong 64 |
| 1154 | if ne get pool add o j 48 |
| 1155 | ret false |
| 1156 | end |
| 1157 | end |
| 1158 | ret true |
| 1159 | end |
| 1160 | |
| 1161 | fn kfind t ulong i ulong to long |
| 1162 | kp i |
| 1163 | ret tfind t |
| 1164 | end |
| 1165 | |
| 1166 | note ---- key helpers (x is an entry index) ---- |
| 1167 | |
| 1168 | fn keyis x ulong i ulong to bool |
| 1169 | kp i |
| 1170 | ret keyeq x |
| 1171 | end |
| 1172 | |
| 1173 | fn haspre x ulong i ulong to bool |
| 1174 | kp i |
| 1175 | n = get argl 0 |
| 1176 | if lt get kln x n |
| 1177 | ret false |
| 1178 | end |
| 1179 | o = get kof x |
| 1180 | for j ulong n |
| 1181 | if ne get pool add o j get arg j |
| 1182 | ret false |
| 1183 | end |
| 1184 | end |
| 1185 | ret true |
| 1186 | end |
| 1187 | |
| 1188 | fn hassuf x ulong i ulong to bool |
| 1189 | kp i |
| 1190 | n = get argl 0 |
| 1191 | m = get kln x |
| 1192 | if lt m n |
| 1193 | ret false |
| 1194 | end |
| 1195 | o = add get kof x sub m n |
| 1196 | for j ulong n |
| 1197 | if ne get pool add o j get arg j |
| 1198 | ret false |
| 1199 | end |
| 1200 | end |
| 1201 | ret true |
| 1202 | end |
| 1203 | |
| 1204 | fn hasdot x ulong to bool |
| 1205 | o = get kof x |
| 1206 | for j ulong get kln x |
| 1207 | if eq get pool add o j 46 |
| 1208 | ret true |
| 1209 | end |
| 1210 | end |
| 1211 | ret false |
| 1212 | end |
| 1213 | |
| 1214 | fn inmf x ulong to bool |
| 1215 | o = get kof x |
| 1216 | n = get kln x |
| 1217 | for j ulong n |
| 1218 | put arg j get pool add o j |
| 1219 | end |
| 1220 | put argl 0 n |
| 1221 | ret ge tfind 0 0 |
| 1222 | end |
| 1223 | |
| 1224 | note ---- commit pins ---- |
| 1225 | |
| 1226 | note rp = the repo of key-table entry i (the part after its first dot); the key name aienos |
| 1227 | note names the kernel repository, renamed to aienux, so rp spells aienux for it |
| 1228 | fn kprepo i ulong to void |
| 1229 | kp i |
| 1230 | n = get argl 0 |
| 1231 | d = ulong 0 |
| 1232 | while and lt d n ne get arg d 46 bound 8192 |
| 1233 | d = add d 1 |
| 1234 | end |
| 1235 | d = add d 1 |
| 1236 | m = ulong 0 |
| 1237 | if lt d n |
| 1238 | m = sub n d |
| 1239 | end |
| 1240 | for j ulong m |
| 1241 | put rp j get arg add d j |
| 1242 | end |
| 1243 | if eq m 6 |
| 1244 | if and eq get rp 0 97 eq get rp 1 105 |
| 1245 | if and eq get rp 2 101 eq get rp 3 110 |
| 1246 | if and eq get rp 4 111 eq get rp 5 115 |
| 1247 | put rp 4 117 |
| 1248 | put rp 5 120 |
| 1249 | end |
| 1250 | end |
| 1251 | end |
| 1252 | end |
| 1253 | put rpl 0 m |
| 1254 | end |
| 1255 | |
| 1256 | fn arp to void |
| 1257 | n = get argl 0 |
| 1258 | for j ulong get rpl 0 |
| 1259 | put arg add n j get rp j |
| 1260 | end |
| 1261 | put argl 0 add n get rpl 0 |
| 1262 | end |
| 1263 | |
| 1264 | fn orp to void |
| 1265 | b = get obl 0 |
| 1266 | for j ulong get rpl 0 |
| 1267 | put obuf add b j get rp j |
| 1268 | end |
| 1269 | put obl 0 add b get rpl 0 |
| 1270 | end |
| 1271 | |
| 1272 | fn kpapp i ulong to void |
| 1273 | s = ulong 0 |
| 1274 | k = ulong 0 |
| 1275 | while lt k i bound 8192 |
| 1276 | if eq get tk s 10 |
| 1277 | k = add k 1 |
| 1278 | end |
| 1279 | s = add s 1 |
| 1280 | end |
| 1281 | n = get argl 0 |
| 1282 | while ne get tk s 10 bound 8191 |
| 1283 | put arg n get tk s |
| 1284 | n = add n 1 |
| 1285 | s = add s 1 |
| 1286 | end |
| 1287 | put argl 0 n |
| 1288 | end |
| 1289 | |
| 1290 | fn avapp e long to void |
| 1291 | x = ulong e |
| 1292 | o = get vof x |
| 1293 | n = get vln x |
| 1294 | b = get argl 0 |
| 1295 | if gt add b n 8191 |
| 1296 | over |
| 1297 | end |
| 1298 | for j ulong n |
| 1299 | put arg add b j get pool add o j |
| 1300 | end |
| 1301 | put argl 0 add b n |
| 1302 | end |
| 1303 | |
| 1304 | fn shaex i ulong e long to bool |
| 1305 | pksh |
| 1306 | kp 32 |
| 1307 | l = getenv |
| 1308 | kprepo i |
| 1309 | if gt l 0 |
| 1310 | pkwords |
| 1311 | put argl 0 0 |
| 1312 | arp |
| 1313 | pka |
| 1314 | put argl 0 0 |
| 1315 | avapp e |
| 1316 | pka |
| 1317 | put rmode 0 0 |
| 1318 | else |
| 1319 | pkt 37 |
| 1320 | pkt 38 |
| 1321 | kp 39 |
| 1322 | arp |
| 1323 | kpapp 40 |
| 1324 | avapp e |
| 1325 | pka |
| 1326 | pkt 41 |
| 1327 | pkt 42 |
| 1328 | put rmode 0 1 |
| 1329 | end |
| 1330 | ret eq run 0 |
| 1331 | end |
| 1332 | |
| 1333 | fn fnot40 i ulong e long to void |
| 1334 | fail0 |
| 1335 | om 7 |
| 1336 | kp i |
| 1337 | oarg |
| 1338 | om 8 |
| 1339 | oval e |
| 1340 | fail1 |
| 1341 | end |
| 1342 | |
| 1343 | fn fnotfound i ulong e long to void |
| 1344 | fail0 |
| 1345 | om 9 |
| 1346 | oval e |
| 1347 | om 10 |
| 1348 | kprepo i |
| 1349 | orp |
| 1350 | fail1 |
| 1351 | end |
| 1352 | |
| 1353 | fn reqpin i ulong to void |
| 1354 | e = kfind 0 i |
| 1355 | if eq vlen e 0 |
| 1356 | fail0 |
| 1357 | om 6 |
| 1358 | kp i |
| 1359 | oarg |
| 1360 | fail1 |
| 1361 | ret |
| 1362 | end |
| 1363 | if not hexn e 40 |
| 1364 | fnot40 i e |
| 1365 | ret |
| 1366 | end |
| 1367 | if not shaex i e |
| 1368 | fnotfound i e |
| 1369 | end |
| 1370 | end |
| 1371 | |
| 1372 | fn optpin i ulong to void |
| 1373 | e = kfind 0 i |
| 1374 | if eq vlen e 0 |
| 1375 | ret |
| 1376 | end |
| 1377 | if not hexn e 40 |
| 1378 | fnot40 i e |
| 1379 | ret |
| 1380 | end |
| 1381 | if not shaex i e |
| 1382 | fnotfound i e |
| 1383 | end |
| 1384 | end |
| 1385 | |
| 1386 | fn reqpins to void |
| 1387 | for i ulong NREQ |
| 1388 | reqpin add 4 i |
| 1389 | end |
| 1390 | end |
| 1391 | |
| 1392 | fn optpins to void |
| 1393 | for i ulong NOPT |
| 1394 | optpin add 11 i |
| 1395 | end |
| 1396 | end |
| 1397 | |
| 1398 | note ---- [executables] ---- |
| 1399 | |
| 1400 | fn exe1 x ulong to void |
| 1401 | e = long x |
| 1402 | if veq e 18 |
| 1403 | if not vpre kfind 0 2 19 |
| 1404 | fail0 |
| 1405 | okey x |
| 1406 | om 11 |
| 1407 | fail1 |
| 1408 | end |
| 1409 | ret |
| 1410 | end |
| 1411 | if not hexn e 64 |
| 1412 | fail0 |
| 1413 | okey x |
| 1414 | om 12 |
| 1415 | fail1 |
| 1416 | end |
| 1417 | z = zero64 e |
| 1418 | if z |
| 1419 | fail0 |
| 1420 | okey x |
| 1421 | om 13 |
| 1422 | fail1 |
| 1423 | end |
| 1424 | end |
| 1425 | |
| 1426 | fn exes to void |
| 1427 | c = ulong 0 |
| 1428 | for b ulong get nbk 0 |
| 1429 | e = get kbk b |
| 1430 | while ne e 0 bound 8192 |
| 1431 | x = sub e 1 |
| 1432 | if haspre x 17 |
| 1433 | c = add c 1 |
| 1434 | exe1 x |
| 1435 | end |
| 1436 | e = get knx x |
| 1437 | end |
| 1438 | end |
| 1439 | if eq c 0 |
| 1440 | fail0 |
| 1441 | om 14 |
| 1442 | fail1 |
| 1443 | end |
| 1444 | end |
| 1445 | |
| 1446 | note ---- amendments ---- |
| 1447 | |
| 1448 | fn splitdir to void |
| 1449 | n = get fpl 0 |
| 1450 | d = ulong 0 |
| 1451 | for i ulong n |
| 1452 | if eq get fpath i 47 |
| 1453 | d = add i 1 |
| 1454 | end |
| 1455 | end |
| 1456 | for i ulong d |
| 1457 | put dirp i get fpath i |
| 1458 | end |
| 1459 | put dirl 0 d |
| 1460 | m = sub n d |
| 1461 | kp 31 |
| 1462 | if ge m 5 |
| 1463 | if sufeq sub n 5 |
| 1464 | m = sub m 5 |
| 1465 | end |
| 1466 | end |
| 1467 | kp 30 |
| 1468 | if gt add m get argl 0 4095 |
| 1469 | over |
| 1470 | end |
| 1471 | for i ulong m |
| 1472 | put apre i get fpath add d i |
| 1473 | end |
| 1474 | for i ulong get argl 0 |
| 1475 | put apre add m i get arg i |
| 1476 | end |
| 1477 | put aprl 0 add m get argl 0 |
| 1478 | end |
| 1479 | |
| 1480 | note does fpath[p..p+5) equal arg (".toml") |
| 1481 | fn sufeq p ulong to bool |
| 1482 | for i ulong 5 |
| 1483 | if ne get fpath add p i get arg i |
| 1484 | ret false |
| 1485 | end |
| 1486 | end |
| 1487 | ret true |
| 1488 | end |
| 1489 | |
| 1490 | note one directory entry name at dbuf[o..), NUL-terminated: keep it when it matches <apre>*.toml |
| 1491 | fn dname o ulong to void |
| 1492 | n = ulong 0 |
| 1493 | while ne get dbuf add o n 0 bound 4096 |
| 1494 | n = add n 1 |
| 1495 | end |
| 1496 | if lt n add get aprl 0 5 |
| 1497 | ret |
| 1498 | end |
| 1499 | for i ulong get aprl 0 |
| 1500 | if ne get dbuf add o i get apre i |
| 1501 | ret |
| 1502 | end |
| 1503 | end |
| 1504 | kp 31 |
| 1505 | for i ulong 5 |
| 1506 | if ne get dbuf add o sub add n i 5 get arg i |
| 1507 | ret |
| 1508 | end |
| 1509 | end |
| 1510 | akeep o n |
| 1511 | end |
| 1512 | |
| 1513 | fn akeep o ulong n ulong to void |
| 1514 | c = get acnt 0 |
| 1515 | p = get apn 0 |
| 1516 | if or ge c 1024 gt add p n len apool |
| 1517 | over |
| 1518 | end |
| 1519 | for i ulong n |
| 1520 | put apool add p i get dbuf add o i |
| 1521 | end |
| 1522 | put ano c p |
| 1523 | put anl c n |
| 1524 | put aix c c |
| 1525 | put apn 0 add p n |
| 1526 | put acnt 0 add c 1 |
| 1527 | end |
| 1528 | |
| 1529 | fn listdir to void |
| 1530 | put acnt 0 0 |
| 1531 | put apn 0 0 |
| 1532 | fd = long 0 |
| 1533 | if eq get dirl 0 0 |
| 1534 | kp 44 |
| 1535 | fd = fopen arg 0 1 0 0 |
| 1536 | else |
| 1537 | fd = fopen dirp 0 get dirl 0 0 0 |
| 1538 | end |
| 1539 | if lt fd 0 |
| 1540 | ret |
| 1541 | end |
| 1542 | while true bound 999999 |
| 1543 | n = getdnts int fd dbuf 0 65536 |
| 1544 | if le n 0 |
| 1545 | break |
| 1546 | end |
| 1547 | p = ulong 0 |
| 1548 | while lt p ulong n bound 65536 |
| 1549 | rl = ulong get dbuf add p 16 |
| 1550 | rl = or rl shl ulong get dbuf add p 17 8 |
| 1551 | dname add p 19 |
| 1552 | p = add p rl |
| 1553 | end |
| 1554 | end |
| 1555 | r = fclose int fd |
| 1556 | end |
| 1557 | |
| 1558 | note byte order of two kept names |
| 1559 | fn aless a ulong b ulong to bool |
| 1560 | n = get anl a |
| 1561 | m = get anl b |
| 1562 | l = n |
| 1563 | if lt m n |
| 1564 | l = m |
| 1565 | end |
| 1566 | for i ulong l |
| 1567 | x = get apool add get ano a i |
| 1568 | y = get apool add get ano b i |
| 1569 | if ne x y |
| 1570 | ret lt x y |
| 1571 | end |
| 1572 | end |
| 1573 | ret lt n m |
| 1574 | end |
| 1575 | |
| 1576 | fn asort to void |
| 1577 | c = get acnt 0 |
| 1578 | for i ulong c |
| 1579 | j = i |
| 1580 | while gt j 0 bound 1024 |
| 1581 | a = get aix sub j 1 |
| 1582 | b = get aix j |
| 1583 | if not aless b a |
| 1584 | break |
| 1585 | end |
| 1586 | put aix sub j 1 b |
| 1587 | put aix j a |
| 1588 | j = sub j 1 |
| 1589 | end |
| 1590 | end |
| 1591 | end |
| 1592 | |
| 1593 | note apath = dirp + name k; anum = the <n> between the prefix and .toml |
| 1594 | fn setapath k ulong to void |
| 1595 | d = get dirl 0 |
| 1596 | o = get ano k |
| 1597 | n = get anl k |
| 1598 | if gt add d n 8191 |
| 1599 | over |
| 1600 | end |
| 1601 | for i ulong d |
| 1602 | put apath i get dirp i |
| 1603 | end |
| 1604 | for i ulong n |
| 1605 | put apath add d i get apool add o i |
| 1606 | end |
| 1607 | put apl 0 add d n |
| 1608 | p = get aprl 0 |
| 1609 | m = sub sub n p 5 |
| 1610 | for i ulong m |
| 1611 | put anum i get apool add o add p i |
| 1612 | end |
| 1613 | put anml 0 m |
| 1614 | end |
| 1615 | |
| 1616 | fn amkey x ulong to bool |
| 1617 | if isfixed x |
| 1618 | ret false |
| 1619 | end |
| 1620 | if not hasdot x |
| 1621 | afail 27 |
| 1622 | okey x |
| 1623 | om 18 |
| 1624 | fail1 |
| 1625 | ret false |
| 1626 | end |
| 1627 | if isdefk x |
| 1628 | afail 35 |
| 1629 | okey x |
| 1630 | om 28 |
| 1631 | fail1 |
| 1632 | ret false |
| 1633 | end |
| 1634 | if inmf x |
| 1635 | afail 35 |
| 1636 | okey x |
| 1637 | om 29 |
| 1638 | fail1 |
| 1639 | end |
| 1640 | if hassuf x 29 |
| 1641 | if not realsha x |
| 1642 | afail 35 |
| 1643 | okey x |
| 1644 | om 30 |
| 1645 | fail1 |
| 1646 | end |
| 1647 | end |
| 1648 | ret true |
| 1649 | end |
| 1650 | |
| 1651 | fn isfixed x ulong to bool |
| 1652 | f = or keyis x 0 keyis x 21 |
| 1653 | f = or f or keyis x 22 keyis x 23 |
| 1654 | ret or f or keyis x 24 keyis x 25 |
| 1655 | end |
| 1656 | |
| 1657 | fn isdefk x ulong to bool |
| 1658 | f = or haspre x 26 haspre x 27 |
| 1659 | ret or f or haspre x 17 haspre x 28 |
| 1660 | end |
| 1661 | |
| 1662 | fn realsha x ulong to bool |
| 1663 | e = long x |
| 1664 | if not hexn e 64 |
| 1665 | ret false |
| 1666 | end |
| 1667 | ret not zero64 e |
| 1668 | end |
| 1669 | |
| 1670 | fn amkeys to ulong |
| 1671 | c = ulong 0 |
| 1672 | for b ulong get nbk 1 |
| 1673 | e = get kbk add 4096 b |
| 1674 | while ne e 0 bound 8192 |
| 1675 | x = sub e 1 |
| 1676 | if amkey x |
| 1677 | c = add c 1 |
| 1678 | end |
| 1679 | e = get knx x |
| 1680 | end |
| 1681 | end |
| 1682 | ret c |
| 1683 | end |
| 1684 | |
| 1685 | fn amhead to void |
| 1686 | e = kfind 1 0 |
| 1687 | if not veq e 20 |
| 1688 | afail 15 |
| 1689 | fail1 |
| 1690 | end |
| 1691 | e = kfind 1 21 |
| 1692 | f = kfind 0 1 |
| 1693 | if not vveq e f |
| 1694 | afail 16 |
| 1695 | oval e |
| 1696 | om 17 |
| 1697 | oval f |
| 1698 | om 18 |
| 1699 | fail1 |
| 1700 | end |
| 1701 | e = kfind 1 22 |
| 1702 | if not vnum e |
| 1703 | afail 19 |
| 1704 | oval e |
| 1705 | om 20 |
| 1706 | oanum |
| 1707 | om 21 |
| 1708 | fail1 |
| 1709 | end |
| 1710 | e = kfind 1 23 |
| 1711 | f = kfind 1 24 |
| 1712 | if or eq vlen e 0 eq vlen f 0 |
| 1713 | afail 22 |
| 1714 | fail1 |
| 1715 | end |
| 1716 | e = kfind 1 25 |
| 1717 | if not mfeq e |
| 1718 | afail 23 |
| 1719 | oval e |
| 1720 | om 24 |
| 1721 | ofile |
| 1722 | om 25 |
| 1723 | omfh |
| 1724 | om 26 |
| 1725 | fail1 |
| 1726 | end |
| 1727 | end |
| 1728 | |
| 1729 | fn amend1 k ulong to void |
| 1730 | setapath k |
| 1731 | if not isrega |
| 1732 | ret |
| 1733 | end |
| 1734 | treset 1 |
| 1735 | put pooln 0 get poolm 0 |
| 1736 | loada |
| 1737 | parse 1 |
| 1738 | amhead |
| 1739 | nk = amkeys |
| 1740 | if eq nk 0 |
| 1741 | afail 31 |
| 1742 | fail1 |
| 1743 | end |
| 1744 | put namend 0 add get namend 0 1 |
| 1745 | end |
| 1746 | |
| 1747 | fn amends to void |
| 1748 | splitdir |
| 1749 | listdir |
| 1750 | asort |
| 1751 | for i ulong get acnt 0 |
| 1752 | amend1 get aix i |
| 1753 | end |
| 1754 | end |
| 1755 | |
| 1756 | note ---- main ---- |
| 1757 | |
| 1758 | fn usage to int |
| 1759 | put obl 0 0 |
| 1760 | om 0 |
| 1761 | b = get obl 0 |
| 1762 | for i ulong get a0l 0 |
| 1763 | put obuf add b i get a0 i |
| 1764 | end |
| 1765 | put obl 0 add b get a0l 0 |
| 1766 | om 1 |
| 1767 | oc 10 |
| 1768 | oflush 2 |
| 1769 | ret 1 |
| 1770 | end |
| 1771 | |
| 1772 | fn main to int |
| 1773 | l = argread 0 a0 0 4095 |
| 1774 | if lt l 0 |
| 1775 | l = long 0 |
| 1776 | end |
| 1777 | put a0l 0 ulong l |
| 1778 | if lt argcnt 2 |
| 1779 | ret usage |
| 1780 | end |
| 1781 | l = argread 1 fpath 0 4095 |
| 1782 | if lt l 0 |
| 1783 | l = long 0 |
| 1784 | end |
| 1785 | put fpl 0 ulong l |
| 1786 | if not isregf |
| 1787 | ret usage |
| 1788 | end |
| 1789 | envinit |
| 1790 | treset 0 |
| 1791 | treset 1 |
| 1792 | put pooln 0 0 |
| 1793 | put failf 0 0 |
| 1794 | put namend 0 0 |
| 1795 | loadm |
| 1796 | note Bind amendments to the raw frozen file, before parser-only NUL removal. |
| 1797 | sha256 |
| 1798 | dropnul |
| 1799 | parse 0 |
| 1800 | put poolm 0 get pooln 0 |
| 1801 | for i ulong 4 |
| 1802 | if eq vlen kfind 0 i 0 |
| 1803 | fail0 |
| 1804 | om 3 |
| 1805 | kp i |
| 1806 | oarg |
| 1807 | fail1 |
| 1808 | end |
| 1809 | end |
| 1810 | if not veq kfind 0 0 13 |
| 1811 | fail0 |
| 1812 | om 4 |
| 1813 | fail1 |
| 1814 | end |
| 1815 | e = kfind 0 2 |
| 1816 | s = or veq e 14 veq e 15 |
| 1817 | if not or s veq e 16 |
| 1818 | fail0 |
| 1819 | om 5 |
| 1820 | fail1 |
| 1821 | end |
| 1822 | reqpins |
| 1823 | optpins |
| 1824 | exes |
| 1825 | amends |
| 1826 | if ne get failf 0 0 |
| 1827 | ret 1 |
| 1828 | end |
| 1829 | put obl 0 0 |
| 1830 | om 32 |
| 1831 | ofile |
| 1832 | om 33 |
| 1833 | onum get namend 0 |
| 1834 | om 34 |
| 1835 | oc 10 |
| 1836 | oflush 1 |
| 1837 | ret 0 |
| 1838 | end |