Move string trimming logic in nsh_fsutils.c as nsh_trimspaces()

This commit is contained in:
Gregory Nutt 2015-11-28 15:01:00 -06:00
parent 64c7e31896
commit 082b452016
3 changed files with 84 additions and 25 deletions

View File

@ -613,8 +613,6 @@
*/
#if defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)
# undef CONFIG_NSH_DISABLE_PS /* 'ps' depends on sched_foreach */
# define CONFIG_NSH_DISABLE_PS 1
# undef CONFIG_NSH_DISABLE_DF /* 'df' depends on foreach_mountpoint */
# define CONFIG_NSH_DISABLE_DF 1
# undef CONFIG_NSH_DISABLE_MKFATFS /* 'mkfatfs' depends on mkfatfs interface */
@ -635,6 +633,11 @@
# define CONFIG_NSH_DISABLE_IFUPDOWN 1
#endif
#if !defined(CONFIG_FS_PROCFS) || defined(CONFIG_FS_PROCFS_EXCLUDE_PROCESS)
# undef CONFIG_NSH_DISABLE_PS /* 'ps' depends on process procfs */
# define CONFIG_NSH_DISABLE_PS 1
#endif
#define NSH_HAVE_CPULOAD 1
#if !defined(CONFIG_FS_PROCFS) || !defined(CONFIG_FS_PROCFS_EXCLUDE_CPULOAD) || \
!defined(CONFIG_SCHED_CPULOAD) || defined(CONFIG_NSH_DISABLE_PS)
@ -647,6 +650,7 @@
#define NSH_HAVE_READFILE 1
#define NSH_HAVE_FOREACH_DIRENTRY 1
#define NSH_HAVE_TRIMDIR 1
#define NSH_HAVE_TRIMSPACES 1
#if CONFIG_NFILE_DESCRIPTORS <= 0
# undef NSH_HAVE_CATFILE
@ -662,22 +666,28 @@
# undef NSH_HAVE_CATFILE
#endif
/* nsh_readfile used by ps command (for load information) */
/* nsh_readfile used by ps command */
#if !defined(NSH_HAVE_CPULOAD)
#if defined(CONFIG_NSH_DISABLE_PS)
# undef NSH_HAVE_READFILE
#endif
/* nsh_foreach_direntry used by the ls command */
/* nsh_foreach_direntry used by the ls and ps commands */
#if defined(CONFIG_NSH_DISABLE_LS)
# undef NSH_HAVE_READFILE
#if defined(CONFIG_NSH_DISABLE_LS) && defined(CONFIG_NSH_DISABLE_PS)
# undef NSH_HAVE_FOREACH_DIRENTRY
#endif
#if defined(CONFIG_NSH_DISABLE_CP)
# undef NSH_HAVE_TRIMDIR
#endif
/* nsh_trimspaces used by the set and ps commands */
#if defined(CONFIG_NSH_DISABLE_SET) && defined(CONFIG_NSH_DISABLE_PS)
# undef NSH_HAVE_TRIMSPACES
#endif
/****************************************************************************
* Public Types
****************************************************************************/
@ -1216,8 +1226,9 @@ int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* Name: nsh_readfile
*
* Description:
* Read a small file into a buffer buffer. An error occurs if the file
* will not fit into the buffer.
* Read a small file into a user-provided buffer. The data is assumed to
* be a string and is guaranteed to be NUL-termined. An error occurs if
* the file content (+terminator) will not fit into the provided 'buffer'.
*
* Input Paramters:
* vtbl - The console vtable
@ -1271,7 +1282,8 @@ int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* Input Parmeters:
* dirpath - The directory path to be trimmed. May be modified!
*
* Returned value
* Returned value:
* None
*
****************************************************************************/
@ -1279,4 +1291,22 @@ int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
void nsh_trimdir(FAR char *dirpath);
#endif
/****************************************************************************
* Name: nsh_trimspaces
*
* Description:
* Trim any leading or trailing spaces from a string.
*
* Input Parmeters:
* str - The sring to be trimmed. May be modified!
*
* Returned value:
* The new string pointer.
*
****************************************************************************/
#ifdef NSH_HAVE_TRIMSPACES
FAR char *nsh_trimspaces(FAR char *str);
#endif
#endif /* __APPS_NSHLIB_NSH_H */

View File

@ -316,23 +316,11 @@ int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *value;
int ndx;
int ret;
/* Strip leading whitespace from the value */
/* Trim whitespace from the value */
for (value = argv[2];
*value != '\0' && isspace(*value);
value++);
/* Strip trailing whitespace from the value */
for (ndx = strlen(value) - 1;
ndx >= 0 && isspace(value[ndx]);
ndx--)
{
value[ndx] = '\0';
}
value = nsh_trimspaces(argv[2]);
/* Set the environment variable */

View File

@ -44,6 +44,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <dirent.h>
@ -364,7 +365,8 @@ int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* Input Parmeters:
* dirpath - The directory path to be trimmed. May be modified!
*
* Returned value
* Returned value:
* None
*
****************************************************************************/
@ -382,3 +384,42 @@ void nsh_trimdir(FAR char *dirpath)
}
#endif
/****************************************************************************
* Name: nsh_trimspaces
*
* Description:
* Trim any leading or trailing spaces from a string.
*
* Input Parmeters:
* str - The sring to be trimmed. May be modified!
*
* Returned value:
* The new string pointer.
*
****************************************************************************/
#ifdef NSH_HAVE_TRIMSPACES
FAR char *nsh_trimspaces(FAR char *str)
{
FAR char *trimmed;
int ndx;
/* Strip leading whitespace from the value */
for (trimmed = str;
*trimmed != '\0' && isspace(*trimmed);
trimmed++);
/* Strip trailing whitespace from the value */
for (ndx = strlen(trimmed) - 1;
ndx >= 0 && isspace(trimmed[ndx]);
ndx--)
{
trimmed[ndx] = '\0';
}
return trimmed;
}
#endif