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_ref.c

430 lines · 16417 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* osh_expand_ref.c -- see osh_expand_ref.h. */
18#include "osh_expand_ref.h"
19
20#include <stdio.h>
21#include <string.h>
22
23#define CAP_FIELDS 32
24#define CAP_VALUE 1024
25#define CAP_VARREQ 64
26
27typedef struct {
28 const uint8_t *in;
29 const OshRefLex *lx;
30 const OshXrEnv *env;
31 OshXrOut *o;
32 unsigned status; /* 0 while fine */
33 uint64_t woff; /* start offset of the word being expanded */
34 int nvreq;
35 /* the command being built */
36 uint64_t *blk;
37 int mode; /* 1 argv, 2 assignment, 3 redirection target, 4 NAME=v argument of export (argv, not split) */
38 size_t fstart; /* start of the open field in out */
39 int exists; /* a field is open */
40 int nfields_word; /* redirection target fields */
41 uint64_t a0off;
42 int standalone; /* the command has no command word */
43 struct {
44 uint8_t name[256];
45 size_t nlen;
46 size_t voff, vlen;
47 } prior[16];
48 int nprior;
49} Cx;
50
51static void err(Cx *c, unsigned code)
52{
53 if (c->status) return;
54 c->status = code;
55 c->o->err_off = c->woff;
56}
57
58static void put(Cx *c, uint8_t b)
59{
60 if (c->status) return;
61 if (c->o->out_used >= OSH_XR_OUTCAP) { err(c, 210); return; }
62 if (!c->exists && c->mode != 2) { c->exists = 1; c->fstart = c->o->out_used; }
63 c->o->out[c->o->out_used++] = b;
64}
65
66static void push_field(Cx *c)
67{
68 if (c->status || c->mode == 2 || !c->exists) return;
69 c->exists = 0;
70 size_t len = c->o->out_used - c->fstart;
71 if (c->mode == 1 || c->mode == 4) {
72 uint64_t n = c->blk[0];
73 if (n >= CAP_FIELDS) { err(c, 209); return; }
74 if (n == 0) c->a0off = c->woff;
75 c->blk[4 + 2 * n] = c->fstart;
76 c->blk[5 + 2 * n] = len;
77 c->blk[0] = n + 1;
78 } else {
79 if (c->nfields_word) { err(c, 243); return; }
80 c->nfields_word = 1;
81 uint64_t r = c->blk[2] - 1;
82 c->blk[4 + 64 + 64 + 4 * r + 2] = c->fstart;
83 c->blk[4 + 64 + 64 + 4 * r + 3] = len;
84 }
85}
86
87static void mark(Cx *c)
88{
89 if (c->status || c->mode == 2) return;
90 if (!c->exists) { c->exists = 1; c->fstart = c->o->out_used; }
91}
92
93/* quoted or literal text: always part of the current field, even when empty */
94static void piece_text(Cx *c, const uint8_t *b, size_t n)
95{
96 mark(c);
97 for (size_t i = 0; i < n && !c->status; i++) {
98 if (b[i] == 0) { err(c, 244); return; }
99 put(c, b[i]);
100 }
101}
102
103/* the result of an unquoted expansion */
104static void piece_split(Cx *c, const uint8_t *b, size_t n)
105{
106 for (size_t i = 0; i < n && !c->status; i++) {
107 uint8_t ch = b[i];
108 if (ch == 0) err(c, 244);
109 else if (ch == 0x20 || ch == 9 || ch == 10) push_field(c);
110 else if (ch == '*' || ch == '?' || ch == '[') err(c, 245);
111 else put(c, ch);
112 }
113}
114
115static int is_nm(int ch, int first)
116{
117 return ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (!first && ch >= '0' && ch <= '9');
118}
119
120static void record_req(Cx *c, int kind, uint64_t a, uint64_t len)
121{
122 if (c->nvreq >= CAP_VARREQ) { err(c, 212); return; }
123 c->nvreq++;
124 if (c->o->nreq < OSH_XR_MAXREQ) {
125 c->o->req[c->o->nreq].kind = kind;
126 c->o->req[c->o->nreq].a = a;
127 c->o->req[c->o->nreq].len = len;
128 c->o->nreq++;
129 }
130}
131
132/* fetch a value through the host protocol; returns 0 ok, else sets the error */
133static int fetch(Cx *c, int kind, const uint8_t *name, size_t nlen, uint64_t a, uint64_t reqa, uint8_t *val, size_t *vlen, uint64_t *npos)
134{
135 record_req(c, kind, reqa, kind == 1 ? nlen : 0);
136 if (c->status) return -1;
137 int found;
138 osh_xr_env_get(c->env, kind, name, nlen, a, &found, vlen, npos, val);
139 if (kind != 5 && found && *vlen > CAP_VALUE) { err(c, 211); return -1; }
140 if (!found) *vlen = 0;
141 return 0;
142}
143
144/* a list reference: at = '@' or '*'; quoted = inside "" */
145static void expand_list(Cx *c, int at, int quoted)
146{
147 uint8_t val[OSH_XR_VALCAP];
148 size_t vl;
149 uint64_t np = 0;
150 if (fetch(c, 5, NULL, 0, 0, 0, val, &vl, &np)) return;
151 int join = c->mode == 2 || c->mode == 4 || (quoted && !at);
152 for (uint64_t k = 1; k <= np && !c->status; k++) {
153 if (fetch(c, 3, NULL, 0, k, k, val, &vl, &np)) return;
154 if (k > 1) {
155 if (join && quoted) piece_text(c, (const uint8_t *)" ", 1);
156 else if (join) put(c, ' ');
157 else if (quoted) push_field(c); /* "$@": a field per positional */
158 else piece_split(c, (const uint8_t *)" ", 1);
159 }
160 if (c->mode == 2 || c->mode == 4) piece_text(c, val, vl);
161 else if (quoted) piece_text(c, val, vl);
162 else piece_split(c, val, vl);
163 }
164}
165
166/* the $ at in[p]: handle the reference; returns the number of bytes it covers, or 0 when it is a literal dollar */
167static size_t expand_ref(Cx *c, size_t p, size_t e, int quoted)
168{
169 const uint8_t *in = c->in;
170 uint8_t val[OSH_XR_VALCAP];
171 size_t vl = 0;
172 uint64_t np = 0;
173 int kind = 0;
174 uint64_t k = 0;
175 const uint8_t *nm = NULL;
176 size_t nl = 0, used = 0;
177 if (p + 1 >= e) return 0;
178 int d = in[p + 1];
179 if (is_nm(d, 1)) {
180 size_t q = p + 1;
181 while (q < e && is_nm(in[q], 0)) q++;
182 kind = 1; nm = in + p + 1; nl = q - p - 1; used = q - p;
183 } else if (d >= '0' && d <= '9') {
184 kind = 3; k = (uint64_t)(d - '0'); used = 2;
185 } else if (d == '?') { kind = 2; used = 2; }
186 else if (d == '#') { kind = 4; used = 2; }
187 else if (d == '@' || d == '*') {
188 expand_list(c, d == '@', quoted);
189 return 2;
190 } else if (d == '{' && p + 3 < e) {
191 int d2 = in[p + 2];
192 if (d2 >= '0' && d2 <= '9') {
193 if (in[p + 3] != '}') return 0;
194 kind = 3; k = (uint64_t)(d2 - '0'); used = 4;
195 } else if (is_nm(d2, 1)) {
196 size_t q = p + 2;
197 while (q < e && is_nm(in[q], 0)) q++;
198 if (q >= e || in[q] != '}') return 0;
199 kind = 1; nm = in + p + 2; nl = q - p - 2; used = q - p + 1;
200 } else return 0;
201 } else return 0;
202 /* standalone assignments see earlier values of the same name */
203 if (kind == 1 && c->mode == 2 && c->standalone) {
204 for (int j = c->nprior - 1; j >= 0; j--)
205 if (c->prior[j].nlen == nl && memcmp(c->prior[j].name, nm, nl) == 0) {
206 const uint8_t *src = c->o->out + c->prior[j].voff;
207 size_t sl = c->prior[j].vlen;
208 if (c->mode == 2 || quoted) piece_text(c, src, sl);
209 return used;
210 }
211 }
212 if (fetch(c, kind, nm, nl, k, kind == 1 ? (uint64_t)(nm - in) : kind == 3 ? k : 0, val, &vl, &np)) return used;
213 /* an empty quoted value adds nothing by itself: the closing quote decides whether "" makes a field */
214 if (c->mode == 2 || c->mode == 4 || (quoted && vl)) piece_text(c, val, vl);
215 else if (!quoted) piece_split(c, val, vl);
216 return used;
217}
218
219/* expand the raw bytes in[s..e) as one word in the current mode */
220static void expand_word(Cx *c, size_t s, size_t e)
221{
222 const uint8_t *in = c->in;
223 size_t p = s;
224 int q = 0; /* 0 unquoted, 1 '', 2 "" */
225 int dq_other = 0, dq_at = 0;
226 while (p < e && !c->status) {
227 int ch = in[p];
228 if (q == 1) {
229 if (ch == '\'') { q = 0; mark(c); } else put(c, (uint8_t)ch);
230 p++;
231 continue;
232 }
233 if (ch == '\\' && p + 1 < e) {
234 int d = in[p + 1];
235 if (d == '\n') { p += 2; continue; }
236 if (q == 0 || d == '$' || d == '"' || d == '\\' || d == '`') {
237 put(c, (uint8_t)d);
238 if (q == 2) dq_other = 1;
239 p += 2;
240 continue;
241 }
242 }
243 if (q == 2 && ch == '"') {
244 if (dq_other || !dq_at) mark(c);
245 q = 0;
246 p++;
247 continue;
248 }
249 if (q == 0 && ch == '"') { q = 2; dq_other = dq_at = 0; p++; continue; }
250 if (q == 0 && ch == '\'') { q = 1; p++; continue; }
251 if (ch == '$') {
252 size_t before = c->o->out_used;
253 size_t used = expand_ref(c, p, e, q == 2);
254 if (used) {
255 /* in "": $@ is dq_at; $* always counts as text; a scalar counts only if its value was not empty,
256 * so "$e$@" with no positionals gives no field (bash 5.2) */
257 if (q == 2) {
258 if (in[p + 1] == '@') dq_at = 1;
259 else if (in[p + 1] == '*' || c->o->out_used > before) dq_other = 1;
260 }
261 p += used;
262 continue;
263 }
264 }
265 if (q == 2) dq_other = 1;
266 put(c, (uint8_t)ch);
267 p++;
268 }
269 push_field(c);
270}
271
272static uint64_t pack_name(const uint8_t *b, size_t n)
273{
274 if (n == 0 || n > 8) return 0;
275 uint64_t v = 0;
276 for (size_t i = 0; i < n; i++) v = (v << 8) | b[i];
277 return v;
278}
279
280static int classify(uint64_t v)
281{
282 static const struct { const char *n; int id; } t[] = {
283 {"cd", 1}, {"pwd", 2}, {"printf", 3}, {"export", 4}, {"unset", 5}, {"exit", 6},
284 {"set", 99}, {"eval", 99}, {"exec", 99}, {".", 99}, {"source", 99}, {"trap", 99}, {"shift", 99}, {"read", 99},
285 {"return", 99}, {"break", 99}, {"continue", 99}, {"local", 99}, {"readonly", 99}, {"alias", 99}, {"wait", 99},
286 {":", 99}, {"declare", 99}, {"typeset", 99}, {"let", 99}, {"command", 99}, {"type", 99}, {"umask", 99},
287 {"hash", 99}, {"ulimit", 99}, {"getopts", 99}, {"jobs", 99}, {"pushd", 99}, {"popd", 99}, {"dirs", 99},
288 {"history", 99}, {"unalias", 99}, {"fg", 99}, {"bg", 99}, {"builtin", 99}, {"enable", 99}, {"shopt", 99},
289 {"mapfile", 99}, {"times", 99}, {"caller", 99}, {"disown", 99}, {"suspend", 99}, {"logout", 99},
290 {"bind", 99}, {"help", 99}, {"compgen", 99}, {"complete", 99}, {"fc", 99}, {"compopt", 99},
291 };
292 for (size_t i = 0; i < sizeof t / sizeof t[0]; i++)
293 if (pack_name((const uint8_t *)t[i].n, strlen(t[i].n)) == v) return t[i].id;
294 return 0;
295}
296
297unsigned osh_xr_next(const OshRefParse *pr, unsigned cand, uint64_t last_status, int noskip)
298{
299 unsigned p = cand;
300 while (p < pr->npipe && p > 0 && !noskip) {
301 uint64_t cn = pr->pipe[p - 1][2];
302 if ((cn == 2 && last_status != 0) || (cn == 3 && last_status == 0)) p++;
303 else break;
304 }
305 return p < pr->npipe ? p : pr->npipe;
306}
307
308void osh_xr_pipe(const uint8_t *in, size_t n, const OshRefLex *lx, const OshRefParse *pr, const OshXrEnv *env, unsigned p, OshXrOut *out)
309{
310 (void)n;
311 Cx c;
312 memset(&c, 0, sizeof c);
313 memset(out, 0, sizeof *out);
314 c.in = in; c.lx = lx; c.env = env; c.o = out;
315 unsigned first = (unsigned)pr->pipe[p][0], nc = (unsigned)pr->pipe[p][1];
316 out->nrec = 8 + (size_t)nc * 164;
317 for (unsigned ci = 0; ci < nc && !c.status; ci++) {
318 c.blk = out->rec + 8 + (size_t)ci * 164;
319 c.nprior = 0;
320 c.standalone = pr->cmd[first + ci][3] == 0;
321 unsigned t = (unsigned)pr->cmd[first + ci][0], te = (unsigned)pr->cmd[first + ci][1];
322 int anyw = 0;
323 while (t < te && !c.status) {
324 const OshRefTok *tk = &lx->tok[t];
325 c.woff = tk->start;
326 c.exists = 0;
327 c.nfields_word = 0;
328 if (tk->kind == 1) {
329 if ((tk->flags & 16) && !anyw) {
330 /* assignment: name copied to out, then the value */
331 uint64_t na = c.blk[1];
332 size_t nl = tk->aux - 1 - tk->start;
333 if (out->out_used + nl > OSH_XR_OUTCAP) { err(&c, 210); break; }
334 size_t noff = out->out_used;
335 memcpy(out->out + noff, in + tk->start, nl);
336 out->out_used += nl;
337 uint64_t *e = c.blk + 4 + 64 + 4 * na;
338 e[0] = noff; e[1] = nl; e[2] = out->out_used;
339 c.blk[1] = na + 1;
340 c.mode = 2;
341 expand_word(&c, tk->aux, tk->start + tk->len);
342 e[3] = out->out_used - e[2];
343 if (c.nprior < 16) {
344 memcpy(c.prior[c.nprior].name, in + tk->start, nl < 256 ? nl : 256);
345 c.prior[c.nprior].nlen = nl;
346 c.prior[c.nprior].voff = e[2];
347 c.prior[c.nprior].vlen = e[3];
348 c.nprior++;
349 }
350 } else {
351 c.mode = 1;
352 /* export NAME=v: bash expands the argument as an assignment (no splitting) when the command word
353 * is the plain word export and is the command's first word (omega#344: `$e export` is not) */
354 if ((tk->flags & 16) && anyw && (uint64_t)(anyw - 1) == c.a0off && c.blk[0] > 0 && c.blk[5] == 6 &&
355 memcmp(out->out + c.blk[4], "export", 6) == 0 && c.a0off + 6 < n &&
356 memcmp(in + c.a0off, "export", 6) == 0 && (in[c.a0off + 6] == ' ' || in[c.a0off + 6] == '\t'))
357 c.mode = 4;
358 if (!anyw) anyw = (int)tk->start + 1;
359 expand_word(&c, tk->start, tk->start + tk->len);
360 }
361 t++;
362 } else {
363 uint64_t r = c.blk[2];
364 uint64_t *e = c.blk + 4 + 64 + 64 + 4 * r;
365 c.blk[2] = r + 1;
366 e[0] = tk->kind == 6 ? 1 : tk->kind == 7 ? 2 : tk->kind == 8 ? 3 : 4;
367 e[1] = tk->aux;
368 const OshRefTok *tg = &lx->tok[t + 1];
369 if (e[0] == 4) {
370 e[2] = (uint64_t)(in[tg->start] - '0');
371 } else {
372 c.woff = tg->start;
373 c.mode = 3;
374 expand_word(&c, tg->start, tg->start + tg->len);
375 if (!c.status && c.nfields_word == 0) err(&c, 243);
376 }
377 t += 2;
378 }
379 }
380 if (!c.status && c.blk[0] > 0) {
381 uint64_t a0o = c.blk[4], a0l = c.blk[5];
382 int b = classify(pack_name(out->out + a0o, a0l));
383 /* readarray: the one bash builtin longer than 8 bytes; pack_name cannot carry it (mirrors cmd_end, #344) */
384 if (a0l == 9 && memcmp(out->out + a0o, "readarray", 9) == 0) b = 99;
385 if (b == 99) { c.woff = c.a0off; err(&c, 239); }
386 else c.blk[3] = (uint64_t)b;
387 }
388 }
389 if (c.status) { out->status = c.status; return; }
390 out->rec[0] = 0x4F524551;
391 out->rec[1] = nc;
392 out->rec[2] = pr->pipe[p][2];
393 out->rec[4] = out->out_used;
394 out->rec[5] = 1;
395 out->status = 103;
396}
397
398static void set_dec(OshXrStr *s, uint64_t v)
399{
400 char t[24];
401 int n = snprintf(t, sizeof t, "%llu", (unsigned long long)v);
402 s->set = 1;
403 s->len = (size_t)n;
404 memcpy(s->b, t, (size_t)n);
405}
406
407void osh_xr_env_get(const OshXrEnv *env, int kind, const uint8_t *name, size_t nlen, uint64_t k, int *found, size_t *len, uint64_t *npos, uint8_t *val)
408{
409 OshXrStr tmp;
410 const OshXrStr *s = NULL;
411 memset(&tmp, 0, sizeof tmp);
412 *npos = (uint64_t)env->npos;
413 *len = 0;
414 *found = 0;
415 if (kind == 5) { *found = 1; return; }
416 if (kind == 1) {
417 for (int i = 0; i < env->nvars; i++)
418 if (strlen(env->var[i].name) == nlen && memcmp(env->var[i].name, name, nlen) == 0) s = &env->var[i].v;
419 } else if (kind == 2) { set_dec(&tmp, env->status); s = &tmp; }
420 else if (kind == 4) { set_dec(&tmp, (uint64_t)env->npos); s = &tmp; }
421 else if (kind == 3) {
422 if (k == 0) s = &env->arg0;
423 else if (k <= (uint64_t)env->npos) s = &env->pos[k - 1];
424 }
425 if (s && s->set) {
426 *found = 1;
427 *len = s->len;
428 memcpy(val, s->b, s->len);
429 }
430}