interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / doi / doi.om

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