[Pdbg] [PATCH v4 03/16] util: Move parse_list() into a separate file

Amitay Isaacs amitay at ozlabs.org
Fri Nov 16 12:14:18 AEDT 2018


... and document the function.

Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
Reviewed-by: Alistair Popple <alistair at popple.id.au>
---
 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 b8f38ca..c8f100e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -85,7 +85,9 @@ pdbg_SOURCES = \
 	src/reg.c \
 	src/ring.c \
 	src/scom.c \
-	src/thread.c
+	src/thread.c \
+	src/util.c \
+	src/util.h
 
 if TARGET_ARM
 pdbg_SOURCES += src/options_arm.c
diff --git a/src/main.c b/src/main.c
index 6ad869d..fd77f6d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -39,6 +39,7 @@
 #include "optcmd.h"
 #include "progress.h"
 #include "pdbgproxy.h"
+#include "util.h"
 
 #define PR_ERROR(x, args...) \
 	pdbg_log(PDBG_ERROR, x, ##args)
@@ -180,79 +181,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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <limits.h>
+#include <assert.h>
+
+/* 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
-- 
2.19.1



More information about the Pdbg mailing list