From patchwork Sat Jan 14 00:01:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Walle X-Patchwork-Id: 136056 X-Patchwork-Delegate: wd@denx.de 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 DE057B6F68 for ; Sat, 14 Jan 2012 11:02:13 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0F593288A7; Sat, 14 Jan 2012 01:02:10 +0100 (CET) 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 d63PL524R-kc; Sat, 14 Jan 2012 01:02:09 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 69A09288A8; Sat, 14 Jan 2012 01:02:07 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 960E6288A8 for ; Sat, 14 Jan 2012 01:02:05 +0100 (CET) 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 1krDggf4r8gC for ; Sat, 14 Jan 2012 01:02:04 +0100 (CET) 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.serverraum.org (mail.serverraum.org [78.47.150.89]) by theia.denx.de (Postfix) with ESMTP id 84865288A7 for ; Sat, 14 Jan 2012 01:02:02 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail.serverraum.org (Postfix) with ESMTP id 751B73EFAF; Sat, 14 Jan 2012 01:04:55 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.serverraum.org Received: from mail.serverraum.org ([127.0.0.1]) by localhost (web.serverraum.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zAktb0Wpxdo0; Sat, 14 Jan 2012 01:04:55 +0100 (CET) Received: from thanatos.fritz.box (95-89-251-205-dynip.superkabel.de [95.89.251.205]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.serverraum.org (Postfix) with ESMTPSA id 2170E3EFAD; Sat, 14 Jan 2012 01:04:55 +0100 (CET) From: Michael Walle To: u-boot@lists.denx.de Date: Sat, 14 Jan 2012 01:01:54 +0100 Message-Id: <1326499314-8121-1-git-send-email-michael@walle.cc> X-Mailer: git-send-email 1.7.2.5 Subject: [U-Boot] [PATCH v2] main: unify command parsing functions 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 Introduce source_commands() which incorporates run_command2() and parts of source(). All command script are now treated the same, that is newlines are accepted within a command script and variable. Signed-off-by: Michael Walle Cc: Wolfgang Denk Cc: Mike Frysinger --- This patch supersedes "cmd_source: introduce run_script()" v2: - argument is now const char* - unify run_commands2() and source() - allow newlines in command variables - use strsep() common/cmd_pxe.c | 2 +- common/cmd_source.c | 32 +------------------------------ common/main.c | 52 ++++++++++++++++++++++++++++++++++---------------- include/common.h | 4 +-- 4 files changed, 38 insertions(+), 52 deletions(-) diff --git a/common/cmd_pxe.c b/common/cmd_pxe.c index 7c0cb66..347fde1 100644 --- a/common/cmd_pxe.c +++ b/common/cmd_pxe.c @@ -537,7 +537,7 @@ static int label_localboot(struct pxe_label *label) printf("running: %s\n", dupcmd); - ret = run_command2(dupcmd, 0); + ret = source_commands(dupcmd, 0); free(dupcmd); diff --git a/common/cmd_source.c b/common/cmd_source.c index 16a627a..33242df 100644 --- a/common/cmd_source.c +++ b/common/cmd_source.c @@ -160,38 +160,8 @@ source (ulong addr, const char *fit_uname) memmove (cmd, (char *)data, len); *(cmd + len) = 0; -#ifdef CONFIG_SYS_HUSH_PARSER /*?? */ - rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON); -#else - { - char *line = cmd; - char *next = cmd; + rcode = source_commands(cmd, 0); - /* - * break into individual lines, - * and execute each line; - * terminate on error. - */ - while (*next) { - if (*next == '\n') { - *next = '\0'; - /* run only non-empty commands */ - if (*line) { - debug ("** exec: \"%s\"\n", - line); - if (run_command (line, 0) < 0) { - rcode = 1; - break; - } - } - line = next + 1; - } - ++next; - } - if (rcode == 0 && *line) - rcode = (run_command(line, 0) >= 0); - } -#endif free (cmd); return rcode; } diff --git a/common/main.c b/common/main.c index e96c95a..cc25d38 100644 --- a/common/main.c +++ b/common/main.c @@ -266,26 +266,44 @@ int abortboot(int bootdelay) # endif /* CONFIG_AUTOBOOT_KEYED */ #endif /* CONFIG_BOOTDELAY >= 0 */ +static inline int _run_command(const char *cmd, int flag) +{ +#ifndef CONFIG_SYS_HUSH_PARSER + return (run_command(cmd, flag) == -1); +#else + flag = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP; + return parse_string_outer(cmd, flag); +#endif +} + /* + * Run a series of commands separated by '\n'. + * * Return 0 on success, or != 0 on error. */ -#ifndef CONFIG_CMD_PXE -static inline -#endif -int run_command2(const char *cmd, int flag) +int source_commands(const char *commands, int flag) { -#ifndef CONFIG_SYS_HUSH_PARSER + int rcode = 0; + char *_commands = strdup(commands); + char *stringp = _commands; + char *line; + /* - * run_command can return 0 or 1 for success, so clean up its result. + * break into individual lines and execute each line; + * terminate on error. */ - if (run_command(cmd, flag) == -1) - return 1; + while ((line = strsep(&stringp, "\n"))) { + /* skip empty lines */ + if (*line == '\0') + continue; + if (_run_command(line, flag)) { + rcode = 1; + break; + } + } - return 0; -#else - return parse_string_outer(cmd, - FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); -#endif + free(_commands); + return rcode; } /****************************************************************************/ @@ -354,7 +372,7 @@ void main_loop (void) int prev = disable_ctrlc(1); /* disable Control C checking */ # endif - run_command2(p, 0); + source_commands(p, 0); # ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc(prev); /* restore Control C checking */ @@ -399,7 +417,7 @@ void main_loop (void) int prev = disable_ctrlc(1); /* disable Control C checking */ # endif - run_command2(s, 0); + source_commands(s, 0); # ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc(prev); /* restore Control C checking */ @@ -410,7 +428,7 @@ void main_loop (void) if (menukey == CONFIG_MENUKEY) { s = getenv("menucmd"); if (s) - run_command2(s, 0); + source_commands(s, 0); } #endif /* CONFIG_MENUKEY */ #endif /* CONFIG_BOOTDELAY */ @@ -1408,7 +1426,7 @@ int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) return 1; } - if (run_command2(arg, flag) != 0) + if (source_commands(arg, flag) != 0) return 1; } return 0; diff --git a/include/common.h b/include/common.h index 3df1def..c2e7813 100644 --- a/include/common.h +++ b/include/common.h @@ -261,9 +261,7 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen); /* common/main.c */ void main_loop (void); int run_command (const char *cmd, int flag); -#ifdef CONFIG_CMD_PXE -int run_command2(const char *cmd, int flag); -#endif +int source_commands(const char *cmd, int flag); int readline (const char *const prompt); int readline_into_buffer (const char *const prompt, char * buffer); int parse_line (char *, char *[]);