interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / forge / forge.om
| 1 | om2 |
| 2 | note INTERPLANE Forge v1: agents express intent, the forge retains authority. |
| 3 | note forge decide [--json] HEAD VERDICT RECEIPT prints "MERGE OK" and exits 0, or "HOLD <CODE>" and |
| 4 | note exits 1 (with --json, one forge-decision/1 JSON object instead). |
| 5 | note forge status RID [REVIEWS [RECEIPTS]] lists the live Radicle patches of RID and decides each |
| 6 | note patch that has a verdict file. |
| 7 | note forge gate RID HEAD [--name N] [--out DIR] [--timeout S] --author A --neg ARGV.. -- ARGV.. runs a |
| 8 | note gate and its negative control, each in its own fresh clone at the exact head and process group, |
| 9 | note each killed with its group after S seconds (default 1800), and writes the gate receipt. |
| 10 | note Spec: docs/FORGE.md. Formats: forge/README.md. Every check fails closed. |
| 11 | global m uchar 1048576 |
| 12 | global g ulong 64 |
| 13 | global hs uint 8 |
| 14 | global hw uint 64 |
| 15 | global hk uint 64 |
| 16 | global hv uint 8 |
| 17 | global hb uchar 64 |
| 18 | note memory map of m: verdict, receipt, patch head, path, output line, exec packet, |
| 19 | note directory records, command output, patch ids, environment, arguments, patch fields |
| 20 | const kv ulong 0 |
| 21 | const vcap ulong 65536 |
| 22 | const kr ulong 65536 |
| 23 | const rcap ulong 4096 |
| 24 | const kh ulong 69632 |
| 25 | const kp ulong 73728 |
| 26 | const pcap ulong 4096 |
| 27 | const kw ulong 77824 |
| 28 | const wcap ulong 8192 |
| 29 | const ke ulong 720896 |
| 30 | const ecap ulong 4096 |
| 31 | const kd ulong 90112 |
| 32 | const dcap ulong 32768 |
| 33 | const ko ulong 131072 |
| 34 | const ocap ulong 524288 |
| 35 | const ki ulong 655360 |
| 36 | const icap ulong 512 |
| 37 | const kx ulong 659456 |
| 38 | const krid ulong 671744 |
| 39 | const krev ulong 672000 |
| 40 | const krcp ulong 676096 |
| 41 | const ks ulong 680192 |
| 42 | const kn ulong 684288 |
| 43 | const kn2 ulong 684800 |
| 44 | const kag ulong 688128 |
| 45 | const acap ulong 4096 |
| 46 | const kt ulong 692224 |
| 47 | const kc ulong 692736 |
| 48 | const knm ulong 693760 |
| 49 | const kod ulong 693888 |
| 50 | const kau ulong 695936 |
| 51 | const kst ulong 696448 |
| 52 | const kom ulong 698496 |
| 53 | const krh ulong 700544 |
| 54 | const ktr ulong 702592 |
| 55 | const kgs ulong 702656 |
| 56 | const kns ulong 702720 |
| 57 | const kos ulong 702784 |
| 58 | const kna ulong 786432 |
| 59 | const knb ulong 851968 |
| 60 | const kbuf ulong 917504 |
| 61 | const bcap ulong 65536 |
| 62 | note g slots: 0 output length, 1 verdict length, 2 receipt length, 3 verdict kind, |
| 63 | note 4 verdict head offset, 5 reviewer offset, 6 reviewer length, 7 clone offset, |
| 64 | note 8 gate_rc, 9 negctl_rc, 10 author offset, 11 author length, 12 receipt keys seen, |
| 65 | note 13 command output length, 19 HOME length, 21 PATH length, 16 RID length, |
| 66 | note 17 reviews dir length, 18 receipts dir length, 23 status word length, |
| 67 | note 24 verdict name length, 25 receipt name length, 30 packet length, 31 packet count, |
| 68 | note 32 read end of the spawn pipe, 33 name length, 34 out dir length, 35 author length, |
| 69 | note 36 first --neg argument, 37 index of "--", 38 OMC entry length (0: unset), |
| 70 | note 39 RAD_HOME entry length (0: unset), 40 temp dir length, 41 JSON output, 42 storage length, |
| 71 | note 43 SHA-256 bytes buffered, 44 SHA-256 total bytes, 45 started ns, 46 finished ns, |
| 72 | note 47 gate rc, 48 negctl rc, 49 and 50 gate log write and read fd, 51 and 52 negctl log write |
| 73 | note and read fd, 53 out files this gate run created, 54 gate timeout in seconds, 55 --timeout seen. |
| 74 | note kna and knb hold the normalized reviewer and author for the self-review check. |
| 75 | text tverd "VERDICT " |
| 76 | text tvw "VERDICT" |
| 77 | text tmaf "MERGE_AFTER_FIX " |
| 78 | text tmrg "MERGE " |
| 79 | text trej "REJECT " |
| 80 | text thead "head=" |
| 81 | text trevw "reviewer=" |
| 82 | text trch "FORGE-RECEIPT v1\n" |
| 83 | text tclone "clone=" |
| 84 | text tgate "gate_rc=" |
| 85 | text tneg "negctl_rc=" |
| 86 | text tauth "author=" |
| 87 | text tgcmd "gate_cmd=" |
| 88 | text tncmd "negctl_cmd=" |
| 89 | text tdec "decide" |
| 90 | text tstat "status" |
| 91 | text tmrgw "MERGE " |
| 92 | text tholdw "HOLD " |
| 93 | text tok "OK" |
| 94 | text tusage "USAGE" |
| 95 | text tbadh "BAD_HEAD" |
| 96 | text tvunr "VERDICT_UNREADABLE" |
| 97 | text tvmal "VERDICT_MALFORMED" |
| 98 | text tvmaf "VERDICT_MERGE_AFTER_FIX" |
| 99 | text tvrej "VERDICT_REJECT" |
| 100 | text thmis "HEAD_MISMATCH" |
| 101 | text tnorv "NO_REVIEWER" |
| 102 | text trunr "RECEIPT_UNREADABLE" |
| 103 | text trmal "RECEIPT_MALFORMED" |
| 104 | text tnong "NO_NEGCTL" |
| 105 | text tcmis "CLONE_MISMATCH" |
| 106 | text tgfail "GATE_FAIL" |
| 107 | text tnpass "NEGCTL_PASSED" |
| 108 | text tself "SELF_REVIEW" |
| 109 | text tvamb "VERDICT_AMBIGUOUS" |
| 110 | text tgtmo "GATE_TIMEOUT" |
| 111 | text thelp "usage: forge decide [--json] <patch-head> <verdict-file> <gate-receipt>\n forge status <RID> [reviews-dir [receipts-dir]]\n forge gate <RID> <head> [--name N] [--out DIR] [--timeout S] --author A --neg ARGV.. -- ARGV..\n" |
| 112 | text thome "HOME=" |
| 113 | text tpath "PATH=" |
| 114 | text tdpath "PATH=/usr/local/bin:/usr/bin:/bin" |
| 115 | text tenv "/usr/bin/env" |
| 116 | text trad "rad" |
| 117 | text tpatch "patch" |
| 118 | text tlist "list" |
| 119 | text tshow "show" |
| 120 | text tall "--all" |
| 121 | text trepo "--repo" |
| 122 | text trevd "/handoffs/hive/manager/reviews" |
| 123 | text trcpd "/handoffs/hive/forge/receipts" |
| 124 | text tvsuf ".verdict.txt" |
| 125 | text trsuf ".receipt.txt" |
| 126 | text tlpat " Patch " |
| 127 | text tlhead " Head " |
| 128 | text tlstat " Status " |
| 129 | text tsp "patch " |
| 130 | text tsst " status=" |
| 131 | text tshd " head=" |
| 132 | text tsvd " verdict=" |
| 133 | text tsdc " decision=" |
| 134 | text tnone "none" |
| 135 | text tnorun "NOT_RUN no verdict file" |
| 136 | text tsum "forge status " |
| 137 | text tspat " patches=" |
| 138 | text tsdec " decided=" |
| 139 | text tsmrg " merge=" |
| 140 | text tshld " hold=" |
| 141 | text tsnr " not_run=" |
| 142 | text terad "forge: rad failed, exit " |
| 143 | text tebad "forge: bad RID\n" |
| 144 | text tehome "forge: HOME is not set\n" |
| 145 | text tefld "forge: patch fields not found for " |
| 146 | text ttrunc "TRUNCATED patches=" |
| 147 | text ttrunc2 " examined=" |
| 148 | text tramb "RECEIPT_AMBIGUOUS" |
| 149 | text tsrc " receipt=" |
| 150 | text tgatew "gate" |
| 151 | text tjson "--json" |
| 152 | text tfname "--name" |
| 153 | text tfout "--out" |
| 154 | text tfauth "--author" |
| 155 | text tfneg "--neg" |
| 156 | text tftmo "--timeout" |
| 157 | text tdd "--" |
| 158 | text tomce "OMC=" |
| 159 | text tradh "RAD_HOME=" |
| 160 | text tgnos "GIT_CONFIG_NOSYSTEM=1" |
| 161 | text tgglob "GIT_CONFIG_GLOBAL=/dev/null" |
| 162 | text tgit "git" |
| 163 | text tinit "init" |
| 164 | text tq "-q" |
| 165 | text tdc "-C" |
| 166 | text tcfg "-c" |
| 167 | text tfetch "fetch" |
| 168 | text tnotags "--no-tags" |
| 169 | text tadv "advice.detachedHead=false" |
| 170 | text tcheck "checkout" |
| 171 | text tdetach "--detach" |
| 172 | text trevp "rev-parse" |
| 173 | text theadw "HEAD" |
| 174 | text ttreew "HEAD^{tree}" |
| 175 | text tinspect "inspect" |
| 176 | text tpayload "--payload" |
| 177 | text tnamek "\"name\": \"" |
| 178 | text trm "/bin/rm" |
| 179 | text trf "-rf" |
| 180 | text ttmp "/tmp/forge-gate-" |
| 181 | text tstor "/storage/" |
| 182 | text tdotrad "/.radicle" |
| 183 | text tgdir "/gate" |
| 184 | text tndir "/neg" |
| 185 | text tglog ".gate.log" |
| 186 | text tnlog ".negctl.log" |
| 187 | text ttgl "/gate.log" |
| 188 | text ttnl "/negctl.log" |
| 189 | text tdevnull "/dev/null" |
| 190 | text ttree "tree=" |
| 191 | text tridk "rid=" |
| 192 | text tomck "omc=" |
| 193 | text tstart "started=" |
| 194 | text tfin "finished=" |
| 195 | text tlsha "log_sha256=" |
| 196 | text tnlsha "negctl_log_sha256=" |
| 197 | text tj1 "{\"schema\":\"forge-decision/1\",\"head\":" |
| 198 | text tjnull "null" |
| 199 | text tjq "\"" |
| 200 | text tjdec ",\"decision\":\"" |
| 201 | text tjrsn "\",\"reason\":\"" |
| 202 | text tjend "\"}" |
| 203 | text tmerge "MERGE" |
| 204 | text thold "HOLD" |
| 205 | text tgo "forge gate " |
| 206 | text tgoh " head=" |
| 207 | text tgor " gate_rc=" |
| 208 | text tgon " negctl_rc=" |
| 209 | text tgorc " receipt=" |
| 210 | text teargs "forge: bad gate arguments (a newline in an argument, a command that starts with NAME=, or too long for the receipt)\n" |
| 211 | text tename "forge: bad repository name\n" |
| 212 | text teclone "forge: fresh clone failed\n" |
| 213 | text tetmp "forge: cannot create the temp dir\n" |
| 214 | text teout "forge: cannot write the receipt or a log (out dir missing, or an out file already exists)\n" |
| 215 | text teomc "forge: OMC is set but cannot be read\n" |
| 216 | text tebadh "forge: bad head\n" |
| 217 | text terun "forge: cannot run the gate command\n" |
| 218 | text tetmo "forge: bad --timeout (whole seconds, 1 to 86400)\n" |
| 219 | text ttmo1 "forge: timeout after " |
| 220 | text ttmo2 " s, process group killed with SIGKILL, exit status recorded as 124\n" |
| 221 | text tamb "ambiguous" |
| 222 | text tiso "0000-00-00T00:00:00Z" |
| 223 | text trpfx "rad:" |
| 224 | |
| 225 | note ---- byte helpers over m ---- |
| 226 | |
| 227 | note mat: true when m[ps..ps+len t) equals t and the match ends at or before lim |
| 228 | fn mat ps ulong lim ulong t view uchar * to bool |
| 229 | n = len t |
| 230 | if gt add ps n lim |
| 231 | ret false |
| 232 | end |
| 233 | i = ulong 0 |
| 234 | while lt i n bound 4096 |
| 235 | if ne get m add ps i get t i |
| 236 | ret false |
| 237 | end |
| 238 | i = add i 1 |
| 239 | end |
| 240 | ret true |
| 241 | end |
| 242 | |
| 243 | fn ishex c uchar to bool |
| 244 | if and ge c 48 le c 57 |
| 245 | ret true |
| 246 | end |
| 247 | if and ge c 97 le c 102 |
| 248 | ret true |
| 249 | end |
| 250 | ret false |
| 251 | end |
| 252 | |
| 253 | note hex40: exactly 40 lowercase hex digits at m[ps..ps+40), all before lim |
| 254 | fn hex40 ps ulong lim ulong to bool |
| 255 | if gt add ps 40 lim |
| 256 | ret false |
| 257 | end |
| 258 | i = ulong 0 |
| 259 | while lt i 40 bound 40 |
| 260 | if eq ishex get m add ps i false |
| 261 | ret false |
| 262 | end |
| 263 | i = add i 1 |
| 264 | end |
| 265 | ret true |
| 266 | end |
| 267 | |
| 268 | note hexn: exactly n lowercase hex digits at m[ps..ps+n) |
| 269 | fn hexn ps ulong n ulong to bool |
| 270 | i = ulong 0 |
| 271 | while lt i n bound 4096 |
| 272 | if eq ishex get m add ps i false |
| 273 | ret false |
| 274 | end |
| 275 | i = add i 1 |
| 276 | end |
| 277 | ret true |
| 278 | end |
| 279 | |
| 280 | note argis: argument i is exactly the text t |
| 281 | fn argis i ulong t view uchar * to bool |
| 282 | n = argread i m add kx 5120 4096 |
| 283 | if lt n 0 |
| 284 | ret false |
| 285 | end |
| 286 | if ne ulong n len t |
| 287 | ret false |
| 288 | end |
| 289 | ret mat add kx 5120 add add kx 5120 len t t |
| 290 | end |
| 291 | |
| 292 | fn eqr a ulong b ulong n ulong to bool |
| 293 | i = ulong 0 |
| 294 | while lt i n bound 65536 |
| 295 | if ne get m add a i get m add b i |
| 296 | ret false |
| 297 | end |
| 298 | i = add i 1 |
| 299 | end |
| 300 | ret true |
| 301 | end |
| 302 | |
| 303 | fn cpy dst ulong src ulong n ulong to void |
| 304 | i = ulong 0 |
| 305 | while lt i n bound 65536 |
| 306 | put m add dst i get m add src i |
| 307 | i = add i 1 |
| 308 | end |
| 309 | end |
| 310 | |
| 311 | fn tcpy dst ulong t view uchar * to ulong |
| 312 | n = len t |
| 313 | i = ulong 0 |
| 314 | while lt i n bound 4096 |
| 315 | put m add dst i get t i |
| 316 | i = add i 1 |
| 317 | end |
| 318 | ret n |
| 319 | end |
| 320 | |
| 321 | note eol: offset of the next newline at or after ps, or lim when there is none |
| 322 | fn eol ps ulong lim ulong to ulong |
| 323 | p = ps |
| 324 | while lt p lim bound 1048576 |
| 325 | if eq get m p 10 |
| 326 | ret p |
| 327 | end |
| 328 | p = add p 1 |
| 329 | end |
| 330 | ret p |
| 331 | end |
| 332 | |
| 333 | note ---- output line ---- |
| 334 | |
| 335 | fn obyte c uchar to void |
| 336 | w = get g 0 |
| 337 | if ge w wcap |
| 338 | trap 12 |
| 339 | end |
| 340 | put m add kw w c |
| 341 | put g 0 add w 1 |
| 342 | end |
| 343 | |
| 344 | fn otext t view uchar * to void |
| 345 | i = ulong 0 |
| 346 | n = len t |
| 347 | while lt i n bound 4096 |
| 348 | obyte get t i |
| 349 | i = add i 1 |
| 350 | end |
| 351 | end |
| 352 | |
| 353 | fn ocopy ps ulong n ulong to void |
| 354 | i = ulong 0 |
| 355 | while lt i n bound 8192 |
| 356 | obyte get m add ps i |
| 357 | i = add i 1 |
| 358 | end |
| 359 | end |
| 360 | |
| 361 | fn onum v ulong to void |
| 362 | b = new uchar 24 0 |
| 363 | i = ulong 24 |
| 364 | x = v |
| 365 | c = true |
| 366 | while c bound 24 |
| 367 | i = sub i 1 |
| 368 | put b i uchar add 48 rem x 10 |
| 369 | x = div x 10 |
| 370 | if eq x 0 |
| 371 | c = false |
| 372 | end |
| 373 | end |
| 374 | while lt i 24 bound 24 |
| 375 | obyte get b i |
| 376 | i = add i 1 |
| 377 | end |
| 378 | end |
| 379 | |
| 380 | fn oflush fd int to void |
| 381 | w = get g 0 |
| 382 | o = ulong 0 |
| 383 | c = true |
| 384 | while c bound 8192 |
| 385 | if ge o w |
| 386 | c = false |
| 387 | else |
| 388 | r = write fd m add kw o sub w o |
| 389 | if le r 0 |
| 390 | c = false |
| 391 | else |
| 392 | o = add o ulong r |
| 393 | end |
| 394 | end |
| 395 | end |
| 396 | put g 0 0 |
| 397 | end |
| 398 | |
| 399 | fn oreason c int to void |
| 400 | if eq c 0 |
| 401 | otext tok |
| 402 | end |
| 403 | if eq c 1 |
| 404 | otext tusage |
| 405 | end |
| 406 | if eq c 2 |
| 407 | otext tbadh |
| 408 | end |
| 409 | if eq c 3 |
| 410 | otext tvunr |
| 411 | end |
| 412 | if eq c 4 |
| 413 | otext tvmal |
| 414 | end |
| 415 | if eq c 5 |
| 416 | otext tvmaf |
| 417 | end |
| 418 | if eq c 6 |
| 419 | otext tvrej |
| 420 | end |
| 421 | if eq c 7 |
| 422 | otext thmis |
| 423 | end |
| 424 | if eq c 8 |
| 425 | otext tnorv |
| 426 | end |
| 427 | if eq c 9 |
| 428 | otext trunr |
| 429 | end |
| 430 | if eq c 10 |
| 431 | otext trmal |
| 432 | end |
| 433 | if eq c 11 |
| 434 | otext tnong |
| 435 | end |
| 436 | if eq c 12 |
| 437 | otext tcmis |
| 438 | end |
| 439 | if eq c 13 |
| 440 | otext tgfail |
| 441 | end |
| 442 | if eq c 14 |
| 443 | otext tnpass |
| 444 | end |
| 445 | if eq c 15 |
| 446 | otext tself |
| 447 | end |
| 448 | if eq c 16 |
| 449 | otext tvamb |
| 450 | end |
| 451 | if eq c 17 |
| 452 | otext tramb |
| 453 | end |
| 454 | if eq c 18 |
| 455 | otext tgtmo |
| 456 | end |
| 457 | end |
| 458 | |
| 459 | note odecis: "MERGE OK" or "HOLD <CODE>" into the output line |
| 460 | fn odecis c int to void |
| 461 | if eq c 0 |
| 462 | otext tmrgw |
| 463 | else |
| 464 | otext tholdw |
| 465 | end |
| 466 | oreason c |
| 467 | end |
| 468 | |
| 469 | note ---- file loading ---- |
| 470 | |
| 471 | note load: read the file named by m[kp..kp+pn) into m[ps..ps+cap); return its length, or -1 when |
| 472 | note it cannot be opened or read or does not fit (a file of cap bytes or more does not fit) |
| 473 | fn load pn ulong ps ulong cap ulong to long |
| 474 | fl = fopen m kp pn 524288 0 |
| 475 | if lt fl 0 |
| 476 | ret -1 |
| 477 | end |
| 478 | fd = int fl |
| 479 | n = ulong 0 |
| 480 | r = long 0 |
| 481 | c = true |
| 482 | while c bound 1048576 |
| 483 | if ge n cap |
| 484 | c = false |
| 485 | else |
| 486 | r = read fd m add ps n sub cap n |
| 487 | if le r 0 |
| 488 | c = false |
| 489 | else |
| 490 | n = add n ulong r |
| 491 | end |
| 492 | end |
| 493 | end |
| 494 | x = fclose fd |
| 495 | if lt r 0 |
| 496 | ret -1 |
| 497 | end |
| 498 | if ge n cap |
| 499 | ret -1 |
| 500 | end |
| 501 | ret long n |
| 502 | end |
| 503 | |
| 504 | note ---- verdict ---- |
| 505 | |
| 506 | note pverd: line 1 is exactly "VERDICT <MERGE|MERGE_AFTER_FIX|REJECT> head=<40 lowercase hex>" |
| 507 | note followed by a newline or the end of the file. Return 0 or 4 (VERDICT_MALFORMED). |
| 508 | fn pverd to int |
| 509 | n = add kv get g 1 |
| 510 | if eq mat kv n tverd false |
| 511 | ret 4 |
| 512 | end |
| 513 | p = add kv 8 |
| 514 | k = ulong 0 |
| 515 | if mat p n tmaf |
| 516 | k = ulong 2 |
| 517 | p = add p 16 |
| 518 | else |
| 519 | if mat p n tmrg |
| 520 | k = ulong 1 |
| 521 | p = add p 6 |
| 522 | else |
| 523 | if mat p n trej |
| 524 | k = ulong 3 |
| 525 | p = add p 7 |
| 526 | end |
| 527 | end |
| 528 | end |
| 529 | if eq k 0 |
| 530 | ret 4 |
| 531 | end |
| 532 | if eq mat p n thead false |
| 533 | ret 4 |
| 534 | end |
| 535 | p = add p 5 |
| 536 | if eq hex40 p n false |
| 537 | ret 4 |
| 538 | end |
| 539 | q = add p 40 |
| 540 | if lt q n |
| 541 | c = get m q |
| 542 | if ne c 10 |
| 543 | ret 4 |
| 544 | end |
| 545 | end |
| 546 | put g 3 k |
| 547 | put g 4 p |
| 548 | ret scanv n |
| 549 | end |
| 550 | |
| 551 | note scanv: no later line may start with VERDICT, and at most one line starts with reviewer=. |
| 552 | note The reviewer value names a reviewer only when it holds a printable ASCII byte (33..126); |
| 553 | note a control byte in it (below 32 other than tab, or 127) is malformed. |
| 554 | fn scanv n ulong to int |
| 555 | p = add eol kv n 1 |
| 556 | put g 5 0 |
| 557 | put g 6 0 |
| 558 | seen = false |
| 559 | while lt p n bound 65536 |
| 560 | e = eol p n |
| 561 | if mat p e tvw |
| 562 | ret 4 |
| 563 | end |
| 564 | if mat p e trevw |
| 565 | if seen |
| 566 | ret 4 |
| 567 | end |
| 568 | seen = true |
| 569 | t = txtchk add p 9 e |
| 570 | if eq t 2 |
| 571 | ret 4 |
| 572 | end |
| 573 | if eq t 0 |
| 574 | put g 5 add p 9 |
| 575 | put g 6 sub e add p 9 |
| 576 | end |
| 577 | end |
| 578 | p = add e 1 |
| 579 | end |
| 580 | ret 0 |
| 581 | end |
| 582 | |
| 583 | note txtchk: m[v..e) as asserted text: 0 when it holds a byte in 33..126 and no control byte, |
| 584 | note 1 when it is empty or only spaces, tabs and bytes of 128 and above, 2 on a control byte |
| 585 | fn txtchk v ulong e ulong to int |
| 586 | p = v |
| 587 | vis = false |
| 588 | while lt p e bound 65536 |
| 589 | c = get m p |
| 590 | if and lt c 32 ne c 9 |
| 591 | ret 2 |
| 592 | end |
| 593 | if eq c 127 |
| 594 | ret 2 |
| 595 | end |
| 596 | if and gt c 32 lt c 127 |
| 597 | vis = true |
| 598 | end |
| 599 | p = add p 1 |
| 600 | end |
| 601 | if vis |
| 602 | ret 0 |
| 603 | end |
| 604 | ret 1 |
| 605 | end |
| 606 | |
| 607 | note ---- gate receipt (forge-receipt v1) ---- |
| 608 | |
| 609 | note pdec: 1 to 3 decimal digits in m[v..e), value at most 255; 1000 means malformed |
| 610 | fn pdec v ulong e ulong to ulong |
| 611 | n = sub e v |
| 612 | if or eq n 0 gt n 3 |
| 613 | ret 1000 |
| 614 | end |
| 615 | x = ulong 0 |
| 616 | i = v |
| 617 | while lt i e bound 3 |
| 618 | c = get m i |
| 619 | if or lt c 48 gt c 57 |
| 620 | ret 1000 |
| 621 | end |
| 622 | x = add mul x 10 ulong sub c 48 |
| 623 | i = add i 1 |
| 624 | end |
| 625 | if gt x 255 |
| 626 | ret 1000 |
| 627 | end |
| 628 | ret x |
| 629 | end |
| 630 | |
| 631 | note ridok: m[p..p+n) is "rad:" then 1 to 60 ASCII letters and digits |
| 632 | fn ridok p ulong n ulong to bool |
| 633 | if or lt n 5 gt n 64 |
| 634 | ret false |
| 635 | end |
| 636 | if eq mat p add p 4 trpfx false |
| 637 | ret false |
| 638 | end |
| 639 | i = ulong 4 |
| 640 | while lt i n bound 64 |
| 641 | c = get m add p i |
| 642 | ok = false |
| 643 | if and ge c 48 le c 57 |
| 644 | ok = true |
| 645 | end |
| 646 | if and ge c 65 le c 90 |
| 647 | ok = true |
| 648 | end |
| 649 | if and ge c 97 le c 122 |
| 650 | ok = true |
| 651 | end |
| 652 | if eq ok false |
| 653 | ret false |
| 654 | end |
| 655 | i = add i 1 |
| 656 | end |
| 657 | ret true |
| 658 | end |
| 659 | |
| 660 | note ronce: receipt key bit seen at most once; return 0 or 10 |
| 661 | fn ronce bit ulong to int |
| 662 | s = get g 12 |
| 663 | if ne and s bit 0 |
| 664 | ret 10 |
| 665 | end |
| 666 | put g 12 or s bit |
| 667 | ret 0 |
| 668 | end |
| 669 | |
| 670 | note rhex: the value of key t in m[p..e) is exactly n lowercase hex digits; return 0 or 10 |
| 671 | fn rhex p ulong e ulong t view uchar * bit ulong n ulong to int |
| 672 | v = add p len t |
| 673 | if ne sub e v n |
| 674 | ret 10 |
| 675 | end |
| 676 | if eq hexn v n false |
| 677 | ret 10 |
| 678 | end |
| 679 | ret ronce bit |
| 680 | end |
| 681 | |
| 682 | note isoz: m[v..e) is a UTC time YYYY-MM-DDTHH:MM:SSZ (digits where tiso has 0) |
| 683 | fn isoz v ulong e ulong to bool |
| 684 | if ne sub e v 20 |
| 685 | ret false |
| 686 | end |
| 687 | i = ulong 0 |
| 688 | while lt i 20 bound 20 |
| 689 | c = get m add v i |
| 690 | w = get tiso i |
| 691 | if eq w 48 |
| 692 | if or lt c 48 gt c 57 |
| 693 | ret false |
| 694 | end |
| 695 | else |
| 696 | if ne c w |
| 697 | ret false |
| 698 | end |
| 699 | end |
| 700 | i = add i 1 |
| 701 | end |
| 702 | ret true |
| 703 | end |
| 704 | |
| 705 | note rkey: one key=value line m[p..e). Known keys only, each at most once. |
| 706 | fn rkey p ulong e ulong to int |
| 707 | s = get g 12 |
| 708 | if mat p e tclone |
| 709 | if ne and s 1 0 |
| 710 | ret 10 |
| 711 | end |
| 712 | v = add p 6 |
| 713 | if ne sub e v 40 |
| 714 | ret 10 |
| 715 | end |
| 716 | if eq hex40 v e false |
| 717 | ret 10 |
| 718 | end |
| 719 | put g 7 v |
| 720 | put g 12 or s 1 |
| 721 | ret 0 |
| 722 | end |
| 723 | if mat p e tgate |
| 724 | if ne and s 2 0 |
| 725 | ret 10 |
| 726 | end |
| 727 | d = pdec add p 8 e |
| 728 | if eq d 1000 |
| 729 | ret 10 |
| 730 | end |
| 731 | put g 8 d |
| 732 | put g 12 or s 2 |
| 733 | ret 0 |
| 734 | end |
| 735 | if mat p e tneg |
| 736 | if ne and s 4 0 |
| 737 | ret 10 |
| 738 | end |
| 739 | d = pdec add p 10 e |
| 740 | if eq d 1000 |
| 741 | ret 10 |
| 742 | end |
| 743 | put g 9 d |
| 744 | put g 12 or s 4 |
| 745 | ret 0 |
| 746 | end |
| 747 | if mat p e tauth |
| 748 | if ne and s 8 0 |
| 749 | ret 10 |
| 750 | end |
| 751 | v = add p 7 |
| 752 | if ne txtchk v e 0 |
| 753 | ret 10 |
| 754 | end |
| 755 | put g 10 v |
| 756 | put g 11 sub e v |
| 757 | put g 12 or s 8 |
| 758 | ret 0 |
| 759 | end |
| 760 | if mat p e tgcmd |
| 761 | if ne and s 16 0 |
| 762 | ret 10 |
| 763 | end |
| 764 | put g 12 or s 16 |
| 765 | ret 0 |
| 766 | end |
| 767 | if mat p e tncmd |
| 768 | if ne and s 32 0 |
| 769 | ret 10 |
| 770 | end |
| 771 | put g 12 or s 32 |
| 772 | ret 0 |
| 773 | end |
| 774 | if mat p e ttree |
| 775 | ret rhex p e ttree 64 40 |
| 776 | end |
| 777 | if mat p e tridk |
| 778 | if eq ridok add p 4 sub e add p 4 false |
| 779 | ret 10 |
| 780 | end |
| 781 | ret ronce 128 |
| 782 | end |
| 783 | if mat p e tomck |
| 784 | if and eq sub e p 8 mat add p 4 e tnone |
| 785 | ret ronce 256 |
| 786 | end |
| 787 | ret rhex p e tomck 256 64 |
| 788 | end |
| 789 | if mat p e tstart |
| 790 | if eq isoz add p 8 e false |
| 791 | ret 10 |
| 792 | end |
| 793 | ret ronce 512 |
| 794 | end |
| 795 | if mat p e tfin |
| 796 | if eq isoz add p 9 e false |
| 797 | ret 10 |
| 798 | end |
| 799 | ret ronce 1024 |
| 800 | end |
| 801 | if mat p e tlsha |
| 802 | ret rhex p e tlsha 2048 64 |
| 803 | end |
| 804 | if mat p e tnlsha |
| 805 | ret rhex p e tnlsha 4096 64 |
| 806 | end |
| 807 | ret 10 |
| 808 | end |
| 809 | |
| 810 | note prcpt: return 0, 10 (RECEIPT_MALFORMED) or 11 (NO_NEGCTL) |
| 811 | fn prcpt to int |
| 812 | n = add kr get g 2 |
| 813 | if eq mat kr n trch false |
| 814 | ret 10 |
| 815 | end |
| 816 | p = add kr 17 |
| 817 | put g 12 0 |
| 818 | while lt p n bound 4096 |
| 819 | e = eol p n |
| 820 | r = rkey p e |
| 821 | if ne r 0 |
| 822 | ret r |
| 823 | end |
| 824 | p = add e 1 |
| 825 | end |
| 826 | s = get g 12 |
| 827 | if ne and s 11 11 |
| 828 | ret 10 |
| 829 | end |
| 830 | if eq and s 4 0 |
| 831 | ret 11 |
| 832 | end |
| 833 | ret 0 |
| 834 | end |
| 835 | |
| 836 | note ispun: a space or an ASCII punctuation byte |
| 837 | fn ispun c uchar to bool |
| 838 | if eq c 32 |
| 839 | ret true |
| 840 | end |
| 841 | if and ge c 33 le c 47 |
| 842 | ret true |
| 843 | end |
| 844 | if and ge c 58 le c 64 |
| 845 | ret true |
| 846 | end |
| 847 | if and ge c 91 le c 96 |
| 848 | ret true |
| 849 | end |
| 850 | if and ge c 123 le c 126 |
| 851 | ret true |
| 852 | end |
| 853 | ret false |
| 854 | end |
| 855 | |
| 856 | note nstrip: drop spaces and ASCII punctuation from both ends of m[d..d+n); return the new length |
| 857 | fn nstrip d ulong n ulong to ulong |
| 858 | e = n |
| 859 | c = true |
| 860 | while c bound 65536 |
| 861 | if eq e 0 |
| 862 | c = false |
| 863 | else |
| 864 | if ispun get m add d sub e 1 |
| 865 | e = sub e 1 |
| 866 | else |
| 867 | c = false |
| 868 | end |
| 869 | end |
| 870 | end |
| 871 | s = ulong 0 |
| 872 | c = true |
| 873 | while c bound 65536 |
| 874 | if ge s e |
| 875 | c = false |
| 876 | else |
| 877 | if ispun get m add d s |
| 878 | s = add s 1 |
| 879 | else |
| 880 | c = false |
| 881 | end |
| 882 | end |
| 883 | end |
| 884 | cpy d add d s sub e s |
| 885 | ret sub e s |
| 886 | end |
| 887 | |
| 888 | note norm: copy m[src..src+n) to m[dst..) folded to ASCII lower case; spaces, tabs, "_" and "-" |
| 889 | note count as spaces and each run of them becomes one space; then spaces and ASCII punctuation |
| 890 | note are stripped from both ends. Return the copied length. Unicode is not folded. |
| 891 | fn norm src ulong n ulong dst ulong to ulong |
| 892 | o = ulong 0 |
| 893 | last = ulong 32 |
| 894 | i = ulong 0 |
| 895 | while lt i n bound 65536 |
| 896 | c = ulong get m add src i |
| 897 | if or or eq c 32 eq c 9 or eq c 95 eq c 45 |
| 898 | if ne last 32 |
| 899 | put m add dst o 32 |
| 900 | o = add o 1 |
| 901 | last = ulong 32 |
| 902 | end |
| 903 | else |
| 904 | if and ge c 65 le c 90 |
| 905 | c = add c 32 |
| 906 | end |
| 907 | put m add dst o uchar c |
| 908 | o = add o 1 |
| 909 | last = c |
| 910 | end |
| 911 | i = add i 1 |
| 912 | end |
| 913 | ret nstrip dst o |
| 914 | end |
| 915 | |
| 916 | note hasauth: the normalized reviewer text contains the normalized receipt author (the author |
| 917 | note reviewed its own work). An author that normalizes to nothing counts as self-review. |
| 918 | fn hasauth to bool |
| 919 | rn = norm get g 5 get g 6 kna |
| 920 | an = norm get g 10 get g 11 knb |
| 921 | if eq an 0 |
| 922 | ret true |
| 923 | end |
| 924 | if gt an rn |
| 925 | ret false |
| 926 | end |
| 927 | i = ulong 0 |
| 928 | lim = sub rn an |
| 929 | while le i lim bound 65536 |
| 930 | if eqr add kna i knb an |
| 931 | ret true |
| 932 | end |
| 933 | i = add i 1 |
| 934 | end |
| 935 | ret false |
| 936 | end |
| 937 | |
| 938 | note ---- the merge decision ---- |
| 939 | |
| 940 | note decide: vr and rr are the verdict and receipt load results; the patch head is m[kh..kh+40). |
| 941 | note The first failing invariant is the reason. 0 is MERGE; everything else is HOLD. |
| 942 | fn decide vr long rr long to int |
| 943 | if eq hex40 kh add kh 40 false |
| 944 | ret 2 |
| 945 | end |
| 946 | if le vr 0 |
| 947 | ret 3 |
| 948 | end |
| 949 | c = pverd |
| 950 | if ne c 0 |
| 951 | ret c |
| 952 | end |
| 953 | k = get g 3 |
| 954 | if eq k 2 |
| 955 | ret 5 |
| 956 | end |
| 957 | if eq k 3 |
| 958 | ret 6 |
| 959 | end |
| 960 | if eq eqr get g 4 kh 40 false |
| 961 | ret 7 |
| 962 | end |
| 963 | if eq get g 6 0 |
| 964 | ret 8 |
| 965 | end |
| 966 | if eq rr long -2 |
| 967 | ret 17 |
| 968 | end |
| 969 | if le rr 0 |
| 970 | ret 9 |
| 971 | end |
| 972 | c = prcpt |
| 973 | if ne c 0 |
| 974 | ret c |
| 975 | end |
| 976 | if eq eqr get g 7 kh 40 false |
| 977 | ret 12 |
| 978 | end |
| 979 | if or eq get g 8 124 eq get g 9 124 |
| 980 | ret 18 |
| 981 | end |
| 982 | if ne get g 8 0 |
| 983 | ret 13 |
| 984 | end |
| 985 | if eq get g 9 0 |
| 986 | ret 14 |
| 987 | end |
| 988 | if hasauth |
| 989 | ret 15 |
| 990 | end |
| 991 | ret 0 |
| 992 | end |
| 993 | |
| 994 | note say: print the decision (text, or one forge-decision/1 JSON object when g 41 is 1) |
| 995 | fn say c int to int |
| 996 | put g 0 0 |
| 997 | if eq get g 41 1 |
| 998 | otext tj1 |
| 999 | if hex40 kh add kh 40 |
| 1000 | otext tjq |
| 1001 | ocopy kh 40 |
| 1002 | otext tjq |
| 1003 | else |
| 1004 | otext tjnull |
| 1005 | end |
| 1006 | otext tjdec |
| 1007 | if eq c 0 |
| 1008 | otext tmerge |
| 1009 | else |
| 1010 | otext thold |
| 1011 | end |
| 1012 | otext tjrsn |
| 1013 | oreason c |
| 1014 | otext tjend |
| 1015 | else |
| 1016 | odecis c |
| 1017 | end |
| 1018 | obyte 10 |
| 1019 | oflush 1 |
| 1020 | if eq c 0 |
| 1021 | ret 0 |
| 1022 | end |
| 1023 | ret 1 |
| 1024 | end |
| 1025 | |
| 1026 | fn cdecide to int |
| 1027 | a = ulong argcnt |
| 1028 | o = ulong 2 |
| 1029 | if ge a 3 |
| 1030 | if argis 2 tjson |
| 1031 | put g 41 1 |
| 1032 | o = ulong 3 |
| 1033 | end |
| 1034 | end |
| 1035 | if ne a add o 3 |
| 1036 | ret say 1 |
| 1037 | end |
| 1038 | hn = argread o m kh 64 |
| 1039 | if ne hn 40 |
| 1040 | put m kh 0 |
| 1041 | ret say 2 |
| 1042 | end |
| 1043 | pn = argread add o 1 m kp pcap |
| 1044 | vr = long -1 |
| 1045 | if gt pn 0 |
| 1046 | vr = load ulong pn kv vcap |
| 1047 | end |
| 1048 | if gt vr 0 |
| 1049 | put g 1 ulong vr |
| 1050 | end |
| 1051 | qn = argread add o 2 m kp pcap |
| 1052 | rr = long -1 |
| 1053 | if gt qn 0 |
| 1054 | rr = load ulong qn kr rcap |
| 1055 | end |
| 1056 | if gt rr 0 |
| 1057 | put g 2 ulong rr |
| 1058 | end |
| 1059 | ret say decide vr rr |
| 1060 | end |
| 1061 | |
| 1062 | note ---- status: the Radicle edge (rad CLI through /usr/bin/env) ---- |
| 1063 | |
| 1064 | fn pkinit to void |
| 1065 | put g 30 2 |
| 1066 | put g 31 0 |
| 1067 | end |
| 1068 | |
| 1069 | note pkadd: append one packed-list record m[ps..ps+n) to the exec packet |
| 1070 | fn pkadd ps ulong n ulong to void |
| 1071 | w = get g 30 |
| 1072 | if gt add add w 2 n ecap |
| 1073 | trap 12 |
| 1074 | end |
| 1075 | put m add ke w uchar and n 255 |
| 1076 | put m add ke add w 1 uchar shr n 8 |
| 1077 | cpy add ke add w 2 ps n |
| 1078 | put g 30 add add w 2 n |
| 1079 | put g 31 add get g 31 1 |
| 1080 | end |
| 1081 | |
| 1082 | fn pktext t view uchar * to void |
| 1083 | w = get g 30 |
| 1084 | n = len t |
| 1085 | if gt add add w 2 n ecap |
| 1086 | trap 12 |
| 1087 | end |
| 1088 | put m add ke w uchar and n 255 |
| 1089 | put m add ke add w 1 uchar shr n 8 |
| 1090 | x = tcpy add ke add w 2 t |
| 1091 | put g 30 add add w 2 n |
| 1092 | put g 31 add get g 31 1 |
| 1093 | end |
| 1094 | |
| 1095 | fn pkdone to ulong |
| 1096 | c = get g 31 |
| 1097 | put m ke uchar and c 255 |
| 1098 | put m add ke 1 uchar shr c 8 |
| 1099 | ret get g 30 |
| 1100 | end |
| 1101 | |
| 1102 | note radpk: /usr/bin/env HOME=.. PATH=.. rad patch <verb> [ID] --repo RID; id is m[ps..ps+7) when n > 0 |
| 1103 | fn radpk verb int ps ulong n ulong to ulong |
| 1104 | pkinit |
| 1105 | pktext tenv |
| 1106 | pkadd kx get g 19 |
| 1107 | pkadd add kx 1024 get g 21 |
| 1108 | pktext trad |
| 1109 | pktext tpatch |
| 1110 | if eq verb 0 |
| 1111 | pktext tlist |
| 1112 | pktext tall |
| 1113 | else |
| 1114 | pktext tshow |
| 1115 | pkadd ps n |
| 1116 | end |
| 1117 | pktext trepo |
| 1118 | pkadd krid get g 16 |
| 1119 | ret pkdone |
| 1120 | end |
| 1121 | |
| 1122 | note spawn: run the packet with stdout on a pipe; return the child pid (read end in g 32) or a negative errno |
| 1123 | fn spawn pn ulong to long |
| 1124 | pf = new uchar 8 0 |
| 1125 | r = pipefds pf 0 8 524288 |
| 1126 | if lt r 0 |
| 1127 | ret r |
| 1128 | end |
| 1129 | rfd = int or ulong get pf 0 shl ulong get pf 1 8 |
| 1130 | wfd = int or ulong get pf 4 shl ulong get pf 5 8 |
| 1131 | pid = fork |
| 1132 | if eq pid 0 |
| 1133 | y = dupfd wfd 1 0 |
| 1134 | y = execprog m ke pn |
| 1135 | exit 127 |
| 1136 | end |
| 1137 | x = fclose wfd |
| 1138 | if lt pid 0 |
| 1139 | x = fclose rfd |
| 1140 | ret pid |
| 1141 | end |
| 1142 | put g 32 ulong rfd |
| 1143 | ret pid |
| 1144 | end |
| 1145 | |
| 1146 | note drain: read the child's stdout into m[ko..ko+ocap), length in g 13; true when it overflowed |
| 1147 | note (the child is then killed, so a runaway child cannot block the forge) |
| 1148 | fn drain rfd int pid long to bool |
| 1149 | n = ulong 0 |
| 1150 | ovf = false |
| 1151 | c = true |
| 1152 | while c bound 1048576 |
| 1153 | if ge n ocap |
| 1154 | ovf = true |
| 1155 | x = killpid int pid 9 |
| 1156 | c = false |
| 1157 | else |
| 1158 | r = read rfd m add ko n sub ocap n |
| 1159 | if le r 0 |
| 1160 | c = false |
| 1161 | else |
| 1162 | n = add n ulong r |
| 1163 | end |
| 1164 | end |
| 1165 | end |
| 1166 | x = fclose rfd |
| 1167 | put g 13 n |
| 1168 | ret ovf |
| 1169 | end |
| 1170 | |
| 1171 | note runcmd: run the packet, capture stdout; return the exit status, or a negative errno |
| 1172 | fn runcmd pn ulong to long |
| 1173 | st = new uchar 4 0 |
| 1174 | pid = spawn pn |
| 1175 | if lt pid 0 |
| 1176 | ret pid |
| 1177 | end |
| 1178 | ovf = drain int get g 32 pid |
| 1179 | w = waitpid int pid st 0 4 0 |
| 1180 | if lt w 0 |
| 1181 | ret w |
| 1182 | end |
| 1183 | if ovf |
| 1184 | ret -27 |
| 1185 | end |
| 1186 | s = or ulong get st 0 shl ulong get st 1 8 |
| 1187 | if ne and s 127 0 |
| 1188 | ret -4 |
| 1189 | end |
| 1190 | ret long and shr s 8 255 |
| 1191 | end |
| 1192 | |
| 1193 | note envget: copy the environment entry with prefix t into m[ps..ps+cap); return its length or 0 |
| 1194 | fn envget t view uchar * ps ulong cap ulong to ulong |
| 1195 | c = ulong envcnt |
| 1196 | i = ulong 0 |
| 1197 | while lt i c bound 65536 |
| 1198 | n = envread i m add kx 5120 4096 |
| 1199 | if gt n 0 |
| 1200 | if mat add kx 5120 add add kx 5120 ulong n t |
| 1201 | if le ulong n cap |
| 1202 | cpy ps add kx 5120 ulong n |
| 1203 | ret ulong n |
| 1204 | end |
| 1205 | end |
| 1206 | end |
| 1207 | i = add i 1 |
| 1208 | end |
| 1209 | ret 0 |
| 1210 | end |
| 1211 | |
| 1212 | note rowid: a patch table row is the box bar (e2 94 82), a space, a state glyph, spaces, |
| 1213 | note 7 hex digits and a space. Return the offset of the digits, or 0. |
| 1214 | fn rowid p ulong e ulong to ulong |
| 1215 | if gt add p 14 e |
| 1216 | ret 0 |
| 1217 | end |
| 1218 | if ne get m p 226 |
| 1219 | ret 0 |
| 1220 | end |
| 1221 | if ne get m add p 1 148 |
| 1222 | ret 0 |
| 1223 | end |
| 1224 | if ne get m add p 2 130 |
| 1225 | ret 0 |
| 1226 | end |
| 1227 | q = add p 3 |
| 1228 | if ne get m q 32 |
| 1229 | ret 0 |
| 1230 | end |
| 1231 | q = add q 1 |
| 1232 | while and lt q e ne get m q 32 bound 1048576 |
| 1233 | q = add q 1 |
| 1234 | end |
| 1235 | while and lt q e eq get m q 32 bound 1048576 |
| 1236 | q = add q 1 |
| 1237 | end |
| 1238 | s = q |
| 1239 | while and lt q e ishex get m q bound 1048576 |
| 1240 | q = add q 1 |
| 1241 | end |
| 1242 | if ne sub q s 7 |
| 1243 | ret 0 |
| 1244 | end |
| 1245 | if ge q e |
| 1246 | ret 0 |
| 1247 | end |
| 1248 | if ne get m q 32 |
| 1249 | ret 0 |
| 1250 | end |
| 1251 | ret s |
| 1252 | end |
| 1253 | |
| 1254 | note plist: copy the first icap patch ids of the list output to ki; return the number of rows listed |
| 1255 | fn plist to ulong |
| 1256 | n = add ko get g 13 |
| 1257 | p = ko |
| 1258 | k = ulong 0 |
| 1259 | while lt p n bound 524288 |
| 1260 | e = eol p n |
| 1261 | s = rowid p e |
| 1262 | if ne s 0 |
| 1263 | if lt k icap |
| 1264 | cpy add ki mul k 8 s 7 |
| 1265 | end |
| 1266 | k = add k 1 |
| 1267 | end |
| 1268 | p = add e 1 |
| 1269 | end |
| 1270 | ret k |
| 1271 | end |
| 1272 | |
| 1273 | note lblval: a field row "<bar><label> value"; return the value offset or 0 |
| 1274 | fn lblval p ulong e ulong t view uchar * to ulong |
| 1275 | if gt add p 3 e |
| 1276 | ret 0 |
| 1277 | end |
| 1278 | if ne get m p 226 |
| 1279 | ret 0 |
| 1280 | end |
| 1281 | if ne get m add p 1 148 |
| 1282 | ret 0 |
| 1283 | end |
| 1284 | if ne get m add p 2 130 |
| 1285 | ret 0 |
| 1286 | end |
| 1287 | q = add p 3 |
| 1288 | if eq mat q e t false |
| 1289 | ret 0 |
| 1290 | end |
| 1291 | q = add q len t |
| 1292 | while and lt q e eq get m q 32 bound 1048576 |
| 1293 | q = add q 1 |
| 1294 | end |
| 1295 | ret q |
| 1296 | end |
| 1297 | |
| 1298 | note full40: 40 hex digits at v, then a space or the end of the row |
| 1299 | fn full40 v ulong e ulong to bool |
| 1300 | if eq hex40 v e false |
| 1301 | ret false |
| 1302 | end |
| 1303 | q = add v 40 |
| 1304 | if lt q e |
| 1305 | if ne get m q 32 |
| 1306 | ret false |
| 1307 | end |
| 1308 | end |
| 1309 | ret true |
| 1310 | end |
| 1311 | |
| 1312 | note sword: copy the status word (up to 31 bytes before a space) at v into ks+128; false when empty |
| 1313 | fn sword v ulong e ulong to bool |
| 1314 | q = v |
| 1315 | c = true |
| 1316 | while c bound 64 |
| 1317 | if ge q e |
| 1318 | c = false |
| 1319 | else |
| 1320 | if eq get m q 32 |
| 1321 | c = false |
| 1322 | else |
| 1323 | if ge sub q v 31 |
| 1324 | c = false |
| 1325 | else |
| 1326 | q = add q 1 |
| 1327 | end |
| 1328 | end |
| 1329 | end |
| 1330 | end |
| 1331 | if eq q v |
| 1332 | ret false |
| 1333 | end |
| 1334 | cpy add ks 128 v sub q v |
| 1335 | put g 23 sub q v |
| 1336 | ret true |
| 1337 | end |
| 1338 | |
| 1339 | note pshow: take the first Patch, Head and Status rows of "rad patch show" into ks; 7 when all found |
| 1340 | fn pshow to ulong |
| 1341 | n = add ko get g 13 |
| 1342 | p = ko |
| 1343 | f = ulong 0 |
| 1344 | while lt p n bound 524288 |
| 1345 | e = eol p n |
| 1346 | if eq and f 1 0 |
| 1347 | v = lblval p e tlpat |
| 1348 | if ne v 0 |
| 1349 | if full40 v e |
| 1350 | cpy ks v 40 |
| 1351 | f = or f 1 |
| 1352 | end |
| 1353 | end |
| 1354 | end |
| 1355 | if eq and f 2 0 |
| 1356 | v = lblval p e tlhead |
| 1357 | if ne v 0 |
| 1358 | if full40 v e |
| 1359 | cpy add ks 64 v 40 |
| 1360 | f = or f 2 |
| 1361 | end |
| 1362 | end |
| 1363 | end |
| 1364 | if eq and f 4 0 |
| 1365 | v = lblval p e tlstat |
| 1366 | if ne v 0 |
| 1367 | if sword v e |
| 1368 | f = or f 4 |
| 1369 | end |
| 1370 | end |
| 1371 | end |
| 1372 | p = add e 1 |
| 1373 | end |
| 1374 | ret f |
| 1375 | end |
| 1376 | |
| 1377 | note sufname: a directory entry name m[a..a+nl) is "<one or more bytes>-<head8><t>" |
| 1378 | fn sufname a ulong nl ulong t view uchar * to bool |
| 1379 | if lt nl add len t 10 |
| 1380 | ret false |
| 1381 | end |
| 1382 | b = sub add a nl add len t 9 |
| 1383 | if ne get m b 45 |
| 1384 | ret false |
| 1385 | end |
| 1386 | if eq eqr add b 1 add ks 64 8 false |
| 1387 | ret false |
| 1388 | end |
| 1389 | ret mat add b 9 add a nl t |
| 1390 | end |
| 1391 | |
| 1392 | fn u16at p ulong to ulong |
| 1393 | ret or ulong get m p shl ulong get m add p 1 8 |
| 1394 | end |
| 1395 | |
| 1396 | fn nlen a ulong to ulong |
| 1397 | n = ulong 0 |
| 1398 | while and lt n 255 ne get m add a n 0 bound 256 |
| 1399 | n = add n 1 |
| 1400 | end |
| 1401 | ret n |
| 1402 | end |
| 1403 | |
| 1404 | note dscan: walk r bytes of getdents64 records at kd; return the number of names that end in |
| 1405 | note "-<head8><t>". While g sl is 0, the next such name is copied to m[dst..) with its length in g sl. |
| 1406 | fn dscan r ulong t view uchar * dst ulong sl ulong to ulong |
| 1407 | k = ulong 0 |
| 1408 | o = ulong 0 |
| 1409 | while lt o r bound 32768 |
| 1410 | rl = u16at add kd add o 16 |
| 1411 | a = add kd add o 19 |
| 1412 | nl = nlen a |
| 1413 | if sufname a nl t |
| 1414 | if eq get g sl 0 |
| 1415 | cpy dst a nl |
| 1416 | put g sl nl |
| 1417 | end |
| 1418 | k = add k 1 |
| 1419 | end |
| 1420 | if eq rl 0 |
| 1421 | o = r |
| 1422 | else |
| 1423 | o = add o rl |
| 1424 | end |
| 1425 | end |
| 1426 | ret k |
| 1427 | end |
| 1428 | |
| 1429 | note findn: scan the directory m[dir..dir+dn) for names ending in "-<head8><t>" (head at ks+64). |
| 1430 | note Return the number of matching names; the first is copied to m[dst..) with its length in g sl. |
| 1431 | fn findn dir ulong dn ulong t view uchar * dst ulong sl ulong to ulong |
| 1432 | fl = fopen m dir dn 524288 0 |
| 1433 | if lt fl 0 |
| 1434 | ret 0 |
| 1435 | end |
| 1436 | fd = int fl |
| 1437 | k = ulong 0 |
| 1438 | put g sl 0 |
| 1439 | c = true |
| 1440 | while c bound 65536 |
| 1441 | r = getdnts fd m kd dcap |
| 1442 | if le r 0 |
| 1443 | c = false |
| 1444 | else |
| 1445 | k = add k dscan ulong r t dst sl |
| 1446 | end |
| 1447 | end |
| 1448 | x = fclose fd |
| 1449 | ret k |
| 1450 | end |
| 1451 | |
| 1452 | note dpath: m[kp..) = dir "/" name; return its length |
| 1453 | fn dpath dir ulong dn ulong nm ulong nn ulong to ulong |
| 1454 | cpy kp dir dn |
| 1455 | put m add kp dn 47 |
| 1456 | cpy add kp add dn 1 nm nn |
| 1457 | ret add add dn 1 nn |
| 1458 | end |
| 1459 | |
| 1460 | note spatch: decide one patch whose fields are in ks; print its line; return 0 merge, 1 hold, 2 not run |
| 1461 | fn spatch id ulong to int |
| 1462 | put g 0 0 |
| 1463 | otext tsp |
| 1464 | ocopy id 7 |
| 1465 | otext tsst |
| 1466 | ocopy add ks 128 get g 23 |
| 1467 | otext tshd |
| 1468 | ocopy add ks 64 40 |
| 1469 | otext tsvd |
| 1470 | k = findn krev get g 17 tvsuf kn 24 |
| 1471 | if eq k 0 |
| 1472 | otext tnone |
| 1473 | otext tsdc |
| 1474 | otext tnorun |
| 1475 | obyte 10 |
| 1476 | oflush 1 |
| 1477 | ret 2 |
| 1478 | end |
| 1479 | ocopy kn get g 24 |
| 1480 | if gt k 1 |
| 1481 | otext tsdc |
| 1482 | odecis 16 |
| 1483 | obyte 10 |
| 1484 | oflush 1 |
| 1485 | ret 1 |
| 1486 | end |
| 1487 | cpy kh add ks 64 40 |
| 1488 | pn = dpath krev get g 17 kn get g 24 |
| 1489 | vr = load pn kv vcap |
| 1490 | if gt vr 0 |
| 1491 | put g 1 ulong vr |
| 1492 | end |
| 1493 | rr = long -1 |
| 1494 | otext tsrc |
| 1495 | kr2 = findn krcp get g 18 trsuf kn2 25 |
| 1496 | if eq kr2 0 |
| 1497 | otext tnone |
| 1498 | end |
| 1499 | if gt kr2 1 |
| 1500 | rr = long -2 |
| 1501 | otext tamb |
| 1502 | end |
| 1503 | if eq kr2 1 |
| 1504 | ocopy kn2 get g 25 |
| 1505 | qn = dpath krcp get g 18 kn2 get g 25 |
| 1506 | rr = load qn kr rcap |
| 1507 | end |
| 1508 | otext tsdc |
| 1509 | if gt rr 0 |
| 1510 | put g 2 ulong rr |
| 1511 | end |
| 1512 | c = decide vr rr |
| 1513 | odecis c |
| 1514 | obyte 10 |
| 1515 | oflush 1 |
| 1516 | if eq c 0 |
| 1517 | ret 0 |
| 1518 | end |
| 1519 | ret 1 |
| 1520 | end |
| 1521 | |
| 1522 | fn serr t view uchar * to int |
| 1523 | put g 0 0 |
| 1524 | otext t |
| 1525 | oflush 2 |
| 1526 | ret 2 |
| 1527 | end |
| 1528 | |
| 1529 | fn cstatus to int |
| 1530 | a = argcnt |
| 1531 | if or lt a 3 gt a 5 |
| 1532 | ret serr thelp |
| 1533 | end |
| 1534 | rl = argread 2 m krid 128 |
| 1535 | if lt rl 0 |
| 1536 | ret serr tebad |
| 1537 | end |
| 1538 | if eq ridok krid ulong rl false |
| 1539 | ret serr tebad |
| 1540 | end |
| 1541 | put g 16 ulong rl |
| 1542 | hl = envget thome kx 1024 |
| 1543 | if le hl 5 |
| 1544 | ret serr tehome |
| 1545 | end |
| 1546 | put g 19 hl |
| 1547 | pl = envget tpath add kx 1024 4096 |
| 1548 | if eq pl 0 |
| 1549 | pl = tcpy add kx 1024 tdpath |
| 1550 | end |
| 1551 | put g 21 pl |
| 1552 | dl = long 0 |
| 1553 | if ge a 4 |
| 1554 | dl = argread 3 m krev 4000 |
| 1555 | else |
| 1556 | cpy krev add kx 5 sub hl 5 |
| 1557 | dl = long add sub hl 5 tcpy add krev sub hl 5 trevd |
| 1558 | end |
| 1559 | if le dl 0 |
| 1560 | ret serr thelp |
| 1561 | end |
| 1562 | put g 17 ulong dl |
| 1563 | el = long 0 |
| 1564 | if eq a 5 |
| 1565 | el = argread 4 m krcp 4000 |
| 1566 | else |
| 1567 | cpy krcp add kx 5 sub hl 5 |
| 1568 | el = long add sub hl 5 tcpy add krcp sub hl 5 trcpd |
| 1569 | end |
| 1570 | if le el 0 |
| 1571 | ret serr thelp |
| 1572 | end |
| 1573 | put g 18 ulong el |
| 1574 | ret slist |
| 1575 | end |
| 1576 | |
| 1577 | fn slist to int |
| 1578 | r = runcmd radpk 0 0 0 |
| 1579 | if ne r 0 |
| 1580 | put g 0 0 |
| 1581 | otext terad |
| 1582 | onum ulong and r 255 |
| 1583 | obyte 10 |
| 1584 | oflush 2 |
| 1585 | ret 2 |
| 1586 | end |
| 1587 | np = plist |
| 1588 | nd = np |
| 1589 | if gt nd icap |
| 1590 | nd = icap |
| 1591 | end |
| 1592 | i = ulong 0 |
| 1593 | nm = ulong 0 |
| 1594 | nh = ulong 0 |
| 1595 | nn = ulong 0 |
| 1596 | while lt i nd bound 512 |
| 1597 | id = add ki mul i 8 |
| 1598 | r = runcmd radpk 1 id 7 |
| 1599 | if ne r 0 |
| 1600 | put g 0 0 |
| 1601 | otext terad |
| 1602 | onum ulong and r 255 |
| 1603 | obyte 10 |
| 1604 | oflush 2 |
| 1605 | ret 2 |
| 1606 | end |
| 1607 | if ne pshow 7 |
| 1608 | put g 0 0 |
| 1609 | otext tefld |
| 1610 | ocopy id 7 |
| 1611 | obyte 10 |
| 1612 | oflush 2 |
| 1613 | ret 2 |
| 1614 | end |
| 1615 | c = spatch id |
| 1616 | if eq c 0 |
| 1617 | nm = add nm 1 |
| 1618 | end |
| 1619 | if eq c 1 |
| 1620 | nh = add nh 1 |
| 1621 | end |
| 1622 | if eq c 2 |
| 1623 | nn = add nn 1 |
| 1624 | end |
| 1625 | i = add i 1 |
| 1626 | end |
| 1627 | put g 0 0 |
| 1628 | otext tsum |
| 1629 | ocopy krid get g 16 |
| 1630 | otext tspat |
| 1631 | onum np |
| 1632 | otext tsdec |
| 1633 | onum add nm nh |
| 1634 | otext tsmrg |
| 1635 | onum nm |
| 1636 | otext tshld |
| 1637 | onum nh |
| 1638 | otext tsnr |
| 1639 | onum nn |
| 1640 | obyte 10 |
| 1641 | oflush 1 |
| 1642 | if gt np icap |
| 1643 | otext ttrunc |
| 1644 | onum np |
| 1645 | otext ttrunc2 |
| 1646 | onum icap |
| 1647 | obyte 10 |
| 1648 | oflush 1 |
| 1649 | ret 2 |
| 1650 | end |
| 1651 | ret 0 |
| 1652 | end |
| 1653 | |
| 1654 | note ---- gate: run a gate and its negative control in fresh clones and write the receipt ---- |
| 1655 | |
| 1656 | note SHA-256 (FIPS 180-4), bytes fed one at a time: state hs, schedule hw, constants hk, block hb. |
| 1657 | note 32-bit words are kept in ulong and masked to 32 bits after every addition. |
| 1658 | fn shinit to void |
| 1659 | put hk 0 1116352408 |
| 1660 | put hk 1 1899447441 |
| 1661 | put hk 2 3049323471 |
| 1662 | put hk 3 3921009573 |
| 1663 | put hk 4 961987163 |
| 1664 | put hk 5 1508970993 |
| 1665 | put hk 6 2453635748 |
| 1666 | put hk 7 2870763221 |
| 1667 | put hk 8 3624381080 |
| 1668 | put hk 9 310598401 |
| 1669 | put hk 10 607225278 |
| 1670 | put hk 11 1426881987 |
| 1671 | put hk 12 1925078388 |
| 1672 | put hk 13 2162078206 |
| 1673 | put hk 14 2614888103 |
| 1674 | put hk 15 3248222580 |
| 1675 | put hk 16 3835390401 |
| 1676 | put hk 17 4022224774 |
| 1677 | put hk 18 264347078 |
| 1678 | put hk 19 604807628 |
| 1679 | put hk 20 770255983 |
| 1680 | put hk 21 1249150122 |
| 1681 | put hk 22 1555081692 |
| 1682 | put hk 23 1996064986 |
| 1683 | put hk 24 2554220882 |
| 1684 | put hk 25 2821834349 |
| 1685 | put hk 26 2952996808 |
| 1686 | put hk 27 3210313671 |
| 1687 | put hk 28 3336571891 |
| 1688 | put hk 29 3584528711 |
| 1689 | put hk 30 113926993 |
| 1690 | put hk 31 338241895 |
| 1691 | put hk 32 666307205 |
| 1692 | put hk 33 773529912 |
| 1693 | put hk 34 1294757372 |
| 1694 | put hk 35 1396182291 |
| 1695 | put hk 36 1695183700 |
| 1696 | put hk 37 1986661051 |
| 1697 | put hk 38 2177026350 |
| 1698 | put hk 39 2456956037 |
| 1699 | put hk 40 2730485921 |
| 1700 | put hk 41 2820302411 |
| 1701 | put hk 42 3259730800 |
| 1702 | put hk 43 3345764771 |
| 1703 | put hk 44 3516065817 |
| 1704 | put hk 45 3600352804 |
| 1705 | put hk 46 4094571909 |
| 1706 | put hk 47 275423344 |
| 1707 | put hk 48 430227734 |
| 1708 | put hk 49 506948616 |
| 1709 | put hk 50 659060556 |
| 1710 | put hk 51 883997877 |
| 1711 | put hk 52 958139571 |
| 1712 | put hk 53 1322822218 |
| 1713 | put hk 54 1537002063 |
| 1714 | put hk 55 1747873779 |
| 1715 | put hk 56 1955562222 |
| 1716 | put hk 57 2024104815 |
| 1717 | put hk 58 2227730452 |
| 1718 | put hk 59 2361852424 |
| 1719 | put hk 60 2428436474 |
| 1720 | put hk 61 2756734187 |
| 1721 | put hk 62 3204031479 |
| 1722 | put hk 63 3329325298 |
| 1723 | put hs 0 1779033703 |
| 1724 | put hs 1 3144134277 |
| 1725 | put hs 2 1013904242 |
| 1726 | put hs 3 2773480762 |
| 1727 | put hs 4 1359893119 |
| 1728 | put hs 5 2600822924 |
| 1729 | put hs 6 528734635 |
| 1730 | put hs 7 1541459225 |
| 1731 | put g 43 0 |
| 1732 | put g 44 0 |
| 1733 | end |
| 1734 | |
| 1735 | note shsched: the 64-word message schedule of the block in hb |
| 1736 | fn shsched to void |
| 1737 | i = ulong 0 |
| 1738 | while lt i 16 bound 16 |
| 1739 | j = mul i 4 |
| 1740 | w = or shl uint get hb j 24 shl uint get hb add j 1 16 |
| 1741 | w = or w or shl uint get hb add j 2 8 uint get hb add j 3 |
| 1742 | put hw i w |
| 1743 | i = add i 1 |
| 1744 | end |
| 1745 | while lt i 64 bound 48 |
| 1746 | x = get hw sub i 15 |
| 1747 | y = get hw sub i 2 |
| 1748 | s0 = xor xor rotr x 7 rotr x 18 shr x 3 |
| 1749 | s1 = xor xor rotr y 17 rotr y 19 shr y 10 |
| 1750 | x = add get hw sub i 16 s0 |
| 1751 | y = add get hw sub i 7 s1 |
| 1752 | put hw i add x y |
| 1753 | i = add i 1 |
| 1754 | end |
| 1755 | end |
| 1756 | |
| 1757 | note shblk: compress the block in hb into hs; the working variables a..h are hv 0..7 |
| 1758 | fn shblk to void |
| 1759 | shsched |
| 1760 | j = ulong 0 |
| 1761 | while lt j 8 bound 8 |
| 1762 | put hv j get hs j |
| 1763 | j = add j 1 |
| 1764 | end |
| 1765 | i = ulong 0 |
| 1766 | while lt i 64 bound 64 |
| 1767 | e = get hv 4 |
| 1768 | t = xor rotr e 6 rotr e 11 |
| 1769 | t = add get hv 7 xor t rotr e 25 |
| 1770 | u = and xor e 4294967295 get hv 6 |
| 1771 | t = add t xor and e get hv 5 u |
| 1772 | t = add t get hk i |
| 1773 | t = add t get hw i |
| 1774 | a = get hv 0 |
| 1775 | u = xor rotr a 2 rotr a 13 |
| 1776 | u = xor u rotr a 22 |
| 1777 | w = xor and a get hv 1 and a get hv 2 |
| 1778 | w = xor w and get hv 1 get hv 2 |
| 1779 | u = add u w |
| 1780 | put hv 7 get hv 6 |
| 1781 | put hv 6 get hv 5 |
| 1782 | put hv 5 e |
| 1783 | put hv 4 add get hv 3 t |
| 1784 | put hv 3 get hv 2 |
| 1785 | put hv 2 get hv 1 |
| 1786 | put hv 1 a |
| 1787 | put hv 0 add t u |
| 1788 | i = add i 1 |
| 1789 | end |
| 1790 | j = ulong 0 |
| 1791 | while lt j 8 bound 8 |
| 1792 | put hs j add get hs j get hv j |
| 1793 | j = add j 1 |
| 1794 | end |
| 1795 | end |
| 1796 | |
| 1797 | fn shbyte c uchar to void |
| 1798 | n = get g 43 |
| 1799 | put hb n c |
| 1800 | n = add n 1 |
| 1801 | if eq n 64 |
| 1802 | shblk |
| 1803 | n = ulong 0 |
| 1804 | end |
| 1805 | put g 43 n |
| 1806 | put g 44 add get g 44 1 |
| 1807 | end |
| 1808 | |
| 1809 | fn hexd v ulong to uchar |
| 1810 | if lt v 10 |
| 1811 | ret uchar add 48 v |
| 1812 | end |
| 1813 | ret uchar add 87 v |
| 1814 | end |
| 1815 | |
| 1816 | note shfin: pad, then write the digest as 64 lowercase hex digits to m[dst..dst+64) |
| 1817 | fn shfin dst ulong to void |
| 1818 | bits = mul get g 44 8 |
| 1819 | shbyte 128 |
| 1820 | while ne get g 43 56 bound 64 |
| 1821 | shbyte 0 |
| 1822 | end |
| 1823 | i = ulong 0 |
| 1824 | while lt i 8 bound 8 |
| 1825 | shbyte uchar and shr bits mul sub 7 i 8 255 |
| 1826 | i = add i 1 |
| 1827 | end |
| 1828 | i = ulong 0 |
| 1829 | while lt i 32 bound 32 |
| 1830 | v = ulong get hs div i 4 |
| 1831 | v = and shr v mul sub 3 rem i 4 8 255 |
| 1832 | put m add dst mul i 2 hexd shr v 4 |
| 1833 | put m add dst add mul i 2 1 hexd and v 15 |
| 1834 | i = add i 1 |
| 1835 | end |
| 1836 | end |
| 1837 | |
| 1838 | note shfd: SHA-256 of what is left to read on fd as hex at m[dst..dst+64), also copying every chunk |
| 1839 | note to fd wf when wf >= 0; false on a read error or a short write |
| 1840 | fn shfd fd int wf long dst ulong to bool |
| 1841 | shinit |
| 1842 | c = true |
| 1843 | ok = true |
| 1844 | while c bound 1048576 |
| 1845 | r = read fd m kbuf bcap |
| 1846 | if lt r 0 |
| 1847 | ok = false |
| 1848 | c = false |
| 1849 | else |
| 1850 | if eq r 0 |
| 1851 | c = false |
| 1852 | else |
| 1853 | i = ulong 0 |
| 1854 | while lt i ulong r bound 65536 |
| 1855 | shbyte get m add kbuf i |
| 1856 | i = add i 1 |
| 1857 | end |
| 1858 | if ge wf 0 |
| 1859 | w = write int wf m kbuf ulong r |
| 1860 | if ne w r |
| 1861 | ok = false |
| 1862 | c = false |
| 1863 | end |
| 1864 | end |
| 1865 | end |
| 1866 | end |
| 1867 | end |
| 1868 | if eq ok false |
| 1869 | ret false |
| 1870 | end |
| 1871 | shfin dst |
| 1872 | ret true |
| 1873 | end |
| 1874 | |
| 1875 | note shfile: SHA-256 of the file at m[kp..kp+pn) as hex at m[dst..dst+64); false when unreadable |
| 1876 | fn shfile pn ulong dst ulong to bool |
| 1877 | fl = fopen m kp pn 524288 0 |
| 1878 | if lt fl 0 |
| 1879 | ret false |
| 1880 | end |
| 1881 | ok = shfd int fl long -1 dst |
| 1882 | x = fclose int fl |
| 1883 | ret ok |
| 1884 | end |
| 1885 | |
| 1886 | fn o2 v ulong to void |
| 1887 | obyte uchar add 48 div v 10 |
| 1888 | obyte uchar add 48 rem v 10 |
| 1889 | end |
| 1890 | |
| 1891 | note oiso: epoch nanoseconds as YYYY-MM-DDTHH:MM:SSZ (UTC, civil date from days since 1970-01-01) |
| 1892 | fn odate days ulong to void |
| 1893 | z = add days 719468 |
| 1894 | era = div z 146097 |
| 1895 | doe = sub z mul era 146097 |
| 1896 | yoe = sub add sub doe div doe 1460 div doe 36524 div doe 146096 |
| 1897 | yoe = div yoe 365 |
| 1898 | y = add yoe mul era 400 |
| 1899 | doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 |
| 1900 | mp = div add mul 5 doy 2 153 |
| 1901 | d = add sub doy div add mul 153 mp 2 5 1 |
| 1902 | mo = add mp 3 |
| 1903 | if ge mp 10 |
| 1904 | mo = sub mp 9 |
| 1905 | end |
| 1906 | if le mo 2 |
| 1907 | y = add y 1 |
| 1908 | end |
| 1909 | onum y |
| 1910 | obyte 45 |
| 1911 | o2 mo |
| 1912 | obyte 45 |
| 1913 | o2 d |
| 1914 | end |
| 1915 | |
| 1916 | fn oiso ns ulong to void |
| 1917 | s = div ns 1000000000 |
| 1918 | odate div s 86400 |
| 1919 | sod = rem s 86400 |
| 1920 | obyte 84 |
| 1921 | o2 div sod 3600 |
| 1922 | obyte 58 |
| 1923 | o2 rem div sod 60 60 |
| 1924 | obyte 58 |
| 1925 | o2 rem sod 60 |
| 1926 | obyte 90 |
| 1927 | end |
| 1928 | |
| 1929 | note nameok: a repository name of 1 to 100 bytes of A-Z a-z 0-9 . _ - that does not start with "." |
| 1930 | fn nameok p ulong n ulong to bool |
| 1931 | if or lt n 1 gt n 100 |
| 1932 | ret false |
| 1933 | end |
| 1934 | if eq get m p 46 |
| 1935 | ret false |
| 1936 | end |
| 1937 | i = ulong 0 |
| 1938 | while lt i n bound 100 |
| 1939 | c = get m add p i |
| 1940 | ok = false |
| 1941 | if and ge c 48 le c 57 |
| 1942 | ok = true |
| 1943 | end |
| 1944 | if and ge c 65 le c 90 |
| 1945 | ok = true |
| 1946 | end |
| 1947 | if and ge c 97 le c 122 |
| 1948 | ok = true |
| 1949 | end |
| 1950 | if or or eq c 46 eq c 95 eq c 45 |
| 1951 | ok = true |
| 1952 | end |
| 1953 | if eq ok false |
| 1954 | ret false |
| 1955 | end |
| 1956 | i = add i 1 |
| 1957 | end |
| 1958 | ret true |
| 1959 | end |
| 1960 | |
| 1961 | note gcheck: arguments a0..a1 form one command: 1 to 40 arguments, no CR or LF in any, the first |
| 1962 | note not empty, not starting with "-" and holding no "=" (so /usr/bin/env runs it as the program), |
| 1963 | note and at most 1024 bytes joined with spaces |
| 1964 | fn gcheck a0 ulong a1 ulong to bool |
| 1965 | if or le a1 a0 gt sub a1 a0 40 |
| 1966 | ret false |
| 1967 | end |
| 1968 | t = ulong 0 |
| 1969 | i = a0 |
| 1970 | while lt i a1 bound 64 |
| 1971 | n = argread i m add kx 5120 4096 |
| 1972 | if lt n 0 |
| 1973 | ret false |
| 1974 | end |
| 1975 | q = ulong n |
| 1976 | if eq i a0 |
| 1977 | if eq q 0 |
| 1978 | ret false |
| 1979 | end |
| 1980 | if eq get m add kx 5120 45 |
| 1981 | ret false |
| 1982 | end |
| 1983 | end |
| 1984 | j = ulong 0 |
| 1985 | while lt j q bound 4096 |
| 1986 | c = get m add add kx 5120 j |
| 1987 | if or eq c 10 eq c 13 |
| 1988 | ret false |
| 1989 | end |
| 1990 | if and eq i a0 eq c 61 |
| 1991 | ret false |
| 1992 | end |
| 1993 | j = add j 1 |
| 1994 | end |
| 1995 | t = add add t q 1 |
| 1996 | i = add i 1 |
| 1997 | end |
| 1998 | ret le t 1025 |
| 1999 | end |
| 2000 | |
| 2001 | note gtmo: the --timeout value at argument j: 1 to 5 decimal digits, 1 to 86400 seconds; 0 when bad |
| 2002 | fn gtmo j ulong to ulong |
| 2003 | n = argread j m kbuf 8 |
| 2004 | if or lt n 1 gt n 5 |
| 2005 | ret 0 |
| 2006 | end |
| 2007 | v = ulong 0 |
| 2008 | i = ulong 0 |
| 2009 | while lt i ulong n bound 8 |
| 2010 | c = get m add kbuf i |
| 2011 | if or lt c 48 gt c 57 |
| 2012 | ret 0 |
| 2013 | end |
| 2014 | v = add mul v 10 ulong sub c 48 |
| 2015 | i = add i 1 |
| 2016 | end |
| 2017 | if gt v 86400 |
| 2018 | ret 0 |
| 2019 | end |
| 2020 | ret v |
| 2021 | end |
| 2022 | |
| 2023 | note gopt: the option at argument i and its value; return 0, or 1 usage, 4 bad author, 5 bad name, |
| 2024 | note 6 bad timeout |
| 2025 | fn gopt i ulong to int |
| 2026 | j = add i 1 |
| 2027 | if argis i tfname |
| 2028 | if gt get g 33 0 |
| 2029 | ret 1 |
| 2030 | end |
| 2031 | n = argread j m knm 101 |
| 2032 | if or lt n 1 gt n 100 |
| 2033 | ret 5 |
| 2034 | end |
| 2035 | put g 33 ulong n |
| 2036 | ret 0 |
| 2037 | end |
| 2038 | if argis i tfout |
| 2039 | if gt get g 34 0 |
| 2040 | ret 1 |
| 2041 | end |
| 2042 | n = argread j m kod 1024 |
| 2043 | if lt n 1 |
| 2044 | ret 1 |
| 2045 | end |
| 2046 | put g 34 ulong n |
| 2047 | ret 0 |
| 2048 | end |
| 2049 | if argis i tfauth |
| 2050 | if gt get g 35 0 |
| 2051 | ret 1 |
| 2052 | end |
| 2053 | n = argread j m kau 257 |
| 2054 | if or lt n 1 gt n 256 |
| 2055 | ret 4 |
| 2056 | end |
| 2057 | if ne txtchk kau add kau ulong n 0 |
| 2058 | ret 4 |
| 2059 | end |
| 2060 | put g 35 ulong n |
| 2061 | ret 0 |
| 2062 | end |
| 2063 | if argis i tftmo |
| 2064 | if gt get g 55 0 |
| 2065 | ret 1 |
| 2066 | end |
| 2067 | v = gtmo j |
| 2068 | if eq v 0 |
| 2069 | ret 6 |
| 2070 | end |
| 2071 | put g 54 v |
| 2072 | put g 55 1 |
| 2073 | ret 0 |
| 2074 | end |
| 2075 | ret 1 |
| 2076 | end |
| 2077 | |
| 2078 | note gdd: index of the first "--" after the --neg arguments start, or 0 |
| 2079 | fn gdd to ulong |
| 2080 | a = ulong argcnt |
| 2081 | j = get g 36 |
| 2082 | while lt j a bound 128 |
| 2083 | if argis j tdd |
| 2084 | ret j |
| 2085 | end |
| 2086 | j = add j 1 |
| 2087 | end |
| 2088 | ret 0 |
| 2089 | end |
| 2090 | |
| 2091 | note gargs: parse "gate RID HEAD [--name N] [--out DIR] [--timeout S] --author A --neg ARGV.. -- ARGV..". |
| 2092 | note Return 0, or 1 usage, 2 bad head, 3 bad RID, 4 bad arguments, 5 bad name, 6 bad timeout. |
| 2093 | fn gargs to int |
| 2094 | a = ulong argcnt |
| 2095 | if lt a 10 |
| 2096 | ret 1 |
| 2097 | end |
| 2098 | if gt a 90 |
| 2099 | ret 4 |
| 2100 | end |
| 2101 | rl = argread 2 m krid 128 |
| 2102 | if lt rl 0 |
| 2103 | ret 3 |
| 2104 | end |
| 2105 | if eq ridok krid ulong rl false |
| 2106 | ret 3 |
| 2107 | end |
| 2108 | put g 16 ulong rl |
| 2109 | hn = argread 3 m kh 64 |
| 2110 | if ne hn 40 |
| 2111 | ret 2 |
| 2112 | end |
| 2113 | if eq hex40 kh add kh 40 false |
| 2114 | ret 2 |
| 2115 | end |
| 2116 | put g 33 0 |
| 2117 | put g 34 0 |
| 2118 | put g 35 0 |
| 2119 | put g 54 1800 |
| 2120 | put g 55 0 |
| 2121 | i = ulong 4 |
| 2122 | c = true |
| 2123 | while c bound 64 |
| 2124 | if ge add i 1 a |
| 2125 | ret 1 |
| 2126 | end |
| 2127 | if argis i tfneg |
| 2128 | put g 36 add i 1 |
| 2129 | c = false |
| 2130 | else |
| 2131 | r = gopt i |
| 2132 | if ne r 0 |
| 2133 | ret r |
| 2134 | end |
| 2135 | i = add i 2 |
| 2136 | end |
| 2137 | end |
| 2138 | k = gdd |
| 2139 | if eq k 0 |
| 2140 | ret 1 |
| 2141 | end |
| 2142 | put g 37 k |
| 2143 | if eq get g 35 0 |
| 2144 | ret 1 |
| 2145 | end |
| 2146 | if eq gcheck get g 36 k false |
| 2147 | ret 4 |
| 2148 | end |
| 2149 | if eq gcheck add k 1 a false |
| 2150 | ret 4 |
| 2151 | end |
| 2152 | if gt get g 33 0 |
| 2153 | if eq nameok knm get g 33 false |
| 2154 | ret 5 |
| 2155 | end |
| 2156 | end |
| 2157 | ret 0 |
| 2158 | end |
| 2159 | |
| 2160 | note genv: HOME, PATH, OMC, RAD_HOME, the storage path, the default out dir and the omc digest. |
| 2161 | note Return 0, or 1 HOME unset, 2 OMC set but unreadable. |
| 2162 | fn genv to int |
| 2163 | hl = envget thome kx 1024 |
| 2164 | if le hl 5 |
| 2165 | ret 1 |
| 2166 | end |
| 2167 | put g 19 hl |
| 2168 | pl = envget tpath add kx 1024 4096 |
| 2169 | if eq pl 0 |
| 2170 | pl = tcpy add kx 1024 tdpath |
| 2171 | end |
| 2172 | put g 21 pl |
| 2173 | put g 38 envget tomce kom 2048 |
| 2174 | put g 39 envget tradh krh 1024 |
| 2175 | n = ulong 0 |
| 2176 | if gt get g 39 9 |
| 2177 | n = sub get g 39 9 |
| 2178 | cpy kst add krh 9 n |
| 2179 | n = add n tcpy add kst n tstor |
| 2180 | else |
| 2181 | n = sub hl 5 |
| 2182 | cpy kst add kx 5 n |
| 2183 | n = add n tcpy add kst n tdotrad |
| 2184 | n = add n tcpy add kst n tstor |
| 2185 | end |
| 2186 | cpy add kst n add krid 4 sub get g 16 4 |
| 2187 | put g 42 add n sub get g 16 4 |
| 2188 | if eq get g 34 0 |
| 2189 | n = sub hl 5 |
| 2190 | cpy kod add kx 5 n |
| 2191 | n = add n tcpy add kod n trcpd |
| 2192 | put g 34 n |
| 2193 | x = makedir m kod n 493 |
| 2194 | end |
| 2195 | if gt get g 38 4 |
| 2196 | n = sub get g 38 4 |
| 2197 | cpy kp add kom 4 n |
| 2198 | if eq shfile n kos false |
| 2199 | ret 2 |
| 2200 | end |
| 2201 | end |
| 2202 | ret 0 |
| 2203 | end |
| 2204 | |
| 2205 | fn envpk to void |
| 2206 | pkinit |
| 2207 | pktext tenv |
| 2208 | pkadd kx get g 19 |
| 2209 | pkadd add kx 1024 get g 21 |
| 2210 | end |
| 2211 | |
| 2212 | note gname: the repository name from --name, or from "rad inspect RID --payload" ("name": "...") |
| 2213 | fn gname to bool |
| 2214 | if gt get g 33 0 |
| 2215 | ret true |
| 2216 | end |
| 2217 | envpk |
| 2218 | if gt get g 39 0 |
| 2219 | pkadd krh get g 39 |
| 2220 | end |
| 2221 | pktext trad |
| 2222 | pktext tinspect |
| 2223 | pkadd krid get g 16 |
| 2224 | pktext tpayload |
| 2225 | r = runcmd pkdone |
| 2226 | if ne r 0 |
| 2227 | ret false |
| 2228 | end |
| 2229 | n = add ko get g 13 |
| 2230 | p = ko |
| 2231 | while lt p n bound 524288 |
| 2232 | if mat p n tnamek |
| 2233 | q = add p len tnamek |
| 2234 | e = q |
| 2235 | while and lt e n ne get m e 34 bound 4096 |
| 2236 | e = add e 1 |
| 2237 | end |
| 2238 | if or ge e n gt sub e q 100 |
| 2239 | ret false |
| 2240 | end |
| 2241 | cpy knm q sub e q |
| 2242 | put g 33 sub e q |
| 2243 | ret nameok knm sub e q |
| 2244 | end |
| 2245 | p = add p 1 |
| 2246 | end |
| 2247 | ret false |
| 2248 | end |
| 2249 | |
| 2250 | note gtmp: create /tmp/forge-gate-<pid>-<16 random hex> with mode 0700 at kt, length in g 40 |
| 2251 | fn gtmp to bool |
| 2252 | put g 40 0 |
| 2253 | n = tcpy kt ttmp |
| 2254 | put g 0 0 |
| 2255 | onum ulong getpid |
| 2256 | obyte 45 |
| 2257 | r = random m add kx 5120 8 0 |
| 2258 | if ne r 8 |
| 2259 | put g 0 0 |
| 2260 | ret false |
| 2261 | end |
| 2262 | i = ulong 0 |
| 2263 | while lt i 8 bound 8 |
| 2264 | v = ulong get m add add kx 5120 i |
| 2265 | obyte hexd shr v 4 |
| 2266 | obyte hexd and v 15 |
| 2267 | i = add i 1 |
| 2268 | end |
| 2269 | cpy add kt n kw get g 0 |
| 2270 | n = add n get g 0 |
| 2271 | put g 0 0 |
| 2272 | x = makedir m kt n 448 |
| 2273 | if ne x 0 |
| 2274 | ret false |
| 2275 | end |
| 2276 | put g 40 n |
| 2277 | ret true |
| 2278 | end |
| 2279 | |
| 2280 | note grm: remove the temp dir (only a path made by gtmp) |
| 2281 | fn grm to void |
| 2282 | if gt get g 40 16 |
| 2283 | pkinit |
| 2284 | pktext trm |
| 2285 | pktext trf |
| 2286 | pkadd kt get g 40 |
| 2287 | x = runcmd pkdone |
| 2288 | end |
| 2289 | end |
| 2290 | |
| 2291 | note cdir: m[kc..) = temp dir + t; return its length |
| 2292 | fn cdir t view uchar * to ulong |
| 2293 | n = get g 40 |
| 2294 | cpy kc kt n |
| 2295 | ret add n tcpy add kc n t |
| 2296 | end |
| 2297 | |
| 2298 | fn gitpk to void |
| 2299 | envpk |
| 2300 | pktext tgnos |
| 2301 | pktext tgglob |
| 2302 | pktext tgit |
| 2303 | end |
| 2304 | |
| 2305 | note gclone: a fresh clone of the head from the Radicle storage into m[kc..kc+dn); true when |
| 2306 | note every git step exits 0 and the clone's HEAD is exactly the head |
| 2307 | fn gclone dn ulong to bool |
| 2308 | gitpk |
| 2309 | pktext tinit |
| 2310 | pktext tq |
| 2311 | pkadd kc dn |
| 2312 | if ne runcmd pkdone 0 |
| 2313 | ret false |
| 2314 | end |
| 2315 | gitpk |
| 2316 | pktext tdc |
| 2317 | pkadd kc dn |
| 2318 | pktext tfetch |
| 2319 | pktext tq |
| 2320 | pktext tnotags |
| 2321 | pkadd kst get g 42 |
| 2322 | pkadd kh 40 |
| 2323 | if ne runcmd pkdone 0 |
| 2324 | ret false |
| 2325 | end |
| 2326 | gitpk |
| 2327 | pktext tdc |
| 2328 | pkadd kc dn |
| 2329 | pktext tcfg |
| 2330 | pktext tadv |
| 2331 | pktext tcheck |
| 2332 | pktext tq |
| 2333 | pktext tdetach |
| 2334 | pkadd kh 40 |
| 2335 | if ne runcmd pkdone 0 |
| 2336 | ret false |
| 2337 | end |
| 2338 | gitpk |
| 2339 | pktext tdc |
| 2340 | pkadd kc dn |
| 2341 | pktext trevp |
| 2342 | pktext theadw |
| 2343 | if ne runcmd pkdone 0 |
| 2344 | ret false |
| 2345 | end |
| 2346 | if ne get g 13 41 |
| 2347 | ret false |
| 2348 | end |
| 2349 | ret eqr ko kh 40 |
| 2350 | end |
| 2351 | |
| 2352 | note gtree: the tree id of the clone at m[kc..kc+dn) into ktr |
| 2353 | fn gtree dn ulong to bool |
| 2354 | gitpk |
| 2355 | pktext tdc |
| 2356 | pkadd kc dn |
| 2357 | pktext trevp |
| 2358 | pktext ttreew |
| 2359 | if ne runcmd pkdone 0 |
| 2360 | ret false |
| 2361 | end |
| 2362 | if ne get g 13 41 |
| 2363 | ret false |
| 2364 | end |
| 2365 | if eq hex40 ko add ko 40 false |
| 2366 | ret false |
| 2367 | end |
| 2368 | cpy ktr ko 40 |
| 2369 | ret true |
| 2370 | end |
| 2371 | |
| 2372 | note opath: m[kp..) = out dir "/" name "-" head8 t; return its length |
| 2373 | fn opath t view uchar * to ulong |
| 2374 | n = get g 34 |
| 2375 | cpy kp kod n |
| 2376 | put m add kp n 47 |
| 2377 | n = add n 1 |
| 2378 | cpy add kp n knm get g 33 |
| 2379 | n = add n get g 33 |
| 2380 | put m add kp n 45 |
| 2381 | cpy add kp add n 1 kh 8 |
| 2382 | n = add n 9 |
| 2383 | ret add n tcpy add kp n t |
| 2384 | end |
| 2385 | |
| 2386 | note tmpath: m[kp..) = temp dir + t; return its length |
| 2387 | fn tmpath t view uchar * to ulong |
| 2388 | n = get g 40 |
| 2389 | cpy kp kt n |
| 2390 | ret add n tcpy add kp n t |
| 2391 | end |
| 2392 | |
| 2393 | note gmklog: create the log temp dir + t with 557249 = O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, |
| 2394 | note mode 0600, open it again with 557056 = O_RDONLY|O_NOFOLLOW|O_CLOEXEC, and require both fds to |
| 2395 | note be the same file (st_dev, st_ino). Write fd to g s, read fd to g s+1: the command writes through |
| 2396 | note the first and forge hashes and copies through the second, so a name swapped later changes |
| 2397 | note nothing. Flag values are aarch64 Linux, the only target the pinned omc emits. |
| 2398 | fn gmklog t view uchar * s ulong to bool |
| 2399 | n = tmpath t |
| 2400 | fl = fopen m kp n 557249 384 |
| 2401 | if lt fl 0 |
| 2402 | ret false |
| 2403 | end |
| 2404 | rl = fopen m kp n 557056 0 |
| 2405 | if lt rl 0 |
| 2406 | ret false |
| 2407 | end |
| 2408 | if ne fstat int fl m kbuf 128 0 |
| 2409 | ret false |
| 2410 | end |
| 2411 | if ne fstat int rl m add kbuf 128 128 0 |
| 2412 | ret false |
| 2413 | end |
| 2414 | if eq eqr kbuf add kbuf 128 16 false |
| 2415 | ret false |
| 2416 | end |
| 2417 | put g s ulong fl |
| 2418 | put g add s 1 ulong rl |
| 2419 | ret true |
| 2420 | end |
| 2421 | |
| 2422 | note gfree: true when nothing (file or symlink) is at the out path for suffix t yet; forge never |
| 2423 | note replaces an out file, and the O_EXCL create at the end refuses one that appears meanwhile |
| 2424 | fn gfree t view uchar * to bool |
| 2425 | n = opath t |
| 2426 | r = lstat m kp n m kbuf 128 |
| 2427 | ret lt r 0 |
| 2428 | end |
| 2429 | |
| 2430 | note gcopy: create the out file for suffix t (557249, mode 0644 before umask), then hash the log |
| 2431 | note behind read fd g s into dst while copying it there; g 53 counts the out files created |
| 2432 | fn gcopy t view uchar * s ulong dst ulong to bool |
| 2433 | n = opath t |
| 2434 | fl = fopen m kp n 557249 420 |
| 2435 | if lt fl 0 |
| 2436 | ret false |
| 2437 | end |
| 2438 | put g 53 add get g 53 1 |
| 2439 | ok = shfd int get g s long fl dst |
| 2440 | x = fclose int fl |
| 2441 | ret ok |
| 2442 | end |
| 2443 | |
| 2444 | note gunl: remove the out files this run created, in creation order (gate log, negctl log, receipt) |
| 2445 | fn gunl to void |
| 2446 | c = get g 53 |
| 2447 | if gt c 0 |
| 2448 | n = opath tglog |
| 2449 | x = unlink m kp n |
| 2450 | end |
| 2451 | if gt c 1 |
| 2452 | n = opath tnlog |
| 2453 | x = unlink m kp n |
| 2454 | end |
| 2455 | if gt c 2 |
| 2456 | n = opath trsuf |
| 2457 | x = unlink m kp n |
| 2458 | end |
| 2459 | put g 53 0 |
| 2460 | end |
| 2461 | |
| 2462 | note pkarg: append argument i to the exec packet |
| 2463 | fn pkarg i ulong to void |
| 2464 | w = get g 30 |
| 2465 | if gt add w 2 ecap |
| 2466 | trap 12 |
| 2467 | end |
| 2468 | n = argread i m add ke add w 2 sub ecap add w 2 |
| 2469 | if lt n 0 |
| 2470 | trap 12 |
| 2471 | end |
| 2472 | put m add ke w uchar and ulong n 255 |
| 2473 | put m add ke add w 1 uchar shr ulong n 8 |
| 2474 | put g 30 add add w 2 ulong n |
| 2475 | put g 31 add get g 31 1 |
| 2476 | end |
| 2477 | |
| 2478 | note grun: run arguments a0..a1 through /usr/bin/env (HOME, PATH and OMC when set) in the clone |
| 2479 | note m[kc..kc+dn), in a new process group, stdin /dev/null, stdout and stderr on the open log fd lf |
| 2480 | note (duplicated, never reopened by name); return gwait's result for the child |
| 2481 | fn grun a0 ulong a1 ulong dn ulong lf ulong to long |
| 2482 | envpk |
| 2483 | if gt get g 38 0 |
| 2484 | pkadd kom get g 38 |
| 2485 | end |
| 2486 | i = a0 |
| 2487 | while lt i a1 bound 64 |
| 2488 | pkarg i |
| 2489 | i = add i 1 |
| 2490 | end |
| 2491 | pn = pkdone |
| 2492 | pid = fork |
| 2493 | if eq pid 0 |
| 2494 | r = setpgid 0 0 |
| 2495 | if lt r 0 |
| 2496 | exit 126 |
| 2497 | end |
| 2498 | r = chdir m kc dn |
| 2499 | if lt r 0 |
| 2500 | exit 126 |
| 2501 | end |
| 2502 | y = dupfd int lf 1 0 |
| 2503 | if lt y 0 |
| 2504 | exit 126 |
| 2505 | end |
| 2506 | y = dupfd int lf 2 0 |
| 2507 | if lt y 0 |
| 2508 | exit 126 |
| 2509 | end |
| 2510 | nf = fopen tdevnull 0 len tdevnull 0 0 |
| 2511 | if lt nf 0 |
| 2512 | exit 126 |
| 2513 | end |
| 2514 | y = dupfd int nf 0 0 |
| 2515 | y = execprog m ke pn |
| 2516 | exit 127 |
| 2517 | end |
| 2518 | if lt pid 0 |
| 2519 | ret pid |
| 2520 | end |
| 2521 | ret gwait pid lf |
| 2522 | end |
| 2523 | |
| 2524 | note gtmlog: append the timeout line to the log fd lf, so it is in the hashed log |
| 2525 | fn gtmlog lf ulong to void |
| 2526 | put g 0 0 |
| 2527 | otext ttmo1 |
| 2528 | onum get g 54 |
| 2529 | otext ttmo2 |
| 2530 | oflush int lf |
| 2531 | end |
| 2532 | |
| 2533 | note gwait: put the child pid in its own process group (the child does the same before chdir, so |
| 2534 | note there is no race), then poll it every 100 ms until it exits or g 54 seconds have passed. At the |
| 2535 | note deadline send SIGKILL to the whole group (kill of -pid) and to the child by pid (a direct child |
| 2536 | note that left the group is still killed), then poll the reap every 100 ms for at most 10 s more, |
| 2537 | note append the timeout line to the log fd lf and return 124. A child not reaped in those 10 s |
| 2538 | note (uninterruptible sleep) is left to init, so forge itself stays bounded. Otherwise return the |
| 2539 | note exit status (128 + signal for a killed command) or a negative errno. A grandchild that leaves |
| 2540 | note the group (setsid, setpgid) escapes the kill. |
| 2541 | fn gwait pid long lf ulong to long |
| 2542 | st = new uchar 4 0 |
| 2543 | x = setpgid int pid int pid |
| 2544 | dl = add monons mul long get g 54 1000000000 |
| 2545 | w = long 0 |
| 2546 | while eq w 0 bound 999999 |
| 2547 | w = waitpid int pid st 0 4 1 |
| 2548 | if eq w 0 |
| 2549 | if ge monons dl |
| 2550 | x = killpid int neg pid 9 |
| 2551 | x = killpid int pid 9 |
| 2552 | dl = add monons mul long 10 1000000000 |
| 2553 | w = waitpid int pid st 0 4 1 |
| 2554 | while and eq w 0 lt monons dl bound 999 |
| 2555 | x = sleepns 100000000 |
| 2556 | w = waitpid int pid st 0 4 1 |
| 2557 | end |
| 2558 | if lt w 0 |
| 2559 | ret w |
| 2560 | end |
| 2561 | gtmlog lf |
| 2562 | ret 124 |
| 2563 | end |
| 2564 | x = sleepns 100000000 |
| 2565 | end |
| 2566 | end |
| 2567 | if lt w 0 |
| 2568 | ret w |
| 2569 | end |
| 2570 | s = or ulong get st 0 shl ulong get st 1 8 |
| 2571 | if ne and s 127 0 |
| 2572 | ret long add 128 and s 127 |
| 2573 | end |
| 2574 | ret long and shr s 8 255 |
| 2575 | end |
| 2576 | |
| 2577 | note ojoin: arguments a0..a1 joined with single spaces into the output line |
| 2578 | fn ojoin a0 ulong a1 ulong to void |
| 2579 | i = a0 |
| 2580 | while lt i a1 bound 64 |
| 2581 | if gt i a0 |
| 2582 | obyte 32 |
| 2583 | end |
| 2584 | n = argread i m add kx 5120 4096 |
| 2585 | if gt n 0 |
| 2586 | ocopy add kx 5120 ulong n |
| 2587 | end |
| 2588 | i = add i 1 |
| 2589 | end |
| 2590 | end |
| 2591 | |
| 2592 | note gwrite: build the receipt in the output line and create the out receipt with O_EXCL |
| 2593 | fn gwrite gr ulong nr ulong t0 ulong t1 ulong to bool |
| 2594 | put g 0 0 |
| 2595 | otext trch |
| 2596 | otext tridk |
| 2597 | ocopy krid get g 16 |
| 2598 | obyte 10 |
| 2599 | otext tclone |
| 2600 | ocopy kh 40 |
| 2601 | obyte 10 |
| 2602 | otext ttree |
| 2603 | ocopy ktr 40 |
| 2604 | obyte 10 |
| 2605 | otext tomck |
| 2606 | if gt get g 38 4 |
| 2607 | ocopy kos 64 |
| 2608 | else |
| 2609 | otext tnone |
| 2610 | end |
| 2611 | obyte 10 |
| 2612 | otext tgcmd |
| 2613 | ojoin add get g 37 1 ulong argcnt |
| 2614 | obyte 10 |
| 2615 | otext tgate |
| 2616 | onum gr |
| 2617 | obyte 10 |
| 2618 | otext tncmd |
| 2619 | ojoin get g 36 get g 37 |
| 2620 | obyte 10 |
| 2621 | otext tneg |
| 2622 | onum nr |
| 2623 | obyte 10 |
| 2624 | otext tauth |
| 2625 | ocopy kau get g 35 |
| 2626 | obyte 10 |
| 2627 | otext tstart |
| 2628 | oiso t0 |
| 2629 | obyte 10 |
| 2630 | otext tfin |
| 2631 | oiso t1 |
| 2632 | obyte 10 |
| 2633 | otext tlsha |
| 2634 | ocopy kgs 64 |
| 2635 | obyte 10 |
| 2636 | otext tnlsha |
| 2637 | ocopy kns 64 |
| 2638 | obyte 10 |
| 2639 | w = get g 0 |
| 2640 | put g 0 0 |
| 2641 | if ge w rcap |
| 2642 | ret false |
| 2643 | end |
| 2644 | n = opath trsuf |
| 2645 | fl = fopen m kp n 557249 420 |
| 2646 | if lt fl 0 |
| 2647 | ret false |
| 2648 | end |
| 2649 | put g 53 add get g 53 1 |
| 2650 | r = write int fl m kw w |
| 2651 | x = fclose int fl |
| 2652 | if lt r 0 |
| 2653 | ret false |
| 2654 | end |
| 2655 | ret eq ulong r w |
| 2656 | end |
| 2657 | |
| 2658 | note gboth: check the out dir and that no out name is taken, clone twice, run the gate and the |
| 2659 | note negative control with their logs in the 0700 temp dir, then create the out logs and the |
| 2660 | note receipt with O_EXCL from the open log fds; on a failure remove the out files this run created. |
| 2661 | note Return 0, or 1 clone failed, 2 write failed, 3 run failed. |
| 2662 | fn gboth to int |
| 2663 | if ne faccess m kod get g 34 3 0 |
| 2664 | ret 2 |
| 2665 | end |
| 2666 | if eq gfree tglog false |
| 2667 | ret 2 |
| 2668 | end |
| 2669 | if eq gfree tnlog false |
| 2670 | ret 2 |
| 2671 | end |
| 2672 | if eq gfree trsuf false |
| 2673 | ret 2 |
| 2674 | end |
| 2675 | dn = cdir tgdir |
| 2676 | if eq gclone dn false |
| 2677 | ret 1 |
| 2678 | end |
| 2679 | if eq gtree dn false |
| 2680 | ret 1 |
| 2681 | end |
| 2682 | dn = cdir tndir |
| 2683 | if eq gclone dn false |
| 2684 | ret 1 |
| 2685 | end |
| 2686 | if eq gmklog ttgl 49 false |
| 2687 | ret 2 |
| 2688 | end |
| 2689 | if eq gmklog ttnl 51 false |
| 2690 | ret 2 |
| 2691 | end |
| 2692 | t0 = ulong realns |
| 2693 | dn = cdir tgdir |
| 2694 | gr = grun add get g 37 1 ulong argcnt dn get g 49 |
| 2695 | if lt gr 0 |
| 2696 | ret 3 |
| 2697 | end |
| 2698 | dn = cdir tndir |
| 2699 | nr = grun get g 36 get g 37 dn get g 51 |
| 2700 | if lt nr 0 |
| 2701 | ret 3 |
| 2702 | end |
| 2703 | t1 = ulong realns |
| 2704 | put g 53 0 |
| 2705 | if eq gcopy tglog 50 kgs false |
| 2706 | gunl |
| 2707 | ret 2 |
| 2708 | end |
| 2709 | if eq gcopy tnlog 52 kns false |
| 2710 | gunl |
| 2711 | ret 2 |
| 2712 | end |
| 2713 | if eq gwrite ulong gr ulong nr t0 t1 false |
| 2714 | gunl |
| 2715 | ret 2 |
| 2716 | end |
| 2717 | put g 47 ulong gr |
| 2718 | put g 48 ulong nr |
| 2719 | ret 0 |
| 2720 | end |
| 2721 | |
| 2722 | note cgate: exit 0 when the gate passed and its negative control failed without timing out, 1 when |
| 2723 | note the receipt records anything else (it would HOLD), 2 on an error (no receipt written) |
| 2724 | fn cgate to int |
| 2725 | r = gargs |
| 2726 | if eq r 1 |
| 2727 | ret serr thelp |
| 2728 | end |
| 2729 | if eq r 2 |
| 2730 | ret serr tebadh |
| 2731 | end |
| 2732 | if eq r 3 |
| 2733 | ret serr tebad |
| 2734 | end |
| 2735 | if eq r 4 |
| 2736 | ret serr teargs |
| 2737 | end |
| 2738 | if eq r 5 |
| 2739 | ret serr tename |
| 2740 | end |
| 2741 | if eq r 6 |
| 2742 | ret serr tetmo |
| 2743 | end |
| 2744 | r = genv |
| 2745 | if eq r 1 |
| 2746 | ret serr tehome |
| 2747 | end |
| 2748 | if eq r 2 |
| 2749 | ret serr teomc |
| 2750 | end |
| 2751 | if eq gname false |
| 2752 | ret serr tename |
| 2753 | end |
| 2754 | if eq gtmp false |
| 2755 | ret serr tetmp |
| 2756 | end |
| 2757 | r = gboth |
| 2758 | grm |
| 2759 | if eq r 1 |
| 2760 | ret serr teclone |
| 2761 | end |
| 2762 | if eq r 2 |
| 2763 | ret serr teout |
| 2764 | end |
| 2765 | if eq r 3 |
| 2766 | ret serr terun |
| 2767 | end |
| 2768 | put g 0 0 |
| 2769 | otext tgo |
| 2770 | ocopy krid get g 16 |
| 2771 | otext tgoh |
| 2772 | ocopy kh 40 |
| 2773 | otext tgor |
| 2774 | onum get g 47 |
| 2775 | otext tgon |
| 2776 | onum get g 48 |
| 2777 | otext tgorc |
| 2778 | n = opath trsuf |
| 2779 | ocopy kp n |
| 2780 | obyte 10 |
| 2781 | oflush 1 |
| 2782 | if and eq get g 47 0 and ne get g 48 0 ne get g 48 124 |
| 2783 | ret 0 |
| 2784 | end |
| 2785 | ret 1 |
| 2786 | end |
| 2787 | |
| 2788 | fn main to int |
| 2789 | if lt argcnt 2 |
| 2790 | ret serr thelp |
| 2791 | end |
| 2792 | n = argread 1 m kp 16 |
| 2793 | if eq n 6 |
| 2794 | if mat kp add kp 6 tdec |
| 2795 | ret cdecide |
| 2796 | end |
| 2797 | if mat kp add kp 6 tstat |
| 2798 | ret cstatus |
| 2799 | end |
| 2800 | end |
| 2801 | if eq n 4 |
| 2802 | if mat kp add kp 4 tgatew |
| 2803 | ret cgate |
| 2804 | end |
| 2805 | end |
| 2806 | ret serr thelp |
| 2807 | end |