From patchwork Tue Apr 22 15:54:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre Aubert X-Patchwork-Id: 341399 X-Patchwork-Delegate: panto@antoniou-consulting.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 366F71400F9 for ; Wed, 23 Apr 2014 01:55:49 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6BA064B6CD; Tue, 22 Apr 2014 17:55: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 pUOOPNQBWtgE; Tue, 22 Apr 2014 17:55:39 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A55384B6D3; Tue, 22 Apr 2014 17:55:12 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6AD5C4B690 for ; Tue, 22 Apr 2014 17:55:06 +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 6KJjbhsELuw0 for ; Tue, 22 Apr 2014 17:55:03 +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 esa1.staubli.c3s2.iphmx.com (esa1.staubli.c3s2.iphmx.com [68.232.133.30]) by theia.denx.de (Postfix) with ESMTPS id 627714B6A1 for ; Tue, 22 Apr 2014 17:55:01 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.97,904,1389740400"; d="scan'208";a="9822149" Received: from unknown (HELO fav03not.staubli.pri) ([217.109.88.35]) by esa1.staubli.c3s2.iphmx.com with ESMTP; 22 Apr 2014 17:54:18 +0200 Received: from favd8744.staubli.pri ([10.10.100.28]) by fav03not.staubli.pri (Lotus Domino Release 8.5.3FP5) with ESMTP id 2014042217550022-8433 ; Tue, 22 Apr 2014 17:55:00 +0200 Received: from favd8744.staubli.pri (localhost [127.0.0.1]) by favd8744.staubli.pri (8.14.5/8.14.5) with ESMTP id s3MFt0o4014975; Tue, 22 Apr 2014 17:55:00 +0200 Received: (from aubert@localhost) by favd8744.staubli.pri (8.14.5/8.14.5/Submit) id s3MFt0T2014974; Tue, 22 Apr 2014 17:55:00 +0200 From: Pierre Aubert To: u-boot@lists.denx.de Date: Tue, 22 Apr 2014 17:54:46 +0200 Message-Id: <1398182087-14913-3-git-send-email-p.aubert@staubli.com> X-Mailer: git-send-email 1.7.6.5 In-Reply-To: <1398182087-14913-1-git-send-email-p.aubert@staubli.com> References: <1397218337-27204-1-git-send-email-p.aubert@staubli.com> <1398182087-14913-1-git-send-email-p.aubert@staubli.com> X-MIMETrack: Itemize by SMTP Server on FAV03NOT/FAVERGES/STAUBLI(Release 8.5.3FP5|July 31, 2013) at 22/04/2014 17:55:00, Serialize by Router on FAV03NOT/FAVERGES/STAUBLI(Release 8.5.3FP5|July 31, 2013) at 22/04/2014 17:55:01, Serialize complete at 22/04/2014 17:55:01 Cc: Pierre Aubert Subject: [U-Boot] [PATCH V3 2/3] Add the function 'confirm_yesno' for interactive confirmation. 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 User's confirmation is asked in different commands. This commit adds a function for such confirmation. Signed-off-by: Pierre Aubert --- common/cmd_fuse.c | 11 ++--------- common/cmd_nand.c | 16 +++++----------- common/cmd_otp.c | 18 +++--------------- common/console.c | 28 +++++++++++++++++++++++++++- include/common.h | 2 +- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/common/cmd_fuse.c b/common/cmd_fuse.c index 0df57db..abab978 100644 --- a/common/cmd_fuse.c +++ b/common/cmd_fuse.c @@ -33,15 +33,8 @@ static int confirm_prog(void) "what you are doing!\n" "\nReally perform this fuse programming? \n"); - if (getc() == 'y') { - int c; - - putc('y'); - c = getc(); - putc('\n'); - if (c == '\r') - return 1; - } + if (confirm_yesno()) + return 1; puts("Fuse programming aborted\n"); return 0; diff --git a/common/cmd_nand.c b/common/cmd_nand.c index 04ab0f1..a84f7dc 100644 --- a/common/cmd_nand.c +++ b/common/cmd_nand.c @@ -605,22 +605,16 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) opts.spread = spread; if (scrub) { - if (!scrub_yes) - puts(scrub_warn); - - if (scrub_yes) + if (scrub_yes) { opts.scrub = 1; - else if (getc() == 'y') { - puts("y"); - if (getc() == '\r') + } else { + puts(scrub_warn); + if (confirm_yesno()) { opts.scrub = 1; - else { + } else { puts("scrub aborted\n"); return 1; } - } else { - puts("scrub aborted\n"); - return 1; } } ret = nand_erase_opts(nand, &opts); diff --git a/common/cmd_otp.c b/common/cmd_otp.c index 67808aa..593bb8c 100644 --- a/common/cmd_otp.c +++ b/common/cmd_otp.c @@ -158,21 +158,9 @@ int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) lowup(half + count - 1), page + (half + count - 1) / 2, half + count ); - - i = 0; - while (1) { - if (tstc()) { - const char exp_ans[] = "YES\r"; - char c; - putc(c = getc()); - if (exp_ans[i++] != c) { - printf(" Aborting\n"); - return 1; - } else if (!exp_ans[i]) { - puts("\n"); - break; - } - } + if (!confirm_yesno()) { + printf(" Aborting\n"); + return 1; } } diff --git a/common/console.c b/common/console.c index 2dfb788..5453726 100644 --- a/common/console.c +++ b/common/console.c @@ -537,7 +537,33 @@ int ctrlc(void) } return 0; } - +/* Reads user's confirmation. + Returns 1 if user's input is "y", "Y", "yes" or "YES" +*/ +int confirm_yesno(void) +{ + int i; + char str_input[5]; + + /* Flush input */ + while (tstc()) + getc(); + i = 0; + while (i < sizeof(str_input)) { + str_input[i] = getc(); + putc(str_input[i]); + if (str_input[i] == '\r') + break; + i++; + } + putc('\n'); + if (strncmp(str_input, "y\r", 2) == 0 || + strncmp(str_input, "Y\r", 2) == 0 || + strncmp(str_input, "yes\r", 4) == 0 || + strncmp(str_input, "YES\r", 4) == 0) + return 1; + return 0; +} /* pass 1 to disable ctrlc() checking, 0 to enable. * returns previous state */ diff --git a/include/common.h b/include/common.h index baf361b..1d922b9 100644 --- a/include/common.h +++ b/include/common.h @@ -838,7 +838,7 @@ int ctrlc (void); int had_ctrlc (void); /* have we had a Control-C since last clear? */ void clear_ctrlc (void); /* clear the Control-C condition */ int disable_ctrlc (int); /* 1 to disable, 0 to enable Control-C detect */ - +int confirm_yesno(void); /* 1 if input is "y", "Y", "yes" or "YES" */ /* * STDIO based functions (can always be used) */