From patchwork Mon Aug 5 11:45:25 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Imre Deak X-Patchwork-Id: 2838643 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 819D89F479 for ; Mon, 5 Aug 2013 11:48:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8596A20160 for ; Mon, 5 Aug 2013 11:48:01 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 821702015B for ; Mon, 5 Aug 2013 11:48:00 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9ECDFE6D8F for ; Mon, 5 Aug 2013 04:48:00 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga14.intel.com (mga14.intel.com [143.182.124.37]) by gabe.freedesktop.org (Postfix) with ESMTP id A89D8E6D76 for ; Mon, 5 Aug 2013 04:45:49 -0700 (PDT) Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga102.ch.intel.com with ESMTP; 05 Aug 2013 04:45:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.89,817,1367996400"; d="scan'208";a="341831292" Received: from intelbox.fi.intel.com (HELO localhost) ([10.237.72.70]) by azsmga001.ch.intel.com with ESMTP; 05 Aug 2013 04:45:34 -0700 From: Imre Deak To: intel-gfx@lists.freedesktop.org Date: Mon, 5 Aug 2013 14:45:25 +0300 Message-Id: <1375703126-19323-4-git-send-email-imre.deak@intel.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1375703126-19323-1-git-send-email-imre.deak@intel.com> References: <1375703126-19323-1-git-send-email-imre.deak@intel.com> Subject: [Intel-gfx] [igt PATCH 3/4] lib: add subtest extra command line option handling X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: intel-gfx-bounces+patchwork-intel-gfx=patchwork.kernel.org@lists.freedesktop.org Errors-To: intel-gfx-bounces+patchwork-intel-gfx=patchwork.kernel.org@lists.freedesktop.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP At the moment any command line option handling done by tests will interfere with the option handling of the subtest interface. To fix this add a new version of the subtest_init function accepting optional short and long command line options. Merge these together with the subtest interface's own long options and handle both together in the same getopt_long call. Signed-off-by: Imre Deak --- lib/drmtest.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- lib/drmtest.h | 6 +++++ 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/lib/drmtest.c b/lib/drmtest.c index afbaa35..7a68091 100644 --- a/lib/drmtest.c +++ b/lib/drmtest.c @@ -657,7 +657,21 @@ void drmtest_stop_signal_helper(void) static bool list_subtests = false; static char *run_single_subtest = NULL; -void drmtest_subtest_init(int argc, char **argv) +static void print_usage(const char *command_str, const char *help_str, + bool output_on_stderr) +{ + FILE *f = output_on_stderr ? stderr : stdout; + + fprintf(f, "Usage: %s [OPTIONS]\n" + " --list-subtests\n" + " --run-subtest \n" + "%s\n", command_str, help_str); +} + +int drmtest_subtest_init_parse_opts(int argc, char **argv, const char *opts, + struct option *long_opts, + const char *help_str, + drmtest_opt_handler_t opt_handler) { int c, option_index = 0; static struct option long_options[] = { @@ -665,24 +679,75 @@ void drmtest_subtest_init(int argc, char **argv) {"run-subtest", 1, 0, 'r'}, {NULL, 0, 0, 0,} }; + struct option help_opt = + {"help", 0, 0, 'h'}; + const char *command_str; + char *short_opts; + struct option *combined_opts; + int extra_opts; + int all_opts; + int ret = 0; + + command_str = argv[0]; + if (strrchr(command_str, '/')) + command_str = strrchr(command_str, '/') + 1; + + all_opts = 0; + while (long_opts && long_opts[all_opts].name) + all_opts++; + extra_opts = all_opts; + if (help_str) + all_opts++; + all_opts += ARRAY_SIZE(long_options); + + combined_opts = malloc(all_opts * sizeof(*combined_opts)); + memcpy(combined_opts, long_opts, extra_opts * sizeof(*combined_opts)); + if (help_str) { + combined_opts[extra_opts] = help_opt; + extra_opts++; + } + memcpy(&combined_opts[extra_opts], long_options, + ARRAY_SIZE(long_options) * sizeof(*combined_opts)); - /* supress getopt errors about unknown options */ - opterr = 0; - /* restrict the option parsing to long option names to avoid collisions - * with options the test declares */ - while((c = getopt_long(argc, argv, "", - long_options, &option_index)) != -1) { + ret = asprintf(&short_opts, "%s%s", + opts ? opts : "", help_str ? "h" : ""); + assert(ret >= 0); + + while ((c = getopt_long(argc, argv, short_opts, combined_opts, + &option_index)) != -1) { switch(c) { case 'l': - list_subtests = true; - goto out; + if (!run_single_subtest) + list_subtests = true; + break; case 'r': - run_single_subtest = strdup(optarg); + if (!list_subtests) + run_single_subtest = strdup(optarg); + break; + case '?': + case 'h': + print_usage(command_str, help_str, c == '?'); + ret = c == '?' ? -2 : -1; goto out; + default: + ret = opt_handler(c, option_index); + if (ret) + goto out; } } out: + return ret; +} + +void drmtest_subtest_init(int argc, char **argv) +{ + /* supress getopt errors about unknown options */ + opterr = 0; + + (void)drmtest_subtest_init_parse_opts(argc, argv, NULL, NULL, NULL, + NULL); + /* reset opt parsing */ optind = 1; } diff --git a/lib/drmtest.h b/lib/drmtest.h index 172bed5..5ca7e2e 100644 --- a/lib/drmtest.h +++ b/lib/drmtest.h @@ -94,6 +94,12 @@ void drmtest_progress(const char *header, uint64_t i, uint64_t total); /* subtest infrastructure */ void drmtest_subtest_init(int argc, char **argv); +typedef int (*drmtest_opt_handler_t)(int opt, int opt_index); +struct option; +int drmtest_subtest_init_parse_opts(int argc, char **argv, const char *opts, + struct option *long_opts, + const char *help_str, + drmtest_opt_handler_t opt_handler); bool drmtest_run_subtest(const char *subtest_name); bool drmtest_only_list_subtests(void);