[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 16/42] qemu-io: use readline.c
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 16/42] qemu-io: use readline.c |
Date: |
Wed, 15 Jan 2014 11:22:29 +0100 |
From: Stefan Hajnoczi <address@hidden>
Use readline.c for command-line history. There was support for GNU
Readline and BSD Editline but it was never compiled in. Since QEMU has
its own readline.c, just use that when qemu-io runs with stdin attached
to a terminal.
Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
qemu-io.c | 103 +++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 58 insertions(+), 45 deletions(-)
diff --git a/qemu-io.c b/qemu-io.c
index 3b3340a..d7c26d3 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -18,6 +18,7 @@
#include "qemu/main-loop.h"
#include "qemu/option.h"
#include "qemu/config-file.h"
+#include "qemu/readline.h"
#include "block/block_int.h"
#include "trace/control.h"
@@ -32,6 +33,8 @@ extern int qemuio_misalign;
static int ncmdline;
static char **cmdline;
+static ReadLineState *readline_state;
+
static int close_f(BlockDriverState *bs, int argc, char **argv)
{
bdrv_unref(bs);
@@ -203,14 +206,6 @@ static void usage(const char *name)
name);
}
-
-#if defined(ENABLE_READLINE)
-# include <readline/history.h>
-# include <readline/readline.h>
-#elif defined(ENABLE_EDITLINE)
-# include <histedit.h>
-#endif
-
static char *get_prompt(void)
{
static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
@@ -222,52 +217,47 @@ static char *get_prompt(void)
return prompt;
}
-#if defined(ENABLE_READLINE)
-static char *fetchline(void)
+static void readline_printf_func(void *opaque, const char *fmt, ...)
{
- char *line = readline(get_prompt());
- if (line && *line) {
- add_history(line);
- }
- return line;
+ va_list ap;
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
}
-#elif defined(ENABLE_EDITLINE)
-static char *el_get_prompt(EditLine *e)
+
+static void readline_flush_func(void *opaque)
{
- return get_prompt();
+ fflush(stdout);
}
-static char *fetchline(void)
+static void readline_func(void *opaque, const char *str, void *readline_opaque)
{
- static EditLine *el;
- static History *hist;
- HistEvent hevent;
- char *line;
- int count;
-
- if (!el) {
- hist = history_init();
- history(hist, &hevent, H_SETSIZE, 100);
- el = el_init(progname, stdin, stdout, stderr);
- el_source(el, NULL);
- el_set(el, EL_SIGNAL, 1);
- el_set(el, EL_PROMPT, el_get_prompt);
- el_set(el, EL_HIST, history, (const char *)hist);
- }
- line = strdup(el_gets(el, &count));
- if (line) {
- if (count > 0) {
- line[count-1] = '\0';
- }
- if (*line) {
- history(hist, &hevent, H_ENTER, line);
+ char **line = readline_opaque;
+ *line = g_strdup(str);
+}
+
+static void readline_completion_func(void *opaque, const char *str)
+{
+ /* No command or argument completion implemented yet */
+}
+
+static char *fetchline_readline(void)
+{
+ char *line = NULL;
+
+ readline_start(readline_state, get_prompt(), 0, readline_func, &line);
+ while (!line) {
+ int ch = getchar();
+ if (ch == EOF) {
+ break;
}
+ readline_handle_byte(readline_state, ch);
}
return line;
}
-#else
-# define MAXREADLINESZ 1024
-static char *fetchline(void)
+
+#define MAXREADLINESZ 1024
+static char *fetchline_fgets(void)
{
char *p, *line = g_malloc(MAXREADLINESZ);
@@ -283,7 +273,15 @@ static char *fetchline(void)
return line;
}
-#endif
+
+static char *fetchline(void)
+{
+ if (readline_state) {
+ return fetchline_readline();
+ } else {
+ return fetchline_fgets();
+ }
+}
static void prep_fetchline(void *opaque)
{
@@ -339,6 +337,11 @@ static void add_user_command(char *optarg)
cmdline[ncmdline-1] = optarg;
}
+static void reenable_tty_echo(void)
+{
+ qemu_set_tty_echo(STDIN_FILENO, true);
+}
+
int main(int argc, char **argv)
{
int readonly = 0;
@@ -435,6 +438,15 @@ int main(int argc, char **argv)
qemuio_add_command(&open_cmd);
qemuio_add_command(&close_cmd);
+ if (isatty(STDIN_FILENO)) {
+ readline_state = readline_init(readline_printf_func,
+ readline_flush_func,
+ NULL,
+ readline_completion_func);
+ qemu_set_tty_echo(STDIN_FILENO, false);
+ atexit(reenable_tty_echo);
+ }
+
/* open the device */
if (!readonly) {
flags |= BDRV_O_RDWR;
@@ -453,5 +465,6 @@ int main(int argc, char **argv)
if (qemuio_bs) {
bdrv_unref(qemuio_bs);
}
+ g_free(readline_state);
return 0;
}
--
1.8.1.4
- [Qemu-devel] [PULL 13/42] readline: decouple readline from the monitor, (continued)
- [Qemu-devel] [PULL 13/42] readline: decouple readline from the monitor, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 08/42] gluster: Add support for creating zero-filled image, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 12/42] vmdk: Fix big flat extent IO, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 15/42] osdep: add qemu_set_tty_echo(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 10/42] qtest: Fix the bug about disable vnc causes "make check" fail, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 17/42] qemu-io: add command completion, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 14/42] readline: move readline to a generic location, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 20/42] qdict: Add qdict_array_split(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 16/42] qemu-io: use readline.c,
Kevin Wolf <=
- [Qemu-devel] [PULL 18/42] blkdebug: Use errp for read_config(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 21/42] qapi: extend qdict_flatten() for QLists, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 19/42] blkdebug: Don't require sophisticated filename, Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 22/42] qemu-option: Add qemu_config_parse_qdict(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 24/42] blkdebug: Use command-line in read_config(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 23/42] blkdebug: Always call read_config(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 26/42] block: Pass reference to bdrv_file_open(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 25/42] block: Allow reference for bdrv_file_open(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 28/42] block: Add bdrv_open_image(), Kevin Wolf, 2014/01/15
- [Qemu-devel] [PULL 29/42] block: Use bdrv_open_image() in bdrv_open(), Kevin Wolf, 2014/01/15