summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2020-11-26 19:31:54 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2020-11-27 10:02:36 +0100
commit8f57bc80c3e6a9f1c5318b52b98e9cc3268d10a8 (patch)
tree4bdd637c26eeacb9928cf36f2e91ca09a5084c6b
parent870f45338872b5ac02b2f87b6409036a6292ecf3 (diff)
downloadbarebox-8f57bc80c3e6a9f1c5318b52b98e9cc3268d10a8.tar.gz
barebox-8f57bc80c3e6a9f1c5318b52b98e9cc3268d10a8.tar.xz
commands: implement and use parse_assignment helper
We have the split by '=' snippet at multiple locations that parse key=value pairs. Consolidate them to a single location. This makes code a bit easier to read at the cost of an extra 8 bytes (LZO-compressed THUMB2 barebox, static inline version is bigger). No functional change. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--commands/export.c4
-rw-r--r--commands/global.c6
-rw-r--r--commands/nv.c9
-rw-r--r--commands/setenv.c10
-rw-r--r--common/bootchooser.c4
-rw-r--r--common/fastboot.c4
-rw-r--r--common/hush.c3
-rw-r--r--include/command.h1
-rw-r--r--include/string.h1
-rw-r--r--lib/string.c11
10 files changed, 24 insertions, 29 deletions
diff --git a/commands/export.c b/commands/export.c
index 8972b7d528..c69f1595c6 100644
--- a/commands/export.c
+++ b/commands/export.c
@@ -20,10 +20,8 @@ static int do_export(int argc, char *argv[])
return COMMAND_ERROR_USAGE;
while (i < argc) {
- if ((ptr = strchr(argv[i], '='))) {
- *ptr++ = 0;
+ if ((ptr = parse_assignment(argv[i])))
setenv(argv[i], ptr);
- }
if (export(argv[i])) {
printf("could not export: %s\n", argv[i]);
return 1;
diff --git a/commands/global.c b/commands/global.c
index 15b6a9f3d3..cf8e9a5b48 100644
--- a/commands/global.c
+++ b/commands/global.c
@@ -37,11 +37,7 @@ static int do_global(int argc, char *argv[])
return COMMAND_ERROR_USAGE;
for (i = 0; i < argc; i++) {
- value = strchr(argv[i], '=');
- if (value) {
- *value = 0;
- value++;
- }
+ value = parse_assignment(argv[i]);
if (do_remove)
globalvar_remove(argv[i]);
diff --git a/commands/nv.c b/commands/nv.c
index a1cff08ee4..c60bb41677 100644
--- a/commands/nv.c
+++ b/commands/nv.c
@@ -55,13 +55,8 @@ static int do_nv(int argc, char *argv[])
for (i = 0; i < argc; i++) {
int ret;
- value = strchr(argv[i], '=');
- if (value) {
- *value = 0;
- value++;
- } else {
- value = "";
- }
+
+ value = parse_assignment(argv[i]) ?: "";
if (do_remove) {
ret = nvvar_remove(argv[i]);
diff --git a/commands/setenv.c b/commands/setenv.c
index 9aeb8f010b..99604c35c3 100644
--- a/commands/setenv.c
+++ b/commands/setenv.c
@@ -9,17 +9,15 @@
static int do_setenv(int argc, char *argv[])
{
- char *equal;
+ char *val;
int ret;
if (argc < 2)
return COMMAND_ERROR_USAGE;
- equal = strrchr(argv[1], '=');
- if (equal) {
- equal[0] = '\0';
- argv[2] = &equal[1];
- }
+ val = parse_assignment(argv[1]);
+ if (val)
+ argv[2] = val;
if (argv[2])
ret = setenv(argv[1], argv[2]);
diff --git a/common/bootchooser.c b/common/bootchooser.c
index 7aa59d8a82..e982c22730 100644
--- a/common/bootchooser.c
+++ b/common/bootchooser.c
@@ -151,14 +151,12 @@ static int pr_setenv(struct bootchooser *bc, const char *fmt, ...)
if (!str)
return -ENOMEM;
- val = strchr(str, '=');
+ val = parse_assignment(str);
if (!val) {
ret = -EINVAL;
goto err;
}
- *val++ = '\0';
-
oldval = getenv(str);
if (!oldval || strcmp(oldval, val)) {
if (bc->state)
diff --git a/common/fastboot.c b/common/fastboot.c
index 1b6dc28d8e..10b4ccf716 100644
--- a/common/fastboot.c
+++ b/common/fastboot.c
@@ -823,14 +823,12 @@ static void cb_oem_setenv(struct fastboot *fb, const char *cmd)
pr_debug("%s: \"%s\"\n", __func__, cmd);
- value = strchr(var, '=');
+ value = parse_assignment(var);
if (!value) {
ret = -EINVAL;
goto out;
}
- *value++ = 0;
-
ret = setenv(var, value);
if (ret)
goto out;
diff --git a/common/hush.c b/common/hush.c
index a6fc4485bf..109bae4d3f 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1117,12 +1117,11 @@ static int set_local_var(const char *s, int flg_export)
/* Assume when we enter this function that we are already in
* NAME=VALUE format. So the first order of business is to
* split 's' on the '=' into 'name' and 'value' */
- value = strchr(name, '=');
+ value = parse_assignment(name);
if (!value) {
free(name);
return -1;
}
- *value++ = 0;
remove_quotes_in_str(value);
diff --git a/include/command.h b/include/command.h
index 860eae3e35..ccae568f87 100644
--- a/include/command.h
+++ b/include/command.h
@@ -13,6 +13,7 @@
#include <linux/list.h>
#include <linux/stringify.h>
#include <linux/stddef.h>
+#include <string.h>
#ifndef __ASSEMBLY__
diff --git a/include/string.h b/include/string.h
index b51566fd00..ef0b5e199e 100644
--- a/include/string.h
+++ b/include/string.h
@@ -14,5 +14,6 @@ void *__nokasan_default_memset(void *, int, __kernel_size_t);
void *__default_memcpy(void * dest,const void *src,size_t count);
void *__nokasan_default_memcpy(void * dest,const void *src,size_t count);
+char *parse_assignment(char *str);
#endif /* __STRING_H */
diff --git a/lib/string.c b/lib/string.c
index b63041c5fb..dbb66fe4d2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -899,3 +899,14 @@ int match_string(const char * const *array, size_t n, const char *string)
return -EINVAL;
}
EXPORT_SYMBOL(match_string);
+
+char *parse_assignment(char *str)
+{
+ char *value;
+
+ value = strchr(str, '=');
+ if (value)
+ *value++ = '\0';
+
+ return value;
+}