summaryrefslogtreecommitdiffstats
path: root/commands
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 /commands
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 'commands')
-rw-r--r--commands/Kconfig7
-rw-r--r--commands/Makefile1
-rw-r--r--commands/global.c62
3 files changed, 70 insertions, 0 deletions
diff --git a/commands/Kconfig b/commands/Kconfig
index 49a56ca2ff..1c152948c1 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -113,6 +113,13 @@ config CMD_LINUX_EXEC
help
This command executes a command on the Linux host.
+config CMD_GLOBAL
+ select GLOBALVAR
+ tristate
+ prompt "global"
+ help
+ The global command allows to create global variables
+
endmenu
menu "file commands "
diff --git a/commands/Makefile b/commands/Makefile
index f02b5cac3a..4c8a0a9595 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -65,3 +65,4 @@ obj-$(CONFIG_CMD_MAGICVAR) += magicvar.o
obj-$(CONFIG_CMD_IOMEM) += iomem.o
obj-$(CONFIG_CMD_LINUX_EXEC) += linux_exec.o
obj-$(CONFIG_CMD_AUTOMOUNT) += automount.o
+obj-$(CONFIG_CMD_GLOBAL) += global.o
diff --git a/commands/global.c b/commands/global.c
new file mode 100644
index 0000000000..de6b13e7bd
--- /dev/null
+++ b/commands/global.c
@@ -0,0 +1,62 @@
+/*
+ * global.c - global shell variables
+ *
+ * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <common.h>
+#include <malloc.h>
+#include <command.h>
+#include <globalvar.h>
+#include <environment.h>
+
+static int do_global(int argc, char *argv[])
+{
+ int ret;
+ char *value;
+
+ if (argc != 2)
+ return COMMAND_ERROR_USAGE;
+
+ value = strchr(argv[1], '=');
+ if (value) {
+ *value = 0;
+ value++;
+ }
+
+ ret = globalvar_add_simple(argv[1]);
+
+ if (value) {
+ char *name = asprintf("global.%s", argv[1]);
+ ret = setenv(name, value);
+ free(name);
+ }
+
+ return ret ? 1 : 0;
+}
+
+BAREBOX_CMD_HELP_START(global)
+BAREBOX_CMD_HELP_USAGE("global <var>[=<value]\n")
+BAREBOX_CMD_HELP_SHORT("add a new global variable named <var>, optionally set to <value>\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(global)
+ .cmd = do_global,
+ .usage = "create global variables",
+ BAREBOX_CMD_HELP(cmd_global_help)
+BAREBOX_CMD_END