environment variables: introduce new helpers
This introduces some new environment variable helpers and updates the existing ones. Newly introduced are: getenv_bool: read a bool variable getenv_ul: read an unsigned long variable getenev_uint: read an unsigned int variable getenv_nonempty: like normal getenv, but does return NULL instead of an empty string All new helpers take a pointer to the value. This value is only modified when the variable exists. This allows the following programming scheme: unsigned int myvalue = sanedefault; getenv_uint("myvalue", &myvalue); So without checking the return value myvalue contains the best possible value. getenv_ull is updated to this scheme. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
28141f9e5a
commit
5ee4ad2229
|
@ -42,11 +42,9 @@
|
|||
static struct tag *params;
|
||||
static void *armlinux_bootparams = NULL;
|
||||
|
||||
#ifndef CONFIG_ENVIRONMENT_VARIABLES
|
||||
static int armlinux_architecture;
|
||||
static u32 armlinux_system_rev;
|
||||
static u64 armlinux_system_serial;
|
||||
#endif
|
||||
|
||||
BAREBOX_MAGICVAR(armlinux_architecture, "ARM machine ID");
|
||||
BAREBOX_MAGICVAR(armlinux_system_rev, "ARM system revision");
|
||||
|
@ -54,56 +52,41 @@ BAREBOX_MAGICVAR(armlinux_system_serial, "ARM system serial");
|
|||
|
||||
void armlinux_set_architecture(int architecture)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
export_env_ull("armlinux_architecture", architecture);
|
||||
#else
|
||||
armlinux_architecture = architecture;
|
||||
#endif
|
||||
}
|
||||
|
||||
int armlinux_get_architecture(void)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
return getenv_ull("armlinux_architecture");
|
||||
#else
|
||||
getenv_uint("armlinux_architecture", &armlinux_architecture);
|
||||
|
||||
return armlinux_architecture;
|
||||
#endif
|
||||
}
|
||||
|
||||
void armlinux_set_revision(unsigned int rev)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
export_env_ull("armlinux_system_rev", rev);
|
||||
#else
|
||||
armlinux_system_rev = rev;
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int armlinux_get_revision(void)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
return getenv_ull("armlinux_system_rev");
|
||||
#else
|
||||
getenv_uint("armlinux_system_rev", &armlinux_system_rev);
|
||||
|
||||
return armlinux_system_rev;
|
||||
#endif
|
||||
}
|
||||
|
||||
void armlinux_set_serial(u64 serial)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
export_env_ull("armlinux_system_serial", serial);
|
||||
#else
|
||||
armlinux_system_serial = serial;
|
||||
#endif
|
||||
}
|
||||
|
||||
u64 armlinux_get_serial(void)
|
||||
{
|
||||
#ifdef CONFIG_ENVIRONMENT_VARIABLES
|
||||
return getenv_ull("armlinux_system_serial");
|
||||
#else
|
||||
getenv_ull("armlinux_system_serial", &armlinux_system_serial);
|
||||
|
||||
return armlinux_system_serial;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ARM_BOARD_APPEND_ATAG
|
||||
|
|
71
common/env.c
71
common/env.c
|
@ -267,13 +267,74 @@ void export_env_ull(const char *name, unsigned long long val)
|
|||
}
|
||||
EXPORT_SYMBOL(export_env_ull);
|
||||
|
||||
unsigned long long getenv_ull(const char *name)
|
||||
/*
|
||||
* Like regular getenv, but never returns an empty string.
|
||||
* If the string is empty, NULL is returned instead
|
||||
*/
|
||||
const char *getenv_nonempty(const char *var)
|
||||
{
|
||||
const char *valstr = getenv(name);
|
||||
const char *val = getenv(var);
|
||||
|
||||
if (!valstr)
|
||||
return 0;
|
||||
if (val && *val)
|
||||
return val;
|
||||
|
||||
return simple_strtoull(valstr, NULL, 0);
|
||||
return NULL;
|
||||
}
|
||||
EXPORT_SYMBOL(getenv_nonempty);
|
||||
|
||||
int getenv_ull(const char *var , unsigned long long *val)
|
||||
{
|
||||
const char *valstr = getenv(var);
|
||||
|
||||
if (!valstr || !*valstr)
|
||||
return -EINVAL;
|
||||
|
||||
*val = simple_strtoull(valstr, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(getenv_ull);
|
||||
|
||||
int getenv_ul(const char *var , unsigned long *val)
|
||||
{
|
||||
const char *valstr = getenv(var);
|
||||
|
||||
if (!valstr || !*valstr)
|
||||
return -EINVAL;
|
||||
|
||||
*val = simple_strtoul(valstr, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(getenv_ul);
|
||||
|
||||
int getenv_uint(const char *var , unsigned int *val)
|
||||
{
|
||||
const char *valstr = getenv(var);
|
||||
|
||||
if (!valstr || !*valstr)
|
||||
return -EINVAL;
|
||||
|
||||
*val = simple_strtoul(valstr, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(getenv_uint);
|
||||
|
||||
int getenv_bool(const char *var, int *val)
|
||||
{
|
||||
const char *valstr = getenv(var);
|
||||
|
||||
if (!valstr || !*valstr)
|
||||
return -EINVAL;
|
||||
|
||||
if (!*valstr)
|
||||
*val = false;
|
||||
else if (*valstr == '0')
|
||||
*val = false;
|
||||
else
|
||||
*val = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL(getenv_bool);
|
||||
|
|
|
@ -45,7 +45,11 @@ char *var_name(struct variable_d *);
|
|||
const char *getenv(const char *);
|
||||
int setenv(const char *, const char *);
|
||||
void export_env_ull(const char *name, unsigned long long val);
|
||||
unsigned long long getenv_ull(const char *name);
|
||||
int getenv_ull(const char *name, unsigned long long *val);
|
||||
int getenv_ul(const char *name, unsigned long *val);
|
||||
int getenv_uint(const char *name, unsigned int *val);
|
||||
int getenv_bool(const char *var, int *val);
|
||||
const char *getenv_nonempty(const char *var);
|
||||
#else
|
||||
static inline char *getenv(const char *var)
|
||||
{
|
||||
|
@ -56,10 +60,22 @@ static inline int setenv(const char *var, const char *val)
|
|||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void export_env_ull(const char *name, unsigned long long val) {}
|
||||
static inline unsigned long long getenv_ull(const char *name)
|
||||
|
||||
static inline int getenv_ull(const char *name, unsigned long long *val)
|
||||
{
|
||||
return 0;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline int getenv_ul(const char *name, unsigned long *val)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline int getenv_uint(const char *name, unsigned int *val)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline int export(const char *var)
|
||||
|
@ -67,6 +83,15 @@ static inline int export(const char *var)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline int getenv_bool(const char *var, int *val)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline const char *getenv_nonempty(const char *var)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
int env_pop_context(void);
|
||||
|
|
Loading…
Reference in New Issue