mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] commands: add varinfo command
Date: Thu, 16 Jan 2025 15:07:40 +0100	[thread overview]
Message-ID: <20250116140740.297902-1-a.fatoum@pengutronix.de> (raw)

Tab completion is very helpful, but it's not sufficient for enum-typed
global variables. To know what the permissible values are, devinfo
global must be consulted, which can have a lot of output.

As alternative, let's add a varinfo command that can be used together
with parameter tab completion and that will just print out the
parameters that start with the specified string.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig   | 12 ++++++++
 commands/Makefile  |  1 +
 commands/varinfo.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+)
 create mode 100644 commands/varinfo.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 7aa4101f06c0..448a6e240cad 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -286,6 +286,18 @@ config CMD_LSPCI
 	help
 	  The lspci command allows to list all PCI devices.
 
+config CMD_VARINFO
+	tristate
+	prompt "varinfo"
+	help
+	  Show information about variables
+
+	  varinfo VAR
+
+	  Shows information about the variable in its argument. This doesn't
+	  provide more information that what's available with devinfo, but
+	  it can be useful for devices with many parameters (e.g. global).
+
 config CMD_VERSION
 	tristate
 	default y
diff --git a/commands/Makefile b/commands/Makefile
index 0dfa0a33a465..7ea0ab4d87f7 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -57,6 +57,7 @@ obj-$(CONFIG_CMD_MEMTEST)	+= memtest.o
 obj-$(CONFIG_CMD_MEMTESTER)	+= memtester/
 obj-$(CONFIG_CMD_TRUE)		+= true.o
 obj-$(CONFIG_CMD_FALSE)		+= false.o
+obj-$(CONFIG_CMD_VARINFO)	+= varinfo.o
 obj-$(CONFIG_CMD_VERSION)	+= version.o
 obj-$(CONFIG_CMD_HELP)		+= help.o
 obj-$(CONFIG_CMD_LSMOD)		+= lsmod.o
diff --git a/commands/varinfo.c b/commands/varinfo.c
new file mode 100644
index 000000000000..2867c47363ed
--- /dev/null
+++ b/commands/varinfo.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <command.h>
+#include <common.h>
+#include <complete.h>
+#include <driver.h>
+#include <environment.h>
+#include <fnmatch.h>
+
+static int do_varinfo(int argc, char *argv[])
+{
+	struct device *dev;
+	struct param_d *param;
+	const char *prefix = NULL, *val;
+	char *arg, *dot;
+	bool found = false;
+
+	if (argc != 2)
+		return COMMAND_ERROR_USAGE;
+
+	arg = argv[1];
+
+	dot = strchr(arg, '.');
+	if (dot) {
+		*dot = '\0';
+		if (dot[1])
+			prefix = &dot[1];
+	} else {
+		val = getenv(arg);
+		if (!val)
+			goto not_found;
+
+		printf("%s: %s (environment variable)\n", arg, val);
+		return 0;
+	}
+
+	dev = get_device_by_name(arg);
+	if (!dev)
+		return -ENODEV;
+
+	list_for_each_entry(param, &dev->parameters, list) {
+		if (prefix && !strstarts(param->name, prefix))
+			continue;
+
+		printf("%s: %s (type: %s)", param->name,
+		       dev_get_param(dev, param->name), get_param_type(param));
+		if (param->info)
+			param->info(param);
+		printf("\n");
+		found = true;
+	}
+
+	if (!found)
+		goto not_found;
+
+	return 0;
+not_found:
+	printf("%s: no matching variable found\n", arg);
+	return 1;
+}
+
+BAREBOX_CMD_HELP_START(varinfo)
+BAREBOX_CMD_HELP_TEXT("shows information about the variable in its argument")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(varinfo)
+	.cmd		= do_varinfo,
+	BAREBOX_CMD_DESC("show information about variables")
+	BAREBOX_CMD_OPTS("VAR")
+	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
+	BAREBOX_CMD_HELP(cmd_varinfo_help)
+	BAREBOX_CMD_COMPLETE(env_param_noeval_complete)
+BAREBOX_CMD_END
-- 
2.39.5




             reply	other threads:[~2025-01-16 14:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-16 14:07 Ahmad Fatoum [this message]
2025-01-21  7:27 ` Sascha Hauer

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=20250116140740.297902-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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