From patchwork Fri Apr 11 02:01:29 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 338321 X-Patchwork-Delegate: trini@ti.com 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 8C6691400DF for ; Fri, 11 Apr 2014 12:03:39 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A1DCB4B657; Fri, 11 Apr 2014 04:03:28 +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 IcOq7nV06rez; Fri, 11 Apr 2014 04:03:28 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 297614B65A; Fri, 11 Apr 2014 04:02:48 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 11E624B664 for ; Fri, 11 Apr 2014 04:02:38 +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 27R9qFl61TH2 for ; Fri, 11 Apr 2014 04:02:37 +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-ob0-f202.google.com (mail-ob0-f202.google.com [209.85.214.202]) by theia.denx.de (Postfix) with ESMTPS id 55DC84B662 for ; Fri, 11 Apr 2014 04:02:29 +0200 (CEST) Received: by mail-ob0-f202.google.com with SMTP id wo20so1005067obc.3 for ; Thu, 10 Apr 2014 19:02:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vVqEyxtSs7i/FTB0xCTwAj7gU4sgGR5BGZy5ye7mYJc=; b=aMKfEf2uyEPCDF5AUGVPp6BfYMFCUAENg1IvaNWsg7OEzTpVKVaMNsVq8X8zMsvl8K +Bh5fWVNpN1Wmm9TgaiYpcmMOq+oZu9QWzJGqbLrMe2EdjtGzolRyLXF0DLK2zwCAvXK FnjFg7TGRzt4uby5jGR/7tcITis2yCHtatf5qpMMbezMVUHHAdL5h/1DdCH1J3xBFrEq 3BoNFEnb+Wf1eo2rRcxinKGcNX1rckwPc/hz9aGII+Stm81clxV/4fG5ubjdyoMQlKdP sOFYy7vun4t/d4sB0N2M+/i1hz14HbYiyukgyJQRSFWLKTqboN3GOXjPeq3M3EDza1Bb UFTA== X-Gm-Message-State: ALoCoQn+sYDqTDoa5N+qDCXUVN2kdrxNASnAT8PyV97REoJTF5JbSYdv6QL4dnjt15oNMi1LOlIN/EtEgsZWCcqnC/8SDzG+a/xf3rJxWhAgLTPYpQJQZXyPAzAyK1B2DC+ZpD0el+06HPvhSky40xub4LWadnYZxd0BtvcRa+iynpuSUT1ZeQCGQyYrSOd+lblULxqP55vh X-Received: by 10.50.50.211 with SMTP id e19mr555050igo.4.1397181748232; Thu, 10 Apr 2014 19:02:28 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id s65si946194yhc.2.2014.04.10.19.02.28 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Apr 2014 19:02:28 -0700 (PDT) Received: from kaki.bld.corp.google.com (kaki.bld.corp.google.com [172.29.216.32]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 1205A31C26C; Thu, 10 Apr 2014 19:02:28 -0700 (PDT) Received: by kaki.bld.corp.google.com (Postfix, from userid 121222) id 6E852220D51; Thu, 10 Apr 2014 20:02:27 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Thu, 10 Apr 2014 20:01:29 -0600 Message-Id: <1397181696-7271-8-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.9.1.423.g4596e3a In-Reply-To: <1397181696-7271-1-git-send-email-sjg@chromium.org> References: <1397181696-7271-1-git-send-email-sjg@chromium.org> Cc: Tom Rini Subject: [U-Boot] [PATCH 07/14] Move command line API into cli.c 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 We now have a single entry point to the CLI, whether simple or hush. Put this in its own file. Signed-off-by: Simon Glass --- common/Makefile | 1 + common/cli.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/main.c | 93 ------------------------------------------------- 3 files changed, 107 insertions(+), 93 deletions(-) create mode 100644 common/cli.c diff --git a/common/Makefile b/common/Makefile index b707e39..41b6ef5 100644 --- a/common/Makefile +++ b/common/Makefile @@ -18,6 +18,7 @@ endif # We always have this since drivers/ddr/fs/interactive.c needs it obj-y += cli_simple.o +obj-y += cli.o obj-y += cli_readline.o obj-y += s_record.o obj-y += xyzModem.o diff --git a/common/cli.c b/common/cli.c new file mode 100644 index 0000000..9cf7ba1 --- /dev/null +++ b/common/cli.c @@ -0,0 +1,106 @@ +/* + * (C) Copyright 2000 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. + * + * Add to readline cmdline-editing by + * (C) Copyright 2005 + * JinHua Luo, GuangDong Linux Center, + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include +#include +#include + +/* + * Run a command using the selected parser. + * + * @param cmd Command to run + * @param flag Execution flags (CMD_FLAG_...) + * @return 0 on success, or != 0 on error. + */ +int run_command(const char *cmd, int flag) +{ +#ifndef CONFIG_SYS_HUSH_PARSER + /* + * cli_run_command can return 0 or 1 for success, so clean up + * its result. + */ + if (cli_simple_run_command(cmd, flag) == -1) + return 1; + + return 0; +#else + return parse_string_outer(cmd, + FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); +#endif +} + +int run_command_list(const char *cmd, int len, int flag) +{ + int need_buff = 1; + char *buff = (char *)cmd; /* cast away const */ + int rcode = 0; + + if (len == -1) { + len = strlen(cmd); +#ifdef CONFIG_SYS_HUSH_PARSER + /* hush will never change our string */ + need_buff = 0; +#else + /* the built-in parser will change our string if it sees \n */ + need_buff = strchr(cmd, '\n') != NULL; +#endif + } + if (need_buff) { + buff = malloc(len + 1); + if (!buff) + return 1; + memcpy(buff, cmd, len); + buff[len] = '\0'; + } +#ifdef CONFIG_SYS_HUSH_PARSER + rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); +#else + /* + * This function will overwrite any \n it sees with a \0, which + * is why it can't work with a const char *. Here we are making + * using of internal knowledge of this function, to avoid always + * doing a malloc() which is actually required only in a case that + * is pretty rare. + */ + rcode = cli_simple_run_command_list(buff, flag); + if (need_buff) + free(buff); +#endif + + return rcode; +} + +/****************************************************************************/ + +#if defined(CONFIG_CMD_RUN) +int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + int i; + + if (argc < 2) + return CMD_RET_USAGE; + + for (i = 1; i < argc; ++i) { + char *arg; + + arg = getenv(argv[i]); + if (arg == NULL) { + printf("## Error: \"%s\" not defined\n", argv[i]); + return 1; + } + + if (run_command(arg, flag) != 0) + return 1; + } + return 0; +} +#endif diff --git a/common/main.c b/common/main.c index 45f1b5c..91a5b18 100644 --- a/common/main.c +++ b/common/main.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -90,95 +89,3 @@ void main_loop(void) cli_loop(); #endif /*CONFIG_SYS_HUSH_PARSER*/ } - -/****************************************************************************/ - -/* - * Run a command using the selected parser. - * - * @param cmd Command to run - * @param flag Execution flags (CMD_FLAG_...) - * @return 0 on success, or != 0 on error. - */ -int run_command(const char *cmd, int flag) -{ -#ifndef CONFIG_SYS_HUSH_PARSER - /* - * cli_run_command can return 0 or 1 for success, so clean up - * its result. - */ - if (cli_simple_run_command(cmd, flag) == -1) - return 1; - - return 0; -#else - return parse_string_outer(cmd, - FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); -#endif -} - -int run_command_list(const char *cmd, int len, int flag) -{ - int need_buff = 1; - char *buff = (char *)cmd; /* cast away const */ - int rcode = 0; - - if (len == -1) { - len = strlen(cmd); -#ifdef CONFIG_SYS_HUSH_PARSER - /* hush will never change our string */ - need_buff = 0; -#else - /* the built-in parser will change our string if it sees \n */ - need_buff = strchr(cmd, '\n') != NULL; -#endif - } - if (need_buff) { - buff = malloc(len + 1); - if (!buff) - return 1; - memcpy(buff, cmd, len); - buff[len] = '\0'; - } -#ifdef CONFIG_SYS_HUSH_PARSER - rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); -#else - /* - * This function will overwrite any \n it sees with a \0, which - * is why it can't work with a const char *. Here we are making - * using of internal knowledge of this function, to avoid always - * doing a malloc() which is actually required only in a case that - * is pretty rare. - */ - rcode = cli_simple_run_command_list(buff, flag); - if (need_buff) - free(buff); -#endif - - return rcode; -} - -/****************************************************************************/ - -#if defined(CONFIG_CMD_RUN) -int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) -{ - int i; - - if (argc < 2) - return CMD_RET_USAGE; - - for (i=1; i