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 / apps / interplane-forge / tools / bring / src / 10base.om
| 1 | note tools/bring/src/10base.om: the interplane program's shared core (Linux and macOS alike). |
| 2 | note Output, decimal numbers, byte copies and compares, little-endian fields, the packed argument |
| 3 | note list, temp folder paths and the exit path. Platform words live in plat-*.om (pl_* functions). |
| 4 | note Every loop that walks input carries the largest bound (2^63 - 1): each pass consumes at least |
| 5 | note one byte or one record of finite input, so the bound is never what stops it. |
| 6 | text tver "interplane 0.1.1\n" |
| 7 | text tpfx "interplane: " |
| 8 | text tnl "\n" |
| 9 | global gtmp uchar 4096 |
| 10 | global gtmpl ulong 1 |
| 11 | global gtmpok uchar 1 |
| 12 | global gavn ulong 1 |
| 13 | text tavlong "a program's argument list does not fit the 4096 bytes the process start word carries" |
| 14 | text ttmpf "cannot open the temp file " |
| 15 | text tnames "reqh\nfiles\ndirs\nkeep\nlist.tar\nstmt\nstmt.sig\nchal\nchal.sig\nbody\nhdr\ncode\ncerr\nform\nget.tar\nmbox\ngout\n" |
| 16 | |
| 17 | fn wall fd int b view uchar * to bool |
| 18 | n = ulong 0 |
| 19 | while lt n len b bound 9223372036854775807 |
| 20 | r = write fd b n sub len b n |
| 21 | if le r 0 |
| 22 | ret false |
| 23 | end |
| 24 | n = add n ulong r |
| 25 | end |
| 26 | ret true |
| 27 | end |
| 28 | |
| 29 | fn say b view uchar * to void |
| 30 | x = wall 1 b |
| 31 | end |
| 32 | |
| 33 | fn warn b view uchar * to void |
| 34 | x = wall 2 b |
| 35 | end |
| 36 | |
| 37 | note decw: the decimal digits of v at o[n..]; returns the new end |
| 38 | fn decw v ulong o edit uchar * n ulong to ulong |
| 39 | d = new uchar 20 0 |
| 40 | k = ulong 0 |
| 41 | x = v |
| 42 | while true bound 21 |
| 43 | put d k uchar add 48 rem x 10 |
| 44 | k = add k 1 |
| 45 | x = div x 10 |
| 46 | if eq x 0 |
| 47 | break |
| 48 | end |
| 49 | end |
| 50 | for i ulong k |
| 51 | put o add n i get d sub sub k i 1 |
| 52 | end |
| 53 | ret add n k |
| 54 | end |
| 55 | |
| 56 | fn sayd v ulong to void |
| 57 | d = new uchar 24 0 |
| 58 | k = decw v d 0 |
| 59 | say part d 0 k |
| 60 | end |
| 61 | |
| 62 | fn warnd v ulong to void |
| 63 | d = new uchar 24 0 |
| 64 | k = decw v d 0 |
| 65 | warn part d 0 k |
| 66 | end |
| 67 | |
| 68 | note outp: a path or other untrusted bytes for the terminal: control bytes print as '?' |
| 69 | fn sayp b view uchar * to void |
| 70 | o = new uchar 4096 0 |
| 71 | n = ulong 0 |
| 72 | for i ulong len b bound 9223372036854775807 |
| 73 | c = get b i |
| 74 | if or lt c 32 eq c 127 |
| 75 | c = uchar 63 |
| 76 | end |
| 77 | put o n c |
| 78 | n = add n 1 |
| 79 | if eq n 4096 |
| 80 | say part o 0 n |
| 81 | n = ulong 0 |
| 82 | end |
| 83 | end |
| 84 | say part o 0 n |
| 85 | end |
| 86 | |
| 87 | note cpy: copy s into d at n; returns the new end (d too short traps 3: callers size d first) |
| 88 | fn cpy d edit uchar * n ulong s view uchar * to ulong |
| 89 | for i ulong len s bound 9223372036854775807 |
| 90 | put d add n i get s i |
| 91 | end |
| 92 | ret add n len s |
| 93 | end |
| 94 | |
| 95 | fn beq a view uchar * b view uchar * to bool |
| 96 | if ne len a len b |
| 97 | ret false |
| 98 | end |
| 99 | for i ulong len a bound 9223372036854775807 |
| 100 | if ne get a i get b i |
| 101 | ret false |
| 102 | end |
| 103 | end |
| 104 | ret true |
| 105 | end |
| 106 | |
| 107 | note bat: b[i..] starts with t (false when b ends first) |
| 108 | fn bat b view uchar * i ulong t view uchar * to bool |
| 109 | if gt add i len t len b |
| 110 | ret false |
| 111 | end |
| 112 | for k ulong len t bound 9223372036854775807 |
| 113 | if ne get b add i k get t k |
| 114 | ret false |
| 115 | end |
| 116 | end |
| 117 | ret true |
| 118 | end |
| 119 | |
| 120 | note bfind: the first index >= from where t starts in b, or -1 |
| 121 | fn bfind b view uchar * t view uchar * from ulong to long |
| 122 | if gt len t len b |
| 123 | ret -1 |
| 124 | end |
| 125 | i = from |
| 126 | while le add i len t len b bound 9223372036854775807 |
| 127 | if bat b i t |
| 128 | ret long i |
| 129 | end |
| 130 | i = add i 1 |
| 131 | end |
| 132 | ret -1 |
| 133 | end |
| 134 | |
| 135 | fn lcb c uchar to uchar |
| 136 | if and ge c 65 le c 90 |
| 137 | ret add c 32 |
| 138 | end |
| 139 | ret c |
| 140 | end |
| 141 | |
| 142 | note bati: as bat, ASCII case-insensitive (t is lowercase) |
| 143 | fn bati b view uchar * i ulong t view uchar * to bool |
| 144 | if gt add i len t len b |
| 145 | ret false |
| 146 | end |
| 147 | for k ulong len t bound 9223372036854775807 |
| 148 | if ne lcb get b add i k get t k |
| 149 | ret false |
| 150 | end |
| 151 | end |
| 152 | ret true |
| 153 | end |
| 154 | |
| 155 | fn isdig c uchar to bool |
| 156 | ret and ge c 48 le c 57 |
| 157 | end |
| 158 | |
| 159 | fn isaln c uchar to bool |
| 160 | a = and ge c 65 le c 90 |
| 161 | b = and ge c 97 le c 122 |
| 162 | ret or or a b isdig c |
| 163 | end |
| 164 | |
| 165 | fn rd16 b view uchar * o ulong to ulong |
| 166 | ret or ulong get b o shl ulong get b add o 1 8 |
| 167 | end |
| 168 | |
| 169 | fn rd32 b view uchar * o ulong to ulong |
| 170 | lo = rd16 b o |
| 171 | ret or lo shl rd16 b add o 2 16 |
| 172 | end |
| 173 | |
| 174 | fn rd64 b view uchar * o ulong to ulong |
| 175 | lo = rd32 b o |
| 176 | ret or lo shl rd32 b add o 4 32 |
| 177 | end |
| 178 | |
| 179 | fn wr16 b edit uchar * o ulong v ulong to void |
| 180 | put b o uchar and v 255 |
| 181 | put b add o 1 uchar and shr v 8 255 |
| 182 | end |
| 183 | |
| 184 | fn wr64 b edit uchar * o ulong v ulong to void |
| 185 | for i ulong 8 |
| 186 | put b add o i uchar and shr v mul i 8 255 |
| 187 | end |
| 188 | end |
| 189 | |
| 190 | note pdec: a decimal number from all of s; false (o untouched) when s is empty, has a non-digit |
| 191 | note or does not fit 64 bits |
| 192 | fn pdec s view uchar * o edit ulong 1 to bool |
| 193 | if eq len s 0 |
| 194 | ret false |
| 195 | end |
| 196 | v = ulong 0 |
| 197 | for i ulong len s bound 9223372036854775807 |
| 198 | c = get s i |
| 199 | if not isdig c |
| 200 | ret false |
| 201 | end |
| 202 | if gt v 1844674407370955160 |
| 203 | ret false |
| 204 | end |
| 205 | w = add mul v 10 ulong sub c 48 |
| 206 | if lt w mul v 10 |
| 207 | ret false |
| 208 | end |
| 209 | v = w |
| 210 | end |
| 211 | put o 0 v |
| 212 | ret true |
| 213 | end |
| 214 | |
| 215 | note ---- packed argument list (OMEGESE s10: u16 count, then u16 length + bytes per record) |
| 216 | fn avinit a edit uchar * to void |
| 217 | put a 0 0 |
| 218 | put a 1 0 |
| 219 | put gavn 0 2 |
| 220 | end |
| 221 | |
| 222 | fn avadd a edit uchar * s view uchar * to void |
| 223 | n = get gavn 0 |
| 224 | if gt add add n 2 len s len a |
| 225 | die tavlong |
| 226 | end |
| 227 | wr16 a n len s |
| 228 | n = cpy a add n 2 s |
| 229 | put gavn 0 n |
| 230 | wr16 a 0 add rd16 a 0 1 |
| 231 | end |
| 232 | |
| 233 | note ---- the private temp folder |
| 234 | note tpath: <temp folder>/<nm> into o; returns its length |
| 235 | fn tpath nm view uchar * o edit uchar 4096 to ulong |
| 236 | n = get gtmpl 0 |
| 237 | for i ulong n bound 4096 |
| 238 | put o i get gtmp i |
| 239 | end |
| 240 | put o n 47 |
| 241 | ret cpy o add n 1 nm |
| 242 | end |
| 243 | |
| 244 | note topen: open the temp file nm: how 0 read, 2 write (created or emptied, mode 0600) |
| 245 | fn topen nm view uchar * how ulong to int |
| 246 | p = new uchar 4096 0 |
| 247 | k = tpath nm p |
| 248 | fd = pl_open part p 0 k how 384 |
| 249 | if lt fd 0 |
| 250 | warn tpfx |
| 251 | warn ttmpf |
| 252 | warn nm |
| 253 | warn tnl |
| 254 | cleanup |
| 255 | exit 1 |
| 256 | end |
| 257 | ret int fd |
| 258 | end |
| 259 | |
| 260 | note cleanup: remove the temp folder's known files, then the folder (best effort, never fails) |
| 261 | fn cleanup to void |
| 262 | if eq get gtmpok 0 0 |
| 263 | ret |
| 264 | end |
| 265 | p = new uchar 4096 0 |
| 266 | s = ulong 0 |
| 267 | for i ulong len tnames |
| 268 | if eq get tnames i 10 |
| 269 | k = tpath part tnames s sub i s p |
| 270 | x = pl_unlink part p 0 k |
| 271 | s = add i 1 |
| 272 | end |
| 273 | end |
| 274 | k = get gtmpl 0 |
| 275 | for i ulong k bound 4096 |
| 276 | put p i get gtmp i |
| 277 | end |
| 278 | x = pl_rmdir part p 0 k |
| 279 | put gtmpok 0 0 |
| 280 | end |
| 281 | |
| 282 | note die: one line on stderr, cleanup, exit 1 |
| 283 | fn die m view uchar * to void |
| 284 | warn tpfx |
| 285 | warn m |
| 286 | warn tnl |
| 287 | cleanup |
| 288 | exit 1 |
| 289 | end |
| 290 | |
| 291 | fn die2 m view uchar * a view uchar * to void |
| 292 | warn tpfx |
| 293 | warn m |
| 294 | warn a |
| 295 | warn tnl |
| 296 | cleanup |
| 297 | exit 1 |
| 298 | end |