From: Leo C Date: Thu, 16 Jun 2016 14:13:22 +0000 (+0200) Subject: hist-search-backward / hist-search-forward (KEY_PPAGE/KEY_NPAGE) X-Git-Tag: hexrel-6.8.1~9 X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/e347ae0762d0179f0fb8354f85351e7ddac97b95 hist-search-backward / hist-search-forward (KEY_PPAGE/KEY_NPAGE) --- diff --git a/avr/cli_readline.c b/avr/cli_readline.c index 81230e0..045d16e 100644 --- a/avr/cli_readline.c +++ b/avr/cli_readline.c @@ -26,6 +26,14 @@ +char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ + +#define CTL_CH(c) ((c) - 'a' + 1) +#define CTL_BACKSPACE ('\b') +#define DEL ((char)255) +#define DEL7 ((char)127) + + /************************************************************************************************/ /* TODO: * @@ -198,31 +206,11 @@ int vt_parse (void) /************************************************************************************************/ - -char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ - /* * cmdline-editing related codes from vivi. * Author: Janghoon Lyu */ -static void putnstr(char *str, int n) -{ - /* printf_P(PSTR("%.*s"), (int)n, str) */ - while (n-- && *str) - putchar(*str++); -} - - -#define CTL_CH(c) ((c) - 'a' + 1) -#define CTL_BACKSPACE ('\b') -#define DEL ((char)255) -#define DEL7 ((char)127) - -#define getcmd_putch(ch) putchar(ch) -#define getcmd_getch() vt_parse() -#define getcmd_cbeep() getcmd_putch('\a') - struct hist_node_s { struct hist_node_s *next; @@ -234,6 +222,11 @@ typedef struct hist_node_s hist_node; static hist_node *hist_head; static hist_node *hist_cur; +static void hist_reset(void) +{ + hist_cur = hist_head; +} + static hist_node *hist_search_node(char *line) { hist_node *p = hist_head; @@ -342,8 +335,7 @@ static char *hist_prev(void) if (p == NULL) return NULL; - - hist_cur = hist_cur->next; + hist_cur = p->next; return p->line; } @@ -365,30 +357,83 @@ static char *hist_next(void) return p ? p->line : ""; } +static char *hist_search_backward(char* buf, uint8_t num) +{ + hist_node *p = hist_cur; + + if (p == NULL) + return NULL; + + while (p->next && strncmp(p->line, buf, num)) + p = p->next; -#define BEGINNING_OF_LINE() { \ - while (num) { \ - getcmd_putch(CTL_BACKSPACE); \ - num--; \ - } \ + if(!strncmp(p->line, buf, num)) { + hist_cur = p->next; + return p->line; + } + return NULL; } -#define ERASE_TO_EOL() { \ - if (num < eol_num) { \ - /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \ - print_blanks(eol_num - num); \ - do { \ - getcmd_putch(CTL_BACKSPACE); \ - } while (--eol_num > num); \ - } \ +static char *hist_search_forward (char* buf, uint8_t num) +{ + hist_node *p = NULL; + hist_node *match = NULL; + hist_node *q = hist_head; + + if(q == hist_cur) + return NULL; + + while(q->next != hist_cur) { + p = q; + q = q->next; + if (p && !strncmp(p->line, buf, num)) + match = p; + } + + if(match) { + hist_cur = match->next; + return match->line; + } + return NULL; } -#define REFRESH_TO_EOL() { \ - if (num < eol_num) { \ - uint_fast8_t wlen = eol_num - num; \ - putnstr(buf + num, wlen); \ - num = eol_num; \ - } \ +static void putnstr(char *str, int n) +{ + /* printf_P(PSTR("%.*s"), (int)n, str) */ + while (n-- && *str) + putchar(*str++); +} + +static void getcmd_putch(int ch) { putchar(ch);} +static int getcmd_getch(void) { return vt_parse();} +static void getcmd_cbeep(void) { getcmd_putch('\a');} + +static void beginning_of_line(uint8_t *num) +{ + while (*num) { + getcmd_putch(CTL_BACKSPACE); + (*num)--; + } +} + +static void erase_to_eol(uint_fast8_t *num, uint_fast8_t *eol_num) +{ + if (*num < *eol_num) { + /* printf_P(PSTR("%*S"), (int)(*eol_num - *num), PSTR("")); */ + print_blanks(*eol_num - *num); + do { + getcmd_putch(CTL_BACKSPACE); + } while (--(*eol_num) > *num); + } +} + +static void refresh_to_eol(char *buf, uint_fast8_t *num, uint_fast8_t *eol_num) +{ + if (*num < *eol_num) { + uint_fast8_t wlen = *eol_num - *num; + putnstr(buf + *num, wlen); + *num = *eol_num; + } } static void cread_add_char(char ichar, bool insert, uint_fast8_t *num, @@ -447,6 +492,8 @@ static int cread_line(const FLASH char *const prompt, char *buf, if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); + hist_reset(); + while (1) { int ichar = getcmd_getch(); @@ -460,7 +507,7 @@ static int cread_line(const FLASH char *const prompt, char *buf, case KEY_HOME: case CTL_CH('a'): - BEGINNING_OF_LINE(); + beginning_of_line(&num); break; case CTL_CH('c'): /* ^C - break */ *buf = '\0'; /* discard input */ @@ -496,11 +543,11 @@ static int cread_line(const FLASH char *const prompt, char *buf, } break; case CTL_CH('k'): /* kill-line */ - ERASE_TO_EOL(); + erase_to_eol(&num, &eol_num); break; case KEY_END: case CTL_CH('e'): - REFRESH_TO_EOL(); + refresh_to_eol(buf, &num, &eol_num); break; case KEY_IC: case CTL_CH('o'): @@ -508,8 +555,8 @@ static int cread_line(const FLASH char *const prompt, char *buf, break; case CTL_CH('x'): case CTL_CH('u'): /* kill-whole-line */ - BEGINNING_OF_LINE(); - ERASE_TO_EOL(); + beginning_of_line(&num); + erase_to_eol(&num, &eol_num); break; case DEL: case DEL7: @@ -542,15 +589,15 @@ static int cread_line(const FLASH char *const prompt, char *buf, if (hline) { /* nuke the current line */ /* first, go home */ - BEGINNING_OF_LINE(); + beginning_of_line(&num); /* erase to end of line */ - ERASE_TO_EOL(); + erase_to_eol(&num, &eol_num); /* copy new line into place and display */ strcpy(buf, hline); eol_num = strlen(buf); - REFRESH_TO_EOL(); + refresh_to_eol(buf, &num, &eol_num); } else { getcmd_cbeep(); } @@ -558,29 +605,29 @@ static int cread_line(const FLASH char *const prompt, char *buf, getcmd_cbeep(); } break; -#if 0 case KEY_PPAGE: /* history-search-backward */ case KEY_NPAGE: /* history-search-forward */ if (enable_history) { char *hline; if (ichar == KEY_PPAGE) - hline = hist_search_backward(); + hline = hist_search_backward(buf, num); else - hline = hist_search_forward(); + hline = hist_search_forward(buf, num); if (hline) { - /* nuke the current line */ - /* first, go home */ - BEGINNING_OF_LINE(); - /* erase to end of line */ - ERASE_TO_EOL(); + erase_to_eol(&num, &eol_num); /* copy new line into place and display */ - strcpy(buf, hline); + strcpy(buf+num, hline+num); eol_num = strlen(buf); - REFRESH_TO_EOL(); + uint8_t wlen = eol_num - num; + putnstr(buf + num, wlen); + getcmd_putch(' '); + do { + getcmd_putch(CTL_BACKSPACE); + } while (wlen--); } else { getcmd_cbeep(); } @@ -588,7 +635,6 @@ static int cread_line(const FLASH char *const prompt, char *buf, getcmd_cbeep(); } break; -#endif #ifdef CONFIG_AUTO_COMPLETE case '\t': { int num2, col; @@ -620,10 +666,8 @@ static int cread_line(const FLASH char *const prompt, char *buf, *len = eol_num; buf[eol_num] = '\0'; /* lose the newline */ - if (enable_history) { - if (buf[0]) - hist_cur = cread_add_to_hist(buf); - } + if (enable_history && buf[0]) + cread_add_to_hist(buf); return 0; }