posix: uname: move uname shell from sample

Relocate the `uname` shell implementation from uname sample, so
that it can be reused by other application and the uname sample
only uses POSIX APIs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-01-16 14:33:12 +08:00 committed by Carles Cufí
parent cf13057c8d
commit 48c16f9052
8 changed files with 185 additions and 151 deletions

View file

@ -36,6 +36,7 @@ endif()
zephyr_library()
add_subdirectory_ifdef(CONFIG_GETOPT getopt)
add_subdirectory_ifdef(CONFIG_SHELL shell)
zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c)
zephyr_library_sources_ifdef(CONFIG_FNMATCH fnmatch.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_API perror.c)

View file

@ -55,3 +55,5 @@ source "lib/posix/Kconfig.signal"
source "lib/posix/Kconfig.spinlock"
source "lib/posix/Kconfig.timer"
source "lib/posix/Kconfig.uname"
rsource "shell/Kconfig"

View file

@ -0,0 +1,4 @@
# Copyright (c) 2024 Meta
# SPDX-License-Identifier: Apache-2.0
zephyr_library_sources_ifdef(CONFIG_POSIX_UNAME_SHELL uname.c)

8
lib/posix/shell/Kconfig Normal file
View file

@ -0,0 +1,8 @@
# Copyright (c) 2024 Meta
# SPDX-License-Identifier: Apache-2.0
if SHELL
rsource "Kconfig.uname"
endif # SHELL

View file

@ -0,0 +1,12 @@
# Copyright (c) 2024 Meta
# SPDX-License-Identifier: Apache-2.0
if POSIX_UNAME
config POSIX_UNAME_SHELL
bool "Support for `uname` command"
select SHELL_GETOPT
help
Support for `uname` command in the terminal.
endif # POSIX_UNAME

157
lib/posix/shell/uname.c Normal file
View file

@ -0,0 +1,157 @@
/*
* Copyright (c) 2024 Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <unistd.h>
#include <zephyr/posix/sys/utsname.h>
#include <zephyr/shell/shell.h>
#define UNAME_KERNEL BIT(0)
#define UNAME_NODE BIT(1)
#define UNAME_RELEASE BIT(2)
#define UNAME_VERSION BIT(3)
#define UNAME_MACHINE BIT(4)
#define UNAME_PLATFORM BIT(5)
#define UNAME_UNKNOWN BIT(6)
#define UNAME_ALL \
(UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM)
static void uname_print_usage(const struct shell *sh)
{
shell_print(sh, "usage: uname [-asonrvmpi]");
}
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
{
struct getopt_state *state = getopt_state_get();
struct utsname info;
unsigned int set;
int option;
char badarg = 0;
int ret;
set = 0;
/* Get the uname options */
optind = 1;
while ((option = getopt(argc, argv, "asonrvmpi")) != -1) {
switch (option) {
case 'a':
set = UNAME_ALL;
break;
case 'o':
case 's':
set |= UNAME_KERNEL;
break;
case 'n':
set |= UNAME_NODE;
break;
case 'r':
set |= UNAME_RELEASE;
break;
case 'v':
set |= UNAME_VERSION;
break;
case 'm':
set |= UNAME_MACHINE;
break;
case 'p':
if (set != UNAME_ALL) {
set |= UNAME_UNKNOWN;
}
break;
case 'i':
set |= UNAME_PLATFORM;
break;
case '?':
default:
badarg = (char)state->optopt;
break;
}
}
if (argc != optind) {
shell_error(sh, "extra operand %s", argv[optind]);
uname_print_usage(sh);
return -1;
}
/* If a bad argument was encountered, then return without processing the
* command
*/
if (badarg != 0) {
shell_error(sh, "uname: illegal option -- %c", badarg);
uname_print_usage(sh);
return -1;
}
/* 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) {
shell_error(sh, "cannot get system name");
return -1;
}
/* Process each option */
/* print the kernel/operating system name */
if (set & UNAME_KERNEL) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.sysname);
}
/* Print nodename */
if (set & UNAME_NODE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.nodename);
}
/* Print the kernel release */
if (set & UNAME_RELEASE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.release);
}
/* Print the kernel version */
if (set & UNAME_VERSION) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.version);
}
/* Print the machine hardware name */
if (set & UNAME_MACHINE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.machine);
}
/* Print the machine platform name */
if (set & UNAME_PLATFORM) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", CONFIG_BOARD);
}
/* Print "unknown" */
if (set & UNAME_UNKNOWN) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", "unknown");
}
shell_fprintf(sh, SHELL_NORMAL, "\n");
return 0;
}
SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler);

View file

@ -1,4 +1,4 @@
CONFIG_POSIX_API=y
CONFIG_SHELL=y
CONFIG_SHELL_GETOPT=y
CONFIG_POSIX_UNAME_SHELL=y
CONFIG_MP_MAX_NUM_CPUS=1

View file

@ -5,11 +5,8 @@
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <zephyr/shell/shell.h>
int main(void)
{
struct utsname info;
@ -25,150 +22,3 @@ int main(void)
return 0;
}
#define UNAME_KERNEL BIT(0)
#define UNAME_NODE BIT(1)
#define UNAME_RELEASE BIT(2)
#define UNAME_VERSION BIT(3)
#define UNAME_MACHINE BIT(4)
#define UNAME_PLATFORM BIT(5)
#define UNAME_UNKNOWN BIT(6)
#define UNAME_ALL \
(UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM)
static void uname_print_usage(const struct shell *sh)
{
shell_print(sh, "usage: uname [-asonrvmpi]");
}
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
{
struct getopt_state *state = getopt_state_get();
struct utsname info;
unsigned int set;
int option;
char badarg = 0;
int ret;
set = 0;
/* Get the uname options */
optind = 1;
while ((option = getopt(argc, argv, "asonrvmpi")) != -1) {
switch (option) {
case 'a':
set = UNAME_ALL;
break;
case 'o':
case 's':
set |= UNAME_KERNEL;
break;
case 'n':
set |= UNAME_NODE;
break;
case 'r':
set |= UNAME_RELEASE;
break;
case 'v':
set |= UNAME_VERSION;
break;
case 'm':
set |= UNAME_MACHINE;
break;
case 'p':
if (set != UNAME_ALL) {
set |= UNAME_UNKNOWN;
}
break;
case 'i':
set |= UNAME_PLATFORM;
break;
case '?':
default:
badarg = (char)state->optopt;
break;
}
}
if (argc != optind) {
shell_error(sh, "extra operand %s", argv[optind]);
uname_print_usage(sh);
return -1;
}
/* If a bad argument was encountered, then return without processing the
* command
*/
if (badarg != 0) {
shell_error(sh, "uname: illegal option -- %c", badarg);
uname_print_usage(sh);
return -1;
}
/* 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) {
shell_error(sh, "cannot get system name");
return -1;
}
/* Process each option */
/* print the kernel/operating system name */
if (set & UNAME_KERNEL) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.sysname);
}
/* Print nodename */
if (set & UNAME_NODE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.nodename);
}
/* Print the kernel release */
if (set & UNAME_RELEASE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.release);
}
/* Print the kernel version */
if (set & UNAME_VERSION) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.version);
}
/* Print the machine hardware name */
if (set & UNAME_MACHINE) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.machine);
}
/* Print the machine platform name */
if (set & UNAME_PLATFORM) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", CONFIG_BOARD);
}
/* Print "unknown" */
if (set & UNAME_UNKNOWN) {
shell_fprintf(sh, SHELL_NORMAL, "%s ", "unknown");
}
shell_fprintf(sh, SHELL_NORMAL, "\n");
return 0;
}
SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler);