Merged in bkueng/nuttx-apps (pull request #150)

nsh: add inverted logic support in the form of 'if ! <cmd>'

Approved-by: GregoryN <gnutt@nuttx.org>
This commit is contained in:
Beat Küng 2018-08-11 13:14:41 +00:00 committed by GregoryN
parent 367093e65c
commit 888ad352eb
3 changed files with 22 additions and 3 deletions

View File

@ -100,7 +100,7 @@ Conditional Command Execution
command line but is primarily intended for use within NSH scripts
(see the sh command). The syntax is as follows:
if <cmd>
if [!] <cmd>
then
[sequence of <cmd>]
else

View File

@ -754,7 +754,8 @@ struct nsh_itef_s
{
uint8_t ie_ifcond : 1; /* Value of command in 'if' statement */
uint8_t ie_disabled : 1; /* TRUE: Unconditionally disabled */
uint8_t ie_unused : 4;
uint8_t ie_inverted : 1; /* TRUE: inverted logic ('if ! <cmd>') */
uint8_t ie_unused : 3;
uint8_t ie_state : 2; /* If-then-else state (see enum nsh_itef_e) */
};
#endif

View File

@ -468,7 +468,8 @@ static int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, bool result)
if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
{
np->np_fail = false;
np->np_iestate[np->np_iendx].ie_ifcond = result;
np->np_iestate[np->np_iendx].ie_ifcond =
np->np_iestate[np->np_iendx].ie_inverted ^ result;
return OK;
}
else
@ -1957,6 +1958,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
FAR struct nsh_parser_s *np = &vtbl->np;
FAR char *cmd = *ppcmd;
bool disabled;
bool inverted_result = false;
if (cmd)
{
@ -1973,6 +1975,20 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
goto errout;
}
/* Check for inverted logic */
if (strcmp(*ppcmd, "!") == 0)
{
inverted_result = true;
/* Get the next cmd */
*ppcmd = nsh_argument(vtbl, saveptr, memlist);
if (!*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "if");
goto errout;
}
}
/* Verify that "if" is valid in this context */
if (np->np_iestate[np->np_iendx].ie_state == NSH_ITEF_IF)
@ -1996,6 +2012,7 @@ static int nsh_itef(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
np->np_iestate[np->np_iendx].ie_state = NSH_ITEF_IF;
np->np_iestate[np->np_iendx].ie_disabled = disabled;
np->np_iestate[np->np_iendx].ie_ifcond = false;
np->np_iestate[np->np_iendx].ie_inverted = inverted_result;
}
/* Check if the token is "then" */
@ -2085,6 +2102,7 @@ errout:
np->np_iestate[0].ie_state = NSH_ITEF_NORMAL;
np->np_iestate[0].ie_disabled = false;
np->np_iestate[0].ie_ifcond = false;
np->np_iestate[0].ie_inverted = false;
return ERROR;
}
#endif