From patchwork Wed Dec 7 03:47:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Chinner X-Patchwork-Id: 9463833 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E11166022E for ; Wed, 7 Dec 2016 03:47:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CEACD28458 for ; Wed, 7 Dec 2016 03:47:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C2FCF28464; Wed, 7 Dec 2016 03:47:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67E0C28458 for ; Wed, 7 Dec 2016 03:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751347AbcLGDrf (ORCPT ); Tue, 6 Dec 2016 22:47:35 -0500 Received: from ipmail06.adl6.internode.on.net ([150.101.137.145]:56135 "EHLO ipmail06.adl6.internode.on.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752198AbcLGDre (ORCPT ); Tue, 6 Dec 2016 22:47:34 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2BEEgCUhUdYIGuWLHleHAEBBAEBCgEBgzkBAQEBAR+BYIZ0nDgBAQEBAQEGgR2Sb4QWhh4CAgKCI1QBAgEBAQEBAgYBAQEBAQE5RYRpBicvIxAIGDE5AwcUGYhuqmM9izsBMIV0j04FiGKSBJELkExJkUiBThMOg1wcgXEqNIkOAQEB Received: from ppp121-44-150-107.lns20.syd7.internode.on.net (HELO dastard) ([121.44.150.107]) by ipmail06.adl6.internode.on.net with ESMTP; 07 Dec 2016 14:17:29 +1030 Received: from discord.disaster.area ([192.168.1.111]) by dastard with esmtp (Exim 4.80) (envelope-from ) id 1cETCZ-00069O-7B; Wed, 07 Dec 2016 14:47:27 +1100 Received: from dave by discord.disaster.area with local (Exim 4.88) (envelope-from ) id 1cETCZ-0000bH-5x; Wed, 07 Dec 2016 14:47:27 +1100 From: Dave Chinner To: linux-xfs@vger.kernel.org Cc: fstests@vger.kernel.org, amir73il@gmail.com Subject: [PATCH 3/6] libxcmd: merge command() and iterate_command() Date: Wed, 7 Dec 2016 14:47:21 +1100 Message-Id: <20161207034724.1613-4-david@fromorbit.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161207034724.1613-1-david@fromorbit.com> References: <20161207034724.1613-1-david@fromorbit.com> Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Dave Chinner Simplify the command loop further by merging the command loop iteration checks with the command execution function. This removes all visibility of command iteration from the main command execution loop, and enables us to factor and clean up the command loop processing neatly. Signed-Off-By: Dave Chinner --- libxcmd/command.c | 110 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 44 deletions(-) diff --git a/libxcmd/command.c b/libxcmd/command.c index 789aeb5c5e5a..2c94a3199115 100644 --- a/libxcmd/command.c +++ b/libxcmd/command.c @@ -125,22 +125,30 @@ add_user_command(char *optarg) } /* - * To detect one-shot commands, they will return a negative index. If we - * get a negative index on entry, we've already run the one-shot command, - * so we abort straight away. + * Run a command, iterating as necessary. Return 0 for success, non-zero + * if an error occurred. Errors terminate loop iteration immediately. */ static int iterate_command( const cmdinfo_t *ct, - int index) + int argc, + char **argv) { - if (index < 0) + int error = 0; + int j; + + /* if there's nothing to iterate, we're done! */ + if (!iter_func) return 0; - if (ct->flags & CMD_FLAG_ONESHOT) - return -1; - if (iter_func) - return iter_func(index); - return 0; + + for (j = iter_func(0); j; j = iter_func(j)) { + error = command(ct, argc, argv); + if (error) + break; + + } + + return error; } void @@ -150,14 +158,55 @@ add_command_iterator( iter_func = func; } -void -command_loop(void) +static int +process_input( + char *input, + bool iterate) { - int c, i, j = 0, done = 0; - char *input; char **v; const cmdinfo_t *ct; + int c = 0; + int error = 0; + + v = breakline(input, &c); + if (!c) + goto out; + + ct = find_command(v[0]); + if (!ct) { + fprintf(stderr, _("command \"%s\" not found\n"), v[0]); + goto out; + } + + /* oneshot commands don't iterate */ + if (!iterate || (ct->flags & CMD_FLAG_ONESHOT)) + error = command(ct, c, v); + else + error = iterate_command(ct, c, v); +out: + doneline(input, v); + return error; +} + +void +command_loop(void) +{ + char *input; + int done = 0; + int i; + + if (!cmdline) { + /* interactive mode */ + while (!done) { + input = fetchline(); + if (!input) + break; + done = process_input(input, false); + } + return; + } + /* command line mode */ for (i = 0; !done && i < ncmdline; i++) { input = strdup(cmdline[i]); if (!input) { @@ -166,37 +215,10 @@ command_loop(void) cmdline[i], strerror(errno)); exit(1); } - v = breakline(input, &c); - if (c) { - ct = find_command(v[0]); - if (ct) { - j = 0; - while (!done && (j = iterate_command(ct, j))) - done = command(ct, c, v); - } else - fprintf(stderr, _("command \"%s\" not found\n"), - v[0]); - } - doneline(input, v); - } - if (cmdline) { - free(cmdline); - return; - } - while (!done) { - if ((input = fetchline()) == NULL) - break; - v = breakline(input, &c); - if (c) { - ct = find_command(v[0]); - if (ct) - done = command(ct, c, v); - else - fprintf(stderr, _("command \"%s\" not found\n"), - v[0]); - } - doneline(input, v); + done = process_input(input, true); } + free(cmdline); + return; } void