]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cli_readline.c
Adaptions for fatfs R0.12b
[z180-stamp.git] / avr / cli_readline.c
index c2e2326a61560f6d3de2e22290f2b9aa201469bf..2fdc56e907d272142ab0bd512438af14143c4599 100644 (file)
 
 
 
+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:
  *
@@ -77,13 +85,16 @@ 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
@@ -105,7 +116,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
@@ -145,6 +157,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,31 +211,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 <nandy@mizi.com>
  */
 
-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;
@@ -224,6 +227,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;
@@ -332,8 +340,7 @@ static char *hist_prev(void)
 
        if (p == NULL)
                return NULL;
-
-       hist_cur = hist_cur->next;
+       hist_cur = p->next;
 
        return  p->line;
 }
@@ -355,30 +362,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;
+
+       if(!strncmp(p->line, buf, num)) {
+               hist_cur = p->next;
+               return  p->line;
+       }
+       return NULL;
+}
+
+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;
+}
+
+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');}
 
-#define BEGINNING_OF_LINE() {                                                  \
-       while (num) {                                                              \
-               getcmd_putch(CTL_BACKSPACE);                                           \
-               num--;                                                                 \
-       }                                                                          \
+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) {                                                       \
-               uint_fast8_t 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,
@@ -407,25 +467,23 @@ 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, uint_fast8_t 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, bool enable_history)
+                       uint_fast8_t len, bool enable_history)
 {
        uint_fast8_t num = 0;
        uint_fast8_t eol_num = 0;
@@ -433,9 +491,10 @@ static int cread_line(const FLASH char *const prompt, char *buf,
 
        (void) prompt;
 
-       uint_fast8_t init_len = strlen(buf);
-       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) {
                int ichar = getcmd_getch();
@@ -450,9 +509,10 @@ 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 */
+                       putchar('\n');
                        *buf = '\0';                    /* discard input */
                        return -1;
                case KEY_RIGHT:
@@ -486,11 +546,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'):
@@ -498,22 +558,21 @@ 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:
                case 8:                                         /* backward-delete-char */
                        if (num) {
-                               uint_fast8_t 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;
@@ -530,17 +589,13 @@ static int cread_line(const FLASH char *const prompt, char *buf,
                                        hline = hist_next();
 
                                if (hline) {
-                                       /* nuke the current line */
                                        /* first, go home */
-                                       BEGINNING_OF_LINE();
-
+                                       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();
+                                       erase_to_eol(&num, &eol_num);
 
-                                       /* copy new line into place and display */
-                                       strcpy(buf, hline);
-                                       eol_num = strlen(buf);
-                                       REFRESH_TO_EOL();
                                } else {
                                        getcmd_cbeep();
                                }
@@ -548,29 +603,24 @@ 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();
-
+                                       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();
-
-                                       /* copy new line into place and display */
-                                       strcpy(buf, hline);
-                                       eol_num = strlen(buf);
-                                       REFRESH_TO_EOL();
+                                       erase_to_eol(&num2, &eol_num);
+                                       /* cursor back */
+                                       while (num2-- > num)
+                                               getcmd_putch(CTL_BACKSPACE);
                                } else {
                                        getcmd_cbeep();
                                }
@@ -578,7 +628,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;
@@ -602,37 +651,21 @@ static int cread_line(const FLASH char *const prompt, char *buf,
 #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 (enable_history) {
-               if (buf[0])
-               hist_cur = cread_add_to_hist(buf);
-       }
-       return 0;
+       if (enable_history && buf[0])
+               cread_add_to_hist(buf);
+       return eol_num;
 }
 
 /****************************************************************************/
 
-static int cli_readline_into_buffer(const FLASH char *const prompt,
-                       char *buffer, bool enable_history)
-{
-       char *p = buffer;
-       uint_fast8_t len = CONFIG_SYS_CBSIZE;
-       int rc;
-
-       if (prompt)
-               my_puts_P(prompt);
-
-       rc = cread_line(prompt, p, &len, enable_history);
-       return rc < 0 ? rc : (int) len;
-}
-
 int cli_readline(const FLASH char *const prompt, bool enable_history)
 {
        /*
@@ -641,5 +674,8 @@ int cli_readline(const FLASH char *const prompt, bool enable_history)
         */
        console_buffer[0] = '\0';
 
-       return cli_readline_into_buffer(prompt, console_buffer, enable_history);
+       if (prompt)
+               my_puts_P(prompt);
+
+       return cread_line(prompt, console_buffer, CONFIG_SYS_CBSIZE, enable_history);
 }