+#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);
- hist_node *p = hist_head;
+ 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;
+}
+
+static uint_fast8_t hist_get_count(void)
+{
+ char *p = 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;
+ }
- 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());
+ }