[BUG] readline history

Alexander Aring alex.aring at gmail.com
Thu Aug 28 03:08:56 PDT 2014


Hi,

another possible solution which less of runtime decisions.



diff --git a/lib/readline.c b/lib/readline.c
index b70bca8..f9cfa4b 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -28,7 +28,9 @@ struct history {
        struct list_head list;
 };
 
-static LIST_HEAD(history_list);
+static struct history history_list = {
+       .list = LIST_HEAD_INIT(history_list.list),
+};
 
 static struct list_head *history_current;
 static int history_num_entries;
@@ -38,8 +40,8 @@ static void cread_add_to_hist(char *line)
        struct history *history;
        char *newline;
 
-       if (!list_empty(&history_list)) {
-               history = list_last_entry(&history_list, struct history, list);
+       if (!list_empty(&history_list.list)) {
+               history = list_last_entry(&history_list.list, struct history, list);
 
                if (!strcmp(line, history->line))
                        return;
@@ -53,21 +55,21 @@ static void cread_add_to_hist(char *line)
                history = xzalloc(sizeof(*history));
                history_num_entries++;
        } else {
-               history = list_first_entry(&history_list, struct history, list);
+               history = list_first_entry(&history_list.list, struct history, list);
                free(history->line);
                list_del(&history->list);
        }
 
        history->line = newline;
 
-       list_add_tail(&history->list, &history_list);
+       list_add_tail(&history->list, &history_list.list);
 }
 
 static const char *hist_prev(void)
 {
        struct history *history;
 
-       if (history_current->prev == &history_list) {
+       if (history_current->prev == &history_list.list) {
                history = list_entry(history_current, struct history, list);
                getcmd_cbeep();
                return history->line;
@@ -84,12 +86,12 @@ static const char *hist_next(void)
 {
        struct history *history;
 
-       if (history_current->next == &history_list) {
-               history_current = &history_list;
+       if (history_current->next == &history_list.list) {
+               history_current = &history_list.list;
                return "";
        }
 
-       if (history_current == &history_list) {
+       if (history_current == &history_list.list) {
                getcmd_cbeep();
                return "";
        }
@@ -188,7 +190,7 @@ int readline(const char *prompt, char *buf, int len)
 
        complete_reset();
 #endif
-       history_current = &history_list;
+       history_current = &history_list.list;
 
        puts (prompt);
 
@@ -296,10 +298,13 @@ int readline(const char *prompt, char *buf, int len)
                {
                        const char *hline;
 
-                       if (ichar == BB_KEY_UP)
+                       if (ichar == BB_KEY_UP) {
                                hline = hist_prev();
-                       else
+                               if (!hline)
+                                       break;
+                       } else {
                                hline = hist_next();
+                       }
 
                        /* nuke the current line */
                        /* first, go home */




Don't know what I actually did there, just experiment that history_list
isn't a struct list. It has now space for the "char *line" pointer and
it's init with null. Also move the null check only for hlist_prev();

now list_entry on history_list works because it has a "char *line" which
should always be NULL.

- Alex



More information about the barebox mailing list