Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
To: Linus Walleij <linus.walleij@linaro.org>,
	Kent Gibson <warthog618@gmail.com>,
	Vincent Fazio <vfazio@xes-inc.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: linux-gpio@vger.kernel.org, brgl@kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH RFC libgpiod 1/3] tools: factor out line value parsing into tools-common
Date: Fri, 16 Oct 2026 16:18:08 +0200	[thread overview]
Message-ID: <20261016-gpioctl-v1-1-8770622679b8@oss.qualcomm.com> (raw)
In-Reply-To: <20261016-gpioctl-v1-0-8770622679b8@oss.qualcomm.com>

Move functions for parsing of line value arguments to tools-common so
that both gpioset and the upcoming gpioctl can share them.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 tools/gpioset.c      | 91 +---------------------------------------------------
 tools/tools-common.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
 tools/tools-common.h |  5 +++
 3 files changed, 84 insertions(+), 90 deletions(-)

diff --git a/tools/gpioset.c b/tools/gpioset.c
index fce4a7de222f93121c99843a761c5308e021343a..95e99fe6543312ccbece1ec3b0b37ae5d4878790 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -227,95 +227,6 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 	return optind;
 }
 
-static enum gpiod_line_value parse_value(const char *option)
-{
-	if (strcmp(option, "0") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "1") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "inactive") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "active") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "off") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "on") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-	if (strcmp(option, "false") == 0)
-		return GPIOD_LINE_VALUE_INACTIVE;
-	if (strcmp(option, "true") == 0)
-		return GPIOD_LINE_VALUE_ACTIVE;
-
-	return GPIOD_LINE_VALUE_ERROR;
-}
-
-/*
- * Parse line id and values from lvs into lines and values.
- *
- * Accepted forms:
- *     'line=value'
- *     '"line"=value'
- *
- * If line id is quoted then it is returned unquoted.
- */
-static bool parse_line_values(int num_lines, char **lvs, char **lines,
-			      enum gpiod_line_value *values, bool interactive)
-{
-	char *value, *line;
-	int i;
-
-	for (i = 0; i < num_lines; i++) {
-		line = lvs[i];
-
-		if (*line != '"') {
-			value = strchr(line, '=');
-		} else {
-			line++;
-			value = strstr(line, "\"=");
-			if (value) {
-				*value = '\0';
-				value++;
-			}
-		}
-
-		if (!value) {
-			if (interactive)
-				printf("invalid line value: '%s'\n", lvs[i]);
-			else
-				print_error("invalid line value: '%s'", lvs[i]);
-
-			return false;
-		}
-
-		*value = '\0';
-		value++;
-		values[i] = parse_value(value);
-
-		if (values[i] == GPIOD_LINE_VALUE_ERROR) {
-			if (interactive)
-				printf("invalid line value: '%s'\n", value);
-			else
-				print_error("invalid line value: '%s'", value);
-
-			return false;
-		}
-
-		lines[i] = line;
-	}
-
-	return true;
-}
-
-/*
- * Parse line id and values from lvs into lines and values, or die trying.
- */
-static void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
-				     enum gpiod_line_value *values)
-{
-	if (!parse_line_values(num_lines, lvs, lines, values, false))
-		exit(EXIT_FAILURE);
-}
-
 static void print_banner(int num_lines, char **lines)
 {
 	int i;
@@ -794,7 +705,7 @@ static void interact(struct gpiod_line_request **requests,
 			if (num_lines == 0)
 				printf("at least one GPIO line value must be specified\n");
 			else if (parse_line_values(num_lines, &words[1], lines,
-						   values, true) &&
+						   values) &&
 				 valid_lines(resolver, num_lines, lines)) {
 				set_line_values_subset(resolver, num_lines,
 						       lines, values);
diff --git a/tools/tools-common.c b/tools/tools-common.c
index 4482ea5d6a4c6ef3cad3af2fb960d230e1a542a0..6ac9517821f81ee8592493ed37237ef795c8f1a6 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -115,6 +115,84 @@ int parse_bias_or_die(const char *option)
 	return GPIOD_LINE_BIAS_DISABLED;
 }
 
+static int parse_line_value(const char *option)
+{
+	if (strcmp(option, "0") == 0 || strcmp(option, "inactive") == 0 ||
+	    strcmp(option, "off") == 0 || strcmp(option, "false") == 0)
+		return GPIOD_LINE_VALUE_INACTIVE;
+	if (strcmp(option, "1") == 0 || strcmp(option, "active") == 0 ||
+	    strcmp(option, "on") == 0 || strcmp(option, "true") == 0)
+		return GPIOD_LINE_VALUE_ACTIVE;
+
+	return GPIOD_LINE_VALUE_ERROR;
+}
+
+int parse_line_value_or_die(const char *option)
+{
+	int val = parse_line_value(option);
+
+	if (val == GPIOD_LINE_VALUE_ERROR)
+		die("invalid line value: '%s'", option);
+
+	return val;
+}
+
+/*
+ * Parse line id and value pairs from lvs into lines and values.
+ *
+ * Accepted forms:
+ *     'line=value'
+ *     '"line"=value'
+ *
+ * If line id is quoted it is returned unquoted.
+ */
+bool parse_line_values(int num_lines, char **lvs, char **lines,
+		       enum gpiod_line_value *values)
+{
+	char *value, *line;
+	int i;
+
+	for (i = 0; i < num_lines; i++) {
+		line = lvs[i];
+
+		if (*line != '"') {
+			value = strchr(line, '=');
+		} else {
+			line++;
+			value = strstr(line, "\"=");
+			if (value) {
+				*value = '\0';
+				value++;
+			}
+		}
+
+		if (!value) {
+			print_error("invalid line value: '%s'", lvs[i]);
+			return false;
+		}
+
+		*value = '\0';
+		value++;
+		values[i] = parse_line_value(value);
+
+		if (values[i] == GPIOD_LINE_VALUE_ERROR) {
+			print_error("invalid line value: '%s'", value);
+			return false;
+		}
+
+		lines[i] = line;
+	}
+
+	return true;
+}
+
+void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
+			      enum gpiod_line_value *values)
+{
+	if (!parse_line_values(num_lines, lvs, lines, values))
+		exit(EXIT_FAILURE);
+}
+
 long long parse_period(const char *option)
 {
 	unsigned long long p, m = 0;
diff --git a/tools/tools-common.h b/tools/tools-common.h
index 85ee765255bbba991cd2e1fbe6b727a9e998d47e..5083b0908584f98a00321ca3fadf46f55c0a3066 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -35,6 +35,11 @@ void die_oom(void) NORETURN;
 void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void print_version(void);
 int parse_bias_or_die(const char *option);
+int parse_line_value_or_die(const char *option);
+bool parse_line_values(int num_lines, char **lvs, char **lines,
+		       enum gpiod_line_value *values);
+void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
+			      enum gpiod_line_value *values);
 long long parse_period(const char *option);
 unsigned long long parse_period_or_die(const char *option);
 void sleep_us(unsigned long long period);

-- 
2.47.3


  reply	other threads:[~2026-10-16 14:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 14:18 [PATCH RFC libgpiod 0/3] tools: new tool - gpioctl Bartosz Golaszewski
2026-10-16 14:18 ` Bartosz Golaszewski [this message]
2026-10-16 14:18 ` [PATCH RFC libgpiod 2/3] tools: factor out line event formatting into tools-common Bartosz Golaszewski
2026-10-19 11:13   ` Andy Shevchenko
2026-10-21 10:35     ` Bartosz Golaszewski
2026-10-16 14:18 ` [PATCH RFC libgpiod 3/3] tools: implement gpioctl Bartosz Golaszewski
2026-10-16 18:00   ` Linus Walleij
2026-10-19  7:50     ` Bartosz Golaszewski
2026-10-19 11:15     ` Andy Shevchenko
2026-10-19 13:37   ` Vincent Fazio
2026-10-19 13:58     ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261016-gpioctl-v1-1-8770622679b8@oss.qualcomm.com \
    --to=bartosz.golaszewski@oss.qualcomm.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=vfazio@xes-inc.com \
    --cc=warthog618@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox