From 888ad352eb41b656d271183b563142472ad712ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Sat, 11 Aug 2018 13:14:41 +0000 Subject: [PATCH] Merged in bkueng/nuttx-apps (pull request #150) nsh: add inverted logic support in the form of 'if ! ' Approved-by: GregoryN --- nshlib/README.txt | 2 +- nshlib/nsh.h | 3 ++- nshlib/nsh_parse.c | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/nshlib/README.txt b/nshlib/README.txt index 69aa566fd..68b5d7852 100644 --- a/nshlib/README.txt +++ b/nshlib/README.txt @@ -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 + if [!] then [sequence of ] else diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 0a7bdd0e4..187cc5696 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -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 ! ') */ + uint8_t ie_unused : 3; uint8_t ie_state : 2; /* If-then-else state (see enum nsh_itef_e) */ }; #endif diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index f41097703..c25e9ae6d 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -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