nshlib: Move trim_dir() and foreach_direntry() from nsh_fscmds.c to nsh_fsutils.c as nsh_trimdir() and nsh_foreach_direntry(), respectively

This commit is contained in:
Gregory Nutt 2015-11-28 08:29:27 -06:00
parent 510f325ae4
commit 92b1eb36c8
5 changed files with 189 additions and 114 deletions

View File

@ -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 */

View File

@ -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:

View File

@ -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));
}
}

View File

@ -43,7 +43,9 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#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

View File

@ -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