sfdisk: cleanup prompt usage
Let's use fgets-like callback only when compiled with readline, otherwise (and also for non-tty) print prompt by printf(). Reported-by: Ruediger Meier <sweet_f_a@gmx.de> Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
dda7fe12ac
commit
3ba8e422a0
|
@ -1088,6 +1088,7 @@ static int is_device_used(struct sfdisk *sf)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBREADLINE
|
||||||
static char *sfdisk_fgets(struct fdisk_script *dp,
|
static char *sfdisk_fgets(struct fdisk_script *dp,
|
||||||
char *buf, size_t bufsz, FILE *f)
|
char *buf, size_t bufsz, FILE *f)
|
||||||
{
|
{
|
||||||
|
@ -1097,8 +1098,7 @@ static char *sfdisk_fgets(struct fdisk_script *dp,
|
||||||
assert(buf);
|
assert(buf);
|
||||||
assert(bufsz > 2);
|
assert(bufsz > 2);
|
||||||
|
|
||||||
#ifdef HAVE_LIBREADLINE
|
if (sf->interactive) {
|
||||||
if (isatty(STDIN_FILENO)) {
|
|
||||||
char *p = readline(sf->prompt);
|
char *p = readline(sf->prompt);
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
|
@ -1112,14 +1112,12 @@ static char *sfdisk_fgets(struct fdisk_script *dp,
|
||||||
buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
|
buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
|
||||||
buf[len + 1] = '\0';
|
buf[len + 1] = '\0';
|
||||||
free(p);
|
free(p);
|
||||||
|
fflush(stdout);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
if (sf->prompt)
|
|
||||||
fputs(sf->prompt, stdout);
|
|
||||||
fflush(stdout);
|
|
||||||
return fgets(buf, bufsz, f);
|
return fgets(buf, bufsz, f);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* sfdisk <device> [[-N] <partno>]
|
* sfdisk <device> [[-N] <partno>]
|
||||||
|
@ -1151,7 +1149,9 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
|
||||||
if (!dp)
|
if (!dp)
|
||||||
err(EXIT_FAILURE, _("failed to allocate script handler"));
|
err(EXIT_FAILURE, _("failed to allocate script handler"));
|
||||||
fdisk_set_script(sf->cxt, dp);
|
fdisk_set_script(sf->cxt, dp);
|
||||||
|
#ifdef HAVE_LIBREADLINE
|
||||||
fdisk_script_set_fgets(dp, sfdisk_fgets);
|
fdisk_script_set_fgets(dp, sfdisk_fgets);
|
||||||
|
#endif
|
||||||
fdisk_script_set_userdata(dp, (void *) sf);
|
fdisk_script_set_userdata(dp, (void *) sf);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1268,6 +1268,13 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
|
||||||
sf->prompt = xstrdup(SFDISK_PROMPT);
|
sf->prompt = xstrdup(SFDISK_PROMPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef HAVE_LIBREADLINE
|
||||||
|
if (sf->prompt)
|
||||||
|
fputs(sf->prompt, stdout);
|
||||||
|
#else
|
||||||
|
if (!sf->interactive && sf->prompt)
|
||||||
|
fputs(sf->prompt, stdout);
|
||||||
|
#endif
|
||||||
rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
|
rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
|
DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
|
||||||
|
|
Loading…
Reference in New Issue