Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/41f903dfb18bf90b # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#390 (code only) closed derived `41f903dfb18bf90b` #390 Imported from GitHub aien-dev/omega-mv#390 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `8bf4a8b22b27d3638477ffb681ee3ff62b37502e` · Head `d57348d84bb3daab98935ca87c1c728624ec9cd0` ### Apply it ``` git checkout -b patch-41f903dfb18bf90b 8bf4a8b22b27d3638477ffb681ee3ff62b37502e curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/41f903dfb18bf90b.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 390 d57348d84bb3daab98935ca87c1c728624ec9cd0 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":390,"url":"https://github.com/aien-dev/omega-mv/pull/390","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:41Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index fbf9f8206..6e46d7e3b 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "8379cb7322fcc5ebb6053b520eeb441d78497389d692153a9d24e5f5bc6e62fa", - "last_commit": "5503cc2c", + "digest": "81c465bd45aafe66e9828a3b54323563dd3a72593e381f373d2d007cee28dfb2", + "last_commit": "ad2deba1", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "af422cd2fc56b4bd0379689500fd28602449f85b9d8e2a0497aee39977d0d91e", - "last_commit": "5503cc2c", + "digest": "69563c6c84356889dff36ed5a28efa384795a3f19e23bcfd606abd8cd47e48fe", + "last_commit": "ad2deba1", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:6b1d51c1c03faeafe9995d138f7d871c2dc9599a4eb1c6bbac5a1885a0bb73a9", - "compiled_at": "2026-10-10T07:35:04Z", + "children_root": "sha256:50a7a4b6addb9f5eace5530fd8e76977303c4155f49f1d79fbf2da3277847fbc", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:3c7bc245fc5a684091e0bf3ddc09983137031f7f4d2435659948d9052131e022", + "digest": "sha256:2dff1d6f02a124cf2540b6c7a5fc7c22f747fda369d794413a87ea96c88fc966", "evidence_files": 6829, "evidence_root": "sha256:d430e46c9eac00396e5c58c2c8e60eb961500973312cc5eee25cf9d37e75bea2", "files": 9236, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:beb8771d7aae4a2789da8907acad04ffc091f465af3ba06f073a43ae6ad6ba25" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:6fccf760496c624091b593eb1247cc4050cdf187df5385b50b1757e308a1e5e0" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 320f372ff..e512f6084 100644 --- a/src/.crumb +++ b/src/.crumb @@ -115,8 +115,8 @@ "purpose": "Directory src/language. Purpose not yet described by a human." }, { - "digest": "93c5e6270b680c1aef7e311adc56fed4942f41db16d21108af4703e1e323b3d5", - "last_commit": "5503cc2c", + "digest": "f292900401c59fe21e55b2d83f01e2dc4bc1d16b2f4a90519fe3325534c34f18", + "last_commit": "ad2deba1", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, @@ -175,15 +175,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:ee7444d0238f1b94cc0fc32388d8514f5212e265247b084b912d0f68eaa6c348", - "compiled_at": "2026-10-10T07:35:04Z", + "children_root": "sha256:357a40230e26404bf5f877318da0b70108a5ad2721029459767f8f84fb2413b5", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:8379cb7322fcc5ebb6053b520eeb441d78497389d692153a9d24e5f5bc6e62fa", + "digest": "sha256:81c465bd45aafe66e9828a3b54323563dd3a72593e381f373d2d007cee28dfb2", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 445, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:fb059cf960d99aee3f406c6418c7f9292db626309773349f72941358548c9efa" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:c9b7f02b3ca1b83de33415f110466c5eeafded672cac9f29703693604ff24e02" }, "owner_lane": "", "provenance": { diff --git a/src/osh/.crumb b/src/osh/.crumb index 4c3433b6c..bce35eb96 100644 --- a/src/osh/.crumb +++ b/src/osh/.crumb @@ -3,21 +3,21 @@ "generated": { "children": [ { - "digest": "59c39a686b6d2ed147d462e7bd2836de719d54886b10ee285d86fb5db53f7453", - "last_commit": "5503cc2c", + "digest": "656126a21229ac434420f7f00f0f7133925d87f6719a704d37437a6464d3b8d1", + "last_commit": "ad2deba1", "name": "host", "purpose": "osh Linux host: execution service (request decoder, resolution, builtins, redirections, pipeline" } ], - "children_root": "sha256:9943bf9a0ecc2d5b20be13b8ae1513e0507802b4986c5b98a4f864e4e9f0d46f", - "compiled_at": "2026-10-10T07:35:04Z", + "children_root": "sha256:67270aa2cd0e85d3861f60a2b3d9665e42302d03c952e1ef029956b6c3a2a0ec", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:93c5e6270b680c1aef7e311adc56fed4942f41db16d21108af4703e1e323b3d5", + "digest": "sha256:f292900401c59fe21e55b2d83f01e2dc4bc1d16b2f4a90519fe3325534c34f18", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 31, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:29abf5c74c53a12fda5496bbf1cb22873c54a2088949ba2ec4c22c4efcb56db5" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:075fc4a95f2878a4356755046f293d825f999a80988d0095f77abc4cd3c07b1d" } }, "name": "osh", diff --git a/src/osh/host/.crumb b/src/osh/host/.crumb index e87600e31..15926fbf5 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-10T07:35:04Z", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:59c39a686b6d2ed147d462e7bd2836de719d54886b10ee285d86fb5db53f7453", + "digest": "sha256:656126a21229ac434420f7f00f0f7133925d87f6719a704d37437a6464d3b8d1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 16, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:41d4638f591f65202b2cc6eb123c9c9d93b53002b869388a7cd552c44c9068d5" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:048a7ecae865ea459512f8f51434504cf10d2a48a4bee33fd9b6bdc78cde2977" } }, "name": "host", diff --git a/src/osh/host/osh_builtin.c b/src/osh/host/osh_builtin.c index 238759d9e..ef391f7f1 100644 --- a/src/osh/host/osh_builtin.c +++ b/src/osh/host/osh_builtin.c @@ -20,6 +20,7 @@ #include "osh_host.h" #include "osh_priv.h" +#include #include #include #include @@ -214,7 +215,217 @@ static void out_bytes(Out *o, const char *p, size_t n) } } -static int is_conv(char ch) { return ch && strchr("sdiuxoc", ch) != NULL; } +/* printf FORMAT [ARG...] in bash 5.2's shape (#344 RISK-4): flags - + space # 0, width and precision as digits or + * `*` (taken from the arguments), length modifiers h l j z t L ignored as bash does, conversions + * d i o u x X c s b q e E f F g G a A and %%. The format is reused while arguments remain. %s %c %b %q pad with + * spaces even under the 0 flag (bash); %b expands \a \b \e \E \f \n \r \t \v \\ \' \" \0nnn \nnn \xHH \uHHHH + * \UHHHHHHHH and stops all output at \c; %q quotes for reuse as shell input (bash's backslash form, or $'...' + * when a control byte is present). Every conversion is validated before anything is printed (osh refuses where + * bash would print the prefix first). osh limit, not bash's: a width or precision above 1000000 is refused. */ +static int is_conv(char ch) { return ch && strchr("diouxXcsbqeEfFgGaA", ch) != NULL; } + +typedef struct { + int minus, plus, space, alt, zero; + int width, prec; /* -1: not given */ + int star_w, star_p; /* taken from the arguments */ + char conv; +} PfSpec; + +enum { PF_MAX_WIDTH = 1000000 }; + +/* Parse what follows a '%'. Returns the bytes consumed, or 0 with *bad set to the offending byte (NUL: the + * format ended inside the specification). */ +static int pf_parse(const char *p, PfSpec *sp, char *bad) +{ + const char *q = p; + memset(sp, 0, sizeof *sp); + sp->width = sp->prec = -1; + for (;; q++) { + if (*q == '-') sp->minus = 1; + else if (*q == '+') sp->plus = 1; + else if (*q == ' ') sp->space = 1; + else if (*q == '#') sp->alt = 1; + else if (*q == '0') sp->zero = 1; + else break; + } + if (*q == '*') { sp->star_w = 1; q++; } + else if (*q >= '1' && *q <= '9') { + int w = 0; + while (*q >= '0' && *q <= '9') { if (w <= PF_MAX_WIDTH) w = w * 10 + (*q - '0'); q++; } + sp->width = w; + } + if (*q == '.') { + q++; + if (*q == '*') { sp->star_p = 1; q++; } + else { + int pr = 0; + while (*q >= '0' && *q <= '9') { if (pr <= PF_MAX_WIDTH) pr = pr * 10 + (*q - '0'); q++; } + sp->prec = pr; + } + } + while (*q == 'h' || *q == 'l' || *q == 'j' || *q == 'z' || *q == 't' || *q == 'L') q++; + if (!is_conv(*q)) { *bad = *q; return 0; } + sp->conv = *q++; + return (int)(q - p); +} + +typedef struct { char *p; size_t n, cap; int oom; } Buf; + +static void buf_add(Buf *b, const char *s, size_t n) +{ + if (b->oom) return; + if (b->n + n + 1 > b->cap) { + size_t cap = b->cap ? b->cap : 64; + while (cap < b->n + n + 1) cap *= 2; + char *np = realloc(b->p, cap); + if (!np) { b->oom = 1; return; } + b->p = np; + b->cap = cap; + } + memcpy(b->p + b->n, s, n); + b->n += n; + b->p[b->n] = 0; +} + +static void buf_ch(Buf *b, char ch) { buf_add(b, &ch, 1); } + +/* bash's $'...' form: \a \b \t \n \v \f \r \E, \\ and \', other control bytes as three-digit octal; bytes above + * 0x7f as octal when octal_high (declare -x) or raw (%q). */ +static void ansi_c_quote(Buf *b, const char *v, int octal_high) +{ + buf_add(b, "$'", 2); + for (const unsigned char *q = (const unsigned char *)v; *q; q++) { + const char *esc = NULL; + switch (*q) { + case '\a': esc = "\\a"; break; case '\b': esc = "\\b"; break; case '\t': esc = "\\t"; break; + case '\n': esc = "\\n"; break; case '\v': esc = "\\v"; break; case '\f': esc = "\\f"; break; + case '\r': esc = "\\r"; break; case 0x1b: esc = "\\E"; break; + case '\\': esc = "\\\\"; break; case '\'': esc = "\\'"; break; + } + if (esc) { buf_add(b, esc, strlen(esc)); continue; } + if (*q < 0x20 || *q == 0x7f || (*q >= 0x80 && octal_high)) { + char oct[5]; + snprintf(oct, sizeof oct, "\\%03o", *q); + buf_add(b, oct, 4); + continue; + } + buf_ch(b, (char)*q); + } + buf_ch(b, '\''); +} + +/* %q: bash's sh_backslash_quote. Empty is ''; a control byte switches to $'...'; otherwise a backslash before + * space and the shell's special characters, and before ~ or # only at the start. Bytes above 0x7f pass raw. */ +static void quote_q(Buf *b, const char *v) +{ + if (!*v) { buf_add(b, "''", 2); return; } + for (const unsigned char *q = (const unsigned char *)v; *q; q++) + if (*q < 0x20 || *q == 0x7f) { ansi_c_quote(b, v, 0); return; } + for (const char *q = v; *q; q++) { + if (strchr(" `\"'\\$&()|;<>?![]*{},^", *q) || ((*q == '~' || *q == '#') && q == v)) buf_ch(b, '\\'); + buf_ch(b, *q); + } +} + +static void buf_utf8(Buf *b, unsigned long cp) +{ + char u[4]; + int n; + if (cp < 0x80) { u[0] = (char)cp; n = 1; } + else if (cp < 0x800) { u[0] = (char)(0xc0 | (cp >> 6)); u[1] = (char)(0x80 | (cp & 0x3f)); n = 2; } + else if (cp < 0x10000) { u[0] = (char)(0xe0 | (cp >> 12)); u[1] = (char)(0x80 | ((cp >> 6) & 0x3f)); u[2] = (char)(0x80 | (cp & 0x3f)); n = 3; } + else { u[0] = (char)(0xf0 | (cp >> 18)); u[1] = (char)(0x80 | ((cp >> 12) & 0x3f)); u[2] = (char)(0x80 | ((cp >> 6) & 0x3f)); u[3] = (char)(0x80 | (cp & 0x3f)); n = 4; } + buf_add(b, u, (size_t)n); +} + +/* %b argument: bash's escape set. Returns 1 when \c was seen (all further output stops). */ +static int expand_b(Buf *b, const char *v) +{ + for (const char *p = v; *p; p++) { + if (*p != '\\') { buf_ch(b, *p); continue; } + char e = p[1]; + static const char esc_from[] = "abeEfnrtv\\'\"", esc_to[] = "\a\b\033\033\f\n\r\t\v\\'\""; + const char *m = e ? strchr(esc_from, e) : NULL; + if (m) { buf_ch(b, esc_to[m - esc_from]); p++; continue; } + if (e == 'c') return 1; + if (e >= '0' && e <= '7') { + int v8 = 0, k = 0, lim = e == '0' ? 4 : 3; /* \0nnn: up to three digits after the 0; \nnn: up to three */ + while (k < lim && p[1] >= '0' && p[1] <= '7') { v8 = v8 * 8 + (p[1] - '0'); p++; k++; } + buf_ch(b, (char)(v8 & 0xff)); + continue; + } + if (e == 'x' || e == 'u' || e == 'U') { + int lim = e == 'x' ? 2 : e == 'u' ? 4 : 8, k = 0; + unsigned long cp = 0; + const char *q = p + 2; + while (k < lim && isxdigit((unsigned char)*q)) { cp = cp * 16 + (unsigned long)(isdigit((unsigned char)*q) ? *q - '0' : (tolower((unsigned char)*q) - 'a' + 10)); q++; k++; } + if (k == 0) { buf_ch(b, '\\'); continue; } /* no digits: backslash kept, letter follows normally */ + if (e == 'x') buf_ch(b, (char)(cp & 0xff)); else buf_utf8(b, cp); + p = q - 1; + continue; + } + buf_ch(b, '\\'); /* unknown escape: both bytes verbatim, as bash does */ + if (!e) break; + } + return 0; +} + +/* Pad with spaces to the width, honouring '-'. bash pads %s %c %b %q with spaces even under the 0 flag. */ +static void pad_out(Out *o, const char *s, size_t len, const PfSpec *sp) +{ + size_t w = sp->width > 0 ? (size_t)sp->width : 0; + if (!sp->minus) for (size_t k = len; k < w; k++) out_bytes(o, " ", 1); + out_bytes(o, s, len); + if (sp->minus) for (size_t k = len; k < w; k++) out_bytes(o, " ", 1); +} + +/* Numeric conversions go through snprintf with the spec rebuilt from the parsed fields (no `*` reaches it). */ +static int num_out(Out *o, const PfSpec *sp, intmax_t sv, uintmax_t u, long double ld) +{ + char spec[64]; + int n = 0; + spec[n++] = '%'; + if (sp->minus) spec[n++] = '-'; + if (sp->plus) spec[n++] = '+'; + if (sp->space) spec[n++] = ' '; + if (sp->alt) spec[n++] = '#'; + if (sp->zero) spec[n++] = '0'; + if (sp->width >= 0) n += snprintf(spec + n, sizeof spec - (size_t)n, "%d", sp->width); + if (sp->prec >= 0) n += snprintf(spec + n, sizeof spec - (size_t)n, ".%d", sp->prec); + int isf = strchr("eEfFgGaA", sp->conv) != NULL, isd = sp->conv == 'd' || sp->conv == 'i'; + spec[n++] = isf ? 'L' : 'j'; + spec[n++] = sp->conv; + spec[n] = 0; + int len = isf ? snprintf(NULL, 0, spec, ld) : isd ? snprintf(NULL, 0, spec, sv) : snprintf(NULL, 0, spec, u); + if (len < 0) return -1; + char *buf = malloc((size_t)len + 1); + if (!buf) return -1; + if (isf) snprintf(buf, (size_t)len + 1, spec, ld); + else if (isd) snprintf(buf, (size_t)len + 1, spec, sv); + else snprintf(buf, (size_t)len + 1, spec, u); + out_bytes(o, buf, (size_t)len); + free(buf); + return 0; +} + +/* An integer argument as bash reads it: 'x or "x is the byte value, otherwise strtoimax/strtoumax base 0; + * trailing junk or overflow is "invalid number" (status 1) with the parsed prefix (0 if none). */ +static int pf_int(const char *arg, int is_signed, intmax_t *sv, uintmax_t *u, int fd_err, int *status) +{ + *sv = 0; *u = 0; + if (!arg || !*arg) return 0; + if (arg[0] == '\'' || arg[0] == '"') { *u = (unsigned char)arg[1]; *sv = (intmax_t)*u; return 0; } + char *end; + errno = 0; + if (is_signed) *sv = strtoimax(arg, &end, 0); else *u = strtoumax(arg, &end, 0); + if (end == arg || *end || errno == ERANGE) { + if (end == arg) { *sv = 0; *u = 0; } + osh_diag(fd_err, "printf: '%s': invalid number", arg); + *status = 1; + return 1; + } + return 0; +} static int bi_printf(const OshCmd *c, const int io[3]) { @@ -227,7 +438,7 @@ static int bi_printf(const OshCmd *c, const int io[3]) } 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, nspec = 0; + 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 * backslash does: it always takes the next byte with it (a known escape, or an unknown one that is printed * verbatim, as coreutils does), so `\%` is two literal bytes and never starts a conversion. A `%` or `\` that is @@ -240,20 +451,25 @@ static int bi_printf(const OshCmd *c, const int io[3]) if (*p != '%') continue; p++; if (*p == '%') continue; - if (!*p) { - osh_diag(io[2], "printf: %%: invalid conversion specification (the format ends after %%)"); + PfSpec sp; + char bad = 0; + int k = pf_parse(p, &sp, &bad); + if (!k) { + if (!bad) osh_diag(io[2], "printf: `%%%s': missing format character", p); + else osh_diag(io[2], "printf: `%c': invalid format character", bad); return 1; } - if (!is_conv(*p)) { - osh_diag(io[2], "printf: %%%c: unsupported conversion (supported: %%s %%d %%i %%u %%x %%o %%c %%%%)", *p); + if (sp.width > PF_MAX_WIDTH || sp.prec > PF_MAX_WIDTH) { + osh_diag(io[2], "printf: width or precision above %d is not supported", PF_MAX_WIDTH); return 1; } - nspec++; + nargs += 1 + sp.star_w + sp.star_p; + p += k - 1; } Out o = {.fd = io[1]}; - int status = 0, ci = first; + int status = 0, ci = first, stop = 0; do { - for (const char *p = fmt; *p; p++) { + for (const char *p = fmt; *p && !stop; p++) { if (*p == '\\') { char e = p[1]; static const char esc_from[] = "\\abfnrtv\"", esc_to[] = "\\\a\b\f\n\r\t\v\""; @@ -277,45 +493,61 @@ static int bi_printf(const OshCmd *c, const int io[3]) } else { p++; if (*p == '%') { out_bytes(&o, "%", 1); continue; } - if (!*p) break; /* unreachable: the scan refuses a final %; never step past the terminator */ + PfSpec sp; + char bad; + int k = pf_parse(p, &sp, &bad); + if (!k) break; /* unreachable: the scan admitted every specification */ + p += k - 1; + if (sp.star_w) { + intmax_t sv; uintmax_t u; + pf_int(ci < c->nargv ? c->argv[ci++] : NULL, 1, &sv, &u, io[2], &status); + if (sv < 0) { sp.minus = 1; sv = -sv; } + sp.width = sv > PF_MAX_WIDTH ? PF_MAX_WIDTH : (int)sv; + } + if (sp.star_p) { + intmax_t sv; uintmax_t u; + pf_int(ci < c->nargv ? c->argv[ci++] : NULL, 1, &sv, &u, io[2], &status); + sp.prec = sv < 0 ? -1 : sv > PF_MAX_WIDTH ? PF_MAX_WIDTH : (int)sv; + } const char *arg = ci < c->nargv ? c->argv[ci++] : NULL; - if (*p == 's') { - if (arg) out_bytes(&o, arg, strlen(arg)); - } else if (*p == 'c') { - out_bytes(&o, arg && *arg ? arg : "", 1); /* coreutils: an empty or missing argument prints a NUL byte */ - } else { - char num[64]; - int bad = 0; - errno = 0; - uintmax_t u = 0; - intmax_t sv = 0; - if (arg && (arg[0] == '\'' || arg[0] == '"')) { - u = (unsigned char)arg[1]; - sv = (intmax_t)u; - } else if (arg && *arg) { + if (sp.conv == 's') { + size_t len = arg ? strlen(arg) : 0; + if (sp.prec >= 0 && (size_t)sp.prec < len) len = (size_t)sp.prec; + pad_out(&o, arg ? arg : "", len, &sp); + } else if (sp.conv == 'c') { + char ch = arg && *arg ? arg[0] : 0; /* coreutils: an empty or missing argument prints a NUL byte */ + pad_out(&o, &ch, 1, &sp); + } else if (sp.conv == 'b' || sp.conv == 'q') { + Buf b = {0}; + if (sp.conv == 'b') stop = expand_b(&b, arg ? arg : ""); + else quote_q(&b, arg ? arg : ""); + if (b.oom) { free(b.p); osh_diag(io[2], "printf: out of memory"); out_flush(&o); return 1; } + size_t len = b.n; + if (sp.prec >= 0 && (size_t)sp.prec < len) len = (size_t)sp.prec; + pad_out(&o, b.p ? b.p : "", len, &sp); + free(b.p); + } else if (strchr("eEfFgGaA", sp.conv)) { + long double ld = 0; + if (arg && (arg[0] == '\'' || arg[0] == '"')) ld = (unsigned char)arg[1]; + else if (arg && *arg) { char *end; - if (*p == 'd' || *p == 'i') sv = strtoimax(arg, &end, 0); - else u = strtoumax(arg, &end, 0); - if (end == arg || *end || errno == ERANGE) bad = 1; - if (bad && end == arg) { u = 0; sv = 0; } - } - if (bad) { - osh_diag(io[2], "printf: '%s': invalid number", arg); - status = 1; + errno = 0; + ld = strtold(arg, &end); + if (end == arg || *end || errno == ERANGE) { + if (end == arg) ld = 0; + osh_diag(io[2], "printf: '%s': invalid number", arg); + status = 1; + } } - int n; - switch (*p) { - case 'd': case 'i': n = snprintf(num, sizeof num, "%jd", sv); break; - case 'u': n = snprintf(num, sizeof num, "%ju", u); break; - case 'x': n = snprintf(num, sizeof num, "%jx", u); break; - case 'o': n = snprintf(num, sizeof num, "%jo", u); break; - default: n = 0; break; /* unreachable: the scan admitted only d i u x o here */ - } - out_bytes(&o, num, (size_t)n); + if (num_out(&o, &sp, 0, 0, ld)) { osh_diag(io[2], "printf: out of memory"); out_flush(&o); return 1; } + } else { + intmax_t sv; uintmax_t u; + pf_int(arg, sp.conv == 'd' || sp.conv == 'i', &sv, &u, io[2], &status); + if (num_out(&o, &sp, sv, u, 0)) { osh_diag(io[2], "printf: out of memory"); out_flush(&o); return 1; } } } } - } while (nspec > 0 && ci < c->nargv); + } while (!stop && nargs > 0 && ci < c->nargv); out_flush(&o); if (o.err) { osh_diag(io[2], "printf: write error: %s", strerror(o.err)); return 1; } return status; @@ -329,7 +561,7 @@ static int cmp_var(const void *a, const void *b) } /* `declare -x NAME="value"` quoting as bash 5.2 prints it: double quotes with \ before " \ $ `; when the value holds - * a control or non-ASCII byte, the $'...' form with \a \b \t \n \v \f \r \E, \\ \' and three-digit octal otherwise. */ + * a control or non-ASCII byte, bash's $'...' form (ansi_c_quote, octal for bytes above 0x7f). */ static void out_declare_value(Out *o, const char *v) { int ansi = 0; @@ -344,25 +576,10 @@ static void out_declare_value(Out *o, const char *v) out_bytes(o, "\"", 1); return; } - out_bytes(o, "$'", 2); - for (const unsigned char *q = (const unsigned char *)v; *q; q++) { - const char *esc = NULL; - switch (*q) { - case '\a': esc = "\\a"; break; case '\b': esc = "\\b"; break; case '\t': esc = "\\t"; break; - case '\n': esc = "\\n"; break; case '\v': esc = "\\v"; break; case '\f': esc = "\\f"; break; - case '\r': esc = "\\r"; break; case 0x1b: esc = "\\E"; break; - case '\\': esc = "\\\\"; break; case '\'': esc = "\\'"; break; - } - if (esc) { out_bytes(o, esc, strlen(esc)); continue; } - if (*q < 0x20 || *q >= 0x7f) { - char oct[5]; - snprintf(oct, sizeof oct, "\\%03o", *q); - out_bytes(o, oct, 4); - continue; - } - out_bytes(o, (const char *)q, 1); - } - out_bytes(o, "'", 1); + Buf b = {0}; + ansi_c_quote(&b, v, 1); + if (!b.oom) out_bytes(o, b.p, b.n); + free(b.p); } /* export [-fn] [name[=value] ...] | export -p (bash 5.2 shape, #344 RISK-6) diff --git a/tests/.crumb b/tests/.crumb index e8f1edf4a..e49c3c353 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": "c0d62dbbf30e78826e3b7bb57947454a6a62f87a3af37e54c9cc67e9fad5f409", - "last_commit": "5503cc2c", + "digest": "fdb271e4015b2ba0004bbbd97261ae37690b1d713b642d3290054db5d06721e0", + "last_commit": "ad2deba1", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, @@ -289,15 +289,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:2fb52503bdfa2c6e412a51b31ec4ba70e304ad90d78e3ede91b04b32442b861a", - "compiled_at": "2026-10-10T07:35:04Z", + "children_root": "sha256:8ff24c5fce48e75b0d39a3bcc891a8b5b0881a0a5427a2a695c45b06742e396a", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:af422cd2fc56b4bd0379689500fd28602449f85b9d8e2a0497aee39977d0d91e", + "digest": "sha256:69563c6c84356889dff36ed5a28efa384795a3f19e23bcfd606abd8cd47e48fe", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 948, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:3f1a874529f064f2be86c711ef9e0e02b55f66af0b24333bb340ae6b05572bac" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:f8ec8d473ca1952f1cd8f5f0affbb673c040fb6cc9967fdb4dfa4866f78acf74" }, "owner_lane": "", "provenance": { diff --git a/tests/osh/.crumb b/tests/osh/.crumb index f6b0cb3aa..eca97df81 100644 --- a/tests/osh/.crumb +++ b/tests/osh/.crumb @@ -9,8 +9,8 @@ "purpose": "end-to-end tests of the osh program: argprint helper plus a runner comparing native and OSH_INTE" }, { - "digest": "8541cc8e514c2e45b9dcf90300854e41c29faedea2fccc4fb26084ca97efe136", - "last_commit": "5503cc2c", + "digest": "08b6e29a189e9728d7bb8241b4ab9c2c08ba279a1665a469fb32eb37bf408b56", + "last_commit": "ad2deba1", "name": "host", "purpose": "tests for the osh Linux host adapter execution service (hand-built request records, helper-mode " }, @@ -21,15 +21,15 @@ "purpose": "Core-only step traces of the osh shell units over fixture scripts (OSH-AIENOS-0 differential fix" } ], - "children_root": "sha256:5319bc087019becf6da3193bccf34fca5083663f77f282ff14e3e9236c724e1b", - "compiled_at": "2026-10-10T07:35:04Z", + "children_root": "sha256:cd6d93e1cff097a38c8250a4f12f62c7522b91e2b00a5f4af888202eab943f76", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:c0d62dbbf30e78826e3b7bb57947454a6a62f87a3af37e54c9cc67e9fad5f409", + "digest": "sha256:fdb271e4015b2ba0004bbbd97261ae37690b1d713b642d3290054db5d06721e0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 93, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:a89fa817be2b33aef0942a199aacbcc2a9a04b975515016cfcf81e74f82944ef" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:814a75379938d3c43a383ff5fe48e50ef435844a3bcd32fb065f5fdfe65c1353" } }, "name": "osh-tests", diff --git a/tests/osh/host/.crumb b/tests/osh/host/.crumb index 02104d627..325210e50 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-10T07:35:04Z", + "compiled_at": "2026-10-10T07:48:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:8541cc8e514c2e45b9dcf90300854e41c29faedea2fccc4fb26084ca97efe136", + "digest": "sha256:08b6e29a189e9728d7bb8241b4ab9c2c08ba279a1665a469fb32eb37bf408b56", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "5503cc2c90f6154a7a6f191d39e081f63b728669", - "source_tree": "sha256:70c4774cb969d11c064e5e3e16e14a99e8571b97c1a28cbad4e36898d64ae592" + "generated_at_commit": "ad2deba162c8d30b31e1e829b9cd58aa5513b1fd", + "source_tree": "sha256:a7f3f5437d801280e3744a27a373b239c5e511ba4d0d754da65465ef52d105df" } }, "name": "host-tests", diff --git a/tests/osh/host/test_osh_host.c b/tests/osh/host/test_osh_host.c index 4a0a0c1c0..fa55eadd0 100644 --- a/tests/osh/host/test_osh_host.c +++ b/tests/osh/host/test_osh_host.c @@ -768,17 +768,76 @@ static void test_printf(void) printf_case(&t, "%s", "x", "y", "z", NULL, NULL, NULL); printf_case(&t, "100%%\n", NULL, NULL, NULL, NULL, NULL, NULL); printf_case(&t, "\\0101\n", NULL, NULL, NULL, NULL, NULL, NULL); - /* outside the documented subset: refused, nothing written, status 1 */ + /* widths, flags, precision, * from arguments, length modifiers, floats, %b and %q, measured against bash 5.2.21 + * (#344 RISK-4). bash pads %s with spaces even under the 0 flag. */ OshResult r; t_clear(&t); OshBuilder *b = nb(0, 0); - cmd(b, OSH_B_PRINTF, "printf", "%5d\n", "3", NULL); - CHECK(go(&t, b, &r) == 1, "width refused"); + cmd(b, OSH_B_PRINTF, "printf", "[%5d][%-4s][%.2s][%05d][%+d][% d][%#x][%#o][%*d][%.*s][%05s][%X][%ld %lld %hd]\n", + "3", "ab", "xyz", "42", "7", "7", "255", "8", "4", "9", "2", "hello", "cd", "255", "1", "2", "3", NULL); + CHECK(go(&t, b, &r) == 0, "widths status"); char *o = t_out(&t); - CHECK(o[0] == 0, "nothing printed on refusal, got '%s'", o); + CHECK(strcmp(o, "[ 3][ab ][xy][00042][+7][ 7][0xff][010][ 9][he][ cd][FF][1 2 3]\n") == 0, "widths/flags/precision as bash, got '%s'", o); free(o); t_clear(&t); b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%5.2f|%e|%g|%08.3f|%G|%.0d|%+.2d|%5c|%-3c|\n", "3.14159", "2.5", "0.0001", "3.14159", "4", "0", "3", "x", "y", NULL); + CHECK(go(&t, b, &r) == 0, "floats status"); + o = t_out(&t); + CHECK(strcmp(o, " 3.14|2.500000e+00|0.0001|0003.142|4||+03| x|y |\n") == 0, "floats and char widths as bash, got '%s'", o); + free(o); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%b|%5.3b|%-4b|%b|%b\n", "a\\tb\\0101\\x41\\u00e9", "abcdef", "xy", "\\e\\E", "\\q\\\\", NULL); + CHECK(go(&t, b, &r) == 0, "%%b status"); + o = t_out(&t); + CHECK(strcmp(o, "a\tbAA\xc3\xa9| abc|xy |\033\033|\\q\\\n") == 0, "%%b escapes, width and precision as bash, got '%s'", o); + free(o); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "a%bz\n", "x\\cy", NULL); + CHECK(go(&t, b, &r) == 0, "%%b \\c status"); + o = t_out(&t); + CHECK(strcmp(o, "ax") == 0, "%%b \\c stops all output, got '%s'", o); + free(o); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%q|%q|%q|%q|%q|%q|%q\n", "a b", "it's", "", "#x~", "a\tb", "x#y,z{w}", "~p", NULL); + CHECK(go(&t, b, &r) == 0, "%%q status"); + o = t_out(&t); + CHECK(strcmp(o, "a\\ b|it\\'s|''|\\#x~|$'a\\tb'|x#y\\,z\\{w\\}|\\~p\n") == 0, "%%q quotes as bash, got '%s'", o); + free(o); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%*d|\n", "x", "5", NULL); /* bad * width: diagnostic, width 0, status 1 */ + CHECK(go(&t, b, &r) == 1, "bad * width status 1"); + o = t_out(&t); + char *e = t_err(&t); + CHECK(strcmp(o, "5|\n") == 0 && strstr(e, "invalid number"), "bad * width prints with width 0, got '%s' / '%s'", o, e); + free(o); + free(e); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%5%\n", NULL); /* bash: `%': invalid format character */ + CHECK(go(&t, b, &r) == 1, "%%5%% refused"); + o = t_out(&t); + e = t_err(&t); + CHECK(o[0] == 0 && strstr(e, "`%': invalid format character"), "%%5%% nothing printed, bash's diagnostic, got '%s' / '%s'", o, e); + free(o); + free(e); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%5", "x", NULL); + CHECK(go(&t, b, &r) == 1, "%%5 at the end refused"); + e = t_err(&t); + CHECK(strstr(e, "missing format character") != NULL, "%%5 at the end: bash's diagnostic, got '%s'", e); + free(e); + t_clear(&t); + b = nb(0, 0); + cmd(b, OSH_B_PRINTF, "printf", "%2000000d\n", "1", NULL); /* osh limit, documented in the builtin header */ + CHECK(go(&t, b, &r) == 1, "absurd width refused"); + t_clear(&t); + b = nb(0, 0); cmd(b, OSH_B_PRINTF, "printf", "a\\qb\n", NULL); go(&t, b, &r); o = t_out(&t); @@ -1874,25 +1933,29 @@ static const PfRow k_pf_same[] = { {"%s", "-", NULL, NULL}, {"\\a\\b\\f\\r\\v", NULL, NULL, NULL}, {"\\q\\z\\n", NULL, NULL, NULL}, + {"%5d\\n", "3", NULL, NULL}, + {"%-3s|\\n", "a", NULL, NULL}, + {"%.2s\\n", "abc", NULL, NULL}, + {"%ld\\n", "5", NULL, NULL}, + {"%X\\n", "255", NULL, NULL}, + {"%b\\n", "a", NULL, NULL}, + {"%e\\n", "1", NULL, NULL}, + {"%q\\n", "5", NULL, NULL}, + {"ab%q", "5", NULL, NULL}, + {"%s%q\\n", "x", "y", NULL}, + {"%5.2f|%-6s|%+.3d|%08.3f\\n", "3.14159", "ab", "5"}, + {"%b|%b\\n", "a\\tb\\0101", "x\\\\y", NULL}, + {"%*d|%-*d|\\n", "3", "7", "2"}, + {"%#x %#o % d\\n", "255", "8", "4"}, }; /* REFUSE: outside the documented subset or malformed. The builtin refuses BEFORE printing anything and returns 1; * /usr/bin/printf prints what precedes the bad conversion, then fails (also 1). Intentional difference. */ static const PfRow k_pf_refuse[] = { - {"%q\\n", "5", NULL, NULL}, - {"ab%q", "5", NULL, NULL}, - {"%s%q\\n", "x", "y", NULL}, {"%", NULL, NULL, NULL}, {"a%", "x", NULL, NULL}, {"%\\n", NULL, NULL, NULL}, {"\\%%\\n", NULL, NULL, NULL}, - {"%5d\\n", "3", NULL, NULL}, - {"%-3s|\\n", "a", NULL, NULL}, - {"%.2s\\n", "abc", NULL, NULL}, - {"%ld\\n", "5", NULL, NULL}, - {"%X\\n", "255", NULL, NULL}, - {"%b\\n", "a", NULL, NULL}, - {"%e\\n", "1", NULL, NULL}, {"%s %z\\n", "a", NULL, NULL}, }; ```