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/interplane-forge/blob/main/tools/doi/doi.om # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / tools / doi / doi.om` 2111 lines ยท 37297 bytes Raw Blame History 1 om2 2 note tools/doi/doi.om: the engine of tools/doi/doi.sh, which reserves a Zenodo DOI for a paper 3 note repository. doi.sh is the command authors run; it calls these subcommands: 4 note snapshot INPUT OUTPUT copy a nofollow regular descriptor into an exclusive private output 5 note token ZENODO_TOKEN is set and safe inside a quoted curl config line (never printed) 6 note base MODE the API base for MODE, sandbox (the default of doi.sh) or production 7 note check REPO FILE... .zenodo.json, LICENSE and the files to upload; every problem on stderr, exit 1 8 note body REPO the create request: .zenodo.json as metadata, with prereserve_doi 9 note pick MODE FILE WHAT one checked value of a Zenodo answer: bucket publish html doi pubdoi key size message 10 note serve MODE REPO WORK [SOURCE] retains directory custody and metadata across the network phase 11 note consent MODE DOI asks on /dev/tty; exit 0 only when the line typed there is exactly yes 12 note JSON: a file is read whole into gj and checked by recursive descent (modelled on 13 note harness-observe tools/jflat.om at `92eba82c`, not vendored). Nesting is limited by the 8 MiB stack 14 note (trap 6, exit 106). gj holds `16777216` bytes, the largest global array the pinned omc builds (it 15 note has no run-time growth); a file of `16777216` bytes or more is refused with a message. gj is 16 note zero-filled and read once, so gj[n] after the n bytes read is 0: lookahead there needs no check. 17 note Every loop that walks input carries the largest bound: each pass consumes at least one byte. 18 19 global gj uchar `16777216` 20 global gn ulong 1 21 global gmode ulong 1 22 global gprod uchar 1 23 global gbad uchar 1 24 global gfd long 4 25 global gparent long 1 26 27 text tpfx "doi: " 28 text tnl "\n" 29 text tsp " " 30 text tind " " 31 text tcolon ": " 32 text tusage "usage: doi token | base MODE | snapshot INPUT OUTPUT | check REPO FILE... | body REPO | pick MODE FILE WHAT | serve MODE REPO WORK [SOURCE] | consent MODE DOI\n(MODE is sandbox or production. Authors run tools/doi/doi.sh, not this program.)\n" 33 text wtoken "token" 34 text wbase "base" 35 text wcheck "check" 36 text wsnap "snapshot" 37 text wbody "body" 38 text wpick "pick" 39 text wserve "serve" 40 text tdot "." 41 text troot "/" 42 text tready "READY\n" 43 text tsaved "SAVED\n" 44 text tdone "DONE\n" 45 text tin "/in" 46 text mipc "keeper IPC failed" 47 text mkdead "keeper exited unsuccessfully; no upload permitted" 48 text tbody "body.json" 49 text tmcopy "repo/.zenodo.json" 50 text tfiles "files/" 51 text mdrift "metadata changed since submission; edit preserved, draft kept unpublished (see Draft and Reserved DOI above)" 52 text mcand "write-back candidate was replaced before exchange; original metadata restored, draft kept unpublished (see Draft and Reserved DOI above)" 53 text manchor "cannot anchor path without following symlinks: " 54 text mlock "another DOI writer holds this repository" 55 text wconsent "consent" 56 text wpubdoi "pubdoi" 57 text ktitle "title" 58 text kdesc "description" 59 text kcreat "creators" 60 text kname "name" 61 text klic "license" 62 text kid "id" 63 text kupt "upload_type" 64 text kpubt "publication_type" 65 text kimgt "image_type" 66 text kdoi "doi" 67 text kpre "prereserve_doi" 68 text klinks "links" 69 text kbucket "bucket" 70 text kpublish "publish" 71 text khtml "html" 72 text kmeta "metadata" 73 text ksize "size" 74 text kkey "key" 75 text kmsg "message" 76 text kerrs "errors" 77 text kfield "field" 78 text kmsgs "messages" 79 text ktrue "true" 80 text kfalse "false" 81 text knull "null" 82 text tpub "publication" 83 text timage "image" 84 text tuptl "poster\npresentation\ndataset\nvideo\nsoftware\nlesson\nphysicalobject\nother\n" 85 text tlics "LICENSE\nLICENSE.md\nLICENSE.txt\nLICENCE\nCOPYING\n" 86 text tzj ".zenodo.json" 87 text tznew ".zenodo.json.doi-new" 88 text ttty "/dev/tty" 89 text tzpfx "ZENODO_TOKEN=" 90 text tsand "sandbox" 91 text tprod "production" 92 text osand "https://sandbox.zenodo.org" 93 text oprod "https://zenodo.org" 94 text papi "/api" 95 text pfiles "/api/files/" 96 text pdep "/api/deposit/depositions/" 97 text phtml "/" 98 text hdep "deposit/" 99 text hrec "records/" 100 text sactpub "/actions/publish" 101 text dsand "10.5072/" 102 text dprod "10.5281/" 103 text tpdf ".pdf" 104 text thead "{\"metadata\":{\"prereserve_doi\":true," 105 text ttail "}}\n" 106 text tdoiq "\"doi\": \"" 107 text tqc "\"," 108 text tyes "yes" 109 text mbig " is `16777216` bytes or longer (the largest array the pinned omc builds)" 110 text mopen "cannot open " 111 text mread "cannot read " 112 text mnotobj ".zenodo.json is not one JSON object (check its commas, quotes and brackets)" 113 text mmiss ".zenodo.json: missing or not a string: " 114 text mempty ".zenodo.json: empty: " 115 text mcrarr ".zenodo.json: creators must be an array of objects, each with a non-empty \"name\" (\"Family, Given\")" 116 text mcrnone ".zenodo.json: creators is empty: name at least one author" 117 text mlic ".zenodo.json: license must be a non-empty string such as \"cc-by-4.0\", or an object with a non-empty \"id\"" 118 text mupt ".zenodo.json: upload_type must be one of publication poster presentation dataset image video software lesson physicalobject other" 119 text mhasdoi ".zenodo.json already has a \"doi\": one was reserved before. Publish or delete that draft on Zenodo; remove the line only to reserve a new DOI" 120 text mhaspre ".zenodo.json must not set prereserve_doi: this tool sets it" 121 text mesck ".zenodo.json: a top-level key holds a backslash escape; write keys plainly" 122 text mnolic "no LICENSE file at the top of the repository (LICENSE, LICENSE.md, LICENSE.txt, LICENCE or COPYING, not empty)" 123 text mnofile "no file to upload: put the paper's PDF at the top of the repository" 124 text mnopdf "no PDF among the files to upload: the paper's PDF is required" 125 text mfname "a file name may hold only A-Z a-z 0-9 - . _ and must not start with a dot: " 126 text mnotreg "not a non-empty regular file, or a symlink (refused): " 127 text mdup "two files to upload share the name " 128 text mlong "an argument is longer than 4095 bytes" 129 text mmode "MODE must be sandbox or production, not " 130 text mcd "cannot enter the repository " 131 text mnotok "Zenodo's answer is not one JSON object: " 132 text mabsent "Zenodo's answer lacks a usable " 133 text murlbad "Zenodo answer has a missing or invalid HTTPS URL" 134 text murl "refused: Zenodo's answer points outside " 135 text mbaddoi "refused: not a DOI under " 136 text mwhat "WHAT must be bucket publish html doi pubdoi key size or message, not " 137 text mwrite "cannot write " 138 text mrename "cannot exchange metadata; inspect .zenodo.json and .zenodo.json.doi-new; nothing was published" 139 text mtokno "ZENODO_TOKEN is not set: create a personal access token on Zenodo (scopes deposit:write and deposit:actions) and export it in this shell only" 140 text mtokbad "ZENODO_TOKEN holds a byte other than A-Z a-z 0-9 - . _ ~ + / = (the token is never printed)" 141 text mnotty "no terminal at /dev/tty: publishing needs the author to type yes there; nothing was published" 142 text qpub1 "\nPublish " 143 text qpub2 " on " 144 text qsand "?\nThis is the Zenodo sandbox: a test record whose DOI is never registered.\n" 145 text qprod "?\nThis is production Zenodo: the DOI is registered for good, and a published record and its files can never be deleted.\n" 146 text qpub3 "Type yes to publish; anything else keeps the draft unpublished: " 147 148 note ---- output and exits 149 fn wall fd int b view uchar * to bool 150 n = ulong 0 151 while lt n len b bound `9223372036854775807` 152 r = write fd b n sub len b n 153 if le r 0 154 ret false 155 end 156 n = add n ulong r 157 end 158 ret true 159 end 160 161 fn say b view uchar * to void 162 x = wall 1 b 163 end 164 165 fn warn b view uchar * to void 166 x = wall 2 b 167 end 168 169 note outc: untrusted bytes (Zenodo's text, paths) with control bytes shown as '?' 170 fn outc fd int b view uchar * to void 171 o = new uchar 4096 0 172 n = ulong 0 173 for i ulong len b bound `9223372036854775807` 174 c = get b i 175 if or lt c 32 eq c 127 176 c = uchar 63 177 end 178 put o n c 179 n = add n 1 180 if eq n 4096 181 x = wall fd part o 0 n 182 n = ulong 0 183 end 184 end 185 x = wall fd part o 0 n 186 end 187 188 fn usg to void 189 warn tusage 190 exit 2 191 end 192 193 fn die m view uchar * to void 194 warn tpfx 195 warn m 196 warn tnl 197 exit 1 198 end 199 200 fn die2 m view uchar * a view uchar * to void 201 warn tpfx 202 warn m 203 outc 2 a 204 warn tnl 205 exit 1 206 end 207 208 note bad, bad2: one problem on stderr; the command exits 1 after every check has run 209 fn bad m view uchar * to void 210 warn tpfx 211 warn m 212 warn tnl 213 put gbad 0 1 214 end 215 216 fn bad2 m view uchar * a view uchar * to void 217 warn tpfx 218 warn m 219 outc 2 a 220 warn tnl 221 put gbad 0 1 222 end 223 224 note ---- bytes 225 note cpy: copy s into d at n; returns the new end (d too short traps 3: callers size d first) 226 fn cpy d edit uchar * n ulong s view uchar * to ulong 227 for i ulong len s bound `9223372036854775807` 228 put d add n i get s i 229 end 230 ret add n len s 231 end 232 233 fn beq a view uchar * b view uchar * to bool 234 if ne len a len b 235 ret false 236 end 237 for i ulong len a bound `9223372036854775807` 238 if ne get a i get b i 239 ret false 240 end 241 end 242 ret true 243 end 244 245 note bat: b[i..] starts with t 246 fn bat b view uchar * i ulong t view uchar * to bool 247 if gt add i len t len b 248 ret false 249 end 250 for k ulong len t bound `9223372036854775807` 251 if ne get b add i k get t k 252 ret false 253 end 254 end 255 ret true 256 end 257 258 fn lcb c uchar to uchar 259 if and ge c 65 le c 90 260 ret add c 32 261 end 262 ret c 263 end 264 265 note bati: as bat, ASCII case-insensitive (t is lowercase) 266 fn bati b view uchar * i ulong t view uchar * to bool 267 if gt add i len t len b 268 ret false 269 end 270 for k ulong len t bound `9223372036854775807` 271 if ne lcb get b add i k get t k 272 ret false 273 end 274 end 275 ret true 276 end 277 278 fn isdig c uchar to bool 279 ret and ge c 48 le c 57 280 end 281 282 fn isaln c uchar to bool 283 a = and ge c 65 le c 90 284 b = and ge c 97 le c 122 285 ret or or a b isdig c 286 end 287 288 fn ishex c uchar to bool 289 if isdig c 290 ret true 291 end 292 a = and ge c 97 le c 102 293 b = and ge c 65 le c 70 294 ret or a b 295 end 296 297 note namech: a byte of a file name or of a URL or DOI path segment: A-Z a-z 0-9 - . _ 298 fn namech c uchar to bool 299 if isaln c 300 ret true 301 end 302 ret or or eq c 45 eq c 46 eq c 95 303 end 304 305 note tokch: a token byte that cannot end or escape a double-quoted curl config value 306 fn tokch c uchar to bool 307 if namech c 308 ret true 309 end 310 ret or or eq c 126 eq c 43 or eq c 47 eq c 61 311 end 312 313 fn rd16 b view uchar * o ulong to ulong 314 ret or ulong get b o shl ulong get b add o 1 8 315 end 316 317 fn rd32 b view uchar * o ulong to ulong 318 lo = rd16 b o 319 ret or lo shl rd16 b add o 2 16 320 end 321 322 fn rd64 b view uchar * o ulong to ulong 323 lo = rd32 b o 324 ret or lo shl rd32 b add o 4 32 325 end 326 327 fn wr16 b edit uchar * o ulong v ulong to void 328 put b o uchar and v 255 329 put b add o 1 uchar and shr v 8 255 330 end 331 332 note ---- JSON in gj 333 fn isws c uchar to bool 334 ret or or eq c 32 eq c 9 or eq c 10 eq c 13 335 end 336 337 note ws: the first index at or after i that is not JSON whitespace (it stops at the 0 after the input) 338 fn ws i ulong to ulong 339 k = i 340 while isws get gj k bound `9223372036854775807` 341 k = add k 1 342 end 343 ret k 344 end 345 346 fn isesc e uchar to bool 347 if or or eq e 34 eq e 92 eq e 47 348 ret true 349 end 350 if or or eq e 98 eq e 102 eq e 110 351 ret true 352 end 353 ret or eq e 114 eq e 116 354 end 355 356 note skstr: the index after the string whose opening quote is at i, or -1 when it is not one 357 fn skstr i ulong to long 358 if ne get gj i 34 359 ret -1 360 end 361 k = add i 1 362 n = get gn 0 363 while lt k n bound `9223372036854775807` 364 c = get gj k 365 if eq c 34 366 ret long add k 1 367 end 368 if lt c 32 369 ret -1 370 end 371 if eq c 92 372 k = add k 1 373 e = get gj k 374 if eq e 117 375 for j ulong 4 376 k = add k 1 377 if ge k n 378 ret -1 379 end 380 if not ishex get gj k 381 ret -1 382 end 383 end 384 else 385 if not isesc e 386 ret -1 387 end 388 end 389 end 390 k = add k 1 391 end 392 ret -1 393 end 394 395 note atg: gj[i..] starts with t 396 fn atg i ulong t view uchar * to bool 397 if gt add i len t get gn 0 398 ret false 399 end 400 for k ulong len t bound `9223372036854775807` 401 if ne get gj add i k get t k 402 ret false 403 end 404 end 405 ret true 406 end 407 408 fn digs k ulong to ulong 409 m = k 410 while isdig get gj m bound `9223372036854775807` 411 m = add m 1 412 end 413 ret m 414 end 415 416 note sknum: the index after the JSON number at i, or -1 417 fn sknum i ulong to long 418 k = i 419 if eq get gj k 45 420 k = add k 1 421 end 422 if not isdig get gj k 423 ret -1 424 end 425 if eq get gj k 48 426 k = add k 1 427 else 428 k = digs k 429 end 430 if eq get gj k 46 431 k = add k 1 432 if not isdig get gj k 433 ret -1 434 end 435 k = digs k 436 end 437 c = get gj k 438 if or eq c 101 eq c 69 439 k = add k 1 440 c = get gj k 441 if or eq c 43 eq c 45 442 k = add k 1 443 end 444 if not isdig get gj k 445 ret -1 446 end 447 k = digs k 448 end 449 ret long k 450 end 451 452 fn sklit i ulong t view uchar * to long 453 if atg i t 454 ret long add i len t 455 end 456 ret -1 457 end 458 459 note skval: the index after the JSON value at i, or -1 460 fn skval i ulong to long 461 c = get gj i 462 if eq c 34 463 ret skstr i 464 end 465 if eq c 123 466 ret skobj i 467 end 468 if eq c 91 469 ret skarr i 470 end 471 if eq c 116 472 ret sklit i ktrue 473 end 474 if eq c 102 475 ret sklit i kfalse 476 end 477 if eq c 110 478 ret sklit i knull 479 end 480 ret sknum i 481 end 482 483 fn skobj i ulong to long 484 k = ws add i 1 485 if eq get gj k 125 486 ret long add k 1 487 end 488 n = get gn 0 489 while lt k n bound `9223372036854775807` 490 e = skstr k 491 if lt e 0 492 ret -1 493 end 494 k = ws ulong e 495 if ne get gj k 58 496 ret -1 497 end 498 e = skval ws add k 1 499 if lt e 0 500 ret -1 501 end 502 k = ws ulong e 503 c = get gj k 504 if eq c 125 505 ret long add k 1 506 end 507 if ne c 44 508 ret -1 509 end 510 k = ws add k 1 511 end 512 ret -1 513 end 514 515 fn skarr i ulong to long 516 k = ws add i 1 517 if eq get gj k 93 518 ret long add k 1 519 end 520 n = get gn 0 521 while lt k n bound `9223372036854775807` 522 e = skval k 523 if lt e 0 524 ret -1 525 end 526 k = ws ulong e 527 c = get gj k 528 if eq c 93 529 ret long add k 1 530 end 531 if ne c 44 532 ret -1 533 end 534 k = ws add k 1 535 end 536 ret -1 537 end 538 539 note docok: gj holds exactly one JSON object, with only whitespace around it 540 fn docok to bool 541 k = ws 0 542 if ne get gj k 123 543 ret false 544 end 545 e = skobj k 546 if lt e 0 547 ret false 548 end 549 ret eq ws ulong e get gn 0 550 end 551 552 note mem: where the value of member ky starts in the checked object at o, or -1 553 fn mem o ulong ky view uchar * to long 554 k = ws add o 1 555 if eq get gj k 125 556 ret -1 557 end 558 n = get gn 0 559 while lt k n bound `9223372036854775807` 560 e = ulong skstr k 561 hit = and eq sub sub e k 2 len ky atg add k 1 ky 562 v = ws add ws e 1 563 if hit 564 ret long v 565 end 566 k = ws ulong skval v 567 if eq get gj k 125 568 ret -1 569 end 570 k = ws add k 1 571 end 572 ret -1 573 end 574 575 note mtyp: as mem, and -1 when the value does not start with byte c (34 string, 123 object, 91 array) 576 fn mtyp o ulong ky view uchar * c uchar to long 577 v = mem o ky 578 if lt v 0 579 ret -1 580 end 581 if ne get gj ulong v c 582 ret -1 583 end 584 ret v 585 end 586 587 note inner: the string member b of the member a (of kind c) of the object at t, or -1 588 fn inner t ulong a view uchar * c uchar b view uchar * to long 589 v = mtyp t a c 590 if lt v 0 591 ret -1 592 end 593 ret mtyp ulong v b 34 594 end 595 596 note nonemp: v is a string with at least one byte between its quotes 597 fn nonemp v long to bool 598 if lt v 0 599 ret false 600 end 601 u = ulong v 602 if ne get gj u 34 603 ret false 604 end 605 ret gt sub sub ulong skstr u u 2 0 606 end 607 608 note streq: the string at v is exactly t as written (no escapes) 609 fn streq v ulong t view uchar * to bool 610 e = ulong skstr v 611 ret and eq sub sub e v 2 len t atg add v 1 t 612 end 613 614 note inlist: the string at v is one of the lines of l 615 fn inlist v ulong l view uchar * to bool 616 s = ulong 0 617 for i ulong len l bound `9223372036854775807` 618 if eq get l i 10 619 if streq v part l s sub i s 620 ret true 621 end 622 s = add i 1 623 end 624 end 625 ret false 626 end 627 628 fn unesc e uchar to uchar 629 if eq e 98 630 ret 8 631 end 632 if eq e 102 633 ret 12 634 end 635 if eq e 110 636 ret 10 637 end 638 if eq e 114 639 ret 13 640 end 641 if eq e 116 642 ret 9 643 end 644 if eq e 117 645 ret 127 646 end 647 ret e 648 end 649 650 note sdec: the string at v (its opening quote) decoded into o; returns its length. \uXXXX becomes 651 note byte 127, which every check refuses and outc shows as '?'. When o is too short: with cut its 652 note first len o bytes, else -1. 653 fn sdec v ulong o edit uchar * cut bool to long 654 e = sub ulong skstr v 1 655 k = add v 1 656 m = ulong 0 657 while lt k e bound `9223372036854775807` 658 c = get gj k 659 if eq c 92 660 k = add k 1 661 c = unesc get gj k 662 if eq get gj k 117 663 k = add k 4 664 end 665 end 666 if ge m len o 667 if cut 668 ret long m 669 end 670 ret -1 671 end 672 put o m c 673 m = add m 1 674 k = add k 1 675 end 676 ret long m 677 end 678 679 note wgj: write gj[s..e) to fd 680 fn wgj fd int s ulong e ulong to bool 681 n = s 682 while lt n e bound `9223372036854775807` 683 r = write fd gj n sub e n 684 if le r 0 685 ret false 686 end 687 n = add n ulong r 688 end 689 ret true 690 end 691 692 note slurp: the whole file p into gj; its permission bits into gmode 693 fn slurp p view uchar * to void 694 fd = openreg p 695 if lt fd 0 696 die2 mopen p 697 end 698 st = new uchar 128 0 699 x = fstat int fd st 0 128 700 if lt x 0 701 die2 mread p 702 end 703 put gmode 0 and rd32 st 16 4095 704 n = ulong 0 705 while true bound `9223372036854775807` 706 r = read int fd gj n sub `16777216` n 707 if eq r -4 708 continue 709 end 710 if lt r 0 711 die2 mread p 712 end 713 if eq r 0 714 break 715 end 716 n = add n ulong r 717 if eq n `16777216` 718 die2 p mbig 719 end 720 end 721 x = fclose int fd 722 put gn 0 n 723 end 724 725 note ---- .zenodo.json rules 726 fn need t ulong ky view uchar * to void 727 v = mtyp t ky 34 728 if lt v 0 729 bad2 mmiss ky 730 ret 731 end 732 if not nonemp v 733 bad2 mempty ky 734 end 735 end 736 737 fn crs t ulong to void 738 a = mtyp t kcreat 91 739 if lt a 0 740 bad mcrarr 741 ret 742 end 743 k = ws add ulong a 1 744 if eq get gj k 93 745 bad mcrnone 746 ret 747 end 748 n = get gn 0 749 while lt k n bound `9223372036854775807` 750 if ne get gj k 123 751 bad mcrarr 752 ret 753 end 754 if not nonemp mtyp k kname 34 755 bad mcrarr 756 ret 757 end 758 k = ws ulong skval k 759 if eq get gj k 93 760 ret 761 end 762 k = ws add k 1 763 end 764 end 765 766 fn lic t ulong to void 767 v = mem t klic 768 if lt v 0 769 bad2 mmiss klic 770 ret 771 end 772 if eq get gj ulong v 123 773 v = mtyp ulong v kid 34 774 end 775 if not nonemp v 776 bad mlic 777 end 778 end 779 780 fn upt t ulong to void 781 v = mtyp t kupt 34 782 if lt v 0 783 bad2 mmiss kupt 784 ret 785 end 786 u = ulong v 787 if streq u tpub 788 need t kpubt 789 ret 790 end 791 if streq u timage 792 need t kimgt 793 ret 794 end 795 if not inlist u tuptl 796 bad mupt 797 end 798 end 799 800 note pkeys: no top-level key holds an escape, so mem sees every key as written 801 fn pkeys t ulong to void 802 k = ws add t 1 803 if eq get gj k 125 804 ret 805 end 806 n = get gn 0 807 while lt k n bound `9223372036854775807` 808 e = ulong skstr k 809 for j ulong sub sub e k 2 bound `9223372036854775807` 810 if eq get gj add add k 1 j 92 811 bad mesck 812 ret 813 end 814 end 815 k = ws ulong skval ws add ws e 1 816 if eq get gj k 125 817 ret 818 end 819 k = ws add k 1 820 end 821 end 822 823 note meta: every rule for .zenodo.json (in gj); problems go to bad 824 fn meta to void 825 if not docok 826 bad mnotobj 827 ret 828 end 829 t = ws 0 830 pkeys t 831 need t ktitle 832 need t kdesc 833 crs t 834 lic t 835 upt t 836 if ge mem t kdoi 0 837 bad mhasdoi 838 end 839 if ge mem t kpre 0 840 bad mhaspre 841 end 842 end 843 844 note ---- files 845 note openreg: O_CLOEXEC | O_NONBLOCK | O_NOFOLLOW on Linux AArch64 (asm/fcntl.h). 846 note Opening and fstat use the same descriptor: no stat/open symlink race, and no FIFO wait. 847 note walkopen restores cwd; every ancestor opened through the previous directory fd. 848 note AArch64 O_DIRECTORY=16384, O_NOFOLLOW=32768, O_CLOEXEC=524288. 849 fn walkopen p view uchar * flags int mode int to long 850 home = fopen tdot 0 len tdot 540672 0 851 if lt home 0 852 ret -1 853 end 854 r = long 0 855 start = ulong 0 856 if eq len p 0 857 x = fclose int home 858 ret -1 859 end 860 if eq get p 0 47 861 r = chdir troot 0 len troot 862 start = ulong 1 863 end 864 if ge r 0 865 r = walkrel p start flags mode 866 end 867 x = fchdir int home 868 y = fclose int home 869 if lt x 0 870 die manchor 871 end 872 ret r 873 end 874 875 fn walkrel p view uchar * start ulong flags int mode int to long 876 s = start 877 for i ulong sub len p start bound `9223372036854775807` 878 j = add start i 879 if eq get p j 47 880 if eq j s 881 ret -1 882 end 883 if not compok part p s sub j s 884 ret -1 885 end 886 fd = fopen p s sub j s or int 540672 32768 0 887 if lt fd 0 888 ret -1 889 end 890 r = fchdir int fd 891 x = fclose int fd 892 if lt r 0 893 ret -1 894 end 895 s = add j 1 896 end 897 end 898 if ge s len p 899 ret -1 900 end 901 if not compok part p s sub len p s 902 ret -1 903 end 904 ret fopen p s sub len p s flags mode 905 end 906 907 fn compok p view uchar * to bool 908 if beq p tdot 909 ret false 910 end 911 if eq len p 2 912 if and eq get p 0 46 eq get p 1 46 913 ret false 914 end 915 end 916 ret true 917 end 918 919 fn openreg p view uchar * to long 920 fd = walkopen p or int 526336 32768 0 921 if lt fd 0 922 ret -1 923 end 924 st = new uchar 128 0 925 x = fstat int fd st 0 128 926 if ge x 0 927 if eq and rd32 st 16 61440 32768 928 if gt rd64 st 48 0 929 ret fd 930 end 931 end 932 end 933 y = fclose int fd 934 ret -1 935 end 936 937 fn regfile p view uchar * to bool 938 fd = openreg p 939 if lt fd 0 940 ret false 941 end 942 x = fclose int fd 943 ret true 944 end 945 946 note copyfd: descriptor to descriptor; the caller never reopens the input path after its check 947 fn copyfd inf int outf int to bool 948 buf = new uchar 65536 0 949 total = ulong 0 950 while true bound `9223372036854775807` 951 r = read inf buf 0 len buf 952 if eq r -4 953 continue 954 end 955 if lt r 0 956 ret false 957 end 958 if eq r 0 959 ret gt total 0 960 end 961 n = ulong 0 962 while lt n ulong r bound `9223372036854775807` 963 k = write outf buf n sub ulong r n 964 if eq k -4 965 continue 966 end 967 if le k 0 968 ret false 969 end 970 n = add n ulong k 971 end 972 total = add total ulong r 973 end 974 ret false 975 end 976 977 note snapshot INPUT OUTPUT: freeze a checked nofollow descriptor into an exclusive private file. 978 fn dosnap ac ulong to int 979 if ne ac 4 980 usg 981 end 982 p = new uchar 4096 0 983 o = new uchar 4096 0 984 pn = getarg 2 p 985 on = getarg 3 o 986 inf = openreg part p 0 pn 987 if lt inf 0 988 die2 mnotreg part p 0 pn 989 end 990 outf = fopen o 0 on 524481 384 991 if lt outf 0 992 x = fclose int inf 993 die2 mwrite part o 0 on 994 end 995 ok = copyfd int inf int outf 996 x = fclose int inf 997 y = fclose int outf 998 if or not ok or lt x 0 lt y 0 999 z = unlink o 0 on 1000 die2 mwrite part o 0 on 1001 end 1002 ret 0 1003 end 1004 1005 fn bname p view uchar * to ulong 1006 s = ulong 0 1007 for i ulong len p bound `9223372036854775807` 1008 if eq get p i 47 1009 s = add i 1 1010 end 1011 end 1012 ret s 1013 end 1014 1015 fn nameok p view uchar * s ulong to bool 1016 if ge s len p 1017 ret false 1018 end 1019 if eq get p s 46 1020 ret false 1021 end 1022 for i ulong sub len p s bound `9223372036854775807` 1023 if not namech get p add s i 1024 ret false 1025 end 1026 end 1027 ret true 1028 end 1029 1030 note fchk: one file to upload; true when it is a PDF 1031 fn fchk p view uchar * to bool 1032 s = bname p 1033 if not nameok p s 1034 bad2 mfname p 1035 ret false 1036 end 1037 if not regfile p 1038 bad2 mnotreg p 1039 ret false 1040 end 1041 if lt len p 4 1042 ret false 1043 end 1044 ret bati p sub len p 4 tpdf 1045 end 1046 1047 note samename: compare base names without keeping both sliced views live in the argument loop 1048 fn samename p view uchar * b view uchar * to bool 1049 s = bname p 1050 t = bname b 1051 n = sub len p s 1052 if ne n sub len b t 1053 ret false 1054 end 1055 for j ulong n bound `9223372036854775807` 1056 if ne get p add s j get b add t j 1057 ret false 1058 end 1059 end 1060 ret true 1061 end 1062 1063 note dupchk: no later file argument shares the base name of argument i (p) 1064 fn dupchk i ulong ac ulong p view uchar * to void 1065 b = new uchar 4096 0 1066 for j ulong sub sub ac i 1 bound `9223372036854775807` 1067 k = getarg add add i j 1 b 1068 if samename p part b 0 k 1069 bad2 mdup p 1070 ret 1071 end 1072 end 1073 end 1074 1075 fn licok to bool 1076 s = ulong 0 1077 for i ulong len tlics bound `9223372036854775807` 1078 if eq get tlics i 10 1079 if regfile part tlics s sub i s 1080 ret true 1081 end 1082 s = add i 1 1083 end 1084 end 1085 ret false 1086 end 1087 1088 note ---- arguments and mode 1089 fn getarg i ulong b edit uchar * to ulong 1090 r = argread i b 0 len b 1091 if lt r 0 1092 die mlong 1093 end 1094 ret ulong r 1095 end 1096 1097 fn setmode i ulong to void 1098 b = new uchar 4096 0 1099 k = getarg i b 1100 if beq part b 0 k tsand 1101 put gprod 0 0 1102 ret 1103 end 1104 if beq part b 0 k tprod 1105 put gprod 0 1 1106 ret 1107 end 1108 die2 mmode part b 0 k 1109 end 1110 1111 note origin: the scheme and host of the mode, without a final slash 1112 fn origin o edit uchar * to ulong 1113 if eq get gprod 0 1 1114 ret cpy o 0 oprod 1115 end 1116 ret cpy o 0 osand 1117 end 1118 1119 note dpfx: the DOI prefix the mode's Zenodo mints under 1120 fn dpfx o edit uchar * to ulong 1121 if eq get gprod 0 1 1122 ret cpy o 0 dprod 1123 end 1124 ret cpy o 0 dsand 1125 end 1126 1127 fn cdrepo i ulong to void 1128 b = new uchar 4096 0 1129 k = getarg i b 1130 fd = walkopen part b 0 k or int 540672 32768 0 1131 if lt fd 0 1132 die2 mcd part b 0 k 1133 end 1134 r = fchdir int fd 1135 x = fclose int fd 1136 if lt r 0 1137 die mcd 1138 end 1139 end 1140 1141 note urlok: u is p and then at least one byte of A-Z a-z 0-9 - . _ / 1142 fn urlok u view uchar * p view uchar * to bool 1143 if le len u len p 1144 ret false 1145 end 1146 if not bat u 0 p 1147 ret false 1148 end 1149 for i ulong sub len u len p bound `9223372036854775807` 1150 c = get u add len p i 1151 if not namech c 1152 if ne c 47 1153 ret false 1154 end 1155 end 1156 end 1157 ret true 1158 end 1159 1160 fn endsw u view uchar * t view uchar * to bool 1161 if lt len u len t 1162 ret false 1163 end 1164 ret bat u sub len u len t t 1165 end 1166 1167 fn doichk d view uchar * to void 1168 p = new uchar 16 0 1169 k = dpfx p 1170 if not urlok d part p 0 k 1171 warn tpfx 1172 warn mbaddoi 1173 warn part p 0 k 1174 warn tcolon 1175 outc 2 d 1176 warn tnl 1177 exit 1 1178 end 1179 end 1180 1181 note ---- commands 1182 fn dotoken to int 1183 b = new uchar 131073 0 1184 n = ulong envcnt 1185 for i ulong n bound `9223372036854775807` 1186 r = envread i b 0 131073 1187 if lt r 0 1188 continue 1189 end 1190 k = ulong r 1191 if bat part b 0 k 0 tzpfx 1192 if eq k len tzpfx 1193 die mtokno 1194 end 1195 for j ulong sub k len tzpfx bound `9223372036854775807` 1196 if not tokch get b add len tzpfx j 1197 die mtokbad 1198 end 1199 end 1200 ret 0 1201 end 1202 end 1203 die mtokno 1204 ret 1 1205 end 1206 1207 fn dobase ac ulong to int 1208 if ne ac 3 1209 usg 1210 end 1211 setmode 2 1212 b = new uchar 64 0 1213 k = origin b 1214 k = cpy b k papi 1215 say part b 0 k 1216 say tnl 1217 ret 0 1218 end 1219 1220 fn docheck ac ulong to int 1221 if lt ac 3 1222 usg 1223 end 1224 a = new uchar 4096 0 1225 pdfs = ulong 0 1226 for j ulong sub ac 3 bound `9223372036854775807` 1227 i = add j 3 1228 k = getarg i a 1229 if fchk part a 0 k 1230 pdfs = add pdfs 1 1231 end 1232 dupchk i ac part a 0 k 1233 end 1234 if eq ac 3 1235 bad mnofile 1236 else 1237 if eq pdfs 0 1238 bad mnopdf 1239 end 1240 end 1241 cdrepo 2 1242 if not licok 1243 bad mnolic 1244 end 1245 slurp tzj 1246 meta 1247 ret int get gbad 0 1248 end 1249 1250 fn dobody ac ulong to int 1251 if ne ac 3 1252 usg 1253 end 1254 cdrepo 2 1255 slurp tzj 1256 meta 1257 if eq get gbad 0 1 1258 ret 1 1259 end 1260 t = ws 0 1261 e = sub ulong skobj t 1 1262 say thead 1263 if not wgj 1 add t 1 e 1264 ret 1 1265 end 1266 say ttail 1267 ret 0 1268 end 1269 1270 note One bucket component; publish/html bind exact decimal create id, no slash/dot normalization. 1271 fn idmatch b view uchar * to bool 1272 v = mem ws 0 kid 1273 if lt v 0 1274 ret false 1275 end 1276 u = ulong v 1277 e = digs u 1278 if or eq e u ne long e sknum u 1279 ret false 1280 end 1281 if ne len b sub e u 1282 ret false 1283 end 1284 for i ulong len b bound `9223372036854775807` 1285 if ne get b i get gj add u i 1286 ret false 1287 end 1288 end 1289 ret true 1290 end 1291 1292 fn epok b view uchar * kind int to bool 1293 if eq kind 1 1294 if eq len b 0 1295 ret false 1296 end 1297 for i ulong len b bound `9223372036854775807` 1298 c = get b i 1299 if not or isaln c eq c 45 1300 ret false 1301 end 1302 end 1303 ret true 1304 end 1305 if eq kind 2 1306 if not endsw b sactpub 1307 ret false 1308 end 1309 ret idmatch part b 0 sub len b len sactpub 1310 end 1311 if bat b 0 hdep 1312 ret idmatch part b len hdep sub len b len hdep 1313 end 1314 if bat b 0 hrec 1315 ret idmatch part b len hrec sub len b len hrec 1316 end 1317 ret false 1318 end 1319 1320 note pkurl: kind 1 bucket, 2 publish, 3 html; the string must match its mode and path 1321 fn pkurl v long kind int to int 1322 if lt v 0 1323 die murlbad 1324 end 1325 b = new uchar 65536 0 1326 m = sdec ulong v b false 1327 o = new uchar 128 0 1328 k = origin o 1329 if eq kind 1 1330 k = cpy o k pfiles 1331 end 1332 if eq kind 2 1333 k = cpy o k pdep 1334 end 1335 if eq kind 3 1336 k = cpy o k phtml 1337 end 1338 if lt m 0 1339 die2 murl part o 0 k 1340 end 1341 um = ulong m 1342 ok = urlok part b 0 um part o 0 k 1343 if ok 1344 ok = epok part b k sub um k kind 1345 end 1346 if not ok 1347 warn tpfx 1348 warn murl 1349 warn part o 0 k 1350 warn tcolon 1351 outc 2 part b 0 um 1352 warn tnl 1353 exit 1 1354 end 1355 say part b 0 um 1356 say tnl 1357 ret 0 1358 end 1359 1360 fn pkdoi v long ky view uchar * to int 1361 if lt v 0 1362 die2 mabsent ky 1363 end 1364 b = new uchar 4096 0 1365 m = sdec ulong v b false 1366 if lt m 0 1367 die2 mabsent ky 1368 end 1369 um = ulong m 1370 doichk part b 0 um 1371 say part b 0 um 1372 say tnl 1373 ret 0 1374 end 1375 1376 fn pkname t ulong to int 1377 v = mtyp t kkey 34 1378 if lt v 0 1379 die2 mabsent kkey 1380 end 1381 b = new uchar 4096 0 1382 m = sdec ulong v b false 1383 if lt m 0 1384 die2 mabsent kkey 1385 end 1386 um = ulong m 1387 if not nameok part b 0 um 0 1388 die2 mfname kkey 1389 end 1390 say part b 0 um 1391 say tnl 1392 ret 0 1393 end 1394 1395 fn pksize t ulong to int 1396 v = mem t ksize 1397 if lt v 0 1398 die2 mabsent ksize 1399 end 1400 u = ulong v 1401 d = digs u 1402 e = sknum u 1403 if or eq d u ne long d e 1404 die2 mabsent ksize 1405 end 1406 x = wgj 1 u d 1407 say tnl 1408 ret 0 1409 end 1410 1411 fn saystr v ulong to void 1412 b = new uchar 65536 0 1413 m = sdec v b true 1414 outc 1 part b 0 ulong m 1415 end 1416 1417 fn msgs a ulong to void 1418 k = ws add a 1 1419 n = get gn 0 1420 while lt k n bound `9223372036854775807` 1421 if ne get gj k 34 1422 ret 1423 end 1424 say tsp 1425 saystr k 1426 k = ws ulong skstr k 1427 if ne get gj k 44 1428 ret 1429 end 1430 k = ws add k 1 1431 end 1432 end 1433 1434 note errs: Zenodo's per-field errors, one line each: field: messages 1435 fn errs a ulong to void 1436 k = ws add a 1 1437 n = get gn 0 1438 while lt k n bound `9223372036854775807` 1439 if ne get gj k 123 1440 ret 1441 end 1442 say tind 1443 f = mtyp k kfield 34 1444 if ge f 0 1445 saystr ulong f 1446 say tcolon 1447 end 1448 ms = mtyp k kmsgs 91 1449 if ge ms 0 1450 msgs ulong ms 1451 end 1452 say tnl 1453 k = ws ulong skval k 1454 if ne get gj k 44 1455 ret 1456 end 1457 k = ws add k 1 1458 end 1459 end 1460 1461 fn pkmsg t ulong to int 1462 v = mtyp t kmsg 34 1463 if ge v 0 1464 saystr ulong v 1465 say tnl 1466 end 1467 a = mtyp t kerrs 91 1468 if ge a 0 1469 errs ulong a 1470 end 1471 ret 0 1472 end 1473 1474 fn dopick ac ulong to int 1475 if ne ac 5 1476 usg 1477 end 1478 setmode 2 1479 p = new uchar 4096 0 1480 k = getarg 3 p 1481 slurp part p 0 k 1482 if not docok 1483 die2 mnotok part p 0 k 1484 end 1485 w = new uchar 4096 0 1486 m = getarg 4 w 1487 t = ws 0 1488 if beq part w 0 m kbucket 1489 ret pkurl inner t klinks 123 kbucket 1 1490 end 1491 if beq part w 0 m kpublish 1492 ret pkurl inner t klinks 123 kpublish 2 1493 end 1494 if beq part w 0 m khtml 1495 ret pkurl inner t klinks 123 khtml 3 1496 end 1497 if beq part w 0 m kdoi 1498 a = mtyp t kmeta 123 1499 if lt a 0 1500 die2 mabsent kmeta 1501 end 1502 z = mtyp ulong a kpre 123 1503 if lt z 0 1504 die2 mabsent kpre 1505 end 1506 ret pkdoi mtyp ulong z kdoi 34 kdoi 1507 end 1508 if beq part w 0 m wpubdoi 1509 ret pkdoi mtyp t kdoi 34 kdoi 1510 end 1511 if beq part w 0 m kkey 1512 ret pkname t 1513 end 1514 if beq part w 0 m ksize 1515 ret pksize t 1516 end 1517 if beq part w 0 m kmsg 1518 ret pkmsg t 1519 end 1520 die2 mwhat part w 0 m 1521 ret 2 1522 end 1523 1524 note zwrite: .zenodo.json with "doi" as its first member: the bytes up to and with the opening 1525 note brace, the whitespace after it, the doi member, that whitespace again, then the rest unchanged 1526 fn zwrite fd int d view uchar * to bool 1527 t = ws 0 1528 w = ws add t 1 1529 a = wgj fd 0 w 1530 b = wall fd tdoiq 1531 c = wall fd d 1532 e = wall fd tqc 1533 f = wgj fd add t 1 w 1534 g = wgj fd w get gn 0 1535 ret and and and a b and c e and f g 1536 end 1537 1538 note zrepl: rename .zenodo.json.doi-new over .zenodo.json (one step: the old file or the new one) 1539 fn zrepl flags int to void 1540 pk = new uchar 64 0 1541 wr16 pk 0 2 1542 wr16 pk 2 len tznew 1543 k = cpy pk 4 tznew 1544 wr16 pk k len tzj 1545 k = cpy pk add k 2 tzj 1546 r = renames pk 0 k flags 1547 if lt r 0 1548 die mrename 1549 end 1550 end 1551 1552 note READY after exclusive reservation and snapshots; receive one DOI line; SAVED after fsync. 1553 fn homefd i ulong to void 1554 r = fchdir int get gfd i 1555 if lt r 0 1556 die manchor 1557 end 1558 end 1559 1560 fn outsnap p view uchar * inf int to void 1561 homefd 1 1562 fd = fopen p 0 len p 524481 384 1563 if lt fd 0 1564 die2 mwrite p 1565 end 1566 ok = copyfd inf int fd 1567 x = fclose int fd 1568 homefd 0 1569 if or not ok lt x 0 1570 die2 mwrite p 1571 end 1572 end 1573 1574 fn filecopy p view uchar * to void 1575 if not nameok p bname p 1576 die2 mfname p 1577 end 1578 fd = openreg p 1579 if lt fd 0 1580 die2 mnotreg p 1581 end 1582 o = new uchar 4096 0 1583 n = cpy o 0 tfiles 1584 n = cpy o n part p bname p sub len p bname p 1585 outsnap part o 0 n int fd 1586 x = fclose int fd 1587 end 1588 1589 fn scanpdf to void 1590 buf = new uchar 65536 0 1591 fd = fopen tdot 0 len tdot 540672 0 1592 if lt fd 0 1593 die mnofile 1594 end 1595 count = ulong 0 1596 while true bound `9223372036854775807` 1597 r = getdnts int fd buf 0 len buf 1598 if lt r 0 1599 die mnofile 1600 end 1601 if eq r 0 1602 break 1603 end 1604 p = ulong 0 1605 while lt p ulong r bound `9223372036854775807` 1606 n = rd16 buf add p 16 1607 if or lt n 20 gt add p n ulong r 1608 die mnofile 1609 end 1610 j = add p 19 1611 e = j 1612 while lt e add p n bound `9223372036854775807` 1613 if eq get buf e 0 1614 break 1615 end 1616 e = add e 1 1617 end 1618 if ge sub e j 4 1619 if bati part buf j sub e j sub sub e j 4 tpdf 1620 filecopy part buf j sub e j 1621 count = add count 1 1622 end 1623 end 1624 p = add p n 1625 end 1626 end 1627 x = fclose int fd 1628 if eq count 0 1629 die mnopdf 1630 end 1631 end 1632 1633 fn snaplic to void 1634 buf = new uchar 128 0 1635 s = ulong 0 1636 found = false 1637 for i ulong len tlics bound `9223372036854775807` 1638 if eq get tlics i 10 1639 n = sub i s 1640 r = lstat tlics s n buf 0 len buf 1641 if ge r 0 1642 fd = openreg part tlics s n 1643 if lt fd 0 1644 die2 mnotreg part tlics s n 1645 end 1646 x = fclose int fd 1647 found = true 1648 end 1649 s = add i 1 1650 end 1651 end 1652 if not found 1653 die mnolic 1654 end 1655 end 1656 1657 fn snapmeta to void 1658 slurp tzj 1659 meta 1660 if ne get gbad 0 0 1661 exit 1 1662 end 1663 homefd 1 1664 fd = fopen tmcopy 0 len tmcopy 524481 384 1665 if lt fd 0 1666 die mwrite 1667 end 1668 ok = wgj int fd 0 get gn 0 1669 x = fclose int fd 1670 if or not ok lt x 0 1671 die mwrite 1672 end 1673 fd = fopen tbody 0 len tbody 524481 384 1674 if lt fd 0 1675 die mwrite 1676 end 1677 ok = wall int fd thead 1678 ok = and ok wgj int fd add ws 0 1 sub ulong skobj ws 0 1 1679 ok = and ok wall int fd ttail 1680 x = fclose int fd 1681 if or not ok lt x 0 1682 die mwrite 1683 end 1684 homefd 0 1685 end 1686 1687 note Compare to the exact submitted bytes, including after exchange to close the pre-rename gap. 1688 fn samejson fd int to bool 1689 buf = new uchar 65536 0 1690 p = ulong 0 1691 while true bound `9223372036854775807` 1692 r = pread fd buf 0 len buf long p 1693 if eq r -4 1694 continue 1695 end 1696 if lt r 0 1697 ret false 1698 end 1699 if eq r 0 1700 ret eq p get gn 0 1701 end 1702 if gt add p ulong r get gn 0 1703 ret false 1704 end 1705 for i ulong ulong r bound `9223372036854775807` 1706 if ne get buf i get gj add p i 1707 ret false 1708 end 1709 end 1710 p = add p ulong r 1711 end 1712 ret false 1713 end 1714 1715 fn namedok p view uchar * to bool 1716 fd = openreg p 1717 if lt fd 0 1718 ret false 1719 end 1720 ok = samejson int fd 1721 x = fclose int fd 1722 ret and ok ge x 0 1723 end 1724 1725 fn recvdoi d edit uchar * to ulong 1726 n = ulong 0 1727 while lt n len d bound `9223372036854775807` 1728 if ne getppid get gparent 0 1729 die mipc 1730 end 1731 r = read 0 d n 1 1732 if eq r -4 1733 continue 1734 end 1735 if or eq r -11 eq r 0 1736 note EOF before the shell opens its writer is not failure; parent monitors consumer lifetime. 1737 x = waitio 0 1 `10000000` 1738 if lt x 0 1739 if ne x -4 1740 die mipc 1741 end 1742 end 1743 continue 1744 end 1745 if lt r 0 1746 die mread 1747 end 1748 if eq get d n 10 1749 ret n 1750 end 1751 n = add n 1 1752 end 1753 die mbaddoi 1754 ret 0 1755 end 1756 1757 note candino: the file named p is the retained candidate inode (st_dev at 0, st_ino at 8) 1758 fn candino p view uchar * to bool 1759 fd = openreg p 1760 if lt fd 0 1761 ret false 1762 end 1763 a = new uchar 128 0 1764 b = new uchar 128 0 1765 x = fstat int fd a 0 128 1766 y = fstat int get gfd 2 b 0 128 1767 z = fclose int fd 1768 if or lt x 0 lt y 0 1769 ret false 1770 end 1771 ret and eq rd64 a 0 rd64 b 0 eq rd64 a 8 rd64 b 8 1772 end 1773 1774 fn undo m view uchar * to void 1775 zrepl 2 1776 r = fsync int get gfd 0 1777 if lt r 0 1778 die mwrite 1779 end 1780 die m 1781 end 1782 1783 note The candidate stays 0600 until the exchanged inode is proven to be the retained candidate. 1784 fn putdoi d view uchar * to void 1785 homefd 0 1786 if not namedok tzj 1787 die mdrift 1788 end 1789 fd = int get gfd 2 1790 ok = zwrite fd d 1791 r = fsync fd 1792 if or not ok lt r 0 1793 die mwrite 1794 end 1795 note RENAME_EXCHANGE keeps the displaced edit even when an editor wins after namedok. 1796 zrepl 2 1797 inst = candino tzj 1798 if not inst 1799 undo mcand 1800 end 1801 disp = namedok tznew 1802 if not disp 1803 undo mdrift 1804 end 1805 r = fchmod fd int get gmode 0 1806 if lt r 0 1807 die mwrite 1808 end 1809 r = fsync fd 1810 if lt r 0 1811 die mwrite 1812 end 1813 r = fsync int get gfd 0 1814 if lt r 0 1815 die mwrite 1816 end 1817 r = unlink tznew 0 len tznew 1818 if lt r 0 1819 die mwrite 1820 end 1821 r = fsync int get gfd 0 1822 if lt r 0 1823 die mwrite 1824 end 1825 say tsaved 1826 end 1827 1828 note The shell pipeline exists before launch; this private response pipe exists before fork. 1829 note Only Omega supervises keeper lifetime. DONE certifies successful wait, not merely SAVED. 1830 fn stopkid pid int to void 1831 st = new uchar 4 0 1832 r = killpid pid 9 1833 while true bound `9223372036854775807` 1834 r = waitpid pid st 0 4 0 1835 if ne r -4 1836 break 1837 end 1838 end 1839 end 1840 1841 fn watchkid pid int fd int to int 1842 st = new uchar 4 0 1843 buf = new uchar 4096 0 1844 done = false 1845 while true bound `9223372036854775807` 1846 if not done 1847 r = waitpid pid st 0 4 1 1848 if eq r long pid 1849 done = true 1850 if ne rd32 st 0 0 1851 warn tpfx 1852 warn mkdead 1853 warn tnl 1854 ret 1 1855 end 1856 else 1857 if and lt r 0 ne r -4 1858 stopkid pid 1859 die mipc 1860 end 1861 end 1862 end 1863 r = read fd buf 0 len buf 1864 if gt r 0 1865 if not wall 1 part buf 0 ulong r 1866 if not done 1867 stopkid pid 1868 end 1869 ret 1 1870 end 1871 else 1872 if and eq r 0 done 1873 if not wall 1 tdone 1874 ret 1 1875 end 1876 ret 0 1877 end 1878 if and lt r 0 and ne r -11 ne r -4 1879 if not done 1880 stopkid pid 1881 end 1882 die mipc 1883 end 1884 end 1885 note POLLERR on the pipeline writer means the consumer closed its read end. 1886 r = waitio 1 0 0 1887 if or lt r 0 ne and r 8 0 1888 if not done 1889 stopkid pid 1890 end 1891 ret 1 1892 end 1893 r = waitio fd 1 `10000000` 1894 end 1895 ret 1 1896 end 1897 1898 fn kidio fd int to void 1899 p = new uchar 4096 0 1900 n = getarg 4 p 1901 n = cpy p n tin 1902 note startup gate before-input (tests insert SIGSTOP here in compiled fixture variants) 1903 inf = fopen p 0 n or int 524288 2048 0 1904 if lt inf 0 1905 die mipc 1906 end 1907 r = dupfd int inf 0 0 1908 x = fclose int inf 1909 if lt r 0 1910 die mipc 1911 end 1912 note startup gate before-output (input is open, output is not bound) 1913 r = dupfd fd 1 0 1914 x = fclose fd 1915 if lt r 0 1916 die mipc 1917 end 1918 end 1919 1920 fn supervise ac ulong to int 1921 if or lt ac 5 gt ac 6 1922 usg 1923 end 1924 fds = new uchar 8 0 1925 r = pipefds fds 0 8 or int 524288 2048 1926 if lt r 0 1927 die mipc 1928 end 1929 put gparent 0 getpid 1930 pid = fork 1931 if lt pid 0 1932 die mipc 1933 end 1934 if eq pid 0 1935 x = fclose int rd32 fds 0 1936 kidio int rd32 fds 4 1937 ret doserve ac 1938 end 1939 x = fclose int rd32 fds 4 1940 ret watchkid int pid int rd32 fds 0 1941 end 1942 1943 fn doserve ac ulong to int 1944 if or lt ac 5 gt ac 6 1945 usg 1946 end 1947 setmode 2 1948 p = new uchar 4096 0 1949 n = getarg 3 p 1950 fd = walkopen part p 0 n or int 540672 32768 0 1951 if lt fd 0 1952 die2 manchor part p 0 n 1953 end 1954 put gfd 0 fd 1955 r = flock int fd 6 1956 if lt r 0 1957 die mlock 1958 end 1959 n = getarg 4 p 1960 fd = walkopen part p 0 n or int 540672 32768 0 1961 if lt fd 0 1962 die manchor 1963 end 1964 put gfd 1 fd 1965 fd = fopen tdot 0 len tdot 540672 0 1966 if lt fd 0 1967 die manchor 1968 end 1969 put gfd 3 fd 1970 homefd 0 1971 fd = fopen tznew 0 len tznew or int 524482 32768 384 1972 if lt fd 0 1973 die2 mwrite tznew 1974 end 1975 put gfd 2 fd 1976 snapmeta 1977 snaplic 1978 scanpdf 1979 if eq ac 6 1980 homefd 3 1981 n = getarg 5 p 1982 filecopy part p 0 n 1983 end 1984 say tready 1985 n = recvdoi p 1986 doichk part p 0 n 1987 putdoi part p 0 n 1988 ret 0 1989 end 1990 1991 note yesline: one line from fd is exactly yes (end of input before the newline is a no) 1992 fn yesline fd int to bool 1993 b = new uchar 1 0 1994 a = new uchar 3 0 1995 n = ulong 0 1996 ok = true 1997 while true bound `9223372036854775807` 1998 r = read fd b 0 1 1999 if eq r -4 2000 continue 2001 end 2002 if le r 0 2003 ok = false 2004 break 2005 end 2006 if eq get b 0 10 2007 break 2008 end 2009 if lt n 3 2010 put a n get b 0 2011 else 2012 ok = false 2013 end 2014 n = add n 1 2015 end 2016 ret and ok and eq n 3 beq a tyes 2017 end 2018 2019 note question: every part of the prompt must reach the tty before an answer is read 2020 fn question fd int d view uchar * to bool 2021 o = new uchar 128 0 2022 k = origin o 2023 if not wall fd qpub1 2024 ret false 2025 end 2026 if not wall fd d 2027 ret false 2028 end 2029 if not wall fd qpub2 2030 ret false 2031 end 2032 if not wall fd part o 0 k 2033 ret false 2034 end 2035 if eq get gprod 0 1 2036 if not wall fd qprod 2037 ret false 2038 end 2039 else 2040 if not wall fd qsand 2041 ret false 2042 end 2043 end 2044 ret wall fd qpub3 2045 end 2046 2047 note docons: the question and the answer both go through /dev/tty, never stdin or stdout, so no 2048 note pipe or file can answer it; no terminal means no 2049 fn docons ac ulong to int 2050 if ne ac 4 2051 usg 2052 end 2053 setmode 2 2054 d = new uchar 4096 0 2055 dn = getarg 3 d 2056 doichk part d 0 dn 2057 fd = fopen ttty 0 len ttty 524290 0 2058 if lt fd 0 2059 warn tpfx 2060 warn mnotty 2061 warn tnl 2062 ret 1 2063 end 2064 ok = question int fd part d 0 dn 2065 if ok 2066 ok = yesline int fd 2067 end 2068 y = fclose int fd 2069 if ok 2070 ret 0 2071 end 2072 ret 1 2073 end 2074 2075 fn main to int 2076 ac = ulong argcnt 2077 if lt ac 2 2078 usg 2079 end 2080 c = new uchar 4096 0 2081 k = getarg 1 c 2082 if beq part c 0 k wtoken 2083 if ne ac 2 2084 usg 2085 end 2086 ret dotoken 2087 end 2088 if beq part c 0 k wbase 2089 ret dobase ac 2090 end 2091 if beq part c 0 k wsnap 2092 ret dosnap ac 2093 end 2094 if beq part c 0 k wcheck 2095 ret docheck ac 2096 end 2097 if beq part c 0 k wbody 2098 ret dobody ac 2099 end 2100 if beq part c 0 k wpick 2101 ret dopick ac 2102 end 2103 if beq part c 0 k wserve 2104 ret supervise ac 2105 end 2106 if beq part c 0 k wconsent 2107 ret docons ac 2108 end 2109 usg 2110 ret 2 2111 end