From patchwork Mon Jan 23 23:41:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 9533735 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 24AFC6042D for ; Mon, 23 Jan 2017 23:41:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17F0D1FEBD for ; Mon, 23 Jan 2017 23:41:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0999828356; Mon, 23 Jan 2017 23:41:18 +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, UNPARSEABLE_RELAY 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 484C11FEBD for ; Mon, 23 Jan 2017 23:41:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750795AbdAWXlQ (ORCPT ); Mon, 23 Jan 2017 18:41:16 -0500 Received: from userp1040.oracle.com ([156.151.31.81]:22049 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750873AbdAWXlQ (ORCPT ); Mon, 23 Jan 2017 18:41:16 -0500 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v0NNfBKx026320 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 23 Jan 2017 23:41:11 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.14.4) with ESMTP id v0NNfBJ4007618 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 23 Jan 2017 23:41:11 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id v0NNf8Fn022359; Mon, 23 Jan 2017 23:41:09 GMT Received: from localhost (/24.21.211.40) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 23 Jan 2017 15:41:08 -0800 Date: Mon, 23 Jan 2017 15:41:07 -0800 From: "Darrick J. Wong" To: sandeen@redhat.com Cc: linux-xfs@vger.kernel.org Subject: [PATCH v2 2/5] xfs_db: fix the 'source' command when passed as a -c option Message-ID: <20170123234107.GE31202@birch.djwong.org> References: <148494391629.5256.3328772079712970611.stgit@birch.djwong.org> <148494392855.5256.2805905613919243845.stgit@birch.djwong.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <148494392855.5256.2805905613919243845.stgit@birch.djwong.org> User-Agent: Mutt/1.5.24 (2015-08-30) X-Source-IP: aserv0021.oracle.com [141.146.126.233] Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The 'source' command is supposed to read commands out of a file and execute them. This works great when done from an interactive command line, but it doesn't work at all when invoked from the command line because we never actually do anything with the opened file. So don't load stdin into the input stack when we're only executing command line options, and use that to decide if source_f is executing from the command line so that we can actually run the input loop. We'll use this for the per-field fuzzing xfstests. Signed-off-by: Darrick J. Wong --- v2: clean up some warts, handle recursive source'ing properly --- db/init.c | 2 +- db/input.c | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 11 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/db/init.c b/db/init.c index 6ebe6b7..ee0d16e 100644 --- a/db/init.c +++ b/db/init.c @@ -237,7 +237,6 @@ main( bool insane_agcount; int start_iocur_sp; - pushfile(stdin); init(argc, argv, &insane_agcount); start_iocur_sp = iocur_sp; @@ -252,6 +251,7 @@ main( goto close_devices; } + pushfile(stdin); while (!done) { if ((input = fetchline()) == NULL) break; diff --git a/db/input.c b/db/input.c index 8f65190..0cb77bb 100644 --- a/db/input.c +++ b/db/input.c @@ -156,7 +156,7 @@ fetchline_internal(void) rval = NULL; for (rlen = iscont = 0; ; ) { - if (inputstacksize == 1) { + if (curinput == stdin) { if (iscont) dbprintf("... "); else @@ -181,18 +181,24 @@ fetchline_internal(void) } if (ferror(curinput) || feof(curinput) || (len = strlen(buf)) == 0) { - popfile(); - if (curinput == NULL) { + /* + * No more input at this inputstack level; pop + * our fd off and return so that a lower + * level fetchline can handle us. If this was + * an interactive session, print a newline + * because ^D doesn't emit one. + */ + if (curinput == stdin) dbprintf("\n"); - return NULL; - } + + popfile(); iscont = 0; rlen = 0; if (rval) { xfree(rval); rval = NULL; } - continue; + return NULL; } if (inputstacksize == 1) logprintf("%s", buf); @@ -225,7 +231,9 @@ fetchline(void) if (inputstacksize == 1) { line = readline(get_prompt()); - if (line && *line) { + if (!line) + dbprintf("\n"); + else if (line && *line) { add_history(line); logprintf("%s", line); } @@ -314,12 +322,27 @@ source_f( char **argv) { FILE *f; + int c, done = 0; + char *input; + char **v; f = fopen(argv[1], "r"); - if (f == NULL) + if (f == NULL) { dbprintf(_("can't open %s\n"), argv[0]); - else - pushfile(f); + return 0; + } + + /* Run the sourced commands now. */ + pushfile(f); + while (!done) { + if ((input = fetchline_internal()) == NULL) + break; + v = breakline(input, &c); + if (c) + done = command(c, v); + doneline(input, v); + } + return 0; }