From 92b1eb36c8a90e689bf16dc2b723e875ebfb85e2 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 28 Nov 2015 08:29:27 -0600 Subject: [PATCH] nshlib: Move trim_dir() and foreach_direntry() from nsh_fscmds.c to nsh_fsutils.c as nsh_trimdir() and nsh_foreach_direntry(), respectively --- nshlib/nsh.h | 79 +++++++++++++++++++++++++++--- nshlib/nsh_command.c | 8 ++-- nshlib/nsh_fscmds.c | 109 ++++-------------------------------------- nshlib/nsh_fsutils.c | 97 +++++++++++++++++++++++++++++++++++++ nshlib/nsh_proccmds.c | 10 ++-- 5 files changed, 189 insertions(+), 114 deletions(-) diff --git a/nshlib/nsh.h b/nshlib/nsh.h index adb8d3aed..e164ee78c 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -548,7 +548,6 @@ /* Define to enable dumping of all input/output buffers */ #undef CONFIG_NSH_TELNETD_DUMPBUFFER -#undef CONFIG_NSH_FULLPATH /* Make sure that the home directory is defined */ @@ -631,12 +630,16 @@ /* Suppress unused file utilities */ -#define NSH_HAVE_CATFILE 1 -#define NSH_HAVE_READFILE 1 +#define NSH_HAVE_CATFILE 1 +#define NSH_HAVE_READFILE 1 +#define NSH_HAVE_FOREACH_DIRENTRY 1 +#define NSH_HAVE_TRIMDIR 1 #if CONFIG_NFILE_DESCRIPTORS <= 0 # undef NSH_HAVE_CATFILE # undef NSH_HAVE_READFILE +# undef NSH_HAVE_FOREACH_DIRENTRY +# undef NSH_HAVE_TRIMDIR #endif /* nsh_catfile used by cat, ifconfig, ifup/down */ @@ -646,12 +649,22 @@ # undef NSH_HAVE_CATFILE #endif -/* nsh_readfile use by ps command (for load information) */ +/* nsh_readfile used by ps command (for load information) */ #if !defined(NSH_HAVE_CPULOAD) # undef NSH_HAVE_READFILE #endif +/* nsh_foreach_direntry used by the ls command */ + +#if defined(CONFIG_NSH_DISABLE_LS) +# undef NSH_HAVE_READFILE +#endif + +#if defined(CONFIG_NSH_DISABLE_CP) +# undef NSH_HAVE_TRIMDIR +#endif + /**************************************************************************** * Public Types ****************************************************************************/ @@ -743,8 +756,19 @@ struct nsh_parser_s #endif }; +/* This is the general form of a command handler */ + struct nsh_vtbl_s; /* Defined in nsh_console.h */ -typedef int (*cmd_t)(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +typedef CODE int (*nsh_cmd_t)(FAR struct nsh_vtbl_s *vtbl, int argc, + FAR char **argv); + +/* This is the form of a callback from nsh_foreach_direntry() */ + +struct dirent; /* Defined in dirent.h */ +typedef CODE int (*nsh_direntry_handler_t)(FAR struct nsh_vtbl_s *vtbl, + FAR const char *dirpath, + FAR struct dirent *entryp, + FAR void *pvarg); /**************************************************************************** * Public Data @@ -1158,7 +1182,7 @@ FAR const char *nsh_extmatch_getname(int index); * Dump the contents of a file to the current NSH terminal. * * Input Paratemets: - * vtbl - session vtbl + * vtbl - The console vtable * cmd - NSH command name to use in error reporting * filepath - The full path to the file to be dumped * @@ -1181,6 +1205,7 @@ int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, * * Input Paramters: * vtbl - The console vtable + * cmd - NSH command name to use in error reporting * filepath - The full path to the file to be read * buffer - The user-provided buffer into which the file is read. * buflen - The size of the user provided buffer @@ -1197,4 +1222,46 @@ static int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, size_t buflen); #endif +/**************************************************************************** + * Name: nsh_foreach_direntry + * + * Description: + * Call the provided 'handler' for each entry found in the directory at + * 'dirpath'. + * + * Input Parameters + * vtbl - The console vtable + * cmd - NSH command name to use in error reporting + * dirpath - The full path to the directory to be traversed + * handler - The handler to be called for each entry of the directory + * pvarg - User provided argument to be passed to the 'handler' + * + * Returned Value: + * Zero (OK) returned on success; -1 (ERROR) returned on failure. + * + ****************************************************************************/ + +#ifdef NSH_HAVE_FOREACH_DIRENTRY +int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *dirpath, + nsh_direntry_handler_t handler, void *pvarg); +#endif + +/**************************************************************************** + * Name: nsh_trimdir + * + * Description: + * Skip any trailing '/' characters (unless it is also the leading '/') + * + * Input Parmeters: + * dirpath - The directory path to be trimmed. May be modified! + * + * Returned value + * + ****************************************************************************/ + +#ifdef NSH_HAVE_TRIMDIR +void nsh_trimdir(FAR char *dirpath); +#endif + #endif /* __APPS_NSHLIB_NSH_H */ diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 7668763a6..c23049974 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -70,11 +70,11 @@ struct cmdmap_s { - const char *cmd; /* Name of the command */ - cmd_t handler; /* Function that handles the command */ + FAR const char *cmd; /* Name of the command */ + nsh_cmd_t handler; /* Function that handles the command */ uint8_t minargs; /* Minimum number of arguments (including command) */ uint8_t maxargs; /* Maximum number of arguments (including command) */ - const char *usage; /* Usage instructions for 'help' command */ + FAR const char *usage; /* Usage instructions for 'help' command */ }; /**************************************************************************** @@ -784,7 +784,7 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, char *argv[]) { const struct cmdmap_s *cmdmap; const char *cmd; - cmd_t handler = cmd_unrecognized; + nsh_cmd_t handler = cmd_unrecognized; int ret; /* The form of argv is: diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index 1024d1995..b72adba8a 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -93,17 +93,6 @@ #define LSFLAGS_LONG 2 #define LSFLAGS_RECURSIVE 4 -/**************************************************************************** - * Private Types - ****************************************************************************/ - -typedef int (*direntry_handler_t)(FAR struct nsh_vtbl_s *, const char *, - struct dirent *, void *); - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - /**************************************************************************** * Private Data ****************************************************************************/ @@ -127,31 +116,13 @@ static char g_iobuffer[IOBUFFERSIZE]; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: trim_dir - ****************************************************************************/ - -#if !defined(CONFIG_NSH_DISABLE_CP) || defined(CONFIG_NSH_FULLPATH) -static void trim_dir(char *arg) -{ - /* Skip any trailing '/' characters (unless it is also the leading '/') */ - - int len = strlen(arg) - 1; - while (len > 0 && arg[len] == '/') - { - arg[len] = '\0'; - len--; - } -} -#endif - /**************************************************************************** * Name: nsh_getdirpath ****************************************************************************/ #if CONFIG_NFILE_DESCRIPTORS > 0 && \ (!defined(CONFIG_NSH_DISABLE_LS) || !defined(CONFIG_NSH_DISABLE_CP)) -static char *nsh_getdirpath(const char *path, const char *file) +static char *nsh_getdirpath(FAR const char *path, FAR const char *file) { /* Handle the case where all that is left is '/' */ @@ -169,64 +140,6 @@ static char *nsh_getdirpath(const char *path, const char *file) } #endif -/**************************************************************************** - * Name: foreach_direntry - ****************************************************************************/ - -#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_NSH_DISABLE_LS) -static int foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, - FAR const char *dirpath, - direntry_handler_t handler, void *pvarg) -{ - DIR *dirp; - int ret = OK; - - /* Trim trailing '/' from directory names */ - -#ifdef CONFIG_NSH_FULLPATH - trim_dir(arg); -#endif - - /* Open the directory */ - - dirp = opendir(dirpath); - - if (!dirp) - { - /* Failed to open the directory */ - - nsh_output(vtbl, g_fmtnosuch, cmd, "directory", dirpath); - return ERROR; - } - - /* Read each directory entry */ - - for (;;) - { - FAR struct dirent *entryp = readdir(dirp); - if (!entryp) - { - /* Finished with this directory */ - - break; - } - - /* Call the handler with this directory entry */ - - if (handler(vtbl, dirpath, entryp, pvarg) < 0) - { - /* The handler reported a problem */ - - ret = ERROR; - break; - } - } - - closedir(dirp); - return ret; -} -#endif - /**************************************************************************** * Name: ls_specialdir ****************************************************************************/ @@ -354,11 +267,8 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath, if (entryp != NULL) { -#ifdef CONFIG_NSH_FULLPATH - nsh_output(vtbl, " %s/%s", arg, entryp->d_name); -#else nsh_output(vtbl, " %s", entryp->d_name); -#endif + if (DIRENT_ISDIRECTORY(entryp->d_type) && !ls_specialdir(entryp->d_name)) { @@ -405,12 +315,13 @@ static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath, /* Traverse the directory */ - ret = foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg); + ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg); if (ret == 0) { /* Then recurse to list each directory within the directory */ - ret = foreach_direntry(vtbl, "ls", newpath, ls_recursive, pvarg); + ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_recursive, + pvarg); free(newpath); } } @@ -589,7 +500,7 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { /* Yes, it is a directory. Remove any trailing '/' characters from the path */ - trim_dir(argv[2]); + nsh_trimdir(argv[2]); /* Construct the full path to the new file */ @@ -981,14 +892,14 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) nsh_output(vtbl, "%s:\n", fullpath); - ret = foreach_direntry(vtbl, "ls", fullpath, ls_handler, - (FAR void*)((uintptr_t)lsflags)); + ret = nsh_foreach_direntry(vtbl, "ls", fullpath, ls_handler, + (FAR void*)((uintptr_t)lsflags)); if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0) { /* Then recurse to list each directory within the directory */ - ret = foreach_direntry(vtbl, "ls", fullpath, ls_recursive, - (FAR void *)((uintptr_t)lsflags)); + ret = nsh_foreach_direntry(vtbl, "ls", fullpath, ls_recursive, + (FAR void *)((uintptr_t)lsflags)); } } diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c index 17468ec7f..9beebd362 100644 --- a/nshlib/nsh_fsutils.c +++ b/nshlib/nsh_fsutils.c @@ -43,7 +43,9 @@ #include #include #include +#include #include +#include #include "nsh.h" #include "nsh_console.h" @@ -286,3 +288,98 @@ static int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, return ret; } #endif + +/**************************************************************************** + * Name: nsh_foreach_direntry + * + * Description: + * Call the provided 'handler' for each entry found in the directory at + * 'dirpath'. + * + * Input Parameters + * vtbl - The console vtable + * cmd - NSH command name to use in error reporting + * dirpath - The full path to the directory to be traversed + * handler - The handler to be called for each entry of the directory + * pvarg - User provided argument to be passed to the 'handler' + * + * Returned Value: + * Zero (OK) returned on success; -1 (ERROR) returned on failure. + * + ****************************************************************************/ + +#ifdef NSH_HAVE_FOREACH_DIRENTRY +int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *dirpath, + nsh_direntry_handler_t handler, void *pvarg) +{ + DIR *dirp; + int ret = OK; + + /* Open the directory */ + + dirp = opendir(dirpath); + + if (!dirp) + { + /* Failed to open the directory */ + + nsh_output(vtbl, g_fmtnosuch, cmd, "opendir", dirpath); + return ERROR; + } + + /* Read each directory entry */ + + for (;;) + { + FAR struct dirent *entryp = readdir(dirp); + if (!entryp) + { + /* Finished with this directory */ + + break; + } + + /* Call the handler with this directory entry */ + + if (handler(vtbl, dirpath, entryp, pvarg) < 0) + { + /* The handler reported a problem */ + + ret = ERROR; + break; + } + } + + closedir(dirp); + return ret; +} +#endif + +/**************************************************************************** + * Name: nsh_trimdir + * + * Description: + * Skip any trailing '/' characters (unless it is also the leading '/') + * + * Input Parmeters: + * dirpath - The directory path to be trimmed. May be modified! + * + * Returned value + * + ****************************************************************************/ + +#ifdef NSH_HAVE_TRIMDIR +void nsh_trimdir(FAR char *dirpath) +{ + /* Skip any trailing '/' characters (unless it is also the leading '/') */ + + int len = strlen(dirpath) - 1; + while (len > 0 && dirpath[len] == '/') + { + dirpath[len] = '\0'; + len--; + } +} +#endif + diff --git a/nshlib/nsh_proccmds.c b/nshlib/nsh_proccmds.c index ce4883f45..79cebe34c 100644 --- a/nshlib/nsh_proccmds.c +++ b/nshlib/nsh_proccmds.c @@ -132,11 +132,11 @@ static int loadavg(FAR struct nsh_vtbl_s *vtbl, , FAR const char *cmd, #endif /**************************************************************************** - * Name: ps_task + * Name: ps_callback ****************************************************************************/ #ifndef CONFIG_NSH_DISABLE_PS -static void ps_task(FAR struct tcb_s *tcb, FAR void *arg) +static void ps_callback(FAR struct tcb_s *tcb, FAR void *arg) { FAR struct nsh_vtbl_s *vtbl = (FAR struct nsh_vtbl_s*)arg; FAR const char *policy; @@ -219,9 +219,9 @@ static void ps_task(FAR struct tcb_s *tcb, FAR void *arg) ****************************************************************************/ #ifndef CONFIG_NSH_DISABLE_EXEC -int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) { - char *endptr; + FAR char *endptr; uintptr_t addr; addr = (uintptr_t)strtol(argv[1], &endptr, 0); @@ -248,7 +248,7 @@ int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #else nsh_output(vtbl, "PID PRI SCHD TYPE NP STATE NAME\n"); #endif - sched_foreach(ps_task, vtbl); + sched_foreach(ps_callback, vtbl); return OK; } #endif