qemu-io: readline command completion
cover qemu-io: readline command completion
This series decouples readline.c from the QEMU monitor and then reuses it in qemu-io. This adds history and command completion to the qemu-io interactive prompt.
Stefan Hajnoczi (5):
readline: decouple readline from the monitor
readline: move readline to a generic location
osdep: add qemu_set_tty_echo()
qemu-io: use readline.c
qemu-io: add command completion
Makefile.objs | 1 - hmp.c | 6 +- include/monitor/monitor.h | 2 +- include/monitor/readline.h | 56 ----- include/qemu-io.h | 3 + include/qemu/osdep.h | 2 + include/qemu/readline.h | 62 ++++++ monitor.c | 41 +++- qemu-io-cmds.c | 15 ++ qemu-io.c | 109 +++++----- readline.c | 493 -------------------------------------------- util/Makefile.objs | 1 + util/oslib-posix.c | 18 ++ util/oslib-win32.c | 19 ++ util/readline.c | 495 +++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 716 insertions(+), 607 deletions(-) delete mode 100644 include/monitor/readline.h create mode 100644 include/qemu/readline.h delete mode 100644 readline.c create mode 100644 util/readline.c
Signature · 1 line
1.8.3.1
PATCH 1/5 readline: decouple readline from the monitor
Make the readline.c functionality reusable. Instead of calling monitor_printf() and monitor_flush() directly, invoke function pointers provided by the user.
This way readline.c does not know about Monitor and other users will be able to make use of readline.c.
Note that there is already an "opaque" argument to the ReadLineFunc callback. Consistently call it "readline_opaque" from now on to distinguish from the ReadLinePrintfFunc/ReadLineFlushFunc "opaque" argument.
I also dropped the printf macro trickery since it's now highly unlikely that anyone modifying readline.c would call printf(3) directly. We no longer need this protection.
--- hmp.c | 6 +++--- include/monitor/readline.h | 20 +++++++++++++------- monitor.c | 39 ++++++++++++++++++++++++++++++++------- readline.c | 44 +++++++++++++++++++++++--------------------- 4 files changed, 71 insertions(+), 38 deletions(-)
hmp.c +3 -3
@@ -1091,11 +1091,11 @@ void hmp_eject(Monitor *mon, const QDict *qdict)hmp_handle_error (mon, &err); } -static void hmp_change_read_arg (Monitor *mon,const char *password, -void *opaque) +static void hmp_change_read_arg (void *opaque,const char *password, +void *readline_opaque) {qmp_change_vnc_password (password,NULL ); -monitor_read_command (mon,1 ); +monitor_read_command (opaque,1 ); }void hmp_change (Monitor *mon,const QDict *qdict)
include/monitor/readline.h +13 -7
@@ -1,14 +1,15 @@ #ifndef READLINE_H #define READLINE_H -#include "qemu-common.h" - #define READLINE_CMD_BUF_SIZE 4095 #define READLINE_MAX_CMDS 64 #define READLINE_MAX_COMPLETIONS 256 -typedef void ReadLineFunc(Monitor *mon, const char *str, void *opaque); -typedef void ReadLineCompletionFunc(Monitor *mon, +typedef void ReadLinePrintfFunc(void *opaque, const char *fmt, ...); +typedef void ReadLineFlushFunc(void *opaque); +typedef void ReadLineFunc(void *opaque, const char *str, + void *readline_opaque); +typedef void ReadLineCompletionFunc(void *opaque, const char *cmdline); typedef struct ReadLineState {
@@ -35,7 +36,10 @@ typedef struct ReadLineState {void *readline_opaque;int read_password;char prompt[256 ]; -Monitor *mon; + +ReadLinePrintfFunc *printf_func; +ReadLineFlushFunc *flush_func; +void *opaque; } ReadLineState;void readline_add_completion (ReadLineState *rs,const char *str);
@@ -46,11 +50,13 @@ const char *readline_get_history(ReadLineState *rs, unsigned int index);void readline_handle_byte (ReadLineState *rs,int ch);void readline_start (ReadLineState *rs,const char *prompt,int read_password, -ReadLineFunc *readline_func,void *opaque); +ReadLineFunc *readline_func,void *readline_opaque);void readline_restart (ReadLineState *rs);void readline_show_prompt (ReadLineState *rs); -ReadLineState *readline_init (Monitor *mon, +ReadLineState *readline_init (ReadLinePrintfFunc *printf_func, +ReadLineFlushFunc *flush_func, +void *opaque,ReadLineCompletionFunc *completion_finder);#endif /* !READLINE_H */
monitor.c +32 -7
@@ -217,8 +217,8 @@ static const mon_cmd_t qmp_cmds[];Monitor *cur_mon;Monitor *default_mon; -static void monitor_command_cb (Monitor *mon,const char *cmdline, -void *opaque); +static void monitor_command_cb (void *opaque,const char *cmdline, +void *readline_opaque);static inline int qmp_cmd_mode (const Monitor *mon) {
@@ -4338,9 +4338,10 @@ static void monitor_find_completion_by_table(Monitor *mon, } } -static void monitor_find_completion (Monitor *mon, +static void monitor_find_completion (void *opaque,const char *cmdline) { +Monitor *mon = opaque;char *args[MAX_ARGS ];int nb_args, len;
@@ -4751,8 +4752,11 @@ static void monitor_read(void *opaque, const uint8_t *buf, int size) cur_mon = old_mon; } -static void monitor_command_cb (Monitor *mon,const char *cmdline,void *opaque) +static void monitor_command_cb (void *opaque,const char *cmdline, +void *readline_opaque) { +Monitor *mon = opaque; +monitor_suspend (mon);handle_user_command (mon, cmdline);monitor_resume (mon);
@@ -4881,6 +4885,22 @@ static void sortcmdlist(void)* End: */ +/* These functions just adapt the readline interface in a typesafe way. We +* could cast function pointers but that discards compiler checks. +*/ +static void monitor_readline_printf (void *opaque,const char *fmt, ...) +{ +va_list ap; +va_start (ap, fmt); +monitor_vprintf (opaque, fmt, ap); +va_end (ap); +} + +static void monitor_readline_flush (void *opaque) +{ +monitor_flush (opaque); +} +void monitor_init (CharDriverState *chr,int flags) {static int is_first_init =1 ;
@@ -4898,7 +4918,10 @@ void monitor_init(CharDriverState *chr, int flags) mon->chr = chr; mon->flags = flags;if (flags &MONITOR_USE_READLINE ) { - mon->rs =readline_init (mon, monitor_find_completion); + mon->rs =readline_init (monitor_readline_printf, + monitor_readline_flush, + mon, + monitor_find_completion);monitor_read_command (mon,0 ); }
@@ -4920,9 +4943,11 @@ void monitor_init(CharDriverState *chr, int flags) default_mon = mon; } -static void bdrv_password_cb (Monitor *mon,const char *password,void *opaque) +static void bdrv_password_cb (void *opaque,const char *password, +void *readline_opaque) { -BlockDriverState *bs = opaque; +Monitor *mon = opaque; +BlockDriverState *bs = readline_opaque;int ret =0 ;if (bdrv_set_key (bs, password) !=0 ) {
readline.c +23 -21
126 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 2/5 readline: move readline to a generic location
In reply to Stefan HajnocziNow that the monitor and readline are decoupled, readline.h no longer belongs in include/monitor/. Put the header into include/qemu/.
Move the source file into util/ so it can be linked as part of libqemuutil.a.
--- Makefile.objs | 1 - include/monitor/monitor.h | 2 +- include/monitor/readline.h | 62 ------ include/qemu/readline.h | 62 ++++++ monitor.c | 2 +- readline.c | 495 --------------------------------------------- util/Makefile.objs | 1 + util/readline.c | 495 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 560 insertions(+), 560 deletions(-) delete mode 100644 include/monitor/readline.h create mode 100644 include/qemu/readline.h delete mode 100644 readline.c create mode 100644 util/readline.c
Makefile.objs +0 -1
@@ -43,7 +43,6 @@ libcacard-y += libcacard/vcardt.o ifeq ($(CONFIG_SOFTMMU),y) common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o block/ common-obj-y += net/ -common-obj-y += readline.o common-obj-y += qdev-monitor.o device-hotplug.o common-obj-$(CONFIG_WIN32) += os-win32.o common-obj-$(CONFIG_POSIX) += os-posix.o
include/monitor/monitor.h +1 -1
@@ -5,7 +5,7 @@#include "qapi/qmp/qerror.h" #include "qapi/qmp/qdict.h" #include "block/block.h" -#include "monitor/readline.h" +#include "qemu/readline.h" extern Monitor *cur_mon;extern Monitor *default_mon;
include/monitor/readline.h deleted +0 -62
62 lines not on this page · raw message
include/qemu/readline.h new file +62 -0
62 lines not on this page · raw message
monitor.c +1 -1
@@ -37,7 +37,7 @@#include "ui/qemu-spice.h" #include "sysemu/sysemu.h" #include "monitor/monitor.h" -#include "monitor/readline.h" +#include "qemu/readline.h" #include "ui/console.h" #include "sysemu/blockdev.h" #include "audio/audio.h"
readline.c deleted +0 -495
495 lines not on this page · raw message
util/Makefile.objs +1 -0
@@ -12,3 +12,4 @@ util-obj-y += qemu-option.o qemu-progress.o util-obj-y += hexdump.o util-obj-y += crc32c.o util-obj-y += throttle.o +util-obj-y += readline.o
util/readline.c new file +495 -0
495 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 3/5 osdep: add qemu_set_tty_echo()
In reply to Stefan HajnocziUsing stdin with readline.c requires disabling echo and line buffering. Add a portable wrapper to set the terminal attributes under Linux and Windows.
--- include/qemu/osdep.h | 2 ++ util/oslib-posix.c | 18 ++++++++++++++++++ util/oslib-win32.c | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+)
include/qemu/osdep.h +2 -0
@@ -215,4 +215,6 @@ bool fips_get_state(void);*/ char *qemu_get_local_state_pathname (const char *relative_pathname); +void qemu_set_tty_echo (int fd,bool echo); +#endif
util/oslib-posix.c +18 -0
@@ -47,6 +47,9 @@ extern int daemon(int, int);# define QEMU_VMALLOC_ALIGN getpagesize()#endif +#include <termios.h> +#include <unistd.h> +#include <glib/gprintf.h> #include "config-host.h"
@@ -251,3 +254,18 @@ qemu_get_local_state_pathname(const char *relative_pathname)return g_strdup_printf ("%s/%s" ,CONFIG_QEMU_LOCALSTATEDIR , relative_pathname); } + +void qemu_set_tty_echo (int fd,bool echo) +{ +struct termios tty; + +tcgetattr (fd, &tty); + +if (echo) { + tty.c_lflag |=ECHO |ECHONL |ICANON |IEXTEN ; + }else { + tty.c_lflag &= ~(ECHO |ECHONL |ICANON |IEXTEN ); + } + +tcsetattr (fd,TCSANOW , &tty); +}
util/oslib-win32.c +19 -0
@@ -189,3 +189,22 @@ qemu_get_local_state_pathname(const char *relative_pathname)return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s" , base_path, relative_pathname); } + +void qemu_set_tty_echo (int fd,bool echo) +{ +HANDLE handle = (HANDLE )_get_osfhandle (fd); +DWORD dwMode =0 ; + +if (handle ==INVALID_HANDLE_VALUE ) { +return ; + } + +GetConsoleMode (handle, &dwMode); + +if (echo) { +SetConsoleMode (handle, dwMode |ENABLE_ECHO_INPUT |ENABLE_LINE_INPUT ); + }else { +SetConsoleMode (handle, + dwMode & ~(ENABLE_ECHO_INPUT |ENABLE_LINE_INPUT )); + } +}
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 3/5] osdep: add qemu_set_tty_echo()
Am 14.11.2013 um 11:54 hat Stefan Hajnoczi geschrieben:
Quoted 5 lines
Using stdin with
readline.crequires disabling echo and line buffering. Add a portable wrapper to set the terminal attributes under Linux and Windows.Signed-off-by Stefan Hajnoczi <[email protected]>
There's already qemu_chr_fe_set_echo(). Can't we use that? Changing this setting outside the context of a chardev feels wrong anyway.
Kevin
Re: [Qemu-devel] [PATCH 3/5] osdep: add qemu_set_tty_echo()
Am 29.11.2013 um 11:04 hat Kevin Wolf geschrieben:
Quoted 9 lines
Am 14.11.2013 um 11:54 hat Stefan Hajnoczi geschrieben:
Quoted 5 lines
Using stdin with
readline.crequires disabling echo and line buffering. Add a portable wrapper to set the terminal attributes under Linux and Windows.Signed-off-by Stefan Hajnoczi <[email protected]>There's already
qemu_chr_fe_set_echo(). Can't we use that? Changing this setting outside the context of a chardev feels wrong anyway.
Okay, I see, tools don't have chardevs, unfortunately. But can we avoid duplication the other way round then, that is, let qemu-char.c use the new functions? They don't seem to be exactly the same, so I'm not completely sure if they are supposed to do different things or if they are just buggy in different ways.
Kevin
Re: [Qemu-devel] [PATCH 3/5] osdep: add qemu_set_tty_echo()
Quoted 8 lines · Kevin Wolf
Am 29.11.2013 um 11:04 hat Kevin Wolf geschrieben:
Quoted 7 lines
Am 14.11.2013 um 11:54 hat Stefan Hajnoczi geschrieben:
Quoted 5 lines
Using stdin with
readline.crequires disabling echo and line buffering. Add a portable wrapper to set the terminal attributes under Linux and Windows.Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 8 lines
Quoted 2 lines
There's already
qemu_chr_fe_set_echo(). Can't we use that? Changing this setting outside the context of a chardev feels wrong anyway.Okay, I see, tools don't have chardevs, unfortunately. But can we avoid duplication the other way round then, that is, let
qemu-char.cuse the new functions? They don't seem to be exactly the same, so I'm not completely sure if they are supposed to do different things or if they are just buggy in different ways.
I didn't see a nice way of doing that. The chardev approach is a little different - it tries to restore termios on shutdown by stashing it in a global variable.
PATCH 4/5 qemu-io: use readline.c
In reply to Stefan HajnocziUse 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.
--- qemu-io.c | 103 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 45 deletions(-)
qemu-io.c +58 -45
@@ -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 ; }
Signature · 1 line
1.8.3.1
PATCH 5/5 qemu-io: add command completion
In reply to Stefan HajnocziAutocomplete qemu-io commands at the interactive prompt.
Note this only completes command names and not their options.
--- include/qemu-io.h | 3 +++ qemu-io-cmds.c | 15 +++++++++++++++ qemu-io.c | 8 +++++++- 3 files changed, 25 insertions(+), 1 deletion(-)
include/qemu-io.h +3 -0
@@ -42,5 +42,8 @@ bool qemuio_command(BlockDriverState *bs, const char *cmd);void qemuio_add_command (const cmdinfo_t *ci);int qemuio_command_usage (const cmdinfo_t *ci); +void qemuio_complete_command (const char *input, +void (*fn)(const char *cmd,void *opaque), +void *opaque);#endif /* QEMU_IO_H */
qemu-io-cmds.c +15 -0
@@ -94,6 +94,21 @@ static const cmdinfo_t *find_command(const char *cmd)return NULL ; } +/* Invoke fn() for commands with a matching prefix */ +void qemuio_complete_command (const char *input, +void (*fn)(const char *cmd,void *opaque), +void *opaque) +{ +cmdinfo_t *ct; +size_t input_len =strlen (input); + +for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { +if (strncmp (input, ct->name, input_len) ==0 ) { +fn (ct->name, opaque); + } + } +} +static char **breakline (char *input,int *count) {int c =0 ;
qemu-io.c +7 -1
@@ -236,9 +236,15 @@ static void readline_func(void *opaque, const char *str, void *readline_opaque) *line =g_strdup (str); } +static void completion_match (const char *cmd,void *opaque) +{ +readline_add_completion (readline_state, cmd); +} +static void readline_completion_func (void *opaque,const char *str) { -/* No command or argument completion implemented yet */ +readline_set_completion_index (readline_state,strlen (str)); +qemuio_complete_command (str, completion_match,NULL ); }static char *fetchline_readline (void )
Signature · 1 line
1.8.3.1
Quoted 3 lines · On Thu, 14 Nov 2013 11:54:13 +0100 Stefan Hajnoczi <[email protected]> wrote:
This series decouples
readline.cfrom the QEMU monitor and then reuses it in qemu-io. This adds history and command completion to the qemu-io interactive prompt.
I don't know what's the state of this series, but I reviewed the HMP parts in detail, and took a glance at the qemu-io parts:
Quoted 29 lines
Stefan Hajnoczi (5):
readline: decouple readline from the monitor
readline: move readline to a generic location
osdep: addqemu_set_tty_echo()
qemu-io: usereadline.c
qemu-io: add command completionMakefile.objs | 1 - hmp.c | 6 +- include/monitor/monitor.h | 2 +- include/monitor/readline.h | 56 ----- include/qemu-io.h | 3 + include/qemu/osdep.h | 2 + include/qemu/readline.h | 62 ++++++ monitor.c | 41 +++- qemu-io-cmds.c | 15 ++ qemu-io.c | 109 +++++----- readline.c | 493 -------------------------------------------- util/Makefile.objs | 1 + util/oslib-posix.c | 18 ++ util/oslib-win32.c | 19 ++ util/readline.c | 495 +++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 716 insertions(+), 607 deletions(-) delete mode 100644 include/monitor/readline.h create mode 100644 include/qemu/readline.h delete mode 100644 readline.c create mode 100644 util/readline.c