From: Leo C Date: Sat, 13 Jun 2015 18:01:19 +0000 (+0200) Subject: command line history X-Git-Tag: hexrel-6.5~1^2~4 X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/a8b4c9642aee2d7b901e4a7cd19b170a40a361eb command line history --- diff --git a/avr/cli_readline.c b/avr/cli_readline.c index ff83a3a..49ceee7 100644 --- a/avr/cli_readline.c +++ b/avr/cli_readline.c @@ -14,6 +14,8 @@ #include "common.h" #include #include +#include +#include #include #include "config.h" @@ -187,9 +189,6 @@ int vt_parse (void) /************************************************************************************************/ - - - char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ #ifndef CONFIG_CMDLINE_EDITING @@ -247,57 +246,60 @@ static void putnstr(char *str, int n) #define CTL_BACKSPACE ('\b') #define DEL ((char)255) #define DEL7 ((char)127) -#define CREAD_HIST_CHAR ('!') #define getcmd_putch(ch) putchar(ch) #define getcmd_getch() vt_parse() #define getcmd_cbeep() getcmd_putch('\a') -#define HIST_MAX 5 -#define HIST_SIZE CONFIG_SYS_CBSIZE +#define HIST_MAX 20 -static int hist_max; -static int hist_add_idx; -static int hist_cur = -1; -static unsigned hist_num; +static int_fast8_t hist_max; +static int_fast8_t hist_add_idx; +static int_fast8_t hist_cur = -1; static char *hist_list[HIST_MAX]; -static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ - -#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) static void hist_init(void) { - int i; + int_fast8_t i; hist_max = 0; hist_add_idx = 0; hist_cur = -1; - hist_num = 0; for (i = 0; i < HIST_MAX; i++) { - hist_list[i] = hist_lines[i]; - hist_list[i][0] = '\0'; + hist_list[i] = NULL; } } static void cread_add_to_hist(char *line) { - strcpy(hist_list[hist_add_idx], line); + if (hist_max) { + int_fast8_t last = hist_add_idx; + if (--last < 0) + last = hist_max; - if (++hist_add_idx >= HIST_MAX) - hist_add_idx = 0; + if (!strcmp(line, hist_list[last])) + return; + } - if (hist_add_idx > hist_max) - hist_max = hist_add_idx; + char *p = strdup(line); + if (p) { + free(hist_list[hist_add_idx]); + hist_list[hist_add_idx] = p; - hist_num++; + if (++hist_add_idx >= HIST_MAX) + hist_add_idx = 0; + + if (hist_add_idx > hist_max) + hist_max = hist_add_idx; + } } static char *hist_prev(void) { char *ret; - int old_cur; + int_fast8_t old_cur; if (hist_cur < 0) return NULL; @@ -363,10 +365,10 @@ static char *hist_next(void) } \ } -static void cread_add_char(char ichar, int insert, unsigned int *num, - unsigned int *eol_num, char *buf, unsigned int len) +static void cread_add_char(char ichar, bool insert, uint_fast8_t *num, + uint_fast8_t *eol_num, char *buf, uint_fast8_t len) { - unsigned int wlen; + uint_fast8_t wlen; /* room ??? */ if (insert || *num == *eol_num) { @@ -396,9 +398,9 @@ static void cread_add_char(char ichar, int insert, unsigned int *num, } } -static void cread_add_str(char *str, int strsize, int insert, - unsigned int *num, unsigned int *eol_num, - char *buf, unsigned int len) +static void cread_add_str(char *str, int strsize, bool insert, + uint_fast8_t *num, uint_fast8_t *eol_num, + char *buf, uint_fast8_t len) { while (strsize--) { cread_add_char(*str, insert, num, eol_num, buf, len); @@ -406,13 +408,13 @@ static void cread_add_str(char *str, int strsize, int insert, } } -static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *len) +static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len) { - unsigned int num = 0; - unsigned int eol_num = 0; - unsigned int wlen; + uint_fast8_t num = 0; + uint_fast8_t eol_num = 0; + uint_fast8_t wlen; int ichar; - int insert = 1; + bool insert = 1; int init_len = strlen(buf); (void) prompt; @@ -435,25 +437,25 @@ static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *l case CTL_CH('a'): BEGINNING_OF_LINE(); break; - case CTL_CH('c'): /* ^C - break */ - *buf = '\0'; /* discard input */ + case CTL_CH('c'): /* ^C - break */ + *buf = '\0'; /* discard input */ return -1; case KEY_RIGHT: - case CTL_CH('f'): + case CTL_CH('f'): /* forward-char */ if (num < eol_num) { getcmd_putch(buf[num]); num++; } break; case KEY_LEFT: - case CTL_CH('b'): + case CTL_CH('b'): /* backward-char */ if (num) { getcmd_putch(CTL_BACKSPACE); num--; } break; case KEY_DC: - case CTL_CH('d'): + case CTL_CH('d'): /* delete-char */ if (num < eol_num) { wlen = eol_num - num - 1; if (wlen) { @@ -468,7 +470,7 @@ static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *l eol_num--; } break; - case CTL_CH('k'): + case CTL_CH('k'): /* kill-line */ ERASE_TO_EOL(); break; case CTL_CH('e'): @@ -479,13 +481,13 @@ static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *l insert = !insert; break; case CTL_CH('x'): - case CTL_CH('u'): + case CTL_CH('u'): /* kill-whole-line */ BEGINNING_OF_LINE(); ERASE_TO_EOL(); break; case DEL: case DEL7: - case 8: + case 8: /* backward-delete-char */ if (num) { wlen = eol_num - num; num--; @@ -500,9 +502,9 @@ static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *l } break; case KEY_UP: - case CTL_CH('p'): + case CTL_CH('p'): /* previous-history */ case KEY_DOWN: - case CTL_CH('n'): + case CTL_CH('n'): /* next-history */ { char *hline; @@ -575,9 +577,9 @@ static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer { char *p = buffer; #ifdef CONFIG_CMDLINE_EDITING - unsigned int len = CONFIG_SYS_CBSIZE; + uint_fast8_t len = CONFIG_SYS_CBSIZE; int rc; - static int initted; + static bool initted; if (!initted) { hist_init(); diff --git a/avr/con-utils.c b/avr/con-utils.c index 5ee1ff1..4a96771 100644 --- a/avr/con-utils.c +++ b/avr/con-utils.c @@ -64,6 +64,7 @@ static uint_fast8_t ctrlc_was_pressed; uint_fast8_t ctrlc(void) { + bg_shed(); if (!ctrlc_disabled) { switch (serial_getc()) { case 0x03: /* ^C - Control C */