NSH: Add support for basename and dirname commands

This commit is contained in:
Gregory Nutt 2015-11-23 10:21:15 -06:00
parent ea7aab8afa
commit 178c9780a8
6 changed files with 118 additions and 18 deletions

View File

@ -1455,3 +1455,6 @@
uses only open and ioctl. From Ken Petit (2015-11-23).
* apps/fsutils/smartfs/ and apps/nshlib/: Add configuration option
to supported multiple root directories. From Ken Petit (2015-11-23).
* apps/nshlib: Add support for 'basename' and 'dirname' commands
(2015-11-23).

View File

@ -164,6 +164,11 @@ config NSH_DISABLE_BASE64ENC
default n if !DEFAULT_SMALL
depends on NETUTILS_CODECS && CODECS_BASE64
config NSH_DISABLE_BASENAME
bool "Disable basename"
default y if DEFAULT_SMALL
default n if !DEFAULT_SMALL
config NSH_DISABLE_CAT
bool "Disable cat"
default n
@ -201,6 +206,11 @@ config NSH_DISABLE_DELROUTE
default y if DEFAULT_SMALL
default n if !DEFAULT_SMALL
config NSH_DISABLE_DIRNAME
bool "Disable dirname"
default y if DEFAULT_SMALL
default n if !DEFAULT_SMALL
config NSH_DISABLE_ECHO
bool "Disable echo"
default n

View File

@ -301,6 +301,11 @@ o base64dec [-w] [-f] <string or filepath>
o base64dec [-w] [-f] <string or filepath>
o basename <path> [<suffix>]
Extract the final string from a <path> by removing the preceding path
segments and (optionally) removing any trailing <suffix>.
o break
The break command is only meaningful within the body of the a while or
@ -413,6 +418,11 @@ o df
512 985 2 983 /tmp
nsh>
o dirname <path>
Extract the path string leading up to the full <path> by removing
the final directory or file name.
o echo [<string|$name> [<string|$name>...]]
Copy the sequence of strings and expanded environment variables to
@ -1000,13 +1010,15 @@ Command Dependencies on Configuration Settings
addroute CONFIG_NET && CONFIG_NET_ROUTE
base64dec CONFIG_NETUTILS_CODECS && CONFIG_CODECS_BASE64
base64enc CONFIG_NETUTILS_CODECS && CONFIG_CODECS_BASE64
basename --
break !CONFIG_NSH_DISABLESCRIPT && !CONFIG_NSH_DISABLE_LOOPS
cat CONFIG_NFILE_DESCRIPTORS > 0
cd !CONFIG_DISABLE_ENVIRON && CONFIG_NFILE_DESCRIPTORS > 0
cp CONFIG_NFILE_DESCRIPTORS > 0
dd CONFIG_NFILE_DESCRIPTORS > 0
delrout CONFIG_NET && CONFIG_NET_ROUTE
delroute CONFIG_NET && CONFIG_NET_ROUTE
df !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE (see note 3)
dirname --
echo --
exec --
exit --
@ -1069,23 +1081,24 @@ settings. All of these settings make the configuration of NSH potentially compl
also allow it to squeeze into very small memory footprints.
CONFIG_NSH_DISABLE_ADDROUTE, CONFIG_NSH_DISABLE_BASE64DEC, CONFIG_NSH_DISABLE_BASE64ENC,
CONFIG_NSH_DISABLE_CAT, CONFIG_NSH_DISABLE_CD, CONFIG_NSH_DISABLE_CP,
CONFIG_NSH_DISABLE_DD, CONFIG_NSH_DISABLE_DELROUTE, CONFIG_NSH_DISABLE_DF,
CONFIG_NSH_DISABLE_ECHO, CONFIG_NSH_DISABLE_EXEC, CONFIG_NSH_DISABLE_EXIT,
CONFIG_NSH_DISABLE_FREE, CONFIG_NSH_DISABLE_GET, CONFIG_NSH_DISABLE_HELP,
CONFIG_NSH_DISABLE_HEXDUMP, CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_IFUPDOWN,
CONFIG_NSH_DISABLE_KILL, CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS,
CONFIG_NSH_DISABLE_MD5 CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR,
CONFIG_NSH_DISABLE_MKFATFS, CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD,
CONFIG_NSH_DISABLE_MH, CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW,
CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_NSLOOKUP,
CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING,
CONFIG_NSH_DISABLE_PING6, CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD,
CONFIG_NSH_DISABLE_REBOOT, CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR,
CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN,
CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT,
CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE,
CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD
CONFIG_NSH_DISABLE_BASENAME, CONFIG_NSH_DISABLE_CAT, CONFIG_NSH_DISABLE_CD,
CONFIG_NSH_DISABLE_CP, CONFIG_NSH_DISABLE_DD, CONFIG_NSH_DISABLE_DELROUTE,
CONFIG_NSH_DISABLE_DF, CONFIG_NSH_DISABLE_DIRNAME, CONFIG_NSH_DISABLE_ECHO,
CONFIG_NSH_DISABLE_EXEC, CONFIG_NSH_DISABLE_EXIT, CONFIG_NSH_DISABLE_FREE,
CONFIG_NSH_DISABLE_GET, CONFIG_NSH_DISABLE_HELP, CONFIG_NSH_DISABLE_HEXDUMP,
CONFIG_NSH_DISABLE_IFCONFIG, CONFIG_NSH_DISABLE_IFUPDOWN, CONFIG_NSH_DISABLE_KILL,
CONFIG_NSH_DISABLE_LOSETUP, CONFIG_NSH_DISABLE_LS, CONFIG_NSH_DISABLE_MD5,
CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, CONFIG_NSH_DISABLE_MKFATFS,
CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, CONFIG_NSH_DISABLE_MH,
CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, CONFIG_NSH_DISABLE_MV,
CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_NSLOOKUP, CONFIG_NSH_DISABLE_POWEROFF,
CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING, CONFIG_NSH_DISABLE_PING6,
CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_REBOOT,
CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET,
CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN, CONFIG_NSH_DISABLE_SLEEP,
CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET,
CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USLEEP,
CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD
Verbose help output can be suppressed by defining CONFIG_NSH_HELP_TERSE. In that
case, the help command is still available but will be slightly smaller.

