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