]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: add a generic prompt_loop_yes_no() helper
authorLennart Poettering <lennart@amutable.com>
Thu, 28 May 2026 13:33:49 +0000 (15:33 +0200)
committerLennart Poettering <lennart@amutable.com>
Sat, 27 Jun 2026 15:28:39 +0000 (17:28 +0200)
Factor a yes/no variant of prompt_loop() out into prompt-util.[ch], so the
various interactive tools can share a single implementation, and convert
systemd-sysinstall's installation confirmation question over to it.

src/shared/prompt-util.c
src/shared/prompt-util.h
src/sysinstall/sysinstall.c

index 69728e0cb4c4ca27510bb68078641815f63dfdd9..7837a90fa40321a82262635ebe0e1bf9d435c4d9 100644 (file)
@@ -213,6 +213,47 @@ int prompt_loop(
         }
 }
 
+static int boolean_is_valid(const char *name, void *userdata) {
+        return parse_boolean(name) >= 0;
+}
+
+int prompt_loop_yes_no(const char *question, bool def, bool *ret) {
+        int r;
+
+        assert(question);
+        assert(ret);
+
+        char **l = STRV_MAKE("yes", "no");
+
+        _cleanup_free_ char *reply = NULL;
+        r = prompt_loop(question,
+                        GLYPH_WARNING_SIGN,
+                        /* prefill= */ def ? "yes" : "no",
+                        /* menu= */ l,
+                        /* accepted= */ NULL,
+                        /* ellipsize_percentage= */ 20,
+                        /* n_columns= */ 2,
+                        /* column_width= */ 40,
+                        /* is_valid= */ boolean_is_valid,
+                        /* refresh= */ NULL,
+                        /* userdata= */ NULL,
+                        PROMPT_SHOW_MENU|PROMPT_MAY_SKIP|PROMPT_HIDE_MENU_HINT|PROMPT_HIDE_SKIP_HINT,
+                        &reply);
+        if (r < 0)
+                return r;
+        if (r == 0) { /* Skipped (empty input): fall back to the default. */
+                *ret = def;
+                return 0;
+        }
+
+        r = parse_boolean(reply);
+        if (r < 0)
+                return log_error_errno(r, "Failed to parse reply: %s", reply);
+
+        *ret = r;
+        return 1;
+}
+
 /* Default: bright white on blue background */
 #define ANSI_COLOR_CHROME "\x1B[0;44;1;37m"
 
index 7afccd1b3dec5b4984e1962b4e7bb27b49a5d7c1..255433b6717cbdf774a5f15f72e91e22d4a89f1e 100644 (file)
@@ -26,6 +26,8 @@ int prompt_loop(const char *text,
                 PromptFlags flags,
                 char **ret);
 
+int prompt_loop_yes_no(const char *question, bool def, bool *ret);
+
 int chrome_show(const char *top, const char *bottom);
 void chrome_hide(void);
 
index 01e8cd048337dae218e2cc072d23be1ff2e18ba6..5d684cb0abc6590f1c67343d396af4f41e256ebb 100644 (file)
@@ -613,29 +613,14 @@ static int prompt_confirm(void) {
 
         putchar('\n');
 
-        char **l = STRV_MAKE("yes", "no");
-
-        _cleanup_free_ char *reply = NULL;
-        r = prompt_loop(arg_summary ? "Please type 'yes' to confirm the choices above and begin the installation" :
-                                      "Please type 'yes' to begin the installation",
-                        GLYPH_WARNING_SIGN,
-                        /* prefill= */ NULL,
-                        /* menu= */ l,
-                        /* accepted= */ l,
-                        /* ellipsize_percentage= */ 20,
-                        /* n_columns= */ 2,
-                        /* column_width= */ 40,
-                        /* is_valid= */ NULL,
-                        /* refresh= */ NULL,
-                        /* userdata= */ NULL,
-                        PROMPT_SHOW_MENU|PROMPT_MAY_SKIP|PROMPT_HIDE_MENU_HINT|PROMPT_HIDE_SKIP_HINT,
-                        &reply);
+        bool yes;
+        r = prompt_loop_yes_no(arg_summary ? "Please type 'yes' to confirm the choices above and begin the installation" :
+                                             "Please type 'yes' to begin the installation",
+                               /* def= */ false,
+                               &yes);
         if (r < 0)
                 return r;
-        if (r == 0)
-                return log_error_errno(SYNTHETIC_ERRNO(ECANCELED), "Installation cancelled.");
-
-        if (!streq(reply, "yes"))
+        if (!yes)
                 return log_error_errno(SYNTHETIC_ERRNO(ECANCELED), "Installation not confirmed, cancelling.");
 
         return 0;