View File

@ -821,9 +821,15 @@ void nsh_usbtrace(void);
/* Shell command handlers */
#ifndef CONFIG_NSH_DISABLE_BASENAME
int cmd_basename(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
int cmd_break(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif
#ifndef CONFIG_NSH_DISABLE_DIRNAME
int cmd_dirname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif
#ifndef CONFIG_NSH_DISABLE_ECHO
int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif

View File

@ -123,6 +123,10 @@ static const struct cmdmap_s g_cmdmap[] =
# endif
#endif
#ifndef CONFIG_NSH_DISABLE_BASENAME
{ "basename", cmd_basename, 2, 3, "<path> [<suffix>]" },
#endif
#if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_LOOPS)
{ "break", cmd_break, 1, 1, NULL },
#endif
@ -144,6 +148,10 @@ static const struct cmdmap_s g_cmdmap[] =
# endif
#endif
#ifndef CONFIG_NSH_DISABLE_DIRNAME
{ "dirname", cmd_dirname, 2, 2, "<path>" },
#endif
#ifndef CONFIG_NSH_DISABLE_DATE
{ "date", cmd_date, 1, 3, "[-s \"MMM DD HH:MM:SS YYYY\"]" },
#endif

View File

@ -539,6 +539,66 @@ static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_basename
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_BASENAME
int cmd_basename(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *filename;
/* Usage: basename <path> [<suffix>]
*
* NOTE: basename() may modify path.
*/
filename = basename(argv[1]);
if (argc > 2)
{
FAR char *suffix = argv[2];
int nndx;
int sndx;
/* Check for any trailing sub-string */
nndx = strlen(filename);
sndx = strlen(suffix);
nndx -= sndx;
if (nndx > 0 && strcmp(&filename[nndx], suffix) == 0)
{
filename[nndx] = '\0';
}
}
/* And output the resulting basename */
nsh_output(vtbl, "%s\n", filename);
return OK;
}
#endif
/****************************************************************************
* Name: cmd_dirname
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_DIRNAME
int cmd_dirname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *filename;
/* Usage: dirname <path>
*
* NOTE: basename() may modify path.
*/
filename = dirname(argv[1]);
nsh_output(vtbl, "%s\n", filename);
return OK;
}
#endif
/****************************************************************************
* Name: cmd_cat
****************************************************************************/