]> git.sur5r.net Git - u-boot/blobdiff - common/main.c
* Code cleanup:
[u-boot] / common / main.c
index 08d54c5c14094dfa3a4eb44d6c3ed78d34ed2105..f7830a1f3db0f295722522324f09f815455a5bf4 100644 (file)
  * MA 02111-1307 USA
  */
 
+/* #define     DEBUG   */
+
 #include <common.h>
 #include <watchdog.h>
 #include <command.h>
-#include <cmd_nvedit.h>
-#include <cmd_bootm.h>
 #include <malloc.h>
-#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
-#include <cmd_boot.h>          /* for do_reset() prototype */
-#endif
 
 #ifdef CFG_HUSH_PARSER
 #include <hush.h>
 #endif
 
+#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
+extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);              /* for do_reset() prototype */
+#endif
+
+extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
+
+
 #define MAX_DELAY_STOP_STR 32
 
 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
@@ -141,7 +145,7 @@ static __inline__ int abortboot(int bootdelay)
                        if (delaykey[i].len > 0 &&
                            presskey_len >= delaykey[i].len &&
                            memcmp (presskey + presskey_len - delaykey[i].len,
-                                   delaykey[i].str,
+                                   delaykey[i].str,
                                    delaykey[i].len) == 0) {
 #  if DEBUG_BOOTKEYS
                                printf("got %skey\n",
@@ -194,17 +198,17 @@ static __inline__ int abortboot(int bootdelay)
 #endif
 
 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
-        /*
-         * Check if key already pressed
-         * Don't check if bootdelay < 0
-         */
+       /*
+        * Check if key already pressed
+        * Don't check if bootdelay < 0
+        */
        if (bootdelay >= 0) {
                if (tstc()) {   /* we got a key press   */
                        (void) getc();  /* consume input        */
                        printf ("\b\b\b 0\n");
                        return 1;       /* don't auto boot      */
                }
-        }
+       }
 #endif
 
        while (bootdelay > 0) {
@@ -277,6 +281,16 @@ void main_loop (void)
        }
 #endif  /* CONFIG_MODEM_SUPPORT */
 
+#ifdef CONFIG_VERSION_VARIABLE
+       {
+               extern char version_string[];
+               char *str = getenv("ver");
+
+               if (!str)
+                       setenv ("ver", version_string);  /* set version variable */
+       }
+#endif /* CONFIG_VERSION_VARIABLE */
+
 #ifdef CFG_HUSH_PARSER
        u_boot_hush_start ();
 #endif
@@ -304,21 +318,16 @@ void main_loop (void)
        s = getenv ("bootdelay");
        bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
 
-#if 0
-       printf ("### main_loop entered:\n\n");
-#endif
+       debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
 
 # ifdef CONFIG_BOOT_RETRY_TIME
-       s = getenv ("bootretry");
-       if (s != NULL)
-               retry_time = (int)simple_strtoul(s, NULL, 10);
-       else
-               retry_time =  CONFIG_BOOT_RETRY_TIME;
-       if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
-               retry_time = CONFIG_BOOT_RETRY_MIN;
+       init_cmd_timeout ();
 # endif        /* CONFIG_BOOT_RETRY_TIME */
 
        s = getenv ("bootcmd");
+
+       debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
+
        if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
 # ifdef CONFIG_AUTOBOOT_KEYED
                int prev = disable_ctrlc(1);    /* disable Control C checking */
@@ -337,11 +346,9 @@ void main_loop (void)
        }
 
 # ifdef CONFIG_MENUKEY
