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 / src / osh / osh_expand.osc
| 1 | // GENERATED by src/osh/osh_gen.sh from osh_layout.lst and osh_expand.osc.in. Do not edit; edit the .in or the list. |
| 2 | // osh expander unit (aien-architecture#158, cut 4c): bounded, resumable parameter expansion, quote removal, field |
| 3 | // splitting and request-record writer. Entry: expand_run(inp, w). Contract: aien-protocols |
| 4 | // specs/osh-platform/OSH_PLATFORM_ABI.md (v1 DRAFT) sections 5 to 7; design: handoffs/osh-audit/E-parser-design.md |
| 5 | // section 4. Needs the external slices (OSC-EXT-BYTES). |
| 6 | // |
| 7 | // It reads the token records (lexer) and the command and pipeline tables (parser) the earlier units left in the same |
| 8 | // workspace, expands ONE pipeline at a time (the host runs it and updates $? before the next call), and writes the |
| 9 | // request record (ABI 7.1) at REQUEST, with the expanded bytes (1 byte per cell) at OUT. |
| 10 | // |
| 11 | // expand_run returns one status: |
| 12 | // 103 PIPELINE_READY the request record is valid (header, ncmds blocks of 164 cells, OUT bytes); execute it, write |
| 13 | // the pipeline status to S_LAST_STATUS, then call expand_run again |
| 14 | // 104 COMPLETE no pipeline of the list is left (also: a blank list) |
| 15 | // 102 NEED_VAR a variable is wanted: S_VR_KIND (1 NAME at S_VR_A/S_VR_LEN in inp, 2 $?, 3 positional S_VR_A, |
| 16 | // 4 $#, 5 positional count only) ; the host writes STAGING {found, len, npos, 0, bytes...} and calls |
| 17 | // expand_run again. The core writes STAGING[0] = 2 (unanswered) with the request; a repeated call |
| 18 | // while STAGING[0] is still above 1 returns 102 again and changes nothing (idempotent). |
| 19 | // 101 BUDGET_EXHAUSTED 1024 steps spent (a step is one raw byte, one value byte or one token), call again |
| 20 | // 209 CAP_FIELDS 210 CAP_OUT 211 CAP_VALUE 212 CAP_VARREQ 243 AMBIGUOUS_REDIRECT 244 VALUE_NUL 245 VALUE_GLOB |
| 21 | // 239 UNSUPPORTED_BUILTIN (argv[0] after expansion is set eval exec . source trap shift read return break continue |
| 22 | // local readonly alias wait : declare typeset let command type umask hash ulimit getopts jobs pushd popd dirs |
| 23 | // history unalias fg bg builtin enable shopt mapfile readarray times caller disown suspend logout bind help compgen complete |
| 24 | // fc compopt; |
| 25 | // names over 8 bytes such as readarray are not caught) 213 WORKSPACE_SIZE 214 ABI_MAGIC 215 ABI_VERSION 216 ABI_LENGTH |
| 26 | // 217 ABI_RESERVED (also: the parser has not completed the list, or an expander cell is corrupt) |
| 27 | // A refusal stores code and the byte offset of the word being expanded (the command word start for 239) in the session |
| 28 | // and makes it terminal (phase 255); a repeated call returns the same code. Pipelines before it were already returned. |
| 29 | // |
| 30 | // Pipeline selection (unless the host set S_NOSKIP = 1, which emits every pipeline): pipeline p > 0 runs iff the connector after pipeline p-1 allows it given S_LAST_STATUS (&& needs 0, |
| 31 | // || needs nonzero, ; always); a skipped pipeline is never expanded and S_LAST_STATUS is unchanged, so the next |
| 32 | // connector sees the same status (left to right, as a POSIX list). |
| 33 | // |
| 34 | // Expansion per word, one pass over the raw token bytes: backslash removal (unquoted: \x gives x; in "": only \$ \" \\ |
| 35 | // \` and backslash-newline are escapes, other backslashes stay), '' and "" removal, backslash-newline removal, and |
| 36 | // $NAME ${NAME} $0..$9 ${0..9} $? $# $@ $*. Field splitting is by the fixed IFS (space, tab, newline) and only for unquoted |
| 37 | // expansions in command words and redirection targets. An unquoted expansion result byte * ? [ is VALUE_GLOB. |
| 38 | // Empty rules: '' and "" and "$X" keep one empty field; an unquoted expansion that is empty or all IFS adds none; |
| 39 | // "$@" with no positionals adds none; "a$@b" with none is ab. Assignment values (NAME=value before the command word) are |
| 40 | // expanded without splitting and without the glob check; $@ and $* there join the positionals with one space. In a |
| 41 | // standalone assignment list (no command word) a later value sees an earlier value of the same name. |
| 42 | // A redirection target must expand to exactly one field (AMBIGUOUS_REDIRECT otherwise). N>&M copies N and M. |
| 43 | // |
| 44 | // Word state lives in the session (EX_* cells); every step is atomic, so any call may be repeated after 101 and 102. |
| 45 | |
| 46 | // Record a refusal: code and byte offset into the session, session terminal. Returns the code. |
| 47 | fn fail(w: cells, code: u64, off: u64) -> u64 { |
| 48 | w[4] = code; |
| 49 | w[5] = off; |
| 50 | w[2] = 255; |
| 51 | return code; |
| 52 | } |
| 53 | |
| 54 | // A refusal at the start of the word being expanded. |
| 55 | fn werr(w: cells, code: u64) -> u64 { |
| 56 | return fail(w, code, w[64 + w[32] * 4 + 1]); |
| 57 | } |
| 58 | |
| 59 | // ABI header check (OSH_PLATFORM_ABI.md section 3): first failure wins, nothing is written. |
| 60 | fn abi_check(w: cells) -> u64 { |
| 61 | if w.len < 2 { return 216; } |
| 62 | if w[0] != 1330858071 { return 214; } |
| 63 | if w[1] != 1 { return 215; } |
| 64 | if w.len < 11456 { return 213; } |
| 65 | for j in 48 .. 64 { |
| 66 | if w[j as u64] != 0 { return 217; } |
| 67 | } |
| 68 | if w[28] > 1 { return 217; } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | // 1 when c may be in a NAME (digits only when first is 0). |
| 73 | fn nch(c: u64, first: u64) -> u64 { |
| 74 | if c >= 65 && c <= 90 { return 1; } |
| 75 | if c >= 97 && c <= 122 { return 1; } |
| 76 | if c == 95 { return 1; } |
| 77 | if first == 0 && c >= 48 && c <= 57 { return 1; } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | // End (exclusive) of the NAME that starts at p, below e. |
| 82 | fn nend(inp: bytes, p: u64, e: u64) -> u64 { |
| 83 | let mut i: u64 = p + 1; |
| 84 | while i < e && nch(inp[i], 0) == 1 bound 4096 { |
| 85 | i = i + 1; |
| 86 | } |
| 87 | return i; |
| 88 | } |
| 89 | |
| 90 | // The `$` at p (token ends at e): 1 when it starts a reference (EX_RK, S_VR_A, S_VR_LEN, EX_NEXT set), 0 when it is a |
| 91 | // literal dollar. RK 8 is $@ and 9 is $*; begin_ref turns them into list modes. |
| 92 | fn dollar(inp: bytes, w: cells, p: u64, e: u64) -> u64 { |
| 93 | if p + 1 >= e { return 0; } |
| 94 | let c: u64 = inp[p + 1]; |
| 95 | w[10] = 0; |
| 96 | w[9] = 0; |
| 97 | if nch(c, 1) == 1 { |
| 98 | let n: u64 = nend(inp, p + 1, e); |
| 99 | w[22] = 1; |
| 100 | w[9] = p + 1; |
| 101 | w[10] = n - p - 1; |
| 102 | w[18] = n; |
| 103 | return 1; |
| 104 | } |
| 105 | if c >= 48 && c <= 57 { |
| 106 | w[22] = 3; |
| 107 | w[9] = c - 48; |
| 108 | w[18] = p + 2; |
| 109 | return 1; |
| 110 | } |
| 111 | if c == 63 || c == 35 || c == 64 || c == 42 { |
| 112 | let mut rk: u64 = 2; |
| 113 | if c == 35 { rk = 4; } |
| 114 | if c == 64 { rk = 8; } |
| 115 | if c == 42 { rk = 9; } |
| 116 | w[22] = rk; |
| 117 | w[18] = p + 2; |
| 118 | return 1; |
| 119 | } |
| 120 | if c == 123 && p + 3 < e { |
| 121 | let d: u64 = inp[p + 2]; |
| 122 | if d >= 48 && d <= 57 { |
| 123 | if inp[p + 3] != 125 { return 0; } |
| 124 | w[22] = 3; |
| 125 | w[9] = d - 48; |
| 126 | w[18] = p + 4; |
| 127 | return 1; |
| 128 | } |
| 129 | if nch(d, 1) == 1 { |
| 130 | let n2: u64 = nend(inp, p + 2, e); |
| 131 | if n2 < e && inp[n2] == 125 { |
| 132 | w[22] = 1; |
| 133 | w[9] = p + 2; |
| 134 | w[10] = n2 - p - 2; |
| 135 | w[18] = n2 + 1; |
| 136 | return 1; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | // First cell of the request block of the current command. |
| 144 | fn cbase(w: cells) -> u64 { |
| 145 | return 1928 + 8 + w[31] * 164; |
| 146 | } |
| 147 | |
| 148 | // Open a field if none is open. |
| 149 | fn fopen_(w: cells) { |
| 150 | if w[13] == 0 { |
| 151 | w[13] = 1; |
| 152 | w[14] = w[42]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Close the open field (if any): argv entry, or the single redirection target. |
| 157 | fn fclose(w: cells) -> u64 { |
| 158 | if w[13] == 0 { return 0; } |
| 159 | w[13] = 0; |
| 160 | let wm: u64 = w[45]; |
| 161 | if wm == 2 { return 0; } |
| 162 | let off: u64 = w[14]; |
| 163 | let ln: u64 = w[42] - off; |
| 164 | let cb: u64 = cbase(w); |
| 165 | if wm == 1 || wm == 4 { |
| 166 | let n: u64 = w[cb]; |
| 167 | if n >= 32 { return werr(w, 209); } |
| 168 | if n == 0 { w[44] = w[64 + w[32] * 4 + 1]; } |
| 169 | w[cb + 4 + n * 2] = off; |
| 170 | w[cb + 5 + n * 2] = ln; |
| 171 | w[cb] = n + 1; |
| 172 | return 0; |
| 173 | } |
| 174 | if w[16] != 0 { return werr(w, 243); } |
| 175 | w[16] = 1; |
| 176 | let e: u64 = cb + 132 + (w[cb + 2] - 1) * 4; |
| 177 | w[e + 2] = off; |
| 178 | w[e + 3] = ln; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | // Append one byte to the open field (opening one if needed). |
| 183 | fn lit(w: cells, c: u64) -> u64 { |
| 184 | let o: u64 = w[42]; |
| 185 | if o >= 8192 { return werr(w, 210); } |
| 186 | fopen_(w); |
| 187 | w[3264 + o] = c; |
| 188 | w[42] = o + 1; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | // One byte of an unquoted expansion result: an IFS byte ends the field, * ? [ are refused, anything else is literal. |
| 193 | fn sbyte(w: cells, c: u64) -> u64 { |
| 194 | if c == 32 || c == 9 || c == 10 { return fclose(w); } |
| 195 | if c == 42 || c == 63 || c == 91 { return werr(w, 245); } |
| 196 | return lit(w, c); |
| 197 | } |
| 198 | |
| 199 | // Ask the host for a variable (kind in S_VR_KIND, operands already in S_VR_A and S_VR_LEN). |
| 200 | fn request(w: cells, kind: u64) -> u64 { |
| 201 | let n: u64 = w[33]; |
| 202 | if n >= 64 { return werr(w, 212); } |
| 203 | w[33] = n + 1; |
| 204 | w[8] = kind; |
| 205 | w[11] = w[11] + 1; |
| 206 | w[896] = 2; |
| 207 | w[46] = 2; |
| 208 | return 102; |
| 209 | } |
| 210 | |
| 211 | // Cell of the latest earlier assignment entry of this command named inp[ns..ns+nl), or 0. Only in a command with no |
| 212 | // command word, so that `A=1 B=$A` sees the new A (a prefix assignment sees the old value). |
| 213 | fn find_prior(inp: bytes, w: cells, ns: u64, nl: u64) -> u64 { |
| 214 | let pb: u64 = 832 + w[30] * 4; |
| 215 | if w[576 + (w[pb] + w[31]) * 8 + 3] != 0 { return 0; } |
| 216 | let cb: u64 = cbase(w); |
| 217 | let na: u64 = w[cb + 1]; |
| 218 | let mut hit: u64 = 0; |
| 219 | let mut j: u64 = 0; |
| 220 | while j + 1 < na bound 16 { |
| 221 | let e: u64 = cb + 68 + j * 4; |
| 222 | if w[e + 1] == nl { |
| 223 | let mut same: u64 = 1; |
| 224 | let mut i: u64 = 0; |
| 225 | while i < nl && same == 1 bound 4096 { |
| 226 | if inp[ns + i] != w[3264 + w[e] + i] { same = 0; } |
| 227 | i = i + 1; |
| 228 | } |
| 229 | if same == 1 { hit = e; } |
| 230 | } |
| 231 | j = j + 1; |
| 232 | } |
| 233 | return hit; |
| 234 | } |
| 235 | |
| 236 | // A reference was parsed (dollar): decide how its value is fetched. q is the quote state. |
| 237 | fn begin_ref(inp: bytes, w: cells, q: u64) -> u64 { |
| 238 | let rk: u64 = w[22]; |
| 239 | let wm: u64 = w[45]; |
| 240 | let mut vm: u64 = 0; |
| 241 | if q == 0 && wm != 2 && wm != 4 { vm = 1; } |
| 242 | w[21] = vm; |
| 243 | // In "": $@ marks bit 1 and $* bit 2; a scalar reference marks bit 2 only when a value byte is copied (xval), |
| 244 | // so "$e$@" with no positionals gives no field (bash) while "$e" alone still gives one empty field. |
| 245 | if q == 2 { |
| 246 | if rk == 8 { w[15] = w[15] | 1; } |
| 247 | if rk == 9 { w[15] = w[15] | 2; } |
| 248 | } |
| 249 | if rk >= 8 { |
| 250 | let mut lk: u64 = 6; |
| 251 | if vm == 1 { lk = 5; } |
| 252 | if rk == 8 && q == 2 && wm != 2 && wm != 4 { lk = 7; } |
| 253 | w[22] = lk; |
| 254 | return request(w, 5); |
| 255 | } |
| 256 | if rk == 1 && wm == 2 { |
| 257 | let hit: u64 = find_prior(inp, w, w[9], w[10]); |
| 258 | if hit != 0 { |
| 259 | w[19] = 3264 + w[hit + 2]; |
| 260 | w[20] = 3264 + w[hit + 2] + w[hit + 3]; |
| 261 | w[46] = 1; |
| 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | return request(w, rk); |
| 266 | } |
| 267 | |
| 268 | // The host answered: STAGING holds the value (kind 1 to 4, or one positional of a list) or the positional count. |
| 269 | fn answer(w: cells) -> u64 { |
| 270 | let f: u64 = w[896]; |
| 271 | if f > 1 { return 102; } |
| 272 | if w[8] == 5 { |
| 273 | let np: u64 = w[896 + 2]; |
| 274 | w[24] = np; |
| 275 | w[23] = 1; |
| 276 | if np == 0 { |
| 277 | w[47] = w[18]; |
| 278 | w[46] = 0; |
| 279 | return 0; |
| 280 | } |
| 281 | w[9] = 1; |
| 282 | w[10] = 0; |
| 283 | return request(w, 3); |
| 284 | } |
| 285 | let mut ln: u64 = 0; |
| 286 | if f == 1 { ln = w[896 + 1]; } |
| 287 | if ln > 1024 { return werr(w, 211); } |
| 288 | w[19] = 896 + 4; |
| 289 | w[20] = 896 + 4 + ln; |
| 290 | w[46] = 1; |
| 291 | if w[22] == 7 { fopen_(w); } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | // The value was copied completely: next positional of a list (with its separator), or back to the raw bytes. |
| 296 | fn vdone(w: cells) -> u64 { |
| 297 | let rk: u64 = w[22]; |
| 298 | let k: u64 = w[23]; |
| 299 | if rk >= 5 && k < w[24] { |
| 300 | let mut r: u64 = 0; |
| 301 | if rk == 6 { |
| 302 | r = lit(w, 32); |
| 303 | } else { |
| 304 | r = fclose(w); |
| 305 | } |
| 306 | if r != 0 { return r; } |
| 307 | w[23] = k + 1; |
| 308 | w[9] = k + 1; |
| 309 | w[10] = 0; |
| 310 | return request(w, 3); |
| 311 | } |
| 312 | w[47] = w[18]; |
| 313 | w[46] = 0; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | // One value byte. |
| 318 | fn xval(w: cells) -> u64 { |
| 319 | let cur: u64 = w[19]; |
| 320 | if cur >= w[20] { return vdone(w); } |
| 321 | let c: u64 = w[cur]; |
| 322 | if c == 0 || c > 255 { return werr(w, 244); } |
| 323 | w[19] = cur + 1; |
| 324 | if w[21] == 1 { return sbyte(w, c); } |
| 325 | if w[12] == 2 { w[15] = w[15] | 2; } |
| 326 | return lit(w, c); |
| 327 | } |
| 328 | |
| 329 | // The word is complete: close its field, finish the assignment value, check the redirection target. |
| 330 | fn wend(w: cells) -> u64 { |
| 331 | let r: u64 = fclose(w); |
| 332 | if r != 0 { return r; } |
| 333 | let wm: u64 = w[45]; |
| 334 | if wm == 2 { |
| 335 | let e: u64 = cbase(w) + 4 + 64 + (w[cbase(w) + 1] - 1) * 4; |
| 336 | w[e + 3] = w[42] - w[e + 2]; |
| 337 | } |
| 338 | if wm == 3 && w[16] == 0 { return werr(w, 243); } |
| 339 | w[45] = 0; |
| 340 | w[32] = w[32] + 1; |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | // One raw byte of the word (or its end). 0 = go on, 1 = word complete, 102 = variable wanted, >= 200 = refusal. |
| 345 | fn xraw(inp: bytes, w: cells) -> u64 { |
| 346 | let tb: u64 = 64 + w[32] * 4; |
| 347 | let e: u64 = w[tb + 1] + w[tb + 2]; |
| 348 | let p: u64 = w[47]; |
| 349 | if p >= e { return wend(w); } |
| 350 | let c: u64 = inp[p]; |
| 351 | let q: u64 = w[12]; |
| 352 | w[47] = p + 1; |
| 353 | if q == 1 { |
| 354 | if c == 39 { |
| 355 | w[12] = 0; |
| 356 | fopen_(w); |
| 357 | return 0; |
| 358 | } |
| 359 | return lit(w, c); |
| 360 | } |
| 361 | if c == 92 && p + 1 < e { |
| 362 | let d: u64 = inp[p + 1]; |
| 363 | if d == 10 { |
| 364 | w[47] = p + 2; |
| 365 | return 0; |
| 366 | } |
| 367 | if q == 0 { |
| 368 | w[47] = p + 2; |
| 369 | return lit(w, d); |
| 370 | } |
| 371 | if d == 36 || d == 34 || d == 92 || d == 96 { |
| 372 | w[47] = p + 2; |
| 373 | w[15] = w[15] | 2; |
| 374 | return lit(w, d); |
| 375 | } |
| 376 | } |
| 377 | if q == 2 && c == 34 { |
| 378 | w[12] = 0; |
| 379 | let f: u64 = w[15]; |
| 380 | if (f & 2) != 0 || (f & 1) == 0 { fopen_(w); } |
| 381 | w[15] = 0; |
| 382 | return 0; |
| 383 | } |
| 384 | if q == 0 && c == 34 { |
| 385 | w[12] = 2; |
| 386 | w[15] = 0; |
| 387 | return 0; |
| 388 | } |
| 389 | if q == 0 && c == 39 { |
| 390 | w[12] = 1; |
| 391 | return 0; |
| 392 | } |
| 393 | if c == 36 && dollar(inp, w, p, e) == 1 { |
| 394 | w[47] = p; |
| 395 | return begin_ref(inp, w, q); |
| 396 | } |
| 397 | if q == 2 { w[15] = w[15] | 2; } |
| 398 | return lit(w, c); |
| 399 | } |
| 400 | |
| 401 | // Start a word: mode 1 argv, 2 assignment value, 3 redirection target, 4 argv word NAME=v of export (expanded as |
| 402 | // an assignment: no splitting, no glob refusal of values, $@ joined); raw cursor at pos. |
| 403 | fn wset(w: cells, mode: u64, pos: u64) { |
| 404 | w[45] = mode; |
| 405 | w[46] = 0; |
| 406 | w[47] = pos; |
| 407 | w[12] = 0; |
| 408 | w[13] = 0; |
| 409 | w[15] = 0; |
| 410 | w[16] = 0; |
| 411 | } |
| 412 | |
| 413 | // The assignment word at token tb: copy its name to OUT, record the entry, start its value. |
| 414 | fn start_assign(inp: bytes, w: cells, tb: u64) -> u64 { |
| 415 | let s: u64 = w[tb + 1]; |
| 416 | let aux: u64 = w[tb + 3]; |
| 417 | if aux <= s + 1 { return fail(w, 217, s); } |
| 418 | let nl: u64 = aux - 1 - s; |
| 419 | let cb: u64 = cbase(w); |
| 420 | let na: u64 = w[cb + 1]; |
| 421 | if na >= 16 { return fail(w, 207, s); } |
| 422 | let o: u64 = w[42]; |
| 423 | if o + nl > 8192 { return fail(w, 210, s); } |
| 424 | let mut i: u64 = 0; |
| 425 | while i < nl bound 4096 { |
| 426 | w[3264 + o + i] = inp[s + i]; |
| 427 | i = i + 1; |
| 428 | } |
| 429 | w[42] = o + nl; |
| 430 | let e: u64 = cb + 68 + na * 4; |
| 431 | w[e] = o; |
| 432 | w[e + 1] = nl; |
| 433 | w[e + 2] = o + nl; |
| 434 | w[e + 3] = 0; |
| 435 | w[cb + 1] = na + 1; |
| 436 | wset(w, 2, aux); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | // The redirection operator at token t (kind k): record the entry; N>&M is complete, others start the target word. |
| 441 | fn start_redir(inp: bytes, w: cells, t: u64, k: u64) -> u64 { |
| 442 | let tb: u64 = 64 + t * 4; |
| 443 | let s: u64 = w[tb + 1]; |
| 444 | let cb: u64 = cbase(w); |
| 445 | let r: u64 = w[cb + 2]; |
| 446 | if r >= 8 { return fail(w, 208, s); } |
| 447 | if t + 1 >= w[17] { return fail(w, 217, s); } |
| 448 | let ub: u64 = tb + 4; |
| 449 | if (w[ub] & 255) != 1 { return fail(w, 217, s); } |
| 450 | w[cb + 2] = r + 1; |
| 451 | let e: u64 = cb + 132 + r * 4; |
| 452 | let mut kind: u64 = 1; |
| 453 | if k == 7 { kind = 2; } |
| 454 | if k == 8 { kind = 3; } |
| 455 | if k == 10 || k == 11 { kind = 4; } |
| 456 | w[e] = kind; |
| 457 | w[e + 1] = w[tb + 3]; |
| 458 | if kind == 4 { |
| 459 | let d: u64 = inp[w[ub + 1]]; |
| 460 | if d < 48 || d > 50 { return fail(w, 217, s); } |
| 461 | w[e + 2] = d - 48; |
| 462 | w[32] = t + 2; |
| 463 | return 0; |
| 464 | } |
| 465 | wset(w, 3, w[ub + 1]); |
| 466 | w[32] = t + 1; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | // The first bytes of an OUT field packed big-endian; 0 for an empty field or one longer than 8 bytes. |
| 471 | fn pack8o(w: cells, off: u64, n: u64) -> u64 { |
| 472 | if n == 0 || n > 8 { return 0; } |
| 473 | let mut v: u64 = 0; |
| 474 | let mut i: u64 = 0; |
| 475 | while i < n bound 8 { |
| 476 | v = (v << 8) | w[3264 + off + i]; |
| 477 | i = i + 1; |
| 478 | } |
| 479 | return v; |
| 480 | } |
| 481 | |
| 482 | // 1 to 6 builtin id (cd pwd printf export unset exit), 99 unsupported builtin, 0 external. |
| 483 | fn classify(v: u64) -> u64 { |
| 484 | if v == 0x6364 { return 1; } |
| 485 | if v == 0x707764 { return 2; } |
| 486 | if v == 0x7072696e7466 { return 3; } |
| 487 | if v == 0x6578706f7274 { return 4; } |
| 488 | if v == 0x756e736574 { return 5; } |
| 489 | if v == 0x65786974 { return 6; } |
| 490 | if v == 0x736574 || v == 0x6576616c || v == 0x65786563 || v == 0x2e || v == 0x736f75726365 { return 99; } |
| 491 | if v == 0x74726170 || v == 0x7368696674 || v == 0x72656164 || v == 0x72657475726e { return 99; } |
| 492 | if v == 0x627265616b || v == 0x636f6e74696e7565 || v == 0x6c6f63616c { return 99; } |
| 493 | if v == 0x726561646f6e6c79 || v == 0x616c696173 || v == 0x77616974 { return 99; } |
| 494 | // more bash builtins: : declare typeset let command type umask hash ulimit getopts jobs pushd popd dirs history |
| 495 | // unalias fg bg builtin enable shopt mapfile times caller disown suspend logout bind help compgen complete fc compopt |
| 496 | if v == 0x3a || v == 0x6465636c617265 || v == 0x74797065736574 || v == 0x6c6574 || v == 0x636f6d6d616e64 { return 99; } |
| 497 | if v == 0x74797065 || v == 0x756d61736b || v == 0x68617368 || v == 0x756c696d6974 || v == 0x6765746f707473 { return 99; } |
| 498 | if v == 0x6a6f6273 || v == 0x7075736864 || v == 0x706f7064 || v == 0x64697273 || v == 0x686973746f7279 { return 99; } |
| 499 | if v == 0x756e616c696173 || v == 0x6667 || v == 0x6267 || v == 0x6275696c74696e || v == 0x656e61626c65 { return 99; } |
| 500 | if v == 0x73686f7074 || v == 0x6d617066696c65 || v == 0x74696d6573 || v == 0x63616c6c6572 || v == 0x6469736f776e { return 99; } |
| 501 | if v == 0x73757370656e64 || v == 0x6c6f676f7574 || v == 0x62696e64 || v == 0x68656c70 || v == 0x636f6d7067656e { return 99; } |
| 502 | if v == 0x636f6d706c657465 || v == 0x6663 || v == 0x636f6d706f7074 { return 99; } |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | // The pipeline is fully expanded: header, state, status. |
| 507 | fn pipe_ready(w: cells) -> u64 { |
| 508 | let pb: u64 = 832 + w[30] * 4; |
| 509 | w[1928] = 1330791761; |
| 510 | w[1928 + 1] = w[pb + 1]; |
| 511 | w[1928 + 2] = w[pb + 2]; |
| 512 | w[1928 + 3] = 0; |
| 513 | w[1928 + 4] = w[42]; |
| 514 | w[1928 + 5] = 1; |
| 515 | w[29] = 2; |
| 516 | return 103; |
| 517 | } |
| 518 | |
| 519 | // All tokens of the command are expanded: classify argv[0], go to the next command or finish the pipeline. |
| 520 | fn cmd_end(w: cells) -> u64 { |
| 521 | let cb: u64 = cbase(w); |
| 522 | if w[cb] > 0 { |
| 523 | let b: u64 = classify(pack8o(w, w[cb + 4], w[cb + 5])); |
| 524 | if b == 99 { return fail(w, 239, w[44]); } |
| 525 | // readarray is the one bash builtin longer than 8 bytes, so pack8o cannot carry it: matched here (#344) |
| 526 | let o9: u64 = w[cb + 4]; |
| 527 | if w[cb + 5] == 9 { |
| 528 | if pack8o(w, o9, 8) == 0x7265616461727261 { |
| 529 | if w[3264 + o9 + 8] == 0x79 { return fail(w, 239, w[44]); } |
| 530 | } |
| 531 | } |
| 532 | w[cb + 3] = b; |
| 533 | } |
| 534 | let pb: u64 = 832 + w[30] * 4; |
| 535 | let c: u64 = w[31] + 1; |
| 536 | if c >= w[pb + 1] { return pipe_ready(w); } |
| 537 | w[31] = c; |
| 538 | w[32] = w[576 + (w[pb] + c) * 8]; |
| 539 | w[43] = 0; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | // 1 when argv[0] of this command is the plain word `export` (raw bytes `export` then a blank, value `export`) and |
| 544 | // that word is the command's first word: bash expands its NAME=value arguments as assignments, so their values are |
| 545 | // not split (mode 4). An empty expansion before it (`e=; $e export A=$y`) leaves an ordinary command (omega#344). |
| 546 | fn is_export(inp: bytes, w: cells) -> u64 { |
| 547 | let cb: u64 = cbase(w); |
| 548 | if w[cb] == 0 { return 0; } |
| 549 | if pack8o(w, w[cb + 4], w[cb + 5]) != 0x6578706f7274 { return 0; } |
| 550 | let a: u64 = w[44]; |
| 551 | if w[43] != a + 1 { return 0; } |
| 552 | if a + 6 >= inp.len { return 0; } |
| 553 | let mut v: u64 = 0; |
| 554 | let mut i: u64 = 0; |
| 555 | while i < 6 bound 6 { |
| 556 | v = (v << 8) | inp[a + i]; |
| 557 | i = i + 1; |
| 558 | } |
| 559 | if v != 0x6578706f7274 { return 0; } |
| 560 | let d: u64 = inp[a + 6]; |
| 561 | if d == 32 || d == 9 { return 1; } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | // Between words: classify the next token of the command and start it. |
| 566 | fn next_task(inp: bytes, w: cells) -> u64 { |
| 567 | let pb: u64 = 832 + w[30] * 4; |
| 568 | let cmb: u64 = 576 + (w[pb] + w[31]) * 8; |
| 569 | let t: u64 = w[32]; |
| 570 | if t >= w[cmb + 1] { return cmd_end(w); } |
| 571 | let tb: u64 = 64 + t * 4; |
| 572 | let hdr: u64 = w[tb]; |
| 573 | let k: u64 = hdr & 255; |
| 574 | if k == 1 { |
| 575 | if ((hdr >> 8) & 16) != 0 && w[43] == 0 { return start_assign(inp, w, tb); } |
| 576 | let mut m: u64 = 1; |
| 577 | if ((hdr >> 8) & 16) != 0 && is_export(inp, w) == 1 { m = 4; } |
| 578 | if w[43] == 0 { w[43] = w[tb + 1] + 1; } |
| 579 | wset(w, m, w[tb + 1]); |
| 580 | return 0; |
| 581 | } |
| 582 | if k == 6 || k == 7 || k == 8 || k == 10 || k == 11 { return start_redir(inp, w, t, k); } |
| 583 | return fail(w, 217, w[tb + 1]); |
| 584 | } |
| 585 | |
| 586 | // Begin pipeline p: clear its request block and the per-pipeline state. |
| 587 | fn pipe_begin(w: cells, p: u64) { |
| 588 | let pb: u64 = 832 + p * 4; |
| 589 | let n: u64 = 8 + w[pb + 1] * 164; |
| 590 | let mut i: u64 = 0; |
| 591 | while i < n bound 1320 { |
| 592 | w[1928 + i] = 0; |
| 593 | i = i + 1; |
| 594 | } |
| 595 | w[31] = 0; |
| 596 | w[32] = w[576 + w[pb] * 8]; |
| 597 | w[33] = 0; |
| 598 | w[42] = 0; |
| 599 | w[43] = 0; |
| 600 | w[45] = 0; |
| 601 | w[46] = 0; |
| 602 | w[29] = 1; |
| 603 | } |
| 604 | |
| 605 | // Pick the next pipeline that runs (connector rules). 0 = begun, 104 = none left. |
| 606 | fn next_pipe(w: cells) -> u64 { |
| 607 | let np: u64 = w[37]; |
| 608 | let mut p: u64 = w[30]; |
| 609 | let mut go: u64 = 0; |
| 610 | while p < np && go == 0 bound 16 { |
| 611 | go = 1; |
| 612 | if p > 0 && w[25] == 0 { |
| 613 | let cn: u64 = w[832 + (p - 1) * 4 + 2]; |
| 614 | let ls: u64 = w[3]; |
| 615 | if cn == 2 && ls != 0 { go = 0; } |
| 616 | if cn == 3 && ls == 0 { go = 0; } |
| 617 | } |
| 618 | if go == 0 { p = p + 1; } |
| 619 | } |
| 620 | w[30] = p; |
| 621 | if p >= np { |
| 622 | w[29] = 3; |
| 623 | return 104; |
| 624 | } |
| 625 | pipe_begin(w, p); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | fn expand_run(inp: bytes, w: cells) -> u64 { |
| 630 | let ac: u64 = abi_check(w); |
| 631 | if ac != 0 { return ac; } |
| 632 | if w[2] == 255 { return w[4]; } |
| 633 | if w[34] != 4 { return 217; } |
| 634 | if w[41] == 0 { |
| 635 | for j in 29 .. 34 { |
| 636 | w[j as u64] = 0; |
| 637 | } |
| 638 | for j in 12 .. 27 { |
| 639 | if j != 17 && j != 25 { w[j as u64] = 0; } |
| 640 | } |
| 641 | w[41] = 1; |
| 642 | } |
| 643 | if w[29] > 3 || w[30] > w[37] { return 217; } |
| 644 | if w[29] == 3 { return 104; } |
| 645 | if w[29] == 2 { |
| 646 | w[30] = w[30] + 1; |
| 647 | w[29] = 0; |
| 648 | } |
| 649 | if w[29] == 0 { |
| 650 | let nr: u64 = next_pipe(w); |
| 651 | if nr != 0 { return nr; } |
| 652 | } |
| 653 | let mut n: u64 = 0; |
| 654 | let mut rc: u64 = 0; |
| 655 | while rc == 0 && n < 1024 bound 1024 { |
| 656 | if w[45] == 0 { |
| 657 | rc = next_task(inp, w); |
| 658 | } else { |
| 659 | let ph: u64 = w[46]; |
| 660 | if ph == 1 { |
| 661 | rc = xval(w); |
| 662 | } else { |
| 663 | if ph == 2 { |
| 664 | rc = answer(w); |
| 665 | } else { |
| 666 | rc = xraw(inp, w); |
| 667 | } |
| 668 | } |
| 669 | if rc == 1 { rc = 0; } |
| 670 | } |
| 671 | n = n + 1; |
| 672 | } |
| 673 | if rc != 0 { return rc; } |
| 674 | return 101; |
| 675 | } |