Misc. if-then-else-fi fixes

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@831 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-08-18 23:15:59 +00:00
parent ffaed10379
commit cab9dbe225
2 changed files with 33 additions and 17 deletions

View File

@ -782,12 +782,15 @@ int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
FILE *stream; FILE *stream;
char *buffer; char *buffer;
char *pret; char *pret;
int ret = ERROR;
/* Get a reference to the common input buffer */ /* Get a reference to the common input buffer */
buffer = nsh_linebuffer(vtbl); buffer = nsh_linebuffer(vtbl);
if (buffer) if (buffer)
{ {
/* Open the file containing the script */
stream = fopen(argv[1], "r"); stream = fopen(argv[1], "r");
if (!stream) if (!stream)
{ {
@ -795,6 +798,10 @@ int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
return ERROR; return ERROR;
} }
/* Loop, processing each command line in the script file (or
* until an error occurs)
*/
do do
{ {
/* Get the next line of input from the file*/ /* 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. * 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); fclose(stream);
} }
return OK; return ret;
} }
#endif #endif

View File

@ -452,7 +452,7 @@ static inline int nsh_ifthenelse(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd,
if (!*ppcmd) if (!*ppcmd)
{ {
nsh_output(vtbl, g_fmtarginvalid, "if"); nsh_output(vtbl, g_fmtarginvalid, "if");
return ERROR; goto errout;
} }
/* Verify that "if" is valid in this context */ /* 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) if (vtbl->np.np_state != NSH_PARSER_NORMAL)
{ {
nsh_output(vtbl, g_fmtcontext, "if"); nsh_output(vtbl, g_fmtcontext, "if");
return ERROR; goto errout;
} }
vtbl->np.np_state = NSH_PARSER_IF; 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) if (*ppcmd)
{ {
nsh_output(vtbl, g_fmtarginvalid, "then"); nsh_output(vtbl, g_fmtarginvalid, "then");
return ERROR; goto errout;
} }
/* Verify that "then" is valid in this context */ /* 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) if (vtbl->np.np_state != NSH_PARSER_IF)
{ {
nsh_output(vtbl, g_fmtcontext, "then"); nsh_output(vtbl, g_fmtcontext, "then");
return ERROR; goto errout;
} }
vtbl->np.np_state = NSH_PARSER_THEN; 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) if (*ppcmd)
{ {
nsh_output(vtbl, g_fmtarginvalid, "else"); nsh_output(vtbl, g_fmtarginvalid, "else");
return ERROR; goto errout;
} }
/* Verify that "then" is valid in this context */ /* 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) if (vtbl->np.np_state != NSH_PARSER_THEN)
{ {
nsh_output(vtbl, g_fmtcontext, "else"); nsh_output(vtbl, g_fmtcontext, "else");
return ERROR; goto errout;
} }
vtbl->np.np_state = NSH_PARSER_ELSE; 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) if (*ppcmd)
{ {
nsh_output(vtbl, g_fmtarginvalid, "fi"); nsh_output(vtbl, g_fmtarginvalid, "fi");
return ERROR; goto errout;
} }
/* Verify that "fi" is valid in this context */ /* 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) if (vtbl->np.np_state != NSH_PARSER_THEN && vtbl->np.np_state != NSH_PARSER_ELSE)
{ {
nsh_output(vtbl, g_fmtcontext, "fi"); nsh_output(vtbl, g_fmtcontext, "fi");
return ERROR; goto errout;
} }
vtbl->np.np_state = NSH_PARSER_NORMAL; vtbl->np.np_state = NSH_PARSER_NORMAL;
} }
else if (vtbl->np.np_state == NSH_PARSER_IF) else if (vtbl->np.np_state == NSH_PARSER_IF)
{ {
nsh_output(vtbl, g_fmtcontext, cmd); nsh_output(vtbl, g_fmtcontext, cmd);
return ERROR; goto errout;
} }
} }
return OK; 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 * 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; vtbl->np.np_fail = result;
if (vtbl->np.np_state == NSH_PARSER_IF) if (vtbl->np.np_state == NSH_PARSER_IF)
{ {
vtbl->np.np_fail = FALSE;
vtbl->np.np_ifcond = result; 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). * command task succeeded).
*/ */
nsh_saveresult(vtbl, FALSE); return nsh_saveresult(vtbl, FALSE);
return OK;
errout_with_redirect: errout_with_redirect:
if (vtbl->np.np_redirect) if (vtbl->np.np_redirect)
@ -968,6 +978,5 @@ errout_with_redirect:
close(fd); close(fd);
} }
errout: errout:
nsh_saveresult(vtbl, TRUE); return nsh_saveresult(vtbl, TRUE);
return ERROR;
} }