From 14a262001de84ca49a5b828b90e097f271acaf0a Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 30 Sep 2018 10:55:23 -0600 Subject: [PATCH] apps/nshlib: Add support for the 'env' command. --- nshlib/Kconfig | 4 ++++ nshlib/README.txt | 56 ++++++++++++++++++++++++++++++-------------- nshlib/nsh.h | 18 ++++++++++---- nshlib/nsh_command.c | 4 ++++ nshlib/nsh_envcmds.c | 12 ++++++++++ 5 files changed, 72 insertions(+), 22 deletions(-) diff --git a/nshlib/Kconfig b/nshlib/Kconfig index 6ba2ee0f9..57b155859 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -284,6 +284,10 @@ config NSH_DISABLE_ECHO bool "Disable echo" default n +config NSH_DISABLE_ENV + bool "Disable env" + default n + config NSH_DISABLE_EXEC bool "Disable exec" default y if DEFAULT_SMALL diff --git a/nshlib/README.txt b/nshlib/README.txt index 5c3f67d9a..adf17a9b8 100644 --- a/nshlib/README.txt +++ b/nshlib/README.txt @@ -481,6 +481,25 @@ o echo [-n] [ [...]] The -n option will suppress the trailing newline character. +o env + + Show the current address environment. Example: + + nsh> env + PATH=/bin + + nsh> set foo bar + nsh> env + PATH=/bin + foo=bar + + nsh> unset PATH + nsh> env + env + foo=bar + + nsh> + o exec Execute the user logic at address . NSH will pause @@ -1347,6 +1366,7 @@ Command Dependencies on Configuration Settings df !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE (see note 3) dirname -- echo -- + env -- CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_PROCFS && !CONFIG_DISABLE_ENVIRON && !CONFIG_PROCFS_EXCLUDE_ENVIRON exec -- exit -- free -- @@ -1424,24 +1444,24 @@ also allow it to squeeze into very small memory footprints. 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_LN, 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_MODCMDS, CONFIG_NSH_DISABLE_MOUNT, - CONFIG_NSH_DISABLE_MW, CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, - CONFIG_NSH_DISABLE_NSLOOKUP, CONFIG_NSH_DISABLE_PASSWD, CONFIG_NSH_DISABLE_PING6, - CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PUT, - CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_READLINK, CONFIG_NSH_DISABLE_REBOOT, - CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_ROUTE, - CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN, - CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DIABLE_TIME, - CONFIG_NSH_DISABLE_TRUNCATE, CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET, - CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USERADD, - CONFIG_NSH_DISABLE_USERDEL, CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, - CONFIG_NSH_DISABLE_XD + CONFIG_NSH_DISABLE_ENV, 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_LN, + 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_MODCMDS, + CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, CONFIG_NSH_DISABLE_MV, + CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_NSLOOKUP, CONFIG_NSH_DISABLE_PASSWD, + CONFIG_NSH_DISABLE_PING6, CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, + CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_READLINK, + CONFIG_NSH_DISABLE_REBOOT, CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, + CONFIG_NSH_DISABLE_ROUTE, CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, + CONFIG_NSH_DISABLE_SHUTDOWN, CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, + CONFIG_NSH_DIABLE_TIME, CONFIG_NSH_DISABLE_TRUNCATE, CONFIG_NSH_DISABLE_UMOUNT, + CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, + CONFIG_NSH_DISABLE_USERADD, CONFIG_NSH_DISABLE_USERDEL, 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. diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 2b34b7640..ad56f6049 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -677,14 +677,20 @@ # undef NSH_HAVE_TRIMDIR #endif -/* nsh_catfile used by cat, ifconfig, ifup/down, df, free, irqinfo, and mount (with - * no arguments). +#if !defined(CONFIG_FS_PROCFS) || defined(CONFIG_DISABLE_ENVIRON) || \ + defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON) || !defined(NSH_HAVE_CATFILE) +# undef CONFIG_NSH_DISABLE_ENV +# define CONFIG_NSH_DISABLE_ENV 1 +#endif + +/* nsh_catfile used by cat, ifconfig, ifup/down, df, free, env, irqinfo, and + * mount (with no arguments). */ #if !defined(CONFIG_NSH_DISABLE_CAT) && !defined(CONFIG_NSH_DISABLE_IFCONFIG) && \ !defined(CONFIG_NSH_DISABLE_IFUPDOWN) && !defined(CONFIG_NSH_DISABLE_DF) && \ - !defined(CONFIG_NSH_DISABLE_FREE) && !defined(HAVE_IRQINFO) && \ - !defined(HAVE_MOUNT_LIST) + !defined(CONFIG_NSH_DISABLE_FREE) && !defined(CONFIG_NSH_DISABLE_ENV) && \ + !defined(HAVE_IRQINFO) && !defined(HAVE_MOUNT_LIST) # undef NSH_HAVE_CATFILE #endif @@ -1166,6 +1172,10 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif /* !CONFIG_DISABLE_MOUNTPOINT */ #endif /* CONFIG_NFILE_DESCRIPTORS */ +#ifndef CONFIG_NSH_DISABLE_ENV +int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +#endif + #if defined(CONFIG_NET) # if defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP) int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 632ccd302..aa32b8ee4 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -192,6 +192,10 @@ static const struct cmdmap_s g_cmdmap[] = # endif #endif +#ifndef CONFIG_NSH_DISABLE_ENV + { "env", cmd_env, 1, 1, NULL }, +#endif + #ifndef CONFIG_NSH_DISABLE_EXEC { "exec", cmd_exec, 2, 3, "" }, #endif diff --git a/nshlib/nsh_envcmds.c b/nshlib/nsh_envcmds.c index 60964bb56..526a7dd05 100644 --- a/nshlib/nsh_envcmds.c +++ b/nshlib/nsh_envcmds.c @@ -292,6 +292,18 @@ int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) } #endif +/**************************************************************************** + * Name: cmd_env + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_ENV +int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + return nsh_catfile(vtbl, argv[0], + CONFIG_NSH_PROC_MOUNTPOINT "/self/group/env"); +} +#endif + /**************************************************************************** * Name: cmd_pwd ****************************************************************************/