interplane.Get Omega
Read in your language

babel

aien-dev/babel

Brought by drakestapleton

branch main Branches Tags History

babel / src / 10-io.om

522 lines · 7928 bytes Raw Blame History
1note babel 10-io: files, directories, environment, tool lookup, child processes.
2global Ev0 ulong 512
3global Evn ulong 512
4global Evc ulong 1
5global Gv0 ulong 1
6global Gvn ulong 1
7global Pk uchar 4096
8global Kn ulong 1
9global Kc ulong 1
10global Rio ulong 8
11global Rmg ulong 1
12global St uchar 8
13text DevNull "/dev/null"
14text EnvBin "/usr/bin/env"
15text EvPath "PATH="
16text EvHome "HOME="
17text EvUser "USER="
18text EvTmp "TMPDIR="
19text EvLang "LANG="
20text EvCh "CARGO_HOME="
21text EvRh "RUSTUP_HOME="
22text EvRt "RUSTUP_TOOLCHAIN="
23text EvGp "GOPATH="
24text EvGm "GOMODCACHE="
25text EvGtl "GOTOOLCHAIN=local"
26text EvCgo "CGO_ENABLED=0"
27text EvGpx "GOPROXY=off"
28text EvGfl "GOFLAGS=-mod=mod"
29text EvCtc "CARGO_TERM_COLOR=never"
30text EvNet "CARGO_NET_OFFLINE=true"
31
32note write Ob[c0, c1) to the file named Ob[p0, p1); 0 or -errno
33fn wfile c0 ulong c1 ulong p0 ulong p1 ulong to long
34fd = fopen Ob p0 sub p1 p0 577 420
35if lt fd 0
36ret fd
37end
38o = c0
39while lt o c1 bound 999999
40w = write int fd Ob o sub c1 o
41if le w 0
42r = fclose int fd
43ret -5
44end
45o = add o ulong w
46end
47r = fclose int fd
48ret 0
49end
50
51note read the file named Ob[p0, p1) into Fb; size or -errno (-27 when larger than Fb)
52fn rfile p0 ulong p1 ulong to long
53fd = fopen Ob p0 sub p1 p0 0 0
54if lt fd 0
55ret fd
56end
57n = ulong 0
58while true bound 999999
59if ge n len Fb
60r = fclose int fd
61ret -27
62end
63r = read int fd Fb n sub len Fb n
64if lt r 0
65c = fclose int fd
66ret r
67end
68if eq r 0
69break
70end
71n = add n ulong r
72end
73c = fclose int fd
74put Fn 0 n
75ret long n
76end
77
78note copy file Ob[s0, s1) to Ob[d0, d1), hashing the bytes into Hash; size or -errno
79fn cphash s0 ulong s1 ulong d0 ulong d1 ulong to long
80fi = fopen Ob s0 sub s1 s0 0 0
81if lt fi 0
82ret fi
83end
84fo = fopen Ob d0 sub d1 d0 577 420
85if lt fo 0
86c = fclose int fi
87ret fo
88end
89hx = new uchar 104 0
90hd = new uchar 32 0
91b = new uchar 262144 0
92sha256_init hx
93t = ulong 0
94while true bound 999999
95r = read int fi b 0 262144
96if le r 0
97break
98end
99n = ulong r
100sha256_update hx part b 0 n
101w = write int fo b 0 n
102if ne w r
103c = fclose int fi
104c = fclose int fo
105ret -5
106end
107t = add t n
108end
109sha256_final hx hd
110for i ulong 32
111put Hash i get hd i
112end
113c = fclose int fi
114c = fclose int fo
115ret long t
116end
117
118note SHA-256 of the file Ob[p0, p1) into Hash; size or -errno
119fn hfile p0 ulong p1 ulong to long
120fi = fopen Ob p0 sub p1 p0 0 0
121if lt fi 0
122ret fi
123end
124hx = new uchar 104 0
125hd = new uchar 32 0
126b = new uchar 262144 0
127sha256_init hx
128t = ulong 0
129while true bound 999999
130r = read int fi b 0 262144
131if le r 0
132break
133end
134n = ulong r
135sha256_update hx part b 0 n
136t = add t n
137end
138sha256_final hx hd
139for i ulong 32
140put Hash i get hd i
141end
142c = fclose int fi
143ret long t
144end
145
146note lstat Ob[p0, p1) into Sb: 0 absent, 1 file, 2 directory, 3 other
147fn ftype p0 ulong p1 ulong to ulong
148r = lstat Ob p0 sub p1 p0 Sb 0 128
149if lt r 0
150ret 0
151end
152m = and get Sb 17 240
153if eq m 128
154ret 1
155end
156if eq m 64
157ret 2
158end
159ret 3
160end
161
162fn fsize to ulong
163s = ulong 0
164for i ulong 8
165s = or shl s 8 ulong get Sb sub 55 i
166end
167ret s
168end
169
170note mkdir -p of Ob[p0, p1) (absolute path); 0 or -errno
171fn mkdirs p0 ulong p1 ulong to long
172i = add p0 1
173while le i p1 bound 999999
174e = eq i p1
175if not e
176e = eq get Ob i 47
177end
178if e
179r = makedir Ob p0 sub i p0 493
180if lt r 0
181if ne r -17
182ret r
183end
184end
185end
186i = add i 1
187end
188ret 0
189end
190
191note read the entries of directory Ob[p0, p1) onto the arena as records
192note (type uchar, name bytes, 0); "." and ".." skipped; returns the record end or 0 on error
193fn dirrecs p0 ulong p1 ulong to ulong
194fd = fopen Ob p0 sub p1 p0 16384 0
195if lt fd 0
196ret 0
197end
198while true bound 999999
199r = getdnts int fd Dn 0 len Dn
200if le r 0
201break
202end
203o = ulong 0
204while lt o ulong r bound 999999
205o = drec o
206end
207end
208c = fclose int fd
209ret mark
210end
211
212note copy one getdents64 record at Dn[o] to the arena; returns the next record offset
213fn drec o ulong to ulong
214n = add o 19
215k = n
216while ne get Dn k 0 bound 4096
217k = add k 1
218end
219sk = false
220if eq get Dn n 46
221if eq k add n 1
222sk = true
223end
224if eq k add n 2
225if eq get Dn add n 1 46
226sk = true
227end
228end
229end
230if not sk
231bc get Dn add o 18
232for i ulong sub k n
233bc get Dn add n i
234end
235bc 0
236end
237ret add o or ulong get Dn add o 16 shl ulong get Dn add o 17 8
238end
239
240note rm -rf of Ob[p0, p1); 0 or -errno
241fn rmtree p0 ulong p1 ulong to long
242t = ftype p0 p1
243if eq t 0
244ret 0
245end
246if ne t 2
247ret unlink Ob p0 sub p1 p0
248end
249m = mark
250e = dirrecs p0 p1
251if eq e 0
252reset m
253ret -13
254end
255o = m
256while lt o e bound 999999
257ty = get Ob o
258n = add o 1
259k = n
260while ne get Ob k 0 bound 4096
261k = add k 1
262end
263c0 = mark
264bob p0 sub p1 p0
265bc 47
266bob n sub k n
267c1 = mark
268if eq ty 4
269r = rmtree c0 c1
270else
271r = unlink Ob c0 sub c1 c0
272end
273reset c0
274o = add k 1
275end
276reset m
277ret rmdir Ob p0 sub p1 p0
278end
279
280note environment snapshot: every NAME=VALUE goes to the pool
281fn envinit to void
282n = envcnt
283if lt n 0
284ret
285end
286c = ulong 0
287for i ulong ulong n
288if ge c 512
289break
290end
291r = envread i Ag 0 len Ag
292if le r 0
293continue
294end
295m = mark
296for j ulong ulong r
297bc get Ag j
298end
299put Ev0 c psave m ulong r
300put Evn c ulong r
301reset m
302c = add c 1
303end
304put Evc 0 c
305end
306
307note look NAME= up; sets Gv0/Gvn to the value in the pool
308fn getenv s view uchar * to bool
309for i ulong get Evc 0
310o = get Ev0 i
311n = get Evn i
312if plpre o n s
313put Gv0 0 add o len s
314put Gvn 0 sub n len s
315ret true
316end
317end
318ret false
319end
320
321fn envfwd o ulong n ulong to bool
322if plpre o n EvPath
323ret true
324end
325if plpre o n EvHome
326ret true
327end
328if plpre o n EvUser
329ret true
330end
331if plpre o n EvTmp
332ret true
333end
334if plpre o n EvLang
335ret true
336end
337if plpre o n EvCh
338ret true
339end
340if plpre o n EvRh
341ret true
342end
343if plpre o n EvRt
344ret true
345end
346if plpre o n EvGp
347ret true
348end
349ret plpre o n EvGm
350end
351
352note search PATH for an executable named s; sets Gv0/Gvn to its absolute path in the pool
353fn which s view uchar * to bool
354if not getenv EvPath
355ret false
356end
357o = get Gv0 0
358e = add o get Gvn 0
359i = o
360while le i e bound 999999
361d = eq i e
362if not d
363d = eq get Pl i 58
364end
365if d
366if gt i o
367m = mark
368bpl o sub i o
369bc 47
370bs s
371x = mark
372r = faccess Ob m sub x m 1
373if eq r 0
374put Gv0 0 psave m sub x m
375put Gvn 0 sub x m
376reset m
377ret true
378end
379reset m
380end
381o = add i 1
382end
383i = add i 1
384end
385ret false
386end
387
388note packed argv in Pk: u16 count, then u16 length + bytes per record
389fn krst to void
390put Kn 0 2
391put Kc 0 0
392end
393
394fn kbyte c uchar to void
395o = get Kn 0
396if lt o 4096
397put Pk o c
398put Kn 0 add o 1
399else
400put Cap 0 1
401end
402end
403
404fn khead n ulong to void
405kbyte uchar and n 255
406kbyte uchar shr n 8
407put Kc 0 add get Kc 0 1
408end
409
410fn kob o ulong n ulong to void
411khead n
412for i ulong n
413kbyte get Ob add o i
414end
415end
416
417fn kpl o ulong n ulong to void
418khead n
419for i ulong n
420kbyte get Pl add o i
421end
422end
423
424fn ktx s view uchar * to void
425khead len s
426for i ulong len s
427kbyte get s i
428end
429end
430
431note argv prefix: /usr/bin/env with the forwarded variables and fixed offline/toolchain pins
432fn kenv to void
433krst
434ktx EnvBin
435for i ulong get Evc 0
436o = get Ev0 i
437n = get Evn i
438if envfwd o n
439kpl o n
440end
441end
442ktx EvGtl
443ktx EvCgo
444ktx EvGpx
445ktx EvGfl
446ktx EvCtc
447ktx EvNet
448end
449
450fn kfin to void
451c = get Kc 0
452put Pk 0 uchar and c 255
453put Pk 1 uchar shr c 8
454end
455
456note redirections: Rio 0/1 stdin, 2/3 stdout, 4/5 stderr, 6/7 cwd (Ob ranges; empty = /dev/null or none)
457fn rclr to void
458for i ulong 8
459put Rio i 0
460end
461put Rmg 0 0
462end
463
464fn ropen i ulong flags int fd int to void
465a = get Rio i
466b = get Rio add i 1
467f = long -1
468if gt b a
469f = fopen Ob a sub b a flags 420
470else
471f = fopen DevNull 0 len DevNull flags 0
472end
473if lt f 0
474exit int 126
475end
476r = dupfd int f fd 0
477if lt r 0
478exit int 126
479end
480c = fclose int f
481end
482
483fn childio to void
484ropen 0 0 0
485ropen 2 577 1
486if eq get Rmg 0 1
487r = dupfd 1 2 0
488else
489ropen 4 577 2
490end
491a = get Rio 6
492b = get Rio 7
493if gt b a
494c = chdir Ob a sub b a
495if lt c 0
496exit int 126
497end
498end
499end
500
501note run Pk; exit status, 128 + signal, or 255 when the process could not be started
502fn spawn to ulong
503kfin
504pid = fork
505if lt pid 0
506ret 255
507end
508if eq pid 0
509childio
510r = execprog Pk 0 get Kn 0
511exit int 127
512end
513w = waitpid int pid St 0 4 0
514if ne w pid
515ret 255
516end
517s = ulong and get St 0 127
518if ne s 0
519ret add 128 s
520end
521ret ulong get St 1
522end