From patchwork Wed May 11 16:41:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 67569 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp298518qge; Wed, 11 May 2016 08:56:48 -0700 (PDT) X-Received: by 10.140.104.48 with SMTP id z45mr4318849qge.49.1462982208408; Wed, 11 May 2016 08:56:48 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id k21si5623885qke.100.2016.05.11.08.56.48; Wed, 11 May 2016 08:56:48 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 1C1BF61657; Wed, 11 May 2016 15:56:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 3574461653; Wed, 11 May 2016 15:46:22 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 613136165B; Wed, 11 May 2016 15:46:07 +0000 (UTC) Received: from mail-lf0-f53.google.com (mail-lf0-f53.google.com [209.85.215.53]) by lists.linaro.org (Postfix) with ESMTPS id 0ABFF6165B for ; Wed, 11 May 2016 15:44:20 +0000 (UTC) Received: by mail-lf0-f53.google.com with SMTP id m64so53858815lfd.1 for ; Wed, 11 May 2016 08:44:19 -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=cq9LzwZVdEw4+8TNNagsOUW6muqstXts/x5REM/6SNg=; b=fFI1hhqcV6oOLnBhZjge5O4uDR8J75TReASewHhJscYJwS4bcM9FfJw8ejHVtUSKtO pEW9o1OvYrqtTie8NtJdDyI+VHFUnOK1NhRh0drHtdnmCzGi9sf8VS4M2m984fWxB6cW xF6CF2q2pyajZTHTcOGl6AY0sgfY0MgUG3wtIITRrJsyb4P74C3FdDDRoRzYe59VWNf4 zkqKz0V3kRyN+BxrxIzAh5zzzK4hr4//xgFZ05jLkkYMKIiy00F8/Rwj2MmLb9RowiuZ HycRXtWQZYYTajZln7Ol/oi3fGb1NZPxFMqBfxVlTAN/TZSMll+7S/HgMEENURi89Typ Xw+A== X-Gm-Message-State: AOPr4FXnL3MXuH8aafSb45YThx0l1K+I5iXlktf1Bmgeo9KSKbqQs7qlFnRLTUYcPXOtBbBI+O4= X-Received: by 10.25.24.35 with SMTP id o35mr1935177lfi.25.1462981458907; Wed, 11 May 2016 08:44:18 -0700 (PDT) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-90-46.cust.bredband2.com. [83.233.90.46]) by smtp.gmail.com with ESMTPSA id n37sm1421164lfg.46.2016.05.11.08.44.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 May 2016 08:44:18 -0700 (PDT) From: Christophe Milard To: brian.brooks@linaro.org, mike.holmes@linaro.org, lng-odp@lists.linaro.org Date: Wed, 11 May 2016 18:41:56 +0200 Message-Id: <1462984942-53326-13-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1462984942-53326-1-git-send-email-christophe.milard@linaro.org> References: <1462984942-53326-1-git-send-email-christophe.milard@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv6 12/38] helper: parsing the complete set of options X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" The odph_parse_options() function is given the ability to receive getopt command line description parameter from it caller, hence allowing the caller to have some command line parameter(s). The caller shall first call odph_parse_options() with its own parameter description as parameter. odph_parse_options() is then checking the complete set of options, issuing error message for unknown options (those being neither a caller's valid command line option or a helper valid command line option), and collecting the sementic of helper options. Then the caller shall parse the sementic of its own options, with the opterr variable set to zero (hence ignoring helper options). Signed-off-by: Christophe Milard --- helper/include/odp/helper/linux.h | 16 +++++++++++++++- helper/linux.c | 24 +++++++++++++++++++----- helper/test/odpthreads.c | 2 +- test/validation/common/odp_cunit_common.c | 2 +- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/helper/include/odp/helper/linux.h b/helper/include/odp/helper/linux.h index 9767af4..71c8027 100644 --- a/helper/include/odp/helper/linux.h +++ b/helper/include/odp/helper/linux.h @@ -25,6 +25,7 @@ extern "C" { #include #include +#include #include /** Thread parameter for Linux pthreads and processes */ @@ -241,15 +242,28 @@ int odph_merge_getopt_options(const char *shortopts1, * Parse linux helper options * * Parse the command line options. Pick up options meant for the helper itself. + * If the caller is also having a set of option to parse, it should include + * their description here (shortopts desribes the short options and longopts + * describes the long options, as for getopt_long()). + * This function will issue errors on unknown arguments, so callers failing + * to pass their own command line options description here will see their + * options rejected. + * (the caller wants to set opterr to zero when parsing its own stuff + * with getopts to avoid reacting on helper's options). * * @param argc argument count * @param argv argument values + * @param caller_shortopts caller's set of short options (string). or NULL. + * @param caller_longopts caller's set of long options (getopt option array). + * or NULL. * * @return On success: 0 * On failure: -1 (failure occurs only if a value passed for a helper * option is invalid. callers cannot have own options) */ -int odph_parse_options(int argc, char *argv[]); +int odph_parse_options(int argc, char *argv[], + const char *caller_shortopts, + const struct option *caller_longopts); #ifdef __cplusplus } #endif diff --git a/helper/linux.c b/helper/linux.c index d1b7825..5fc09a1 100644 --- a/helper/linux.c +++ b/helper/linux.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include @@ -576,27 +575,39 @@ int odph_merge_getopt_options(const char *shortopts1, /* * Parse command line options to extract options affecting helpers. */ -int odph_parse_options(int argc, char *argv[]) +int odph_parse_options(int argc, char *argv[], + const char *caller_shortopts, + const struct option *caller_longopts) { int c; + char *shortopts; + struct option *longopts; - static struct option long_options[] = { + static struct option helper_long_options[] = { /* These options set a flag. */ {"odph_proc", no_argument, &helper_options.proc, 1}, {"odph_thread", no_argument, &helper_options.thrd, 1}, {0, 0, 0, 0} }; + static char *helper_short_options = ""; + /* defaults: */ helper_options.proc = false; helper_options.thrd = false; + /* merge caller's command line options descriptions with helper's: */ + if (odph_merge_getopt_options(caller_shortopts, helper_short_options, + caller_longopts, helper_long_options, + &shortopts, &longopts) < 0) + return -1; + while (1) { /* getopt_long stores the option index here. */ int option_index = 0; - c = getopt_long (argc, argv, "", - long_options, &option_index); + c = getopt_long (argc, argv, + shortopts, longopts, &option_index); /* Detect the end of the options. */ if (c == -1) @@ -605,5 +616,8 @@ int odph_parse_options(int argc, char *argv[]) optind = 0; /* caller expects this to be zero if it parses too*/ + free(shortopts); + free(longopts); + return 0; } diff --git a/helper/test/odpthreads.c b/helper/test/odpthreads.c index 369da62..bba4fa5 100644 --- a/helper/test/odpthreads.c +++ b/helper/test/odpthreads.c @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) char cpumaskstr[ODP_CPUMASK_STR_SIZE]; /* let helper collect its own arguments (e.g. --odph_proc) */ - odph_parse_options(argc, argv); + odph_parse_options(argc, argv, NULL, NULL); if (odp_init_global(&instance, NULL, NULL)) { LOG_ERR("Error: ODP global init failed.\n"); diff --git a/test/validation/common/odp_cunit_common.c b/test/validation/common/odp_cunit_common.c index be23c6b..7df9aa6 100644 --- a/test/validation/common/odp_cunit_common.c +++ b/test/validation/common/odp_cunit_common.c @@ -354,5 +354,5 @@ int odp_cunit_register(odp_suiteinfo_t testsuites[]) */ int odp_cunit_parse_options(int argc, char *argv[]) { - return odph_parse_options(argc, argv); + return odph_parse_options(argc, argv, NULL, NULL); }