]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
Add option -r to env import to allow import of text files with CRLF as line endings
authorAlexander Holler <holler@ahsoftware.de>
Mon, 14 Jul 2014 15:49:55 +0000 (17:49 +0200)
committerTom Rini <trini@ti.com>
Tue, 22 Jul 2014 11:44:26 +0000 (07:44 -0400)
When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>
common/cmd_nvedit.c
common/env_common.c
include/search.h
lib/hashtable.c

index e6c33956e7b5c493b62128aa5ce6c06ff67bb38a..855808c3e4a4d58bc3437da32725a3b725face01 100644 (file)
@@ -950,11 +950,15 @@ sep_err:
 
 #ifdef CONFIG_CMD_IMPORTENV
 /*
- * env import [-d] [-t | -b | -c] addr [size]
+ * env import [-d] [-t [-r] | -b | -c] addr [size]
  *     -d:     delete existing environment before importing;
  *             otherwise overwrite / append to existion definitions
  *     -t:     assume text format; either "size" must be given or the
  *             text data must be '\0' terminated
+ *     -r:     handle CRLF like LF, that means exported variables with
+ *             a content which ends with \r won't get imported. Used
+ *             to import text files created with editors which are using CRLF
+ *             for line endings. Only effective in addition to -t.
  *     -b:     assume binary format ('\0' separated, "\0\0" terminated)
  *     -c:     assume checksum protected environment format
  *     addr:   memory address to read from
@@ -970,6 +974,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
        int     chk = 0;
        int     fmt = 0;
        int     del = 0;
+       int     crlf_is_lf = 0;
        size_t  size;
 
        cmd = *argv;
@@ -994,6 +999,9 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
                                        goto sep_err;
                                sep = '\n';
                                break;
+                       case 'r':               /* handle CRLF like LF */
+                               crlf_is_lf = 1;
+                               break;
                        case 'd':
                                del = 1;
                                break;
@@ -1009,6 +1017,9 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
        if (!fmt)
                printf("## Warning: defaulting to text format\n");
 
+       if (sep != '\n' && crlf_is_lf )
+               crlf_is_lf = 0;
+
        addr = simple_strtoul(argv[0], NULL, 16);
        ptr = map_sysmem(addr, 0);
 
@@ -1050,8 +1061,8 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
                ptr = (char *)ep->data;
        }
 
-       if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, 0,
-                     NULL) == 0) {
+       if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
+                       crlf_is_lf, 0, NULL) == 0) {
                error("Environment import failed: errno = %d\n", errno);
                return 1;
        }
@@ -1180,7 +1191,7 @@ static char env_help_text[] =
 #endif
 #endif
 #if defined(CONFIG_CMD_IMPORTENV)
-       "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
+       "env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n"
 #endif
        "env print [-a | name ...] - print environment\n"
 #if defined(CONFIG_CMD_RUN)
index 3b979bcb9869996d14937aeba5c2856a7147ae3a..af59c72e1fd7d502f4899d99e8a1458cc7354d13 100644 (file)
@@ -118,7 +118,7 @@ void set_default_env(const char *s)
        }
 
        if (himport_r(&env_htab, (char *)default_environment,
-                       sizeof(default_environment), '\0', flags,
+                       sizeof(default_environment), '\0', flags, 0,
                        0, NULL) == 0)
                error("Environment import failed: errno = %d\n", errno);
 
@@ -135,7 +135,7 @@ int set_default_vars(int nvars, char * const vars[])
         */
        return himport_r(&env_htab, (const char *)default_environment,
                                sizeof(default_environment), '\0',
-                               H_NOCLEAR | H_INTERACTIVE, nvars, vars);
+                               H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars);
 }
 
 #ifdef CONFIG_ENV_AES
@@ -212,7 +212,7 @@ int env_import(const char *buf, int check)
                return ret;
        }
 
-       if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
+       if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
                        0, NULL)) {
                gd->flags |= GD_FLG_ENV_READY;
                return 1;
index ae3efc43ca0b34e9e956437a4d9770fff1159ef6..9701efb2dfe37c51d462ae85bc04648d49192f44 100644 (file)
@@ -102,7 +102,8 @@ extern ssize_t hexport_r(struct hsearch_data *__htab,
  */
 extern int himport_r(struct hsearch_data *__htab,
                     const char *__env, size_t __size, const char __sep,
-                    int __flag, int nvars, char * const vars[]);
+                    int __flag, int __crlf_is_lf, int nvars,
+                    char * const vars[]);
 
 /* Walk the whole table calling the callback on each element */
 extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
index 4356b234ececa2ec586da0198d5306f5de6114f9..18ed5901ec284b63150435bf5af9b8db6b42d373 100644 (file)
@@ -776,7 +776,7 @@ static int drop_var_from_set(const char *name, int nvars, char * vars[])
 
 int himport_r(struct hsearch_data *htab,
                const char *env, size_t size, const char sep, int flag,
-               int nvars, char * const vars[])
+               int crlf_is_lf, int nvars, char * const vars[])
 {
        char *data, *sp, *dp, *name, *value;
        char *localvars[nvars];
@@ -841,6 +841,21 @@ int himport_r(struct hsearch_data *htab,
                }
        }
 
+       if(!size)
+               return 1;               /* everything OK */
+       if(crlf_is_lf) {
+               /* Remove Carriage Returns in front of Line Feeds */
+               unsigned ignored_crs = 0;
+               for(;dp < data + size && *dp; ++dp) {
+                       if(*dp == '\r' &&
+                          dp < data + size - 1 && *(dp+1) == '\n')
+                               ++ignored_crs;
+                       else
+                               *(dp-ignored_crs) = *dp;
+               }
+               size -= ignored_crs;
+               dp = data;
+       }
        /* Parse environment; allow for '\0' and 'sep' as separators */
        do {
                ENTRY e, *rv;