Merge git://git.denx.de/u-boot-sh
[oweals/u-boot.git] / common / cli_hush.c
index e0c436f1a7c799f6d441d3843097a0f8b5205fdf..5b1f11907438e893911ca6f8e0650be8a423392c 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * sh.c -- a prototype Bourne shell grammar parser
  *      Intended to follow the original Thompson and Ritchie
  *      document how quoting rules not precisely followed for variable assignments
  *      maybe change map[] to use 2-bit entries
  *      (eventually) remove all the printf's
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #define __U_BOOT__
 #ifdef __U_BOOT__
+#include <common.h>         /* readline */
+#include <env.h>
 #include <malloc.h>         /* malloc, free, realloc*/
 #include <linux/ctype.h>    /* isalpha, isdigit */
-#include <common.h>        /* readline */
+#include <console.h>
 #include <bootretry.h>
 #include <cli.h>
 #include <cli_hush.h>
@@ -559,7 +560,7 @@ static int builtin_cd(struct child_prog *child)
 {
        char *newdir;
        if (child->argv[1] == NULL)
-               newdir = getenv("HOME");
+               newdir = env_get("HOME");
        else
                newdir = child->argv[1];
        if (chdir(newdir)) {
@@ -947,7 +948,7 @@ static inline void cmdedit_set_initial_prompt(void)
 #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
        PS1 = NULL;
 #else
-       PS1 = getenv("PS1");
+       PS1 = env_get("PS1");
        if(PS1==0)
                PS1 = "\\w \\$ ";
 #endif
@@ -973,6 +974,30 @@ static inline void setup_prompt_string(int promptmode, char **prompt_str)
 }
 #endif
 
+#ifdef __U_BOOT__
+static int uboot_cli_readline(struct in_str *i)
+{
+       char *prompt;
+       char __maybe_unused *ps_prompt = NULL;
+
+       if (i->promptmode == 1)
+               prompt = CONFIG_SYS_PROMPT;
+       else
+               prompt = CONFIG_SYS_PROMPT_HUSH_PS2;
+
+#ifdef CONFIG_CMDLINE_PS_SUPPORT
+       if (i->promptmode == 1)
+               ps_prompt = env_get("PS1");
+       else
+               ps_prompt = env_get("PS2");
+       if (ps_prompt)
+               prompt = ps_prompt;
+#endif
+
+       return cli_readline(prompt);
+}
+#endif
+
 static void get_user_input(struct in_str *i)
 {
 #ifndef __U_BOOT__
@@ -1002,11 +1027,8 @@ static void get_user_input(struct in_str *i)
 
        bootretry_reset_cmd_timeout();
        i->__promptme = 1;
-       if (i->promptmode == 1) {
-               n = cli_readline(CONFIG_SYS_PROMPT);
-       } else {
-               n = cli_readline(CONFIG_SYS_PROMPT_HUSH_PS2);
-       }
+       n = uboot_cli_readline(i);
+
 #ifdef CONFIG_BOOT_RETRY_TIME
        if (n == -2) {
          puts("\nTimeout waiting for command\n");
@@ -1827,8 +1849,7 @@ static int run_list_real(struct pipe *pi)
                                continue;
                        } else {
                                /* insert new value from list for variable */
-                               if (pi->progs->argv[0])
-                                       free(pi->progs->argv[0]);
+                               free(pi->progs->argv[0]);
                                pi->progs->argv[0] = *list++;
 #ifndef __U_BOOT__
                                pi->progs->glob_result.gl_pathv[0] =
@@ -1840,7 +1861,7 @@ static int run_list_real(struct pipe *pi)
                if (rmode == RES_DO) {
                        if (!flag_rep) continue;
                }
-               if ((rmode == RES_DONE)) {
+               if (rmode == RES_DONE) {
                        if (flag_rep) {
                                flag_restore = 1;
                        } else {
@@ -2150,7 +2171,7 @@ int set_local_var(const char *s, int flg_export)
        name=strdup(s);
 
 #ifdef __U_BOOT__
-       if (getenv(name) != NULL) {
+       if (env_get(name) != NULL) {
                printf ("ERROR: "
                                "There is a global environment variable with the same name.\n");
                free(name);
@@ -2161,7 +2182,7 @@ int set_local_var(const char *s, int flg_export)
         * NAME=VALUE format.  So the first order of business is to
         * split 's' on the '=' into 'name' and 'value' */
        value = strchr(name, '=');
-       if (value == NULL && ++value == NULL) {
+       if (value == NULL || *(value + 1) == 0) {
                free(name);
                return -1;
        }
@@ -2243,7 +2264,7 @@ void unset_local_var(const char *name)
                        } else {
 #ifndef __U_BOOT__
                                if(cur->flg_export)
-                                       unsetenv(cur->name);
+                                       unenv_set(cur->name);
 #endif
                                free(cur->name);
                                free(cur->value);
@@ -2470,11 +2491,16 @@ static int done_word(o_string *dest, struct p_context *ctx)
                }
                argc = ++child->argc;
                child->argv = realloc(child->argv, (argc+1)*sizeof(*child->argv));
-               if (child->argv == NULL) return 1;
+               if (child->argv == NULL) {
+                       free(str);
+                       return 1;
+               }
                child->argv_nonnull = realloc(child->argv_nonnull,
                                        (argc+1)*sizeof(*child->argv_nonnull));
-               if (child->argv_nonnull == NULL)
+               if (child->argv_nonnull == NULL) {
+                       free(str);
                        return 1;
+               }
                child->argv[argc-1]=str;
                child->argv_nonnull[argc-1] = dest->nonnull;
                child->argv[argc]=NULL;
@@ -2766,7 +2792,7 @@ static char *lookup_param(char *src)
                }
        }
 
-       p = getenv(src);
+       p = env_get(src);
        if (!p)
                p = get_local_var(src);
 
@@ -3130,7 +3156,7 @@ static void mapset(const unsigned char *set, int code)
 static void update_ifs_map(void)
 {
        /* char *ifs and char map[256] are both globals. */
-       ifs = (uchar *)getenv("IFS");
+       ifs = (uchar *)env_get("IFS");
        if (ifs == NULL) ifs=(uchar *)" \t\n";
        /* Precompute a list of 'flow through' behavior so it can be treated
         * quickly up front.  Computation is necessary because of IFS.
@@ -3162,7 +3188,7 @@ static int parse_stream_outer(struct in_str *inp, int flag)
        o_string temp=NULL_O_STRING;
        int rcode;
 #ifdef __U_BOOT__
-       int code = 0;
+       int code = 1;
 #endif
        do {
                ctx.type = flag;
@@ -3170,7 +3196,8 @@ static int parse_stream_outer(struct in_str *inp, int flag)
                update_ifs_map();
                if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING)) mapset((uchar *)";$&|", 0);
                inp->promptmode=1;
-               rcode = parse_stream(&temp, &ctx, inp, '\n');
+               rcode = parse_stream(&temp, &ctx, inp,
+                                    flag & FLAG_CONT_ON_NEWLINE ? -1 : '\n');
 #ifdef __U_BOOT__
                if (rcode == 1) flag_repeat = 0;
 #endif
@@ -3235,8 +3262,10 @@ int parse_string_outer(const char *s, int flag)
 #ifdef __U_BOOT__
        char *p = NULL;
        int rcode;
-       if ( !s || !*s)
+       if (!s)
                return 1;
+       if (!*s)
+               return 0;
        if (!(p = strchr(s, '\n')) || *++p) {
                p = xmalloc(strlen(s) + 2);
                strcpy(p, s);
@@ -3499,9 +3528,9 @@ static char *insert_var_value_sub(char *inp, int tag_subst)
        char *p, *p1, *res_str = NULL;
 
        while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
-               /* check the beginning of the string for normal charachters */
+               /* check the beginning of the string for normal characters */
                if (p != inp) {
-                       /* copy any charachters to the result string */
+                       /* copy any characters to the result string */
                        len = p - inp;
                        res_str = xrealloc(res_str, (res_str_len + len));
                        strncpy((res_str + res_str_len), inp, len);
@@ -3569,7 +3598,7 @@ static char **make_list_in(char **inp, char *name)
                p3 = insert_var_value(inp[i]);
                p1 = p3;
                while (*p1) {
-                       if ((*p1 == ' ')) {
+                       if (*p1 == ' ') {
                                p1++;
                                continue;
                        }
@@ -3635,8 +3664,8 @@ static char *make_string(char **inp, int *nonnull)
 }
 
 #ifdef __U_BOOT__
-static int do_showvar(cmd_tbl_t *cmdtp, int flag, int argc,
-                     char * const argv[])
+static int do_showvar(struct cmd_tbl *cmdtp, int flag, int argc,
+                     char *const argv[])
 {
        int i, k;
        int rcode = 0;