Add support for the NSH uname command

This commit is contained in:
Gregory Nutt 2015-07-04 15:13:11 -06:00
parent f9afefe7e9
commit d710b7c3b7
6 changed files with 210 additions and 3 deletions

View File

@ -1333,4 +1333,5 @@
(2015-07-04).
* apps/system/poweroff: Remove the system poweroff command. This is
replaced with the NSH shutdown commandi (2015-07-02).
* apps/nshlib: Add support for a uname command (2015-07-04).

View File

@ -362,6 +362,11 @@ config NSH_DISABLE_UMOUNT
bool "Disable umount"
default n
config NSH_DISABLE_UNAME
bool "Disable uname"
default y if DEFAULT_SMALL
default n if !DEFAULT_SMALL
config NSH_DISABLE_UNSET
bool "Disable unset"
default n

View File

@ -924,9 +924,27 @@ o unset <name>
nsh>
o urldecode [-f] <string or filepath>
o urldecode [-f] <string or filepath>
o urlencode [-f] <string or filepath>
o urlencode [-f] <string or filepath>
o uname [-a | -imnoprsv]
Print certain system information. With no options, the output is the same as -s.
-a Print all information, in the following order, except omit -p and -i if unknown:
-s, -o, Print the operating system name (NuttX)
-n Print the network node hostname (only availabel if CONFIG_NET=y)
-r Print the kernel release
-v Print the kernel version
-m Print the machine hardware name
-p, -i Print "unknown"
o usleep <usec>
@ -1020,6 +1038,7 @@ Command Dependencies on Configuration Settings
sleep !CONFIG_DISABLE_SIGNALS
test !CONFIG_NSH_DISABLESCRIPT
umount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE
uname !CONFIG_NSH_DISABLE_UNAME
unset !CONFIG_DISABLE_ENVIRON
urldecode CONFIG_NETUTILS_CODECS && CONFIG_CODECS_URLCODE
urlencode CONFIG_NETUTILS_CODECS && CONFIG_CODECS_URLCODE

View File

@ -1001,6 +1001,10 @@ void nsh_usbtrace(void);
int cmd_shutdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif
#ifndef CONFIG_NSH_DISABLE_UNAME
int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif
#ifndef CONFIG_DISABLE_ENVIRON
# ifndef CONFIG_NSH_DISABLE_SET
int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);

View File

@ -401,6 +401,14 @@ static const struct cmdmap_s g_cmdmap[] =
{ "true", cmd_true, 1, 1, NULL },
#endif
#ifndef CONFIG_NSH_DISABLE_UNAME
#if CONFIG_NET
{ "uname", cmd_uname, 1, 7, "[-a | -imnoprsv]" },
#else
{ "uname", cmd_uname, 1, 7, "[-a | -imoprsv]" },
#endif
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_READABLE)
# ifndef CONFIG_NSH_DISABLE_UMOUNT
{ "umount", cmd_umount, 2, 2, "<dir-path>" },

View File

@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <sys/boardctl.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <errno.h>
@ -47,9 +48,24 @@
#include "nsh_console.h"
/****************************************************************************
* Definitions
* Pre-processor Definitions
****************************************************************************/
#define UNAME_KERNEL (1 << 0)
#define UNAME_NODE (1 << 1)
#define UNAME_RELEASE (1 << 2)
#define UNAME_VERISON (1 << 3)
#define UNAME_MACHINE (1 << 4)
#define UNAME_UNKNOWN (1 << 5)
#ifdef CONFIG_NET
# define UNAME_ALL (UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | \
UNAME_VERISON | UNAME_MACHINE)
#else
# define UNAME_ALL (UNAME_KERNEL | UNAME_RELEASE | UNAME_VERISON | \
UNAME_MACHINE)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
@ -203,3 +219,157 @@ int cmd_reboot(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
return ERROR;
}
#endif
/****************************************************************************
* Name: cmd_uname
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_UNAME
int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *str;
struct utsname info;
unsigned int set;
int option;
bool badarg;
bool first;
int ret;
int i;
/* Get the ping options */
set = 0;
while ((option = getopt(argc, argv, "asonrvmpi")) != ERROR)
{
switch (option)
{
case 'a':
set = UNAME_ALL;
break;
case 'o':
case 's':
set |= UNAME_KERNEL;
break;
#if CONFIG_NET
case 'n':
set |= UNAME_NODE;
break;
#endif
case 'r':
set |= UNAME_RELEASE;
break;
case 'v':
set |= UNAME_VERISON;
break;
case 'm':
set |= UNAME_MACHINE;
break;
case 'p':
case 'i':
if (set != UNAME_ALL)
{
set |= UNAME_UNKNOWN;
}
break;
case '?':
default:
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
badarg = true;
break;
}
}
/* If a bad argument was encountered, then return without processing the
* command
*/
if (badarg)
{
return ERROR;
}
/* If nothing is provided on the command line, the default is -s */
if (set == 0)
{
set = UNAME_KERNEL;
}
/* Get uname data */
ret = uname(&info);
if (ret < 0)
{
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "uname", NSH_ERRNO);
return ERROR;
}
/* Process each option */
first = true;
for (i = 0; set != 0; i++)
{
unsigned int mask = (1 << i);
if ((set & mask) != 0)
{
set &= ~mask;
switch (i)
{
case 0: /* print the kernel/operating system name */
str = info.sysname;
str[SYS_NAMELEN-1] = '\0';
break;
#if CONFIG_NET
case 1: /* Print noname */
str = info.nodename;
str[HOST_NAME_MAX-1] = '\0';
break;
#endif
case 2: /* Print the kernel release */
str = info.release;
str[SYS_NAMELEN-1] = '\0';
break;
case 3: /* Print the kernel version */
str = info.version;
str[SYS_NAMELEN-1] = '\0';
break;
case 4: /* Print the machine hardware name */
str = info.machine;
str[SYS_NAMELEN-1] = '\0';
break;
case 5: /* Print invalid */
str = "unknown";
break;
default:
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
if (!first)
{
nsh_output(vtbl, " ");
}
nsh_output(vtbl, str);
first = false;
}
}
nsh_output(vtbl, "\n");
return OK;
}
#endif