summaryrefslogtreecommitdiff
path: root/nuttx/examples/nsh
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-18 23:15:59 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-18 23:15:59 +0000
commit2cde9d488e766651e1641d9ab4c41237c3647fa3 (patch)
treec1c2f33505ab44ed74ecc4dfdc6004b5667d6dcb /nuttx/examples/nsh
parenta2b87c9a5c89134182092d18630747b5777307eb (diff)
downloadpx4-nuttx-2cde9d488e766651e1641d9ab4c41237c3647fa3.tar.gz
px4-nuttx-2cde9d488e766651e1641d9ab4c41237c3647fa3.tar.bz2
px4-nuttx-2cde9d488e766651e1641d9ab4c41237c3647fa3.zip
Misc. if-then-else-fi fixes
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@831 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/nsh')
-rw-r--r--nuttx/examples/nsh/nsh_fscmds.c13
-rw-r--r--nuttx/examples/nsh/nsh_main.c37
2 files changed, 33 insertions, 17 deletions
diff --git a/nuttx/examples/nsh/nsh_fscmds.c b/nuttx/examples/nsh/nsh_fscmds.c
index 8ec2d7c25..75bbfc8e9 100644
--- a/nuttx/examples/nsh/nsh_fscmds.c
+++ b/nuttx/examples/nsh/nsh_fscmds.c
@@ -782,12 +782,15 @@ int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
FILE *stream;
char *buffer;
char *pret;
+ int ret = ERROR;
/* Get a reference to the common input buffer */
buffer = nsh_linebuffer(vtbl);
if (buffer)
{
+ /* Open the file containing the script */
+
stream = fopen(argv[1], "r");
if (!stream)
{
@@ -795,6 +798,10 @@ int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
return ERROR;
}
+ /* Loop, processing each command line in the script file (or
+ * until an error occurs)
+ */
+
do
{
/* Get the next line of input from the file*/
@@ -808,13 +815,13 @@ int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
* considerable amount of stack may be used.
*/
- (void)nsh_parse(vtbl, buffer);
+ ret = nsh_parse(vtbl, buffer);
}
}
- while(pret);
+ while (pret && ret == OK);
fclose(stream);
}
- return OK;
+ return ret;
}
#endif
diff --git a/nuttx/examples/nsh/nsh_main.c b/nuttx/examples/nsh/nsh_main.c
index b2915a75a..8c2999cf7 100644
--- a/nuttx/examples/nsh/nsh_main.c
+++ b/nuttx/examples/nsh/nsh_main.c
@@ -452,7 +452,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (!*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "if");
- return ERROR;
+ goto errout;
}
/* Verify that "if" is valid in this context */
@@ -460,7 +460,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (vtbl->np.np_state != NSH_PARSER_NORMAL)
{
nsh_output(vtbl, g_fmtcontext, "if");
- return ERROR;
+ goto errout;
}
vtbl->np.np_state = NSH_PARSER_IF;
}
@@ -472,7 +472,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "then");
- return ERROR;
+ goto errout;
}
/* Verify that "then" is valid in this context */
@@ -480,7 +480,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (vtbl->np.np_state != NSH_PARSER_IF)
{
nsh_output(vtbl, g_fmtcontext, "then");
- return ERROR;
+ goto errout;
}
vtbl->np.np_state = NSH_PARSER_THEN;
}
@@ -492,7 +492,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "else");
- return ERROR;
+ goto errout;
}
/* Verify that "then" is valid in this context */
@@ -500,7 +500,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (vtbl->np.np_state != NSH_PARSER_THEN)
{
nsh_output(vtbl, g_fmtcontext, "else");
- return ERROR;
+ goto errout;
}
vtbl->np.np_state = NSH_PARSER_ELSE;
}
@@ -512,7 +512,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (*ppcmd)
{
nsh_output(vtbl, g_fmtarginvalid, "fi");
- return ERROR;
+ goto errout;
}
/* Verify that "fi" is valid in this context */
@@ -520,17 +520,21 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (vtbl->np.np_state != NSH_PARSER_THEN && vtbl->np.np_state != NSH_PARSER_ELSE)
{
nsh_output(vtbl, g_fmtcontext, "fi");
- return ERROR;
+ goto errout;
}
vtbl->np.np_state = NSH_PARSER_NORMAL;
}
else if (vtbl->np.np_state == NSH_PARSER_IF)
{
nsh_output(vtbl, g_fmtcontext, cmd);
- return ERROR;
+ goto errout;
}
}
return OK;
+
+errout:
+ vtbl->np.np_state = NSH_PARSER_NORMAL;
+ return ERROR;
}
/****************************************************************************
@@ -559,12 +563,19 @@ static inline boolean nsh_cmdenabled(FAR struct nsh_vtbl_s *vtbl)
* Name: nsh_saveresult
****************************************************************************/
-static inline void nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, boolean result)
+static inline int nsh_saveresult(FAR struct nsh_vtbl_s *vtbl, boolean result)
{
vtbl->np.np_fail = result;
if (vtbl->np.np_state == NSH_PARSER_IF)
{
+ vtbl->np.np_fail = FALSE;
vtbl->np.np_ifcond = result;
+ return OK;
+ }
+ else
+ {
+ vtbl->np.np_fail = result;
+ return result ? ERROR : OK;
}
}
@@ -959,8 +970,7 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline)
* command task succeeded).
*/
- nsh_saveresult(vtbl, FALSE);
- return OK;
+ return nsh_saveresult(vtbl, FALSE);
errout_with_redirect:
if (vtbl->np.np_redirect)
@@ -968,6 +978,5 @@ errout_with_redirect:
close(fd);
}
errout:
- nsh_saveresult(vtbl, TRUE);
- return ERROR;
+ return nsh_saveresult(vtbl, TRUE);
}