apps/nshlib: If CONFIG_NSH_VARS=y, the NSH 'set' command with not argument will list all of the local NSH variables.

This commit is contained in:
Gregory Nutt 2018-10-02 11:54:18 -06:00
parent cb982ea8df
commit cf6c6a9556
5 changed files with 128 additions and 7 deletions

View File

@ -1136,6 +1136,12 @@ o set [{+|-}{e|x|xe|ex}] [<name> <value>]
The local Bash variable would that then shadow the environment variable
with a differing value.
If CONFIG_NSH_VARS is selected and no arguments are provided, then the
'set' command will list all list all NSH variables.
nsh> set
foolbar=foovalue
Set the 'exit on error control' and/or 'print a trace' of commands when parsing
scripts in NSH. The settinngs are in effect from the point of exection, until
they are changed again, or in the case of the init script, the settings are

View File

@ -863,6 +863,13 @@ typedef CODE int (*nsh_direntry_handler_t)(FAR struct nsh_vtbl_s *vtbl,
FAR struct dirent *entryp,
FAR void *pvarg);
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_NSH_DISABLE_SET)
/* Used with nsh_foreach_var() */
typedef int (*nsh_foreach_var_t)(FAR struct nsh_vtbl_s *vtbl, FAR void *arg,
FAR const char *pair);
#endif
/****************************************************************************
* Public Data
****************************************************************************/
@ -1464,7 +1471,7 @@ FAR char *nsh_trimspaces(FAR char *str);
* Description:
* Get, set, or unset an NSH variable.
*
* Input Parmeters:
* Input Parameters:
* vtbl - NSH session data
* name - The name of the variable to get or set
* value - The value to use with nsh_setvar()
@ -1472,7 +1479,7 @@ FAR char *nsh_trimspaces(FAR char *str);
* Returned value:
* nsh_getvar() returns a read-only reference to the variable value on
* success or NULL on failure.
* nset_unsetvar() returns OK on success or an netaged errno value on
* nset_unsetvar() returns OK on success or an negated errno value on
* failure.
*
****************************************************************************/
@ -1488,4 +1495,27 @@ int nsh_unsetvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name);
#endif
#endif
/****************************************************************************
* Name: nsh_foreach_var
*
* Description:
* Visit each name-value pair in the environment.
*
* Input Parameters:
* vtbl - NSH session data
* cb - The callback function to be invoked for each environment
* variable.
*
* Returned Value:
* Zero if the all NSH variables have been traversed. A non-zero value
* means that the callback function requested early termination by
* returning a nonzero value.
*
****************************************************************************/
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_NSH_DISABLE_SET)
int nsh_foreach_var(FAR struct nsh_vtbl_s *vtbl, nsh_foreach_var_t cb,
FAR void *arg);
#endif
#endif /* __APPS_NSHLIB_NSH_H */

View File

@ -450,6 +450,15 @@ static const struct cmdmap_s g_cmdmap[] =
#endif
#ifndef CONFIG_NSH_DISABLE_SET
#ifdef CONFIG_NSH_VARS
# if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_NSH_DISABLESCRIPT)
{ "set", cmd_set, 1, 4, "[{+|-}{e|x|xe|ex}] [<name> <value>]" },
# elif !defined(CONFIG_DISABLE_ENVIRON) && defined(CONFIG_NSH_DISABLESCRIPT)
{ "set", cmd_set, 1, 3, "[<name> <value>]" },
# elif defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_NSH_DISABLESCRIPT)
{ "set", cmd_set, 1, 2, "[{+|-}{e|x|xe|ex}]" },
# endif
#else
# if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_NSH_DISABLESCRIPT)
{ "set", cmd_set, 2, 4, "[{+|-}{e|x|xe|ex}] [<name> <value>]" },
# elif !defined(CONFIG_DISABLE_ENVIRON) && defined(CONFIG_NSH_DISABLESCRIPT)
@ -458,6 +467,7 @@ static const struct cmdmap_s g_cmdmap[] =
{ "set", cmd_set, 2, 2, "{+|-}{e|x|xe|ex}" },
# endif
#endif
#endif /* CONFIG_NSH_DISABLE_SET */
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT)
# ifndef CONFIG_NSH_DISABLE_SH

View File

@ -124,6 +124,19 @@ static inline char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
return alloc;
}
/****************************************************************************
* Name: nsh_dumpvar
****************************************************************************/
#if defined(CONFIG_NSH_VARS) && !defined(CONFIG_NSH_DISABLE_SET)
static int nsh_dumpvar(FAR struct nsh_vtbl_s *vtbl, FAR void *arg,
FAR const char *pair)
{
nsh_output(vtbl, "%s\n", pair);
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -336,8 +349,17 @@ int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
const char opts[] = NSH_NP_SET_OPTIONS;
int op;
/* REVISIT: set with no arguments should show all of the NSH variables */
#ifdef CONFIG_NSH_VARS
/* Set with no arguments will show all of the NSH variables */
if (argc == 1)
{
ret = nsh_foreach_var(vtbl, nsh_dumpvar, NULL);
nsh_output(vtbl, "\n");
return ret < 0 ? ERROR : OK;
}
else
#endif
#ifdef NSH_HAVE_VARS
/* Support set [{+|-}{e|x|xe|ex}] [<name> <value>] */

View File

@ -187,7 +187,7 @@ int nsh_removevar(FAR struct console_stdio_s *pstate, FAR char *pair)
* Description:
* Get, set, or unset an NSH variable.
*
* Input Parmeters:
* Input Parameters:
* vtbl - NSH session data
* name - The name of the variable to get or set
* value - The value to use with nsh_setvar()
@ -195,7 +195,7 @@ int nsh_removevar(FAR struct console_stdio_s *pstate, FAR char *pair)
* Returned value:
* nsh_getvar() returns a read-only reference to the variable value on
* success or NULL on failure.
* nset_unsetvar() returns OK on success or an netaged errno value on
* nset_unsetvar() returns OK on success or an negated errno value on
* failure.
*
****************************************************************************/
@ -204,8 +204,7 @@ int nsh_removevar(FAR struct console_stdio_s *pstate, FAR char *pair)
* Name: nsh_getvar
****************************************************************************/
FAR const char *nsh_getvar(FAR struct nsh_vtbl_s *vtbl,
FAR const char *name)
FAR char *nsh_getvar(FAR struct nsh_vtbl_s *vtbl,FAR const char *name)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *pair;
@ -365,4 +364,58 @@ int nsh_unsetvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name)
}
#endif
/****************************************************************************
* Name: nsh_foreach_var
*
* Description:
* Visit each name-value pair in the environment.
*
* Input Parameters:
* vtbl - NSH session data
* cb - The callback function to be invoked for each environment
* variable.
*
* Returned Value:
* Zero if the all NSH variables have been traversed. A non-zero value
* means that the callback function requested early termination by
* returning a nonzero value.
*
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_SET
int nsh_foreach_var(FAR struct nsh_vtbl_s *vtbl, nsh_foreach_var_t cb,
FAR void *arg)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *ptr;
FAR char *end;
int ret = OK;
/* Verify input parameters */
DEBUGASSERT(pstate != NULL && cb != NULL);
/* Search for a name=value string with matching name */
end = &pstate->varp[pstate->varsz];
for (ptr = pstate->varp; ptr < end; ptr += (strlen(ptr) + 1))
{
/* Perform the callback */
ret = cb(vtbl, arg, ptr);
/* Terminate the traversal early if the callback so requests by
* returning a non-zero value.
*/
if (ret != 0)
{
break;
}
}
return ret;
}
#endif
#endif /* CONFIG_NSH_VARS */