From patchwork Tue Oct 2 06:04:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amitay Isaacs X-Patchwork-Id: 977647 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42PTNp6cG3z9shx for ; Tue, 2 Oct 2018 16:13:30 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="lCWt3NIk"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42PTNp3vCKzF38q for ; Tue, 2 Oct 2018 16:13:30 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="lCWt3NIk"; dkim-atps=neutral X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42PTBb4kxPzF3Bw for ; Tue, 2 Oct 2018 16:04:39 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="lCWt3NIk"; dkim-atps=neutral Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 42PTBb1Bcrz9sj0; Tue, 2 Oct 2018 16:04:39 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ozlabs.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ozlabs.org; s=201707; t=1538460279; bh=gOh7WQZGGg9bpZ+YOXACmLhVkGWenNVrF3oIVFsjqAU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lCWt3NIk30mSDixlufDD5iIjRS1cw7804d0EtAWn05yRrNPpwzqidD46d3Bjc+1tn 1k4ovM0PzW6TJYKvGdJRFczL2mwDj2NWMei/tNnRRICUe74TSj7DkF9VKPVI2ZiiM0 suHlKI/OfE8tpd+LgsgE0S/F6AA/gP4UMs1Dxh2TR0O28N/XjbuhcHJlNtQwN1pqqM ojRZFGzxfX1PbdFH34tBrrws6d3HHUxwkS/hzo0nNbNPnUy+eqEdz4duhnBNKGFILd nZ/KPQjngE4aawuAlcuz4VOV1adGimBcyiGCOvht3QI0P8xvztm0kzX9tLWkSlADAm uWcWG0BA/Pugg== From: Amitay Isaacs To: pdbg@lists.ozlabs.org Date: Tue, 2 Oct 2018 16:04:24 +1000 Message-Id: <20181002060430.3344784-4-amitay@ozlabs.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181002060430.3344784-1-amitay@ozlabs.org> References: <20181002060430.3344784-1-amitay@ozlabs.org> Subject: [Pdbg] [PATCH 03/10] util: Move parse_list() into a separate file X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amitay Isaacs MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" ... and document the function. Signed-off-by: Amitay Isaacs Reviewed-by: Alistair Popple --- Makefile.am | 4 ++- src/main.c | 74 +---------------------------------------- src/util.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 34 +++++++++++++++++++ 4 files changed, 133 insertions(+), 74 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/Makefile.am b/Makefile.am index 86d8733..8bf2ba0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,7 +83,9 @@ pdbg_SOURCES = \ src/reg.c \ src/ring.c \ src/scom.c \ - src/thread.c + src/thread.c \ + src/util.c \ + src/util.h src/main.c: $(DT_headers) diff --git a/src/main.c b/src/main.c index cbcf612..f89ff6b 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,7 @@ #include "options.h" #include "optcmd.h" #include "progress.h" +#include "util.h" #define PR_ERROR(x, args...) \ pdbg_log(PDBG_ERROR, x, ##args) @@ -177,79 +178,6 @@ static void print_usage(char *pname) printf(" %-15s %-27s %s\n", actions[i].name, actions[i].args, actions[i].desc); } -/* Parse argument of the form 0-5,7,9-11,15,17 */ -static bool parse_list(const char *arg, int max, int *list, int *count) -{ - char str[strlen(arg)+1]; - char *tok, *tmp, *saveptr = NULL; - int i; - - assert(max < INT_MAX); - - strcpy(str, arg); - - tmp = str; - while ((tok = strtok_r(tmp, ",", &saveptr)) != NULL) { - char *a, *b, *endptr, *saveptr2 = NULL; - unsigned long int from, to; - - a = strtok_r(tok, "-", &saveptr2); - if (a == NULL) { - return false; - } else { - endptr = NULL; - from = strtoul(a, &endptr, 0); - if (*endptr != '\0') { - fprintf(stderr, "Invalid value %s\n", a); - return false; - } - if (from >= max) { - fprintf(stderr, "Value %s larger than max %d\n", a, max-1); - return false; - } - } - - b = strtok_r(NULL, "-", &saveptr2); - if (b == NULL) { - to = from; - } else { - endptr = NULL; - to = strtoul(b, &endptr, 0); - if (*endptr != '\0') { - fprintf(stderr, "Invalid value %s\n", b); - return false; - } - if (to >= max) { - fprintf(stderr, "Value %s larger than max %d\n", b, max-1); - return false; - } - } - - if (from > to) { - fprintf(stderr, "Invalid range %s-%s\n", a, b); - return false; - } - - for (i = from; i <= to; i++) - list[i] = 1; - - tmp = NULL; - }; - - if (count != NULL) { - int n = 0; - - for (i = 0; i < max; i++) { - if (list[i] == 1) - n++; - } - - *count = n; - } - - return true; -} - #ifdef TARGET_PPC int get_pir(int linux_cpu) { diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..3a4520d --- /dev/null +++ b/src/util.c @@ -0,0 +1,95 @@ +/* Copyright 2018 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include + +/* Parse argument of the form 0-5,7,9-11,15,17 */ +bool parse_list(const char *arg, int max, int *list, int *count) +{ + char str[strlen(arg)+1]; + char *tok, *tmp, *saveptr = NULL; + int i; + + assert(max < INT_MAX); + + strcpy(str, arg); + + tmp = str; + while ((tok = strtok_r(tmp, ",", &saveptr)) != NULL) { + char *a, *b, *endptr, *saveptr2 = NULL; + unsigned long int from, to; + + a = strtok_r(tok, "-", &saveptr2); + if (a == NULL) { + return false; + } else { + endptr = NULL; + from = strtoul(a, &endptr, 0); + if (*endptr != '\0') { + fprintf(stderr, "Invalid value %s\n", a); + return false; + } + if (from >= max) { + fprintf(stderr, "Value %s larger than max %d\n", a, max-1); + return false; + } + } + + b = strtok_r(NULL, "-", &saveptr2); + if (b == NULL) { + to = from; + } else { + endptr = NULL; + to = strtoul(b, &endptr, 0); + if (*endptr != '\0') { + fprintf(stderr, "Invalid value %s\n", b); + return false; + } + if (to >= max) { + fprintf(stderr, "Value %s larger than max %d\n", b, max-1); + return false; + } + } + + if (from > to) { + fprintf(stderr, "Invalid range %s-%s\n", a, b); + return false; + } + + for (i = from; i <= to; i++) + list[i] = 1; + + tmp = NULL; + }; + + if (count != NULL) { + int n = 0; + + for (i = 0; i < max; i++) { + if (list[i] == 1) + n++; + } + + *count = n; + } + + return true; +} + diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..131e3f9 --- /dev/null +++ b/src/util.h @@ -0,0 +1,34 @@ +/* Copyright 2018 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __UTIL_H +#define __UTIL_H + +/** + * @brief Parse a range or a list of numbers from a string into an array + * + * For each number present in the string, set the corresponding list element + * to 1. The list acts as the index flags. The range of valid numbers varies + * from 0 to sizeof(list)-1. + * + * @param[in] arg String to parse + * @param[in] max The size of the list + * @param[in] list The list of flags + * @param[out] count Optional count of distinct numbers found + * @return true on success, false on error + */ +bool parse_list(const char *arg, int max, int *list, int *count); + +#endif