-       if (menukey == CONFIG_MENUKEY)
-       {
+       if (menukey == CONFIG_MENUKEY) {
            s = getenv("menucmd");
-           if (s)
-           {
+           if (s) {
 # ifndef CFG_HUSH_PARSER
                run_command (s, bd, 0);
 # else
@@ -411,10 +418,26 @@ void main_loop (void)
 #endif /*CFG_HUSH_PARSER*/
 }
 
+#ifdef CONFIG_BOOT_RETRY_TIME
+/***************************************************************************
+ * initialise command line timeout
+ */
+void init_cmd_timeout(void)
+{
+       char *s = getenv ("bootretry");
+
+       if (s != NULL)
+               retry_time = (int)simple_strtoul(s, NULL, 10);
+       else
+               retry_time =  CONFIG_BOOT_RETRY_TIME;
+
+       if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
+               retry_time = CONFIG_BOOT_RETRY_MIN;
+}
+
 /***************************************************************************
  * reset command line timeout to retry_time seconds
  */
-#ifdef CONFIG_BOOT_RETRY_TIME
 void reset_cmd_timeout(void)
 {
        endtime = endtick(retry_time);
@@ -612,7 +635,7 @@ static void process_macros (const char *input, char *output)
        int state = 0;  /* 0 = waiting for '$'  */
                        /* 1 = waiting for '('  */
                        /* 2 = waiting for ')'  */
-
+                       /* 3 = waiting for '''  */
 #ifdef DEBUG_PARSER
        char *output_start = output;
 
@@ -625,16 +648,22 @@ static void process_macros (const char *input, char *output)
            c = *input++;
            inputcnt--;
 
+           if (state!=3) {
            /* remove one level of escape characters */
            if ((c == '\\') && (prev != '\\')) {
                if (inputcnt-- == 0)
                        break;
                prev = c;
-               c = *input++;
+               c = *input++;
+           }
            }
 
            switch (state) {
            case 0:                     /* Waiting for (unescaped) $    */
+               if ((c == '\'') && (prev != '\\')) {
+                       state = 3;
+                       break;
+               }
                if ((c == '$') && (prev != '\\')) {
                        state++;
                } else {
@@ -682,8 +711,15 @@ static void process_macros (const char *input, char *output)
                        state = 0;
                }
                break;
+           case 3:                     /* Waiting for '        */
+               if ((c == '\'') && (prev != '\\')) {
+                       state = 0;
+               } else {
+                       *(output++) = c;
+                       outputcnt--;
+               }
+               break;
            }
-
            prev = c;
        }
 
@@ -722,8 +758,9 @@ int run_command (const char *cmd, int flag)
        char finaltoken[CFG_CBSIZE];
        char *str = cmdbuf;
        char *argv[CFG_MAXARGS + 1];    /* NULL terminated      */
-       int argc;
+       int argc, inquotes;
        int repeatable = 1;
+       int rc = 0;
 
 #ifdef DEBUG_PARSER
        printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
@@ -757,8 +794,13 @@ int run_command (const char *cmd, int flag)
                 * Find separator, or string end
                 * Allow simple escape of ';' by writing "\;"
                 */
-               for (sep = str; *sep; sep++) {
-                       if ((*sep == ';') &&    /* separator            */
+               for (inquotes = 0, sep = str; *sep; sep++) {
+                       if ((*sep=='\'') &&
+                           (*(sep-1) != '\\'))
+                               inquotes=!inquotes;
+
+                       if (!inquotes &&
+                           (*sep == ';') &&    /* separator            */
                            ( sep != str) &&    /* past string start    */
                            (*(sep-1) != '\\')) /* and NOT escaped      */
                                break;
@@ -787,13 +829,15 @@ int run_command (const char *cmd, int flag)
                /* Look up command in command table */
                if ((cmdtp = find_cmd(argv[0])) == NULL) {
                        printf ("Unknown command '%s' - try 'help'\n", argv[0]);
-                       return -1;      /* give up after bad command */
+                       rc = -1;        /* give up after bad command */
+                       continue;
                }
 
                /* found - check max args */
                if (argc > cmdtp->maxargs) {
                        printf ("Usage:\n%s\n", cmdtp->usage);
-                       return -1;
+                       rc = -1;
+                       continue;
                }
 
 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
@@ -804,7 +848,8 @@ int run_command (const char *cmd, int flag)
 #endif
                        if (flag & CMD_FLAG_BOOTD) {
                                printf ("'bootd' recursion detected\n");
-                               return -1;
+                               rc = -1;
+                               continue;
                        }
                        else
                                flag |= CMD_FLAG_BOOTD;
@@ -813,7 +858,7 @@ int run_command (const char *cmd, int flag)
 
                /* OK - call function to do the command */
                if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
-                       return (-1);
+                       rc = -1;
                }
 
                repeatable &= cmdtp->repeatable;
@@ -823,7 +868,7 @@ int run_command (const char *cmd, int flag)
                        return 0;       /* if stopped then not repeatable */
        }
 
-       return repeatable;
+       return rc ? rc : repeatable;
 }
 
 /****************************************************************************/
@@ -832,7 +877,6 @@ int run_command (const char *cmd, int flag)
 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 {
        int i;
-       int rcode = 1;
 
        if (argc < 2) {
                printf ("Usage:\n%s\n", cmdtp->usage);
@@ -840,13 +884,21 @@ int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
        }
 
        for (i=1; i<argc; ++i) {
+               char *arg;
+
+               if ((arg = getenv (argv[i])) == NULL) {
+                       printf ("## Error: \"%s\" not defined\n", argv[i]);
+                       return 1;
+               }
 #ifndef CFG_HUSH_PARSER
-           if (run_command (getenv (argv[i]), flag) != -1) ++rcode;
+               if (run_command (arg, flag) == -1)
+                       return 1;
 #else
-           if (parse_string_outer(getenv (argv[i]),
-                   FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) == 0) ++rcode;
+               if (parse_string_outer(arg,
+                   FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
+                       return 1;
 #endif
        }
-       return ((rcode == i) ? 0 : 1);
+       return 0;
 }
-#endif
+#endif /* CFG_CMD_RUN */