]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ask-string: support prefilling a string query with an explicit string
authorLennart Poettering <lennart@amutable.com>
Tue, 19 May 2026 12:59:38 +0000 (14:59 +0200)
committerLennart Poettering <lennart@amutable.com>
Wed, 20 May 2026 14:34:21 +0000 (16:34 +0200)
src/basic/terminal-util.c
src/basic/terminal-util.h
src/firstboot/firstboot.c
src/home/homectl-prompts.c
src/home/homectl.c
src/shared/prompt-util.c
src/shared/prompt-util.h
src/sysinstall/sysinstall.c

index 5b3fc8990d6960d085d22507b518f09d1ab62adf..0d5b57138bc1d9eabb3a7ba5ad6a1b1b6dacf49f 100644 (file)
@@ -283,6 +283,7 @@ static void clear_by_backspace(size_t n) {
 
 int ask_string_full(
                 char **ret,
+                const char *prefill,
                 GetCompletionsCallback get_completions,
                 void *userdata,
                 const char *text, ...) {
@@ -296,8 +297,17 @@ int ask_string_full(
         _cleanup_free_ char *string = NULL;
         size_t n = 0;
 
-        if (get_completions) {
-                /* Figure out what string to preselect the query with */
+        if (prefill) {
+                /* Prefill query with explicit data if specified */
+
+                string = strdup(prefill);
+                if (!string)
+                        return -ENOMEM;
+
+                n = strlen(string);
+
+        } else if (get_completions) {
+                /* Otherwise, figure out what string to preselect the query with */
                 _cleanup_strv_free_ char **completions = NULL;
                 r = get_completions("", GET_COMPLETIONS_PRESELECT, &completions, userdata);
                 if (r < 0)
index abf999e6d5562603a4a57b338d14b5d6b213b6c2..f8cd74f4105215b5d2210485a25b6ff534b50866 100644 (file)
@@ -95,8 +95,8 @@ typedef enum GetCompletionsFlags {
 } GetCompletionsFlags;
 
 typedef int (*GetCompletionsCallback)(const char *key, GetCompletionsFlags flags, char ***ret_list, void *userdata);
-int ask_string_full(char **ret, GetCompletionsCallback get_completions, void *userdata, const char *text, ...) _printf_(4, 5);
-#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, text, ##__VA_ARGS__)
+int ask_string_full(char **ret, const char *prefill, GetCompletionsCallback get_completions, void *userdata, const char *text, ...) _printf_(5, 6);
+#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, NULL, text, ##__VA_ARGS__)
 
 bool any_key_to_proceed(void);
 int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers);
index 8048ec0e810caddb9c090df67bffc8b8bb132cec..ea36b910646d652d99f029853c641aad856ea280 100644 (file)
@@ -295,6 +295,7 @@ static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
 
                 r = prompt_loop("Please enter the new system locale name or number",
                                 GLYPH_WORLD,
+                                /* prefill= */ NULL,
                                 locales,
                                 /* accepted= */ NULL,
                                 /* ellipsize_percentage= */ 60,
@@ -312,6 +313,7 @@ static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
 
                 r = prompt_loop("Please enter the new system message locale name or number",
                                 GLYPH_WORLD,
+                                /* prefill= */ NULL,
                                 locales,
                                 /* accepted= */ NULL,
                                 /* ellipsize_percentage= */ 60,
@@ -457,6 +459,7 @@ static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
         return prompt_loop(
                         "Please enter the new keymap name or number",
                         GLYPH_KEYBOARD,
+                        /* prefill= */ NULL,
                         kmaps,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 60,
@@ -573,6 +576,7 @@ static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
         return prompt_loop(
                         "Please enter the new timezone name or number",
                         GLYPH_CLOCK,
+                        /* prefill= */ NULL,
                         zones,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 30,
@@ -680,6 +684,7 @@ static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
 
         r = prompt_loop("Please enter the new hostname",
                         GLYPH_LABEL,
+                        /* prefill= */ NULL,
                         /* menu= */ NULL,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 100,
@@ -888,6 +893,7 @@ static int prompt_root_shell(int rfd, sd_varlink **mute_console_link) {
         return prompt_loop(
                         "Please enter the new root shell",
                         GLYPH_SHELL,
+                        /* prefill= */ NULL,
                         /* menu= */ NULL,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 0,
index 71640377e38886ac8fbf74cbbe3785a1a606ffd7..95a6ec3d04f40c2b46c926abf4376e1d1ef8dcfc 100644 (file)
@@ -117,6 +117,7 @@ int prompt_groups(const char *username, char ***ret_groups) {
                 _cleanup_free_ char *s = NULL;
                 r = ask_string_full(
                                 &s,
+                                /* prefill= */ NULL,
                                 group_completion_callback,
                                 &available,
                                 "%s Please enter an auxiliary group for user %s (empty to continue, \"list\" to list available groups): ",
@@ -231,6 +232,7 @@ int prompt_shell(const char *username, char **ret_shell) {
         return prompt_loop(
                         q,
                         GLYPH_SHELL,
+                        /* prefill= */ NULL,
                         /* menu= */ NULL,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 0,
index ab81efbed6dd193a0860146eb70e534d3a423f5c..6891475f3780f6f815a032bc1f282319abf5e5a0 100644 (file)
@@ -3009,6 +3009,7 @@ static int create_interactively(void) {
 
         r = prompt_loop("Please enter user name to create",
                         GLYPH_IDCARD,
+                        /* prefill= */ NULL,
                         /* menu= */ NULL,
                         /* accepted= */ NULL,
                         /* ellipsize_percentage= */ 60,
index 2f334f9b52832543145dd1c71d99fb4bca7fca79..69728e0cb4c4ca27510bb68078641815f63dfdd9 100644 (file)
@@ -60,6 +60,7 @@ static int get_completions(
 int prompt_loop(
                 const char *text,
                 Glyph emoji,
+                const char *prefill,     /* if non-NULL: prefill prompt with this string */
                 char **menu,             /* if non-NULL: choices to suggest */
                 char **accepted,         /* if non-NULL: choices to accept (should be a superset of 'menu') */
                 unsigned ellipsize_percentage,
@@ -116,6 +117,7 @@ int prompt_loop(
                 _cleanup_free_ char *p = NULL;
                 r = ask_string_full(
                                 &p,
+                                prefill,
                                 get_completions,
                                 &(CompletionData) { menu, accepted },
                                 "%s%s%s%s: ",
index 436127b32c23771cc6b9f89bbbc83da148398fcd..7afccd1b3dec5b4984e1962b4e7bb27b49a5d7c1 100644 (file)
@@ -14,6 +14,7 @@ typedef enum PromptFlags {
 
 int prompt_loop(const char *text,
                 Glyph emoji,
+                const char *prefill,
                 char **menu,
                 char **accepted,
                 unsigned ellipsize_percentage,
index 7d08b4866b272d2bd5b60d0103618f2fae412800..23f4472e1c0a4d969ae89c3251e1ea902c0b4813 100644 (file)
@@ -384,6 +384,7 @@ static int prompt_block_device(sd_varlink **repart_link, char **ret_node) {
 
         r = prompt_loop("Please enter target disk device",
                         GLYPH_COMPUTER_DISK,
+                        /* prefill= */ NULL,
                         menu,
                         accepted,
                         /* ellipsize_percentage= */ 20,
@@ -540,6 +541,7 @@ static int prompt_erase(
                         "Please type 'keep' to install the OS in addition to what the disk already contains, or 'erase' to erase all data on the disk" :
                         "Please type 'erase' to confirm that all data on the disk shall be erased",
                         GLYPH_BROOM,
+                        /* prefill= */ NULL,
                         /* menu= */ l,
                         /* accepted= */ l,
                         /* ellipsize_percentage= */ 20,
@@ -578,6 +580,7 @@ static int prompt_touch_variables(void) {
         _cleanup_free_ char *reply = NULL;
         r = prompt_loop("Type 'yes' to register OS installation in firmware variables of the local system, 'no' otherwise",
                         GLYPH_ROCKET,
+                        /* prefill= */ NULL,
                         /* menu= */ l,
                         /* accepted= */ l,
                         /* ellipsize_percentage= */ 20,
@@ -616,6 +619,7 @@ static int prompt_confirm(void) {
         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,