mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/3] bootchooser: rename pr_setenv() to bc_setenv()
@ 2022-06-20  9:39 Sascha Hauer
  2022-06-20  9:39 ` [PATCH v2 2/3] env: let setenv() take printf arguments Sascha Hauer
  2022-06-20  9:39 ` [PATCH v2 3/3] treewide: Simplify setenv() calls Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-06-20  9:39 UTC (permalink / raw)
  To: Barebox List

We are about to introduce a generic function names pr_setenv(), so
rename the existent bootchooser specific function to bc_setenv().
For consistency, rename pr_getenv() accordingly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/bootchooser.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/common/bootchooser.c b/common/bootchooser.c
index 75dfbc6166..eb3dda52ab 100644
--- a/common/bootchooser.c
+++ b/common/bootchooser.c
@@ -128,7 +128,7 @@ static void pr_target(struct bootchooser_target *target)
 		printf("    disabled due to %s\n", reason);
 }
 
-static int pr_setenv(struct bootchooser *bc, const char *fmt, ...)
+static int bc_setenv(struct bootchooser *bc, const char *fmt, ...)
 {
 	va_list ap;
 	int ret = 0;
@@ -162,7 +162,7 @@ err:
 	return ret;
 }
 
-static const char *pr_getenv(const char *fmt, ...)
+static const char *bc_getenv(const char *fmt, ...)
 {
 	va_list ap;
 	char *str;
@@ -258,7 +258,7 @@ static struct bootchooser_target *bootchooser_target_new(struct bootchooser *bc,
 		target->remaining_attempts = 0;
 	}
 
-	val = pr_getenv("%s.boot", target->prefix);
+	val = bc_getenv("%s.boot", target->prefix);
 	if (!val)
 		val = target->name;
 	target->boot = xstrdup(val);
@@ -497,17 +497,17 @@ int bootchooser_save(struct bootchooser *bc)
 	int ret;
 
 	if (bc->last_chosen)
-		pr_setenv(bc, "%s.last_chosen=%d", bc->state_prefix,
+		bc_setenv(bc, "%s.last_chosen=%d", bc->state_prefix,
 			  bc->last_chosen->id);
 
 	list_for_each_entry(target, &bc->targets, list) {
-		ret = pr_setenv(bc, "%s.remaining_attempts=%d",
+		ret = bc_setenv(bc, "%s.remaining_attempts=%d",
 				target->state_prefix,
 				target->remaining_attempts);
 		if (ret)
 			return ret;
 
-		ret = pr_setenv(bc, "%s.priority=%d",
+		ret = bc_setenv(bc, "%s.priority=%d",
 				target->state_prefix, target->priority);
 		if (ret)
 			return ret;
-- 
2.30.2




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

* [PATCH v2 2/3] env: let setenv() take printf arguments
  2022-06-20  9:39 [PATCH v2 1/3] bootchooser: rename pr_setenv() to bc_setenv() Sascha Hauer
@ 2022-06-20  9:39 ` Sascha Hauer
  2022-06-20  9:39 ` [PATCH v2 3/3] treewide: Simplify setenv() calls Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-06-20  9:39 UTC (permalink / raw)
  To: Barebox List

It's a common pattern to (ba)sprintf to a string and then call setenv()
with this string. Introduce pr_setenv() as a shortcut to simplify this
pattern.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/env.c          | 32 ++++++++++++++++++++++++++++++--
 include/environment.h |  7 +++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/common/env.c b/common/env.c
index 05add63f62..141cd66046 100644
--- a/common/env.c
+++ b/common/env.c
@@ -244,13 +244,12 @@ static int dev_setenv(const char *name, const char *val)
 
 /**
  * setenv - set environment variables
- * @_name - Variable name
+ * @name - Variable name
  * @value - the value to set, empty string not handled specially
  *
  * Returns 0 for success and a negative error code otherwise
  * Use unsetenv() to unset.
  */
-
 int setenv(const char *_name, const char *value)
 {
 	char *name = strdup(_name);
@@ -277,6 +276,35 @@ out:
 }
 EXPORT_SYMBOL(setenv);
 
+/**
+ * pr_setenv - set environment variables
+ * @name - Variable name
+ * @fmt - the format string to use
+ *
+ * Returns 0 for success and a negative error code otherwise
+ * Use unsetenv() to unset.
+ */
+int pr_setenv(const char *name, const char *fmt, ...)
+{
+	va_list ap;
+	int ret = 0;
+	char *value;
+	int len;
+
+	va_start(ap, fmt);
+	len = vasprintf(&value, fmt, ap);
+	va_end(ap);
+
+	if (len < 0)
+		return -ENOMEM;
+
+	ret = setenv(name, value);
+	free(value);
+
+	return ret;
+}
+EXPORT_SYMBOL(setenv);
+
 int export(const char *varname)
 {
 	const char *val = getenv_raw(&context->local, varname);
diff --git a/include/environment.h b/include/environment.h
index 19e522cfb6..1557c3a1d7 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -32,6 +32,7 @@ char *var_name(struct variable_d *);
 #ifdef CONFIG_ENVIRONMENT_VARIABLES
 const char *getenv(const char *);
 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);
 int getenv_ull(const char *name, unsigned long long *val);
 int getenv_ul(const char *name, unsigned long *val);
@@ -49,6 +50,12 @@ static inline int setenv(const char *var, const char *val)
 	return 0;
 }
 
+static inline __attribute__ ((format(__printf__, 2, 3))) int pr_setenv(
+	const char *var, const char *fmt, ...)
+{
+	return 0;
+}
+
 static inline void export_env_ull(const char *name, unsigned long long val) {}
 
 static inline int getenv_ull(const char *name, unsigned long long *val)
-- 
2.30.2




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

* [PATCH v2 3/3] treewide: Simplify setenv() calls
  2022-06-20  9:39 [PATCH v2 1/3] bootchooser: rename pr_setenv() to bc_setenv() Sascha Hauer
  2022-06-20  9:39 ` [PATCH v2 2/3] env: let setenv() take printf arguments Sascha Hauer
@ 2022-06-20  9:39 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2022-06-20  9:39 UTC (permalink / raw)
  To: Barebox List

We now have pr_setenv() which is a setenv() variant that takes a
format string. Use it where appropriate.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/clk.c      | 10 +++-------
 commands/crc.c      | 14 ++++----------
 commands/hwclock.c  |  4 +---
 commands/loadb.c    |  4 +---
 commands/loads.c    |  4 +---
 common/bootsource.c |  8 ++------
 common/menutree.c   |  9 +--------
 7 files changed, 13 insertions(+), 40 deletions(-)

diff --git a/commands/clk.c b/commands/clk.c
index dfbc7c988f..b1741b9da4 100644
--- a/commands/clk.c
+++ b/commands/clk.c
@@ -139,13 +139,9 @@ static int do_clk_get_rate(int argc, char *argv[])
 
 	rate = clk_get_rate(clk);
 
-	if (variable_name) {
-		char *t;
-
-		t = basprintf("%lu", rate);
-		setenv(variable_name, t);
-		free(t);
-	} else
+	if (variable_name)
+		pr_setenv(variable_name, "%lu", rate);
+	else
 		printf("%lu\n", rate);
 
 	return COMMAND_SUCCESS;
diff --git a/commands/crc.c b/commands/crc.c
index 80ecf7fe29..23ffd4360b 100644
--- a/commands/crc.c
+++ b/commands/crc.c
@@ -83,17 +83,11 @@ static int do_crc(int argc, char *argv[])
 	printf("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
 			filename, (ulong)start, (ulong)start + total - 1, crc);
 
-	if (crcvarname) {
-		char *crcstr = basprintf("0x%lx", crc);
-		setenv(crcvarname, crcstr);
-		kfree(crcstr);
-	}
+	if (crcvarname)
+		pr_setenv(crcvarname, "0x%lx", crc);
 
-	if (sizevarname) {
-		char *sizestr = basprintf("0x%lx", total);
-		setenv(sizevarname, sizestr);
-		kfree(sizestr);
-	}
+	if (sizevarname)
+		pr_setenv(sizevarname, "0x%lx", total);
 
 #ifdef CONFIG_CMD_CRC_CMP
 	if (vfilename) {
diff --git a/commands/hwclock.c b/commands/hwclock.c
index abb0500e6a..c594e070ac 100644
--- a/commands/hwclock.c
+++ b/commands/hwclock.c
@@ -153,11 +153,9 @@ static int do_hwclock(int argc, char *argv[])
 
 	if (env_name) {
 		unsigned long time;
-		char t[12];
 
 		rtc_tm_to_time(&tm, &time);
-		snprintf(t, 12, "%lu", time);
-		setenv(env_name, t);
+		pr_setenv(env_name, "%lu", time);
 	} else {
 		printf("%s\n", time_str(&tm));
 	}
diff --git a/commands/loadb.c b/commands/loadb.c
index 17d3af84b5..7ab989f459 100644
--- a/commands/loadb.c
+++ b/commands/loadb.c
@@ -542,7 +542,6 @@ packet_error:
 static ulong load_serial_bin(void)
 {
 	int size, i;
-	char buf[32];
 
 	/* Try to allocate the buffer we shall write to files */
 	write_buffer = malloc(MAX_WRITE_BUFFER);
@@ -576,8 +575,7 @@ static ulong load_serial_bin(void)
 		write_idx = 0;
 	}
 	printf("## Total Size      = 0x%08x = %d Bytes\n", size, size);
-	sprintf(buf, "%X", size);
-	setenv("filesize", buf);
+	pr_setenv("filesize", "%X", size);
 
 err_quit:
 	free(write_buffer);
diff --git a/commands/loads.c b/commands/loads.c
index 8260673c51..7c5df31251 100644
--- a/commands/loads.c
+++ b/commands/loads.c
@@ -65,7 +65,6 @@ static ulong load_serial(ulong offset)
 	int	type;				/* return code for record type	*/
 	ulong	addr;				/* load address from S-Record	*/
 	ulong	size;				/* number of bytes transferred	*/
-	char	buf[32];
 	ulong	store_addr;
 	ulong	start_addr = ~0;
 	ulong	end_addr   =  0;
@@ -100,8 +99,7 @@ static ulong load_serial(ulong offset)
 			    "## Total Size      = 0x%08lX = %ld Bytes\n",
 			    start_addr, end_addr, size, size
 			    );
-			sprintf(buf, "%lX", size);
-			setenv("filesize", buf);
+			pr_setenv("filesize", "%lX", size);
 			return addr;
 		case SREC_START:
 			break;
diff --git a/common/bootsource.c b/common/bootsource.c
index 1f8d053a81..79dacfd1d0 100644
--- a/common/bootsource.c
+++ b/common/bootsource.c
@@ -113,16 +113,12 @@ void bootsource_set(enum bootsource src)
 
 void bootsource_set_instance(int instance)
 {
-	char buf[32];
-
 	bootsource_instance = instance;
 
 	if (instance < 0)
-		sprintf(buf, "unknown");
+		setenv("bootsource_instance","unknown");
 	else
-		snprintf(buf, sizeof(buf), "%d", instance);
-
-	setenv("bootsource_instance", buf);
+		pr_setenv("bootsource_instance", "%d", instance);
 }
 
 enum bootsource bootsource_get(void)
diff --git a/common/menutree.c b/common/menutree.c
index 7fa835a7fe..751350d754 100644
--- a/common/menutree.c
+++ b/common/menutree.c
@@ -34,14 +34,7 @@ static void menutree_action(struct menu *m, struct menu_entry *me)
 
 static void setenv_bool(const char *var, bool val)
 {
-	const char *str;
-
-	if (val)
-		str = "1";
-	else
-		str = "0";
-
-	setenv(var, str);
+	pr_setenv(var, "%d", val);
 }
 
 static void menutree_box(struct menu *m, struct menu_entry *me)
-- 
2.30.2




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

end of thread, other threads:[~2022-06-20  9:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20  9:39 [PATCH v2 1/3] bootchooser: rename pr_setenv() to bc_setenv() Sascha Hauer
2022-06-20  9:39 ` [PATCH v2 2/3] env: let setenv() take printf arguments Sascha Hauer
2022-06-20  9:39 ` [PATCH v2 3/3] treewide: Simplify setenv() calls Sascha Hauer

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