From patchwork Sun May 13 12:50:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Holler X-Patchwork-Id: 158832 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 7CB5DB7008 for ; Sun, 13 May 2012 22:01:47 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 67C63281B4; Sun, 13 May 2012 14:01:43 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id e+JMnOpVFaOk; Sun, 13 May 2012 14:01:43 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7559E281AD; Sun, 13 May 2012 14:01:42 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D9662281AD for ; Sun, 13 May 2012 14:01:39 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Y2tQ-6HuI1+Z for ; Sun, 13 May 2012 14:01:39 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.ahsoftware.de (h1446028.stratoserver.net [85.214.92.142]) by theia.denx.de (Postfix) with ESMTP id 17BF4281AB for ; Sun, 13 May 2012 14:01:37 +0200 (CEST) Received: by mail.ahsoftware.de (Postfix, from userid 65534) id 2BC62A8472; Sun, 13 May 2012 14:01:37 +0200 (CEST) Received: from eiche.ahsoftware (p57B20414.dip0.t-ipconnect.de [87.178.4.20]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.ahsoftware.de (Postfix) with ESMTPSA id 77FB9A8416 for ; Sun, 13 May 2012 14:01:35 +0200 (CEST) Received: by eiche.ahsoftware (Postfix, from userid 65534) id A89BE3FD96; Sun, 13 May 2012 14:01:34 +0200 (CEST) Received: from beagle.ahsoftware (unknown [192.168.213.4]) by eiche.ahsoftware (Postfix) with ESMTP id DB6C63FD15; Sun, 13 May 2012 12:01:27 +0000 (UTC) From: Alexander Holler To: u-boot@lists.denx.de Date: Sun, 13 May 2012 14:50:06 +0200 Message-Id: <1336913407-7383-1-git-send-email-holler@ahsoftware.de> X-Mailer: git-send-email 1.7.3.4 Subject: [U-Boot] [PATCH 1/2] Add option -r to env import to allow import of text files with CRLF as line endings X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de 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 --- 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(-) 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;