From d43d19b44e56bf57d49f87c184a23551dfe215be Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 30 Oct 2026 09:59:17 -0500 Subject: [PATCH 1/2] osh: bash-differential follow-ups (refs #344) - -c 'exit 1 2; ...' ends the -c string like bash (status 1); script/stdin/prompt unchanged - $e export A=$y: export is assignment-mode only as the first word (expander + C ref) - children get _=, over inherited/assigned _ - pwd/cd after a removed cwd reached via symlink: logical PWD fallback, bash getcwd message - builtin diagnostics in bash 5.2 wording: cd/pwd/printf/unset invalid option + usage, pwd -LP, pwd operands, printf usage, export backtick identifier - interactive prompt: SIGINT blocked except in pselect wait (no lost ^C between flag reset and read); prompt signal dispositions and mask restored when osh_shell_run_* returns - tests/osh/bashdiff: 28-case harness vs /usr/bin/bash with negative control; make osh-bash-diff - bash_verify.sh pinned to env -i LC_ALL=C TZ=UTC PATH=/usr/bin:/bin --- .crumb | 24 +++--- mk/.crumb | 8 +- mk/osh.mk | 8 +- src/.crumb | 12 +-- src/osh/.crumb | 14 +-- src/osh/host/.crumb | 8 +- src/osh/host/osh_builtin.c | 53 +++++++++--- src/osh/host/osh_exec.c | 2 +- src/osh/host/osh_host.h | 2 +- src/osh/host/osh_shell.c | 76 ++++++++++++++--- src/osh/host/osh_vars.c | 40 +++++---- src/osh/osh_expand.osc | 8 +- src/osh/osh_expand.osc.in | 8 +- src/osh/osh_expand_ref.c | 6 +- src/osh/osh_layout.lst | 2 +- tests/.crumb | 12 +-- tests/osh/.crumb | 18 ++-- tests/osh/bashdiff/bashdiff.sh | 128 ++++++++++++++++++++++++++++ tests/osh/bashdiff/cases.txt | 76 +++++++++++++++++ tests/osh/host/.crumb | 8 +- tests/osh/host/test_osh_host.c | 19 +++-- tests/osh/vectors/README.md | 2 +- tests/osh/vectors/bash_verified.log | 3 +- tests/osh/vectors/bash_verify.sh | 6 +- 24 files changed, 425 insertions(+), 118 deletions(-) create mode 100755 tests/osh/bashdiff/bashdiff.sh create mode 100644 tests/osh/bashdiff/cases.txt diff --git a/.crumb b/.crumb index f3e51fe46..1348ac779 100644 --- a/.crumb +++ b/.crumb @@ -59,7 +59,7 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "8f0e5cb325040247440527f6b54b65cf72c47ff5adca75b4f32a1ddf2b2d6ade", + "digest": "177b531107c9f65b903af316a14356d946155ca5967eb1f54539b3191b2a3701", "last_commit": "1f320caad", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "bdfaf2a0933b765205e4911249e7bdfb2503920525135bd991c85cc4f8c066c3", + "last_commit": "089e5b053", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "e9f9bfdd80e39e62ac58004ecc90d27bc3a6f127b05a81420451ccda4fcb3a8e", + "last_commit": "62d1d0d66", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:a852b0b77586a9765794f5aba537200796760b4b611bb9d7767ecbccf41d9ca7", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:19eec38e8df61580f796493ce198f21f1ed9cb32f4aadd0df8f59d455b3259ba", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:87e2ba7fd03858c9b1675b0163c114c64860bc0006bc3159b8dc1de10ee353de" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index e38efa48f..103310cf9 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-28T03:06:12Z", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:8f0e5cb325040247440527f6b54b65cf72c47ff5adca75b4f32a1ddf2b2d6ade", + "digest": "sha256:177b531107c9f65b903af316a14356d946155ca5967eb1f54539b3191b2a3701", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 75, - "generated_at_commit": "79b8c1f9c24195c53ac8dad111d35175a4431506", - "source_tree": "sha256:b8302c1e15664dfe98c550a03830f5d4cf75c5451a6c75eed5042e623dea5d10" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:08dc821605cb16770d8116bbe9a75fc661a0668ecf61517b84510e40710827a1" }, "owner_lane": "", "provenance": { diff --git a/mk/osh.mk b/mk/osh.mk index ab6f6697e..af2b4d3c0 100644 --- a/mk/osh.mk +++ b/mk/osh.mk @@ -9,7 +9,7 @@ # make test-osh-parse check + parser driver (lexer + parser units, reference tokenizer + parser), plain and ASan/UBSan ifndef OSH_MK OSH_MK := 1 -.PHONY: osh-gen osh-gen-check test-osh-lex test-osh-parse test-osh-expand osh test-osh-e2e osh-bash-verify +.PHONY: osh-gen osh-gen-check test-osh-lex test-osh-parse test-osh-expand osh test-osh-e2e osh-bash-verify osh-bash-diff OSH_DIR = $(OUT_DIR)/osh OSH_FUZZ ?= 100000 OSH_GEN = src/osh/osh_gen.sh @@ -116,6 +116,12 @@ test-osh-e2e: osh-gen-check $(OSH_DIR)/osh $(OSH_DIR)/argprint $(OSH_DIR)/test_o @tail -3 $(OSH_DIR)/e2e.out; tail -1 $(OSH_DIR)/e2e.out | grep -q '^OSH_E2E_PASS$$' @echo "test-osh-e2e: PASS" +# omega#344: osh against /usr/bin/bash case by case (stdout, stderr, status, files), then the negative control +# (every expectation corrupted must be detected). Needs bash 5.2 at /usr/bin/bash. +osh-bash-diff: $(OSH_DIR)/osh + bash tests/osh/bashdiff/bashdiff.sh $(abspath $(OSH_DIR)/osh) tests/osh/bashdiff/cases.txt + BASHDIFF_NEGCTL=1 bash tests/osh/bashdiff/bashdiff.sh $(abspath $(OSH_DIR)/osh) tests/osh/bashdiff/cases.txt + # Core-only step trace (OSH-AIENOS-0): the lexer, parser and expander run over fixture scripts through osh_core_call, nothing # is executed. `make osh-trace` runs every tests/osh/trace/*.sh native AND interpreted, requires the two traces to be # identical, and requires them to equal the committed .trace file. `make osh-trace-gen` rewrites the .trace files. diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..f7abee563 100644 --- a/src/.crumb +++ b/src/.crumb @@ -115,7 +115,7 @@ "purpose": "Directory src/language. Purpose not yet described by a human." }, { - "digest": "cd0551dbfb6fc354741efe9a3ad1808ffaaf6a8d7e9e7345851333d7a721cee0", + "digest": "e0eee44045097fd7edf9f542b96bfdfc42813ecf6f27c25bd1c57f7adf985bc4", "last_commit": "d863780eb", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " @@ -187,15 +187,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:2f729945214be989a74d7c2064ae4ba3c2deb52ebea3f334b459198a7bd032ed", - "compiled_at": "2026-10-30T02:49:13Z", + "children_root": "sha256:11fe6b8734e8bbb23d04f78d1e6c9dfb33e6021a171e1d376a17656e43a902a7", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", + "digest": "sha256:bdfaf2a0933b765205e4911249e7bdfb2503920525135bd991c85cc4f8c066c3", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 568, - "generated_at_commit": "2cb1ccb5d3c1a1ce1cb94a7a3dd3c221395c4e15", - "source_tree": "sha256:7f1fef46a27e8baf5cf567bb590d417ede3767c5f07e714d0f0812af0645bb4b" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:39a95f62fe202310e5587b1e6aeb17c6a02a55d1b06c1f0c861bb48401c8e523" }, "owner_lane": "", "provenance": { diff --git a/src/osh/.crumb b/src/osh/.crumb index 930024a7e..600dbef66 100644 --- a/src/osh/.crumb +++ b/src/osh/.crumb @@ -3,21 +3,21 @@ "generated": { "children": [ { - "digest": "ed0b34d5fdccf68aa964a28afc052e5dad141d4b52be2eddc4ee9bd41595b2d1", - "last_commit": "dc7c1304", + "digest": "957a6431fb65d9a6db2e29cf3c24d405da40eae9c8f5c8f44067f22ee756775d", + "last_commit": "d863780eb", "name": "host", "purpose": "osh Linux host: execution service (request decoder, resolution, builtins, redirections, pipeline" } ], - "children_root": "sha256:310e660592d237e5e2cce3c017ec1b2333cc0be82c3dbaabfd0ee33a362d715f", - "compiled_at": "2026-10-24T04:15:18Z", + "children_root": "sha256:a46c60bb822dd5ab3ba58ded117e76d6e78d0ae5563574512aefb2e84cad3503", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:cd0551dbfb6fc354741efe9a3ad1808ffaaf6a8d7e9e7345851333d7a721cee0", + "digest": "sha256:e0eee44045097fd7edf9f542b96bfdfc42813ecf6f27c25bd1c57f7adf985bc4", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 31, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:3197e6551accf7fd7f3c42be3a36e8fbff70a836d4b477a9642fb3cc6d47d56c" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:a347d812bb1071a3b6721fed6d0b5546770351fa8f7fc8bbd2c7f340149ff9d5" } }, "name": "osh", diff --git a/src/osh/host/.crumb b/src/osh/host/.crumb index c3b214944..f7a657e92 100644 --- a/src/osh/host/.crumb +++ b/src/osh/host/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:ed0b34d5fdccf68aa964a28afc052e5dad141d4b52be2eddc4ee9bd41595b2d1", + "digest": "sha256:957a6431fb65d9a6db2e29cf3c24d405da40eae9c8f5c8f44067f22ee756775d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 16, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:47823c87e6eb4f245ca6445c813407b8ea6d378e400933837945c1975f49adea" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:91229874a9fc61dd1ac6f281d39584e2955ae6d84b0e56c3f39c65d41342286e" } }, "name": "host", diff --git a/src/osh/host/osh_builtin.c b/src/osh/host/osh_builtin.c index a7969ea78..cf5de74ee 100644 --- a/src/osh/host/osh_builtin.c +++ b/src/osh/host/osh_builtin.c @@ -109,6 +109,8 @@ static int canon(const char *in, char *out, size_t cap) return 0; } +/* The logical working directory: $PWD when it names ".", else getcwd. When the directory was removed getcwd fails + * (ENOENT) and bash keeps using the directory it remembered, so an absolute $PWD is used then (omega#344). */ static int logical_pwd(const OshSession *s, char *out, size_t cap) { const char *p = osh_var_get(s, "PWD"); @@ -118,7 +120,21 @@ static int logical_pwd(const OshSession *s, char *out, size_t cap) strcpy(out, p); return 0; } - return getcwd(out, cap) ? 0 : -1; + if (getcwd(out, cap)) return 0; + if (errno == ENOENT && p && p[0] == '/' && strlen(p) < cap) { + strcpy(out, p); + return 0; + } + return -1; +} + +/* bash's message when getcwd cannot name a removed directory; printed without the shell-name prefix, as bash does. */ +static void getcwd_error(int fd, const char *who) +{ + char buf[256]; + int n = snprintf(buf, sizeof buf, "%s: error retrieving current directory: getcwd: cannot access parent directories: %s\n", + who, strerror(errno)); + (void)osh_write_all(fd, buf, (size_t)n); } static int bi_cd(OshSession *s, const OshCmd *c, const int io[3], int in_parent) @@ -131,7 +147,7 @@ static int bi_cd(OshSession *s, const OshCmd *c, const int io[3], int in_parent) for (const char *o = c->argv[i] + 1; *o; o++) { if (*o == 'L') physical = 0; else if (*o == 'P') physical = 1; - else { osh_diag(io[2], "cd: -%c: invalid option\ncd: usage: cd [-L|-P] [dir]", *o); return 2; } + else { osh_diag(io[2], "cd: -%c: invalid option\ncd: usage: cd [-L|[-P [-e]] [-@]] [dir]", *o); return 2; } } } if (c->nargv - i > 1) { osh_diag(io[2], "cd: too many arguments"); return 1; } @@ -188,15 +204,18 @@ static int bi_pwd(OshSession *s, const OshCmd *c, const int io[3]) { int physical = 0, i = 1; for (; i < c->nargv && c->argv[i][0] == '-' && c->argv[i][1]; i++) { - if (strcmp(c->argv[i], "-P") == 0) physical = 1; - else if (strcmp(c->argv[i], "-L") == 0) physical = 0; - else if (strcmp(c->argv[i], "--") == 0) { i++; break; } - else { osh_diag(io[2], "pwd: %s: invalid option", c->argv[i]); return 2; } + if (strcmp(c->argv[i], "--") == 0) break; + for (const char *o = c->argv[i] + 1; *o; o++) { + if (*o == 'P') physical = 1; + else if (*o == 'L') physical = 0; + else { osh_diag(io[2], "pwd: -%c: invalid option\npwd: usage: pwd [-LP]", *o); return 2; } + } } - if (i < c->nargv) { osh_diag(io[2], "pwd: too many arguments"); return 1; } + /* bash 5.2 ignores operands of pwd */ char p[PATH_MAX + 1]; if (physical ? !getcwd(p, PATH_MAX) : logical_pwd(s, p, PATH_MAX)) { - osh_diag(io[2], "pwd: %s", strerror(errno)); + if (errno == ENOENT) getcwd_error(io[2], "pwd"); + else osh_diag(io[2], "pwd: %s", strerror(errno)); return 1; } strcat(p, "\n"); @@ -447,12 +466,18 @@ static int bi_printf(const OshCmd *c, const int io[3]) { int ai = 1; if (ai < c->nargv && strcmp(c->argv[ai], "--") == 0) ai++; - /* bash takes any other leading -x as an option (-v sets a variable); none is supported, so none is printed */ + /* bash takes any other leading -x as an option: -v sets a variable and is refused here (not supported); any other + * letter is bash's "invalid option" with the usage line, status 2. Nothing is printed in either case. */ else if (ai < c->nargv && c->argv[ai][0] == '-' && c->argv[ai][1]) { - osh_diag(io[2], "printf: %s: option not supported", c->argv[ai]); + if (c->argv[ai][1] == 'v') osh_diag(io[2], "printf: %s: option not supported", c->argv[ai]); + else osh_diag(io[2], "printf: -%c: invalid option\nprintf: usage: printf [-v var] format [arguments]", c->argv[ai][1]); + return 2; + } + if (ai >= c->nargv) { /* bash: the bare usage line, no shell-name prefix, status 2 */ + static const char u[] = "printf: usage: printf [-v var] format [arguments]\n"; + (void)osh_write_all(io[2], u, sizeof u - 1); return 2; } - if (ai >= c->nargv) { osh_diag(io[2], "printf: usage: printf format [arguments]"); return 2; } /* bash: 2 */ const char *fmt = c->argv[ai++]; int first = ai, nargs = 0; /* arguments one pass over the format consumes */ /* Validate EVERY conversion before anything is printed. The scan and the print loop below agree on what a @@ -639,10 +664,10 @@ static int bi_export(OshSession *s, const OshCmd *c, const int io[3]) char name[256]; size_t nl = eq ? (size_t)(eq - a) : strlen(a); if (fn) { osh_diag(io[2], "export: %s: not a function", a); status = 1; continue; } - if (nl == 0 || nl >= sizeof name) { osh_diag(io[2], "export: '%s': not a valid identifier", a); status = 1; continue; } + if (nl == 0 || nl >= sizeof name) { osh_diag(io[2], "export: `%s': not a valid identifier", a); status = 1; continue; } memcpy(name, a, nl); name[nl] = 0; - if (!osh_name_valid(name)) { osh_diag(io[2], "export: '%s': not a valid identifier", a); status = 1; continue; } + if (!osh_name_valid(name)) { osh_diag(io[2], "export: `%s': not a valid identifier", a); status = 1; continue; } if (unexport) { if (eq && osh_var_set(s, name, eq + 1)) { osh_diag(io[2], "export: out of memory"); status = 1; continue; } osh_var_unexport(s, name); @@ -657,7 +682,7 @@ static int bi_unset(OshSession *s, const OshCmd *c, const int io[3]) for (; i < c->nargv && c->argv[i][0] == '-' && c->argv[i][1]; i++) { if (strcmp(c->argv[i], "-v") == 0) continue; if (strcmp(c->argv[i], "--") == 0) { i++; break; } - osh_diag(io[2], "unset: %s: invalid option", c->argv[i]); + osh_diag(io[2], "unset: -%c: invalid option\nunset: usage: unset [-f] [-v] [-n] [name ...]", c->argv[i][1]); return 2; } for (; i < c->nargv; i++) { diff --git a/src/osh/host/osh_exec.c b/src/osh/host/osh_exec.c index 8684c341f..f76d9f27f 100644 --- a/src/osh/host/osh_exec.c +++ b/src/osh/host/osh_exec.c @@ -648,7 +648,7 @@ static void run_pipeline(OshSession *s, const OshRequest *r, JCtx *jc, OshResult if (wr >= 0) { close(wr); p.pp[i][1] = -1; } continue; } - envp = osh_build_envp(s, c->assign, c->nassign); + envp = osh_build_envp(s, c->assign, c->nassign, path); if (!envp) { launch_failed = 1; set_cmd(&res->cmd[i], 1, OSH_OUT_NOT_STARTED, OSH_E_LIMIT); break; } } diff --git a/src/osh/host/osh_host.h b/src/osh/host/osh_host.h index 95806fbef..d13512e9f 100644 --- a/src/osh/host/osh_host.h +++ b/src/osh/host/osh_host.h @@ -180,7 +180,7 @@ int osh_var_export(OshSession *s, const char *name, const char *value_or_null); int osh_var_unexport(OshSession *s, const char *name); /* keeps the variable, drops the export mark */ int osh_var_unset(OshSession *s, const char *name); int osh_name_valid(const char *s); -char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov); /* malloc'd, free with osh_envp_free */ +char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov, const char *prog); /* prog: _=prog (NULL: none); malloc'd, free with osh_envp_free */ void osh_envp_free(char **envp); /* ---- results ---- */ diff --git a/src/osh/host/osh_shell.c b/src/osh/host/osh_shell.c index 4575ec6fb..4b3d590b3 100644 --- a/src/osh/host/osh_shell.c +++ b/src/osh/host/osh_shell.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "osh_layout.h" @@ -126,8 +127,12 @@ static int append(OshShell *sh, const uint8_t *p, size_t n) /* Interactive only: ^C at the prompt abandons the line being typed (bash prints a new prompt, $? = 130); the shell * itself ignores ^\ ^Z and background tty signals. osh_exec installs its own handling around each pipeline and puts - * these back afterwards. The SIGINT handler has no SA_RESTART, so the blocked read() returns EINTR. */ + * these back afterwards. While osh reads and parses input SIGINT stays blocked and is only let in by the pselect + * that waits for the next byte, so a ^C that lands between two reads (while the core lexes or parses) is pending + * and wakes that pselect instead of being lost or noticed one line late (omega#344). It is unblocked again while a + * list runs. The embedder's dispositions and mask are saved on entry and restored when run_src returns. */ static volatile sig_atomic_t g_prompt_int; +static sigset_t g_prompt_wait; /* the mask pselect waits with: the entry mask without SIGINT */ static void on_prompt_int(int sig) { @@ -135,18 +140,33 @@ static void on_prompt_int(int sig) g_prompt_int = 1; } -static void prompt_signals(void) +static const int k_prompt_sigs[] = {SIGINT, SIGQUIT, SIGTSTP, SIGTTIN, SIGTTOU}; +#define NPROMPT ((int)(sizeof k_prompt_sigs / sizeof k_prompt_sigs[0])) + +typedef struct { + struct sigaction old[NPROMPT]; + sigset_t mask, blocked; /* the entry mask; the entry mask plus SIGINT */ +} PromptSigs; + +static void prompt_signals(PromptSigs *ps) { struct sigaction sa; memset(&sa, 0, sizeof sa); sigemptyset(&sa.sa_mask); - sa.sa_handler = on_prompt_int; - sigaction(SIGINT, &sa, NULL); - sa.sa_handler = SIG_IGN; - sigaction(SIGQUIT, &sa, NULL); - sigaction(SIGTSTP, &sa, NULL); - sigaction(SIGTTIN, &sa, NULL); - sigaction(SIGTTOU, &sa, NULL); + sigprocmask(SIG_SETMASK, NULL, &ps->mask); + ps->blocked = ps->mask; + sigaddset(&ps->blocked, SIGINT); + g_prompt_wait = ps->mask; /* without SIGINT unless the embedder itself blocks it, which then stays a block */ + for (int i = 0; i < NPROMPT; i++) { + sa.sa_handler = k_prompt_sigs[i] == SIGINT ? on_prompt_int : SIG_IGN; + sigaction(k_prompt_sigs[i], &sa, &ps->old[i]); + } +} + +static void prompt_restore(const PromptSigs *ps) +{ + for (int i = 0; i < NPROMPT; i++) sigaction(k_prompt_sigs[i], &ps->old[i], NULL); + sigprocmask(SIG_SETMASK, &ps->mask, NULL); } /* Append one line (with its newline) to the list buffer. A last line with no newline gets one, except @@ -171,6 +191,17 @@ static int read_line(OshShell *sh, LineSrc *src) } for (;;) { uint8_t c; + if (sh->interactive) { /* SIGINT is blocked here; only this wait lets it in (see prompt_signals) */ + if (src->fd >= FD_SETSIZE) return -1; + fd_set rf; + FD_ZERO(&rf); + FD_SET(src->fd, &rf); + if (pselect(src->fd + 1, &rf, NULL, NULL, NULL, &g_prompt_wait) < 0) { + if (errno != EINTR) return -1; + if (g_prompt_int) return -2; + continue; + } + } ssize_t r = read(src->fd, &c, 1); if (r < 0 && errno == EINTR) { if (g_prompt_int) return -2; @@ -241,7 +272,8 @@ static void refusal(OshShell *sh, uint64_t code, int syntax) sh->refused++; } -/* expand and run the parsed list. Returns 1 when the shell must stop (exit builtin, interrupted, fault). */ +/* expand and run the parsed list. Returns 1 when the shell must stop (exit builtin, interrupted, fault), 2 when + * `exit` with too many arguments dropped the rest of the list. */ static int run_list(OshShell *sh) { uint64_t *w = sh->w; @@ -258,7 +290,7 @@ static int run_list(OshShell *sh) w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)sh->s.last_status; if (sh->s.exit_requested) return 1; if (res.killed_by_int) return 0; - if (sh->s.abort_list) { sh->s.abort_list = 0; return 0; } + if (sh->s.abort_list) { sh->s.abort_list = 0; return 2; } continue; } case ST_COMPLETE: return 0; @@ -272,9 +304,8 @@ static int run_list(OshShell *sh) return 1; } -static int run_src(OshShell *sh, LineSrc *src) +static int run_loop(OshShell *sh, LineSrc *src, const PromptSigs *ps) { - if (sh->interactive) prompt_signals(); for (;;) { reset_list(sh); g_prompt_int = 0; @@ -307,7 +338,13 @@ static int run_src(OshShell *sh, LineSrc *src) /* an empty list at end of input: nothing to run */ } else if (st == 0) { sh->lists++; - if (run_list(sh)) return sh->s.exit_requested ? sh->s.exit_status : sh->s.last_status; + if (ps) sigprocmask(SIG_SETMASK, &ps->mask, NULL); /* children and osh_exec see the entry mask */ + int rl = run_list(sh); + if (ps) sigprocmask(SIG_SETMASK, &ps->blocked, NULL); + if (rl == 1) return sh->s.exit_requested ? sh->s.exit_status : sh->s.last_status; + /* bash: in a -c string the failed exit discards the whole rest of the string (status 1); a script file, + * stdin and the interactive prompt go on with the next line (omega#344) */ + if (rl == 2 && src->exact_end) return sh->s.last_status; } else { if (st >= 200 && st != OSH_CORE_FAULT) refusal(sh, st, 1); else { fprintf(stderr, "osh: syntax error: unexpected end of input\n"); sh->s.last_status = 2; } @@ -317,6 +354,17 @@ static int run_src(OshShell *sh, LineSrc *src) } } +static int run_src(OshShell *sh, LineSrc *src) +{ + if (!sh->interactive) return run_loop(sh, src, NULL); + PromptSigs ps; + prompt_signals(&ps); + sigprocmask(SIG_SETMASK, &ps.blocked, NULL); + int rc = run_loop(sh, src, &ps); + prompt_restore(&ps); + return rc; +} + int osh_shell_run_string(OshShell *sh, const char *text, size_t len) { LineSrc src = {text, len, 0, -1, 1}; diff --git a/src/osh/host/osh_vars.c b/src/osh/host/osh_vars.c index 795bf33c2..38fd72503 100644 --- a/src/osh/host/osh_vars.c +++ b/src/osh/host/osh_vars.c @@ -20,7 +20,8 @@ * osh_session_init, and never again. Names and values are raw bytes without NUL. * Bookkeeping as bash 5.2 does it: PWD per POSIX; SHLVL = inherited level + 1 (non-numeric counts as 0, never below 0, * above 1000 resets to 1 with a warning); OLDPWD exported, kept only when the inherited value names a directory and - * set by cd. `_` (bash's last argument) is not maintained. + * set by cd. `_` is not a shell variable ($_ is refused as SPECIAL_PARAM); as bash does, each program started gets + * _= in its environment (osh_build_envp prog). */ #include "osh_host.h" #include "osh_priv.h" @@ -189,9 +190,25 @@ void osh_envp_free(char **envp) free(envp); } -char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov) +static int envp_put(char **e, size_t *n, const char *name, const char *value) { - char **e = calloc(s->nvars + s->nraw + (size_t)nov + 1, sizeof *e); + size_t nl = strlen(name), vl = strlen(value); + char *str = malloc(nl + vl + 2); + if (!str) return -1; + memcpy(str, name, nl); + str[nl] = '='; + memcpy(str + nl + 1, value, vl + 1); + size_t j = 0; + for (; j < *n; j++) + if (strncmp(e[j], name, nl) == 0 && e[j][nl] == '=') break; + if (j < *n) { free(e[j]); e[j] = str; } + else e[(*n)++] = str; + return 0; +} + +char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov, const char *prog) +{ + char **e = calloc(s->nvars + s->nraw + (size_t)nov + 2, sizeof *e); if (!e) return NULL; size_t n = 0; for (size_t i = 0; i < s->nvars; i++) { @@ -209,19 +226,10 @@ char **osh_build_envp(const OshSession *s, const OshAssign *ov, int nov) if (!(e[n] = strdup(s->raw_env[i]))) { osh_envp_free(e); return NULL; } n++; } - for (int k = 0; k < nov; k++) { - size_t nl = strlen(ov[k].name), vl = strlen(ov[k].value); - char *str = malloc(nl + vl + 2); - if (!str) { osh_envp_free(e); return NULL; } - memcpy(str, ov[k].name, nl); - str[nl] = '='; - memcpy(str + nl + 1, ov[k].value, vl + 1); - size_t j = 0; - for (; j < n; j++) - if (strncmp(e[j], ov[k].name, nl) == 0 && e[j][nl] == '=') break; - if (j < n) { free(e[j]); e[j] = str; } - else e[n++] = str; - } + for (int k = 0; k < nov; k++) + if (envp_put(e, &n, ov[k].name, ov[k].value)) { osh_envp_free(e); return NULL; } + /* bash puts _= into every program's environment, over any inherited or assigned _ */ + if (prog && envp_put(e, &n, "_", prog)) { osh_envp_free(e); return NULL; } e[n] = NULL; return e; } diff --git a/src/osh/osh_expand.osc b/src/osh/osh_expand.osc index 6890c78f5..5bca38473 100644 --- a/src/osh/osh_expand.osc +++ b/src/osh/osh_expand.osc @@ -540,13 +540,15 @@ fn cmd_end(w: cells) -> u64 { return 0; } -// 1 when argv[0] of this command is the plain word `export` (raw bytes `export` then a blank, value `export`): bash -// expands its NAME=value arguments as assignments, so their values are not split (mode 4). +// 1 when argv[0] of this command is the plain word `export` (raw bytes `export` then a blank, value `export`) and +// that word is the command's first word: bash expands its NAME=value arguments as assignments, so their values are +// not split (mode 4). An empty expansion before it (`e=; $e export A=$y`) leaves an ordinary command (omega#344). fn is_export(inp: bytes, w: cells) -> u64 { let cb: u64 = cbase(w); if w[cb] == 0 { return 0; } if pack8o(w, w[cb + 4], w[cb + 5]) != 0x6578706f7274 { return 0; } let a: u64 = w[44]; + if w[43] != a + 1 { return 0; } if a + 6 >= inp.len { return 0; } let mut v: u64 = 0; let mut i: u64 = 0; @@ -573,7 +575,7 @@ fn next_task(inp: bytes, w: cells) -> u64 { if ((hdr >> 8) & 16) != 0 && w[43] == 0 { return start_assign(inp, w, tb); } let mut m: u64 = 1; if ((hdr >> 8) & 16) != 0 && is_export(inp, w) == 1 { m = 4; } - w[43] = 1; + if w[43] == 0 { w[43] = w[tb + 1] + 1; } wset(w, m, w[tb + 1]); return 0; } diff --git a/src/osh/osh_expand.osc.in b/src/osh/osh_expand.osc.in index 8e2db6f5b..cca644ec5 100644 --- a/src/osh/osh_expand.osc.in +++ b/src/osh/osh_expand.osc.in @@ -539,13 +539,15 @@ fn cmd_end(w: cells) -> u64 { return 0; } -// 1 when argv[0] of this command is the plain word `export` (raw bytes `export` then a blank, value `export`): bash -// expands its NAME=value arguments as assignments, so their values are not split (mode 4). +// 1 when argv[0] of this command is the plain word `export` (raw bytes `export` then a blank, value `export`) and +// that word is the command's first word: bash expands its NAME=value arguments as assignments, so their values are +// not split (mode 4). An empty expansion before it (`e=; $e export A=$y`) leaves an ordinary command (omega#344). fn is_export(inp: bytes, w: cells) -> u64 { let cb: u64 = cbase(w); if w[cb] == 0 { return 0; } if pack8o(w, w[cb + 4], w[cb + 5]) != 0x6578706f7274 { return 0; } let a: u64 = w[@EX_A0@]; + if w[@EX_ANYW@] != a + 1 { return 0; } if a + 6 >= inp.len { return 0; } let mut v: u64 = 0; let mut i: u64 = 0; @@ -572,7 +574,7 @@ fn next_task(inp: bytes, w: cells) -> u64 { if ((hdr >> 8) & 16) != 0 && w[@EX_ANYW@] == 0 { return start_assign(inp, w, tb); } let mut m: u64 = 1; if ((hdr >> 8) & 16) != 0 && is_export(inp, w) == 1 { m = 4; } - w[@EX_ANYW@] = 1; + if w[@EX_ANYW@] == 0 { w[@EX_ANYW@] = w[tb + 1] + 1; } wset(w, m, w[tb + 1]); return 0; } diff --git a/src/osh/osh_expand_ref.c b/src/osh/osh_expand_ref.c index b3c7bd280..d6334e172 100644 --- a/src/osh/osh_expand_ref.c +++ b/src/osh/osh_expand_ref.c @@ -350,12 +350,12 @@ void osh_xr_pipe(const uint8_t *in, size_t n, const OshRefLex *lx, const OshRefP } else { c.mode = 1; /* export NAME=v: bash expands the argument as an assignment (no splitting) when the command word - * is the plain word export */ - if ((tk->flags & 16) && anyw && c.blk[0] > 0 && c.blk[5] == 6 && + * is the plain word export and is the command's first word (omega#344: `$e export` is not) */ + if ((tk->flags & 16) && anyw && (uint64_t)(anyw - 1) == c.a0off && c.blk[0] > 0 && c.blk[5] == 6 && memcmp(out->out + c.blk[4], "export", 6) == 0 && c.a0off + 6 < n && memcmp(in + c.a0off, "export", 6) == 0 && (in[c.a0off + 6] == ' ' || in[c.a0off + 6] == '\t')) c.mode = 4; - anyw = 1; + if (!anyw) anyw = (int)tk->start + 1; expand_word(&c, tk->start, tk->start + tk->len); } t++; diff --git a/src/osh/osh_layout.lst b/src/osh/osh_layout.lst index 301222adc..59008ed40 100644 --- a/src/osh/osh_layout.lst +++ b/src/osh/osh_layout.lst @@ -98,7 +98,7 @@ EX_CMD 31 # command index inside the pipeline EX_TOK 32 # token cursor (the word token while a word is open) EX_NVREQ 33 # variable requests in this pipeline EX_OUT 42 # OUT bytes used -EX_ANYW 43 # a non-assignment word was seen in this command +EX_ANYW 43 # offset+1 of the first non-assignment word of this command, 0 before it EX_A0 44 # input offset of the word that produced argv[0] EX_WM 45 # word mode: 0 none, 1 argv, 2 assignment value, 3 redirection target EX_PH 46 # 0 raw scan, 1 copying a value, 2 waiting for the host's answer diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..bfaa36051 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -199,7 +199,7 @@ "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { - "digest": "d958af5413726ddc48a47721800d9766939129f480b26492332ba094577837cb", + "digest": "1aeab35b1d2e714d54d6ce800141ae16c00434ed43c233e8d2e5dbfb5fe9d60c", "last_commit": "d863780eb", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" @@ -301,15 +301,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:55f44a3f8770aafc677178ad5b04d273aaca550c8c10541ce97a9f34c27517df", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:e9f9bfdd80e39e62ac58004ecc90d27bc3a6f127b05a81420451ccda4fcb3a8e", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:74ded732f0fe8b3a71dde459b845cbf1532540efa1ddf411371ee7aa1cabac7b" }, "owner_lane": "", "provenance": { diff --git a/tests/osh/.crumb b/tests/osh/.crumb index 50a79cb89..b2f7e9ea3 100644 --- a/tests/osh/.crumb +++ b/tests/osh/.crumb @@ -4,32 +4,32 @@ "children": [ { "digest": "4a34c4254df84173e225ddfcd932b9d703cdbef74b76c8702c17003fa3ddda4c", - "last_commit": "dc7c1304", + "last_commit": "d863780eb", "name": "e2e", "purpose": "end-to-end tests of the osh program: argprint helper plus a runner comparing native and OSH_INTE" }, { - "digest": "4c16e4211dbc6411b14f2cc0ae0c7201c6cbc6275c0d8aac8d24a00dc1fe9c99", - "last_commit": "dc7c1304", + "digest": "484f30e60a7d356b4c35eb50a1ff102719cb668b7c88e40ffe9736eb59d321f3", + "last_commit": "d863780eb", "name": "host", "purpose": "tests for the osh Linux host adapter execution service (hand-built request records, helper-mode " }, { "digest": "12b30cde36c62e66c13ab90d447e321ac648541af04d0b19d65393418bdba79b", - "last_commit": "dc7c1304", + "last_commit": "d863780eb", "name": "trace", "purpose": "Core-only step traces of the osh shell units over fixture scripts (OSH-AIENOS-0 differential fix" } ], - "children_root": "sha256:0b5655b5310165991cb5863b391d5d5a89d2f13789e3e09dfb5f167ff0fcd56a", - "compiled_at": "2026-10-24T04:15:18Z", + "children_root": "sha256:991d9ea1d859fa961358d0c05581a46b7a5cfdb764fefbfeaeaef9a3da2d02da", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:d958af5413726ddc48a47721800d9766939129f480b26492332ba094577837cb", + "digest": "sha256:1aeab35b1d2e714d54d6ce800141ae16c00434ed43c233e8d2e5dbfb5fe9d60c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 93, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:1d065ed1a8de4f7224de651d2d5cf8e05914fb8bbc9ad3a44e37f0466c369429" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:ee3c74320576e32046abccd58f37fea059ab03af87cba9729415a8303ad4bc1c" } }, "name": "osh-tests", diff --git a/tests/osh/bashdiff/bashdiff.sh b/tests/osh/bashdiff/bashdiff.sh new file mode 100755 index 000000000..5fb62a38c --- /dev/null +++ b/tests/osh/bashdiff/bashdiff.sh @@ -0,0 +1,128 @@ +#!/bin/bash +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2026 Drake Stapleton (AIEN / aien-dev) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# bashdiff.sh: run each case under REAL GNU bash 5.2 and under osh and compare (omega#344; grown from the #340 review +# runner handoffs/osh-audit/L-diff.sh). Usage: bashdiff.sh OSH CASES [name-regex] +# +# Both shells run in a fresh scratch directory under the same pinned environment: env -i, LC_ALL=C, TZ=UTC, +# PATH=/usr/bin:/bin, HOME=/home. osh gets `--caps` with every effect allowed under / and a private journal. +# Case file: blocks headed `### NAME | MODE | EXPECT | ARGS`, then the body lines. +# MODE c SH -c BODY SH ARGS f SH s.sh ARGS (body in s.sh) s SH < s.sh +# EXPECT match stdout, exit status, files and stderr (prefix-normalized) byte for byte +# class stdout, exit status and files byte for byte; stderr only empty versus non-empty +# refuse=T osh must refuse by name: its stderr contains T, stdout is empty, status is nonzero, and the +# scratch directory is unchanged (no side effect); the bash result is shown, not compared +# A body line `%%` splits it: lines before it are setup, run by bash in the scratch directory before each shell; the +# "unchanged" check of refuse= compares against the state after setup. Lines starting `#` before the first case are +# comments. stderr normalization: a leading `SH: `, `bash: `, `osh: `, `s.sh: `, the bash or osh path, with an optional `line N: `, becomes `SH: `. +# BASHDIFF_NEGCTL=1: negative control. Every expectation is deliberately corrupted (bash stdout gets one extra byte; +# a refuse= text gets a suffix no shell prints); every case must then FAIL, and the run passes only if all did. +# Prints one line per case (PASS/REFUSED/FAIL) and `BASHDIFF PASS cases=N` or `BASHDIFF FAIL ...`; exit 0 only on PASS. +set -u +[ $# -ge 2 ] || { echo "usage: bashdiff.sh OSH CASES [name-regex]" >&2; exit 2; } +OSH=$(readlink -f "$1"); CF=$(readlink -f "$2"); ONLY=${3:-.} +[ -x "$OSH" ] || { echo "bashdiff: $1 is not executable" >&2; exit 2; } +BASH_REF=/usr/bin/bash +ver=$("$BASH_REF" -c 'echo $BASH_VERSION') +case $ver in 5.2.*) ;; *) echo "bashdiff: need GNU bash 5.2.x at $BASH_REF, have $ver" >&2; exit 2 ;; esac +NEG=${BASHDIFF_NEGCTL:-0} +W=$(mktemp -d "${TMPDIR:-/tmp}/bashdiff.XXXXXX") +trap 'chmod -R u+rwx "$W" 2>/dev/null; rm -rf "$W"' EXIT +printf 'principal 1\nallow spawn /\nallow read /\nallow write /\nallow chdir /\n' > "$W/policy" +mkdir -m 700 "$W/j" +echo "bashdiff: bash $ver ($BASH_REF), env -i LC_ALL=C TZ=UTC PATH=/usr/bin:/bin, osh $OSH" +pass=0; fail=0; refused=0; total=0 +norm() { sed -E "s#^(SH|bash|osh|s\\.sh|$BASH_REF|$OSH)(: line [0-9]+)?: #SH: #"; } +snap() { ( cd "$1" && find . -path ./home -prune -o -print | LC_ALL=C sort | while IFS= read -r f; do + if [ -L "$f" ]; then printf 'L %s -> %s\n' "$f" "$(readlink "$f")" + elif [ -d "$f" ]; then printf 'D %s\n' "$f" + else printf 'F %s %s\n' "$f" "$(od -An -c "$f" 2>/dev/null | tr -s ' \n' ' ')"; fi; done ); } +runone() { # $1 label $2 shell-kind (bash|osh) $3 mode, rest args; body in $W/body, setup in $W/setup + local pre=$W/$1 kind=$2 mode=$3; shift 3 + local d=$W/run + chmod -R u+rwx "$d" 2>/dev/null; rm -rf "$d"; mkdir -p "$d/home" + ( cd "$d" && env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "$BASH_REF" "$W/setup" ) >/dev/null 2>&1 + cp "$W/body" "$d/s.sh" + snap "$d" > "$pre.before" + local -a sh + if [ "$kind" = bash ]; then sh=("$BASH_REF"); else sh=("$OSH" --caps "$W/policy" --journal "$W/j/effects.journal"); fi + ( cd "$d" && case $mode in + c) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" -c "$(cat s.sh)" SH "$@" ;; + f) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" s.sh "$@" ;; + s) exec env -i PATH=/usr/bin:/bin HOME="$d/home" LC_ALL=C TZ=UTC "${sh[@]}" < s.sh ;; + *) echo "bashdiff: bad mode $mode" >&2; exit 99 ;; + esac ) > "$pre.out" 2> "$pre.rawerr" < /dev/null + echo $? > "$pre.rc" + sed "s#$d#DIR#g" "$pre.rawerr" | norm > "$pre.err" + sed -i "s#$d#DIR#g" "$pre.out" + snap "$d" > "$pre.files" +} +show() { local k; for k in "$@"; do cmp -s "$W/b.$k" "$W/o.$k" && continue + echo " -- $k bash:"; head -c 600 "$W/b.$k" | sed 's/^/ /'; echo " -- $k osh:"; head -c 600 "$W/o.$k" | sed 's/^/ /'; done; } +runcase() { + local name=$1 mode=$2 expect=$3; shift 3 + total=$((total + 1)) + runone b bash "$mode" "$@" + runone o osh "$mode" "$@" + local ok=1 + case $expect in + match|class) + [ "$NEG" = 1 ] && printf 'X' >> "$W/b.out" + cmp -s "$W/b.out" "$W/o.out" && cmp -s "$W/b.rc" "$W/o.rc" && cmp -s "$W/b.files" "$W/o.files" || ok=0 + if [ "$expect" = match ]; then cmp -s "$W/b.err" "$W/o.err" || ok=0 + else [ -s "$W/b.err" ] && [ -s "$W/o.err" ] || { [ ! -s "$W/b.err" ] && [ ! -s "$W/o.err" ]; } || ok=0; fi + if [ $ok = 1 ]; then pass=$((pass + 1)); echo "PASS $name" + else fail=$((fail + 1)); echo "FAIL $name ($expect)"; show out err rc files; fi ;; + refuse=*) + local t=${expect#refuse=} + [ "$NEG" = 1 ] && t="$t" + grep -qF -- "$t" "$W/o.rawerr" || ok=0 + [ -s "$W/o.out" ] && ok=0 + [ "$(cat "$W/o.rc")" != 0 ] || ok=0 + cmp -s "$W/o.before" "$W/o.files" || ok=0 + if [ $ok = 1 ]; then refused=$((refused + 1)); pass=$((pass + 1)); echo "REFUSED $name :: $(head -c 160 "$W/o.rawerr" | tr '\n' '|')" + else fail=$((fail + 1)); echo "FAIL $name (refuse=$t)" + echo " osh rc=$(cat "$W/o.rc") stdout=$(head -c 200 "$W/o.out" | tr '\n' '|') stderr=$(head -c 300 "$W/o.rawerr" | tr '\n' '|')" + cmp -s "$W/o.before" "$W/o.files" || { echo " osh changed the scratch directory:"; diff "$W/o.before" "$W/o.files" | head -20 | sed 's/^/ /'; } + echo " bash rc=$(cat "$W/b.rc") stdout=$(head -c 200 "$W/b.out" | tr '\n' '|')"; fi ;; + *) echo "bashdiff: case $name: bad expectation '$expect'" >&2; exit 2 ;; + esac +} +name=; mode=; expect=; args=() +flush() { + [ -n "$name" ] || return 0 + [[ $name =~ $ONLY ]] || return 0 + if grep -qx '%%' "$W/raw"; then sed '/^%%$/,$d' "$W/raw" > "$W/setup"; sed '1,/^%%$/d' "$W/raw" > "$W/body" + else : > "$W/setup"; cp "$W/raw" "$W/body"; fi + runcase "$name" "$mode" "$expect" "${args[@]}" +} +: > "$W/raw" +while IFS= read -r line || [ -n "$line" ]; do + if [[ $line == '### '* ]]; then + flush + IFS='|' read -r name mode expect rest <<< "${line#\#\#\# }" + name=${name// /}; mode=${mode// /}; expect=${expect# }; expect=${expect% } + eval "args=(${rest:-})"; : > "$W/raw" + elif [ -n "$name" ]; then printf '%s\n' "$line" >> "$W/raw" + fi +done < "$CF" +flush +if [ "$NEG" = 1 ]; then + if [ "$total" -gt 0 ] && [ "$pass" -eq 0 ]; then echo "BASHDIFF NEGCTL PASS cases=$total detected=$fail"; exit 0; fi + echo "BASHDIFF NEGCTL FAIL cases=$total undetected=$pass"; exit 1 +fi +if [ "$total" -gt 0 ] && [ "$fail" -eq 0 ]; then echo "BASHDIFF PASS cases=$total refused=$refused"; exit 0; fi +echo "BASHDIFF FAIL cases=$total pass=$pass fail=$fail refused=$refused"; exit 1 diff --git a/tests/osh/bashdiff/cases.txt b/tests/osh/bashdiff/cases.txt new file mode 100644 index 000000000..acc7534f5 --- /dev/null +++ b/tests/osh/bashdiff/cases.txt @@ -0,0 +1,76 @@ +# omega#344 bash-differential cases. Grammar: see bashdiff.sh. Run: make osh-bash-diff +# exit with too many arguments: bash -c stops the whole string at the failed exit (status 1); a script file or stdin +# continues with the next line. +### exit-many-c | c | match | +exit 1 2; echo after +echo next +### exit-many-f | f | match | +exit 1 2; echo after +echo next +### exit-many-s | s | match | +exit 1 2; echo after +echo next +### exit-many-c-andor | c | match | +true && exit 1 2 || echo or; echo after +echo next +# An empty expansion before `export` does not make it the declaration builtin: A=$y is split like any argument. +### export-after-empty-word | c | match | +y="a b"; e=; $e export A=$y; echo "[$A]" +### export-quoted-name | c | match | +y="a b"; "export" A=$y; echo "[$A]" +### export-plain | c | match | +y="a b"; export A=$y; echo "[$A]" +### export-after-redirect | c | match | +y="a b"; >/dev/null export A=$y; echo "[$A]" +# bash puts _= into each child's environment. +### underscore-child-env | c | match | +env | grep '^_=' +### underscore-printenv | c | match | +printenv _ +### underscore-inherited | c | match | +_=/x/y env | grep '^_=' +### underscore-param | c | refuse=SPECIAL_PARAM | +echo hi > f; echo $_ +# pwd and cd .. after the working directory was removed while reached through a symlink. +### pwd-removed-cwd-logical | c | match | +mkdir real; ln -s real lnk +%% +cd lnk; rmdir ../real; pwd; echo "rc=$?" +### pwd-removed-cwd-physical | c | match | +mkdir real; ln -s real lnk +%% +cd lnk; rmdir ../real; pwd -P; echo "rc=$?" +### cd-dotdot-removed-cwd | c | match | +mkdir real; ln -s real lnk +%% +cd lnk; rmdir ../real; cd ..; echo "rc=$?"; pwd; ls +# stderr wording of builtin errors. +### export-bad-name | c | match | +export 1x=2; echo "rc=$?" +### printf-no-format | c | match | +printf; echo "rc=$?" +### unset-bad-option | c | match | +unset -x; echo "rc=$?" +# Builtin option and operand diagnostics (bash 5.2.21 wording, status 2 for an invalid option). +### cd-bad-option | c | match | +cd -x; echo "rc=$?" +### pwd-bad-option | c | match | +pwd -x; echo "rc=$?" +### pwd-LP | c | match | +mkdir real; ln -s real lnk +%% +cd lnk; pwd -LP; pwd -PL; echo "rc=$?" +### pwd-operand | c | match | +pwd a b >/dev/null; echo "rc=$?" +### export-bad-option | c | match | +export -x; echo "rc=$?" +### export-eq-name | c | match | +export =a; echo "rc=$?" +### printf-bad-option | c | match | +printf -x; echo "rc=$?" +### printf-v | c | refuse=option not supported | +printf -v v %s x +### unset-bad-name | c | match | +unset 1x; echo "rc=$?" +### exit-nonnumeric | c | match | +exit x diff --git a/tests/osh/host/.crumb b/tests/osh/host/.crumb index 1c46bcef2..051c48430 100644 --- a/tests/osh/host/.crumb +++ b/tests/osh/host/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-30T14:59:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:4c16e4211dbc6411b14f2cc0ae0c7201c6cbc6275c0d8aac8d24a00dc1fe9c99", + "digest": "sha256:484f30e60a7d356b4c35eb50a1ff102719cb668b7c88e40ffe9736eb59d321f3", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:eeecdfe30e23c599b459a0736d2be00beb21d2a633519c1bfd504930a2cde4b4" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:f1b0ac1936be7c65f8d84130c8898ea40b39ee95e16bfb6d946c5879a7c72391" } }, "name": "host-tests", diff --git a/tests/osh/host/test_osh_host.c b/tests/osh/host/test_osh_host.c index c4469fe83..3cb6d4f04 100644 --- a/tests/osh/host/test_osh_host.c +++ b/tests/osh/host/test_osh_host.c @@ -736,9 +736,9 @@ static void test_builtins(void) o = t_out(&t); CHECK(strcmp(o, "over\n") == 0 && strcmp(osh_var_get(&t.s, "SA"), "one") == 0, "prefix over shell var '%s'", o); free(o); - /* env is built fresh: exactly the exported table plus the override, nothing from the test process environ */ + /* env is built fresh: exactly the exported table plus the override plus _ (omega#344), nothing from the test process environ */ t_clear(&t); - int nexp = 0; + int nexp = 1; for (size_t i = 0; i < t.s.nvars; i++) nexp += t.s.vars[i].exported && t.s.vars[i].value; setenv("LEAK_FROM_PROCESS_ENV", "1", 1); b = nb(0, 0); @@ -1544,7 +1544,7 @@ static void test_session_env(void) CHECK(osh_var_get(&s, "NOEQ") == NULL && osh_var_get(&s, "9x") == NULL && osh_var_get(&s, "A B") == NULL, "malformed entries are not variables"); CHECK(osh_var_get(&s, "PWD") != NULL, "PWD set"); OshAssign ov[2] = {{"A", "9"}, {"Z", "z"}}; - char **e = osh_build_envp(&s, ov, 2); + char **e = osh_build_envp(&s, ov, 2, NULL); int a = 0, z = 0, n = 0, raw = 0; for (char **p = e; *p; p++) { n++; a += !strcmp(*p, "A=9"); z += !strcmp(*p, "Z=z"); raw += !strcmp(*p, "9x=1") + !strcmp(*p, "A B=1") + !strcmp(*p, "BASH_FUNC_f%%=() { echo hi; }"); } @@ -1559,10 +1559,17 @@ static void test_session_env(void) osh_envp_free(e); CHECK(osh_var_set(&s, "bad name", "x") < 0 && osh_var_export(&s, "", NULL) < 0, "invalid names refused"); osh_var_set(&s, "U", "u"); - e = osh_build_envp(&s, NULL, 0); + e = osh_build_envp(&s, NULL, 0, NULL); int found = 0; - for (char **p = e; *p; p++) found += !strncmp(*p, "U=", 2); - CHECK(found == 0, "unexported variable not in envp"); + for (char **p = e; *p; p++) found += !strncmp(*p, "U=", 2) + !strncmp(*p, "_=", 2); + CHECK(found == 0, "unexported variable not in envp; no _ without a program"); + osh_envp_free(e); + /* omega#344: bash gives every program _=, over an assigned or inherited _ */ + OshAssign us[1] = {{"_", "/x/y"}}; + e = osh_build_envp(&s, us, 1, "/usr/bin/env"); + int u = 0, uok = 0; + for (char **p = e; *p; p++) { u += !strncmp(*p, "_=", 2); uok += !strcmp(*p, "_=/usr/bin/env"); } + CHECK(u == 1 && uok == 1, "_ is the executed path, once (%d %d)", u, uok); osh_envp_free(e); osh_session_free(&s); } diff --git a/tests/osh/vectors/README.md b/tests/osh/vectors/README.md index 480c5d82e..c3a56775d 100644 --- a/tests/osh/vectors/README.md +++ b/tests/osh/vectors/README.md @@ -30,7 +30,7 @@ v016 to v020 were added in omega PR #336 for the codes 236 `GROUP`, 246 `BRACE_E ## Bash-verified vectors (omega PR #336 review fixes) -`bash_vectors.tsv` holds 52 lexer vectors whose expectations come from real GNU bash 5.2, not from the reference tokenizer. Columns (TAB separated): id, input, expect, equiv. `bash_verify.sh` runs both the input and its continuation-free equivalent (or, when equiv is `-`, the script rendered from the expected tokens) under bash and requires identical transcripts (stdout, stderr, files written); the result is recorded in `bash_verified.log` (first line: the bash version, 5.2.21 when written). `make osh-bash-verify` runs it. The lexer driver then checks the lexer against the same expect column and also lexes the equivalent script, so the OSC lexer, the C reference and bash are three separate oracles for backslash-newline handling. +`bash_vectors.tsv` holds 60 lexer vectors whose expectations come from real GNU bash 5.2, not from the reference tokenizer. Columns (TAB separated): id, input, expect, equiv. `bash_verify.sh` runs both the input and its continuation-free equivalent (or, when equiv is `-`, the script rendered from the expected tokens) under bash and requires identical transcripts (stdout, stderr, files written); the result is recorded in `bash_verified.log` (first lines: the bash version and path, 5.2.21 when written, and the pinned environment). The script re-executes itself under `env -i` with `PATH=/usr/bin:/bin LC_ALL=C TZ=UTC` (TMPDIR kept), so the caller's locale and exported variables cannot change the reference answers (omega#344). `make osh-bash-verify` runs it. The lexer driver then checks the lexer against the same expect column and also lexes the equivalent script, so the OSC lexer, the C reference and bash are three separate oracles for backslash-newline handling. Findings pinned here: backslash-newline is removed before the next byte is classified, mid-operator (`|\|` is `||`, `a>\>f` is `>>`, `2\>f` redirects fd 2, `1\2>f` is fd 12), after `$` (`$\$`, `"$\(`, `$\{`), in tilde and brace detection, inside double quotes, and not inside single quotes. A backslash in a comment is literal: `# c\e b` runs `e b`. `x+=1` is an append assignment and `$_` the last-argument parameter; both are refused (248 `APPEND_ASSIGN`, 225 `SPECIAL_PARAM`). diff --git a/tests/osh/vectors/bash_verified.log b/tests/osh/vectors/bash_verified.log index 4fcb019fc..67569ad2f 100644 --- a/tests/osh/vectors/bash_verified.log +++ b/tests/osh/vectors/bash_verified.log @@ -1,4 +1,5 @@ -bash: GNU bash, version 5.2.21(1)-release (aarch64-unknown-linux-gnu) +bash: GNU bash, version 5.2.21(1)-release (aarch64-unknown-linux-gnu) (/usr/bin/bash) +env: env -i, LC_ALL=C TZ=UTC PATH=/usr/bin:/bin file: bash_vectors.tsv c01 same out:[a][b]|errmark|out:[x=]|errmark c02 same out:[a]|errmark|out:[x=]|errmark diff --git a/tests/osh/vectors/bash_verify.sh b/tests/osh/vectors/bash_verify.sh index 68dac5d75..51c57b6c5 100755 --- a/tests/osh/vectors/bash_verify.sh +++ b/tests/osh/vectors/bash_verify.sh @@ -19,6 +19,10 @@ # script rendered from the expected tokens when the equiv column is "-") and requires identical transcripts # (stdout, stderr, and the files written). Usage: bash_verify.sh [bash_vectors.tsv] Writes bash_verified.log. set -u +# omega#344: the reference run is pinned, so a caller's locale, PATH, TZ or exported variables cannot change bash's answers +if [ "${BASH_VERIFY_PINNED:-}" != 1 ]; then + exec env -i BASH_VERIFY_PINNED=1 PATH=/usr/bin:/bin LC_ALL=C TZ=UTC TMPDIR="${TMPDIR:-/tmp}" bash "$0" "$@" +fi here=$(cd "$(dirname "$0")" && pwd) tsv=${1:-$here/bash_vectors.tsv} ver=$(bash --version | head -1) @@ -57,7 +61,7 @@ render() { # expect tokens -> script } n=0; bad=0 log="$here/bash_verified.log" -{ echo "bash: $ver"; echo "file: bash_vectors.tsv"; } > "$log" +{ echo "bash: $ver ($(command -v bash))"; echo "env: env -i, LC_ALL=C TZ=UTC PATH=/usr/bin:/bin"; echo "file: bash_vectors.tsv"; } > "$log" while IFS=$'\t' read -r id inp exp eq; do case $id in c*) ;; *) continue ;; esac n=$((n + 1)) From 3c58db43a46399a2656212130edbcc92e34d3073 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 30 Oct 2026 11:04:21 -0500 Subject: [PATCH 2/2] crumb: refresh after osh follow-ups (Refs #344) crumb compile . on the PR head; crumb verify . was the AIEN Checks failure. --- .crumb | 20 ++++++++++---------- tests/.crumb | 16 ++++++++-------- tests/osh/.crumb | 12 ++++++------ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.crumb b/.crumb index 1348ac779..d89ee9e60 100644 --- a/.crumb +++ b/.crumb @@ -60,7 +60,7 @@ }, { "digest": "177b531107c9f65b903af316a14356d946155ca5967eb1f54539b3191b2a3701", - "last_commit": "1f320caad", + "last_commit": "d43d19b44", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -78,13 +78,13 @@ }, { "digest": "bdfaf2a0933b765205e4911249e7bdfb2503920525135bd991c85cc4f8c066c3", - "last_commit": "089e5b053", + "last_commit": "d43d19b44", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "e9f9bfdd80e39e62ac58004ecc90d27bc3a6f127b05a81420451ccda4fcb3a8e", - "last_commit": "62d1d0d66", + "digest": "5c12ee199edecd5f094228c2ad367150413aaaad738776315941099706b2b245", + "last_commit": "d43d19b44", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -95,15 +95,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:a852b0b77586a9765794f5aba537200796760b4b611bb9d7767ecbccf41d9ca7", - "compiled_at": "2026-10-30T14:59:08Z", + "children_root": "sha256:d1271b2763abb0b8d075016fe06d6e33070e7d4b67d8d12b8e6efd30d5eeadf3", + "compiled_at": "2026-10-30T16:03:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:19eec38e8df61580f796493ce198f21f1ed9cb32f4aadd0df8f59d455b3259ba", + "digest": "sha256:70a64eaf3f9cc084b74e6295b69b2be76c7847b8479e260f31d9647e6453d27b", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", - "source_tree": "sha256:87e2ba7fd03858c9b1675b0163c114c64860bc0006bc3159b8dc1de10ee353de" + "files": 17851, + "generated_at_commit": "d43d19b44e56bf57d49f87c184a23551dfe215be", + "source_tree": "sha256:8d754af81a9f587fffd11af78325b47792719d7ef8d5b0b2da30fd372be30b22" }, "owner_lane": "", "provenance": { diff --git a/tests/.crumb b/tests/.crumb index bfaa36051..a283f644e 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -199,8 +199,8 @@ "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { - "digest": "1aeab35b1d2e714d54d6ce800141ae16c00434ed43c233e8d2e5dbfb5fe9d60c", - "last_commit": "d863780eb", + "digest": "628d85f858a9ed11e4502064192aeb797bf9fe6f73a3932db520d5b9c22d43f1", + "last_commit": "d43d19b44", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, @@ -301,15 +301,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:55f44a3f8770aafc677178ad5b04d273aaca550c8c10541ce97a9f34c27517df", - "compiled_at": "2026-10-30T14:59:08Z", + "children_root": "sha256:5e908f9e157f84f765b089230efb9b20c98bb844d1503c598ae17843c93c1d0e", + "compiled_at": "2026-10-30T16:03:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:e9f9bfdd80e39e62ac58004ecc90d27bc3a6f127b05a81420451ccda4fcb3a8e", + "digest": "sha256:5c12ee199edecd5f094228c2ad367150413aaaad738776315941099706b2b245", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1386, - "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", - "source_tree": "sha256:74ded732f0fe8b3a71dde459b845cbf1532540efa1ddf411371ee7aa1cabac7b" + "files": 1388, + "generated_at_commit": "d43d19b44e56bf57d49f87c184a23551dfe215be", + "source_tree": "sha256:08628e3bcc2482cf6897e1a14fda591d56b4c19c746725e8e69219ee04262175" }, "owner_lane": "", "provenance": { diff --git a/tests/osh/.crumb b/tests/osh/.crumb index b2f7e9ea3..92bf4ddd1 100644 --- a/tests/osh/.crumb +++ b/tests/osh/.crumb @@ -10,7 +10,7 @@ }, { "digest": "484f30e60a7d356b4c35eb50a1ff102719cb668b7c88e40ffe9736eb59d321f3", - "last_commit": "d863780eb", + "last_commit": "d43d19b44", "name": "host", "purpose": "tests for the osh Linux host adapter execution service (hand-built request records, helper-mode " }, @@ -22,14 +22,14 @@ } ], "children_root": "sha256:991d9ea1d859fa961358d0c05581a46b7a5cfdb764fefbfeaeaef9a3da2d02da", - "compiled_at": "2026-10-30T14:59:08Z", + "compiled_at": "2026-10-30T16:03:29Z", "compiler": "crumb-compile/1", - "digest": "sha256:1aeab35b1d2e714d54d6ce800141ae16c00434ed43c233e8d2e5dbfb5fe9d60c", + "digest": "sha256:628d85f858a9ed11e4502064192aeb797bf9fe6f73a3932db520d5b9c22d43f1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 93, - "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", - "source_tree": "sha256:ee3c74320576e32046abccd58f37fea059ab03af87cba9729415a8303ad4bc1c" + "files": 95, + "generated_at_commit": "d43d19b44e56bf57d49f87c184a23551dfe215be", + "source_tree": "sha256:0d673dfc4dc9027d4e3cd5b1179998d4f9c9cb20aeb07ad2b8d3d6b17c2e981b" } }, "name": "osh-tests",