X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/f2175f495629af499e2bb950f1691ebc58569156..f1e16f884308e8ef720a4ecbcdcc97af97dce4bd:/avr/cli_readline.c?ds=sidebyside diff --git a/avr/cli_readline.c b/avr/cli_readline.c index 49ceee7..0ed8f67 100644 --- a/avr/cli_readline.c +++ b/avr/cli_readline.c @@ -1,5 +1,5 @@ /* - * (C) Copyright 2014 Leo C. + * (C) Copyright 2014-2016 Leo C. * * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. @@ -8,13 +8,13 @@ * (C) Copyright 2005 * JinHua Luo, GuangDong Linux Center, * - * SPDX-License-Identifier: GPL-2.0+ + * SPDX-License-Identifier: GPL-2.0 */ +#include "cli_readline.h" #include "common.h" #include #include -#include #include #include @@ -22,8 +22,22 @@ #include "con-utils.h" #include "print-utils.h" #include "command.h" -#include "cli_readline.h" +#include "debug.h" + +#define DEBUG_READLINE 0 /* set to 1 to debug */ + +#define debug_readline(fmt, args...) \ + debug_cond(DEBUG_READLINE, fmt, ##args) + + + +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) /************************************************************************************************/ @@ -67,23 +81,26 @@ struct fkey_tbl_s { static const FLASH struct fkey_tbl_s fkey_table[] = { FKEY_TBL_ITEM(B, KEY_DOWN), // Down arrow key -FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key +FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key -FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key +FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key FKEY_TBL_ITEM(1~, KEY_HOME), // Home key -FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key -FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key -FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key -FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key +FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key +FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key +FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key +FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key FKEY_TBL_ITEM(4~, KEY_END), // End key FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key +/* */ +FKEY_TBL_ITEM(H, KEY_HOME), // Home key +FKEY_TBL_ITEM(F, KEY_END), // End key /* VT400: */ FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5 -/* Linux consoe */ +/* Linux console */ FKEY_TBL_ITEM([A, KEY_F(1)), // Function key F1 FKEY_TBL_ITEM([B, KEY_F(2)), // Function key F2 FKEY_TBL_ITEM([C, KEY_F(3)), // Function key F3 @@ -94,9 +111,9 @@ FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9 -FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10 -FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11 -FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12 +FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10 +FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11 +FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12 { NULL } /* Mark end of table */ }; @@ -105,7 +122,8 @@ FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12 typedef enum { STATE_GROUND, STATE_ESCAPE, - STATE_CSI_ENTRY + STATE_CSI_ENTRY, + STATE_SS3 } vtparse_state_t; #define CHB_SIZE 15 @@ -116,7 +134,7 @@ int vt_parse (void) static vtparse_state_t state = STATE_GROUND; char buf[CHB_SIZE+1]; uint8_t param[2]; - uint8_t i_buf; + uint8_t i_buf = 0; uint8_t i_param; int ch; @@ -145,6 +163,17 @@ int vt_parse (void) i_param = 0; continue; } + if (ch == 'O') { + state = STATE_SS3; + continue; + } + state = STATE_GROUND; + break; + case STATE_SS3: + if (ch == 'F') + ch = KEY_END; + if (ch == 'H') + ch = KEY_HOME; state = STATE_GROUND; break; case STATE_CSI_ENTRY: @@ -188,181 +217,236 @@ int vt_parse (void) /************************************************************************************************/ +/* + * cmdline-editing related codes from vivi. + * Author: Janghoon Lyu + */ + -char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ +char histbuf[CONFIG_SYS_HISTSIZE+1]; +#define HISTBUFE (histbuf+CONFIG_SYS_HISTSIZE) -#ifndef CONFIG_CMDLINE_EDITING -static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */ -static const FLASH char tab_seq[] = " "; /* used to expand TABs */ +static char *hist_head; +static char *hist_cur; -static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) +static void hist_reset(void) { - char *s; - - if (*np == 0) - return p; + if (hist_head == NULL) + hist_head = HISTBUFE; + hist_cur = hist_head; +} - if (*(--p) == '\t') { /* will retype the whole line */ - while (*colp > plen) { - my_puts_P(erase_seq); - (*colp)--; - } - for (s = buffer; s < p; ++s) { - if (*s == '\t') { - my_puts_P(tab_seq + ((*colp) & 07)); - *colp += 8 - ((*colp) & 07); - } else { - ++(*colp); - putchar(*s); - } - } - } else { - my_puts_P(erase_seq); - (*colp)--; - } - (*np)--; +static char *hist_entry_next(char *p) +{ + if (*p) + while (*p++); return p; } -#endif /* CONFIG_CMDLINE_EDITING */ +static char *hist_entry_prev(char *p) +{ + if (p == hist_head) + return NULL; -#ifdef CONFIG_CMDLINE_EDITING + --p; + while (p != hist_head && *(p-1) != 0) + --p; -/* - * cmdline-editing related codes from vivi. - * Author: Janghoon Lyu - */ + return p; +} -static void putnstr(char *str, int n) +static char *hist_search_entry(char *line) { - /* printf_P(PSTR("%.*s"), (int)n, str) */ - while (n-- && *str) - putchar(*str++); + char *p = hist_head; + + while (*p && strcmp(p, line)) + p = hist_entry_next(p); + return *p ? p : NULL; } +static char *hist_delete(size_t amount) +{ + char *p; -#define CTL_CH(c) ((c) - 'a' + 1) -#define CTL_BACKSPACE ('\b') -#define DEL ((char)255) -#define DEL7 ((char)127) + p = HISTBUFE - amount; + if (p < hist_head) + p = hist_head; -#define getcmd_putch(ch) putchar(ch) -#define getcmd_getch() vt_parse() -#define getcmd_cbeep() getcmd_putch('\a') + while (p > hist_head && *(p-1)) + --p; -#define HIST_MAX 20 + if (p == hist_head) + hist_head = HISTBUFE; + else { + size_t shift = HISTBUFE - p; + size_t len = p - hist_head; + hist_head = memmove(hist_head + shift, hist_head, len); + } + return hist_head; +} -static int_fast8_t hist_max; -static int_fast8_t hist_add_idx; -static int_fast8_t hist_cur = -1; +static char *hist_delete_entry(char *entry) +{ + size_t shift = strlen(entry) + 1; + size_t len = entry - hist_head; -static char *hist_list[HIST_MAX]; + hist_head = memmove(hist_head + shift, hist_head, len); -static void hist_init(void) + return hist_head; +} + + +static char *hist_add_entry(char *line) { - int_fast8_t i; + char *p = hist_head; - hist_max = 0; - hist_add_idx = 0; - hist_cur = -1; + if (p == NULL) + p = HISTBUFE; - for (i = 0; i < HIST_MAX; i++) { - hist_list[i] = NULL; - } + char *q = p - strlen(line) - 1; + if (q < histbuf) + q = histbuf; + + strlcpy(q, line, p - q); + + hist_head = q; + + return hist_head; } -static void cread_add_to_hist(char *line) +static uint_fast8_t hist_get_count(void) { - if (hist_max) { - int_fast8_t last = hist_add_idx; - if (--last < 0) - last = hist_max; + char *p = hist_head; + uint_fast8_t n = 0; - if (!strcmp(line, hist_list[last])) - return; + while (*p) { + ++n; + while (*p++); } + return n; +} - char *p = strdup(line); - if (p) { - free(hist_list[hist_add_idx]); - hist_list[hist_add_idx] = p; - - if (++hist_add_idx >= HIST_MAX) - hist_add_idx = 0; +static int hist_get_size(void) +{ + return HISTBUFE - hist_head; +} - if (hist_add_idx > hist_max) - hist_max = hist_add_idx; +static char *cread_add_to_hist(char *line) +{ + char *p; + + p = hist_search_entry(line); + if (p) + hist_delete_entry(p); + else { + size_t free = hist_head - histbuf; + if (free < strlen(line) + 1) + hist_delete(strlen(line) + 1 - free); } + hist_add_entry(line); + + return p; } static char *hist_prev(void) { - char *ret; - int_fast8_t old_cur; + char *p = hist_cur; - if (hist_cur < 0) + if (*p == '\0') return NULL; + hist_cur = hist_entry_next(p); - old_cur = hist_cur; - if (--hist_cur < 0) - hist_cur = hist_max; - - if (hist_cur == hist_add_idx) { - hist_cur = old_cur; - ret = NULL; - } else { - ret = hist_list[hist_cur]; - } - - return ret; + return p; } static char *hist_next(void) { - char *ret; + char *p = hist_cur; - if (hist_cur < 0) + if(p == hist_head) return NULL; - if (hist_cur == hist_add_idx) + p = hist_entry_prev(p); + hist_cur = p; + + return p == hist_head ? "" : hist_entry_prev(p); +} + +static char *hist_search_backward(char* buf, uint8_t num) +{ + char *p = hist_cur; + + if (*p == '\0') return NULL; - if (++hist_cur > hist_max) - hist_cur = 0; + while (*p && strncmp(p, buf, num)) + p = hist_entry_next(p); + + if(!strncmp(p, buf, num)) { + hist_cur = hist_entry_next(p); + return p; + } + + return NULL; +} + +static char *hist_search_forward (char* buf, uint8_t num) +{ + char *p = hist_cur; + + if(p != hist_head && (p = hist_entry_prev(p)) != NULL) { + do { + p = hist_entry_prev(p); + } while (p && strncmp(p, buf, num) != 0); - if (hist_cur == hist_add_idx) - ret = ""; - else - ret = hist_list[hist_cur]; + //if(!strncmp(p, buf, num)) { + if(p) { + hist_cur = hist_entry_next(p); + return p; + } + } - return ret; + return NULL; } +static void putnstr(char *str, int n) +{ + /* printf_P(PSTR("%.*s"), (int)n, str) */ + while (n-- && *str) + putchar(*str++); +} -#define BEGINNING_OF_LINE() { \ - while (num) { \ - getcmd_putch(CTL_BACKSPACE); \ - num--; \ - } \ +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)--; + } } -#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 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); + } } -#define REFRESH_TO_EOL() { \ - if (num < eol_num) { \ - wlen = eol_num - num; \ - putnstr(buf + num, wlen); \ - num = eol_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, @@ -391,39 +475,37 @@ static void cread_add_char(char ichar, bool insert, uint_fast8_t *num, getcmd_putch(CTL_BACKSPACE); } else { /* echo the character */ - wlen = 1; buf[*num] = ichar; - putnstr(buf + *num, wlen); + putnstr(buf + *num, 1); (*num)++; } } -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) +static void cread_add_str(char *str, 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); - str++; - } + char c; + + while ((c = *str++) != '\0') + cread_add_char(c, insert, num, eol_num, buf, len); } -static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len) +static int cread_line(const FLASH char *const prompt, char *buf, + uint_fast8_t len, bool enable_history) { uint_fast8_t num = 0; uint_fast8_t eol_num = 0; - uint_fast8_t wlen; - int ichar; bool insert = 1; - int init_len = strlen(buf); (void) prompt; - if (init_len) - cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); + if (buf[0]) + cread_add_str(buf, 1, &num, &eol_num, buf, len); + + hist_reset(); while (1) { - ichar = getcmd_getch(); + int ichar = getcmd_getch(); if ((ichar == '\n') || (ichar == '\r')) { putchar('\n'); @@ -435,9 +517,10 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l case KEY_HOME: case CTL_CH('a'): - BEGINNING_OF_LINE(); + beginning_of_line(&num); break; case CTL_CH('c'): /* ^C - break */ + putchar('\n'); *buf = '\0'; /* discard input */ return -1; case KEY_RIGHT: @@ -457,7 +540,7 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l case KEY_DC: case CTL_CH('d'): /* delete-char */ if (num < eol_num) { - wlen = eol_num - num - 1; + uint_fast8_t wlen = eol_num - num - 1; if (wlen) { memmove(&buf[num], &buf[num+1], wlen); putnstr(buf + num, wlen); @@ -471,10 +554,11 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l } 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'): @@ -482,22 +566,21 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l 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: case 8: /* backward-delete-char */ if (num) { - wlen = eol_num - num; - num--; + uint_fast8_t wlen = eol_num - --num; + buf[eol_num] = ' '; memmove(&buf[num], &buf[num+1], wlen); getcmd_putch(CTL_BACKSPACE); putnstr(buf + num, wlen); - getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); - } while (wlen--); + } while (--wlen); eol_num--; } break; @@ -505,32 +588,54 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l case CTL_CH('p'): /* previous-history */ case KEY_DOWN: case CTL_CH('n'): /* next-history */ - { - char *hline; + if (enable_history) { + char *hline; + + if (ichar == CTL_CH('p') || ichar == KEY_UP) + hline = hist_prev(); + else + hline = hist_next(); + + if (hline) { + /* first, go home */ + beginning_of_line(&num); + /* overwrite current line */ + cread_add_str(hline, 0, &num, &eol_num, buf, len); + /* erase to end of line */ + erase_to_eol(&num, &eol_num); - if (ichar == CTL_CH('p') || ichar == KEY_UP) - hline = hist_prev(); - else - hline = hist_next(); - - if (!hline) { + } else { + getcmd_cbeep(); + } + } else { getcmd_cbeep(); - continue; } - - /* nuke the current line */ - /* first, go home */ - BEGINNING_OF_LINE(); - - /* erase to end of line */ - ERASE_TO_EOL(); - - /* copy new line into place and display */ - strcpy(buf, hline); - eol_num = strlen(buf); - REFRESH_TO_EOL(); - continue; - } + break; + 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(buf, num); + else + hline = hist_search_forward(buf, num); + + if (hline) { + uint_fast8_t num2 = num; + /* overwrite current line from cursor position */ + cread_add_str(hline+num, 0, &num2, &eol_num, buf, len); + /* erase to end of line */ + erase_to_eol(&num2, &eol_num); + /* cursor back */ + while (num2-- > num) + getcmd_putch(CTL_BACKSPACE); + } else { + getcmd_cbeep(); + } + } else { + getcmd_cbeep(); + } + break; #ifdef CONFIG_AUTO_COMPLETE case '\t': { int num2, col; @@ -554,135 +659,34 @@ static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *l #endif default: if (isprint(ichar)) - cread_add_char(ichar, insert, &num, &eol_num, buf, - *len); + cread_add_char(ichar, insert, &num, &eol_num, buf, len); break; } } - *len = eol_num; + while (eol_num && buf[eol_num-1] == ' ') + --eol_num; /* remove trailing blanks */ buf[eol_num] = '\0'; /* lose the newline */ - if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */) - cread_add_to_hist(buf); - hist_cur = hist_add_idx; - - return 0; -} - -#endif /* CONFIG_CMDLINE_EDITING */ - -/****************************************************************************/ - -static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer) -{ - char *p = buffer; -#ifdef CONFIG_CMDLINE_EDITING - uint_fast8_t len = CONFIG_SYS_CBSIZE; - int rc; - static bool initted; - - if (!initted) { - hist_init(); - initted = 1; + uint_fast8_t i = 0; + while (buf[i] == ' ') + ++i; /* remove leading blanks */ + if (i) { + eol_num -= i; + memmove(buf, buf+i, eol_num+1); } - if (prompt) - my_puts_P(prompt); - - rc = cread_line(prompt, p, &len); - return rc < 0 ? rc : (int) len; - -#else /* CONFIG_CMDLINE_EDITING */ - char *p_buf = p; - int n = 0; /* buffer index */ - int plen = 0; /* prompt length */ - int col; /* output column cnt */ - char c; - - /* print prompt */ - if (prompt) { - plen = strlen_P(prompt); - my_puts_P(prompt); - } - col = plen; - - for (;;) { - - c = my_getchar(1); - - /* - * Special character handling - */ - switch (c) { - case '\r': /* Enter */ - case '\n': - *p = '\0'; - my_puts_P(PSTR("\r\n")); - return p - p_buf; - - case '\0': /* nul */ - continue; - - case 0x03: /* ^C - break */ - p_buf[0] = '\0'; /* discard input */ - return -1; - - case 0x15: /* ^U - erase line */ - while (col > plen) { - my_puts_P(erase_seq); - --col; - } - p = p_buf; - n = 0; - continue; - - case 0x17: /* ^W - erase word */ - p = delete_char(p_buf, p, &col, &n, plen); - while ((n > 0) && (*p != ' ')) - p = delete_char(p_buf, p, &col, &n, plen); - continue; - - case 0x08: /* ^H - backspace */ - case 0x7F: /* DEL - backspace */ - p = delete_char(p_buf, p, &col, &n, plen); - continue; - - default: - /* - * Must be a normal character then - */ - if (n < CONFIG_SYS_CBSIZE-2) { - if (c == '\t') { /* expand TABs */ -#ifdef CONFIG_AUTO_COMPLETE - /* - * if auto completion triggered just - * continue - */ - *p = '\0'; - if (cmd_auto_complete(prompt, - console_buffer, - &n, &col)) { - p = p_buf + n; /* reset */ - continue; - } -#endif - my_puts_P(tab_seq + (col & 07)); - col += 8 - (col & 07); - } else { - ++col; - putchar(c); - } - *p++ = c; - ++n; - } else { /* Buffer full */ - putchar('\a'); - } - } + debug_readline("### hist_head: %p, hist_cur: %p\n", hist_head, hist_cur); + if (enable_history && buf[0]) { + cread_add_to_hist(buf); + debug_readline("### hist_head: %p, hist_cur: %p, hist_size: %3d, hist_count: %2d\n", + hist_head, hist_cur, hist_get_size(), hist_get_count()); } -#endif /* CONFIG_CMDLINE_EDITING */ + return eol_num; } -int cli_readline(const FLASH char *const prompt) +/****************************************************************************/ + +int cli_readline(const FLASH char *const prompt, bool enable_history) { /* * If console_buffer isn't 0-length the user will be prompted to modify @@ -690,5 +694,8 @@ int cli_readline(const FLASH char *const prompt) */ console_buffer[0] = '\0'; - return cli_readline_into_buffer(prompt, console_buffer); + if (prompt) + my_puts_P(prompt); + + return cread_line(prompt, console_buffer, CONFIG_SYS_CBSIZE, enable_history); }