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 / map / src / 00-base.om
| 1 | note synapse-map: repo graph, routing, scaffolding and checks for the agents working on aien-dev. |
| 2 | note Composed after seed/json/json.om, seed/json/jsonw.om and seed/net/http.om under one om2 line by |
| 3 | note build.sh. Signatures use unsized arrays (`*`): at this omc pin a const defined after any fn breaks |
| 4 | note later signatures that name a const as an array length, and every library fn precedes these files. |
| 5 | note Name prefixes: functions sm_*, globals g*, texts t*. Library names (js_*, h*) are not reused. |
| 6 | |
| 7 | note ======== state ======== |
| 8 | note gst slots (ulong): see the const block below. |
| 9 | global gst ulong 64 |
| 10 | note gpool: every string the program keeps (records, rules, skills, tokens, paths) |
| 11 | global gpool uchar 8388608 |
| 12 | note gout: stdout and file output; also request bodies (written after the pending output, then cut) |
| 13 | global gout uchar 4194304 |
| 14 | note gfile: the last file or HTTP response body read |
| 15 | global gfile uchar 2097152 |
| 16 | note records: 16 string fields (offset, length) and 16 ulong flags per record, at most 1000 records |
| 17 | global gro ulong 16384 |
| 18 | global grl ulong 16384 |
| 19 | global grf ulong 16384 |
| 20 | note arguments: bytes and (offset, length) pairs, at most 64 arguments |
| 21 | global gab uchar 65536 |
| 22 | global gao ulong 128 |
| 23 | global gpath uchar 4096 |
| 24 | global gpk uchar 4096 |
| 25 | global gnum uchar 32 |
| 26 | |
| 27 | const SPOOL ulong 0 |
| 28 | const SNREC ulong 1 |
| 29 | const SOUT ulong 2 |
| 30 | const SNRUL ulong 3 |
| 31 | const SNSKL ulong 4 |
| 32 | const SNTOK ulong 5 |
| 33 | const SXIP ulong 6 |
| 34 | const SXPORT ulong 7 |
| 35 | const SORGO ulong 8 |
| 36 | const SORGL ulong 9 |
| 37 | const SARGC ulong 10 |
| 38 | const SHOMO ulong 11 |
| 39 | const SHOML ulong 12 |
| 40 | const SXST ulong 13 |
| 41 | const SOVF ulong 14 |
| 42 | const SFLEN ulong 15 |
| 43 | const SPLEN ulong 16 |
| 44 | const SNXR ulong 17 |
| 45 | const SCTYP ulong 18 |
| 46 | const SSLEN ulong 19 |
| 47 | const SIDXO ulong 20 |
| 48 | const SIDXL ulong 21 |
| 49 | const SBINO ulong 22 |
| 50 | const SBINL ulong 23 |
| 51 | const SBAD ulong 24 |
| 52 | const SNONE ulong 4294967295 |
| 53 | |
| 54 | note record string fields |
| 55 | const FREPO ulong 0 |
| 56 | const FPAR ulong 1 |
| 57 | const FPST ulong 2 |
| 58 | const FPUR ulong 3 |
| 59 | const FLAY ulong 4 |
| 60 | const FDEF ulong 5 |
| 61 | const FPUSH ulong 6 |
| 62 | const FBRF ulong 7 |
| 63 | const FSRC ulong 8 |
| 64 | const FRULE ulong 9 |
| 65 | const FNAME ulong 10 |
| 66 | const FTOP ulong 11 |
| 67 | const FURL ulong 12 |
| 68 | const FCPUR ulong 13 |
| 69 | const FCLAY ulong 14 |
| 70 | const FCPAR ulong 15 |
| 71 | note record flags |
| 72 | const KARCH ulong 0 |
| 73 | const KFORK ulong 1 |
| 74 | const KBNUL ulong 2 |
| 75 | const KPNUL ulong 3 |
| 76 | const KPIDX ulong 4 |
| 77 | const KVIS ulong 5 |
| 78 | const KRUL ulong 6 |
| 79 | const KCONF ulong 7 |
| 80 | const KSEM ulong 8 |
| 81 | const KSEMS ulong 9 |
| 82 | |
| 83 | note ======== output buffer ======== |
| 84 | fn sm_ob c uchar to void |
| 85 | n = get gst SOUT |
| 86 | if ge n len gout |
| 87 | put gst SOVF 1 |
| 88 | ret |
| 89 | end |
| 90 | put gout n c |
| 91 | put gst SOUT add n 1 |
| 92 | end |
| 93 | |
| 94 | fn sm_ot t view uchar * to void |
| 95 | for i ulong len t |
| 96 | sm_ob get t i |
| 97 | end |
| 98 | end |
| 99 | |
| 100 | fn sm_op o ulong n ulong to void |
| 101 | for i ulong n |
| 102 | sm_ob get gpool add o i |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | fn sm_hexd v ulong to uchar |
| 107 | if lt v 10 |
| 108 | ret uchar add v 48 |
| 109 | end |
| 110 | ret uchar add v 87 |
| 111 | end |
| 112 | |
| 113 | note sm_oesc: one byte of a JSON string body (RFC 8259 section 7); bytes >= 0x20 other than " and \ pass |
| 114 | fn sm_oesc c uchar to void |
| 115 | if eq c 34 |
| 116 | sm_ob 92 |
| 117 | sm_ob 34 |
| 118 | ret |
| 119 | end |
| 120 | if eq c 92 |
| 121 | sm_ob 92 |
| 122 | sm_ob 92 |
| 123 | ret |
| 124 | end |
| 125 | if lt c 32 |
| 126 | sm_ob 92 |
| 127 | if eq c 10 |
| 128 | sm_ob 110 |
| 129 | ret |
| 130 | end |
| 131 | if eq c 9 |
| 132 | sm_ob 116 |
| 133 | ret |
| 134 | end |
| 135 | if eq c 13 |
| 136 | sm_ob 114 |
| 137 | ret |
| 138 | end |
| 139 | sm_ob 117 |
| 140 | sm_ob 48 |
| 141 | sm_ob 48 |
| 142 | sm_ob sm_hexd ulong shr c 4 |
| 143 | sm_ob sm_hexd ulong and c 15 |
| 144 | ret |
| 145 | end |
| 146 | sm_ob c |
| 147 | end |
| 148 | |
| 149 | fn sm_oq o ulong n ulong to void |
| 150 | sm_ob 34 |
| 151 | for i ulong n |
| 152 | sm_oesc get gpool add o i |
| 153 | end |
| 154 | sm_ob 34 |
| 155 | end |
| 156 | |
| 157 | fn sm_otq t view uchar * to void |
| 158 | sm_ob 34 |
| 159 | for i ulong len t |
| 160 | sm_oesc get t i |
| 161 | end |
| 162 | sm_ob 34 |
| 163 | end |
| 164 | |
| 165 | fn sm_on v ulong to void |
| 166 | if eq v 0 |
| 167 | sm_ob 48 |
| 168 | ret |
| 169 | end |
| 170 | k = ulong 0 |
| 171 | x = v |
| 172 | while gt x 0 bound 21 |
| 173 | put gnum k uchar add rem x 10 48 |
| 174 | x = div x 10 |
| 175 | k = add k 1 |
| 176 | end |
| 177 | for i ulong k |
| 178 | sm_ob get gnum sub sub k i 1 |
| 179 | end |
| 180 | end |
| 181 | |
| 182 | note sm_omil: v thousandths as a JSON number, e.g. 667 -> 0.667 |
| 183 | fn sm_omil v ulong to void |
| 184 | sm_on div v 1000 |
| 185 | sm_ob 46 |
| 186 | f = rem v 1000 |
| 187 | sm_ob uchar add div f 100 48 |
| 188 | sm_ob uchar add rem div f 10 10 48 |
| 189 | sm_ob uchar add rem f 10 48 |
| 190 | end |
| 191 | |
| 192 | note sm_ok: `"name":` ; sm_okc: `,"name":` |
| 193 | fn sm_ok t view uchar * to void |
| 194 | sm_ob 34 |
| 195 | sm_ot t |
| 196 | sm_ob 34 |
| 197 | sm_ob 58 |
| 198 | end |
| 199 | |
| 200 | fn sm_okc t view uchar * to void |
| 201 | sm_ob 44 |
| 202 | sm_ok t |
| 203 | end |
| 204 | |
| 205 | fn sm_obool b bool to void |
| 206 | if b |
| 207 | sm_ob 116 |
| 208 | sm_ob 114 |
| 209 | sm_ob 117 |
| 210 | sm_ob 101 |
| 211 | ret |
| 212 | end |
| 213 | sm_ob 102 |
| 214 | sm_ob 97 |
| 215 | sm_ob 108 |
| 216 | sm_ob 115 |
| 217 | sm_ob 101 |
| 218 | end |
| 219 | |
| 220 | fn sm_onull to void |
| 221 | sm_ob 110 |
| 222 | sm_ob 117 |
| 223 | sm_ob 108 |
| 224 | sm_ob 108 |
| 225 | end |
| 226 | |
| 227 | note sm_flush: write gout[0, SOUT) to fd; every pass writes at least one byte or returns |
| 228 | fn sm_flush fd int to long |
| 229 | n = get gst SOUT |
| 230 | p = ulong 0 |
| 231 | while lt p n bound 999999 |
| 232 | k = write fd gout p sub n p |
| 233 | if le k 0 |
| 234 | put gst SOUT 0 |
| 235 | ret -5 |
| 236 | end |
| 237 | p = add p ulong k |
| 238 | end |
| 239 | put gst SOUT 0 |
| 240 | ret 0 |
| 241 | end |
| 242 | |
| 243 | fn sm_err t view uchar * to void |
| 244 | k = write 2 t 0 len t |
| 245 | end |
| 246 | |
| 247 | fn sm_errp o ulong n ulong to void |
| 248 | if gt n 0 |
| 249 | k = write 2 gpool o n |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | fn sm_errnl to void |
| 254 | k = write 2 gnum 0 0 |
| 255 | put gnum 0 10 |
| 256 | k = write 2 gnum 0 1 |
| 257 | end |
| 258 | |
| 259 | text tovf "synapse-map: internal buffer full\n" |
| 260 | |
| 261 | note An empty text is an unsized name at omc 2c824805 (nm_len 0 marks `view T *`): its len reads a count |
| 262 | note register that nothing set, so `text t ""` gives garbage lengths (omega-mv #775, fixed after this pin |
| 263 | note in e2662fff). The empty view is `part tempty 0 0`; keep it until omega.pin moves past that fix. |
| 264 | text tempty "." |
| 265 | |
| 266 | note sm_die: flush pending stdout, then end the process with code |
| 267 | fn sm_die code int to void |
| 268 | if ne get gst SOVF 0 |
| 269 | put gst SOUT 0 |
| 270 | k = write 2 tovf 0 len tovf |
| 271 | exit 70 |
| 272 | end |
| 273 | r = sm_flush 1 |
| 274 | exit code |
| 275 | end |
| 276 | |
| 277 | note ======== string pool ======== |
| 278 | fn sm_pb c uchar to void |
| 279 | o = get gst SPOOL |
| 280 | if ge o len gpool |
| 281 | put gst SOVF 2 |
| 282 | ret |
| 283 | end |
| 284 | put gpool o c |
| 285 | put gst SPOOL add o 1 |
| 286 | end |
| 287 | |
| 288 | fn sm_pt t view uchar * to ulong |
| 289 | o = get gst SPOOL |
| 290 | for i ulong len t |
| 291 | sm_pb get t i |
| 292 | end |
| 293 | ret o |
| 294 | end |
| 295 | |
| 296 | fn sm_pcp o ulong n ulong to ulong |
| 297 | s = get gst SPOOL |
| 298 | for i ulong n |
| 299 | sm_pb get gpool add o i |
| 300 | end |
| 301 | ret s |
| 302 | end |
| 303 | |
| 304 | fn sm_pnum v ulong to void |
| 305 | if eq v 0 |
| 306 | sm_pb 48 |
| 307 | ret |
| 308 | end |
| 309 | k = ulong 0 |
| 310 | x = v |
| 311 | while gt x 0 bound 21 |
| 312 | put gnum k uchar add rem x 10 48 |
| 313 | x = div x 10 |
| 314 | k = add k 1 |
| 315 | end |
| 316 | for i ulong k |
| 317 | sm_pb get gnum sub sub k i 1 |
| 318 | end |
| 319 | end |
| 320 | |
| 321 | fn sm_lc c uchar to uchar |
| 322 | if and ge c 65 le c 90 |
| 323 | ret add c 32 |
| 324 | end |
| 325 | ret c |
| 326 | end |
| 327 | |
| 328 | fn sm_peq a ulong an ulong b ulong bn ulong to bool |
| 329 | if ne an bn |
| 330 | ret false |
| 331 | end |
| 332 | for i ulong an |
| 333 | if ne get gpool add a i get gpool add b i |
| 334 | ret false |
| 335 | end |
| 336 | end |
| 337 | ret true |
| 338 | end |
| 339 | |
| 340 | note sm_pieq: ASCII case-insensitive equality (GitHub owner/repo names) |
| 341 | fn sm_pieq a ulong an ulong b ulong bn ulong to bool |
| 342 | if ne an bn |
| 343 | ret false |
| 344 | end |
| 345 | for i ulong an |
| 346 | if ne sm_lc get gpool add a i sm_lc get gpool add b i |
| 347 | ret false |
| 348 | end |
| 349 | end |
| 350 | ret true |
| 351 | end |
| 352 | |
| 353 | fn sm_pteq a ulong an ulong t view uchar * to bool |
| 354 | if ne an len t |
| 355 | ret false |
| 356 | end |
| 357 | for i ulong an |
| 358 | if ne get gpool add a i get t i |
| 359 | ret false |
| 360 | end |
| 361 | end |
| 362 | ret true |
| 363 | end |
| 364 | |
| 365 | note sm_pfind: first index of byte c in pool slice, or n |
| 366 | fn sm_pfind a ulong n ulong c uchar to ulong |
| 367 | for i ulong n |
| 368 | if eq get gpool add a i c |
| 369 | ret i |
| 370 | end |
| 371 | end |
| 372 | ret n |
| 373 | end |
| 374 | |
| 375 | note ======== records ======== |
| 376 | fn sm_fo r ulong f ulong to ulong |
| 377 | ret get gro add mul r 16 f |
| 378 | end |
| 379 | |
| 380 | fn sm_fl r ulong f ulong to ulong |
| 381 | ret get grl add mul r 16 f |
| 382 | end |
| 383 | |
| 384 | fn sm_fset r ulong f ulong o ulong n ulong to void |
| 385 | put gro add mul r 16 f o |
| 386 | put grl add mul r 16 f n |
| 387 | end |
| 388 | |
| 389 | fn sm_fsett r ulong f ulong t view uchar * to void |
| 390 | o = sm_pt t |
| 391 | sm_fset r f o len t |
| 392 | end |
| 393 | |
| 394 | fn sm_kg r ulong k ulong to ulong |
| 395 | ret get grf add mul r 16 k |
| 396 | end |
| 397 | |
| 398 | fn sm_ks r ulong k ulong v ulong to void |
| 399 | put grf add mul r 16 k v |
| 400 | end |
| 401 | |
| 402 | fn sm_fq r ulong f ulong to void |
| 403 | sm_oq sm_fo r f sm_fl r f |
| 404 | end |
| 405 | |
| 406 | fn sm_feqt r ulong f ulong t view uchar * to bool |
| 407 | ret sm_pteq sm_fo r f sm_fl r f t |
| 408 | end |
| 409 | |
| 410 | note sm_find: record whose repo equals pool slice (case-insensitive), or SNONE |
| 411 | fn sm_find o ulong n ulong to ulong |
| 412 | for r ulong get gst SNREC |
| 413 | if sm_pieq sm_fo r FREPO sm_fl r FREPO o n |
| 414 | ret r |
| 415 | end |
| 416 | end |
| 417 | ret SNONE |
| 418 | end |
| 419 | |
| 420 | note sm_parref: length of the owner/repo part of a parent string (bytes before '@') |
| 421 | fn sm_parref r ulong to ulong |
| 422 | ret sm_pfind sm_fo r FPAR sm_fl r FPAR 64 |
| 423 | end |
| 424 | |
| 425 | note ======== arguments ======== |
| 426 | fn sm_args to long |
| 427 | n = argcnt |
| 428 | if gt n 64 |
| 429 | ret -7 |
| 430 | end |
| 431 | p = ulong 0 |
| 432 | for i ulong ulong n |
| 433 | k = argread i gab p sub len gab p |
| 434 | if lt k 0 |
| 435 | ret k |
| 436 | end |
| 437 | put gao mul i 2 p |
| 438 | put gao add mul i 2 1 ulong k |
| 439 | p = add p ulong k |
| 440 | end |
| 441 | put gst SARGC ulong n |
| 442 | ret 0 |
| 443 | end |
| 444 | |
| 445 | fn sm_ao i ulong to ulong |
| 446 | ret get gao mul i 2 |
| 447 | end |
| 448 | |
| 449 | fn sm_al i ulong to ulong |
| 450 | ret get gao add mul i 2 1 |
| 451 | end |
| 452 | |
| 453 | fn sm_aeq i ulong t view uchar * to bool |
| 454 | if ge i get gst SARGC |
| 455 | ret false |
| 456 | end |
| 457 | o = sm_ao i |
| 458 | n = sm_al i |
| 459 | if ne n len t |
| 460 | ret false |
| 461 | end |
| 462 | for j ulong n |
| 463 | if ne get gab add o j get t j |
| 464 | ret false |
| 465 | end |
| 466 | end |
| 467 | ret true |
| 468 | end |
| 469 | |
| 470 | note sm_aflag: index of flag t at argument 2 or later, 0 when absent |
| 471 | fn sm_aflag t view uchar * to ulong |
| 472 | for i ulong get gst SARGC |
| 473 | if ge i 2 |
| 474 | if sm_aeq i t |
| 475 | ret i |
| 476 | end |
| 477 | end |
| 478 | end |
| 479 | ret 0 |
| 480 | end |
| 481 | |
| 482 | text tnoval "synapse-map: missing value for " |
| 483 | |
| 484 | note sm_aval: index of the value after flag t, 0 when the flag is absent; a flag without value is usage 64 |
| 485 | fn sm_aval t view uchar * to ulong |
| 486 | i = sm_aflag t |
| 487 | if eq i 0 |
| 488 | ret 0 |
| 489 | end |
| 490 | if ge add i 1 get gst SARGC |
| 491 | sm_err tnoval |
| 492 | sm_err t |
| 493 | sm_errnl |
| 494 | sm_die 64 |
| 495 | end |
| 496 | ret add i 1 |
| 497 | end |
| 498 | |
| 499 | fn sm_apool i ulong to ulong |
| 500 | o = get gst SPOOL |
| 501 | n = sm_al i |
| 502 | a = sm_ao i |
| 503 | for j ulong n |
| 504 | sm_pb get gab add a j |
| 505 | end |
| 506 | put gst SSLEN n |
| 507 | ret o |
| 508 | end |
| 509 | |
| 510 | note sm_wmatch: argument bytes gab[o, o + n) equal w[s, s + n) |
| 511 | fn sm_wmatch o ulong n ulong w view uchar * s ulong to bool |
| 512 | for q ulong n |
| 513 | if ne get gab add o q get w add s q |
| 514 | ret false |
| 515 | end |
| 516 | end |
| 517 | ret true |
| 518 | end |
| 519 | |
| 520 | note sm_inlist: argument i equals one of the space-separated words of w |
| 521 | fn sm_inlist i ulong w view uchar * to bool |
| 522 | o = sm_ao i |
| 523 | n = sm_al i |
| 524 | s = ulong 0 |
| 525 | for j ulong add len w 1 |
| 526 | e = eq j len w |
| 527 | if not e |
| 528 | e = eq get w j 32 |
| 529 | end |
| 530 | if e |
| 531 | if eq sub j s n |
| 532 | if sm_wmatch o n w s |
| 533 | ret true |
| 534 | end |
| 535 | end |
| 536 | s = add j 1 |
| 537 | end |
| 538 | end |
| 539 | ret false |
| 540 | end |
| 541 | |
| 542 | text tunk "synapse-map: unknown flag " |
| 543 | |
| 544 | note sm_argscan: every argument after the subcommand is a value flag (vf) with its value, a switch (sw) |
| 545 | note or a positional; returns the number of positionals (first index in SSLEN), usage 64 on unknown flags |
| 546 | fn sm_argscan vf view uchar * sw view uchar * to ulong |
| 547 | np = ulong 0 |
| 548 | put gst SSLEN 0 |
| 549 | i = ulong 2 |
| 550 | while lt i get gst SARGC bound 70 |
| 551 | o = sm_ao i |
| 552 | isf = false |
| 553 | if ge sm_al i 2 |
| 554 | if eq get gab o 45 |
| 555 | if eq get gab add o 1 45 |
| 556 | isf = true |
| 557 | end |
| 558 | end |
| 559 | end |
| 560 | if isf |
| 561 | if sm_inlist i vf |
| 562 | i = add i 2 |
| 563 | else |
| 564 | if sm_inlist i sw |
| 565 | i = add i 1 |
| 566 | else |
| 567 | sm_err tunk |
| 568 | k = write 2 gab o sm_al i |
| 569 | sm_errnl |
| 570 | sm_die 64 |
| 571 | end |
| 572 | end |
| 573 | else |
| 574 | if eq np 0 |
| 575 | put gst SSLEN i |
| 576 | end |
| 577 | np = add np 1 |
| 578 | i = add i 1 |
| 579 | end |
| 580 | end |
| 581 | ret np |
| 582 | end |
| 583 | |
| 584 | note ======== paths ======== |
| 585 | fn sm_pclr to void |
| 586 | put gst SPLEN 0 |
| 587 | end |
| 588 | |
| 589 | fn sm_pc c uchar to void |
| 590 | n = get gst SPLEN |
| 591 | if ge n len gpath |
| 592 | put gst SOVF 3 |
| 593 | ret |
| 594 | end |
| 595 | put gpath n c |
| 596 | put gst SPLEN add n 1 |
| 597 | end |
| 598 | |
| 599 | fn sm_ptx t view uchar * to void |
| 600 | for i ulong len t |
| 601 | sm_pc get t i |
| 602 | end |
| 603 | end |
| 604 | |
| 605 | fn sm_ppool o ulong n ulong to void |
| 606 | for i ulong n |
| 607 | sm_pc get gpool add o i |
| 608 | end |
| 609 | end |
| 610 | |
| 611 | fn sm_parg i ulong to void |
| 612 | o = sm_ao i |
| 613 | for j ulong sm_al i |
| 614 | sm_pc get gab add o j |
| 615 | end |
| 616 | end |
| 617 | |
| 618 | note sm_phome: gpath = home "/" t |
| 619 | fn sm_phome t view uchar * to void |
| 620 | sm_pclr |
| 621 | sm_ppool get gst SHOMO get gst SHOML |
| 622 | sm_pc 47 |
| 623 | sm_ptx t |
| 624 | end |
| 625 | |
| 626 | note sm_pbin: gpath = directory of argv[0] "/" t |
| 627 | fn sm_pbin t view uchar * to void |
| 628 | sm_pclr |
| 629 | sm_ppool get gst SBINO get gst SBINL |
| 630 | sm_pc 47 |
| 631 | sm_ptx t |
| 632 | end |
| 633 | |
| 634 | note sm_envpre: environment entry b starts with t |
| 635 | fn sm_envpre b view uchar 8192 t view uchar * to bool |
| 636 | for j ulong len t |
| 637 | if ne get b j get t j |
| 638 | ret false |
| 639 | end |
| 640 | end |
| 641 | ret true |
| 642 | end |
| 643 | |
| 644 | note sm_env: value of the environment entry starting with t (e.g. "HOME=") into the pool, or SNONE |
| 645 | fn sm_env t view uchar * to ulong |
| 646 | b = new uchar 8192 0 |
| 647 | n = envcnt |
| 648 | for i ulong ulong n |
| 649 | k = envread i b 0 8192 |
| 650 | if ge k long len t |
| 651 | if sm_envpre b t |
| 652 | o = get gst SPOOL |
| 653 | for j ulong sub ulong k len t |
| 654 | sm_pb get b add len t j |
| 655 | end |
| 656 | put gst SSLEN sub ulong k len t |
| 657 | ret o |
| 658 | end |
| 659 | end |
| 660 | end |
| 661 | ret SNONE |
| 662 | end |
| 663 | |
| 664 | note sm_envkv: the whole environment entry "t<value>" (t ends in '=') into the pool; SNONE when absent |
| 665 | fn sm_envkv t view uchar * to ulong |
| 666 | o = sm_pt t |
| 667 | v = sm_env t |
| 668 | if eq v SNONE |
| 669 | ret SNONE |
| 670 | end |
| 671 | put gst SSLEN add get gst SSLEN len t |
| 672 | ret o |
| 673 | end |
| 674 | |
| 675 | note ======== files ======== |
| 676 | note sm_rdfile: gpath into gfile; length, or -errno; -27 when the file is larger than gfile |
| 677 | fn sm_rdfile to long |
| 678 | x = new uchar 1 0 |
| 679 | fd = fopen gpath 0 get gst SPLEN 524288 0 |
| 680 | if lt fd 0 |
| 681 | ret fd |
| 682 | end |
| 683 | p = ulong 0 |
| 684 | for b ulong 2097153 |
| 685 | if ge p len gfile |
| 686 | break |
| 687 | end |
| 688 | k = read int fd gfile p sub len gfile p |
| 689 | if lt k 0 |
| 690 | c = fclose int fd |
| 691 | ret k |
| 692 | end |
| 693 | if eq k 0 |
| 694 | c = fclose int fd |
| 695 | put gst SFLEN p |
| 696 | ret long p |
| 697 | end |
| 698 | p = add p ulong k |
| 699 | end |
| 700 | k = read int fd x 0 1 |
| 701 | c = fclose int fd |
| 702 | if ne k 0 |
| 703 | ret -27 |
| 704 | end |
| 705 | put gst SFLEN p |
| 706 | ret long p |
| 707 | end |
| 708 | |
| 709 | note sm_wratom: gout[0, SOUT) into gpath ".tmp", fsync, then rename over gpath (atomic replace) |
| 710 | fn sm_wratom to long |
| 711 | n = get gst SPLEN |
| 712 | if gt add mul n 2 12 len gpk |
| 713 | ret -36 |
| 714 | end |
| 715 | put gpk 0 2 |
| 716 | put gpk 1 0 |
| 717 | put gpk 2 uchar and add n 4 255 |
| 718 | put gpk 3 uchar shr add n 4 8 |
| 719 | for i ulong n |
| 720 | put gpk add 4 i get gpath i |
| 721 | end |
| 722 | put gpk add 4 n 46 |
| 723 | put gpk add 5 n 116 |
| 724 | put gpk add 6 n 109 |
| 725 | put gpk add 7 n 112 |
| 726 | q = add n 8 |
| 727 | put gpk q uchar and n 255 |
| 728 | put gpk add q 1 uchar shr n 8 |
| 729 | for i ulong n |
| 730 | put gpk add add q 2 i get gpath i |
| 731 | end |
| 732 | fd = fopen gpk 4 add n 4 524865 420 |
| 733 | if lt fd 0 |
| 734 | put gst SOUT 0 |
| 735 | ret fd |
| 736 | end |
| 737 | r = sm_flush int fd |
| 738 | s = fsync int fd |
| 739 | c = fclose int fd |
| 740 | r = hfe r hfe s c |
| 741 | if lt r 0 |
| 742 | ret r |
| 743 | end |
| 744 | ret renames gpk 0 add q add n 2 0 |
| 745 | end |
| 746 | |
| 747 | note sm_wrnew: gout[0, SOUT) into gpath, refusing to replace an existing file (O_EXCL) |
| 748 | fn sm_wrnew to long |
| 749 | fd = fopen gpath 0 get gst SPLEN 524993 420 |
| 750 | if lt fd 0 |
| 751 | put gst SOUT 0 |
| 752 | ret fd |
| 753 | end |
| 754 | r = sm_flush int fd |
| 755 | c = fclose int fd |
| 756 | ret hfe r c |
| 757 | end |
| 758 | |
| 759 | note sm_mkdirp: create gpath and its missing parents (errors surface at the next open) |
| 760 | fn sm_mkdirp to void |
| 761 | n = get gst SPLEN |
| 762 | for i ulong n |
| 763 | if gt i 0 |
| 764 | if eq get gpath i 47 |
| 765 | r = makedir gpath 0 i 493 |
| 766 | end |
| 767 | end |
| 768 | end |
| 769 | r = makedir gpath 0 n 493 |
| 770 | end |