Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/bridge/omega-bridge/tools/run-device-window.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / bridge / omega-bridge / tools / run-device-window.om` 2380 lines ยท 45868 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note run-device-window.om: the Omega successor of tools/run-device-window.sh (Linear DRA-441). 3 note Build: "$OMC" < tools/run-device-window.om > build/run-device-window && chmod +x build/run-device-window 4 note usage: build/run-device-window --ledger ID [--dry-run] [--pairs N] 5 note It runs the same rows as the shell script, in the same order, with the same output lines, 6 note the same refusals and the same evidence files (rc.txt, window.txt, NN-*.log, NN-*.out/, 7 note NN-*.out.sha256, cubins/). The shell script's header has the cubin export prerequisite. 8 note Differences from the shell script, all deliberate and listed in tests/device-window/README.md: 9 note - the repository root is dirname(dirname(/proc/self/exe)), the physical path of the binary; 10 note - script_sha256 is the SHA-256 of the running binary (/proc/self/exe); 11 note - SHA-256 is computed here (FIPS 180-4, adapted from aien-architecture-mv 12 note tools/zero-legacy/sha256.om, streamed in 64-byte blocks); no sha256sum is run; 13 note - NN-*.out.sha256 lists names in bytewise order (the shell used the locale's collation); 14 note - Omega has no signal handler, so the close of a window opened here is done by a closer 15 note process. It is forked before the open, starts its own session and tells this program it 16 note is ready (so a Ctrl-C cannot fall between the fork and its setsid). The open's standard 17 note output goes to the closer, not to this program: the closer reads it to its end, keeps it, 18 note and a child of the closer hands a copy back (so a write to a program that is gone ends 19 note that child by SIGPIPE, never the closer). The closer then reads a pipe whose only write 20 note end this program holds: on every exit of its own this program writes N there (a window was 21 note opened here) or X (none was) and closes it, and the closer runs `spark-window close ` 22 note at once with the id it kept (the shell's trap EXIT), or exits on X. Every child started 23 note after the closer gets, as its standard input, the write end of a second "hold" pipe. When 24 note the pipe ends with neither N nor X (this program was killed: SIGTERM, Ctrl-C, kill -9, also 25 note during the open), the closer reads the hold pipe to its end, which comes only when every 26 note row has exited, and closes the window then: a window is never closed while a row of it 27 note still runs on the chip. The shell script closed after the row on SIGTERM and never closed 28 note on kill -9 (the lease ran out). A row's standard input is that write-only pipe, not the 29 note terminal. On a signal this program dies by that signal (status 128+N in a shell) where the 30 note shell script exited 130. If the closer itself is killed from outside, the window stays 31 note open until its lease ends, as in the shell after kill -9; this program then says so on 32 note stderr and in window.txt, naming the window. The closer is checked just before N is 33 note written: a closer that dies in the microseconds between that check and the write still 34 note ends this program by SIGPIPE, with the same result (the window is left to its lease); 35 note - every child (spark-window, git, the torch probe) runs as /usr/bin/env -i -- CMD.., 36 note because execprog starts a program with an empty environment and no PATH search, and 37 note execenv is capped at 64 entries and 4096 bytes (the environment on spark-b87b had 78 38 note entries and 4796 bytes on 2026-11-16). /usr/bin/env is a host dependency: GNU coreutils 39 note 9.4 on spark-b87b, sha256 `d38fa7db8452a564868961fd5e9a0aa787095676b0fd6e497b766ac40ec65c8b`. 40 note Its Omega replacement is an execenv whose environment packet is sized from its input, the 41 note compiler extension proposed in evidence/device-window/DRA-441/execenv-proposal.md. 42 note Environment entries with no '=' or a leading '=' are not passed on (no shell exports them); 43 note - an argument is read whole up to the kernel's MAX_ARG_STRLEN (131072 bytes with its zero); 44 note - a command whose output is a value (the window id, a git head) and prints more than 65536 45 note bytes is refused, and a window id must be 1..4095 bytes of [A-Za-z0-9._-]; otherwise this 46 note program refuses with exit 3 and the closer closes nothing, so that window is left to its 47 note lease (the shell would have run `spark-window close ""`); 48 note - a value or path longer than 4095 bytes is refused (the Linux path limit); the CUDA_VISIBLE_DEVICES 49 note and SPARK_WINDOW refusals of --dry-run quote the value whole, as the shell does. These checks 50 note come first, so the order of refusals differs from the shell in two cases, both still refused 51 note before anything reaches the GPU: an environment entry over 65535 bytes (exit 3, this 52 note program's message, where the shell gives its CUDA_VISIBLE_DEVICES/SPARK_WINDOW refusal), and 53 note a CUDAPP, CUBIN_DIR, CUDAPP_TORCH_LIB or PYTHON over 4095 bytes in --dry-run (exit 2, path 54 note limit, where the shell may first give its exit-3 dry-run refusal); 55 note - before anything is printed or opened, the packets of the open, each row and the close are 56 note built once, so an environment that does not fit the packet (65536 bytes and 1024 records, 57 note each record at most 65535 bytes: the compiler's execprog packet) is refused at once with 58 note exit 3, never in the middle of a window. The git and dry-run python packets are not built 59 note then: each holds the same environment and arguments shorter than a row's (the torch row 60 note names the checkout path at least twice), so a run that passes preflight fits them too. 61 text tb "/usr/bin/env\n-i\n--\nspark-window\nrun\nopen\nclose\n--ledger\n--dry-run\n--pairs\nREFUSE: --ledger needs an ID\nREFUSE: --pairs needs a value\nunknown \nREFUSE: no ledger id; usage: run-device-window --ledger ID [--dry-run] [--pairs N]\nREFUSE: ledger id must match [A-Za-z0-9._-]+\nREFUSE: --pairs must be an integer\nREFUSE: --pairs \n < 5\n is out of range\nREFUSE: a value or path is longer than 4095 bytes (the Linux path limit)\nPAIRS=\nCUDAPP=\nCUDAPP_TORCH_LIB=\nCUBIN_DIR=\nPYTHON=\nMAXPY=\nHOME=\nCUDA_VISIBLE_DEVICES=\nSPARK_WINDOW=\n5\n/proc/self/exe\n/proc/sys/kernel/hostname\n/dev/null\n/../cudapp\n/attach/torch/libcudapp_attach.so\n/evidence/device-window/\n/evidence/device-window\nmaxinterop-device-\n/build/window-cubins\n/matmul-16x16x8.cubin\n/attention-hd128-gqa8.cubin\n/evidence/MAX-INTEROP/68/matmul-16x16x8.cubin\n/conformance/build\npython3\n/build/cubin_probe\n/build/libomega_gpu.a\nomega_matmul_bf16\n16\n8\n--load-only\nomega_gqa_attention_causal\n2\n1\n--expect-reject\n/conformance/run.sh\n/conformance/max_runner.py\n--device\nenv\nPYTHONPATH=\n/attach/torch\n/attach/torch/test_cudapp_torch.py\n/torch-receipt.json\ncubin-matmul\ncubin-gqa-load\ncubin-stale-reject\nconformance-device\npair\n-conformance-device\ntorch-cudapp\nPLAN \n: spark-window run \nREFUSE: dry-run requires CUDA_VISIBLE_DEVICES set and empty (got '\n')\nunset\nREFUSE: dry-run inside an open SPARK_WINDOW=\nDRY-RUN ledger=\n window=\n pairs=\n out=evidence/device-window/\nWOULD: spark-window open \n --ledger \n --gb10 exclusive --quiet none --lease 20 --wait\nINPUTS\nOK \nMISSING \n/workspace/maxinterop/venv/bin/python\nrun-device-window: HOME: parameter not set\n-c\nimport importlib.util, sys; sys.exit(importlib.util.find_spec(\"torch\") is None)\nOK torch importable by PYTHON=\nMISSING torch for PYTHON=\n (set PYTHON to an interpreter with torch)\nEXISTS \n (device mode refuses to overwrite evidence)\nROWS \n (3 cubin + 1 conformance + \n paired + 1 torch); GPU rows NOT_RUN\nDRY-RUN INCOMPLETE: build missing inputs before the window\nDRY-RUN PASS: plan complete, no device opened\nREFUSE: \n exists; evidence is never overwritten, use a new --ledger id\nREFUSE: cannot create \n/rc.txt\n/window.txt\n--gb10\nexclusive\n--quiet\nnone\n--lease\n20\n--wait\nREFUSE: spark-window open failed; no row ran\nwindow_id \nopened_here \nhost \nstart_utc \nbridge_head \nbridge_dirty \ncudapp_head \nscript_sha256 \ninput_sha256 \npairs \nend_utc \nunknown\ngit\n-C\nrev-parse\nHEAD\nstatus\n--porcelain\n--untracked-files=no\n/cubins\nmatmul-16x16x8.\nattention-hd128-gqa8.\nomega-gpu-\nmax-gpu-\n*\ncp: cannot stat '\n*': No such file or directory\n.out\n.out.sha256\n.log\n rc=\nDEVICE WINDOW \n: all \n rows rc=0\n: FAIL (see \n)\n \nrun-device-window: cannot open \nrun-device-window: cannot exec /usr/bin/env\ncp: cannot copy \n to \nrm: cannot remove \nmkdir: cannot create directory \nrun-device-window: environment and arguments exceed the execprog packet (65536 bytes, 1024 records)\nrun-device-window: an environment entry is longer than 65535 bytes\nREFUSE: an argument is longer than 131071 bytes (the Linux MAX_ARG_STRLEN)\nREFUSE: cannot read /proc/self/exe\nREFUSE: cannot start the window closer; no window was opened and no row ran\nREFUSE: a command printed more than 65536 bytes where a short value was expected\nREFUSE: spark-window open printed no usable window id (empty, longer than 4095 bytes, or not [A-Za-z0-9._-]+); nothing was closed: check spark-window status\n" 62 text tcg1 "run-device-window: the window closer is gone; window " 63 text tcg2 " was not closed here and ends with its lease (see spark-window status)" 64 const kEnv ulong 0 65 const kDashI ulong 1 66 const kDD ulong 2 67 const kSW ulong 3 68 const kRun ulong 4 69 const kOpen ulong 5 70 const kClose ulong 6 71 const kLedger ulong 7 72 const kDry ulong 8 73 const kPairs ulong 9 74 const kLedgerNeeds ulong 10 75 const kPairsNeeds ulong 11 76 const kUnknown ulong 12 77 const kNoLedger ulong 13 78 const kBadLedger ulong 14 79 const kBadPairs ulong 15 80 const kPairsPre ulong 16 81 const kPairsLt ulong 17 82 const kPairsBig ulong 18 83 const kTooLong ulong 19 84 const kEPAIRS ulong 20 85 const kECUDAPP ulong 21 86 const kETORCH ulong 22 87 const kECUBIN ulong 23 88 const kEPY ulong 24 89 const kEMAXPY ulong 25 90 const kEHOME ulong 26 91 const kECVD ulong 27 92 const kESW ulong 28 93 const kFive ulong 29 94 const kSelfExe ulong 30 95 const kHostFile ulong 31 96 const kDevNull ulong 32 97 const kCudappRel ulong 33 98 const kTorchRel ulong 34 99 const kOutRel ulong 35 100 const kEvDir ulong 36 101 const kNamePre ulong 37 102 const kCubinsRel ulong 38 103 const kMmRel ulong 39 104 const kGqaRel ulong 40 105 const kStaleRel ulong 41 106 const kCbuildRel ulong 42 107 const kPython3 ulong 43 108 const kProbeRel ulong 44 109 const kLibRel ulong 45 110 const kMmFn ulong 46 111 const kN16 ulong 47 112 const kN8 ulong 48 113 const kLoadOnly ulong 49 114 const kGqaFn ulong 50 115 const kN2 ulong 51 116 const kN1 ulong 52 117 const kExpReject ulong 53 118 const kConfRel ulong 54 119 const kMaxRunner ulong 55 120 const kDevice ulong 56 121 const kEnvWord ulong 57 122 const kPyPath ulong 58 123 const kTorchDir ulong 59 124 const kTorchTest ulong 60 125 const kReceipt ulong 61 126 const kStMatmul ulong 62 127 const kStGqa ulong 63 128 const kStStale ulong 64 129 const kStConf ulong 65 130 const kStPair ulong 66 131 const kStPairSuf ulong 67 132 const kStTorch ulong 68 133 const kPlan ulong 69 134 const kPlanMid ulong 70 135 const kRefCvd ulong 71 136 const kQuoteParen ulong 72 137 const kUnset ulong 73 138 const kRefSw ulong 74 139 const kDryHead ulong 75 140 const kWinEq ulong 76 141 const kPairsEq ulong 77 142 const kOutEq ulong 78 143 const kWould ulong 79 144 const kWouldLedger ulong 80 145 const kWouldTail ulong 81 146 const kInputs ulong 82 147 const kOK ulong 83 148 const kMissing ulong 84 149 const kMaxpyRel ulong 85 150 const kHomeUnset ulong 86 151 const kDashC ulong 87 152 const kTorchProbe ulong 88 153 const kTorchOk ulong 89 154 const kTorchMiss ulong 90 155 const kTorchMissTail ulong 91 156 const kExists ulong 92 157 const kExistsTail ulong 93 158 const kRows ulong 94 159 const kRowsMid ulong 95 160 const kRowsTail ulong 96 161 const kDryInc ulong 97 162 const kDryPass ulong 98 163 const kRefExists ulong 99 164 const kRefExistsTail ulong 100 165 const kRefCreate ulong 101 166 const kRcTxt ulong 102 167 const kWinTxt ulong 103 168 const kGb10 ulong 104 169 const kExcl ulong 105 170 const kQuiet ulong 106 171 const kNone ulong 107 172 const kLease ulong 108 173 const kN20 ulong 109 174 const kWait ulong 110 175 const kRefOpen ulong 111 176 const kWWid ulong 112 177 const kWOpened ulong 113 178 const kWHost ulong 114 179 const kWStart ulong 115 180 const kWBHead ulong 116 181 const kWBDirty ulong 117 182 const kWCHead ulong 118 183 const kWScript ulong 119 184 const kWInput ulong 120 185 const kWPairs ulong 121 186 const kWEnd ulong 122 187 const kUnknownW ulong 123 188 const kGit ulong 124 189 const kDashCap ulong 125 190 const kRevParse ulong 126 191 const kHEAD ulong 127 192 const kStatus ulong 128 193 const kPorcelain ulong 129 194 const kUntracked ulong 130 195 const kCubinsDir ulong 131 196 const kMmPre ulong 132 197 const kGqaPre ulong 133 198 const kOmegaPre ulong 134 199 const kMaxPre ulong 135 200 const kStar ulong 136 201 const kCpStat ulong 137 202 const kCpStatTail ulong 138 203 const kOutSuf ulong 139 204 const kShaSuf ulong 140 205 const kLogSuf ulong 141 206 const kRcEq ulong 142 207 const kDW ulong 143 208 const kAllRows ulong 144 209 const kRowsRc0 ulong 145 210 const kFailSee ulong 146 211 const kParen ulong 147 212 const kTwoSp ulong 148 213 const kCannotOpen ulong 149 214 const kCannotExec ulong 150 215 const kCpFail ulong 151 216 const kTo ulong 152 217 const kRmFail ulong 153 218 const kMkFail ulong 154 219 const kPkFull ulong 155 220 const kEnvLong ulong 156 221 const kArgLong ulong 157 222 const kNoExe ulong 158 223 const kNoCloser ulong 159 224 const kCapBig ulong 160 225 const kBadWid ulong 161 226 227 const sHere ulong 0 228 const sCudapp ulong 1 229 const sTorch ulong 2 230 const sOut ulong 3 231 const sName ulong 4 232 const sCubins ulong 5 233 const sMm ulong 6 234 const sGqa ulong 7 235 const sStale ulong 8 236 const sLedger ulong 9 237 const sPy ulong 10 238 const sPairs ulong 11 239 const sCbuild ulong 12 240 const sLabel ulong 13 241 const sWid ulong 14 242 const sTmp ulong 15 243 const sVal ulong 16 244 const sLast ulong 17 245 const sCand ulong 18 246 const sSrc ulong 19 247 const sDst ulong 20 248 const sDir ulong 21 249 const sPath ulong 22 250 const gDry ulong 0 251 const gPairs ulong 1 252 const gStep ulong 2 253 const gFail ulong 3 254 const gOpened ulong 4 255 const gClPid ulong 5 256 const gClW ulong 6 257 const gMissing ulong 7 258 const gHn ulong 8 259 const gHt ulong 9 260 const gCn ulong 10 261 const gNl ulong 11 262 const gWf ulong 12 263 const gHit ulong 13 264 const gDnl ulong 14 265 const gClH ulong 15 266 const gHasCl ulong 16 267 const gCo ulong 17 268 const gBig ulong 18 269 const gAl ulong 19 270 const gClI ulong 20 271 const gClB ulong 21 272 const gEvo ulong 22 273 const gEvl ulong 23 274 const kNoFd ulong `18446744073709551615` 275 const mask ulong `4294967295` 276 global sb uchar 65536 277 global sn ulong 1 278 global kp ulong 2 279 global sv uchar 131072 280 global sl ulong 32 281 global pk uchar 65536 282 global pn ulong 2 283 global ar uchar 65536 284 global arn ulong 2 285 global eb uchar 65536 286 global fb uchar 65536 287 global cb uchar 65536 288 global ab uchar 131072 289 global stb uchar 128 290 global st4 uchar 4 291 global ends uchar 32 292 global one uchar 1 293 global g ulong 24 294 global hx uchar 64 295 global h ulong 8 296 global w ulong 64 297 global k ulong 64 298 global hs ulong 8 299 global hb uchar 64 300 global dbuf uchar 32768 301 global ds long 4 302 global dn uchar 512 303 global cp2 ulong 2 304 global tm ulong 8 305 fn tbpos x ulong to void 306 i = ulong 0 307 e = ulong 0 308 c = true 309 while c bound 999999 310 if ge i len tb 311 c = false 312 else 313 if eq e x 314 c = false 315 else 316 if eq get tb i 10 317 e = add e 1 318 end 319 i = add i 1 320 end 321 end 322 end 323 put kp 0 i 324 j = i 325 d = true 326 while d bound 999999 327 if ge j len tb 328 d = false 329 else 330 if eq get tb j 10 331 d = false 332 else 333 j = add j 1 334 end 335 end 336 end 337 put kp 1 sub j i 338 end 339 fn sbclr to void 340 put sn 0 0 341 end 342 fn sbc b uchar to void 343 n = get sn 0 344 put sb n b 345 put sn 0 add n 1 346 end 347 fn sbk x ulong to void 348 tbpos x 349 s = get kp 0 350 m = get kp 1 351 for i ulong m 352 sbc get tb add s i 353 end 354 end 355 note sbnumw v wd: v in decimal, zero-padded to at least wd digits 356 fn sbnumw v ulong wd ulong to void 357 d = new uchar 24 0 358 n = ulong 0 359 x = v 360 c = true 361 while c bound 30 362 put d n uchar add 48 rem x 10 363 n = add n 1 364 x = div x 10 365 if and eq x 0 ge n wd 366 c = false 367 end 368 end 369 while gt n 0 bound 30 370 n = sub n 1 371 sbc get d n 372 end 373 end 374 fn sbnum v ulong to void 375 sbnumw v 1 376 end 377 note out fd: write sb and a newline to fd, then clear sb 378 fn out fd int to void 379 sbc 10 380 r = write fd sb 0 get sn 0 381 sbclr 382 end 383 note clgone: the closer is gone after a window was opened here, so nothing will close it: say so, 384 note naming the window, on stderr and at the end of window.txt (review N2) 385 fn clgone to void 386 sbclr 387 sbs sOut 388 sbk kWinTxt 389 e = fopen sb 0 get sn 0 1089 438 390 sbclr 391 for i ulong 53 392 sbc get tcg1 i 393 end 394 sbs sWid 395 for j ulong 70 396 sbc get tcg2 j 397 end 398 sbc 10 399 if ge e 0 400 x = write int e sb 0 get sn 0 401 y = fclose int e 402 end 403 r = write 2 sb 0 get sn 0 404 sbclr 405 end 406 note bail rc: every exit after the closer started goes through this. It closes the ends of the 407 note open's pipes it still holds, writes N (a window was opened here: close it now) or X (nothing 408 note to close) to the closer unless the closer has already exited (then there is no reader and a 409 note write would end this program by SIGPIPE), closes both pipe ends, waits for the closer, exits rc 410 fn bail rc ulong to void 411 if eq get g gHasCl 1 412 if ne get g gClI kNoFd 413 z1 = fclose int get g gClI 414 put g gClI kNoFd 415 end 416 if ne get g gClB kNoFd 417 z2 = fclose int get g gClB 418 put g gClB kNoFd 419 end 420 put one 0 88 421 if eq get g gOpened 1 422 put one 0 78 423 end 424 d = waitpid int get g gClPid st4 0 4 1 425 if eq d 0 426 wr = write int get g gClW one 0 1 427 end 428 if ne d 0 429 if eq get g gOpened 1 430 clgone 431 end 432 end 433 z = fclose int get g gClW 434 y = fclose int get g gClH 435 if eq d 0 436 r = waitpid int get g gClPid st4 0 4 0 437 end 438 put g gHasCl 0 439 put g gOpened 0 440 end 441 exit int rc 442 end 443 fn diek x ulong rc ulong to void 444 sbclr 445 sbk x 446 out 2 447 bail rc 448 end 449 note svset s: move sb into string slot s (4095 bytes at most: the Linux path limit) 450 fn svset s ulong to void 451 n = get sn 0 452 if gt n 4095 453 diek kTooLong 2 454 end 455 o = mul s 4096 456 for i ulong n 457 put sv add o i get sb i 458 end 459 put sl s n 460 sbclr 461 end 462 fn sbs s ulong to void 463 o = mul s 4096 464 for i ulong get sl s 465 sbc get sv add o i 466 end 467 end 468 fn svk s ulong x ulong to void 469 sbclr 470 sbk x 471 svset s 472 end 473 fn sbeqk x ulong to bool 474 tbpos x 475 a = get kp 0 476 m = get kp 1 477 if ne m get sn 0 478 ret false 479 end 480 for i ulong m 481 if ne get sb i get tb add a i 482 ret false 483 end 484 end 485 ret true 486 end 487 note execprog packet: u16 count, then records of u16 length and bytes 488 fn pkbeg to void 489 put pn 0 2 490 put pn 1 0 491 end 492 fn pkroom n ulong to void 493 if or gt add add get pn 0 2 n 65536 ge get pn 1 1024 494 diek kPkFull 3 495 end 496 end 497 fn pkrec to void 498 n = get sn 0 499 pkroom n 500 p = get pn 0 501 put pk p uchar and n 255 502 put pk add p 1 uchar shr n 8 503 for i ulong n 504 put pk add add p 2 i get sb i 505 end 506 put pn 0 add add p 2 n 507 put pn 1 add get pn 1 1 508 sbclr 509 end 510 fn pkeb n ulong to void 511 pkroom n 512 p = get pn 0 513 put pk p uchar and n 255 514 put pk add p 1 uchar shr n 8 515 for i ulong n 516 put pk add add p 2 i get eb i 517 end 518 put pn 0 add add p 2 n 519 put pn 1 add get pn 1 1 520 end 521 fn pkk x ulong to void 522 sbk x 523 pkrec 524 end 525 fn pks s ulong to void 526 sbs s 527 pkrec 528 end 529 fn pkend to void 530 c = get pn 1 531 put pk 0 uchar and c 255 532 put pk 1 uchar shr c 8 533 end 534 note envok l: the entry in eb (l bytes) is NAME=VALUE with a non-empty NAME 535 fn envok l ulong to bool 536 if eq l 0 537 ret false 538 end 539 if eq get eb 0 61 540 ret false 541 end 542 for i ulong l 543 if eq get eb i 61 544 ret true 545 end 546 end 547 ret false 548 end 549 note pkhead: /usr/bin/env -i -- ; the command follows 550 fn pkhead to void 551 pkbeg 552 pkk kEnv 553 pkk kDashI 554 pkk kDD 555 c = ulong envcnt 556 for i ulong c 557 l = envread i eb 0 65536 558 if lt l 0 559 diek kEnvLong 3 560 end 561 if envok ulong l 562 pkeb ulong l 563 end 564 end 565 end 566 note step arguments: records like the packet's, in ar 567 fn arbeg to void 568 put arn 0 0 569 put arn 1 0 570 end 571 fn arrec to void 572 n = get sn 0 573 p = get arn 0 574 if gt add add p 2 n 65536 575 diek kPkFull 3 576 end 577 put ar p uchar and n 255 578 put ar add p 1 uchar shr n 8 579 for i ulong n 580 put ar add add p 2 i get sb i 581 end 582 put arn 0 add add p 2 n 583 put arn 1 add get arn 1 1 584 sbclr 585 end 586 fn ark x ulong to void 587 sbk x 588 arrec 589 end 590 fn ars s ulong to void 591 sbs s 592 arrec 593 end 594 note arjoin: append the step arguments to sb, separated by spaces 595 fn arjoin to void 596 p = ulong 0 597 for r ulong get arn 1 598 if gt r 0 599 sbc 32 600 end 601 n = or ulong get ar p shl ulong get ar add p 1 8 602 for i ulong n 603 sbc get ar add add p 2 i 604 end 605 p = add add p 2 n 606 end 607 end 608 fn pkar to void 609 p = ulong 0 610 for r ulong get arn 1 611 n = or ulong get ar p shl ulong get ar add p 1 8 612 for i ulong n 613 sbc get ar add add p 2 i 614 end 615 pkrec 616 p = add add p 2 n 617 end 618 end 619 note envpre a m: the entry in eb starts with the m table bytes at a 620 fn envpre a ulong m ulong to bool 621 for j ulong m 622 if ne get eb j get tb add a j 623 ret false 624 end 625 end 626 ret true 627 end 628 note envval x: when NAME= (table entry x) is in the environment, the entry stays in eb and g 629 note gEvo/gEvl hold the offset and the length of its value (envscan has refused longer entries) 630 fn envval x ulong to bool 631 tbpos x 632 a = get kp 0 633 m = get kp 1 634 c = ulong envcnt 635 for i ulong c 636 l = envread i eb 0 65536 637 if ge l long m 638 if envpre a m 639 put g gEvo m 640 put g gEvl sub ulong l m 641 ret true 642 end 643 end 644 end 645 ret false 646 end 647 note outev fd: write sb and then the value envval found to fd (no length limit), clear sb 648 fn outev fd int to void 649 r = write fd sb 0 get sn 0 650 q = write fd eb get g gEvo get g gEvl 651 sbclr 652 end 653 note envget x s: when NAME= (table entry x) is in the environment, its value goes to slot s 654 fn envget x ulong s ulong to bool 655 if envval x 656 sbclr 657 o = get g gEvo 658 for q ulong get g gEvl 659 sbc get eb add o q 660 end 661 svset s 662 ret true 663 end 664 ret false 665 end 666 note envscan: refuse at once (exit 3) when an environment entry does not fit one execprog record; 667 note otherwise it would be skipped when a value is looked up and refused only at the first child 668 fn envscan to void 669 c = ulong envcnt 670 for i ulong c 671 l = envread i eb 0 65536 672 if lt l 0 673 diek kEnvLong 3 674 end 675 end 676 end 677 note envset x s: ${NAME:-} is non-empty, value in slot s 678 fn envset x ulong s ulong to bool 679 if envget x s 680 ret gt get sl s 0 681 end 682 ret false 683 end 684 note fexist: [ -e "$sb" ] (sb cleared) 685 fn fexist to bool 686 n = get sn 0 687 if or eq n 0 gt n 4095 688 sbclr 689 ret false 690 end 691 r = faccess sb 0 n 0 692 sbclr 693 ret eq r 0 694 end 695 fn fdat i ulong to int 696 v = uint 0 697 for j ulong 4 698 v = or v shl uint get ends add i j uint mul j 8 699 end 700 ret int v 701 end 702 note spawn o e: fork and exec the packet in pk; stdout to fd o and stderr to fd e when >= 0 703 fn spawn o int e int to long 704 p = fork 705 if ne p 0 706 ret p 707 end 708 if ge o 0 709 a = dupfd o 1 0 710 end 711 if ge e 0 712 b = dupfd e 2 0 713 end 714 if eq get g gHasCl 1 715 c = dupfd int get g gClH 0 0 716 end 717 x = execprog pk 0 get pn 0 718 sbclr 719 sbk kCannotExec 720 out 2 721 exit 127 722 end 723 note reap p: the shell's $? for the child: its exit code, or 128 + signal 724 fn reap p long to ulong 725 if lt p 0 726 ret 127 727 end 728 r = waitpid int p st4 0 4 0 729 if lt r 0 730 ret 127 731 end 732 s = and get st4 0 127 733 if ne s 0 734 ret add 128 ulong s 735 end 736 ret ulong get st4 1 737 end 738 fn devnull to int 739 tbpos kDevNull 740 f = fopen tb get kp 0 get kp 1 1 0 741 ret int f 742 end 743 note cbadd q: count the newlines of fb[0, q) into gNl and keep them in cb while there is room; 744 note a byte past the room sets gCo, and sbcap/sbcapall then refuse 745 fn cbadd q ulong to void 746 t = get g gCn 747 nl = get g gNl 748 for i ulong q 749 c = get fb i 750 if eq c 10 751 nl = add nl 1 752 end 753 if lt t 65536 754 put cb t c 755 t = add t 1 756 else 757 put g gCo 1 758 end 759 end 760 put g gCn t 761 put g gNl nl 762 end 763 note capture e: run the packet with stdout into cb (first 65536 bytes kept, gCo set past them; 764 note gNl newlines counted over the whole output) and stderr to fd e (< 0: inherited) 765 fn capture e int to ulong 766 put g gCn 0 767 put g gNl 0 768 put g gCo 0 769 r = pipefds ends 0 8 0 770 if ne r 0 771 ret 127 772 end 773 rf = fdat 0 774 wf = fdat 4 775 p = spawn wf e 776 r = fclose wf 777 go = true 778 while go bound `9223372036854775807` 779 q = read rf fb 0 65536 780 if le q 0 781 go = false 782 else 783 cbadd ulong q 784 end 785 end 786 r = fclose rf 787 ret reap p 788 end 789 note sbcap: append the captured output without its trailing newlines, as $(...) does 790 fn sbcap to void 791 if eq get g gCo 1 792 diek kCapBig 3 793 end 794 t = get g gCn 795 c = true 796 while c bound 65537 797 if eq t 0 798 c = false 799 else 800 if ne get cb sub t 1 10 801 c = false 802 else 803 t = sub t 1 804 end 805 end 806 end 807 for i ulong t 808 sbc get cb i 809 end 810 end 811 note sbcapall: append the captured output as it is (a later word ends the $(...)) 812 fn sbcapall to void 813 if eq get g gCo 1 814 diek kCapBig 3 815 end 816 for i ulong get g gCn 817 sbc get cb i 818 end 819 end 820 note time: civil date from days since 1970-01-01 (Hinnant's algorithm) 821 fn civ to void 822 a = get tm 0 823 r = rem a 86400 824 put tm 4 div r 3600 825 put tm 5 div rem r 3600 60 826 put tm 6 rem r 60 827 a = add div a 86400 719468 828 era = div a 146097 829 doe = sub a mul era 146097 830 t = sub doe div doe 1460 831 t = add t div doe 36524 832 t = sub t div doe 146096 833 a = div t 365 834 y = add a mul era 400 835 t = add mul 365 a div a 4 836 doe = sub doe sub t div a 100 837 mp = div add mul 5 doe 2 153 838 t = div add mul 153 mp 2 5 839 put tm 3 add sub doe t 1 840 m = add mp 3 841 if ge mp 10 842 m = sub mp 9 843 end 844 if le m 2 845 y = add y 1 846 end 847 put tm 1 y 848 put tm 2 m 849 end 850 note sbtime: date -u +%Y-%m-%dT%H:%M:%SZ 851 fn sbtime to void 852 put tm 0 div ulong realns `1000000000` 853 civ 854 sbnum get tm 1 855 sbc 45 856 sbnumw get tm 2 2 857 sbc 45 858 sbnumw get tm 3 2 859 sbc 84 860 sbnumw get tm 4 2 861 sbc 58 862 sbnumw get tm 5 2 863 sbc 58 864 sbnumw get tm 6 2 865 sbc 90 866 end 867 note SHA-256 (FIPS 180-4), from aien-architecture-mv tools/zero-legacy/sha256.om, streamed 868 fn initk to void 869 put k 0 `1116352408` 870 put k 1 `1899447441` 871 put k 2 `3049323471` 872 put k 3 `3921009573` 873 put k 4 `961987163` 874 put k 5 `1508970993` 875 put k 6 `2453635748` 876 put k 7 `2870763221` 877 put k 8 `3624381080` 878 put k 9 `310598401` 879 put k 10 `607225278` 880 put k 11 `1426881987` 881 put k 12 `1925078388` 882 put k 13 `2162078206` 883 put k 14 `2614888103` 884 put k 15 `3248222580` 885 put k 16 `3835390401` 886 put k 17 `4022224774` 887 put k 18 `264347078` 888 put k 19 `604807628` 889 put k 20 `770255983` 890 put k 21 `1249150122` 891 put k 22 `1555081692` 892 put k 23 `1996064986` 893 put k 24 `2554220882` 894 put k 25 `2821834349` 895 put k 26 `2952996808` 896 put k 27 `3210313671` 897 put k 28 `3336571891` 898 put k 29 `3584528711` 899 put k 30 `113926993` 900 put k 31 `338241895` 901 put k 32 `666307205` 902 put k 33 `773529912` 903 put k 34 `1294757372` 904 put k 35 `1396182291` 905 put k 36 `1695183700` 906 put k 37 `1986661051` 907 put k 38 `2177026350` 908 put k 39 `2456956037` 909 put k 40 `2730485921` 910 put k 41 `2820302411` 911 put k 42 `3259730800` 912 put k 43 `3345764771` 913 put k 44 `3516065817` 914 put k 45 `3600352804` 915 put k 46 `4094571909` 916 put k 47 `275423344` 917 put k 48 `430227734` 918 put k 49 `506948616` 919 put k 50 `659060556` 920 put k 51 `883997877` 921 put k 52 `958139571` 922 put k 53 `1322822218` 923 put k 54 `1537002063` 924 put k 55 `1747873779` 925 put k 56 `1955562222` 926 put k 57 `2024104815` 927 put k 58 `2227730452` 928 put k 59 `2361852424` 929 put k 60 `2428436474` 930 put k 61 `2756734187` 931 put k 62 `3204031479` 932 put k 63 `3329325298` 933 end 934 fn inith to void 935 put h 0 `1779033703` 936 put h 1 `3144134277` 937 put h 2 `1013904242` 938 put h 3 `2773480762` 939 put h 4 `1359893119` 940 put h 5 `2600822924` 941 put h 6 `528734635` 942 put h 7 `1541459225` 943 put g gHn 0 944 put g gHt 0 945 end 946 fn rotr x ulong r ulong to ulong 947 ret and or shr x r shl x sub 32 r mask 948 end 949 fn bsig0 x ulong to ulong 950 ret xor rotr x 2 xor rotr x 13 rotr x 22 951 end 952 fn bsig1 x ulong to ulong 953 ret xor rotr x 6 xor rotr x 11 rotr x 25 954 end 955 fn ssig0 x ulong to ulong 956 ret xor rotr x 7 xor rotr x 18 shr x 3 957 end 958 fn ssig1 x ulong to ulong 959 ret xor rotr x 17 xor rotr x 19 shr x 10 960 end 961 fn ch x ulong y ulong z ulong to ulong 962 ret xor and x y and and not x mask z 963 end 964 fn maj x ulong y ulong z ulong to ulong 965 ret xor and x y xor and x z and y z 966 end 967 fn sched to void 968 for i ulong 16 969 o = mul i 4 970 v = ulong get hb o 971 v = or shl v 8 ulong get hb add o 1 972 v = or shl v 8 ulong get hb add o 2 973 v = or shl v 8 ulong get hb add o 3 974 put w i v 975 end 976 for j ulong 48 977 i = add j 16 978 s = add ssig1 get w sub i 2 get w sub i 7 979 s = add s ssig0 get w sub i 15 980 s = add s get w sub i 16 981 put w i and s mask 982 end 983 end 984 fn round i ulong to void 985 a = get hs 0 986 e = get hs 4 987 t1 = add get hs 7 bsig1 e 988 t1 = add t1 ch e get hs 5 get hs 6 989 t1 = add t1 get k i 990 t1 = and add t1 get w i mask 991 t2 = and add bsig0 a maj a get hs 1 get hs 2 mask 992 put hs 7 get hs 6 993 put hs 6 get hs 5 994 put hs 5 e 995 put hs 4 and add get hs 3 t1 mask 996 put hs 3 get hs 2 997 put hs 2 get hs 1 998 put hs 1 a 999 put hs 0 and add t1 t2 mask 1000 end 1001 fn block to void 1002 sched 1003 for i ulong 8 1004 put hs i get h i 1005 end 1006 for i ulong 64 1007 round i 1008 end 1009 for i ulong 8 1010 put h i and add get h i get hs i mask 1011 end 1012 end 1013 fn hbyte b uchar to void 1014 f = get g gHn 1015 put hb f b 1016 f = add f 1 1017 if eq f 64 1018 block 1019 f = 0 1020 end 1021 put g gHn f 1022 end 1023 fn hfin to void 1024 bits = mul get g gHt 8 1025 hbyte 128 1026 while ne get g gHn 56 bound 128 1027 hbyte 0 1028 end 1029 for i ulong 8 1030 hbyte uchar and shr bits mul sub 7 i 8 255 1031 end 1032 for i ulong 8 1033 v = get h i 1034 for j ulong 8 1035 d = and shr v mul sub 7 j 4 15 1036 c = uchar add d 48 1037 if ge d 10 1038 c = uchar add d 87 1039 end 1040 put hx add mul i 8 j c 1041 end 1042 end 1043 end 1044 note shafile: SHA-256 of the file named in sb into hx; false when it cannot be opened or read 1045 fn shafile to bool 1046 n = get sn 0 1047 if or eq n 0 gt n 4095 1048 sbclr 1049 ret false 1050 end 1051 f = fopen sb 0 n 0 0 1052 sbclr 1053 if lt f 0 1054 ret false 1055 end 1056 inith 1057 go = true 1058 while go bound `9223372036854775807` 1059 r = read int f fb 0 65536 1060 if lt r 0 1061 z = fclose int f 1062 ret false 1063 end 1064 if eq r 0 1065 go = false 1066 else 1067 for i ulong ulong r 1068 hbyte get fb i 1069 end 1070 put g gHt add get g gHt ulong r 1071 end 1072 end 1073 y = fclose int f 1074 hfin 1075 ret true 1076 end 1077 fn sbhx to void 1078 for i ulong 64 1079 sbc get hx i 1080 end 1081 end 1082 note directory iteration over getdents64 records (reclen u16 at 16, name from 19, NUL-ended) 1083 fn dopen to bool 1084 n = get sn 0 1085 f = fopen sb 0 n 0 0 1086 sbclr 1087 put ds 0 f 1088 put ds 1 0 1089 put ds 2 0 1090 ret ge f 0 1091 end 1092 fn dclose to void 1093 if ge get ds 0 0 1094 z = fclose int get ds 0 1095 end 1096 put ds 0 -1 1097 end 1098 fn dnext to bool 1099 f = int get ds 0 1100 if lt f 0 1101 ret false 1102 end 1103 if ge get ds 1 get ds 2 1104 r = getdnts f dbuf 0 32768 1105 if le r 0 1106 ret false 1107 end 1108 put ds 1 0 1109 put ds 2 r 1110 end 1111 p = ulong get ds 1 1112 rl = or ulong get dbuf add p 16 shl ulong get dbuf add p 17 8 1113 n = ulong 0 1114 c = true 1115 while c bound 512 1116 b = get dbuf add add p 19 n 1117 if or eq b 0 ge add add p 19 n add p rl 1118 c = false 1119 else 1120 put dn n b 1121 n = add n 1 1122 end 1123 end 1124 put g gDnl n 1125 put ds 1 long add p rl 1126 ret true 1127 end 1128 note dpre x: the entry name starts with table entry x; kStar matches every name not starting with "." 1129 fn dpre x ulong to bool 1130 m = get g gDnl 1131 if eq x kStar 1132 ret and gt m 0 ne get dn 0 46 1133 end 1134 tbpos x 1135 a = get kp 0 1136 l = get kp 1 1137 if lt m l 1138 ret false 1139 end 1140 for i ulong l 1141 if ne get dn i get tb add a i 1142 ret false 1143 end 1144 end 1145 ret true 1146 end 1147 fn sbdn to void 1148 for i ulong get g gDnl 1149 sbc get dn i 1150 end 1151 end 1152 note dncmp s: bytewise order of the entry name against slot s (-1, 0, 1) 1153 fn dncmp s ulong to long 1154 o = mul s 4096 1155 m = get g gDnl 1156 l = get sl s 1157 i = ulong 0 1158 while and lt i m lt i l bound 4097 1159 a = get dn i 1160 b = get sv add o i 1161 if lt a b 1162 ret -1 1163 end 1164 if gt a b 1165 ret 1 1166 end 1167 i = add i 1 1168 end 1169 if lt m l 1170 ret -1 1171 end 1172 if gt m l 1173 ret 1 1174 end 1175 ret 0 1176 end 1177 fn sbquote s ulong to void 1178 sbc 39 1179 sbs s 1180 sbc 39 1181 end 1182 note mkdirp: mkdir -p of the path in sb (it goes to slot sPath); true when it is a directory 1183 fn mkdirp to bool 1184 svset sPath 1185 n = get sl sPath 1186 o = mul sPath 4096 1187 for i ulong n 1188 if and gt i 0 eq get sv add o i 47 1189 r = makedir sv o i 511 1190 end 1191 end 1192 q = makedir sv o n 511 1193 z = stat sv o n stb 0 128 1194 if lt z 0 1195 ret false 1196 end 1197 m = or ulong get stb 16 shl ulong get stb 17 8 1198 ret eq and m 61440 16384 1199 end 1200 fn mkdirpd to void 1201 if not mkdirp 1202 sbk kMkFail 1203 sbquote sPath 1204 out 2 1205 bail 1 1206 end 1207 end 1208 note cpf: cp slot sSrc to slot sDst (a regular file; the target keeps cp's mode rules) 1209 fn cpf to bool 1210 o = mul sSrc 4096 1211 n = get sl sSrc 1212 z = stat sv o n stb 0 128 1213 if lt z 0 1214 ret false 1215 end 1216 m = or ulong get stb 16 shl ulong get stb 17 8 1217 if eq and m 61440 16384 1218 ret false 1219 end 1220 f = fopen sv o n 0 0 1221 if lt f 0 1222 ret false 1223 end 1224 d = fopen sv mul sDst 4096 get sl sDst 577 int and m 511 1225 if lt d 0 1226 y = fclose int f 1227 ret false 1228 end 1229 ok = true 1230 go = true 1231 while go bound `9223372036854775807` 1232 r = read int f fb 0 65536 1233 if le r 0 1234 go = false 1235 ok = eq r 0 1236 else 1237 x = write int d fb 0 ulong r 1238 if ne x r 1239 go = false 1240 ok = false 1241 end 1242 end 1243 end 1244 a = fclose int f 1245 b = fclose int d 1246 ret ok 1247 end 1248 note cpglob x: copy every entry of slot sDir whose name starts with table entry x into slot sCand 1249 fn cpglob x ulong to ulong 1250 sbs sDir 1251 c = ulong 0 1252 if dopen 1253 while dnext bound `9223372036854775807` 1254 if dpre x 1255 sbs sDir 1256 sbc 47 1257 sbdn 1258 svset sSrc 1259 sbs sCand 1260 sbc 47 1261 sbdn 1262 svset sDst 1263 c = add c 1 1264 if not cpf 1265 dclose 1266 sbk kCpFail 1267 sbquote sSrc 1268 sbk kTo 1269 sbquote sDst 1270 out 2 1271 bail 1 1272 end 1273 end 1274 end 1275 dclose 1276 end 1277 ret c 1278 end 1279 note rmglob x: rm -f slot sCbuild/* 1280 fn rmglob x ulong to void 1281 sbs sCbuild 1282 if dopen 1283 while dnext bound `9223372036854775807` 1284 if dpre x 1285 sbs sCbuild 1286 sbc 47 1287 sbdn 1288 svset sSrc 1289 r = unlink sv mul sSrc 4096 get sl sSrc 1290 if lt r 0 1291 dclose 1292 sbk kRmFail 1293 sbquote sSrc 1294 out 2 1295 bail 1 1296 end 1297 end 1298 end 1299 dclose 1300 end 1301 end 1302 note shalist: (cd sDir && sha256sum -- *) > file named in sb, in bytewise name order 1303 fn shalist to void 1304 n = get sn 0 1305 f = fopen sb 0 n 577 438 1306 sbclr 1307 if ge f 0 1308 put sl sLast 0 1309 have = false 1310 more = true 1311 while more bound `9223372036854775807` 1312 found = false 1313 sbs sDir 1314 if dopen 1315 while dnext bound `9223372036854775807` 1316 if dpre kStar 1317 if or not have gt dncmp sLast 0 1318 if or not found lt dncmp sVal 0 1319 sbdn 1320 svset sVal 1321 found = true 1322 end 1323 end 1324 end 1325 end 1326 dclose 1327 end 1328 if found 1329 sbs sVal 1330 svset sLast 1331 have = true 1332 sbs sDir 1333 sbc 47 1334 sbs sLast 1335 if shafile 1336 sbhx 1337 sbc 32 1338 sbc 32 1339 sbs sLast 1340 sbc 10 1341 r = write int f sb 0 get sn 0 1342 sbclr 1343 end 1344 else 1345 more = false 1346 end 1347 end 1348 z = fclose int f 1349 end 1350 end 1351 note lcomp s e: the last path component of slot s within [0, e) into cp2 (start, end) 1352 fn lcomp s ulong e ulong to void 1353 o = mul s 4096 1354 t = e 1355 c = true 1356 while c bound 4097 1357 if eq t 0 1358 c = false 1359 else 1360 if ne get sv add o sub t 1 47 1361 c = false 1362 else 1363 t = sub t 1 1364 end 1365 end 1366 end 1367 a = t 1368 d = true 1369 while d bound 4097 1370 if eq a 0 1371 d = false 1372 else 1373 if eq get sv add o sub a 1 47 1374 d = false 1375 else 1376 a = sub a 1 1377 end 1378 end 1379 end 1380 put cp2 0 a 1381 put cp2 1 t 1382 end 1383 note slash s e: e with the trailing slashes of slot s before it removed 1384 fn slash s ulong e ulong to ulong 1385 o = mul s 4096 1386 t = e 1387 c = true 1388 while c bound 4097 1389 if eq t 0 1390 c = false 1391 else 1392 if ne get sv add o sub t 1 47 1393 c = false 1394 else 1395 t = sub t 1 1396 end 1397 end 1398 end 1399 ret t 1400 end 1401 fn sbrange s ulong a ulong t ulong to void 1402 o = add mul s 4096 a 1403 for i ulong sub t a 1404 sbc get sv add o i 1405 end 1406 end 1407 note sbpb s: $(basename "$(dirname s)")/$(basename s) 1408 fn sbpb s ulong to void 1409 n = get sl s 1410 lcomp s n 1411 a = get cp2 0 1412 t = get cp2 1 1413 e = slash s a 1414 if eq e 0 1415 if and gt n 0 eq get sv mul s 4096 47 1416 sbc 47 1417 else 1418 sbc 46 1419 end 1420 else 1421 lcomp s e 1422 sbrange s get cp2 0 get cp2 1 1423 end 1424 sbc 47 1425 if eq a t 1426 if gt n 0 1427 sbc 47 1428 end 1429 else 1430 sbrange s a t 1431 end 1432 end 1433 note dirup s: slot s becomes its dirname (the root stays "/") 1434 fn dirup s ulong to void 1435 lcomp s get sl s 1436 e = slash s get cp2 0 1437 if eq e 0 1438 e = 1 1439 end 1440 put sl s e 1441 end 1442 note teerc: echo the line in sb | tee -a "$out/rc.txt" 1443 fn teerc to void 1444 svset sTmp 1445 sbs sOut 1446 sbk kRcTxt 1447 n = get sn 0 1448 f = fopen sb 0 n 1089 438 1449 sbclr 1450 sbs sTmp 1451 sbc 10 1452 r = write 1 sb 0 get sn 0 1453 if ge f 0 1454 q = write int f sb 0 get sn 0 1455 z = fclose int f 1456 end 1457 sbclr 1458 end 1459 note stepbeg: the step name is in sb; label = %02d-name 1460 fn stepbeg to void 1461 svset sTmp 1462 n = add get g gStep 1 1463 put g gStep n 1464 sbnumw n 2 1465 sbc 45 1466 sbs sTmp 1467 svset sLabel 1468 end 1469 note stepgo: dry-run prints the PLAN line; device mode runs spark-window run ARGS > NN-label.log 2>&1 1470 fn stepgo to void 1471 if eq get g gDry 1 1472 sbk kPlan 1473 sbs sLabel 1474 sbk kPlanMid 1475 arjoin 1476 out 1 1477 else 1478 pkhead 1479 pkk kSW 1480 pkk kRun 1481 pkar 1482 pkend 1483 if eq get g gDry 2 1484 ret 1485 end 1486 sbs sOut 1487 sbc 47 1488 sbs sLabel 1489 sbk kLogSuf 1490 svset sPath 1491 sbs sPath 1492 f = fopen sb 0 get sn 0 577 438 1493 sbclr 1494 rc = ulong 2 1495 if lt f 0 1496 sbk kCannotOpen 1497 sbs sPath 1498 out 2 1499 else 1500 p = spawn int f int f 1501 z = fclose int f 1502 rc = reap p 1503 end 1504 sbs sLabel 1505 sbk kRcEq 1506 sbnum rc 1507 teerc 1508 if ne rc 0 1509 put g gFail 1 1510 end 1511 end 1512 end 1513 fn probe to void 1514 sbs sCudapp 1515 sbk kProbeRel 1516 arrec 1517 end 1518 note confstep: the step name is in sb; one conformance/run.sh --device row 1519 fn confstep to void 1520 stepbeg 1521 dev = eq get g gDry 0 1522 if dev 1523 rmglob kOmegaPre 1524 rmglob kMaxPre 1525 end 1526 arbeg 1527 sbs sHere 1528 sbk kConfRel 1529 arrec 1530 ark kDevice 1531 stepgo 1532 if dev 1533 sbs sOut 1534 sbc 47 1535 sbs sLabel 1536 sbk kOutSuf 1537 svset sCand 1538 sbs sCand 1539 mkdirpd 1540 sbs sCbuild 1541 svset sDir 1542 a = cpglob kOmegaPre 1543 b = cpglob kMaxPre 1544 sbs sCand 1545 svset sDir 1546 sbs sOut 1547 sbc 47 1548 sbs sLabel 1549 sbk kShaSuf 1550 shalist 1551 end 1552 end 1553 fn plan to void 1554 put g gStep 0 1555 sbk kStMatmul 1556 stepbeg 1557 arbeg 1558 probe 1559 ars sMm 1560 ark kMmFn 1561 ark kN16 1562 ark kN16 1563 ark kN8 1564 stepgo 1565 sbk kStGqa 1566 stepbeg 1567 arbeg 1568 probe 1569 ark kLoadOnly 1570 ars sGqa 1571 ark kGqaFn 1572 ark kN2 1573 ark kN8 1574 ark kN1 1575 stepgo 1576 sbk kStStale 1577 stepbeg 1578 arbeg 1579 probe 1580 ark kExpReject 1581 ars sStale 1582 ark kMmFn 1583 ark kN16 1584 ark kN16 1585 ark kN8 1586 stepgo 1587 sbk kStConf 1588 confstep 1589 i = ulong 1 1590 while le i get g gPairs bound `9223372036854775807` 1591 sbk kStPair 1592 sbnum i 1593 sbk kStPairSuf 1594 confstep 1595 i = add i 1 1596 end 1597 sbk kStTorch 1598 stepbeg 1599 arbeg 1600 ark kEnvWord 1601 sbk kETORCH 1602 sbs sTorch 1603 arrec 1604 sbk kPyPath 1605 sbs sHere 1606 sbk kTorchDir 1607 arrec 1608 ars sPy 1609 sbs sHere 1610 sbk kTorchTest 1611 arrec 1612 sbs sOut 1613 sbk kReceipt 1614 arrec 1615 stepgo 1616 end 1617 note need: [ -e path ] for the path in sb 1618 fn need to void 1619 svset sTmp 1620 sbs sTmp 1621 if fexist 1622 sbk kOK 1623 else 1624 sbk kMissing 1625 put g gMissing 1 1626 end 1627 sbs sTmp 1628 out 1 1629 end 1630 note pkopen: the packet of spark-window open (the shell's open line) 1631 fn pkopen to void 1632 pkhead 1633 pkk kSW 1634 pkk kOpen 1635 pks sName 1636 pkk kLedger 1637 pks sLedger 1638 pkk kGb10 1639 pkk kExcl 1640 pkk kQuiet 1641 pkk kNone 1642 pkk kLease 1643 pkk kN20 1644 pkk kWait 1645 pkend 1646 end 1647 note preflight: build every packet this run can pass to execprog (the open, each row of the plan, 1648 note and the close with a window id of 4095 bytes) before anything is printed or opened, so an 1649 note environment too large for the packet refuses at once (exit 3), never mid-plan or mid-window 1650 fn preflight to void 1651 pkopen 1652 pkhead 1653 pkk kSW 1654 pkk kClose 1655 pkroom 4095 1656 d = get g gDry 1657 put g gDry 2 1658 plan 1659 put g gDry d 1660 put g gStep 0 1661 end 1662 fn dryrun to void 1663 cv = envval kECVD 1664 if or not cv gt get g gEvl 0 1665 sbk kRefCvd 1666 if cv 1667 outev 2 1668 else 1669 sbk kUnset 1670 end 1671 sbk kQuoteParen 1672 out 2 1673 exit 3 1674 end 1675 if envval kESW 1676 if gt get g gEvl 0 1677 sbk kRefSw 1678 outev 2 1679 out 2 1680 exit 3 1681 end 1682 end 1683 preflight 1684 sbk kDryHead 1685 sbs sLedger 1686 sbk kWinEq 1687 sbs sName 1688 sbk kPairsEq 1689 sbs sPairs 1690 sbk kOutEq 1691 sbs sLedger 1692 out 1 1693 sbk kWould 1694 sbs sName 1695 sbk kWouldLedger 1696 sbs sLedger 1697 sbk kWouldTail 1698 out 1 1699 plan 1700 sbk kInputs 1701 out 1 1702 put g gMissing 0 1703 sbs sCudapp 1704 sbk kProbeRel 1705 need 1706 sbs sMm 1707 need 1708 sbs sGqa 1709 need 1710 sbs sStale 1711 need 1712 sbs sHere 1713 sbk kConfRel 1714 need 1715 sbs sHere 1716 sbk kMaxRunner 1717 need 1718 sbs sHere 1719 sbk kTorchTest 1720 need 1721 sbs sTorch 1722 need 1723 if envset kEMAXPY sVal 1724 sbs sVal 1725 else 1726 if not envget kEHOME sVal 1727 diek kHomeUnset 2 1728 end 1729 sbs sVal 1730 sbk kMaxpyRel 1731 end 1732 need 1733 sbs sCudapp 1734 sbk kLibRel 1735 need 1736 pkhead 1737 pks sPy 1738 pkk kDashC 1739 pkk kTorchProbe 1740 pkend 1741 d = devnull 1742 p = spawn -1 d 1743 if ge d 0 1744 z = fclose d 1745 end 1746 if eq reap p 0 1747 sbk kTorchOk 1748 sbs sPy 1749 else 1750 sbk kTorchMiss 1751 sbs sPy 1752 sbk kTorchMissTail 1753 put g gMissing 1 1754 end 1755 out 1 1756 sbs sOut 1757 if fexist 1758 sbk kExists 1759 sbs sOut 1760 sbk kExistsTail 1761 out 1 1762 put g gMissing 1 1763 end 1764 sbk kRows 1765 sbnum get g gStep 1766 sbk kRowsMid 1767 sbs sPairs 1768 sbk kRowsTail 1769 out 1 1770 if eq get g gMissing 1 1771 sbk kDryInc 1772 out 1 1773 exit 1 1774 end 1775 sbk kDryPass 1776 out 1 1777 exit 0 1778 end 1779 note clread f: read the closer pipe to its end into sb (the id, a newline, N on a normal exit) 1780 fn clread f int to void 1781 sbclr 1782 go = true 1783 while go bound `9223372036854775807` 1784 q = read f fb 0 65536 1785 if le q 0 1786 go = false 1787 else 1788 for i ulong ulong q 1789 if lt get sn 0 65536 1790 sbc get fb i 1791 end 1792 end 1793 end 1794 end 1795 end 1796 note cldrain f: read the hold pipe to its end: every child holding its write end has exited 1797 fn cldrain f int to void 1798 go = true 1799 while go bound `9223372036854775807` 1800 q = read f fb 0 65536 1801 if le q 0 1802 go = false 1803 end 1804 end 1805 end 1806 note widok: the captured open output, without trailing newlines, is 1..4095 bytes of [A-Za-z0-9._-] 1807 fn widok to bool 1808 if eq get g gCo 1 1809 ret false 1810 end 1811 t = get g gCn 1812 c = true 1813 while c bound 65537 1814 if eq t 0 1815 c = false 1816 else 1817 if ne get cb sub t 1 10 1818 c = false 1819 else 1820 t = sub t 1 1821 end 1822 end 1823 end 1824 if or eq t 0 gt t 4095 1825 ret false 1826 end 1827 for i ulong t 1828 b = get cb i 1829 ok = or or and ge b 48 le b 57 and ge b 65 le b 90 and ge b 97 le b 122 1830 ok = or ok or or eq b 46 eq b 95 eq b 45 1831 if not ok 1832 ret false 1833 end 1834 end 1835 ret true 1836 end 1837 note clchild: the closer (see the header). Its descriptors: 0/4 main pipe, 8/12 hold pipe, 16/20 1838 note ready pipe (then the id back to this program), 24/28 the open's standard output 1839 fn clchild to void 1840 a = setsid 1841 z = fclose fdat 4 1842 z = fclose fdat 12 1843 z = fclose fdat 16 1844 z = fclose fdat 28 1845 put one 0 1 1846 wr = write fdat 20 one 0 1 1847 put g gCn 0 1848 put g gNl 0 1849 put g gCo 0 1850 go = true 1851 while go bound `9223372036854775807` 1852 q = read fdat 24 fb 0 65536 1853 if le q 0 1854 go = false 1855 else 1856 cbadd ulong q 1857 end 1858 end 1859 z = fclose fdat 24 1860 f = fork 1861 if eq f 0 1862 clfwd 1863 end 1864 z = fclose fdat 20 1865 if gt f 0 1866 r = waitpid int f st4 0 4 0 1867 end 1868 clread fdat 0 1869 v = ulong 0 1870 if gt get sn 0 0 1871 v = ulong get sb 0 1872 end 1873 sbclr 1874 if eq v 88 1875 exit 0 1876 end 1877 if not widok 1878 exit 0 1879 end 1880 if ne v 78 1881 cldrain fdat 8 1882 end 1883 sbcap 1884 svset sWid 1885 pkhead 1886 pkk kSW 1887 pkk kClose 1888 pks sWid 1889 pkend 1890 x = execprog pk 0 get pn 0 1891 sbk kCannotExec 1892 out 2 1893 exit 127 1894 end 1895 note clfwd: a child of the closer hands the open's output to this program on fd 20 and exits; when 1896 note this program is gone the write ends this child by SIGPIPE, never the closer 1897 fn clfwd to void 1898 n = get g gCn 1899 if gt n 0 1900 w1 = write fdat 20 cb 0 n 1901 end 1902 if eq get g gCo 1 1903 put one 0 120 1904 w2 = write fdat 20 one 0 1 1905 end 1906 exit 0 1907 end 1908 note closer: fork the closer before the open and wait until it is in its own session 1909 fn closer to void 1910 r = pipefds ends 0 8 0 1911 if eq r 0 1912 r = pipefds ends 8 8 0 1913 end 1914 if eq r 0 1915 r = pipefds ends 16 8 0 1916 end 1917 if eq r 0 1918 r = pipefds ends 24 8 0 1919 end 1920 if ne r 0 1921 diek kNoCloser 3 1922 end 1923 p = fork 1924 if lt p 0 1925 diek kNoCloser 3 1926 end 1927 if eq p 0 1928 clchild 1929 end 1930 z = fclose fdat 0 1931 z = fclose fdat 8 1932 z = fclose fdat 20 1933 z = fclose fdat 24 1934 put one 0 0 1935 q = read fdat 16 one 0 1 1936 put g gClPid ulong p 1937 put g gClW ulong fdat 4 1938 put g gClH ulong fdat 12 1939 put g gClB ulong fdat 16 1940 put g gClI ulong fdat 28 1941 put g gHasCl 1 1942 if ne q 1 1943 diek kNoCloser 3 1944 end 1945 end 1946 fn wl to void 1947 sbc 10 1948 r = write int get g gWf sb 0 get sn 0 1949 sbclr 1950 end 1951 note gitrev x s: line x + $(git -C slot rev-parse HEAD 2>/dev/null || echo unknown) 1952 fn gitrev x ulong s ulong to void 1953 pkhead 1954 pkk kGit 1955 pkk kDashCap 1956 pks s 1957 pkk kRevParse 1958 pkk kHEAD 1959 pkend 1960 d = devnull 1961 rc = capture d 1962 if ge d 0 1963 z = fclose d 1964 end 1965 sbk x 1966 if ne rc 0 1967 sbcapall 1968 sbk kUnknownW 1969 else 1970 sbcap 1971 end 1972 wl 1973 end 1974 fn inp s ulong to void 1975 sbs s 1976 ok = shafile 1977 if not ok 1978 sbk kCannotOpen 1979 sbs s 1980 out 2 1981 end 1982 sbk kWInput 1983 if ok 1984 sbhx 1985 end 1986 sbc 32 1987 sbpb s 1988 wl 1989 end 1990 fn wintxt to void 1991 sbs sOut 1992 sbk kWinTxt 1993 svset sPath 1994 sbs sPath 1995 f = fopen sb 0 get sn 0 577 438 1996 sbclr 1997 if lt f 0 1998 sbk kCannotOpen 1999 sbs sPath 2000 out 2 2001 bail 2 2002 end 2003 put g gWf ulong f 2004 sbk kWWid 2005 sbs sWid 2006 wl 2007 sbk kWOpened 2008 sbnum get g gOpened 2009 wl 2010 sbk kWHost 2011 tbpos kHostFile 2012 hf = fopen tb get kp 0 get kp 1 0 0 2013 if ge hf 0 2014 r = read int hf cb 0 65536 2015 y = fclose int hf 2016 for i ulong ulong r 2017 if ne get cb i 10 2018 sbc get cb i 2019 end 2020 end 2021 end 2022 wl 2023 sbk kWStart 2024 sbtime 2025 wl 2026 gitrev kWBHead sHere 2027 pkhead 2028 pkk kGit 2029 pkk kDashCap 2030 pks sHere 2031 pkk kStatus 2032 pkk kPorcelain 2033 pkk kUntracked 2034 pkend 2035 d = devnull 2036 rc = capture d 2037 if ge d 0 2038 z = fclose d 2039 end 2040 sbk kWBDirty 2041 sbnum get g gNl 2042 wl 2043 gitrev kWCHead sCudapp 2044 sbk kSelfExe 2045 ok = shafile 2046 sbk kWScript 2047 if ok 2048 sbhx 2049 end 2050 wl 2051 sbs sCudapp 2052 sbk kProbeRel 2053 svset sSrc 2054 inp sSrc 2055 inp sMm 2056 inp sGqa 2057 inp sStale 2058 inp sTorch 2059 sbs sCudapp 2060 sbk kLibRel 2061 svset sSrc 2062 inp sSrc 2063 sbk kWPairs 2064 sbs sPairs 2065 wl 2066 q = fclose int f 2067 end 2068 fn device to void 2069 preflight 2070 sbs sHere 2071 sbk kEvDir 2072 mkdirpd 2073 sbs sOut 2074 r = makedir sb 0 get sn 0 511 2075 sbclr 2076 if lt r 0 2077 sbs sOut 2078 if fexist 2079 sbk kRefExists 2080 sbs sOut 2081 sbk kRefExistsTail 2082 else 2083 sbk kRefCreate 2084 sbs sOut 2085 end 2086 out 2 2087 exit 2 2088 end 2089 sbs sOut 2090 sbk kRcTxt 2091 f = fopen sb 0 get sn 0 577 438 2092 sbclr 2093 if lt f 0 2094 sbk kCannotOpen 2095 sbs sOut 2096 sbk kRcTxt 2097 out 2 2098 exit 2 2099 end 2100 z = fclose int f 2101 put g gOpened 0 2102 if envset kESW sWid 2103 put g gOpened 0 2104 else 2105 closer 2106 pkopen 2107 put g gCn 0 2108 put g gNl 0 2109 put g gCo 0 2110 op = spawn int get g gClI -1 2111 oz = fclose int get g gClI 2112 put g gClI kNoFd 2113 ogo = true 2114 while ogo bound `9223372036854775807` 2115 oq = read int get g gClB fb 0 65536 2116 if le oq 0 2117 ogo = false 2118 else 2119 cbadd ulong oq 2120 end 2121 end 2122 oy = fclose int get g gClB 2123 put g gClB kNoFd 2124 rc = reap op 2125 if ne rc 0 2126 diek kRefOpen 3 2127 end 2128 if not widok 2129 diek kBadWid 3 2130 end 2131 sbcap 2132 svset sWid 2133 put g gOpened 1 2134 end 2135 wintxt 2136 sbs sOut 2137 sbk kCubinsDir 2138 svset sCand 2139 sbs sCand 2140 mkdirpd 2141 sbs sCubins 2142 svset sDir 2143 a = cpglob kMmPre 2144 b = cpglob kGqaPre 2145 if eq a 0 2146 sbk kCpStat 2147 sbs sCubins 2148 sbc 47 2149 sbk kMmPre 2150 sbk kCpStatTail 2151 out 2 2152 end 2153 if eq b 0 2154 sbk kCpStat 2155 sbs sCubins 2156 sbc 47 2157 sbk kGqaPre 2158 sbk kCpStatTail 2159 out 2 2160 end 2161 if or eq a 0 eq b 0 2162 bail 1 2163 end 2164 plan 2165 sbs sOut 2166 sbk kWinTxt 2167 e = fopen sb 0 get sn 0 1089 438 2168 sbclr 2169 sbk kWEnd 2170 sbtime 2171 sbc 10 2172 if ge e 0 2173 x = write int e sb 0 get sn 0 2174 y = fclose int e 2175 end 2176 sbclr 2177 sbk kDW 2178 sbs sLedger 2179 if eq get g gFail 0 2180 sbk kAllRows 2181 sbnum get g gStep 2182 sbk kRowsRc0 2183 else 2184 sbk kFailSee 2185 sbs sOut 2186 sbk kRcTxt 2187 sbk kParen 2188 end 2189 out 1 2190 bail get g gFail 2191 end 2192 note argsb i: argument i whole into ab (gAl bytes, up to MAX_ARG_STRLEN), and into sb as well 2193 note when it fits there (gBig 0) 2194 fn argsb i ulong to void 2195 sbclr 2196 r = argread i ab 0 131072 2197 if lt r 0 2198 diek kArgLong 2 2199 end 2200 n = ulong r 2201 put g gAl n 2202 put g gBig 0 2203 if gt n 65535 2204 put g gBig 1 2205 else 2206 for j ulong n 2207 sbc get ab j 2208 end 2209 end 2210 end 2211 note argval i: the value of an option; longer than 65535 bytes is past the path limit as well 2212 fn argval i ulong to void 2213 argsb i 2214 if eq get g gBig 1 2215 diek kTooLong 2 2216 end 2217 end 2218 note okch b: [A-Za-z0-9._-] 2219 fn okch b uchar to bool 2220 if and ge b 48 le b 57 2221 ret true 2222 end 2223 if and ge b 65 le b 90 2224 ret true 2225 end 2226 if and ge b 97 le b 122 2227 ret true 2228 end 2229 ret or or eq b 46 eq b 95 eq b 45 2230 end 2231 fn parse to void 2232 if not envset kEPAIRS sPairs 2233 svk sPairs kFive 2234 end 2235 put sl sLedger 0 2236 c = ulong argcnt 2237 i = ulong 1 2238 while lt i c bound `9223372036854775807` 2239 argsb i 2240 if sbeqk kLedger 2241 if ge add i 1 c 2242 diek kLedgerNeeds 2 2243 end 2244 argval add i 1 2245 svset sLedger 2246 i = add i 2 2247 else 2248 if sbeqk kDry 2249 put g gDry 1 2250 i = add i 1 2251 else 2252 if sbeqk kPairs 2253 if ge add i 1 c 2254 diek kPairsNeeds 2 2255 end 2256 argval add i 1 2257 svset sPairs 2258 i = add i 2 2259 else 2260 tbpos kUnknown 2261 r = write 2 tb get kp 0 get kp 1 2262 if eq get g gBig 1 2263 wr = write 2 ab 0 get g gAl 2264 end 2265 out 2 2266 exit 2 2267 end 2268 end 2269 end 2270 end 2271 sbclr 2272 end 2273 fn check to void 2274 if eq get sl sLedger 0 2275 diek kNoLedger 2 2276 end 2277 o = mul sLedger 4096 2278 for i ulong get sl sLedger 2279 if not okch get sv add o i 2280 diek kBadLedger 2 2281 end 2282 end 2283 n = get sl sPairs 2284 if eq n 0 2285 diek kBadPairs 2 2286 end 2287 p = mul sPairs 4096 2288 v = ulong 0 2289 big = false 2290 for j ulong n 2291 b = get sv add p j 2292 if or lt b 48 gt b 57 2293 diek kBadPairs 2 2294 end 2295 d = ulong sub b 48 2296 if gt v div sub `9223372036854775807` d 10 2297 big = true 2298 else 2299 v = add mul v 10 d 2300 end 2301 end 2302 if big 2303 sbk kPairsPre 2304 sbs sPairs 2305 sbk kPairsBig 2306 out 2 2307 exit 2 2308 end 2309 if lt v 5 2310 sbk kPairsPre 2311 sbs sPairs 2312 sbk kPairsLt 2313 out 2 2314 exit 2 2315 end 2316 put g gPairs v 2317 end 2318 fn paths to void 2319 tbpos kSelfExe 2320 r = readlnk tb get kp 0 get kp 1 sb 0 4096 2321 if or le r 0 ge r 4096 2322 diek kNoExe 2 2323 end 2324 put sn 0 ulong r 2325 svset sHere 2326 dirup sHere 2327 dirup sHere 2328 if not envset kECUDAPP sCudapp 2329 sbs sHere 2330 sbk kCudappRel 2331 svset sCudapp 2332 end 2333 if not envset kETORCH sTorch 2334 sbs sHere 2335 sbk kTorchRel 2336 svset sTorch 2337 end 2338 sbs sHere 2339 sbk kOutRel 2340 sbs sLedger 2341 svset sOut 2342 sbk kNamePre 2343 sbs sLedger 2344 svset sName 2345 if not envset kECUBIN sCubins 2346 sbs sCudapp 2347 sbk kCubinsRel 2348 svset sCubins 2349 end 2350 sbs sCubins 2351 sbk kMmRel 2352 svset sMm 2353 sbs sCubins 2354 sbk kGqaRel 2355 svset sGqa 2356 sbs sCudapp 2357 sbk kStaleRel 2358 svset sStale 2359 sbs sHere 2360 sbk kCbuildRel 2361 svset sCbuild 2362 if not envset kEPY sPy 2363 svk sPy kPython3 2364 end 2365 end 2366 fn main to int 2367 put g gDry 0 2368 put g gOpened 0 2369 put ds 0 -1 2370 initk 2371 parse 2372 check 2373 envscan 2374 paths 2375 if eq get g gDry 1 2376 dryrun 2377 end 2378 device 2379 ret 0 2380 end