diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-01-17 13:24:44 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-01-17 13:24:44 -0600 |
commit | 3895a010a5c8d971ec04802ae569f44a06bd572b (patch) | |
tree | d81fc78a1873dd6faa5b634845ddb940c6ce2cab /apps/nshlib/nsh_script.c | |
parent | 5d3165161596265372177859b50577ae1ee54be0 (diff) | |
download | nuttx-3895a010a5c8d971ec04802ae569f44a06bd572b.tar.gz nuttx-3895a010a5c8d971ec04802ae569f44a06bd572b.tar.bz2 nuttx-3895a010a5c8d971ec04802ae569f44a06bd572b.zip |
Add true and false commands; repartition some logic to better support forthcoming looping
Diffstat (limited to 'apps/nshlib/nsh_script.c')
-rw-r--r-- | apps/nshlib/nsh_script.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/apps/nshlib/nsh_script.c b/apps/nshlib/nsh_script.c index 3aa698b31..088df6878 100644 --- a/apps/nshlib/nsh_script.c +++ b/apps/nshlib/nsh_script.c @@ -83,10 +83,10 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR const char *path) { - char *fullpath; - FILE *stream; - char *buffer; - char *pret; + FAR char *fullpath; + FAR FILE *savestream; + FAR char *buffer; + FAR char *pret; int ret = ERROR; /* The path to the script may be relative to the current working directory */ @@ -102,13 +102,24 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, buffer = nsh_linebuffer(vtbl); if (buffer) { + /* Save the parent stream in case of nested script processing */ + + savestream = vtbl->np.np_stream; + /* Open the file containing the script */ - stream = fopen(fullpath, "r"); - if (!stream) + vtbl->np.np_stream = fopen(fullpath, "r"); + if (!vtbl->np.np_stream) { nsh_output(vtbl, g_fmtcmdfailed, cmd, "fopen", NSH_ERRNO); + + /* Free the allocated path */ + nsh_freefullpath(fullpath); + + /* Restore the parent script stream */ + + vtbl->np.np_stream = savestream; return ERROR; } @@ -121,7 +132,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, /* Get the next line of input from the file */ fflush(stdout); - pret = fgets(buffer, CONFIG_NSH_LINELEN, stream); + pret = fgets(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_stream); if (pret) { /* Parse process the command. NOTE: this is recursive... @@ -133,9 +144,18 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, } } while (pret && ret == OK); - fclose(stream); + + /* Close the script file */ + + fclose(vtbl->np.np_stream); + + /* Restore the parent script stream */ + + vtbl->np.np_stream = savestream; } + /* Free the allocated path */ + nsh_freefullpath(fullpath); return ret; } |