X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/e0b20381498f524cb1902dc486249a967edf2592..16af58eaba93819b09becadf7caae0df890baed0:/avr/cli_readline.c diff --git a/avr/cli_readline.c b/avr/cli_readline.c index ff83a3a..32c15c8 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,19 +8,21 @@ * (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 #include "config.h" #include "con-utils.h" #include "print-utils.h" #include "command.h" -#include "cli_readline.h" @@ -187,49 +189,8 @@ int vt_parse (void) /************************************************************************************************/ - - - char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ -#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 *delete_char (char *buffer, char *p, int *colp, int *np, int plen) -{ - char *s; - - if (*np == 0) - return p; - - 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)--; - - return p; -} -#endif /* CONFIG_CMDLINE_EDITING */ - - -#ifdef CONFIG_CMDLINE_EDITING - /* * cmdline-editing related codes from vivi. * Author: Janghoon Lyu @@ -247,57 +208,47 @@ 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) +static void cread_add_to_hist(char *line) { - int i; - - hist_max = 0; - hist_add_idx = 0; - hist_cur = -1; - hist_num = 0; + if (hist_max) { + int_fast8_t last = hist_add_idx; + if (--last < 0) + last = hist_max; - for (i = 0; i < HIST_MAX; i++) { - hist_list[i] = hist_lines[i]; - hist_list[i][0] = '\0'; + if (!strcmp(line, hist_list[last])) + return; } -} - -static void cread_add_to_hist(char *line) -{ - strcpy(hist_list[hist_add_idx], line); - if (++hist_add_idx >= HIST_MAX) - hist_add_idx = 0; + 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_max = hist_add_idx; + if (++hist_add_idx >= HIST_MAX) + hist_add_idx = 0; - hist_num++; + 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 +314,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 +347,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 +357,14 @@ 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, bool enable_history) { - 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 +387,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,9 +420,10 @@ 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 KEY_END: case CTL_CH('e'): REFRESH_TO_EOL(); break; @@ -479,13 +432,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,35 +453,36 @@ 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'): - { - char *hline; - - if (ichar == CTL_CH('p') || ichar == KEY_UP) - hline = hist_prev(); - else - hline = hist_next(); - - if (!hline) { + case CTL_CH('n'): /* next-history */ + if (enable_history) { + char *hline; + + if (ichar == CTL_CH('p') || ichar == KEY_UP) + hline = hist_prev(); + else + hline = hist_next(); + + if (hline) { + /* 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(); + } 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; #ifdef CONFIG_AUTO_COMPLETE case '\t': { int num2, col; @@ -560,127 +514,31 @@ static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *l *len = eol_num; 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; - + if (enable_history) { + if (buf[0]) + 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) +static int cli_readline_into_buffer(const FLASH char *const prompt, + char *buffer, bool enable_history) { 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; - - if (!initted) { - hist_init(); - initted = 1; - } if (prompt) my_puts_P(prompt); - rc = cread_line(prompt, p, &len); + rc = cread_line(prompt, p, &len, enable_history); 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'); - } - } - } -#endif /* CONFIG_CMDLINE_EDITING */ } -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 @@ -688,5 +546,5 @@ int cli_readline(const FLASH char *const prompt) */ console_buffer[0] = '\0'; - return cli_readline_into_buffer(prompt, console_buffer); + return cli_readline_into_buffer(prompt, console_buffer, enable_history); }