commit
81c07a9555
|
@ -776,7 +776,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
|
|||
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
|
||||
size_t n = 0, allocated = 0, count = 0;
|
||||
_cleanup_free_ char *buffer = NULL;
|
||||
int r;
|
||||
int r, tty = -1;
|
||||
|
||||
assert(f);
|
||||
|
||||
|
@ -850,6 +850,17 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
|
|||
|
||||
count++;
|
||||
|
||||
if (eol != EOL_NONE) {
|
||||
/* If we are on a tty, we can't wait for more input. But we expect only
|
||||
* \n as the single EOL marker, so there is no need to wait. We check
|
||||
* this condition last to avoid isatty() check if not necessary. */
|
||||
|
||||
if (tty < 0)
|
||||
tty = isatty(fileno(f));
|
||||
if (tty > 0)
|
||||
break;
|
||||
}
|
||||
|
||||
if (eol != EOL_NONE) {
|
||||
previous_eol |= eol;
|
||||
continue;
|
||||
|
|
|
@ -200,38 +200,33 @@ int ask_char(char *ret, const char *replies, const char *fmt, ...) {
|
|||
}
|
||||
|
||||
int ask_string(char **ret, const char *text, ...) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
va_list ap;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
assert(text);
|
||||
|
||||
for (;;) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
va_list ap;
|
||||
if (colors_enabled())
|
||||
fputs(ANSI_HIGHLIGHT, stdout);
|
||||
|
||||
if (colors_enabled())
|
||||
fputs(ANSI_HIGHLIGHT, stdout);
|
||||
va_start(ap, text);
|
||||
vprintf(text, ap);
|
||||
va_end(ap);
|
||||
|
||||
va_start(ap, text);
|
||||
vprintf(text, ap);
|
||||
va_end(ap);
|
||||
if (colors_enabled())
|
||||
fputs(ANSI_NORMAL, stdout);
|
||||
|
||||
if (colors_enabled())
|
||||
fputs(ANSI_NORMAL, stdout);
|
||||
fflush(stdout);
|
||||
|
||||
fflush(stdout);
|
||||
r = read_line(stdin, LONG_LINE_MAX, &line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -EIO;
|
||||
|
||||
r = read_line(stdin, LONG_LINE_MAX, &line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -EIO;
|
||||
|
||||
if (!isempty(line)) {
|
||||
*ret = TAKE_PTR(line);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*ret = TAKE_PTR(line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int reset_terminal_fd(int fd, bool switch_to_text) {
|
||||
|
|
|
@ -164,7 +164,7 @@ static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned perc
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) {
|
||||
static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
|
||||
int r;
|
||||
|
||||
assert(text);
|
||||
|
@ -175,7 +175,8 @@ static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *
|
|||
_cleanup_free_ char *p = NULL;
|
||||
unsigned u;
|
||||
|
||||
r = ask_string(&p, "%s %s (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
|
||||
r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ",
|
||||
special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to query user: %m");
|
||||
|
||||
|
@ -184,23 +185,26 @@ static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (streq(p, "list")) {
|
||||
r = show_menu(l, 3, 22, percentage);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
putchar('\n');
|
||||
continue;
|
||||
};
|
||||
|
||||
r = safe_atou(p, &u);
|
||||
if (r >= 0) {
|
||||
char *c;
|
||||
|
||||
if (u <= 0 || u > strv_length(l)) {
|
||||
log_error("Specified entry number out of range.");
|
||||
continue;
|
||||
}
|
||||
|
||||
log_info("Selected '%s'.", l[u-1]);
|
||||
|
||||
c = strdup(l[u-1]);
|
||||
if (!c)
|
||||
if (free_and_strdup(ret, l[u-1]) < 0)
|
||||
return log_oom();
|
||||
|
||||
free(*ret);
|
||||
*ret = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -209,10 +213,7 @@ static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *
|
|||
continue;
|
||||
}
|
||||
|
||||
free(*ret);
|
||||
*ret = p;
|
||||
p = 0;
|
||||
return 0;
|
||||
return free_and_replace(*ret, p);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -248,21 +249,16 @@ static int prompt_locale(void) {
|
|||
} else {
|
||||
print_welcome();
|
||||
|
||||
printf("\nAvailable Locales:\n\n");
|
||||
r = show_menu(locales, 3, 22, 60);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
putchar('\n');
|
||||
|
||||
r = prompt_loop("Please enter system locale name or number", locales, locale_is_valid, &arg_locale);
|
||||
r = prompt_loop("Please enter system locale name or number",
|
||||
locales, 60, locale_is_valid, &arg_locale);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (isempty(arg_locale))
|
||||
return 0;
|
||||
|
||||
r = prompt_loop("Please enter system message locale name or number", locales, locale_is_valid, &arg_locale_messages);
|
||||
r = prompt_loop("Please enter system message locale name or number",
|
||||
locales, 60, locale_is_valid, &arg_locale_messages);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -338,15 +334,8 @@ static int prompt_keymap(void) {
|
|||
|
||||
print_welcome();
|
||||
|
||||
printf("\nAvailable keymaps:\n\n");
|
||||
r = show_menu(kmaps, 3, 22, 60);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
putchar('\n');
|
||||
|
||||
return prompt_loop("Please enter system keymap name or number",
|
||||
kmaps, keymap_is_valid, &arg_keymap);
|
||||
kmaps, 60, keymap_is_valid, &arg_keymap);
|
||||
}
|
||||
|
||||
static int process_keymap(void) {
|
||||
|
@ -414,14 +403,8 @@ static int prompt_timezone(void) {
|
|||
|
||||
print_welcome();
|
||||
|
||||
printf("\nAvailable Time Zones:\n\n");
|
||||
r = show_menu(zones, 3, 22, 30);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
putchar('\n');
|
||||
|
||||
r = prompt_loop("Please enter timezone name or number", zones, timezone_is_valid_log_error, &arg_timezone);
|
||||
r = prompt_loop("Please enter timezone name or number",
|
||||
zones, 30, timezone_is_valid_log_error, &arg_timezone);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
|
Loading…
Reference in New Issue