9
0
Fork 0

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>
This commit is contained in:
Sascha Hauer 2012-04-29 20:21:16 +02:00
parent 9b42dfff0f
commit b8c94a1561
7 changed files with 155 additions and 0 deletions

View File

@ -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 "

View File

@ -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

62
commands/global.c Normal file
View File

@ -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

View File

@ -47,6 +47,9 @@ config BINFMT
bool
select FILETYPE
config GLOBALVAR
bool
menu "General Settings "
config LOCALVERSION
@ -493,6 +496,7 @@ config DEFAULT_ENVIRONMENT_GENERIC
select HUSH_GETOPT
select CMD_CRC
select CMD_CRC_CMP
select CMD_AUTOMOUNT if HAVE_DEFAULT_ENVIRONMENT_NEW
prompt "Default environment generic"
help
With this option barebox will use the generic default
@ -502,6 +506,9 @@ config DEFAULT_ENVIRONMENT_GENERIC
at least contain a /env/config file.
This will be able to overwrite the files from defaultenv.
config HAVE_DEFAULT_ENVIRONMENT_NEW
bool
config DEFAULT_ENVIRONMENT_PATH
string
depends on DEFAULT_ENVIRONMENT

View File

@ -29,6 +29,7 @@ obj-$(CONFIG_CMD_BOOTM) += uimage.o
obj-y += startup.o
obj-y += misc.o
obj-y += memsize.o
obj-$(CONFIG_GLOBALVAR) += globalvar.o
obj-$(CONFIG_FILETYPE) += filetype.o
obj-y += resource.o
obj-$(CONFIG_MENU) += menu.o

65
common/globalvar.c Normal file
View File

@ -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);

12
include/globalvar.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __GLOBALVAR_H
#define __GLOBALVAR_H
int globalvar_add_simple(const char *name);
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);
char *globalvar_get_match(const char *match, const char *seperator);
#endif /* __GLOBALVAR_H */