]> www.infradead.org Git - users/rw/ppcboot.git/commitdiff
Fix nasty bug: run_command failed in some cases when the executed
authorwdenk <wdenk>
Sat, 3 Mar 2001 22:11:50 +0000 (22:11 +0000)
committerwdenk <wdenk>
Sat, 3 Mar 2001 22:11:50 +0000 (22:11 +0000)
command modifiec the environment (like "bootp" does when it creates
or modifies environment variables). The reason was that we used a
pointer directly into the environment area, which may change while we
are running the command. Grrrgh...

common/main.c

index 41339d6f58f908ff8da0b45ac564411a36cc30e2..98fb0991e86760e441bd66143c66274a233d4ea1 100644 (file)
@@ -374,6 +374,9 @@ int parse_line (char *line, char *argv[])
 {
        int nargs = 0;
 
+#ifdef DEBUG_PARSER
+       printf ("parse_line: \"%s\"\n", line);
+#endif
        while (nargs < CFG_MAXARGS) {
 
                /* skip any white space */
@@ -383,6 +386,9 @@ int parse_line (char *line, char *argv[])
 
                if (*line == '\0') {    /* end of line, no more args    */
                        argv[nargs] = NULL;
+#ifdef DEBUG_PARSER
+               printf ("parse_line: nargs=%d\n", nargs);
+#endif
                        return (nargs);
                }
 
@@ -395,6 +401,9 @@ int parse_line (char *line, char *argv[])
 
                if (*line == '\0') {    /* end of line, no more args    */
                        argv[nargs] = NULL;
+#ifdef DEBUG_PARSER
+               printf ("parse_line: nargs=%d\n", nargs);
+#endif
                        return (nargs);
                }
 
@@ -403,6 +412,9 @@ int parse_line (char *line, char *argv[])
 
        printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
 
+#ifdef DEBUG_PARSER
+       printf ("parse_line: nargs=%d\n", nargs);
+#endif
        return (nargs);
 }
 
@@ -420,7 +432,7 @@ static void process_macros (char *input, char *output)
 #ifdef DEBUG_PARSER
        char *output_start = output;
 
-       printf ("[PROCESS_MACROS] INPUT=%s\n", input);
+       printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
 #endif
 
        prev = '\0';                    /* previous character   */
@@ -495,7 +507,8 @@ static void process_macros (char *input, char *output)
                *output = 0;
 
 #ifdef DEBUG_PARSER
-       printf ("[PROCESS_MACROS] OUTPUT=%s\n", output_start);
+       printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
+               strlen(output_start), output_start);
 #endif
 }
 
@@ -518,7 +531,7 @@ int run_command (const char *cmd, bd_t *bd, int flag)
        int repeatable = 1;
 
 #ifdef DEBUG_PARSER
-       printf ("[RUN_COMMAND] cmd=\"%s\"\n", cmd ? cmd : "NULL");
+       printf ("[RUN_COMMAND] cmd[%p]=\"%s\"\n", cmd, cmd ? cmd : "NULL");
 #endif
        if (!cmd || !*cmd)
                return -1;      /* empty command, do nothing */
@@ -528,7 +541,7 @@ int run_command (const char *cmd, bd_t *bd, int flag)
         */
 
 #ifdef DEBUG_PARSER
-       printf ("[PROCESS_SEPARATORS] %s\n", s);
+       printf ("[PROCESS_SEPARATORS] %s\n", cmd);
 #endif
        while (*str) {
                const char *sep;
@@ -609,6 +622,14 @@ int run_command (const char *cmd, bd_t *bd, int flag)
 
 /****************************************************************************/
 
+/* WARNING:
+ *
+ * We must create a temporary copy of each value we get from
+ * getenv(), since getenv() returns a pointer directly to the
+ * environment data, which may change magicly when the command we run
+ * creates or modifies environment variables (like "bootp" does).
+ */
+
 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
 void do_run (cmd_tbl_t * cmdtp, bd_t * bd, int flag, int argc, char *argv[])
 {
@@ -620,7 +641,13 @@ void do_run (cmd_tbl_t * cmdtp, bd_t * bd, int flag, int argc, char *argv[])
        }
 
        for (i=1; i<argc; ++i) {
-               run_command (getenv (argv[i]), bd, flag);
+               char arg_buf[CFG_CBSIZE];
+               char *val = getenv (argv[i]);
+               if (val) {
+                       strcpy (arg_buf, val);
+                       val = arg_buf;
+               }
+               run_command (val, bd, flag);
        }
 }
 #endif