From cab9dbe225a9742f5533d49be54cb37065a25cd0 Mon Sep 17 00:00:00 2001 From: patacongo Date: Mon, 18 Aug 2008 23:15:59 +0000 Subject: [PATCH] Misc. if-then-else-fi fixes git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@831 42af7a65-404d-4744-a932-0658087f49c3 --- examples/nsh/nsh_fscmds.c | 13 ++++++++++--- examples/nsh/nsh_main.c | 37 +++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/examples/nsh/nsh_fscmds.c b/examples/nsh/nsh_fscmds.c index 8ec2d7c252..75bbfc8e9d 100644 --- a/examples/nsh/nsh_fscmds.c +++ b/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/examples/nsh/nsh_main.c b/examples/nsh/nsh_main.c index b2915a75ae..8c2999cf71 100644 --- a/examples/nsh/nsh_main.c +++ b/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); }