mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Alexander Aring <alex.aring@gmail.com>
To: Teresa Gamez <t.gamez@phytec.de>
Cc: barebox@lists.infradead.org
Subject: Re: [BUG] readline history
Date: Thu, 28 Aug 2014 12:08:56 +0200	[thread overview]
Message-ID: <20140828100855.GA26411@omega> (raw)
In-Reply-To: <20140828092542.GA20889@omega>

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

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2014-08-28 10:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-28  7:50 Teresa Gamez
2014-08-28  8:35 ` Alexander Aring
2014-08-28  9:25 ` Alexander Aring
2014-08-28 10:08   ` Alexander Aring [this message]
2014-09-01  8:30 ` Sascha Hauer
2014-09-01  8:44   ` Alexander Aring
2014-09-01 12:25     ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140828100855.GA26411@omega \
    --to=alex.aring@gmail.com \
    --cc=barebox@lists.infradead.org \
    --cc=t.gamez@phytec.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox