From d7d438ef62aa7819d85aa91a952c0f7c9b85f401 Mon Sep 17 00:00:00 2001 From: "Fabio M. Di Nitto" Date: Thu, 28 Oct 2010 19:43:38 +0200 Subject: [PATCH] vty_cli: add basic concept of prompt fix line redraw when parsing history Signed-off-by: Fabio M. Di Nitto --- vty_cli.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/vty_cli.c b/vty_cli.c index 386f97231..cf7fc66c4 100644 --- a/vty_cli.c +++ b/vty_cli.c @@ -21,6 +21,11 @@ static const char telnet_backward_char[] = { 0x08, 0x0 }; static const char telnet_newline[] = { '\n', '\r', 0x0 }; +static void knet_vty_prompt(struct knet_vty *vty) +{ + knet_vty_write(vty, "%s", "knet> "); +} + static void knet_vty_reset_buf(struct knet_vty *vty) { memset(vty->line, 0, sizeof(vty->line)); @@ -54,20 +59,6 @@ static void knet_vty_add_to_buf(struct knet_vty *vty, unsigned char *buf, int po knet_vty_write(vty, "%s", telnet_backward_char); } -static void knet_vty_rewrite_line(struct knet_vty *vty) -{ - int i; - - for (i = 0; i <= vty->cursor_pos; i++) - knet_vty_write(vty, "%s", telnet_backward_char); - - knet_vty_write(vty, "%s", vty->line); - - for (i = 0; i < (vty->line_idx - vty->cursor_pos); i++) - knet_vty_write(vty, "%s", telnet_backward_char); - -} - static void knet_vty_forward_char(struct knet_vty *vty) { char buf[2]; @@ -245,7 +236,7 @@ static void knet_vty_history_print(struct knet_vty *vty) memcpy(vty->line, vty->history[vty->history_pos], len); vty->cursor_pos = vty->line_idx = len; - knet_vty_rewrite_line(vty); + knet_vty_write(vty, "%s", vty->line); } static void knet_vty_history_prev(struct knet_vty *vty) @@ -410,6 +401,7 @@ static int knet_vty_process_buf(struct knet_vty *vty, unsigned char *buf, int bu case CONTROL('C'): knet_vty_write(vty, "%s", telnet_newline); knet_vty_reset_buf(vty); + knet_vty_prompt(vty); break; case CONTROL('D'): knet_vty_delete_char(vty); @@ -454,6 +446,7 @@ static int knet_vty_process_buf(struct knet_vty *vty, unsigned char *buf, int bu knet_vty_history_add(vty); } knet_vty_reset_buf(vty); + knet_vty_prompt(vty); break; case '\t': log_info("command completion"); @@ -482,6 +475,8 @@ void knet_vty_cli_bind(struct knet_vty *vty) unsigned char buf[VTY_MAX_BUFFER_SIZE]; int readlen; + knet_vty_prompt(vty); + while (se_result >= 0 && !vty->got_epipe) { FD_ZERO (&rfds); FD_SET (vty->vty_sock, &rfds);