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 v2 3/3] commands: add varinfo command
Date: Thu, 20 Mar 2025 06:20:59 +0100	[thread overview]
Message-ID: <20250320052059.1732184-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052059.1732184-1-a.fatoum@pengutronix.de>

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>
---
v1 -> v2:
  - lookup shell environment variables by prefix as well (Sascha)
  - completio without trailing equal sign (Sascha)
---
 commands/Kconfig   | 12 ++++++
 commands/Makefile  |  1 +
 commands/varinfo.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++
 common/complete.c  |  6 +++
 include/complete.h |  1 +
 5 files changed, 111 insertions(+)
 create mode 100644 commands/varinfo.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 8c05b8eff606..4cce4b24cc98 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 36ef59b38759..f97b77c7cc16 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -59,6 +59,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..5965c60159ca
--- /dev/null
+++ b/commands/varinfo.c
@@ -0,0 +1,91 @@
+// 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>
+
+struct iter_ctx {
+	const char *prefix;
+	bool found;
+};
+
+static int printenv_by_prefix(struct variable_d *v, void *data)
+{
+	struct iter_ctx *ctx = data;
+
+	if (strstarts(var_name(v), ctx->prefix)) {
+		printf("%s: %s (environment variable)\n", var_name(v), var_val(v));
+		ctx->found = true;
+	}
+
+	return 0;
+}
+
+static int do_varinfo(int argc, char *argv[])
+{
+	struct device *dev;
+	struct param_d *param;
+	const char *prefix = NULL;
+	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 {
+		struct iter_ctx ctx = { arg };
+
+		envvar_for_each(printenv_by_prefix, &ctx);
+		if (!ctx.found)
+			goto not_found;
+
+		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_plain_complete)
+BAREBOX_CMD_END
diff --git a/common/complete.c b/common/complete.c
index 6061c8ba6ecb..6cb674a0cc9c 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -366,6 +366,12 @@ int env_param_noeval_complete(struct string_list *sl, char *instr)
 }
 EXPORT_SYMBOL(env_param_noeval_complete);
 
+int env_param_plain_complete(struct string_list *sl, char *instr)
+{
+	return env_param_complete(sl, instr, 0);
+}
+EXPORT_SYMBOL(env_param_plain_complete);
+
 static int tab_pressed = 0;
 
 void complete_reset(void)
diff --git a/include/complete.h b/include/complete.h
index 2068760ac235..21d3c0430f64 100644
--- a/include/complete.h
+++ b/include/complete.h
@@ -24,6 +24,7 @@ int devicetree_nodepath_complete(struct string_list *sl, char *instr);
 int devicetree_complete(struct string_list *sl, char *instr);
 int devicetree_file_complete(struct string_list *sl, char *instr);
 int env_param_noeval_complete(struct string_list *sl, char *instr);
+int env_param_plain_complete(struct string_list *sl, char *instr);
 int tutorial_complete(struct string_list *sl, char *instr);
 
 #endif /* __COMPLETE_ */
-- 
2.39.5




      parent reply	other threads:[~2025-03-20  5:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20  5:20 [PATCH v2 1/3] complete: give device_param_complete a flags parameter Ahmad Fatoum
2025-03-20  5:20 ` [PATCH v2 2/3] env: add envvar_for_each helper for variable iteration Ahmad Fatoum
2025-03-20  5:20 ` Ahmad Fatoum [this message]

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=20250320052059.1732184-3-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