From 0150cd81cf608b93778a067189829f354fe27e4b Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Thu, 14 Nov 2013 11:54:15 +0100 Subject: readline: move readline to a generic location Now 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. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- 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 diff --git a/Makefile.objs b/Makefile.objs index 857bb53..ac1d0e1 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -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 diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 22d8b8f..7e5f752 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -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; diff --git a/include/monitor/readline.h b/include/monitor/readline.h deleted file mode 100644 index a89fe4a..0000000 --- a/include/monitor/readline.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef READLINE_H -#define READLINE_H - -#define READLINE_CMD_BUF_SIZE 4095 -#define READLINE_MAX_CMDS 64 -#define READLINE_MAX_COMPLETIONS 256 - -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 { - char cmd_buf[READLINE_CMD_BUF_SIZE + 1]; - int cmd_buf_index; - int cmd_buf_size; - - char last_cmd_buf[READLINE_CMD_BUF_SIZE + 1]; - int last_cmd_buf_index; - int last_cmd_buf_size; - - int esc_state; - int esc_param; - - char *history[READLINE_MAX_CMDS]; - int hist_entry; - - ReadLineCompletionFunc *completion_finder; - char *completions[READLINE_MAX_COMPLETIONS]; - int nb_completions; - int completion_index; - - ReadLineFunc *readline_func; - void *readline_opaque; - int read_password; - char prompt[256]; - - ReadLinePrintfFunc *printf_func; - ReadLineFlushFunc *flush_func; - void *opaque; -} ReadLineState; - -void readline_add_completion(ReadLineState *rs, const char *str); -void readline_set_completion_index(ReadLineState *rs, int completion_index); - -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 *readline_opaque); -void readline_restart(ReadLineState *rs); -void readline_show_prompt(ReadLineState *rs); - -ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, - ReadLineFlushFunc *flush_func, - void *opaque, - ReadLineCompletionFunc *completion_finder); - -#endif /* !READLINE_H */ diff --git a/include/qemu/readline.h b/include/qemu/readline.h new file mode 100644 index 0000000..a89fe4a --- /dev/null +++ b/include/qemu/readline.h @@ -0,0 +1,62 @@ +#ifndef READLINE_H +#define READLINE_H + +#define READLINE_CMD_BUF_SIZE 4095 +#define READLINE_MAX_CMDS 64 +#define READLINE_MAX_COMPLETIONS 256 + +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 { + char cmd_buf[READLINE_CMD_BUF_SIZE + 1]; + int cmd_buf_index; + int cmd_buf_size; + + char last_cmd_buf[READLINE_CMD_BUF_SIZE + 1]; + int last_cmd_buf_index; + int last_cmd_buf_size; + + int esc_state; + int esc_param; + + char *history[READLINE_MAX_CMDS]; + int hist_entry; + + ReadLineCompletionFunc *completion_finder; + char *completions[READLINE_MAX_COMPLETIONS]; + int nb_completions; + int completion_index; + + ReadLineFunc *readline_func; + void *readline_opaque; + int read_password; + char prompt[256]; + + ReadLinePrintfFunc *printf_func; + ReadLineFlushFunc *flush_func; + void *opaque; +} ReadLineState; + +void readline_add_completion(ReadLineState *rs, const char *str); +void readline_set_completion_index(ReadLineState *rs, int completion_index); + +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 *readline_opaque); +void readline_restart(ReadLineState *rs); +void readline_show_prompt(ReadLineState *rs); + +ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, + ReadLineFlushFunc *flush_func, + void *opaque, + ReadLineCompletionFunc *completion_finder); + +#endif /* !READLINE_H */ diff --git a/monitor.c b/monitor.c index 32d0264..80456fb 100644 --- a/monitor.c +++ b/monitor.c @@ -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" diff --git a/readline.c b/readline.c deleted file mode 100644 index ca894d1..0000000 --- a/readline.c +++ /dev/null @@ -1,495 +0,0 @@ -/* - * QEMU readline utility - * - * Copyright (c) 2003-2004 Fabrice Bellard - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "qemu-common.h" -#include "monitor/readline.h" - -#define IS_NORM 0 -#define IS_ESC 1 -#define IS_CSI 2 -#define IS_SS3 3 - -void readline_show_prompt(ReadLineState *rs) -{ - rs->printf_func(rs->opaque, "%s", rs->prompt); - rs->flush_func(rs->opaque); - rs->last_cmd_buf_index = 0; - rs->last_cmd_buf_size = 0; - rs->esc_state = IS_NORM; -} - -/* update the displayed command line */ -static void readline_update(ReadLineState *rs) -{ - int i, delta, len; - - if (rs->cmd_buf_size != rs->last_cmd_buf_size || - memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { - for(i = 0; i < rs->last_cmd_buf_index; i++) { - rs->printf_func(rs->opaque, "\033[D"); - } - rs->cmd_buf[rs->cmd_buf_size] = '\0'; - if (rs->read_password) { - len = strlen(rs->cmd_buf); - for(i = 0; i < len; i++) - rs->printf_func(rs->opaque, "*"); - } else { - rs->printf_func(rs->opaque, "%s", rs->cmd_buf); - } - rs->printf_func(rs->opaque, "\033[K"); - memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size); - rs->last_cmd_buf_size = rs->cmd_buf_size; - rs->last_cmd_buf_index = rs->cmd_buf_size; - } - if (rs->cmd_buf_index != rs->last_cmd_buf_index) { - delta = rs->cmd_buf_index - rs->last_cmd_buf_index; - if (delta > 0) { - for(i = 0;i < delta; i++) { - rs->printf_func(rs->opaque, "\033[C"); - } - } else { - delta = -delta; - for(i = 0;i < delta; i++) { - rs->printf_func(rs->opaque, "\033[D"); - } - } - rs->last_cmd_buf_index = rs->cmd_buf_index; - } - rs->flush_func(rs->opaque); -} - -static void readline_insert_char(ReadLineState *rs, int ch) -{ - if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) { - memmove(rs->cmd_buf + rs->cmd_buf_index + 1, - rs->cmd_buf + rs->cmd_buf_index, - rs->cmd_buf_size - rs->cmd_buf_index); - rs->cmd_buf[rs->cmd_buf_index] = ch; - rs->cmd_buf_size++; - rs->cmd_buf_index++; - } -} - -static void readline_backward_char(ReadLineState *rs) -{ - if (rs->cmd_buf_index > 0) { - rs->cmd_buf_index--; - } -} - -static void readline_forward_char(ReadLineState *rs) -{ - if (rs->cmd_buf_index < rs->cmd_buf_size) { - rs->cmd_buf_index++; - } -} - -static void readline_delete_char(ReadLineState *rs) -{ - if (rs->cmd_buf_index < rs->cmd_buf_size) { - memmove(rs->cmd_buf + rs->cmd_buf_index, - rs->cmd_buf + rs->cmd_buf_index + 1, - rs->cmd_buf_size - rs->cmd_buf_index - 1); - rs->cmd_buf_size--; - } -} - -static void readline_backspace(ReadLineState *rs) -{ - if (rs->cmd_buf_index > 0) { - readline_backward_char(rs); - readline_delete_char(rs); - } -} - -static void readline_backword(ReadLineState *rs) -{ - int start; - - if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { - return; - } - - start = rs->cmd_buf_index - 1; - - /* find first word (backwards) */ - while (start > 0) { - if (!qemu_isspace(rs->cmd_buf[start])) { - break; - } - - --start; - } - - /* find first space (backwards) */ - while (start > 0) { - if (qemu_isspace(rs->cmd_buf[start])) { - ++start; - break; - } - - --start; - } - - /* remove word */ - if (start < rs->cmd_buf_index) { - memmove(rs->cmd_buf + start, - rs->cmd_buf + rs->cmd_buf_index, - rs->cmd_buf_size - rs->cmd_buf_index); - rs->cmd_buf_size -= rs->cmd_buf_index - start; - rs->cmd_buf_index = start; - } -} - -static void readline_bol(ReadLineState *rs) -{ - rs->cmd_buf_index = 0; -} - -static void readline_eol(ReadLineState *rs) -{ - rs->cmd_buf_index = rs->cmd_buf_size; -} - -static void readline_up_char(ReadLineState *rs) -{ - int idx; - - if (rs->hist_entry == 0) - return; - if (rs->hist_entry == -1) { - /* Find latest entry */ - for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { - if (rs->history[idx] == NULL) - break; - } - rs->hist_entry = idx; - } - rs->hist_entry--; - if (rs->hist_entry >= 0) { - pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), - rs->history[rs->hist_entry]); - rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); - } -} - -static void readline_down_char(ReadLineState *rs) -{ - if (rs->hist_entry == -1) - return; - if (rs->hist_entry < READLINE_MAX_CMDS - 1 && - rs->history[++rs->hist_entry] != NULL) { - pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), - rs->history[rs->hist_entry]); - } else { - rs->cmd_buf[0] = 0; - rs->hist_entry = -1; - } - rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); -} - -static void readline_hist_add(ReadLineState *rs, const char *cmdline) -{ - char *hist_entry, *new_entry; - int idx; - - if (cmdline[0] == '\0') - return; - new_entry = NULL; - if (rs->hist_entry != -1) { - /* We were editing an existing history entry: replace it */ - hist_entry = rs->history[rs->hist_entry]; - idx = rs->hist_entry; - if (strcmp(hist_entry, cmdline) == 0) { - goto same_entry; - } - } - /* Search cmdline in history buffers */ - for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { - hist_entry = rs->history[idx]; - if (hist_entry == NULL) - break; - if (strcmp(hist_entry, cmdline) == 0) { - same_entry: - new_entry = hist_entry; - /* Put this entry at the end of history */ - memmove(&rs->history[idx], &rs->history[idx + 1], - (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *)); - rs->history[READLINE_MAX_CMDS - 1] = NULL; - for (; idx < READLINE_MAX_CMDS; idx++) { - if (rs->history[idx] == NULL) - break; - } - break; - } - } - if (idx == READLINE_MAX_CMDS) { - /* Need to get one free slot */ - g_free(rs->history[0]); - memmove(rs->history, &rs->history[1], - (READLINE_MAX_CMDS - 1) * sizeof(char *)); - rs->history[READLINE_MAX_CMDS - 1] = NULL; - idx = READLINE_MAX_CMDS - 1; - } - if (new_entry == NULL) - new_entry = g_strdup(cmdline); - rs->history[idx] = new_entry; - rs->hist_entry = -1; -} - -/* completion support */ - -void readline_add_completion(ReadLineState *rs, const char *str) -{ - if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { - rs->completions[rs->nb_completions++] = g_strdup(str); - } -} - -void readline_set_completion_index(ReadLineState *rs, int index) -{ - rs->completion_index = index; -} - -static void readline_completion(ReadLineState *rs) -{ - int len, i, j, max_width, nb_cols, max_prefix; - char *cmdline; - - rs->nb_completions = 0; - - cmdline = g_malloc(rs->cmd_buf_index + 1); - memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index); - cmdline[rs->cmd_buf_index] = '\0'; - rs->completion_finder(rs->opaque, cmdline); - g_free(cmdline); - - /* no completion found */ - if (rs->nb_completions <= 0) - return; - if (rs->nb_completions == 1) { - len = strlen(rs->completions[0]); - for(i = rs->completion_index; i < len; i++) { - readline_insert_char(rs, rs->completions[0][i]); - } - /* extra space for next argument. XXX: make it more generic */ - if (len > 0 && rs->completions[0][len - 1] != '/') - readline_insert_char(rs, ' '); - } else { - rs->printf_func(rs->opaque, "\n"); - max_width = 0; - max_prefix = 0; - for(i = 0; i < rs->nb_completions; i++) { - len = strlen(rs->completions[i]); - if (i==0) { - max_prefix = len; - } else { - if (len < max_prefix) - max_prefix = len; - for(j=0; jcompletions[i][j] != rs->completions[0][j]) - max_prefix = j; - } - } - if (len > max_width) - max_width = len; - } - if (max_prefix > 0) - for(i = rs->completion_index; i < max_prefix; i++) { - readline_insert_char(rs, rs->completions[0][i]); - } - max_width += 2; - if (max_width < 10) - max_width = 10; - else if (max_width > 80) - max_width = 80; - nb_cols = 80 / max_width; - j = 0; - for(i = 0; i < rs->nb_completions; i++) { - rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); - if (++j == nb_cols || i == (rs->nb_completions - 1)) { - rs->printf_func(rs->opaque, "\n"); - j = 0; - } - } - readline_show_prompt(rs); - } - for (i = 0; i < rs->nb_completions; i++) { - g_free(rs->completions[i]); - } -} - -/* return true if command handled */ -void readline_handle_byte(ReadLineState *rs, int ch) -{ - switch(rs->esc_state) { - case IS_NORM: - switch(ch) { - case 1: - readline_bol(rs); - break; - case 4: - readline_delete_char(rs); - break; - case 5: - readline_eol(rs); - break; - case 9: - readline_completion(rs); - break; - case 10: - case 13: - rs->cmd_buf[rs->cmd_buf_size] = '\0'; - if (!rs->read_password) - readline_hist_add(rs, rs->cmd_buf); - rs->printf_func(rs->opaque, "\n"); - rs->cmd_buf_index = 0; - rs->cmd_buf_size = 0; - rs->last_cmd_buf_index = 0; - rs->last_cmd_buf_size = 0; - rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); - break; - case 23: - /* ^W */ - readline_backword(rs); - break; - case 27: - rs->esc_state = IS_ESC; - break; - case 127: - case 8: - readline_backspace(rs); - break; - case 155: - rs->esc_state = IS_CSI; - break; - default: - if (ch >= 32) { - readline_insert_char(rs, ch); - } - break; - } - break; - case IS_ESC: - if (ch == '[') { - rs->esc_state = IS_CSI; - rs->esc_param = 0; - } else if (ch == 'O') { - rs->esc_state = IS_SS3; - rs->esc_param = 0; - } else { - rs->esc_state = IS_NORM; - } - break; - case IS_CSI: - switch(ch) { - case 'A': - case 'F': - readline_up_char(rs); - break; - case 'B': - case 'E': - readline_down_char(rs); - break; - case 'D': - readline_backward_char(rs); - break; - case 'C': - readline_forward_char(rs); - break; - case '0' ... '9': - rs->esc_param = rs->esc_param * 10 + (ch - '0'); - goto the_end; - case '~': - switch(rs->esc_param) { - case 1: - readline_bol(rs); - break; - case 3: - readline_delete_char(rs); - break; - case 4: - readline_eol(rs); - break; - } - break; - default: - break; - } - rs->esc_state = IS_NORM; - the_end: - break; - case IS_SS3: - switch(ch) { - case 'F': - readline_eol(rs); - break; - case 'H': - readline_bol(rs); - break; - } - rs->esc_state = IS_NORM; - break; - } - readline_update(rs); -} - -void readline_start(ReadLineState *rs, const char *prompt, int read_password, - ReadLineFunc *readline_func, void *opaque) -{ - pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); - rs->readline_func = readline_func; - rs->readline_opaque = opaque; - rs->read_password = read_password; - readline_restart(rs); -} - -void readline_restart(ReadLineState *rs) -{ - rs->cmd_buf_index = 0; - rs->cmd_buf_size = 0; -} - -const char *readline_get_history(ReadLineState *rs, unsigned int index) -{ - if (index >= READLINE_MAX_CMDS) - return NULL; - return rs->history[index]; -} - -ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, - ReadLineFlushFunc *flush_func, - void *opaque, - ReadLineCompletionFunc *completion_finder) -{ - ReadLineState *rs = g_malloc0(sizeof(*rs)); - - rs->hist_entry = -1; - rs->opaque = opaque; - rs->printf_func = printf_func; - rs->flush_func = flush_func; - rs->completion_finder = completion_finder; - - return rs; -} diff --git a/util/Makefile.objs b/util/Makefile.objs index af3e5cb..937376b 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -13,3 +13,4 @@ util-obj-y += hexdump.o util-obj-y += crc32c.o util-obj-y += throttle.o util-obj-y += getauxval.o +util-obj-y += readline.o diff --git a/util/readline.c b/util/readline.c new file mode 100644 index 0000000..8441be4 --- /dev/null +++ b/util/readline.c @@ -0,0 +1,495 @@ +/* + * QEMU readline utility + * + * Copyright (c) 2003-2004 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "qemu-common.h" +#include "qemu/readline.h" + +#define IS_NORM 0 +#define IS_ESC 1 +#define IS_CSI 2 +#define IS_SS3 3 + +void readline_show_prompt(ReadLineState *rs) +{ + rs->printf_func(rs->opaque, "%s", rs->prompt); + rs->flush_func(rs->opaque); + rs->last_cmd_buf_index = 0; + rs->last_cmd_buf_size = 0; + rs->esc_state = IS_NORM; +} + +/* update the displayed command line */ +static void readline_update(ReadLineState *rs) +{ + int i, delta, len; + + if (rs->cmd_buf_size != rs->last_cmd_buf_size || + memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { + for(i = 0; i < rs->last_cmd_buf_index; i++) { + rs->printf_func(rs->opaque, "\033[D"); + } + rs->cmd_buf[rs->cmd_buf_size] = '\0'; + if (rs->read_password) { + len = strlen(rs->cmd_buf); + for(i = 0; i < len; i++) + rs->printf_func(rs->opaque, "*"); + } else { + rs->printf_func(rs->opaque, "%s", rs->cmd_buf); + } + rs->printf_func(rs->opaque, "\033[K"); + memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size); + rs->last_cmd_buf_size = rs->cmd_buf_size; + rs->last_cmd_buf_index = rs->cmd_buf_size; + } + if (rs->cmd_buf_index != rs->last_cmd_buf_index) { + delta = rs->cmd_buf_index - rs->last_cmd_buf_index; + if (delta > 0) { + for(i = 0;i < delta; i++) { + rs->printf_func(rs->opaque, "\033[C"); + } + } else { + delta = -delta; + for(i = 0;i < delta; i++) { + rs->printf_func(rs->opaque, "\033[D"); + } + } + rs->last_cmd_buf_index = rs->cmd_buf_index; + } + rs->flush_func(rs->opaque); +} + +static void readline_insert_char(ReadLineState *rs, int ch) +{ + if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) { + memmove(rs->cmd_buf + rs->cmd_buf_index + 1, + rs->cmd_buf + rs->cmd_buf_index, + rs->cmd_buf_size - rs->cmd_buf_index); + rs->cmd_buf[rs->cmd_buf_index] = ch; + rs->cmd_buf_size++; + rs->cmd_buf_index++; + } +} + +static void readline_backward_char(ReadLineState *rs) +{ + if (rs->cmd_buf_index > 0) { + rs->cmd_buf_index--; + } +} + +static void readline_forward_char(ReadLineState *rs) +{ + if (rs->cmd_buf_index < rs->cmd_buf_size) { + rs->cmd_buf_index++; + } +} + +static void readline_delete_char(ReadLineState *rs) +{ + if (rs->cmd_buf_index < rs->cmd_buf_size) { + memmove(rs->cmd_buf + rs->cmd_buf_index, + rs->cmd_buf + rs->cmd_buf_index + 1, + rs->cmd_buf_size - rs->cmd_buf_index - 1); + rs->cmd_buf_size--; + } +} + +static void readline_backspace(ReadLineState *rs) +{ + if (rs->cmd_buf_index > 0) { + readline_backward_char(rs); + readline_delete_char(rs); + } +} + +static void readline_backword(ReadLineState *rs) +{ + int start; + + if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { + return; + } + + start = rs->cmd_buf_index - 1; + + /* find first word (backwards) */ + while (start > 0) { + if (!qemu_isspace(rs->cmd_buf[start])) { + break; + } + + --start; + } + + /* find first space (backwards) */ + while (start > 0) { + if (qemu_isspace(rs->cmd_buf[start])) { + ++start; + break; + } + + --start; + } + + /* remove word */ + if (start < rs->cmd_buf_index) { + memmove(rs->cmd_buf + start, + rs->cmd_buf + rs->cmd_buf_index, + rs->cmd_buf_size - rs->cmd_buf_index); + rs->cmd_buf_size -= rs->cmd_buf_index - start; + rs->cmd_buf_index = start; + } +} + +static void readline_bol(ReadLineState *rs) +{ + rs->cmd_buf_index = 0; +} + +static void readline_eol(ReadLineState *rs) +{ + rs->cmd_buf_index = rs->cmd_buf_size; +} + +static void readline_up_char(ReadLineState *rs) +{ + int idx; + + if (rs->hist_entry == 0) + return; + if (rs->hist_entry == -1) { + /* Find latest entry */ + for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { + if (rs->history[idx] == NULL) + break; + } + rs->hist_entry = idx; + } + rs->hist_entry--; + if (rs->hist_entry >= 0) { + pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), + rs->history[rs->hist_entry]); + rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); + } +} + +static void readline_down_char(ReadLineState *rs) +{ + if (rs->hist_entry == -1) + return; + if (rs->hist_entry < READLINE_MAX_CMDS - 1 && + rs->history[++rs->hist_entry] != NULL) { + pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), + rs->history[rs->hist_entry]); + } else { + rs->cmd_buf[0] = 0; + rs->hist_entry = -1; + } + rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); +} + +static void readline_hist_add(ReadLineState *rs, const char *cmdline) +{ + char *hist_entry, *new_entry; + int idx; + + if (cmdline[0] == '\0') + return; + new_entry = NULL; + if (rs->hist_entry != -1) { + /* We were editing an existing history entry: replace it */ + hist_entry = rs->history[rs->hist_entry]; + idx = rs->hist_entry; + if (strcmp(hist_entry, cmdline) == 0) { + goto same_entry; + } + } + /* Search cmdline in history buffers */ + for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { + hist_entry = rs->history[idx]; + if (hist_entry == NULL) + break; + if (strcmp(hist_entry, cmdline) == 0) { + same_entry: + new_entry = hist_entry; + /* Put this entry at the end of history */ + memmove(&rs->history[idx], &rs->history[idx + 1], + (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *)); + rs->history[READLINE_MAX_CMDS - 1] = NULL; + for (; idx < READLINE_MAX_CMDS; idx++) { + if (rs->history[idx] == NULL) + break; + } + break; + } + } + if (idx == READLINE_MAX_CMDS) { + /* Need to get one free slot */ + g_free(rs->history[0]); + memmove(rs->history, &rs->history[1], + (READLINE_MAX_CMDS - 1) * sizeof(char *)); + rs->history[READLINE_MAX_CMDS - 1] = NULL; + idx = READLINE_MAX_CMDS - 1; + } + if (new_entry == NULL) + new_entry = g_strdup(cmdline); + rs->history[idx] = new_entry; + rs->hist_entry = -1; +} + +/* completion support */ + +void readline_add_completion(ReadLineState *rs, const char *str) +{ + if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { + rs->completions[rs->nb_completions++] = g_strdup(str); + } +} + +void readline_set_completion_index(ReadLineState *rs, int index) +{ + rs->completion_index = index; +} + +static void readline_completion(ReadLineState *rs) +{ + int len, i, j, max_width, nb_cols, max_prefix; + char *cmdline; + + rs->nb_completions = 0; + + cmdline = g_malloc(rs->cmd_buf_index + 1); + memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index); + cmdline[rs->cmd_buf_index] = '\0'; + rs->completion_finder(rs->opaque, cmdline); + g_free(cmdline); + + /* no completion found */ + if (rs->nb_completions <= 0) + return; + if (rs->nb_completions == 1) { + len = strlen(rs->completions[0]); + for(i = rs->completion_index; i < len; i++) { + readline_insert_char(rs, rs->completions[0][i]); + } + /* extra space for next argument. XXX: make it more generic */ + if (len > 0 && rs->completions[0][len - 1] != '/') + readline_insert_char(rs, ' '); + } else { + rs->printf_func(rs->opaque, "\n"); + max_width = 0; + max_prefix = 0; + for(i = 0; i < rs->nb_completions; i++) { + len = strlen(rs->completions[i]); + if (i==0) { + max_prefix = len; + } else { + if (len < max_prefix) + max_prefix = len; + for(j=0; jcompletions[i][j] != rs->completions[0][j]) + max_prefix = j; + } + } + if (len > max_width) + max_width = len; + } + if (max_prefix > 0) + for(i = rs->completion_index; i < max_prefix; i++) { + readline_insert_char(rs, rs->completions[0][i]); + } + max_width += 2; + if (max_width < 10) + max_width = 10; + else if (max_width > 80) + max_width = 80; + nb_cols = 80 / max_width; + j = 0; + for(i = 0; i < rs->nb_completions; i++) { + rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); + if (++j == nb_cols || i == (rs->nb_completions - 1)) { + rs->printf_func(rs->opaque, "\n"); + j = 0; + } + } + readline_show_prompt(rs); + } + for (i = 0; i < rs->nb_completions; i++) { + g_free(rs->completions[i]); + } +} + +/* return true if command handled */ +void readline_handle_byte(ReadLineState *rs, int ch) +{ + switch(rs->esc_state) { + case IS_NORM: + switch(ch) { + case 1: + readline_bol(rs); + break; + case 4: + readline_delete_char(rs); + break; + case 5: + readline_eol(rs); + break; + case 9: + readline_completion(rs); + break; + case 10: + case 13: + rs->cmd_buf[rs->cmd_buf_size] = '\0'; + if (!rs->read_password) + readline_hist_add(rs, rs->cmd_buf); + rs->printf_func(rs->opaque, "\n"); + rs->cmd_buf_index = 0; + rs->cmd_buf_size = 0; + rs->last_cmd_buf_index = 0; + rs->last_cmd_buf_size = 0; + rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); + break; + case 23: + /* ^W */ + readline_backword(rs); + break; + case 27: + rs->esc_state = IS_ESC; + break; + case 127: + case 8: + readline_backspace(rs); + break; + case 155: + rs->esc_state = IS_CSI; + break; + default: + if (ch >= 32) { + readline_insert_char(rs, ch); + } + break; + } + break; + case IS_ESC: + if (ch == '[') { + rs->esc_state = IS_CSI; + rs->esc_param = 0; + } else if (ch == 'O') { + rs->esc_state = IS_SS3; + rs->esc_param = 0; + } else { + rs->esc_state = IS_NORM; + } + break; + case IS_CSI: + switch(ch) { + case 'A': + case 'F': + readline_up_char(rs); + break; + case 'B': + case 'E': + readline_down_char(rs); + break; + case 'D': + readline_backward_char(rs); + break; + case 'C': + readline_forward_char(rs); + break; + case '0' ... '9': + rs->esc_param = rs->esc_param * 10 + (ch - '0'); + goto the_end; + case '~': + switch(rs->esc_param) { + case 1: + readline_bol(rs); + break; + case 3: + readline_delete_char(rs); + break; + case 4: + readline_eol(rs); + break; + } + break; + default: + break; + } + rs->esc_state = IS_NORM; + the_end: + break; + case IS_SS3: + switch(ch) { + case 'F': + readline_eol(rs); + break; + case 'H': + readline_bol(rs); + break; + } + rs->esc_state = IS_NORM; + break; + } + readline_update(rs); +} + +void readline_start(ReadLineState *rs, const char *prompt, int read_password, + ReadLineFunc *readline_func, void *opaque) +{ + pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); + rs->readline_func = readline_func; + rs->readline_opaque = opaque; + rs->read_password = read_password; + readline_restart(rs); +} + +void readline_restart(ReadLineState *rs) +{ + rs->cmd_buf_index = 0; + rs->cmd_buf_size = 0; +} + +const char *readline_get_history(ReadLineState *rs, unsigned int index) +{ + if (index >= READLINE_MAX_CMDS) + return NULL; + return rs->history[index]; +} + +ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, + ReadLineFlushFunc *flush_func, + void *opaque, + ReadLineCompletionFunc *completion_finder) +{ + ReadLineState *rs = g_malloc0(sizeof(*rs)); + + rs->hist_entry = -1; + rs->opaque = opaque; + rs->printf_func = printf_func; + rs->flush_func = flush_func; + rs->completion_finder = completion_finder; + + return rs; +} -- cgit v1.1