Added support for set [{+|-}{e|x|xe|ex}] [<name> <value>]
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 returned to the default settings when it exits. Included child scripts will run with the parents settings and changes made in the child script will effect the parent on return. Use 'set -e' to enable and 'set +e' to disable (ignore) the exit condition on commands. The default is -e. Errors cause script to exit. Use 'set -x' to enable and 'set +x' to disable (silence) printing a trace of the script commands as they are ececuted. The default is +x. No printing of a trace of script commands as they are executed.
This commit is contained in:
parent
74ae283487
commit
d03aa9112e
@ -1017,10 +1017,10 @@ o rmmod <module-name>
|
||||
NAME INIT UNINIT ARG TEXT SIZE DATA SIZE
|
||||
nsh>
|
||||
|
||||
o set <name> <value>
|
||||
o set [{+|-}{e|x|xe|ex}] [<name> <value>]
|
||||
|
||||
Set the environment variable <name> to the sting <value>.
|
||||
For example,
|
||||
Set the environment variable <name> to the sting <value> and or set NSH
|
||||
parser control options. For example,
|
||||
|
||||
nsh> echo $foobar
|
||||
|
||||
@ -1029,6 +1029,38 @@ o set <name> <value>
|
||||
foovalue
|
||||
nsh>
|
||||
|
||||
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
|
||||
returned to the default settings when it exits. Included child scripts will run
|
||||
with the parents settings and changes made in the child script will effect the
|
||||
parent on return.
|
||||
|
||||
Use 'set -e' to enable and 'set +e' to disable (ignore) the exit condition on commands.
|
||||
The default is -e. Errors cause script to exit.
|
||||
|
||||
Use 'set -x' to enable and 'set +x' to disable (silence) printing a trace of the script
|
||||
commands as they are ececuted.
|
||||
The default is +x. No printing of a trace of script commands as they are executed.
|
||||
|
||||
Example 1 - no exit on command not found
|
||||
set +e
|
||||
notacommand
|
||||
|
||||
Example 2 - will exit on command not found
|
||||
set -e
|
||||
notacommand
|
||||
|
||||
Example 3 - will exit on command not found, and print a trace of the script commmands
|
||||
set -ex
|
||||
|
||||
Example 4 - will exit on command not found, and print a trace of the script commmands
|
||||
and set foobar to foovalue.
|
||||
set -ex foobar foovalue
|
||||
nsh> echo $foobar
|
||||
foovalue
|
||||
|
||||
|
||||
o sh <script-path>
|
||||
|
||||
Execute the sequence of NSH commands in the file referred
|
||||
|
30
nshlib/nsh.h
30
nshlib/nsh.h
@ -690,6 +690,15 @@
|
||||
# undef NSH_HAVE_TRIMSPACES
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
# define NSH_NP_SET_OPTIONS "ex" /* Maintain order see nsh_npflags_e */
|
||||
# define NSH_NP_SET_OPTIONS_INIT (NSH_PFALG_SILENT)
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_DISABLE_ENVIRON) && defined(CONFIG_NSH_DISABLESCRIPT)
|
||||
# define CONFIG_NSH_DISABLE_SET
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
@ -741,6 +750,22 @@ struct nsh_loop_s
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
/* Define the bits that correspond to the option defined in
|
||||
* NSH_NP_SET_OPTIONS. The bit value is 1 shifted left the offset
|
||||
* of the char in NSH_NP_SET_OPTIONS string.
|
||||
*/
|
||||
|
||||
enum nsh_npflags_e
|
||||
{
|
||||
NSH_PFALG_IGNORE = 1, /* set for +e no exit on errors,
|
||||
* cleared -e exit on error */
|
||||
NSH_PFALG_SILENT = 2, /* cleared -x print a trace of commands
|
||||
* when parsing.
|
||||
* set +x no print a trace of commands */
|
||||
};
|
||||
#endif
|
||||
|
||||
/* These structure provides the overall state of the parser */
|
||||
|
||||
struct nsh_parser_s
|
||||
@ -752,6 +777,9 @@ struct nsh_parser_s
|
||||
bool np_redirect; /* true: Output from the last command was re-directed */
|
||||
#endif
|
||||
bool np_fail; /* true: The last command failed */
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
uint8_t np_flags; /* See nsh_npflags_e above */
|
||||
#endif
|
||||
#ifndef CONFIG_NSH_DISABLEBG
|
||||
int np_nice; /* "nice" value applied to last background cmd */
|
||||
#endif
|
||||
@ -1175,10 +1203,10 @@ int cmd_lsmod(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||
int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
#ifndef CONFIG_NSH_DISABLE_SET
|
||||
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
# ifndef CONFIG_NSH_DISABLE_UNSET
|
||||
int cmd_unset(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||
# endif
|
||||
|
@ -436,9 +436,13 @@ static const struct cmdmap_s g_cmdmap[] =
|
||||
{ "rmmod", cmd_rmmod, 2, 2, "<module-name>" },
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
#ifndef CONFIG_NSH_DISABLE_SET
|
||||
# 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)
|
||||
{ "set", cmd_set, 3, 3, "<name> <value>" },
|
||||
# elif defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_NSH_DISABLESCRIPT)
|
||||
{ "set", cmd_set, 2, 2, "{+|-}{e|x|xe|ex}" },
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
@ -480,5 +480,11 @@ FAR struct console_stdio_s *nsh_newconsole(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
/* Set the initial option flags */
|
||||
|
||||
pstate->cn_vtbl.np.np_flags = NSH_NP_SET_OPTIONS_INIT;
|
||||
#endif
|
||||
|
||||
return pstate;
|
||||
}
|
||||
|
@ -85,6 +85,12 @@ int nsh_consolemain(int argc, char *argv[])
|
||||
/* Execute the start-up script */
|
||||
|
||||
(void)nsh_initscript(&pstate->cn_vtbl);
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
/* Reset the option flags */
|
||||
|
||||
pstate->cn_vtbl.np.np_flags = NSH_NP_SET_OPTIONS_INIT;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NSH_USBDEV_TRACE
|
||||
|
@ -311,28 +311,85 @@ int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
||||
* Name: cmd_set
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
#ifndef CONFIG_NSH_DISABLE_SET
|
||||
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
||||
{
|
||||
FAR char *value;
|
||||
int ret;
|
||||
int ret = OK;
|
||||
int ndx = 1;
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
FAR char *popt;
|
||||
const char opts[] = NSH_NP_SET_OPTIONS;
|
||||
int op;
|
||||
|
||||
/* Support set [{+|-}{e|x|xe|ex}] [<name> <value>] */
|
||||
|
||||
if (argc == 2 || argc == 4)
|
||||
{
|
||||
if (strlen(argv[1]) < 2)
|
||||
{
|
||||
ret = -EINVAL;
|
||||
nsh_output(vtbl, g_fmtargrequired, argv[0], "set", NSH_ERRNO);
|
||||
}
|
||||
else
|
||||
{
|
||||
op = argv[1][0];
|
||||
if (op != '-' && op != '+')
|
||||
{
|
||||
ret = -EINVAL;
|
||||
nsh_output(vtbl, g_fmtarginvalid, argv[0], "set", NSH_ERRNO);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = &argv[1][1];
|
||||
while(*value && *value != ' ')
|
||||
{
|
||||
popt = strchr(opts, *value++);
|
||||
if (popt == NULL)
|
||||
{
|
||||
nsh_output(vtbl, g_fmtarginvalid, argv[0], "set", NSH_ERRNO);
|
||||
ret = -EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
if (op == '+')
|
||||
{
|
||||
vtbl->np.np_flags |= 1 << (popt-opts);
|
||||
}
|
||||
else
|
||||
{
|
||||
vtbl->np.np_flags &= ~(1 << (popt-opts));
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == OK)
|
||||
{
|
||||
ndx = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
# ifndef CONFIG_DISABLE_ENVIRON
|
||||
if (ret == OK && (argc == 3 || argc == 4))
|
||||
# endif
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
{
|
||||
/* Trim whitespace from the value */
|
||||
|
||||
value = nsh_trimspaces(argv[2]);
|
||||
value = nsh_trimspaces(argv[ndx+1]);
|
||||
|
||||
/* Set the environment variable */
|
||||
|
||||
ret = setenv(argv[1], value, TRUE);
|
||||
ret = setenv(argv[ndx], value, TRUE);
|
||||
if (ret < 0)
|
||||
{
|
||||
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "setenv", NSH_ERRNO);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -136,10 +136,15 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
||||
* considerable amount of stack may be used.
|
||||
*/
|
||||
|
||||
if ((vtbl->np.np_flags & NSH_PFALG_SILENT) == 0)
|
||||
{
|
||||
nsh_output(vtbl,"%s", buffer);
|
||||
}
|
||||
|
||||
ret = nsh_parse(vtbl, buffer);
|
||||
}
|
||||
}
|
||||
while (pret && ret == OK);
|
||||
while (pret && (ret == OK || (vtbl->np.np_flags & NSH_PFALG_IGNORE)));
|
||||
|
||||
/* Close the script file */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user