interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / build / common.om

1174 lines · 20038 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/build/common.om: shared pure seeded OM-2 runtime for build, release, and automation coordinators.
4note Dynamic run-time pools (global ... *) grow on demand with enlarge: zero artificial file/buffer caps.
5note Streaming FIPS 180-4 SHA-256 (64 KiB chunks) hashes files of arbitrary size.
6
7text cb_hexd "0123456789abcdef"
8text cb_nl "\n"
9text cb_sp " "
10text cb_slash "/"
11text cb_dot "."
12text cb_dotdot ".."
13text cb_devnull "/dev/null"
14text cb_sha_kiv "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298 1779033703 3144134277 1013904242 2773480762 1359893119 2600822924 528734635 1541459225"
15
16note Dynamic byte pools (grow via enlarge, no fixed cap)
17global cb_rb uchar *
18global cb_wb uchar *
19global cb_sb uchar *
20global cb_n ulong 32
21note cb_n slots:
22note 0 = cb_rb len, 1 = cb_wb len, 2 = cb_sb len
23note 3 = cb_pa len, 4 = cb_pb len, 5 = cb_av bytes, 6 = cb_av argc
24note 7 = cb_ev bytes, 8 = cb_ev envc, 9 = cb_sha initialized
25note 10 = cb_sha total_bytes, 11 = cb_sha buf_len
26note 12 = cb_tmp_len
27
28note Path & small scratch buffers
29global cb_pa uchar 4096
30global cb_pb uchar 4096
31global cb_pc uchar 4096
32global cb_av uchar 65536
33global cb_ev uchar 4096
34global cb_pkt uchar 4096
35global cb_st uchar 4
36global cb_numbuf uchar 32
37global cb_chunk1 uchar 65536
38global cb_chunk2 uchar 65536
39global cb_dent uchar 32768
40global cb_envtmp uchar 4096
41
42note Streaming SHA-256 state
43global cb_sk uint 72
44global cb_sw uint 64
45global cb_sv uint 8
46global cb_sh uint 8
47global cb_sbuf uchar 128
48global cb_sdig uchar 32
49global cb_shex uchar 64
50
51note ---- dynamic pool growth ----
52fn cb_rb_need need ulong to bool
53if le need len cb_rb
54ret true
55end
56cap = len cb_rb
57if eq cap 0
58cap = 65536
59end
60while lt cap need bound 64
61cap = mul cap 2
62end
63z = enlarge cb_rb cap
64if ne z 0
65ret false
66end
67ret true
68end
69
70fn cb_wb_need need ulong to bool
71if le need len cb_wb
72ret true
73end
74cap = len cb_wb
75if eq cap 0
76cap = 65536
77end
78while lt cap need bound 64
79cap = mul cap 2
80end
81z = enlarge cb_wb cap
82if ne z 0
83ret false
84end
85ret true
86end
87
88fn cb_sb_need need ulong to bool
89if le need len cb_sb
90ret true
91end
92cap = len cb_sb
93if eq cap 0
94cap = 65536
95end
96while lt cap need bound 64
97cap = mul cap 2
98end
99z = enlarge cb_sb cap
100if ne z 0
101ret false
102end
103ret true
104end
105
106fn cb_wb_clr to void
107put cb_n 1 0
108end
109
110fn cb_wb_ch c uchar to void
111n = get cb_n 1
112if not cb_wb_need add n 1
113exit 70
114end
115put cb_wb n c
116put cb_n 1 add n 1
117end
118
119fn cb_wb_tx t view uchar * to void
120m = len t
121n = get cb_n 1
122if not cb_wb_need add n m
123exit 70
124end
125for i ulong m bound 9223372036854775807
126put cb_wb add n i get t i
127end
128put cb_n 1 add n m
129end
130
131fn cb_wb_rb off ulong cnt ulong to void
132n = get cb_n 1
133if not cb_wb_need add n cnt
134exit 70
135end
136for i ulong cnt bound 9223372036854775807
137put cb_wb add n i get cb_rb add off i
138end
139put cb_n 1 add n cnt
140end
141
142fn cb_wb_num v ulong to void
143x = v
144i = ulong 32
145for j ulong 21
146i = sub i 1
147put cb_numbuf i uchar add 48 rem x 10
148x = div x 10
149if eq x 0
150break
151end
152end
153for q ulong sub 32 i bound 32
154cb_wb_ch get cb_numbuf add i q
155end
156end
157
158note ---- fd write helpers ----
159fn cb_wtx fd int t view uchar * to void
160n = len t
161o = ulong 0
162while lt o n bound 9223372036854775807
163k = write fd t o sub n o
164if le k 0
165ret
166end
167o = add o ulong k
168end
169end
170
171fn cb_out t view uchar * to void
172cb_wtx 1 t
173end
174
175fn cb_err t view uchar * to void
176cb_wtx 2 t
177end
178
179fn cb_wnum fd int v ulong to void
180x = v
181i = ulong 32
182for j ulong 21
183i = sub i 1
184put cb_numbuf i uchar add 48 rem x 10
185x = div x 10
186if eq x 0
187break
188end
189end
190r = write fd cb_numbuf i sub 32 i
191end
192
193fn cb_wbuf_pa fd int to void
194n = get cb_n 3
195if gt n 0
196r = write fd cb_pa 0 n
197end
198end
199
200fn cb_wbuf_pb fd int to void
201n = get cb_n 4
202if gt n 0
203r = write fd cb_pb 0 n
204end
205end
206
207fn cb_wbuf_hex fd int to void
208r = write fd cb_shex 0 64
209end
210
211note ---- path buffer cb_pa[0..cb_n[3]) and cb_pb[0..cb_n[4]) ----
212fn cb_pa_clr to void
213put cb_n 3 0
214end
215
216fn cb_pa_ch c uchar to void
217n = get cb_n 3
218if ge n 4095
219exit 70
220end
221put cb_pa n c
222put cb_n 3 add n 1
223end
224
225fn cb_pa_tx t view uchar * to void
226m = len t
227n = get cb_n 3
228if gt add n m 4095
229exit 70
230end
231for i ulong m bound 4096
232put cb_pa add n i get t i
233end
234put cb_n 3 add n m
235end
236
237fn cb_pa_set t view uchar * to void
238cb_pa_clr
239cb_pa_tx t
240end
241
242fn cb_pa_pb to void
243n = get cb_n 4
244for i ulong n bound 4096
245put cb_pa i get cb_pb i
246end
247put cb_n 3 n
248end
249
250fn cb_pa_add_pb to void
251m = get cb_n 4
252n = get cb_n 3
253if gt add n m 4095
254exit 70
255end
256for i ulong m bound 4096
257put cb_pa add n i get cb_pb i
258end
259put cb_n 3 add n m
260end
261
262fn cb_pb_clr to void
263put cb_n 4 0
264end
265
266fn cb_pb_ch c uchar to void
267n = get cb_n 4
268if ge n 4095
269exit 70
270end
271put cb_pb n c
272put cb_n 4 add n 1
273end
274
275fn cb_pb_tx t view uchar * to void
276m = len t
277n = get cb_n 4
278if gt add n m 4095
279exit 70
280end
281for i ulong m bound 4096
282put cb_pb add n i get t i
283end
284put cb_n 4 add n m
285end
286
287fn cb_pb_set t view uchar * to void
288cb_pb_clr
289cb_pb_tx t
290end
291
292fn cb_pb_from_pa to void
293n = get cb_n 3
294for i ulong n bound 4096
295put cb_pb i get cb_pa i
296end
297put cb_n 4 n
298end
299
300fn cb_pa_arg idx ulong to bool
301k = argread idx cb_pa 0 4095
302if lt k 0
303ret false
304end
305put cb_n 3 ulong k
306ret true
307end
308
309fn cb_pb_arg idx ulong to bool
310k = argread idx cb_pb 0 4095
311if lt k 0
312ret false
313end
314put cb_n 4 ulong k
315ret true
316end
317
318fn cb_pa_eq t view uchar * to bool
319n = get cb_n 3
320if ne n len t
321ret false
322end
323for i ulong n bound 4096
324if ne get cb_pa i get t i
325ret false
326end
327end
328ret true
329end
330
331fn cb_pa_ends t view uchar * to bool
332n = get cb_n 3
333m = len t
334if lt n m
335ret false
336end
337off = sub n m
338for i ulong m bound 4096
339if ne get cb_pa add off i get t i
340ret false
341end
342end
343ret true
344end
345
346note ---- environment variable lookup into cb_pb (returns true if set and non-empty) ----
347fn cb_env_match name view uchar * to bool
348ml = len name
349for j ulong ml bound 4096
350if ne get cb_envtmp j get name j
351ret false
352end
353end
354ret true
355end
356
357fn cb_env_copy_pb start ulong vlen ulong to void
358for j ulong vlen bound 4096
359put cb_pb j get cb_envtmp add start j
360end
361put cb_n 4 vlen
362end
363
364fn cb_getenv name view uchar * to bool
365ec = envcnt
366ml = len name
367for idx ulong ulong ec bound 65536
368k = envread idx cb_envtmp 0 4095
369if lt k 0
370continue
371end
372kl = ulong k
373if lt kl add ml 1
374continue
375end
376if ne get cb_envtmp ml 61
377continue
378end
379if cb_env_match name
380cb_env_copy_pb add ml 1 sub kl add ml 1
381ret true
382end
383end
384put cb_n 4 0
385ret false
386end
387
388note ---- streaming FIPS 180-4 SHA-256 ----
389fn cb_sha_init_k to void
390if ne get cb_n 9 0
391ret
392end
393put cb_n 9 1
394p = ulong 0
395for i ulong 72
396v = ulong 0
397while lt get cb_sha_kiv p 48 bound 4
398p = add p 1
399end
400while ge get cb_sha_kiv p 48 bound 11
401v = add mul v 10 ulong sub get cb_sha_kiv p 48
402p = add p 1
403if ge p len cb_sha_kiv
404put cb_sk i uint v
405ret
406end
407end
408put cb_sk i uint v
409end
410end
411
412fn cb_sha_reset to void
413cb_sha_init_k
414for j ulong 8
415put cb_sh j get cb_sk add j 64
416end
417put cb_n 10 0
418put cb_n 11 0
419end
420
421fn cb_sha_s0 a uint to uint
422ret xor xor rotr a 2 rotr a 13 rotr a 22
423end
424
425fn cb_sha_s1 e uint to uint
426ret xor xor rotr e 6 rotr e 11 rotr e 25
427end
428
429fn cb_sha_g0 x uint to uint
430ret xor xor rotr x 7 rotr x 18 shr x 3
431end
432
433fn cb_sha_g1 x uint to uint
434ret xor xor rotr x 17 rotr x 19 shr x 10
435end
436
437fn cb_sha_maj a uint b uint c uint to uint
438ret xor xor and a b and a c and b c
439end
440
441fn cb_sha_rnd i ulong to void
442e = get cb_sv 4
443t1 = add get cb_sv 7 cb_sha_s1 e
444t1 = add t1 xor and e get cb_sv 5 and not e get cb_sv 6
445t1 = add t1 add get cb_sk i get cb_sw i
446a = get cb_sv 0
447t2 = add cb_sha_s0 a cb_sha_maj a get cb_sv 1 get cb_sv 2
448put cb_sv 7 get cb_sv 6
449put cb_sv 6 get cb_sv 5
450put cb_sv 5 e
451put cb_sv 4 add get cb_sv 3 t1
452put cb_sv 3 get cb_sv 2
453put cb_sv 2 get cb_sv 1
454put cb_sv 1 a
455put cb_sv 0 add t1 t2
456end
457
458fn cb_sha_blk_sbuf off ulong to void
459for t ulong 16
460p = add off mul t 4
461v = uint get cb_sbuf p
462v = or shl v 8 uint get cb_sbuf add p 1
463v = or shl v 8 uint get cb_sbuf add p 2
464v = or shl v 8 uint get cb_sbuf add p 3
465put cb_sw t v
466end
467for t ulong 48
468u = add t 16
469x = add cb_sha_g1 get cb_sw sub u 2 get cb_sw sub u 7
470put cb_sw u add x add cb_sha_g0 get cb_sw sub u 15 get cb_sw sub u 16
471end
472for j ulong 8
473put cb_sv j get cb_sh j
474end
475for i ulong 64
476cb_sha_rnd i
477end
478for j ulong 8
479put cb_sh j add get cb_sh j get cb_sv j
480end
481end
482
483fn cb_sha_copy_chunk bl ulong src_off ulong take ulong to void
484for j ulong take bound 64
485put cb_sbuf add bl j get cb_chunk1 add src_off j
486end
487end
488
489fn cb_sha_copy_rb bl ulong src_off ulong take ulong to void
490for j ulong take bound 64
491put cb_sbuf add bl j get cb_rb add src_off j
492end
493end
494
495fn cb_sha_push_chunk off ulong cnt ulong to void
496i = ulong 0
497while lt i cnt bound 9223372036854775807
498bl = get cb_n 11
499take = sub 64 bl
500if lt sub cnt i take
501take = sub cnt i
502end
503cb_sha_copy_chunk bl add off i take
504bl = add bl take
505i = add i take
506put cb_n 10 add get cb_n 10 take
507if eq bl 64
508cb_sha_blk_sbuf 0
509put cb_n 11 0
510else
511put cb_n 11 bl
512end
513end
514end
515
516fn cb_sha_push_rb off ulong cnt ulong to void
517i = ulong 0
518while lt i cnt bound 9223372036854775807
519bl = get cb_n 11
520take = sub 64 bl
521if lt sub cnt i take
522take = sub cnt i
523end
524cb_sha_copy_rb bl add off i take
525bl = add bl take
526i = add i take
527put cb_n 10 add get cb_n 10 take
528if eq bl 64
529cb_sha_blk_sbuf 0
530put cb_n 11 0
531else
532put cb_n 11 bl
533end
534end
535end
536
537fn cb_sha_finish to void
538r = get cb_n 11
539tot = get cb_n 10
540for j ulong sub 128 r bound 128
541put cb_sbuf add r j 0
542end
543put cb_sbuf r 128
544e = ulong 64
545if ge r 56
546e = 128
547end
548bits = mul tot 8
549for j ulong 8
550put cb_sbuf sub sub e 1 j uchar and shr bits mul j 8 255
551end
552cb_sha_blk_sbuf 0
553if eq e 128
554cb_sha_blk_sbuf 64
555end
556for j ulong 8
557v = get cb_sh j
558for q ulong 4
559s = uint mul sub 3 q 8
560put cb_sdig add mul j 4 q uchar and shr v s 255
561end
562end
563for j ulong 32
564b = get cb_sdig j
565put cb_shex mul j 2 get cb_hexd ulong shr b 4
566put cb_shex add mul j 2 1 get cb_hexd ulong and b 15
567end
568end
569
570fn cb_sha_file_pa to bool
571fd = fopen cb_pa 0 get cb_n 3 524288 0
572if lt fd 0
573ret false
574end
575cb_sha_reset
576ok = true
577while true bound 9223372036854775807
578k = read int fd cb_chunk1 0 65536
579if lt k 0
580ok = false
581break
582end
583if eq k 0
584break
585end
586cb_sha_push_chunk 0 ulong k
587end
588z = fclose int fd
589if not ok
590ret false
591end
592cb_sha_finish
593ret true
594end
595
596fn cb_sha_rb off ulong cnt ulong to void
597cb_sha_reset
598cb_sha_push_rb off cnt
599cb_sha_finish
600end
601
602fn cb_is_hex c uchar to bool
603if and ge c 48 le c 57
604ret true
605end
606ret and ge c 97 le c 102
607end
608
609fn cb_hex_val c uchar to ulong
610if and ge c 48 le c 57
611ret ulong sub c 48
612end
613if and ge c 97 le c 102
614ret add 10 ulong sub c 97
615end
616if and ge c 65 le c 70
617ret add 10 ulong sub c 65
618end
619ret 255
620end
621
622note ---- file I/O (zero caps via dynamic pools or 64 KiB streaming) ----
623fn cb_read_pa to bool
624fd = fopen cb_pa 0 get cb_n 3 524288 0
625if lt fd 0
626put cb_n 0 0
627ret false
628end
629n = ulong 0
630ok = true
631while true bound 9223372036854775807
632if not cb_rb_need add n 65536
633ok = false
634break
635end
636k = read int fd cb_rb n 65536
637if lt k 0
638ok = false
639break
640end
641if eq k 0
642break
643end
644n = add n ulong k
645end
646z = fclose int fd
647put cb_n 0 n
648ret ok
649end
650
651fn cb_append_pa_to_wb to bool
652fd = fopen cb_pa 0 get cb_n 3 524288 0
653if lt fd 0
654ret false
655end
656n = get cb_n 1
657ok = true
658while true bound 9223372036854775807
659if not cb_wb_need add n 65536
660ok = false
661break
662end
663k = read int fd cb_wb n 65536
664if lt k 0
665ok = false
666break
667end
668if eq k 0
669break
670end
671n = add n ulong k
672end
673z = fclose int fd
674put cb_n 1 n
675ret ok
676end
677
678fn cb_write_wb_pa mode int to bool
679r = unlink cb_pa 0 get cb_n 3
680fd = fopen cb_pa 0 get cb_n 3 524865 mode
681if lt fd 0
682ret false
683end
684n = get cb_n 1
685o = ulong 0
686ok = true
687while lt o n bound 9223372036854775807
688k = write int fd cb_wb o sub n o
689if le k 0
690ok = false
691break
692end
693o = add o ulong k
694end
695z = fclose int fd
696ret ok
697end
698
699fn cb_write_rb_pa mode int to bool
700r = unlink cb_pa 0 get cb_n 3
701fd = fopen cb_pa 0 get cb_n 3 524865 mode
702if lt fd 0
703ret false
704end
705n = get cb_n 0
706o = ulong 0
707ok = true
708while lt o n bound 9223372036854775807
709k = write int fd cb_rb o sub n o
710if le k 0
711ok = false
712break
713end
714o = add o ulong k
715end
716z = fclose int fd
717ret ok
718end
719
720fn cb_copy_pa_to_pb mode int to bool
721fdi = fopen cb_pa 0 get cb_n 3 524288 0
722if lt fdi 0
723ret false
724end
725z0 = unlink cb_pb 0 get cb_n 4
726fdo = fopen cb_pb 0 get cb_n 4 524865 mode
727if lt fdo 0
728z1 = fclose int fdi
729ret false
730end
731ok = true
732while true bound 9223372036854775807
733k = read int fdi cb_chunk1 0 65536
734if lt k 0
735ok = false
736break
737end
738if eq k 0
739break
740end
741o = ulong 0
742kl = ulong k
743while lt o kl bound 65537
744w = write int fdo cb_chunk1 o sub kl o
745if le w 0
746ok = false
747break
748end
749o = add o ulong w
750end
751if not ok
752break
753end
754end
755z2 = fclose int fdi
756z3 = fclose int fdo
757ret ok
758end
759
760fn cb_chunks_eq n ulong to bool
761for j ulong n bound 65536
762if ne get cb_chunk1 j get cb_chunk2 j
763ret false
764end
765end
766ret true
767end
768
769fn cb_read_exact_c1 fd int have ulong want ulong to bool
770u = have
771while lt u want bound 65537
772r = read fd cb_chunk1 u sub want u
773if le r 0
774ret false
775end
776u = add u ulong r
777end
778ret true
779end
780
781fn cb_read_exact_c2 fd int have ulong want ulong to bool
782u = have
783while lt u want bound 65537
784r = read fd cb_chunk2 u sub want u
785if le r 0
786ret false
787end
788u = add u ulong r
789end
790ret true
791end
792
793fn cb_cmp_pa_pb to bool
794fd1 = fopen cb_pa 0 get cb_n 3 524288 0
795if lt fd1 0
796ret false
797end
798fd2 = fopen cb_pb 0 get cb_n 4 524288 0
799if lt fd2 0
800z0 = fclose int fd1
801ret false
802end
803same = true
804while true bound 9223372036854775807
805k1 = read int fd1 cb_chunk1 0 65536
806k2 = read int fd2 cb_chunk2 0 65536
807if or lt k1 0 lt k2 0
808same = false
809break
810end
811if and eq k1 0 eq k2 0
812break
813end
814if or eq k1 0 eq k2 0
815same = false
816break
817end
818u1 = ulong k1
819u2 = ulong k2
820if lt u1 u2
821if not cb_read_exact_c1 int fd1 u1 u2
822same = false
823break
824end
825u1 = u2
826end
827if lt u2 u1
828if not cb_read_exact_c2 int fd2 u2 u1
829same = false
830break
831end
832end
833if not cb_chunks_eq u1
834same = false
835break
836end
837end
838z1 = fclose int fd1
839z2 = fclose int fd2
840ret same
841end
842
843fn cb_file_size_pa to long
844fd = fopen cb_pa 0 get cb_n 3 524288 0
845if lt fd 0
846ret -1
847end
848tot = long 0
849while true bound 9223372036854775807
850k = read int fd cb_chunk1 0 65536
851if lt k 0
852z0 = fclose int fd
853ret -1
854end
855if eq k 0
856break
857end
858tot = add tot k
859end
860z1 = fclose int fd
861ret tot
862end
863
864fn cb_mkdir_p_pa to bool
865n = get cb_n 3
866if eq n 0
867ret false
868end
869for i ulong n bound 4096
870if and gt i 0 eq get cb_pa i 47
871r = makedir cb_pa 0 i 493
872if and lt r 0 ne r -17
873ret false
874end
875end
876end
877r = makedir cb_pa 0 n 493
878if and lt r 0 ne r -17
879ret false
880end
881ret true
882end
883
884note atomic rename cb_pa -> cb_pb
885fn cb_rename_pa_pb to bool
886n1 = get cb_n 3
887n2 = get cb_n 4
888if or eq n1 0 eq n2 0
889ret false
890end
891if gt add add n1 n2 6 4096
892ret false
893end
894put cb_pkt 0 2
895put cb_pkt 1 0
896put cb_pkt 2 uchar and n1 255
897put cb_pkt 3 uchar shr n1 8
898for i ulong n1 bound 4096
899put cb_pkt add 4 i get cb_pa i
900end
901p = add 4 n1
902put cb_pkt p uchar and n2 255
903put cb_pkt add p 1 uchar shr n2 8
904for i ulong n2 bound 4096
905put cb_pkt add add p 2 i get cb_pb i
906end
907tot = add add p 2 n2
908r = renames cb_pkt 0 tot 0
909ret eq r 0
910end
911
912note ---- subprocess spawner (packed argv + optional envp) ----
913fn cb_av_clr to void
914put cb_n 5 2
915put cb_n 6 0
916put cb_av 0 0
917put cb_av 1 0
918end
919
920fn cb_av_push_pa to void
921p = get cb_n 5
922n = get cb_n 3
923if gt add add p 2 n 65536
924exit 70
925end
926put cb_av p uchar and n 255
927put cb_av add p 1 uchar shr n 8
928for i ulong n bound 4096
929put cb_av add add p 2 i get cb_pa i
930end
931put cb_n 5 add add p 2 n
932c = add get cb_n 6 1
933put cb_n 6 c
934put cb_av 0 uchar and c 255
935put cb_av 1 uchar shr c 8
936end
937
938fn cb_av_push_pb to void
939p = get cb_n 5
940n = get cb_n 4
941if gt add add p 2 n 65536
942exit 70
943end
944put cb_av p uchar and n 255
945put cb_av add p 1 uchar shr n 8
946for i ulong n bound 4096
947put cb_av add add p 2 i get cb_pb i
948end
949put cb_n 5 add add p 2 n
950c = add get cb_n 6 1
951put cb_n 6 c
952put cb_av 0 uchar and c 255
953put cb_av 1 uchar shr c 8
954end
955
956fn cb_av_push_tx t view uchar * to void
957p = get cb_n 5
958n = len t
959if gt add add p 2 n 65536
960exit 70
961end
962put cb_av p uchar and n 255
963put cb_av add p 1 uchar shr n 8
964for i ulong n bound 4096
965put cb_av add add p 2 i get t i
966end
967put cb_n 5 add add p 2 n
968c = add get cb_n 6 1
969put cb_n 6 c
970put cb_av 0 uchar and c 255
971put cb_av 1 uchar shr c 8
972end
973
974fn cb_ev_clr to void
975put cb_n 7 2
976put cb_n 8 0
977put cb_ev 0 0
978put cb_ev 1 0
979end
980
981fn cb_ev_push_env_pa to void
982p = get cb_n 7
983n = get cb_n 3
984if gt add add p 2 n 4096
985ret
986end
987if ge get cb_n 8 64
988ret
989end
990put cb_ev p uchar and n 255
991put cb_ev add p 1 uchar shr n 8
992for i ulong n bound 4096
993put cb_ev add add p 2 i get cb_pa i
994end
995put cb_n 7 add add p 2 n
996c = add get cb_n 8 1
997put cb_n 8 c
998put cb_ev 0 uchar and c 255
999put cb_ev 1 uchar shr c 8
1000end
1001
1002fn cb_redir s int d int to void
1003if ge s 0
1004if ne dupfd s d 0 long d
1005exit 127
1006end
1007end
1008end
1009
1010note spawn with fds fi/fo/fe (-1 = inherit parent fd); closes any >=0 fds in parent.
1011note returns exit code 0..255, 256+sig on signal, 512 on spawn error.
1012fn cb_spawn_fds fi int fo int fe int use_ev bool to ulong
1013pid = fork
1014if lt pid 0
1015if ge fi 0
1016z0 = fclose fi
1017end
1018if ge fo 0
1019z1 = fclose fo
1020end
1021if ge fe 0
1022z2 = fclose fe
1023end
1024ret 512
1025end
1026if eq pid 0
1027cb_redir fi 0
1028cb_redir fo 1
1029cb_redir fe 2
1030if use_ev
1031r = execenv cb_av 0 get cb_n 5 cb_ev 0 get cb_n 7
1032else
1033r = execprog cb_av 0 get cb_n 5
1034end
1035exit 127
1036end
1037if ge fi 0
1038z0 = fclose fi
1039end
1040if ge fo 0
1041z1 = fclose fo
1042end
1043if ge fe 0
1044z2 = fclose fe
1045end
1046w = waitpid int pid cb_st 0 4 0
1047if ne w pid
1048ret 512
1049end
1050if ne get cb_st 0 0
1051ret add 256 ulong get cb_st 0
1052end
1053ret ulong get cb_st 1
1054end
1055
1056note compile: OMC (in cb_pc[0..omclen)) < SRC (in cb_pa) > OUT (in cb_pb) 2> err (or inherit if fe == -1)
1057fn cb_run_omc omclen ulong mode int fe int to ulong
1058fi = fopen cb_pa 0 get cb_n 3 524288 0
1059if lt fi 0
1060if ge fe 0
1061z0 = fclose fe
1062end
1063ret 512
1064end
1065z1 = unlink cb_pb 0 get cb_n 4
1066fo = fopen cb_pb 0 get cb_n 4 524865 mode
1067if lt fo 0
1068z2 = fclose int fi
1069if ge fe 0
1070z3 = fclose fe
1071end
1072ret 512
1073end
1074cb_av_clr
1075p = get cb_n 5
1076put cb_av p uchar and omclen 255
1077put cb_av add p 1 uchar shr omclen 8
1078for i ulong omclen bound 4096
1079put cb_av add add p 2 i get cb_pc i
1080end
1081put cb_n 5 add add p 2 omclen
1082put cb_n 6 1
1083put cb_av 0 1
1084put cb_av 1 0
1085ret cb_spawn_fds int fi int fo fe false
1086end
1087
1088fn cb_dent_nlen ns ulong lim ulong to ulong
1089nl = ulong 0
1090while lt add ns nl lim bound 256
1091if eq get cb_dent add ns nl 0
1092break
1093end
1094nl = add nl 1
1095end
1096ret nl
1097end
1098
1099fn cb_rm_flat_one base_len ulong ns ulong nl ulong to void
1100if and eq nl 1 eq get cb_dent ns 46
1101ret
1102end
1103if and eq nl 2 and eq get cb_dent ns 46 eq get cb_dent add ns 1 46
1104ret
1105end
1106put cb_n 3 base_len
1107cb_pa_ch 47
1108for j ulong nl bound 256
1109cb_pa_ch get cb_dent add ns j
1110end
1111z = unlink cb_pa 0 get cb_n 3
1112end
1113
1114fn cb_rm_flat_buf base_len ulong nr ulong to void
1115pos = ulong 0
1116while lt pos nr bound 32768
1117rlen = or ulong get cb_dent add pos 16 shl ulong get cb_dent add pos 17 8
1118if eq rlen 0
1119break
1120end
1121ns = add pos 19
1122nl = cb_dent_nlen ns add pos rlen
1123cb_rm_flat_one base_len ns nl
1124pos = add pos rlen
1125end
1126put cb_n 3 base_len
1127end
1128
1129note remove a flat temporary directory (files inside + rmdir) at cb_pa
1130fn cb_rm_flat_pa to void
1131base_len = get cb_n 3
1132dfd = fopen cb_pa 0 base_len 540672 0
1133if lt dfd 0
1134ret
1135end
1136while true bound 65536
1137nr = getdnts int dfd cb_dent 0 32768
1138if le nr 0
1139break
1140end
1141cb_rm_flat_buf base_len ulong nr
1142end
1143z0 = fclose int dfd
1144put cb_n 3 base_len
1145z1 = rmdir cb_pa 0 base_len
1146end
1147
1148fn cb_rb_match_at i ulong t view uchar * to bool
1149for j ulong len t bound 4096
1150if ne get cb_rb add i j get t j
1151ret false
1152end
1153end
1154ret true
1155end
1156
1157note check whether file in cb_pa contains text t
1158fn cb_file_contains_pa t view uchar * to bool
1159if not cb_read_pa
1160ret false
1161end
1162n = get cb_n 0
1163m = len t
1164if lt n m
1165ret false
1166end
1167lim = add sub n m 1
1168for i ulong lim bound 9223372036854775807
1169if cb_rb_match_at i t
1170ret true
1171end
1172end
1173ret false
1174end