]> Git Repo - J-u-boot.git/blobdiff - common/cli_readline.c
bloblist: check bloblist with specified buffer size
[J-u-boot.git] / common / cli_readline.c
index fdb84d9204f648752e5147e8c65303b2050e0827..2507be2295269a64b9eefe50ba4572c5e4111337 100644 (file)
@@ -12,6 +12,8 @@
 #include <bootretry.h>
 #include <cli.h>
 #include <command.h>
+#include <hang.h>
+#include <malloc.h>
 #include <time.h>
 #include <watchdog.h>
 #include <asm/global_data.h>
@@ -85,7 +87,6 @@ static int hist_cur = -1;
 static unsigned hist_num;
 
 static char *hist_list[HIST_MAX];
-static char hist_lines[HIST_MAX][HIST_SIZE + 1];       /* Save room for NULL */
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
@@ -97,8 +98,9 @@ static void getcmd_putchars(int count, int ch)
                getcmd_putch(ch);
 }
 
-static void hist_init(void)
+static int hist_init(void)
 {
+       unsigned char *hist;
        int i;
 
        hist_max = 0;
@@ -106,10 +108,14 @@ static void hist_init(void)
        hist_cur = -1;
        hist_num = 0;
 
-       for (i = 0; i < HIST_MAX; i++) {
-               hist_list[i] = hist_lines[i];
-               hist_list[i][0] = '\0';
-       }
+       hist = calloc(HIST_MAX, HIST_SIZE + 1);
+       if (!hist)
+               return -ENOMEM;
+
+       for (i = 0; i < HIST_MAX; i++)
+               hist_list[i] = hist + (i * (HIST_SIZE + 1));
+
+       return 0;
 }
 
 static void cread_add_to_hist(char *line)
@@ -361,34 +367,35 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar)
                break;
        case CTL_CH('p'):
        case CTL_CH('n'):
-       {
-               char *hline;
+               if (cls->history) {
+                       char *hline;
 
-               if (ichar == CTL_CH('p'))
-                       hline = hist_prev();
-               else
-                       hline = hist_next();
+                       if (ichar == CTL_CH('p'))
+                               hline = hist_prev();
+                       else
+                               hline = hist_next();
 
-               if (!hline) {
-                       getcmd_cbeep();
-                       break;
-               }
+                       if (!hline) {
+                               getcmd_cbeep();
+                               break;
+                       }
 
-               /* nuke the current line */
-               /* first, go home */
-               BEGINNING_OF_LINE();
+                       /* nuke the current line */
+                       /* first, go home */
+                       BEGINNING_OF_LINE();
 
-               /* erase to end of line */
-               ERASE_TO_EOL();
+                       /* erase to end of line */
+                       ERASE_TO_EOL();
 
-               /* copy new line into place and display */
-               strcpy(buf, hline);
-               cls->eol_num = strlen(buf);
-               REFRESH_TO_EOL();
+                       /* copy new line into place and display */
+                       strcpy(buf, hline);
+                       cls->eol_num = strlen(buf);
+                       REFRESH_TO_EOL();
+                       break;
+               }
                break;
-       }
        case '\t':
-               if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
+               if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
                        int num2, col;
 
                        /* do not autocomplete when in the middle */
@@ -423,25 +430,33 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar)
        return -EAGAIN;
 }
 
+void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size)
+{
+       int init_len = strlen(buf);
+
+       memset(cls, '\0', sizeof(struct cli_line_state));
+       cls->insert = true;
+       cls->buf = buf;
+       cls->len = buf_size;
+
+       if (init_len)
+               cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf,
+                             buf_size);
+}
+
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
                      int timeout)
 {
        struct cli_ch_state s_cch, *cch = &s_cch;
        struct cli_line_state s_cls, *cls = &s_cls;
        char ichar;
-       int init_len = strlen(buf);
        int first = 1;
 
        cli_ch_init(cch);
-       memset(cls, '\0', sizeof(struct cli_line_state));
-       cls->insert = true;
-       cls->len = *len;
+       cli_cread_init(cls, buf, *len);
        cls->prompt = prompt;
-       cls->buf = buf;
-
-       if (init_len)
-               cread_add_str(buf, init_len, 1, &cls->num, &cls->eol_num, buf,
-                             *len);
+       cls->history = true;
+       cls->cmd_complete = true;
 
        while (1) {
                int ret;
@@ -484,8 +499,9 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 
 #else /* !CONFIG_CMDLINE_EDITING */
 
-static inline void hist_init(void)
+static inline int hist_init(void)
 {
+       return 0;
 }
 
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
@@ -524,7 +540,7 @@ static int cread_line_simple(const char *const prompt, char *p)
        int n = 0;              /* buffer index */
        int plen = 0;           /* prompt length */
        int col;                /* output column cnt */
-       char c;
+       int c;
 
        /* print prompt */
        if (prompt) {
@@ -634,8 +650,9 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer,
         */
        if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
                if (!initted) {
-                       hist_init();
-                       initted = 1;
+                       rc = hist_init();
+                       if (rc == 0)
+                               initted = 1;
                }
 
                if (prompt)
This page took 0.031847 seconds and 4 git commands to generate.