diff options
author | aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162> | 2009-03-05 23:01:37 +0000 |
---|---|---|
committer | aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162> | 2009-03-05 23:01:37 +0000 |
commit | 4c36ba323582773a87e9d277b0ce8febcf2113fd (patch) | |
tree | 97a1cb91b1c14722266cbc5f5c9b6de8e7b0207c | |
parent | bb806047e22f20b96fc4f2e1a8c93bfe16e552d7 (diff) |
monitor: Introduce ReadLineState (Jan Kiszka)
As another step towards decoupled monitor terminals encapsulate the
state of the readline processor in a separate data structure called
ReadLineState and adapt all interfaces appropriately. For now the
monitor continues to instantiate just a single readline state.
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6714 c046a42c-6fe2-441c-8c8c-71466251a162
-rw-r--r-- | monitor.c | 36 | ||||
-rw-r--r-- | readline.c | 396 | ||||
-rw-r--r-- | readline.h | 48 |
3 files changed, 249 insertions, 231 deletions
@@ -81,6 +81,7 @@ static uint8_t term_outbuf[1024]; static int term_outbuf_index; static BlockDriverCompletionFunc *password_completion_cb; static void *password_opaque; +ReadLineState *rs; Monitor *cur_mon = NULL; @@ -91,7 +92,7 @@ static CPUState *mon_cpu = NULL; static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, void *opaque) { - readline_start("Password: ", 1, readline_func, opaque); + readline_start(rs, "Password: ", 1, readline_func, opaque); } void monitor_flush(Monitor *mon) @@ -366,7 +367,7 @@ static void do_info_history(Monitor *mon) i = 0; for(;;) { - str = readline_get_history(i); + str = readline_get_history(rs, i); if (!str) break; monitor_printf(mon, "%d: '%s'\n", i, str); @@ -2687,7 +2688,7 @@ static void cmd_completion(const char *name, const char *list) memcpy(cmd, pstart, len); cmd[len] = '\0'; if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { - readline_add_completion(cmd); + readline_add_completion(rs, cmd); } if (*p == '\0') break; @@ -2740,7 +2741,7 @@ static void file_completion(const char *input) stat(file, &sb); if(S_ISDIR(sb.st_mode)) pstrcat(file, sizeof(file), "/"); - readline_add_completion(file); + readline_add_completion(rs, file); } } closedir(ffs); @@ -2753,7 +2754,7 @@ static void block_completion_it(void *opaque, BlockDriverState *bs) if (input[0] == '\0' || !strncmp(name, (char *)input, strlen(input))) { - readline_add_completion(name); + readline_add_completion(rs, name); } } @@ -2783,7 +2784,7 @@ static void parse_cmdline(const char *cmdline, *pnb_args = nb_args; } -void readline_find_completion(const char *cmdline) +static void monitor_find_completion(const char *cmdline) { const char *cmdname; char *args[MAX_ARGS]; @@ -2813,7 +2814,7 @@ void readline_find_completion(const char *cmdline) cmdname = ""; else cmdname = args[0]; - readline_set_completion_index(strlen(cmdname)); + readline_set_completion_index(rs, strlen(cmdname)); for(cmd = mon_cmds; cmd->name != NULL; cmd++) { cmd_completion(cmdname, cmd->name); } @@ -2837,23 +2838,23 @@ void readline_find_completion(const char *cmdline) switch(*ptype) { case 'F': /* file completion */ - readline_set_completion_index(strlen(str)); + readline_set_completion_index(rs, strlen(str)); file_completion(str); break; case 'B': /* block device name completion */ - readline_set_completion_index(strlen(str)); + readline_set_completion_index(rs, strlen(str)); bdrv_iterate(block_completion_it, (void *)str); break; case 's': /* XXX: more generic ? */ if (!strcmp(cmd->name, "info")) { - readline_set_completion_index(strlen(str)); + readline_set_completion_index(rs, strlen(str)); for(cmd = info_cmds; cmd->name != NULL; cmd++) { cmd_completion(str, cmd->name); } } else if (!strcmp(cmd->name, "sendkey")) { - readline_set_completion_index(strlen(str)); + readline_set_completion_index(rs, strlen(str)); for(key = key_defs; key->name != NULL; key++) { cmd_completion(str, key->name); } @@ -2876,8 +2877,8 @@ static void term_read(void *opaque, const uint8_t *buf, int size) { int i; - for (i = 0; i < size; i++) - readline_handle_byte(buf[i]); + for(i = 0; i < size; i++) + readline_handle_byte(rs, buf[i]); } static int monitor_suspended; @@ -2886,7 +2887,7 @@ static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) { monitor_handle_command(mon, cmdline); if (!monitor_suspended) - readline_show_prompt(); + readline_show_prompt(rs); else monitor_suspended = 2; } @@ -2905,8 +2906,8 @@ void monitor_resume(Monitor *mon) static void monitor_start_input(void) { - readline_start("(qemu) ", 0, monitor_command_cb, NULL); - readline_show_prompt(); + readline_start(rs, "(qemu) ", 0, monitor_command_cb, NULL); + readline_show_prompt(rs); } static void term_event(void *opaque, int event) @@ -2935,6 +2936,7 @@ void monitor_init(CharDriverState *chr) mon = qemu_mallocz(sizeof(*mon)); mon->chr = chr; + rs = readline_init(mon, monitor_find_completion); qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, mon); @@ -2942,7 +2944,7 @@ void monitor_init(CharDriverState *chr) if (!cur_mon) cur_mon = mon; - readline_start("", 0, monitor_command_cb, NULL); + readline_start(rs, "", 0, monitor_command_cb, NULL); } static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) diff --git a/readline.c b/readline.c index ffa5424faf..1868b1082b 100644 --- a/readline.c +++ b/readline.c @@ -24,148 +24,118 @@ #include "readline.h" #include "monitor.h" -#define TERM_CMD_BUF_SIZE 4095 -#define TERM_MAX_CMDS 64 -#define NB_COMPLETIONS_MAX 256 - #define IS_NORM 0 #define IS_ESC 1 #define IS_CSI 2 #define printf do_not_use_printf -static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1]; -static int term_cmd_buf_index; -static int term_cmd_buf_size; - -static char term_last_cmd_buf[TERM_CMD_BUF_SIZE + 1]; -static int term_last_cmd_buf_index; -static int term_last_cmd_buf_size; - -static int term_esc_state; -static int term_esc_param; - -static char *term_history[TERM_MAX_CMDS]; -static int term_hist_entry = -1; - -static int nb_completions; -static int completion_index; -static char *completions[NB_COMPLETIONS_MAX]; - -static ReadLineFunc *term_readline_func; -static int term_is_password; -static char term_prompt[256]; -static void *term_readline_opaque; - -void readline_show_prompt(void) +void readline_show_prompt(ReadLineState *rs) { - Monitor *mon = cur_mon; - - monitor_printf(mon, "%s", term_prompt); - monitor_flush(mon); - term_last_cmd_buf_index = 0; - term_last_cmd_buf_size = 0; - term_esc_state = IS_NORM; + monitor_printf(rs->mon, "%s", rs->prompt); + monitor_flush(rs->mon); + rs->last_cmd_buf_index = 0; + rs->last_cmd_buf_size = 0; + rs->esc_state = IS_NORM; } /* update the displayed command line */ -static void term_update(void) +static void readline_update(ReadLineState *rs) { - Monitor *mon = cur_mon; int i, delta, len; - if (term_cmd_buf_size != term_last_cmd_buf_size || - memcmp(term_cmd_buf, term_last_cmd_buf, term_cmd_buf_size) != 0) { - for(i = 0; i < term_last_cmd_buf_index; i++) { - monitor_printf(mon, "\033[D"); + 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++) { + monitor_printf(rs->mon, "\033[D"); } - term_cmd_buf[term_cmd_buf_size] = '\0'; - if (term_is_password) { - len = strlen(term_cmd_buf); + rs->cmd_buf[rs->cmd_buf_size] = '\0'; + if (rs->read_password) { + len = strlen(rs->cmd_buf); for(i = 0; i < len; i++) - monitor_printf(mon, "*"); + monitor_printf(rs->mon, "*"); } else { - monitor_printf(mon, "%s", term_cmd_buf); + monitor_printf(rs->mon, "%s", rs->cmd_buf); } - monitor_printf(mon, "\033[K"); - memcpy(term_last_cmd_buf, term_cmd_buf, term_cmd_buf_size); - term_last_cmd_buf_size = term_cmd_buf_size; - term_last_cmd_buf_index = term_cmd_buf_size; + monitor_printf(rs->mon, "\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 (term_cmd_buf_index != term_last_cmd_buf_index) { - delta = term_cmd_buf_index - term_last_cmd_buf_index; + 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++) { - monitor_printf(mon, "\033[C"); + monitor_printf(rs->mon, "\033[C"); } } else { delta = -delta; for(i = 0;i < delta; i++) { - monitor_printf(mon, "\033[D"); + monitor_printf(rs->mon, "\033[D"); } } - term_last_cmd_buf_index = term_cmd_buf_index; + rs->last_cmd_buf_index = rs->cmd_buf_index; } - monitor_flush(mon); + monitor_flush(rs->mon); } -static void term_insert_char(int ch) +static void readline_insert_char(ReadLineState *rs, int ch) { - if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) { - memmove(term_cmd_buf + term_cmd_buf_index + 1, - term_cmd_buf + term_cmd_buf_index, - term_cmd_buf_size - term_cmd_buf_index); - term_cmd_buf[term_cmd_buf_index] = ch; - term_cmd_buf_size++; - term_cmd_buf_index++; + 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 term_backward_char(void) +static void readline_backward_char(ReadLineState *rs) { - if (term_cmd_buf_index > 0) { - term_cmd_buf_index--; + if (rs->cmd_buf_index > 0) { + rs->cmd_buf_index--; } } -static void term_forward_char(void) +static void readline_forward_char(ReadLineState *rs) { - if (term_cmd_buf_index < term_cmd_buf_size) { - term_cmd_buf_index++; + if (rs->cmd_buf_index < rs->cmd_buf_size) { + rs->cmd_buf_index++; } } -static void term_delete_char(void) +static void readline_delete_char(ReadLineState *rs) { - if (term_cmd_buf_index < term_cmd_buf_size) { - memmove(term_cmd_buf + term_cmd_buf_index, - term_cmd_buf + term_cmd_buf_index + 1, - term_cmd_buf_size - term_cmd_buf_index - 1); - term_cmd_buf_size--; + 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 term_backspace(void) +static void readline_backspace(ReadLineState *rs) { - if (term_cmd_buf_index > 0) { - term_backward_char(); - term_delete_char(); + if (rs->cmd_buf_index > 0) { + readline_backward_char(rs); + readline_delete_char(rs); } } -static void term_backword(void) +static void readline_backword(ReadLineState *rs) { int start; - if (term_cmd_buf_index == 0 || term_cmd_buf_index > term_cmd_buf_size) { + if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { return; } - start = term_cmd_buf_index - 1; + start = rs->cmd_buf_index - 1; /* find first word (backwards) */ while (start > 0) { - if (!qemu_isspace(term_cmd_buf[start])) { + if (!qemu_isspace(rs->cmd_buf[start])) { break; } @@ -174,7 +144,7 @@ static void term_backword(void) /* find first space (backwards) */ while (start > 0) { - if (qemu_isspace(term_cmd_buf[start])) { + if (qemu_isspace(rs->cmd_buf[start])) { ++start; break; } @@ -183,61 +153,61 @@ static void term_backword(void) } /* remove word */ - if (start < term_cmd_buf_index) { - memmove(term_cmd_buf + start, - term_cmd_buf + term_cmd_buf_index, - term_cmd_buf_size - term_cmd_buf_index); - term_cmd_buf_size -= term_cmd_buf_index - start; - term_cmd_buf_index = start; + 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 term_bol(void) +static void readline_bol(ReadLineState *rs) { - term_cmd_buf_index = 0; + rs->cmd_buf_index = 0; } -static void term_eol(void) +static void readline_eol(ReadLineState *rs) { - term_cmd_buf_index = term_cmd_buf_size; + rs->cmd_buf_index = rs->cmd_buf_size; } -static void term_up_char(void) +static void readline_up_char(ReadLineState *rs) { int idx; - if (term_hist_entry == 0) + if (rs->hist_entry == 0) return; - if (term_hist_entry == -1) { + if (rs->hist_entry == -1) { /* Find latest entry */ - for (idx = 0; idx < TERM_MAX_CMDS; idx++) { - if (term_history[idx] == NULL) + for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { + if (rs->history[idx] == NULL) break; } - term_hist_entry = idx; + rs->hist_entry = idx; } - term_hist_entry--; - if (term_hist_entry >= 0) { - pstrcpy(term_cmd_buf, sizeof(term_cmd_buf), - term_history[term_hist_entry]); - term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); + 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 term_down_char(void) +static void readline_down_char(ReadLineState *rs) { - if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1) + if (rs->hist_entry == READLINE_MAX_CMDS - 1 || rs->hist_entry == -1) return; - if (term_history[++term_hist_entry] != NULL) { - pstrcpy(term_cmd_buf, sizeof(term_cmd_buf), - term_history[term_hist_entry]); + if (rs->history[++rs->hist_entry] != NULL) { + pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), + rs->history[rs->hist_entry]); } else { - term_hist_entry = -1; + rs->hist_entry = -1; } - term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); + rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); } -static void term_hist_add(const char *cmdline) +static void readline_hist_add(ReadLineState *rs, const char *cmdline) { char *hist_entry, *new_entry; int idx; @@ -245,99 +215,99 @@ static void term_hist_add(const char *cmdline) if (cmdline[0] == '\0') return; new_entry = NULL; - if (term_hist_entry != -1) { + if (rs->hist_entry != -1) { /* We were editing an existing history entry: replace it */ - hist_entry = term_history[term_hist_entry]; - idx = term_hist_entry; + 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 < TERM_MAX_CMDS; idx++) { - hist_entry = term_history[idx]; + 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(&term_history[idx], &term_history[idx + 1], - (TERM_MAX_CMDS - idx + 1) * sizeof(char *)); - term_history[TERM_MAX_CMDS - 1] = NULL; - for (; idx < TERM_MAX_CMDS; idx++) { - if (term_history[idx] == NULL) + 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 == TERM_MAX_CMDS) { + if (idx == READLINE_MAX_CMDS) { /* Need to get one free slot */ - free(term_history[0]); - memcpy(term_history, &term_history[1], - (TERM_MAX_CMDS - 1) * sizeof(char *)); - term_history[TERM_MAX_CMDS - 1] = NULL; - idx = TERM_MAX_CMDS - 1; + free(rs->history[0]); + memcpy(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 = strdup(cmdline); - term_history[idx] = new_entry; - term_hist_entry = -1; + rs->history[idx] = new_entry; + rs->hist_entry = -1; } /* completion support */ -void readline_add_completion(const char *str) +void readline_add_completion(ReadLineState *rs, const char *str) { - if (nb_completions < NB_COMPLETIONS_MAX) { - completions[nb_completions++] = qemu_strdup(str); + if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { + rs->completions[rs->nb_completions++] = qemu_strdup(str); } } -void readline_set_completion_index(int index) +void readline_set_completion_index(ReadLineState *rs, int index) { - completion_index = index; + rs->completion_index = index; } -static void term_completion(void) +static void readline_completion(ReadLineState *rs) { Monitor *mon = cur_mon; int len, i, j, max_width, nb_cols, max_prefix; char *cmdline; - nb_completions = 0; + rs->nb_completions = 0; - cmdline = qemu_malloc(term_cmd_buf_index + 1); - memcpy(cmdline, term_cmd_buf, term_cmd_buf_index); - cmdline[term_cmd_buf_index] = '\0'; - readline_find_completion(cmdline); + cmdline = qemu_malloc(rs->cmd_buf_index + 1); + memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index); + cmdline[rs->cmd_buf_index] = '\0'; + rs->completion_finder(cmdline); qemu_free(cmdline); /* no completion found */ - if (nb_completions <= 0) + if (rs->nb_completions <= 0) return; - if (nb_completions == 1) { - len = strlen(completions[0]); - for(i = completion_index; i < len; i++) { - term_insert_char(completions[0][i]); + 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 && completions[0][len - 1] != '/') - term_insert_char(' '); + if (len > 0 && rs->completions[0][len - 1] != '/') + readline_insert_char(rs, ' '); } else { monitor_printf(mon, "\n"); max_width = 0; max_prefix = 0; - for(i = 0; i < nb_completions; i++) { - len = strlen(completions[i]); + 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; j<max_prefix; j++) { - if (completions[i][j] != completions[0][j]) + if (rs->completions[i][j] != rs->completions[0][j]) max_prefix = j; } } @@ -345,8 +315,8 @@ static void term_completion(void) max_width = len; } if (max_prefix > 0) - for(i = completion_index; i < max_prefix; i++) { - term_insert_char(completions[0][i]); + for(i = rs->completion_index; i < max_prefix; i++) { + readline_insert_char(rs, rs->completions[0][i]); } max_width += 2; if (max_width < 10) @@ -355,135 +325,147 @@ static void term_completion(void) max_width = 80; nb_cols = 80 / max_width; j = 0; - for(i = 0; i < nb_completions; i++) { - monitor_printf(mon, "%-*s", max_width, completions[i]); - if (++j == nb_cols || i == (nb_completions - 1)) { - monitor_printf(mon, "\n"); + for(i = 0; i < rs->nb_completions; i++) { + monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]); + if (++j == nb_cols || i == (rs->nb_completions - 1)) { + monitor_printf(rs->mon, "\n"); j = 0; } } - readline_show_prompt(); + readline_show_prompt(rs); } } /* return true if command handled */ -void readline_handle_byte(int ch) +void readline_handle_byte(ReadLineState *rs, int ch) { - Monitor *mon = cur_mon; - - switch(term_esc_state) { + switch(rs->esc_state) { case IS_NORM: switch(ch) { case 1: - term_bol(); + readline_bol(rs); break; case 4: - term_delete_char(); + readline_delete_char(rs); break; case 5: - term_eol(); + readline_eol(rs); break; case 9: - term_completion(); + readline_completion(rs); break; case 10: case 13: - term_cmd_buf[term_cmd_buf_size] = '\0'; - if (!term_is_password) - term_hist_add(term_cmd_buf); - monitor_printf(mon, "\n"); - term_cmd_buf_index = 0; - term_cmd_buf_size = 0; - term_last_cmd_buf_index = 0; - term_last_cmd_buf_size = 0; - /* NOTE: readline_start can be called here */ - term_readline_func(mon, term_cmd_buf, term_readline_opaque); + rs->cmd_buf[rs->cmd_buf_size] = '\0'; + if (!rs->read_password) + readline_hist_add(rs, rs->cmd_buf); + monitor_printf(rs->mon, "\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->mon, rs->cmd_buf, rs->readline_opaque); break; case 23: /* ^W */ - term_backword(); + readline_backword(rs); break; case 27: - term_esc_state = IS_ESC; + rs->esc_state = IS_ESC; break; case 127: case 8: - term_backspace(); + readline_backspace(rs); break; case 155: - term_esc_state = IS_CSI; + rs->esc_state = IS_CSI; break; default: if (ch >= 32) { - term_insert_char(ch); + readline_insert_char(rs, ch); } break; } break; case IS_ESC: if (ch == '[') { - term_esc_state = IS_CSI; - term_esc_param = 0; + rs->esc_state = IS_CSI; + rs->esc_param = 0; } else { - term_esc_state = IS_NORM; + rs->esc_state = IS_NORM; } break; case IS_CSI: switch(ch) { case 'A': case 'F': - term_up_char(); + readline_up_char(rs); break; case 'B': case 'E': - term_down_char(); + readline_down_char(rs); break; case 'D': - term_backward_char(); + readline_backward_char(rs); break; case 'C': - term_forward_char(); + readline_forward_char(rs); break; case '0' ... '9': - term_esc_param = term_esc_param * 10 + (ch - '0'); + rs->esc_param = rs->esc_param * 10 + (ch - '0'); goto the_end; case '~': - switch(term_esc_param) { + switch(rs->esc_param) { case 1: - term_bol(); + readline_bol(rs); break; case 3: - term_delete_char(); + readline_delete_char(rs); break; case 4: - term_eol(); + readline_eol(rs); break; } break; default: break; } - term_esc_state = IS_NORM; + rs->esc_state = IS_NORM; the_end: break; } - term_update(); + readline_update(rs); } -void readline_start(const char *prompt, int is_password, +void readline_start(ReadLineState *rs, const char *prompt, int read_password, ReadLineFunc *readline_func, void *opaque) { - pstrcpy(term_prompt, sizeof(term_prompt), prompt); - term_readline_func = readline_func; - term_readline_opaque = opaque; - term_is_password = is_password; - term_cmd_buf_index = 0; - term_cmd_buf_size = 0; + pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); + rs->readline_func = readline_func; + rs->readline_opaque = opaque; + rs->read_password = read_password; + rs->cmd_buf_index = 0; + rs->cmd_buf_size = 0; } -const char *readline_get_history(unsigned int index) +const char *readline_get_history(ReadLineState *rs, unsigned int index) { - if (index >= TERM_MAX_CMDS) + if (index >= READLINE_MAX_CMDS) return NULL; - return term_history[index]; + return rs->history[index]; +} + +ReadLineState *readline_init(Monitor *mon, + ReadLineCompletionFunc *completion_finder) +{ + ReadLineState *rs = qemu_mallocz(sizeof(*rs)); + + if (!rs) + return NULL; + + rs->hist_entry = -1; + rs->mon = mon; + rs->completion_finder = completion_finder; + + return rs; } diff --git a/readline.h b/readline.h index c5c10d6e3f..fcf0f3344c 100644 --- a/readline.h +++ b/readline.h @@ -3,18 +3,52 @@ #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(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; -void readline_add_completion(const char *str); -void readline_set_completion_index(int index); -void readline_find_completion(const char *cmdline); + char *history[READLINE_MAX_CMDS]; + int hist_entry; -const char *readline_get_history(unsigned int index); + ReadLineCompletionFunc *completion_finder; + char *completions[READLINE_MAX_COMPLETIONS]; + int nb_completions; + int completion_index; -void readline_handle_byte(int ch); + ReadLineFunc *readline_func; + void *readline_opaque; + int read_password; + char prompt[256]; + Monitor *mon; +} ReadLineState; -void readline_start(const char *prompt, int is_password, +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 *opaque); -void readline_show_prompt(void); +void readline_show_prompt(ReadLineState *rs); + +ReadLineState *readline_init(Monitor *mon, + ReadLineCompletionFunc *completion_finder); #endif /* !READLINE_H */ |