mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/3] complete: give device_param_complete a flags parameter
@ 2025-03-20  5:20 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 ` [PATCH v2 3/3] commands: add varinfo command Ahmad Fatoum
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2025-03-20  5:20 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We currently always complete device parameters with either a dollar sign
in front or an equal sign after. In preparation for allowing completions
with neither, give env_param_complete() a flag.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 common/complete.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/common/complete.c b/common/complete.c
index 5b8b499ed38f..6061c8ba6ecb 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -9,11 +9,15 @@
 #include <complete.h>
 #include <xfuncs.h>
 #include <fs.h>
+#include <linux/bits.h>
 #include <linux/stat.h>
 #include <libgen.h>
 #include <command.h>
 #include <environment.h>
 
+#define DEVPARAM_COMPLETE_ASSIGNMENT	BIT(0)
+#define DEVPARAM_COMPLETE_DOLLAR	BIT(1)
+
 static bool is_valid_escape(const char *str)
 {
 	return str[0] == '\\' && (str[1] == ' ' || str[1] == '\\');
@@ -170,7 +174,8 @@ int device_complete(struct string_list *sl, char *instr)
 EXPORT_SYMBOL(device_complete);
 
 static int device_param_complete(struct device *dev, const char *devname,
-				 struct string_list *sl, char *instr, int eval)
+				 struct string_list *sl, char *instr,
+				 unsigned flags)
 {
 	struct param_d *param;
 
@@ -179,8 +184,8 @@ static int device_param_complete(struct device *dev, const char *devname,
 			continue;
 
 		string_list_add_asprintf(sl, "%s%s.%s%c",
-			eval ? "$" : "", devname, param->name,
-			eval ? ' ' : '=');
+			flags & DEVPARAM_COMPLETE_DOLLAR ? "$" : "", devname, param->name,
+			flags & DEVPARAM_COMPLETE_ASSIGNMENT ? '=' : ' ');
 	}
 
 	return 0;
@@ -292,21 +297,22 @@ int tutorial_complete(struct string_list *sl, char *instr)
 }
 EXPORT_SYMBOL(tutorial_complete);
 
-static int env_param_complete(struct string_list *sl, char *instr, int eval)
+static int env_param_complete(struct string_list *sl, char *instr, unsigned flags)
 {
 	struct device *dev;
 	struct variable_d *var;
 	struct env_context *c;
 	int len;
-	char end = '=', *pos, *dot;
+	char end = ' ', *pos, *dot;
 	char *begin = "";
 
 	if (!instr)
 		instr = "";
 
-	if (eval) {
+	if (flags & DEVPARAM_COMPLETE_DOLLAR) {
 		begin = "$";
-		end = ' ';
+	} else if (flags & DEVPARAM_COMPLETE_ASSIGNMENT) {
+		end = '=';
 	}
 
 	len = strlen(instr);
@@ -338,7 +344,7 @@ static int env_param_complete(struct string_list *sl, char *instr, int eval)
 		dev = get_device_by_name(devname);
 
 		if (dev)
-			device_param_complete(dev, devname, sl, dot + 1, eval);
+			device_param_complete(dev, devname, sl, dot + 1, flags);
 
 		free(devname);
 		pos = dot + 1;
@@ -348,7 +354,7 @@ static int env_param_complete(struct string_list *sl, char *instr, int eval)
 
 	for_each_device(dev) {
 		if (!strncmp(instr, dev_name(dev), len))
-			device_param_complete(dev, dev_name(dev), sl, "", eval);
+			device_param_complete(dev, dev_name(dev), sl, "", flags);
 	}
 
 	return 0;
@@ -356,7 +362,7 @@ static int env_param_complete(struct string_list *sl, char *instr, int eval)
 
 int env_param_noeval_complete(struct string_list *sl, char *instr)
 {
-	return env_param_complete(sl, instr, 0);
+	return env_param_complete(sl, instr, DEVPARAM_COMPLETE_ASSIGNMENT);
 }
 EXPORT_SYMBOL(env_param_noeval_complete);
 
@@ -415,7 +421,7 @@ static char* cmd_complete_lookup(struct string_list *sl, char *instr)
 
 end:
 	if (ret == COMPLETE_CONTINUE && *instr == '$')
-		env_param_complete(sl, instr + 1, 1);
+		env_param_complete(sl, instr + 1, DEVPARAM_COMPLETE_DOLLAR);
 
 	return res;
 }
@@ -460,7 +466,7 @@ int complete(char *instr, char **outstr)
 			env_param_complete(&sl, instr, 0);
 		}
 		if (*instr == '$')
-			env_param_complete(&sl, instr + 1, 1);
+			env_param_complete(&sl, instr + 1, DEVPARAM_COMPLETE_DOLLAR);
 	}
 
 	pos = strlen_escaped(instr);
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/3] env: add envvar_for_each helper for variable iteration
  2025-03-20  5:20 [PATCH v2 1/3] complete: give device_param_complete a flags parameter Ahmad Fatoum
@ 2025-03-20  5:20 ` Ahmad Fatoum
  2025-03-20  5:20 ` [PATCH v2 3/3] commands: add varinfo command Ahmad Fatoum
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2025-03-20  5:20 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Instead of opencoding the iteration where needed, implement a helper
invokes a callback on every variable.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 common/env.c          | 37 +++++++++++++++++++++++++++++++++++++
 include/environment.h |  7 +++++++
 2 files changed, 44 insertions(+)

diff --git a/common/env.c b/common/env.c
index e34e3ac078d6..fd01178aee54 100644
--- a/common/env.c
+++ b/common/env.c
@@ -123,6 +123,21 @@ static const char *getenv_raw(struct list_head *l, const char *name)
 	return NULL;
 }
 
+static int getenv_raw_call(int (*fn)(struct variable_d *v, void *data),
+			   struct list_head *l, void *data)
+{
+	struct variable_d *v;
+	int ret;
+
+	list_for_each_entry(v, l, list) {
+		ret = fn(v, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static const char *dev_getenv(const char *name)
 {
 	const char *pos, *val, *dot, *varname;
@@ -179,6 +194,28 @@ const char *getenv(const char *name)
 }
 EXPORT_SYMBOL(getenv);
 
+int envvar_for_each(int (*fn)(struct variable_d *v, void *data), void *data)
+{
+	struct env_context *c;
+	int ret;
+
+	c = context;
+
+	ret = getenv_raw_call(fn, &c->local, data);
+	if (ret)
+		return ret;
+
+	while (c) {
+		ret = getenv_raw_call(fn, &c->global, data);
+		if (ret)
+			return ret;
+		c = c->parent;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(envvar_for_each);
+
 static int setenv_raw(struct list_head *l, const char *name, const char *value)
 {
 	struct variable_d *v;
diff --git a/include/environment.h b/include/environment.h
index f26c68ba3e34..c24796427675 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -31,6 +31,7 @@ const char *var_name(struct variable_d *);
 
 #if IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES) && IN_PROPER
 const char *getenv(const char *);
+int envvar_for_each(int (*fn)(struct variable_d *v, void *data), void *data);
 int setenv(const char *, const char *);
 int pr_setenv(const char *, const char *fmt, ...)  __attribute__ ((format(__printf__, 2, 3)));
 void export_env_ull(const char *name, unsigned long long val);
@@ -46,6 +47,12 @@ static inline char *getenv(const char *var)
 	return NULL;
 }
 
+static inline int envvar_for_each(int (*fn)(struct list_head *l, void *data),
+				  void *data)
+{
+	return 0;
+}
+
 static inline int setenv(const char *var, const char *val)
 {
 	return 0;
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 3/3] commands: add varinfo command
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2025-03-20  5:20 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-03-20  5:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v2 3/3] commands: add varinfo command Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox