mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-10-20 04:26:04 +08:00
nsh: Decouple with CONFIG_FILE_STREAM
Replace all fwrite/fread/fgets/... to write/read/... Before: ``` text data bss dec hex filename 109827 601 6608 117036 1c92c nuttx/nuttx ``` After: ``` text data bss dec hex filename 108053 601 6608 115262 1c23e nuttx/nuttx ``` After with CONFIG_FILE_STREAM disabled: ``` text data bss dec hex filename 105667 601 6608 112876 1b8ec nuttx/nuttx ``` Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
@@ -30,7 +30,9 @@
|
||||
#include "nsh.h"
|
||||
#include "nsh_console.h"
|
||||
|
||||
#if defined(CONFIG_FILE_STREAM) && !defined(CONFIG_NSH_DISABLESCRIPT)
|
||||
#include <system/readline.h>
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLESCRIPT
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
@@ -85,9 +87,8 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
FAR const char *path, bool log)
|
||||
{
|
||||
FAR char *fullpath;
|
||||
FAR FILE *savestream;
|
||||
int savestream;
|
||||
FAR char *buffer;
|
||||
FAR char *pret;
|
||||
int ret = ERROR;
|
||||
|
||||
/* The path to the script may relative to the current working directory */
|
||||
@@ -105,12 +106,12 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
{
|
||||
/* Save the parent stream in case of nested script processing */
|
||||
|
||||
savestream = vtbl->np.np_stream;
|
||||
savestream = vtbl->np.np_fd;
|
||||
|
||||
/* Open the file containing the script */
|
||||
|
||||
vtbl->np.np_stream = fopen(fullpath, "r");
|
||||
if (!vtbl->np.np_stream)
|
||||
vtbl->np.np_fd = open(fullpath, O_RDOK);
|
||||
if (vtbl->np.np_fd < 0)
|
||||
{
|
||||
if (log)
|
||||
{
|
||||
@@ -123,7 +124,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
|
||||
/* Restore the parent script stream */
|
||||
|
||||
vtbl->np.np_stream = savestream;
|
||||
vtbl->np.np_fd = savestream;
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
@@ -133,10 +134,6 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
|
||||
do
|
||||
{
|
||||
/* Flush any output generated by the previous line */
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLE_LOOPS
|
||||
/* Get the current file position. This is used to control
|
||||
* looping. If a loop begins in the next line, then this file
|
||||
@@ -144,7 +141,7 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
* script file. Note that ftell will return -1 on failure.
|
||||
*/
|
||||
|
||||
vtbl->np.np_foffs = ftell(vtbl->np.np_stream);
|
||||
vtbl->np.np_foffs = lseek(vtbl->np.np_fd, 0, SEEK_CUR);
|
||||
vtbl->np.np_loffs = 0;
|
||||
|
||||
if (vtbl->np.np_foffs < 0 && log)
|
||||
@@ -155,8 +152,8 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
|
||||
/* Now read the next line from the script file */
|
||||
|
||||
pret = fgets(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_stream);
|
||||
if (pret)
|
||||
ret = readline_fd(buffer, CONFIG_NSH_LINELEN, vtbl->np.np_fd, -1);
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* Parse process the command. NOTE: this is recursive...
|
||||
* we got to cmd_source via a call to nsh_parse. So some
|
||||
@@ -171,15 +168,15 @@ int nsh_script(FAR struct nsh_vtbl_s *vtbl, FAR const FAR char *cmd,
|
||||
ret = nsh_parse(vtbl, buffer);
|
||||
}
|
||||
}
|
||||
while (pret && (ret == OK || (vtbl->np.np_flags & NSH_PFLAG_IGNORE)));
|
||||
while (ret >= 0 || (vtbl->np.np_flags & NSH_PFLAG_IGNORE));
|
||||
|
||||
/* Close the script file */
|
||||
|
||||
fclose(vtbl->np.np_stream);
|
||||
close(vtbl->np.np_fd);
|
||||
|
||||
/* Restore the parent script stream */
|
||||
|
||||
vtbl->np.np_stream = savestream;
|
||||
vtbl->np.np_fd = savestream;
|
||||
}
|
||||
|
||||
/* Free the allocated path */
|
||||
@@ -265,4 +262,4 @@ int nsh_loginscript(FAR struct nsh_vtbl_s *vtbl)
|
||||
#endif
|
||||
#endif /* CONFIG_NSH_ROMFSETC */
|
||||
|
||||
#endif /* CONFIG_FILE_STREAM && !CONFIG_NSH_DISABLESCRIPT */
|
||||
#endif /* CONFIG_NSH_DISABLESCRIPT */
|
||||
|
Reference in New Issue
Block a user