summaryrefslogtreecommitdiffhomepage
path: root/src/exec.c
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-27 21:48:49 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-27 21:48:49 -0600
commitced8928b067a3bd3047f7e094eb9d9d575ae9444 (patch)
tree9f8b20592193e5e3cad154d318c36d79289e8ebe /src/exec.c
parent1cb0581bd9e9ac62cc31f0efa39a70d405f699a9 (diff)
downloadzash-ced8928b067a3bd3047f7e094eb9d9d575ae9444.tar.gz
feat: select, the ksh88 menu loop
IBM's /bin/sh is of the KornShell line and `select` is part of what its scripts use, so the shell that replaces it has to have it. The menu goes to stderr as "number TAB word", then PS3 (default "#? "); the answer is read with the rules of `read REPLY`; a number in range picks that word, anything else leaves the variable empty and the body still runs, a blank line only prompts again, and end of input ends the loop. The node is the `for` node with a `menu` flag, since the two differ only in what the body sees. tests/zash/select.sh prints the same bytes under zash and under the CBT /bin/sh; the specification has the rule.
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c51
1 files changed, 45 insertions, 6 deletions
diff --git a/src/exec.c b/src/exec.c
index b1db90b..1b04b25 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -1802,7 +1802,42 @@ static int eval_loop(node *n, unsigned flags) {
}
/**
- * @brief Run a `for` loop.
+ * @brief Run one `select` pass: menu, prompt, one line of answer.
+ *
+ * Kept apart from eval_for() only so the `for` case stays readable.
+ *
+ * @param n The loop node.
+ * @param flags Evaluation flags.
+ * @param words The words the menu offers.
+ * @param count How many there are.
+ * @return The status of the last command run in the body.
+ */
+static int select_loop(node *n, unsigned flags, char **words, int count) {
+ int status = 0;
+ for (;;) {
+ for (int i = 0; i < count; i++)
+ out_printf(&out2, "%d\t%s\n", i + 1, words[i]);
+ const char *ps3 = var_get("PS3");
+ out_puts(&out2, ps3 ? ps3 : "#? ");
+ out_flush(&out2);
+ if (builtin_read_reply() != 0) /* end of input ends the loop */
+ break;
+ const char *reply = var_get("REPLY");
+ if (!reply || !*reply) /* a blank line only prompts again */
+ continue;
+ char *end;
+ long pick = strtol(reply, &end, 10);
+ bool chosen = *end == '\0' && pick >= 1 && pick <= count;
+ var_set(n->u.for_.var, chosen ? words[pick - 1] : "", 0);
+ status = eval_node(n->u.for_.body, flags & EV_TESTED);
+ if (loop_should_stop())
+ break;
+ }
+ return status;
+}
+
+/**
+ * @brief Run a `for` or `select` loop.
*/
static int eval_for(node *n, unsigned flags) {
int count;
@@ -1826,11 +1861,15 @@ static int eval_for(node *n, unsigned flags) {
if (setjmp(jl.buf) == 0) {
exc_catch(&jl);
loop_depth++;
- for (int i = 0; i < count; i++) {
- var_set(n->u.for_.var, words[i], 0);
- status = eval_node(n->u.for_.body, flags & EV_TESTED);
- if (loop_should_stop())
- break;
+ if (n->u.for_.menu) {
+ status = select_loop(n, flags, words, count);
+ } else {
+ for (int i = 0; i < count; i++) {
+ var_set(n->u.for_.var, words[i], 0);
+ status = eval_node(n->u.for_.body, flags & EV_TESTED);
+ if (loop_should_stop())
+ break;
+ }
}
loop_depth--;
} else {