[03/10] util: Move parse_list() into a separate file

Message ID 20181002060430.3344784-4-amitay@ozlabs.org
State New
Headers show
Series
  • Device tree path base targeting
Related show

Commit Message

Amitay Isaacs Oct. 2, 2018, 6:04 a.m.
... and document the function.

Signed-off-by: Amitay Isaacs <amitay@ozlabs.org>
---
 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

Patch

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 <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