summaryrefslogtreecommitdiffstats
path: root/disk-utils
diff options
context:
space:
mode:
authorKarel Zak2015-03-25 10:31:37 +0100
committerKarel Zak2015-03-25 10:31:37 +0100
commit740c36f657ddef7b8adf829230e91995a0dd3143 (patch)
treeeb9c6f9785ce8e92905d1418e4a8612b3b56aefb /disk-utils
parentbuild-sys: add --with-readline (diff)
downloadkernel-qcow2-util-linux-740c36f657ddef7b8adf829230e91995a0dd3143.tar.gz
kernel-qcow2-util-linux-740c36f657ddef7b8adf829230e91995a0dd3143.tar.xz
kernel-qcow2-util-linux-740c36f657ddef7b8adf829230e91995a0dd3143.zip
fdisk: add GNU Readline support to fdisk
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r--disk-utils/Makemodule.am4
-rw-r--r--disk-utils/fdisk.c64
2 files changed, 52 insertions, 16 deletions
diff --git a/disk-utils/Makemodule.am b/disk-utils/Makemodule.am
index 35a02bd6e..dd4e96b7d 100644
--- a/disk-utils/Makemodule.am
+++ b/disk-utils/Makemodule.am
@@ -134,7 +134,8 @@ fdisk_SOURCES = \
disk-utils/fdisk-list.c \
disk-utils/fdisk-list.h
-fdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libsmartcols.la libtcolors.la
+fdisk_LDADD = $(LDADD) libcommon.la libfdisk.la \
+ libsmartcols.la libtcolors.la $(READLINE_LIBS)
fdisk_CFLAGS = $(AM_CFLAGS) -I$(ul_libfdisk_incdir) -I$(ul_libsmartcols_incdir)
if BUILD_LIBBLKID
@@ -147,7 +148,6 @@ fdisk_CFLAGS += -I$(ul_libuuid_incdir)
fdisk_LDADD += libuuid.la
endif
-
if HAVE_STATIC_FDISK
sbin_PROGRAMS += fdisk.static
fdisk_static_SOURCES = $(fdisk_SOURCES)
diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c
index 6c63d06cd..0e3f57997 100644
--- a/disk-utils/fdisk.c
+++ b/disk-utils/fdisk.c
@@ -22,6 +22,9 @@
#include <time.h>
#include <limits.h>
#include <libsmartcols.h>
+#ifdef HAVE_LIBREADLINE
+# include <readline/readline.h>
+#endif
#include "c.h"
#include "xalloc.h"
@@ -58,6 +61,22 @@ static void fdiskprog_init_debug(void)
__UL_INIT_DEBUG(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG);
}
+#ifdef HAVE_LIBREADLINE
+static char *rl_fgets(char *s, int n, FILE *stream, const char *prompt)
+{
+ char *p;
+
+ rl_outstream = stream;
+ p = readline(prompt);
+ if (!p)
+ return NULL;
+
+ memcpy(s, p, n);
+ free(p);
+ return s;
+}
+#endif
+
int get_user_reply(struct fdisk_context *cxt, const char *prompt,
char *buf, size_t bufsz)
{
@@ -65,20 +84,37 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt,
size_t sz;
do {
- fputs(prompt, stdout);
- fflush(stdout);
-
- if (!fgets(buf, bufsz, stdin)) {
- if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
- fprintf(stderr, _("\nDo you really want to quit? "));
-
- if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
- continue;
- }
- fdisk_unref_context(cxt);
- exit(EXIT_FAILURE);
- } else
- break;
+#ifdef HAVE_LIBREADLINE
+ if (isatty(STDIN_FILENO)) {
+ if (!rl_fgets(buf, bufsz, stdout, prompt)) {
+ if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
+ if (rl_fgets(buf, bufsz, stderr,
+ _("\nDo you really want to quit? "))
+ && !rpmatch(buf))
+ continue;
+ }
+ fdisk_unref_context(cxt);
+ exit(EXIT_FAILURE);
+ } else
+ break;
+ }
+ else
+#endif
+ {
+ fputs(prompt, stdout);
+ fflush(stdout);
+ if (!fgets(buf, bufsz, stdin)) {
+ if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
+ fprintf(stderr, _("\nDo you really want to quit? "));
+
+ if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
+ continue;
+ }
+ fdisk_unref_context(cxt);
+ exit(EXIT_FAILURE);
+ } else
+ break;
+ }
} while (1);
for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */