summaryrefslogtreecommitdiffstats
path: root/common/globalvar.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-04-29 20:21:16 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2012-05-14 08:57:01 +0200
commitb8c94a15611ce7e191d022a3cd9f3ba23930a9c8 (patch)
treef0daa10332462b729219562cfca592c0f9268770 /common/globalvar.c
parent9b42dfff0f2cecb6a9c7581c4c0c59ed9c9c586d (diff)
downloadbarebox-b8c94a15611ce7e191d022a3cd9f3ba23930a9c8.tar.gz
barebox-b8c94a15611ce7e191d022a3cd9f3ba23930a9c8.tar.xz
add 'global' command
This implements global shell variable support. This is done by registering a new device named 'global', so global variables are just plain device parameters. Global variables are useful for storing the global state in the environment. Currently we do this by sourcing scripts instead of executing them which is quite limiting. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/globalvar.c')
-rw-r--r--common/globalvar.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/common/globalvar.c b/common/globalvar.c
new file mode 100644
index 0000000000..71296ff5a3
--- /dev/null
+++ b/common/globalvar.c
@@ -0,0 +1,65 @@
+#include <common.h>
+#include <malloc.h>
+#include <globalvar.h>
+#include <init.h>
+
+static struct device_d global_device = {
+ .name = "global",
+ .id = DEVICE_ID_SINGLE,
+};
+
+int globalvar_add(const char *name,
+ int (*set)(struct device_d *dev, struct param_d *p, const char *val),
+ const char *(*get)(struct device_d *, struct param_d *p),
+ unsigned long flags)
+{
+ return dev_add_param(&global_device, name, set, get, flags);
+}
+
+/*
+ * globalvar_get_match
+ *
+ * get a concatenated string of all globalvars beginning with 'match'.
+ * This adds whitespaces between the different globalvars
+ */
+char *globalvar_get_match(const char *match, const char *seperator)
+{
+ char *val = NULL;
+ struct param_d *param;
+
+ list_for_each_entry(param, &global_device.parameters, list) {
+ if (!strncmp(match, param->name, strlen(match))) {
+ const char *p = dev_get_param(&global_device, param->name);
+ if (val) {
+ char *new = asprintf("%s%s%s", val, seperator, p);
+ free(val);
+ val = new;
+ } else {
+ val = xstrdup(p);
+ }
+ }
+ }
+
+ if (!val)
+ val = xstrdup("");
+
+ return val;
+}
+
+/*
+ * globalvar_add_simple
+ *
+ * add a new globalvar named 'name'
+ */
+int globalvar_add_simple(const char *name)
+{
+ return globalvar_add(name, NULL, NULL, 0);
+}
+
+static int globalvar_init(void)
+{
+ register_device(&global_device);
+
+ return 0;
+}
+postconsole_initcall(globalvar_init);