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 / host / osh_shell.c
| 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_shell.c -- see osh_shell.h. */ |
| 18 | #include "osh_shell.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/select.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "osh_layout.h" |
| 29 | |
| 30 | #define WS ((size_t)OSH_WS_CELLS) |
| 31 | enum { ST_MORE = 100, ST_BUDGET = 101, ST_NEED_VAR = 102, ST_PIPE = 103, ST_COMPLETE = 104 }; |
| 32 | #define BUF_MAX ((size_t)1 << 20) |
| 33 | |
| 34 | typedef struct { |
| 35 | const char *str; |
| 36 | size_t len, pos; |
| 37 | int fd; |
| 38 | int exact_end; /* a -c string: a last line with no newline reaches the core as is */ |
| 39 | } LineSrc; |
| 40 | |
| 41 | int osh_shell_init(OshShell *sh, char *const *envp, int native, int interactive, const char *arg0, char **pos, int npos) |
| 42 | { |
| 43 | memset(sh, 0, sizeof *sh); |
| 44 | sh->native = native; |
| 45 | sh->interactive = interactive; |
| 46 | sh->arg0 = (char *)arg0; |
| 47 | sh->pos = pos; |
| 48 | sh->npos = npos; |
| 49 | const char *src[3] = {(const char *)osh_unit_lex, (const char *)osh_unit_parse, (const char *)osh_unit_expand}; |
| 50 | size_t len[3] = {osh_unit_lex_len, osh_unit_parse_len, osh_unit_expand_len}; |
| 51 | char err[300]; |
| 52 | if (osh_core_init(&sh->core, src, len, err, sizeof err) != 0) { |
| 53 | fprintf(stderr, "osh: cannot load the shell core: %s\n", err); |
| 54 | return -1; |
| 55 | } |
| 56 | sh->w = calloc(WS, sizeof(uint64_t)); |
| 57 | if (!sh->w || osh_session_init(&sh->s, envp) != 0) { |
| 58 | fprintf(stderr, "osh: out of memory\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | /* A Linux host has no capability authority (osh_exec.c): the embedder supplies the binding. The osh program is its own |
| 62 | * embedder on Linux and grants itself the hosted-authority binding (domain 2). */ |
| 63 | sh->s.binding.valid = 1; |
| 64 | sh->s.binding.domain = 2; |
| 65 | sh->s.binding.cap_generation = 1; |
| 66 | if (interactive) { |
| 67 | sh->s.interactive = 1; |
| 68 | sh->s.tty_fd = 0; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void osh_shell_free(OshShell *sh) |
| 74 | { |
| 75 | osh_session_free(&sh->s); |
| 76 | osh_core_free(&sh->core); |
| 77 | free(sh->w); |
| 78 | free(sh->buf); |
| 79 | } |
| 80 | |
| 81 | static void reset_list(OshShell *sh) |
| 82 | { |
| 83 | memset(sh->w, 0, WS * sizeof(uint64_t)); |
| 84 | sh->w[OSH_S_MAGIC] = OSH_ABI_MAGIC; |
| 85 | sh->w[OSH_S_VERSION] = OSH_ABI_VERSION; |
| 86 | sh->w[OSH_S_WS_CELLS] = OSH_WS_CELLS; |
| 87 | sh->blen = 0; |
| 88 | } |
| 89 | |
| 90 | static uint64_t call(OshShell *sh, int unit) |
| 91 | { |
| 92 | return osh_core_call(&sh->core, unit, sh->native, sh->buf, sh->blen, sh->w, WS); |
| 93 | } |
| 94 | |
| 95 | static uint64_t run_entry(OshShell *sh, int unit) |
| 96 | { |
| 97 | for (int i = 0; i < 100000; i++) { |
| 98 | uint64_t st = call(sh, unit); |
| 99 | if (st != ST_BUDGET) return st; |
| 100 | } |
| 101 | return OSH_CORE_FAULT; |
| 102 | } |
| 103 | |
| 104 | /* lexer until it reports done, then the parser. 0: list parsed; 100: needs more input; >= 200: refused. */ |
| 105 | static uint64_t lex_parse(OshShell *sh) |
| 106 | { |
| 107 | uint64_t st = run_entry(sh, OSH_U_LEX); |
| 108 | if (st != 0) return st; |
| 109 | return run_entry(sh, OSH_U_PARSE); |
| 110 | } |
| 111 | |
| 112 | static int append(OshShell *sh, const uint8_t *p, size_t n) |
| 113 | { |
| 114 | if (sh->blen + n > BUF_MAX) return -1; |
| 115 | if (sh->blen + n > sh->bcap) { |
| 116 | size_t nc = sh->bcap ? sh->bcap * 2 : 4096; |
| 117 | while (nc < sh->blen + n) nc *= 2; |
| 118 | uint8_t *nb = realloc(sh->buf, nc); |
| 119 | if (!nb) return -1; |
| 120 | sh->buf = nb; |
| 121 | sh->bcap = nc; |
| 122 | } |
| 123 | memcpy(sh->buf + sh->blen, p, n); |
| 124 | sh->blen += n; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* Interactive only: ^C at the prompt abandons the line being typed (bash prints a new prompt, $? = 130); the shell |
| 129 | * itself ignores ^\ ^Z and background tty signals. osh_exec installs its own handling around each pipeline and puts |
| 130 | * these back afterwards. While osh reads and parses input SIGINT stays blocked and is only let in by the pselect |
| 131 | * that waits for the next byte, so a ^C that lands between two reads (while the core lexes or parses) is pending |
| 132 | * and wakes that pselect instead of being lost or noticed one line late (omega#344). It is unblocked again while a |
| 133 | * list runs. The embedder's dispositions and mask are saved on entry and restored when run_src returns. */ |
| 134 | static volatile sig_atomic_t g_prompt_int; |
| 135 | static sigset_t g_prompt_wait; /* the mask pselect waits with: the entry mask without SIGINT */ |
| 136 | |
| 137 | static void on_prompt_int(int sig) |
| 138 | { |
| 139 | (void)sig; |
| 140 | g_prompt_int = 1; |
| 141 | } |
| 142 | |
| 143 | static const int k_prompt_sigs[] = {SIGINT, SIGQUIT, SIGTSTP, SIGTTIN, SIGTTOU}; |
| 144 | #define NPROMPT ((int)(sizeof k_prompt_sigs / sizeof k_prompt_sigs[0])) |
| 145 | |
| 146 | typedef struct { |
| 147 | struct sigaction old[NPROMPT]; |
| 148 | sigset_t mask, blocked; /* the entry mask; the entry mask plus SIGINT */ |
| 149 | } PromptSigs; |
| 150 | |
| 151 | static void prompt_signals(PromptSigs *ps) |
| 152 | { |
| 153 | struct sigaction sa; |
| 154 | memset(&sa, 0, sizeof sa); |
| 155 | sigemptyset(&sa.sa_mask); |
| 156 | sigprocmask(SIG_SETMASK, NULL, &ps->mask); |
| 157 | ps->blocked = ps->mask; |
| 158 | sigaddset(&ps->blocked, SIGINT); |
| 159 | g_prompt_wait = ps->mask; /* without SIGINT unless the embedder itself blocks it, which then stays a block */ |
| 160 | for (int i = 0; i < NPROMPT; i++) { |
| 161 | sa.sa_handler = k_prompt_sigs[i] == SIGINT ? on_prompt_int : SIG_IGN; |
| 162 | sigaction(k_prompt_sigs[i], &sa, &ps->old[i]); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static void prompt_restore(const PromptSigs *ps) |
| 167 | { |
| 168 | for (int i = 0; i < NPROMPT; i++) sigaction(k_prompt_sigs[i], &ps->old[i], NULL); |
| 169 | sigprocmask(SIG_SETMASK, &ps->mask, NULL); |
| 170 | } |
| 171 | |
| 172 | /* Append one line (with its newline) to the list buffer. A last line with no newline gets one, except |
| 173 | * from a -c string, where it is appended as is so the core sees the true end of input: bash -c 'echo a\' |
| 174 | * prints a\, but the same text as a script or on stdin prints a. |
| 175 | * 1 got a line, 2 got a last -c line with no newline, 0 end of input with nothing read, |
| 176 | * -1 error (too long / read error), -2 interrupted by ^C at an interactive prompt. */ |
| 177 | static int read_line(OshShell *sh, LineSrc *src) |
| 178 | { |
| 179 | size_t got = 0; |
| 180 | if (src->fd < 0) { |
| 181 | if (src->pos >= src->len) return 0; |
| 182 | const char *s = src->str + src->pos; |
| 183 | size_t n = 0; |
| 184 | while (src->pos + n < src->len && s[n] != '\n') n++; |
| 185 | int nl = src->pos + n < src->len; |
| 186 | if (append(sh, (const uint8_t *)s, n) != 0) return -1; |
| 187 | int add_nl = nl || !src->exact_end; |
| 188 | if (add_nl && append(sh, (const uint8_t *)"\n", 1) != 0) return -1; |
| 189 | src->pos += n + (size_t)nl; |
| 190 | return add_nl ? 1 : 2; |
| 191 | } |
| 192 | for (;;) { |
| 193 | uint8_t c; |
| 194 | if (sh->interactive) { /* SIGINT is blocked here; only this wait lets it in (see prompt_signals) */ |
| 195 | if (src->fd >= FD_SETSIZE) return -1; |
| 196 | fd_set rf; |
| 197 | FD_ZERO(&rf); |
| 198 | FD_SET(src->fd, &rf); |
| 199 | if (pselect(src->fd + 1, &rf, NULL, NULL, NULL, &g_prompt_wait) < 0) { |
| 200 | if (errno != EINTR) return -1; |
| 201 | if (g_prompt_int) return -2; |
| 202 | continue; |
| 203 | } |
| 204 | } |
| 205 | ssize_t r = read(src->fd, &c, 1); |
| 206 | if (r < 0 && errno == EINTR) { |
| 207 | if (g_prompt_int) return -2; |
| 208 | continue; |
| 209 | } |
| 210 | if (r <= 0) { |
| 211 | if (got == 0) return 0; |
| 212 | c = '\n'; |
| 213 | return append(sh, &c, 1) == 0 ? 1 : -1; |
| 214 | } |
| 215 | got++; |
| 216 | if (append(sh, &c, 1) != 0) return -1; |
| 217 | if (c == '\n') return 1; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static size_t dec(uint64_t v, uint8_t *o) |
| 222 | { |
| 223 | uint8_t t[24]; |
| 224 | size_t n = 0; |
| 225 | do { t[n++] = (uint8_t)('0' + v % 10); v /= 10; } while (v); |
| 226 | for (size_t i = 0; i < n; i++) o[i] = t[n - 1 - i]; |
| 227 | return n; |
| 228 | } |
| 229 | |
| 230 | /* answer one NEED_VAR round trip (ABI draft: STAGING = {found, len, npos, 0, bytes one per cell}) */ |
| 231 | static void answer(OshShell *sh) |
| 232 | { |
| 233 | uint64_t *w = sh->w; |
| 234 | uint64_t kind = w[OSH_S_VR_KIND], a = w[OSH_S_VR_A], len = w[OSH_S_VR_LEN]; |
| 235 | const uint8_t *val = NULL; |
| 236 | uint8_t num[24]; |
| 237 | size_t vl = 0; |
| 238 | int found = 0; |
| 239 | switch (kind) { |
| 240 | case 1: { |
| 241 | char name[256]; |
| 242 | if (a <= sh->blen && len < sizeof name && len <= sh->blen - a) { |
| 243 | memcpy(name, sh->buf + a, (size_t)len); |
| 244 | name[len] = 0; |
| 245 | const char *v = osh_var_get(&sh->s, name); |
| 246 | if (v) { found = 1; val = (const uint8_t *)v; vl = strlen(v); } |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case 2: found = 1; vl = dec((uint64_t)(unsigned)sh->s.last_status, num); val = num; break; |
| 251 | case 3: |
| 252 | if (a == 0 && sh->arg0) { found = 1; val = (const uint8_t *)sh->arg0; vl = strlen(sh->arg0); } |
| 253 | else if (a >= 1 && a <= (uint64_t)sh->npos) { found = 1; val = (const uint8_t *)sh->pos[a - 1]; vl = strlen(sh->pos[a - 1]); } |
| 254 | break; |
| 255 | case 4: found = 1; vl = dec((uint64_t)sh->npos, num); val = num; break; |
| 256 | default: break; |
| 257 | } |
| 258 | w[OSH_STAGING + 0] = (uint64_t)found; |
| 259 | w[OSH_STAGING + 1] = vl; |
| 260 | w[OSH_STAGING + 2] = (uint64_t)sh->npos; |
| 261 | w[OSH_STAGING + 3] = 0; |
| 262 | for (size_t i = 0; i < vl && i < 1024; i++) w[OSH_STAGING + 4 + i] = val[i]; /* longer: the unit refuses by len */ |
| 263 | } |
| 264 | |
| 265 | static void refusal(OshShell *sh, uint64_t code, int syntax) |
| 266 | { |
| 267 | const char *nm = osh_code_name((unsigned)code); |
| 268 | char tmp[16]; |
| 269 | if (!nm) { snprintf(tmp, sizeof tmp, "E%llu", (unsigned long long)code); nm = tmp; } |
| 270 | fprintf(stderr, "osh: %s: %s at byte %llu\n", syntax ? "syntax error" : "refused", nm, (unsigned long long)sh->w[OSH_S_ERR_OFF]); |
| 271 | sh->s.last_status = 2; |
| 272 | sh->refused++; |
| 273 | } |
| 274 | |
| 275 | /* expand and run the parsed list. Returns 1 when the shell must stop (exit builtin, interrupted, fault), 2 when |
| 276 | * `exit` with too many arguments dropped the rest of the list. */ |
| 277 | static int run_list(OshShell *sh) |
| 278 | { |
| 279 | uint64_t *w = sh->w; |
| 280 | w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)sh->s.last_status; |
| 281 | for (int guard = 0; guard < 1000000; guard++) { |
| 282 | uint64_t st = osh_core_call(&sh->core, OSH_U_EXPAND, sh->native, sh->buf, sh->blen, w, WS); |
| 283 | switch (st) { |
| 284 | case ST_BUDGET: continue; |
| 285 | case ST_NEED_VAR: answer(sh); continue; |
| 286 | case ST_PIPE: { |
| 287 | uint64_t nc = w[OSH_REQUEST + 1]; |
| 288 | OshResult res; |
| 289 | osh_exec_record(&sh->s, w + OSH_REQUEST, (size_t)(OSH_REQ_HDR_CELLS + nc * OSH_CMD_CELLS), w + OSH_OUT, (size_t)w[OSH_REQUEST + 4], &res); |
| 290 | w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)sh->s.last_status; |
| 291 | if (sh->s.exit_requested) return 1; |
| 292 | if (res.killed_by_int) return 0; |
| 293 | if (sh->s.abort_list) { sh->s.abort_list = 0; return 2; } |
| 294 | continue; |
| 295 | } |
| 296 | case ST_COMPLETE: return 0; |
| 297 | default: |
| 298 | if (st >= 200 && st != OSH_CORE_FAULT) { refusal(sh, st, 0); return !sh->interactive; } |
| 299 | fprintf(stderr, "osh: internal error (unit status %llu)\n", (unsigned long long)st); |
| 300 | sh->s.last_status = 70; |
| 301 | return 1; |
| 302 | } |
| 303 | } |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | static int run_loop(OshShell *sh, LineSrc *src, const PromptSigs *ps) |
| 308 | { |
| 309 | for (;;) { |
| 310 | reset_list(sh); |
| 311 | g_prompt_int = 0; |
| 312 | if (sh->interactive) { fputs("osh$ ", stderr); fflush(stderr); } |
| 313 | uint64_t st = 0; |
| 314 | int eof = 0, intr = 0; |
| 315 | for (;;) { |
| 316 | int r = read_line(sh, src); |
| 317 | if (r == -2) { intr = 1; break; } |
| 318 | if (r < 0) { fprintf(stderr, "osh: input too long or unreadable\n"); sh->s.last_status = 2; return sh->interactive ? sh->s.last_status : 2; } |
| 319 | if (r == 0 && sh->blen == 0) return sh->s.last_status; |
| 320 | if (r == 0 || r == 2) { |
| 321 | eof = 1; |
| 322 | sh->w[OSH_S_EOI] = 1; |
| 323 | } |
| 324 | st = lex_parse(sh); |
| 325 | if (st == 0 && sh->w[OSH_PX_STATE] != 4) st = eof ? ST_COMPLETE : ST_MORE; /* only blank so far */ |
| 326 | if (st == ST_MORE && !eof) { |
| 327 | if (sh->interactive) { fputs("> ", stderr); fflush(stderr); } |
| 328 | continue; |
| 329 | } |
| 330 | break; |
| 331 | } |
| 332 | if (intr) { |
| 333 | fputc('\n', stderr); |
| 334 | sh->s.last_status = 130; |
| 335 | continue; |
| 336 | } |
| 337 | if (st == ST_COMPLETE) { |
| 338 | /* an empty list at end of input: nothing to run */ |
| 339 | } else if (st == 0) { |
| 340 | sh->lists++; |
| 341 | if (ps) sigprocmask(SIG_SETMASK, &ps->mask, NULL); /* children and osh_exec see the entry mask */ |
| 342 | int rl = run_list(sh); |
| 343 | if (ps) sigprocmask(SIG_SETMASK, &ps->blocked, NULL); |
| 344 | if (rl == 1) return sh->s.exit_requested ? sh->s.exit_status : sh->s.last_status; |
| 345 | /* bash: in a -c string the failed exit discards the whole rest of the string (status 1); a script file, |
| 346 | * stdin and the interactive prompt go on with the next line (omega#344) */ |
| 347 | if (rl == 2 && src->exact_end) return sh->s.last_status; |
| 348 | } else { |
| 349 | if (st >= 200 && st != OSH_CORE_FAULT) refusal(sh, st, 1); |
| 350 | else { fprintf(stderr, "osh: syntax error: unexpected end of input\n"); sh->s.last_status = 2; } |
| 351 | if (!sh->interactive) return 2; |
| 352 | } |
| 353 | if (eof) return sh->s.last_status; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static int run_src(OshShell *sh, LineSrc *src) |
| 358 | { |
| 359 | if (!sh->interactive) return run_loop(sh, src, NULL); |
| 360 | PromptSigs ps; |
| 361 | prompt_signals(&ps); |
| 362 | sigprocmask(SIG_SETMASK, &ps.blocked, NULL); |
| 363 | int rc = run_loop(sh, src, &ps); |
| 364 | prompt_restore(&ps); |
| 365 | return rc; |
| 366 | } |
| 367 | |
| 368 | int osh_shell_run_string(OshShell *sh, const char *text, size_t len) |
| 369 | { |
| 370 | LineSrc src = {text, len, 0, -1, 1}; |
| 371 | return run_src(sh, &src); |
| 372 | } |
| 373 | |
| 374 | int osh_shell_run_script(OshShell *sh, const char *text, size_t len) |
| 375 | { |
| 376 | LineSrc src = {text, len, 0, -1, 0}; |
| 377 | return run_src(sh, &src); |
| 378 | } |
| 379 | |
| 380 | int osh_shell_run_fd(OshShell *sh, int fd) |
| 381 | { |
| 382 | LineSrc src = {NULL, 0, 0, fd, 0}; |
| 383 | return run_src(sh, &src); |
| 384 | } |