+#include "debug.h"
+
+
+#define DEBUG_READLINE 0 /* set to 1 to debug */
+
+#define debug_readline(fmt, args...) \
+ debug_cond(DEBUG_READLINE, fmt, ##args)
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(5~, KEY_PPAGE), // Previous-page key
FKEY_TBL_ITEM(4~, KEY_END), // End key
FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab 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
/* 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
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
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
- if (q) {
- while(q->next) {
- p = q;
- q = q->next;
- }
- free(q);
- if (p)
- p->next = NULL;
+ p = HISTBUFE - amount;
+ if (p < hist_head)
+ p = hist_head;
+
+ while (p > hist_head && *(p-1))
+ --p;
+
+ 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);
- while(q && q != pos) {
- p = q;
- q = q->next;
- }
- if (q) {
- if (p)
- p->next = q->next;
- else
- hist_head = q->next;
- q->next = NULL;
- }
- return q;
+ hist_head = memmove(hist_head + shift, hist_head, len);
+
+ return hist_head;
+}
+
+
+static char *hist_add_entry(char *line)
+{
+ char *p = hist_head;
+
+ if (p == NULL)
+ p = HISTBUFE;
+
+ char *q = p - strlen(line) - 1;
+ if (q < histbuf)
+ q = histbuf;
+
+ strlcpy(q, line, p - q);
+
+ hist_head = q;
+
+ return hist_head;
- while (p->next && strncmp(p->line, buf, num))
- p = p->next;
+ while (*p && strncmp(p, buf, num))
+ p = hist_entry_next(p);
- if(!strncmp(p->line, buf, num)) {
- hist_cur = p->next;
- return p->line;
+ if(!strncmp(p, buf, num)) {
+ hist_cur = hist_entry_next(p);
+ return p;
- while(q->next != hist_cur) {
- p = q;
- q = q->next;
- if (p && !strncmp(p->line, buf, num))
- match = p;
+ //if(!strncmp(p, buf, num)) {
+ if(p) {
+ hist_cur = hist_entry_next(p);
+ return p;
+ }
-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);
- 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);
memmove(&buf[num], &buf[num+1], wlen);
getcmd_putch(CTL_BACKSPACE);
putnstr(buf + num, wlen);
memmove(&buf[num], &buf[num+1], wlen);
getcmd_putch(CTL_BACKSPACE);
putnstr(buf + num, wlen);
/* erase to end of line */
erase_to_eol(&num, &eol_num);
/* erase to end of line */
erase_to_eol(&num, &eol_num);
if (ichar == KEY_PPAGE)
hline = hist_search_backward(buf, num);
else
hline = hist_search_forward(buf, num);
if (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_eol(&num, &eol_num);
-
- /* copy new line into place and display */
- strcpy(buf+num, hline+num);
- eol_num = strlen(buf);
- uint8_t wlen = eol_num - num;
- putnstr(buf + num, wlen);
- getcmd_putch(' ');
- do {
+ erase_to_eol(&num2, &eol_num);
+ /* cursor back */
+ while (num2-- > num)
- cread_add_char(ichar, insert, &num, &eol_num, buf,
- *len);
+ cread_add_char(ichar, insert, &num, &eol_num, buf, len);
- if (enable_history && buf[0])
+ uint_fast8_t i = 0;
+ while (buf[i] == ' ')
+ ++i; /* remove leading blanks */
+ if (i) {
+ eol_num -= i;
+ memmove(buf, buf+i, eol_num+1);
+ }
+
+ debug_readline("### hist_head: %p, hist_cur: %p\n", hist_head, hist_cur);
+ if (enable_history && buf[0]) {
+ 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());
+ }
+ 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;
-}
-
- 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);