summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:33:12 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 00:33:12 -0600
commit662f3b37dcfad0fd2f193762464c1b9701c96db9 (patch)
tree311cacd64bda80ca200de6f3715c786ec5a5dc53
parent447151dabfbad0a41c8f049938042cd886d9f451 (diff)
downloadzash-662f3b37dcfad0fd2f193762464c1b9701c96db9.tar.gz
fix(expand): trim each positional parameter for ${@#pat}
`"${@#ab}"` with `set -- abc abd` joined the parameters first ("abc abd"), trimmed the join, and emitted that as a single field ("c abd"). ksh93 and bash trim each parameter and then apply the "$@" rules: two fields, "c" and "d". expand_all_params() takes the pattern and the operator and trims every parameter before emitting, so `"${@#pat}"` still gives one field each, `"${*#pat}"` one field joined by IFS, and unquoted both split as before. join_params_trimmed() is the join path, and trim() gets a forward declaration because it sits further down the file. Test: tests/zash/trim-all.sh, twelve lines covering all four operators, @ and *, quoted and unquoted, empty parameters and a non-blank IFS. The expected output is ksh93's and bash's, which agree line for line. Verified on Linux with make check.
-rw-r--r--src/expand.c56
-rw-r--r--tests/zash/trim-all.exp13
-rw-r--r--tests/zash/trim-all.sh20
3 files changed, 82 insertions, 7 deletions
diff --git a/src/expand.c b/src/expand.c
index 89106ad..1699fb8 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -338,6 +338,9 @@ static void emit_assign_lit(expansion *x, const char *s, size_t n,
/* ---- parameters ------------------------------------------------------ */
+static const char *trim(const char *val, const char *pat, param_op op,
+ size_t *lenp);
+
/**
* @brief Join the positional parameters into one string.
* @param sep Separator, or NUL for none.
@@ -355,6 +358,27 @@ static const char *join_params(char sep) {
}
/**
+ * @brief Join the positional parameters after trimming each one.
+ * @param sep Separator, or NUL for none.
+ * @param pat Pattern for trim().
+ * @param op Which trim.
+ * @return The joined string, in scratch memory.
+ */
+static const char *join_params_trimmed(char sep, const char *pat,
+ param_op op) {
+ strbuf sb;
+ sb_init(&sb, &scratch);
+ for (int i = 0; i < shellparam.n; i++) {
+ if (i && sep)
+ sb_putc(&sb, sep);
+ size_t len;
+ const char *s = trim(shellparam.p[i], pat, op, &len);
+ sb_put(&sb, s, len);
+ }
+ return sb_cstr(&sb);
+}
+
+/**
* @brief Expand `$@` or `$*` with no operator.
*
* `"$@"` gives one field per parameter, even an empty one. `"$*"` gives
@@ -363,27 +387,38 @@ static const char *join_params(char sep) {
* @param x Expansion state.
* @param which `@` or `*`.
* @param quoted Whether the expansion is inside double quotes.
+ * @param pat Pattern for `${@#pat}`, or nullptr for plain `$@`.
+ * @param op Which trim, when `pat` is not nullptr.
*/
-static void expand_all_params(expansion *x, char which, bool quoted) {
+static void expand_all_params(expansion *x, char which, bool quoted,
+ const char *pat, param_op op) {
int n = shellparam.n;
char **p = shellparam.p;
if (x->mode != X_FIELDS) {
- const char *s = join_params(which == '*' ? ifs_first : ' ');
+ const char *s =
+ pat ? join_params_trimmed(which == '*' ? ifs_first : ' ', pat, op)
+ : join_params(which == '*' ? ifs_first : ' ');
emit(x, s, strlen(s), quoted);
return;
}
if (quoted && which == '*') {
- const char *s = join_params(ifs_first);
+ const char *s =
+ pat ? join_params_trimmed(ifs_first, pat, op)
+ : join_params(ifs_first);
emit_quoted(x, s, strlen(s));
return;
}
for (int i = 0; i < n; i++) {
+ const char *s = p[i];
+ size_t len = strlen(s);
+ if (pat)
+ s = trim(s, pat, op, &len);
if (quoted) { /* "$@": one field per parameter, even if empty */
- emit_quoted(x, p[i], strlen(p[i]));
+ emit_quoted(x, s, len);
if (i < n - 1)
field_end(x);
} else {
- emit_split(x, p[i], strlen(p[i]));
+ emit_split(x, s, len);
if (i < n - 1 && x->have) {
field_end(x);
x->sep = SEP_SPACE;
@@ -521,7 +556,7 @@ static void expand_param(expansion *x, const param *pm, bool quoted) {
if (all) {
if (pm->op == PS_NORMAL) {
- expand_all_params(x, pm->special, quoted);
+ expand_all_params(x, pm->special, quoted, nullptr, PS_NORMAL);
return;
}
val = shellparam.n ? join_params(pm->special == '*' ? ifs_first : ' ')
@@ -569,7 +604,7 @@ static void expand_param(expansion *x, const param *pm, bool quoted) {
if (all && !missing && pm->op != PS_LENGTH && pm->op < PS_TRIML) {
/* "${@-word}" with parameters set is just "$@". */
- expand_all_params(x, pm->special, quoted);
+ expand_all_params(x, pm->special, quoted, nullptr, PS_NORMAL);
return;
}
@@ -580,6 +615,13 @@ static void expand_param(expansion *x, const param *pm, bool quoted) {
return;
}
+ if (all && pm->op >= PS_TRIML) {
+ /* "${@#pat}" trims each parameter; "$@" / "$*" rules follow. */
+ expand_all_params(x, pm->special, quoted, expand_pattern(pm->arg),
+ pm->op);
+ return;
+ }
+
if (!val) {
if (opt.nounset && !all)
sh_error("%s: parameter not set", pm->name);
diff --git a/tests/zash/trim-all.exp b/tests/zash/trim-all.exp
new file mode 100644
index 0000000..0380161
--- /dev/null
+++ b/tests/zash/trim-all.exp
@@ -0,0 +1,13 @@
+<c><d>
+<c><d>
+<c d>
+<c><d>
+<ab><abd>
+<abc><>
+<c><d>
+<b><c>
+<b><c>
+<a b a>
+[]
+<ne:two>
+[status 0]
diff --git a/tests/zash/trim-all.sh b/tests/zash/trim-all.sh
new file mode 100644
index 0000000..1b45bf2
--- /dev/null
+++ b/tests/zash/trim-all.sh
@@ -0,0 +1,20 @@
+# `${@#pat}` and `${*#pat}`: the trim applies to each positional parameter,
+# and then the usual `"$@"` / `"$*"` rules decide the fields. ksh93 and
+# bash agree on every line, which is where the expectations come from.
+set -- abc abd
+printf "<%s>" "${@#ab}"; echo
+printf "<%s>" ${@#ab}; echo
+printf "<%s>" "${*#ab}"; echo
+printf "<%s>" ${*#ab}; echo
+printf "<%s>" "${@%c}"; echo
+printf "<%s>" "${@%%*d}"; echo
+printf "<%s>" "${@##a*b}"; echo
+set -- "a b" "a c"
+printf "<%s>" "${@#a }"; echo
+printf "<%s>" ${@#a\ }; echo
+printf "<%s>" "${*% c}"; echo
+set --
+printf "[%s]" "${@#a}"; echo
+set -- one two
+IFS=:
+printf "<%s>" "${*#o}"; echo