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
omega-mv / protocol / synapse / board / tools / build.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tools/build.om: build synapse-board from src/*.om with the pinned seeded omc (replaces build.sh). |
| 3 | note It is an OM-2 program composed after the pinned omega-mv's om/tls/sha2.om (SHA-256 of the compiler); |
| 4 | note README.md (Build) holds the one command that compiles it. |
| 5 | note usage: build OMEGA_SRC OUT (run from the synapse-board repository root) |
| 6 | note OMEGA_SRC checkout of omega-mv at the commit in omega.pin; its seed/out/omc must have the pinned sha256 |
| 7 | note OUT output binary; OUT.unit.om keeps the composed unit, OUT.err omc's diagnostics; missing |
| 8 | note directories above OUT are created |
| 9 | note The unit is the om2 line, then OMEGA_SRC seed/json/json.om, seed/net/http.om and seed/net/notls.om, |
| 10 | note then every src/*.om in byte order of its name. OMEGA_SRC's commit is read from its .git directory |
| 11 | note (or the gitdir named by a .git file): HEAD, the loose ref it names, or that ref's packed-refs line. |
| 12 | note Exit: 0 built; 1 omc rejected the unit (OUT removed, never run); 64 usage or not run from the |
| 13 | note repository root; 65 omega.pin malformed, OMEGA_SRC not at the pinned commit, omc missing or not the |
| 14 | note pinned compiler, an input that cannot be read, or an output that cannot be written. |
| 15 | |
| 16 | text tuse "usage: build OMEGA_SRC OUT (run from the synapse-board repository root)\n" |
| 17 | text tme "build: " |
| 18 | text tnl "\n" |
| 19 | text tpin "omega.pin" |
| 20 | text tsrc "src" |
| 21 | text tdotom ".om" |
| 22 | text tgit "/.git" |
| 23 | text thead "/HEAD" |
| 24 | text tslash "/" |
| 25 | text tpacked "/packed-refs" |
| 26 | text tcommon "/commondir" |
| 27 | text tomc "/seed/out/omc" |
| 28 | text tjson "/seed/json/json.om" |
| 29 | text thttp "/seed/net/http.om" |
| 30 | text tnotls "/seed/net/notls.om" |
| 31 | text tunit ".unit.om" |
| 32 | text ttmp ".tmp" |
| 33 | text terr ".err" |
| 34 | text tom2 "om2\n" |
| 35 | text tpre1 "aien-dev/omega-mv@" |
| 36 | text tpre2 "omc-sha256 " |
| 37 | text tref "ref: " |
| 38 | text tgd "gitdir: " |
| 39 | text emroot "omega.pin not found; run from the synapse-board repository root" |
| 40 | text empin "omega.pin malformed" |
| 41 | text emhead "cannot read the commit of OMEGA_SRC from its .git" |
| 42 | text emrev "OMEGA_SRC is at " |
| 43 | text emwant ", omega.pin wants " |
| 44 | text emomc "omc missing or unreadable: " |
| 45 | text emsha "omc sha256 " |
| 46 | text emsrc "cannot list src/" |
| 47 | text emfull "the host refused memory for the src/ name table (enlarge of gno, gnl or gnm)" |
| 48 | text emread "cannot read " |
| 49 | text emwrite "cannot write " |
| 50 | text emrej "omc rejected " |
| 51 | text emcdir "the commondir of OMEGA_SRC's gitdir names a path over 8191 bytes" |
| 52 | text embuilt "built " |
| 53 | text hexd "0123456789abcdef" |
| 54 | |
| 55 | global gsrc uchar 4096 |
| 56 | global gout uchar 4096 |
| 57 | global gl ulong 6 |
| 58 | note gl: 0 OMEGA_SRC length, 1 OUT length, 2 path length, 3 git dir length, 4 bytes used in gnm, |
| 59 | note 5 path overflow (gp would pass 8191 bytes: openp then refuses with -36, ENAMETOOLONG) |
| 60 | global gp uchar 8192 |
| 61 | global gdir uchar 8192 |
| 62 | global gbuf uchar 65536 |
| 63 | global gpin uchar 4096 |
| 64 | global gpinl ulong 1 |
| 65 | global gwant uchar 40 |
| 66 | global gwomc uchar 64 |
| 67 | global ghave uchar 40 |
| 68 | global gsha uchar 64 |
| 69 | global gline uchar 4096 |
| 70 | global gpk uchar 16384 |
| 71 | global gst uchar 4 |
| 72 | global gdn uchar 32768 |
| 73 | note the src/ name table: run-time pools grown to fit (gnm the name bytes, gno and gnl each name's offset |
| 74 | note and length); a host refusal of the memory is the only way it stops |
| 75 | global gnm uchar * |
| 76 | global gno ulong * |
| 77 | global gnl ulong * |
| 78 | global gnc ulong 2 |
| 79 | note gnc: 0 names collected, 1 set when the host refused to grow the name table |
| 80 | |
| 81 | note ======== diagnostics |
| 82 | fn say s view uchar * to void |
| 83 | r = write 2 tme 0 len tme |
| 84 | r = write 2 s 0 len s |
| 85 | end |
| 86 | fn sayl s view uchar * to void |
| 87 | say s |
| 88 | r = write 2 tnl 0 len tnl |
| 89 | end |
| 90 | fn saypath to void |
| 91 | r = write 2 gp 0 get gl 2 |
| 92 | r = write 2 tnl 0 len tnl |
| 93 | end |
| 94 | |
| 95 | note ======== the path scratch gp |
| 96 | fn pclr to void |
| 97 | put gl 2 0 |
| 98 | put gl 5 0 |
| 99 | end |
| 100 | fn pc c uchar to void |
| 101 | n = get gl 2 |
| 102 | if lt n 8191 |
| 103 | put gp n c |
| 104 | put gl 2 add n 1 |
| 105 | else |
| 106 | put gl 5 1 |
| 107 | end |
| 108 | end |
| 109 | fn pt s view uchar * to void |
| 110 | for i ulong len s |
| 111 | pc get s i |
| 112 | end |
| 113 | end |
| 114 | fn psrc to void |
| 115 | for i ulong get gl 0 |
| 116 | pc get gsrc i |
| 117 | end |
| 118 | end |
| 119 | fn pout to void |
| 120 | for i ulong get gl 1 |
| 121 | pc get gout i |
| 122 | end |
| 123 | end |
| 124 | fn pdir to void |
| 125 | for i ulong get gl 3 |
| 126 | pc get gdir i |
| 127 | end |
| 128 | end |
| 129 | note pname: append the i-th collected src/ name |
| 130 | fn pname i ulong to void |
| 131 | o = get gno i |
| 132 | for k ulong get gnl i |
| 133 | pc get gnm add o k |
| 134 | end |
| 135 | end |
| 136 | fn openp flags ulong mode ulong to long |
| 137 | if ne get gl 5 0 |
| 138 | ret -36 |
| 139 | end |
| 140 | ret fopen gp 0 get gl 2 int flags int mode |
| 141 | end |
| 142 | note mkparents: create each missing directory above OUT (like mkdir -p of its dirname); an existing one is kept |
| 143 | fn mkparents to void |
| 144 | pclr |
| 145 | for i ulong get gl 1 |
| 146 | c = get gout i |
| 147 | if and eq c 47 gt i 0 |
| 148 | r = makedir gp 0 get gl 2 493 |
| 149 | end |
| 150 | pc c |
| 151 | end |
| 152 | end |
| 153 | |
| 154 | note ======== small files: rd reads gp into gline (at most 4095 bytes); byte count or negative |
| 155 | fn rdline to long |
| 156 | fd = openp 524288 0 |
| 157 | if lt fd 0 |
| 158 | ret fd |
| 159 | end |
| 160 | p = ulong 0 |
| 161 | for it ulong 4096 |
| 162 | if ge p 4095 |
| 163 | break |
| 164 | end |
| 165 | r = read int fd gline p sub 4095 p |
| 166 | if lt r 0 |
| 167 | if ne r -4 |
| 168 | c = fclose int fd |
| 169 | ret r |
| 170 | end |
| 171 | else |
| 172 | if eq r 0 |
| 173 | break |
| 174 | end |
| 175 | p = add p ulong r |
| 176 | end |
| 177 | end |
| 178 | c = fclose int fd |
| 179 | if ge p 4095 |
| 180 | ret -27 |
| 181 | end |
| 182 | ret long p |
| 183 | end |
| 184 | |
| 185 | note ======== omega.pin: line 1 aien-dev/omega-mv@SHA40, line 2 omc-sha256 HEX64 |
| 186 | fn ishex c uchar to bool |
| 187 | ret or and ge c 48 le c 57 and ge c 97 le c 102 |
| 188 | end |
| 189 | fn pinload to bool |
| 190 | pclr |
| 191 | pt tpin |
| 192 | fd = openp 524288 0 |
| 193 | if lt fd 0 |
| 194 | ret false |
| 195 | end |
| 196 | r = read int fd gpin 0 4096 |
| 197 | c = fclose int fd |
| 198 | if or lt r 0 ge r 4096 |
| 199 | put gpinl 0 4096 |
| 200 | ret true |
| 201 | end |
| 202 | put gpinl 0 ulong r |
| 203 | ret true |
| 204 | end |
| 205 | fn pinparse to bool |
| 206 | n = get gpinl 0 |
| 207 | a = len tpre1 |
| 208 | b = len tpre2 |
| 209 | want = add add add a 41 add b 64 1 |
| 210 | if ne n want |
| 211 | ret false |
| 212 | end |
| 213 | for i ulong a |
| 214 | if ne get gpin i get tpre1 i |
| 215 | ret false |
| 216 | end |
| 217 | end |
| 218 | for i ulong 40 |
| 219 | c = get gpin add a i |
| 220 | if not ishex c |
| 221 | ret false |
| 222 | end |
| 223 | put gwant i c |
| 224 | end |
| 225 | if ne get gpin add a 40 10 |
| 226 | ret false |
| 227 | end |
| 228 | s = add a 41 |
| 229 | for i ulong b |
| 230 | if ne get gpin add s i get tpre2 i |
| 231 | ret false |
| 232 | end |
| 233 | end |
| 234 | s = add s b |
| 235 | for i ulong 64 |
| 236 | c = get gpin add s i |
| 237 | if not ishex c |
| 238 | ret false |
| 239 | end |
| 240 | put gwomc i c |
| 241 | end |
| 242 | ret eq get gpin add s 64 10 |
| 243 | end |
| 244 | |
| 245 | note ======== OMEGA_SRC's commit, read from its git directory |
| 246 | note gitdir: OMEGA_SRC/.git when that is a directory, else the path a ".git" file names |
| 247 | fn findgit to bool |
| 248 | pclr |
| 249 | psrc |
| 250 | pt tgit |
| 251 | pt thead |
| 252 | fd = openp 524288 0 |
| 253 | if ge fd 0 |
| 254 | c = fclose int fd |
| 255 | n = sub get gl 2 len thead |
| 256 | for i ulong n |
| 257 | put gdir i get gp i |
| 258 | end |
| 259 | put gl 3 n |
| 260 | ret true |
| 261 | end |
| 262 | pclr |
| 263 | psrc |
| 264 | pt tgit |
| 265 | r = rdline |
| 266 | if lt r 0 |
| 267 | ret false |
| 268 | end |
| 269 | m = len tgd |
| 270 | if lt ulong r add m 1 |
| 271 | ret false |
| 272 | end |
| 273 | for i ulong m |
| 274 | if ne get gline i get tgd i |
| 275 | ret false |
| 276 | end |
| 277 | end |
| 278 | e = ulong r |
| 279 | while and gt e m eq get gline sub e 1 10 bound 4096 |
| 280 | e = sub e 1 |
| 281 | end |
| 282 | n = ulong 0 |
| 283 | if ne get gline m 47 |
| 284 | for i ulong get gl 0 |
| 285 | put gdir n get gsrc i |
| 286 | n = add n 1 |
| 287 | end |
| 288 | put gdir n 47 |
| 289 | n = add n 1 |
| 290 | end |
| 291 | for i ulong sub e m |
| 292 | put gdir n get gline add m i |
| 293 | n = add n 1 |
| 294 | end |
| 295 | put gl 3 n |
| 296 | ret true |
| 297 | end |
| 298 | note commondir: a worktree's gitdir names the shared repository in its commondir file; refs live there |
| 299 | fn common to void |
| 300 | pclr |
| 301 | pdir |
| 302 | pt tcommon |
| 303 | r = rdline |
| 304 | if le r 0 |
| 305 | ret |
| 306 | end |
| 307 | e = ulong r |
| 308 | for k ulong 4096 |
| 309 | if eq e 0 |
| 310 | break |
| 311 | end |
| 312 | if ne get gline sub e 1 10 |
| 313 | break |
| 314 | end |
| 315 | e = sub e 1 |
| 316 | end |
| 317 | d = get gl 3 |
| 318 | if eq get gline 0 47 |
| 319 | d = 0 |
| 320 | else |
| 321 | put gdir d 47 |
| 322 | d = add d 1 |
| 323 | end |
| 324 | for i ulong e |
| 325 | if lt d 8191 |
| 326 | put gdir d get gline i |
| 327 | d = add d 1 |
| 328 | else |
| 329 | sayl emcdir |
| 330 | put gl 3 0 |
| 331 | ret |
| 332 | end |
| 333 | end |
| 334 | put gl 3 d |
| 335 | end |
| 336 | note hex40: gline[o..o+40) is 40 lowercase hex digits; copy them into ghave |
| 337 | fn hex40 o ulong to bool |
| 338 | for i ulong 40 |
| 339 | c = get gline add o i |
| 340 | if not ishex c |
| 341 | ret false |
| 342 | end |
| 343 | put ghave i c |
| 344 | end |
| 345 | ret true |
| 346 | end |
| 347 | note headsha: 1 found, 0 not resolvable |
| 348 | fn headsha to bool |
| 349 | if not findgit |
| 350 | ret false |
| 351 | end |
| 352 | pclr |
| 353 | pdir |
| 354 | pt thead |
| 355 | r = rdline |
| 356 | m = len tref |
| 357 | if lt r long add m 1 |
| 358 | ret false |
| 359 | end |
| 360 | isref = true |
| 361 | for i ulong m |
| 362 | if ne get gline i get tref i |
| 363 | isref = false |
| 364 | end |
| 365 | end |
| 366 | if not isref |
| 367 | if lt r 40 |
| 368 | ret false |
| 369 | end |
| 370 | ret hex40 0 |
| 371 | end |
| 372 | e = ulong r |
| 373 | while and gt e m eq get gline sub e 1 10 bound 4096 |
| 374 | e = sub e 1 |
| 375 | end |
| 376 | rl = sub e m |
| 377 | rn = new uchar 1024 0 |
| 378 | if gt rl 1024 |
| 379 | ret false |
| 380 | end |
| 381 | for i ulong rl |
| 382 | put rn i get gline add m i |
| 383 | end |
| 384 | common |
| 385 | pclr |
| 386 | pdir |
| 387 | pt tslash |
| 388 | for i ulong rl |
| 389 | pc get rn i |
| 390 | end |
| 391 | r = rdline |
| 392 | if ge r 40 |
| 393 | ret hex40 0 |
| 394 | end |
| 395 | pclr |
| 396 | pdir |
| 397 | pt tpacked |
| 398 | ret packed rn rl |
| 399 | end |
| 400 | note packed: scan packed-refs line by line for "SHA40 NAME"; lines longer than gline cannot be NAME |
| 401 | fn packed rn view uchar 1024 rl ulong to bool |
| 402 | fd = openp 524288 0 |
| 403 | if lt fd 0 |
| 404 | ret false |
| 405 | end |
| 406 | ll = ulong 0 |
| 407 | skip = false |
| 408 | for it ulong 999999 |
| 409 | r = read int fd gbuf 0 65536 |
| 410 | if lt r 0 |
| 411 | if ne r -4 |
| 412 | c = fclose int fd |
| 413 | ret false |
| 414 | end |
| 415 | continue |
| 416 | end |
| 417 | if eq r 0 |
| 418 | break |
| 419 | end |
| 420 | for i ulong ulong r |
| 421 | c = get gbuf i |
| 422 | if eq c 10 |
| 423 | if and not skip linematch rn rl ll |
| 424 | f = fclose int fd |
| 425 | ret true |
| 426 | end |
| 427 | ll = 0 |
| 428 | skip = false |
| 429 | else |
| 430 | if lt ll 4095 |
| 431 | put gline ll c |
| 432 | ll = add ll 1 |
| 433 | else |
| 434 | skip = true |
| 435 | end |
| 436 | end |
| 437 | end |
| 438 | end |
| 439 | note a last line without its newline is matched too |
| 440 | m = and not skip linematch rn rl ll |
| 441 | c = fclose int fd |
| 442 | ret m |
| 443 | end |
| 444 | fn linematch rn view uchar 1024 rl ulong ll ulong to bool |
| 445 | if ne ll add 41 rl |
| 446 | ret false |
| 447 | end |
| 448 | if ne get gline 40 32 |
| 449 | ret false |
| 450 | end |
| 451 | for i ulong rl |
| 452 | if ne get gline add 41 i get rn i |
| 453 | ret false |
| 454 | end |
| 455 | end |
| 456 | ret hex40 0 |
| 457 | end |
| 458 | |
| 459 | note ======== SHA-256 of OMEGA_SRC/seed/out/omc, streamed (sha2.om from the pinned omega-mv) |
| 460 | fn hashomc to bool |
| 461 | pclr |
| 462 | psrc |
| 463 | pt tomc |
| 464 | fd = openp 524288 0 |
| 465 | if lt fd 0 |
| 466 | ret false |
| 467 | end |
| 468 | ctx = new ulong 40 0 |
| 469 | sha256_init ctx |
| 470 | blk = new uchar 65536 0 |
| 471 | one = new uchar 1 0 |
| 472 | for it ulong 999999 |
| 473 | r = read int fd gbuf 0 65536 |
| 474 | if lt r 0 |
| 475 | if ne r -4 |
| 476 | c = fclose int fd |
| 477 | ret false |
| 478 | end |
| 479 | continue |
| 480 | end |
| 481 | if eq r 0 |
| 482 | break |
| 483 | end |
| 484 | n = ulong r |
| 485 | if eq n 65536 |
| 486 | for i ulong 65536 |
| 487 | put blk i get gbuf i |
| 488 | end |
| 489 | sha256_update ctx blk |
| 490 | else |
| 491 | for i ulong n |
| 492 | put one 0 get gbuf i |
| 493 | sha256_update ctx one |
| 494 | end |
| 495 | end |
| 496 | end |
| 497 | c = fclose int fd |
| 498 | d = new uchar 32 0 |
| 499 | sha256_final ctx d |
| 500 | for i ulong 32 |
| 501 | v = get d i |
| 502 | put gsha mul i 2 get hexd ulong shr v 4 |
| 503 | put gsha add mul i 2 1 get hexd ulong and v 15 |
| 504 | end |
| 505 | ret true |
| 506 | end |
| 507 | |
| 508 | note ======== src/*.om names, sorted by bytes |
| 509 | fn nameless a ulong b ulong to bool |
| 510 | oa = get gno a |
| 511 | ob = get gno b |
| 512 | na = get gnl a |
| 513 | nb = get gnl b |
| 514 | m = na |
| 515 | if lt nb m |
| 516 | m = nb |
| 517 | end |
| 518 | for i ulong m |
| 519 | ca = get gnm add oa i |
| 520 | cb = get gnm add ob i |
| 521 | if ne ca cb |
| 522 | ret lt ca cb |
| 523 | end |
| 524 | end |
| 525 | ret lt na nb |
| 526 | end |
| 527 | note takeent: record the getdents64 record at gdn[p] when it names a visible *.om file; false when the name table is full |
| 528 | fn takeent p ulong to bool |
| 529 | q = add p 19 |
| 530 | n = ulong 0 |
| 531 | while ne get gdn add q n 0 bound 256 |
| 532 | n = add n 1 |
| 533 | end |
| 534 | k = len tdotom |
| 535 | if or le n k eq get gdn q 46 |
| 536 | ret true |
| 537 | end |
| 538 | for i ulong k |
| 539 | if ne get gdn add q add sub n k i get tdotom i |
| 540 | ret true |
| 541 | end |
| 542 | end |
| 543 | c = get gnc 0 |
| 544 | used = get gl 4 |
| 545 | if lt len gno add c 1 |
| 546 | r1 = enlarge gno add mul c 2 64 |
| 547 | r2 = enlarge gnl add mul c 2 64 |
| 548 | if or ne r1 0 ne r2 0 |
| 549 | put gnc 1 1 |
| 550 | ret false |
| 551 | end |
| 552 | end |
| 553 | if lt len gnm add used n |
| 554 | r3 = enlarge gnm add mul add used n 2 4096 |
| 555 | if ne r3 0 |
| 556 | put gnc 1 1 |
| 557 | ret false |
| 558 | end |
| 559 | end |
| 560 | put gno c used |
| 561 | put gnl c n |
| 562 | for i ulong n |
| 563 | put gnm add used i get gdn add q i |
| 564 | end |
| 565 | put gl 4 add used n |
| 566 | put gnc 0 add c 1 |
| 567 | ret true |
| 568 | end |
| 569 | fn sortnames to void |
| 570 | for i ulong get gnc 0 |
| 571 | j = i |
| 572 | while gt j 0 bound 9223372036854775807 |
| 573 | if not nameless j sub j 1 |
| 574 | break |
| 575 | end |
| 576 | ho = get gno j |
| 577 | tl = get gnl j |
| 578 | put gno j get gno sub j 1 |
| 579 | put gnl j get gnl sub j 1 |
| 580 | put gno sub j 1 ho |
| 581 | put gnl sub j 1 tl |
| 582 | j = sub j 1 |
| 583 | end |
| 584 | end |
| 585 | end |
| 586 | fn listsrc to bool |
| 587 | pclr |
| 588 | pt tsrc |
| 589 | fd = openp 540672 0 |
| 590 | if lt fd 0 |
| 591 | ret false |
| 592 | end |
| 593 | put gl 4 0 |
| 594 | put gnc 0 0 |
| 595 | for it ulong 999999 |
| 596 | r = getdnts int fd gdn 0 32768 |
| 597 | if le r 0 |
| 598 | c = fclose int fd |
| 599 | if lt r 0 |
| 600 | ret false |
| 601 | end |
| 602 | sortnames |
| 603 | ret true |
| 604 | end |
| 605 | p = ulong 0 |
| 606 | while lt p ulong r bound 32768 |
| 607 | if not takeent p |
| 608 | c = fclose int fd |
| 609 | ret false |
| 610 | end |
| 611 | p = add p add ulong get gdn add p 16 shl ulong get gdn add p 17 8 |
| 612 | end |
| 613 | end |
| 614 | c = fclose int fd |
| 615 | ret false |
| 616 | end |
| 617 | |
| 618 | note ======== composing the unit |
| 619 | fn wall fd int s view uchar * to bool |
| 620 | p = ulong 0 |
| 621 | for it ulong 4096 |
| 622 | if ge p len s |
| 623 | ret true |
| 624 | end |
| 625 | w = write fd s p sub len s p |
| 626 | if le w 0 |
| 627 | ret false |
| 628 | end |
| 629 | p = add p ulong w |
| 630 | end |
| 631 | ret false |
| 632 | end |
| 633 | note copyin: append the file named by gp to fd; false (gp still names it) when it cannot be read or written |
| 634 | fn copyin fd int to bool |
| 635 | in = openp 524288 0 |
| 636 | if lt in 0 |
| 637 | ret false |
| 638 | end |
| 639 | for it ulong 999999 |
| 640 | r = read int in gbuf 0 65536 |
| 641 | if lt r 0 |
| 642 | if ne r -4 |
| 643 | c = fclose int in |
| 644 | ret false |
| 645 | end |
| 646 | continue |
| 647 | end |
| 648 | if eq r 0 |
| 649 | c = fclose int in |
| 650 | ret true |
| 651 | end |
| 652 | p = ulong 0 |
| 653 | for k ulong 65536 |
| 654 | if ge p ulong r |
| 655 | break |
| 656 | end |
| 657 | w = write fd gbuf p sub ulong r p |
| 658 | if le w 0 |
| 659 | c = fclose int in |
| 660 | ret false |
| 661 | end |
| 662 | p = add p ulong w |
| 663 | end |
| 664 | end |
| 665 | c = fclose int in |
| 666 | ret false |
| 667 | end |
| 668 | note compose: write OUT.unit.om; 0 ok, 65 an input could not be read, 66 the unit could not be written |
| 669 | fn compose to int |
| 670 | pclr |
| 671 | pout |
| 672 | pt tunit |
| 673 | fd = openp 524865 420 |
| 674 | if lt fd 0 |
| 675 | ret 66 |
| 676 | end |
| 677 | if not wall int fd tom2 |
| 678 | c = fclose int fd |
| 679 | ret 66 |
| 680 | end |
| 681 | for s ulong 4 |
| 682 | pclr |
| 683 | if eq s 0 |
| 684 | psrc |
| 685 | pt tjson |
| 686 | end |
| 687 | if eq s 1 |
| 688 | psrc |
| 689 | pt thttp |
| 690 | end |
| 691 | if eq s 2 |
| 692 | psrc |
| 693 | pt tnotls |
| 694 | end |
| 695 | if eq s 3 |
| 696 | break |
| 697 | end |
| 698 | if not copyin int fd |
| 699 | c = fclose int fd |
| 700 | ret 65 |
| 701 | end |
| 702 | end |
| 703 | for i ulong get gnc 0 |
| 704 | pclr |
| 705 | pt tsrc |
| 706 | pt tslash |
| 707 | pname i |
| 708 | if not copyin int fd |
| 709 | c = fclose int fd |
| 710 | ret 65 |
| 711 | end |
| 712 | end |
| 713 | c = fclose int fd |
| 714 | if lt c 0 |
| 715 | ret 66 |
| 716 | end |
| 717 | ret 0 |
| 718 | end |
| 719 | |
| 720 | note ======== running omc: stdin OUT.unit.om, stdout OUT.tmp, stderr OUT.err |
| 721 | fn pkadd to void |
| 722 | n = get gl 2 |
| 723 | c = add ulong get gpk 0 shl ulong get gpk 1 8 |
| 724 | o = ulong 2 |
| 725 | for i ulong c |
| 726 | o = add add o 2 add ulong get gpk o shl ulong get gpk add o 1 8 |
| 727 | end |
| 728 | put gpk o uchar and n 255 |
| 729 | put gpk add o 1 uchar shr n 8 |
| 730 | for i ulong n |
| 731 | put gpk add add o 2 i get gp i |
| 732 | end |
| 733 | c = add c 1 |
| 734 | put gpk 0 uchar and c 255 |
| 735 | put gpk 1 uchar shr c 8 |
| 736 | end |
| 737 | fn pklen to ulong |
| 738 | c = add ulong get gpk 0 shl ulong get gpk 1 8 |
| 739 | o = ulong 2 |
| 740 | for i ulong c |
| 741 | o = add add o 2 add ulong get gpk o shl ulong get gpk add o 1 8 |
| 742 | end |
| 743 | ret o |
| 744 | end |
| 745 | fn child to void |
| 746 | pclr |
| 747 | pout |
| 748 | pt tunit |
| 749 | fd = openp 524288 0 |
| 750 | if lt fd 0 |
| 751 | exit 126 |
| 752 | end |
| 753 | r = dupfd int fd 0 0 |
| 754 | pclr |
| 755 | pout |
| 756 | pt ttmp |
| 757 | fd = openp 524865 493 |
| 758 | if lt fd 0 |
| 759 | exit 126 |
| 760 | end |
| 761 | r = fchmod int fd 493 |
| 762 | if lt r 0 |
| 763 | exit 126 |
| 764 | end |
| 765 | r = dupfd int fd 1 0 |
| 766 | pclr |
| 767 | pout |
| 768 | pt terr |
| 769 | fd = openp 524865 420 |
| 770 | if lt fd 0 |
| 771 | exit 126 |
| 772 | end |
| 773 | r = dupfd int fd 2 0 |
| 774 | put gpk 0 0 |
| 775 | put gpk 1 0 |
| 776 | pclr |
| 777 | psrc |
| 778 | pt tomc |
| 779 | pkadd |
| 780 | r = execprog gpk 0 pklen |
| 781 | exit 127 |
| 782 | end |
| 783 | note runomc: omc's exit status, or -1 when it could not be run or did not exit normally |
| 784 | fn runomc to long |
| 785 | pid = fork |
| 786 | if lt pid 0 |
| 787 | ret -1 |
| 788 | end |
| 789 | if eq pid 0 |
| 790 | child |
| 791 | end |
| 792 | r = waitpid int pid gst 0 4 0 |
| 793 | if ne r pid |
| 794 | ret -1 |
| 795 | end |
| 796 | if ne get gst 0 0 |
| 797 | ret -1 |
| 798 | end |
| 799 | ret long get gst 1 |
| 800 | end |
| 801 | fn errempty to bool |
| 802 | pclr |
| 803 | pout |
| 804 | pt terr |
| 805 | fd = openp 524288 0 |
| 806 | if lt fd 0 |
| 807 | ret false |
| 808 | end |
| 809 | r = read int fd gbuf 0 1 |
| 810 | c = fclose int fd |
| 811 | ret eq r 0 |
| 812 | end |
| 813 | fn rmtmp to void |
| 814 | pclr |
| 815 | pout |
| 816 | pt ttmp |
| 817 | r = unlink gp 0 get gl 2 |
| 818 | end |
| 819 | fn catom to void |
| 820 | pclr |
| 821 | pout |
| 822 | pt terr |
| 823 | fd = openp 524288 0 |
| 824 | if lt fd 0 |
| 825 | ret |
| 826 | end |
| 827 | for it ulong 999999 |
| 828 | r = read int fd gbuf 0 65536 |
| 829 | if le r 0 |
| 830 | break |
| 831 | end |
| 832 | w = write 2 gbuf 0 ulong r |
| 833 | end |
| 834 | c = fclose int fd |
| 835 | end |
| 836 | |
| 837 | fn main to int |
| 838 | if ne argcnt 3 |
| 839 | r = write 2 tuse 0 len tuse |
| 840 | ret 64 |
| 841 | end |
| 842 | n = argread 1 gsrc 0 4096 |
| 843 | if lt n 1 |
| 844 | r = write 2 tuse 0 len tuse |
| 845 | ret 64 |
| 846 | end |
| 847 | put gl 0 ulong n |
| 848 | n = argread 2 gout 0 4096 |
| 849 | if lt n 1 |
| 850 | r = write 2 tuse 0 len tuse |
| 851 | ret 64 |
| 852 | end |
| 853 | put gl 1 ulong n |
| 854 | if not pinload |
| 855 | sayl emroot |
| 856 | ret 64 |
| 857 | end |
| 858 | if not pinparse |
| 859 | sayl empin |
| 860 | ret 65 |
| 861 | end |
| 862 | if not headsha |
| 863 | sayl emhead |
| 864 | ret 65 |
| 865 | end |
| 866 | same = true |
| 867 | for i ulong 40 |
| 868 | if ne get ghave i get gwant i |
| 869 | same = false |
| 870 | end |
| 871 | end |
| 872 | if not same |
| 873 | say emrev |
| 874 | r = write 2 ghave 0 40 |
| 875 | r = write 2 emwant 0 len emwant |
| 876 | r = write 2 gwant 0 40 |
| 877 | r = write 2 tnl 0 len tnl |
| 878 | ret 65 |
| 879 | end |
| 880 | if not hashomc |
| 881 | say emomc |
| 882 | saypath |
| 883 | ret 65 |
| 884 | end |
| 885 | same = true |
| 886 | for i ulong 64 |
| 887 | if ne get gsha i get gwomc i |
| 888 | same = false |
| 889 | end |
| 890 | end |
| 891 | if not same |
| 892 | say emsha |
| 893 | r = write 2 gsha 0 64 |
| 894 | r = write 2 emwant 0 len emwant |
| 895 | r = write 2 gwomc 0 64 |
| 896 | r = write 2 tnl 0 len tnl |
| 897 | ret 65 |
| 898 | end |
| 899 | if not listsrc |
| 900 | if ne get gnc 1 0 |
| 901 | sayl emfull |
| 902 | ret 65 |
| 903 | end |
| 904 | sayl emsrc |
| 905 | ret 65 |
| 906 | end |
| 907 | mkparents |
| 908 | pclr |
| 909 | pout |
| 910 | r = unlink gp 0 get gl 2 |
| 911 | rmtmp |
| 912 | k = compose |
| 913 | if eq k 65 |
| 914 | say emread |
| 915 | saypath |
| 916 | ret 65 |
| 917 | end |
| 918 | if ne k 0 |
| 919 | say emwrite |
| 920 | saypath |
| 921 | ret 65 |
| 922 | end |
| 923 | e = runomc |
| 924 | if or ne e 0 not errempty |
| 925 | rmtmp |
| 926 | catom |
| 927 | say emrej |
| 928 | pclr |
| 929 | pout |
| 930 | pt tunit |
| 931 | saypath |
| 932 | ret 1 |
| 933 | end |
| 934 | pclr |
| 935 | pout |
| 936 | pt ttmp |
| 937 | put gpk 0 0 |
| 938 | put gpk 1 0 |
| 939 | pkadd |
| 940 | pclr |
| 941 | pout |
| 942 | pkadd |
| 943 | r = renames gpk 0 pklen 0 |
| 944 | if lt r 0 |
| 945 | say emwrite |
| 946 | saypath |
| 947 | ret 65 |
| 948 | end |
| 949 | say embuilt |
| 950 | saypath |
| 951 | ret 0 |
| 952 | end |