diff mbox

[U-Boot,1/2] Add option -r to env import to allow import of text files with CRLF as line endings

Message ID 1336913407-7383-1-git-send-email-holler@ahsoftware.de
State Superseded
Headers show

Commit Message

Alexander Holler May 13, 2012, 12:50 p.m. UTC
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.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>
---
 common/cmd_nvedit.c |   17 ++++++++++++++---
 common/env_common.c |    4 ++--
 include/search.h    |    2 +-
 lib/hashtable.c     |   23 +++++++++++++++++++----
 4 files changed, 36 insertions(+), 10 deletions(-)

Comments

Wolfgang Denk May 13, 2012, 6:45 p.m. UTC | #1
Dear Alexander Holler,

In message <1336913407-7383-1-git-send-email-holler@ahsoftware.de> you wrote:
> 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.
> 
> Signed-off-by: Alexander Holler <holler@ahsoftware.de>
> ---
>  common/cmd_nvedit.c |   17 ++++++++++++++---
>  common/env_common.c |    4 ++--
>  include/search.h    |    2 +-
>  lib/hashtable.c     |   23 +++++++++++++++++++----
>  4 files changed, 36 insertions(+), 10 deletions(-)

Please make this optional.  I don't want the increased code size for
all boards when probablyonly one or two will ever activate this.  And
please add documentation, and include a note that the use of this
option is discouraged, as the problem should be addressed outside
U-Boot.

Best regards,

Wolfgang Denk
diff mbox

Patch

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index e1ccdd8..f136953 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -793,11 +793,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
@@ -812,6 +816,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;
@@ -836,6 +841,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;
@@ -851,6 +859,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 = (char *)simple_strtoul(argv[0], NULL, 16);
 
 	if (argc == 2) {
@@ -888,7 +899,7 @@  static int do_env_import(cmd_tbl_t *cmdtp, int flag,
 		addr = (char *)ep->data;
 	}
 
-	if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR) == 0) {
+	if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR, crlf_is_lf) == 0) {
 		error("Environment import failed: errno = %d\n", errno);
 		return 1;
 	}
@@ -974,7 +985,7 @@  U_BOOT_CMD(
 #if defined(CONFIG_CMD_GREPENV)
 	"env grep string [...] - search environment\n"
 #endif
-	"env import [-d] [-t | -b | -c] addr [size] - import environment\n"
+	"env import [-d] [-t [-r] | | -b | -c] addr [size] - import environment\n"
 	"env print [name ...] - print environment\n"
 #if defined(CONFIG_CMD_RUN)
 	"env run var [...] - run commands in an environment variable\n"
diff --git a/common/env_common.c b/common/env_common.c
index c33d22d..9b50ffe 100644
--- a/common/env_common.c
+++ b/common/env_common.c
@@ -182,7 +182,7 @@  void set_default_env(const char *s)
 	}
 
 	if (himport_r(&env_htab, (char *)default_environment,
-			sizeof(default_environment), '\0', 0) == 0)
+			sizeof(default_environment), '\0', 0, 0) == 0)
 		error("Environment import failed: errno = %d\n", errno);
 
 	gd->flags |= GD_FLG_ENV_READY;
@@ -207,7 +207,7 @@  int env_import(const char *buf, int check)
 		}
 	}
 
-	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)) {
 		gd->flags |= GD_FLG_ENV_READY;
 		return 1;
 	}
diff --git a/include/search.h b/include/search.h
index ef53edb..053cf07 100644
--- a/include/search.h
+++ b/include/search.h
@@ -96,7 +96,7 @@  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 __flag, int __crlf_is_lf);
 
 /* Flags for himport_r() */
 #define	H_NOCLEAR	1	/* do not clear hash table before importing */
diff --git a/lib/hashtable.c b/lib/hashtable.c
index abd61c8..e5fa3c3 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -623,9 +623,9 @@  ssize_t hexport_r(struct hsearch_data *htab, const char sep,
  * (entries separated by newline characters).
  *
  * To allow for nicely formatted text input, leading white space
- * (sequences of SPACE and TAB chars) is ignored, and entries starting
- * (after removal of any leading white space) with a '#' character are
- * considered comments and ignored.
+ * (sequences of SPACE and TAB chars) is ignored, all Carriage Returns
+ * are ignored and entries starting (after removal of any leading white
+ * space) with a '#' character are considered comments and ignored.
  *
  * [NOTE: this means that a variable name cannot start with a '#'
  * character.]
@@ -639,7 +639,7 @@  ssize_t hexport_r(struct hsearch_data *htab, const char sep,
  */
 
 int himport_r(struct hsearch_data *htab,
-	      const char *env, size_t size, const char sep, int flag)
+	      const char *env, size_t size, const char sep, int flag, int crlf_is_lf)
 {
 	char *data, *sp, *dp, *name, *value;
 
@@ -698,6 +698,21 @@  int himport_r(struct hsearch_data *htab,
 		}
 	}
 
+	/* Remove all Carriage Returns */
+        if(!size)
+		return 1;		/* everything OK */
+	if(crlf_is_lf) {
+		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;