From patchwork Wed May 11 16:41:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 67568 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp298138qge; Wed, 11 May 2016 08:55:57 -0700 (PDT) X-Received: by 10.140.94.67 with SMTP id f61mr4326462qge.56.1462982157367; Wed, 11 May 2016 08:55:57 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id x62si5640422qke.0.2016.05.11.08.55.57; Wed, 11 May 2016 08:55:57 -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 06FC16163C; Wed, 11 May 2016 15:55:57 +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 E1994616C4; Wed, 11 May 2016 15:46:14 +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 A66D961658; Wed, 11 May 2016 15:46:03 +0000 (UTC) Received: from mail-lf0-f47.google.com (mail-lf0-f47.google.com [209.85.215.47]) by lists.linaro.org (Postfix) with ESMTPS id C42026165F for ; Wed, 11 May 2016 15:44:18 +0000 (UTC) Received: by mail-lf0-f47.google.com with SMTP id j8so53743363lfd.2 for ; Wed, 11 May 2016 08:44:18 -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=96NrZbjSzeIIW9wLBromjw6iad4VvLiUsdi9lr4fAY8=; b=UTdlHwRB/RjsqKScY0+q7v0sr+25kUNtrGZBtsKEHjKseDcFHwTCfFMjdMQNdWQtvv 0bKKKDZravhYMjXZs29GYMjhtXZ2HWFihwd367ufJwLukDdpGW/s6fOhEyY53SPl3mfh UCd2VfdrVd/X2OVkJ7QJOSHnMBFXAJ9pxn4qFiaQSlHjdrk5SJPA9+SOk8ujvjac1gPY i+C03movJL51dgyvAqzHhePo+SsKRkj2B3NEqZzo/3oF3s5HjO0XFI62Ld0mDSXpSoPs 7U1NaY8wl+Qkw5GPpj7XiNy0XM9+Dt/ULGbahH+t9za8Aw72K3/9aDig4y7MHy4XMTod k7ZQ== X-Gm-Message-State: AOPr4FUKk+tu5GaTnhTAon7Nabb4Xb19qNSCnAcy/9/WI4VQ9XWfKGXu0K0UQB3X3VsG6bvtix8= X-Received: by 10.25.218.14 with SMTP id r14mr1985220lfg.120.1462981457706; Wed, 11 May 2016 08:44:17 -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.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 May 2016 08:44:17 -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:55 +0200 Message-Id: <1462984942-53326-12-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 11/38] helper: adding a function to merge getopt parameters 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" A function merging two sets to getopt parmameters (command line argument description) into one single set is added Signed-off-by: Christophe Milard --- helper/include/odp/helper/linux.h | 42 ++++++++++++++++++++++ helper/linux.c | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/helper/include/odp/helper/linux.h b/helper/include/odp/helper/linux.h index f67aa30..9767af4 100644 --- a/helper/include/odp/helper/linux.h +++ b/helper/include/odp/helper/linux.h @@ -194,6 +194,48 @@ int odph_odpthreads_create(odph_odpthread_t *thread_tbl, */ int odph_odpthreads_join(odph_odpthread_t *thread_tbl); +/** + * Merge getopt options + * + * Given two sets of getopt options (each containing possibly both short + * options -a string- and long options -a option array-) this function + * return a single set (i.e. a string for short and an array for long) + * being the concatenation of the two given sets. + * Due to the fact that the size of these arrays is unknown at compilation + * time, this function actually mallocs the the resulting arrays. + * The fourth and fith parameters are actually pointers where these malloc'ed + * areas are returned. + * This means that the caller of this function has to free the two returned + * areas! + * + * @param shortopts1 first set of short options (a string) + * @param shortopts2 second set of short options (a string) + * @param longopts1 first set of long options (a getopt option array) + * @param longopts2 second set of long options (a getopt option array) + * @param shortopts a pointer where the address of the short options list + * (a string) is returned. It contains the concatenation of + * the two given short option strings. + * @param longopts a pointer where the address of the long options list + * (a getopt option array) is returned. + * It contains the concatenation of the two given long + * option arrays. + * if any of shortopts1, shortopts2, longopts1, longopts2 is NULL, the + * corresponding list as assumed to be empty. + * if any of shortopts, longopts is NULL, the corresponding malloc is not + * performed. + * + * @return On success: 0 : both shortopts and longopts are returned (assuming + * the given pointer where not null), possibly + * pointing to an empty string or an empty option array. + * On success, the caller is due to free these areas. + * On failure: -1: Nothing is malloc'ed. + */ +int odph_merge_getopt_options(const char *shortopts1, + const char *shortopts2, + const struct option *longopts1, + const struct option *longopts2, + char **shortopts, + struct option **longopts); /** * Parse linux helper options diff --git a/helper/linux.c b/helper/linux.c index b8d4f49..d1b7825 100644 --- a/helper/linux.c +++ b/helper/linux.c @@ -500,6 +500,80 @@ int odph_odpthreads_join(odph_odpthread_t *thread_tbl) } /* + * return the number of elements in an array of getopt options, excluding the + * terminating {0,0,0,0} + */ +static int get_getopt_options_length(const struct option *longopts) +{ + int l = 0; + + if (!longopts) + return 0; + + while (longopts[l].name) + l++; + + return l; +} + +/* Merge getopt options */ +int odph_merge_getopt_options(const char *shortopts1, + const char *shortopts2, + const struct option *longopts1, + const struct option *longopts2, + char **shortopts, + struct option **longopts) +{ + int shortopts1_len; + int shortopts2_len; + int longopts1_len; + int longopts2_len; + int index; + int res_index = 0; + struct option termination = {0, 0, 0, 0}; + + /* merge short options: */ + if (shortopts) { + shortopts1_len = (shortopts1) ? strlen(shortopts1) : 0; + shortopts2_len = (shortopts2) ? strlen(shortopts2) : 0; + *shortopts = malloc(shortopts1_len + shortopts2_len + 1); + if (!*shortopts) + return -1; + + (*shortopts)[0] = 0; + + if (shortopts1) + strcpy((*shortopts), shortopts1); + if (shortopts2) + strcat((*shortopts), shortopts2); + } + + /* merge long options */ + if (!longopts) + return 0; + + longopts1_len = get_getopt_options_length(longopts1); + longopts2_len = get_getopt_options_length(longopts2); + *longopts = malloc(sizeof(struct option) * + (longopts1_len + longopts2_len + 1)); + if (!*longopts) { + if (shortopts) + free(*shortopts); + return -1; + } + + for (index = 0; (longopts1) && (longopts1[index].name); index++) + (*longopts)[res_index++] = longopts1[index]; + + for (index = 0; (longopts2) && (longopts2[index].name); index++) + (*longopts)[res_index++] = longopts2[index]; + + (*longopts)[res_index] = termination; + + return 0; +} + +/* * Parse command line options to extract options affecting helpers. */ int odph_parse_options(int argc, char *argv[])