interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / osh / osh_expand.osc.in

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