Skip to content

Commit 48c16f9

Browse files
ycsincarlescufi
authored andcommitted
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 <[email protected]>
1 parent cf13057 commit 48c16f9

File tree

8 files changed

+185
-151
lines changed

8 files changed

+185
-151
lines changed

lib/posix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ endif()
3636

3737
zephyr_library()
3838
add_subdirectory_ifdef(CONFIG_GETOPT getopt)
39+
add_subdirectory_ifdef(CONFIG_SHELL shell)
3940
zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c)
4041
zephyr_library_sources_ifdef(CONFIG_FNMATCH fnmatch.c)
4142
zephyr_library_sources_ifdef(CONFIG_POSIX_API perror.c)

lib/posix/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ source "lib/posix/Kconfig.signal"
5555
source "lib/posix/Kconfig.spinlock"
5656
source "lib/posix/Kconfig.timer"
5757
source "lib/posix/Kconfig.uname"
58+
59+
rsource "shell/Kconfig"

lib/posix/shell/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library_sources_ifdef(CONFIG_POSIX_UNAME_SHELL uname.c)

lib/posix/shell/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if SHELL
5+
6+
rsource "Kconfig.uname"
7+
8+
endif # SHELL

lib/posix/shell/Kconfig.uname

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2024 Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if POSIX_UNAME
5+
6+
config POSIX_UNAME_SHELL
7+
bool "Support for `uname` command"
8+
select SHELL_GETOPT
9+
help
10+
Support for `uname` command in the terminal.
11+
12+
endif # POSIX_UNAME

lib/posix/shell/uname.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2024 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <unistd.h>
8+
9+
#include <zephyr/posix/sys/utsname.h>
10+
#include <zephyr/shell/shell.h>
11+
12+
#define UNAME_KERNEL BIT(0)
13+
#define UNAME_NODE BIT(1)
14+
#define UNAME_RELEASE BIT(2)
15+
#define UNAME_VERSION BIT(3)
16+
#define UNAME_MACHINE BIT(4)
17+
#define UNAME_PLATFORM BIT(5)
18+
#define UNAME_UNKNOWN BIT(6)
19+
#define UNAME_ALL \
20+
(UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM)
21+
22+
static void uname_print_usage(const struct shell *sh)
23+
{
24+
shell_print(sh, "usage: uname [-asonrvmpi]");
25+
}
26+
27+
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
28+
{
29+
struct getopt_state *state = getopt_state_get();
30+
struct utsname info;
31+
unsigned int set;
32+
int option;
33+
char badarg = 0;
34+
int ret;
35+
36+
set = 0;
37+
38+
/* Get the uname options */
39+
40+
optind = 1;
41+
while ((option = getopt(argc, argv, "asonrvmpi")) != -1) {
42+
switch (option) {
43+
case 'a':
44+
set = UNAME_ALL;
45+
break;
46+
47+
case 'o':
48+
case 's':
49+
set |= UNAME_KERNEL;
50+
break;
51+
52+
case 'n':
53+
set |= UNAME_NODE;
54+
break;
55+
56+
case 'r':
57+
set |= UNAME_RELEASE;
58+
break;
59+
60+
case 'v':
61+
set |= UNAME_VERSION;
62+
break;
63+
64+
case 'm':
65+
set |= UNAME_MACHINE;
66+
break;
67+
68+
case 'p':
69+
if (set != UNAME_ALL) {
70+
set |= UNAME_UNKNOWN;
71+
}
72+
break;
73+
74+
case 'i':
75+
set |= UNAME_PLATFORM;
76+
break;
77+
78+
case '?':
79+
default:
80+
badarg = (char)state->optopt;
81+
break;
82+
}
83+
}
84+
85+
if (argc != optind) {
86+
shell_error(sh, "extra operand %s", argv[optind]);
87+
uname_print_usage(sh);
88+
return -1;
89+
}
90+
91+
/* If a bad argument was encountered, then return without processing the
92+
* command
93+
*/
94+
95+
if (badarg != 0) {
96+
shell_error(sh, "uname: illegal option -- %c", badarg);
97+
uname_print_usage(sh);
98+
return -1;
99+
}
100+
101+
/* If nothing is provided on the command line, the default is -s */
102+
103+
if (set == 0) {
104+
set = UNAME_KERNEL;
105+
}
106+
107+
/* Get uname data */
108+
109+
ret = uname(&info);
110+
if (ret < 0) {
111+
shell_error(sh, "cannot get system name");
112+
return -1;
113+
}
114+
115+
/* Process each option */
116+
117+
/* print the kernel/operating system name */
118+
if (set & UNAME_KERNEL) {
119+
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.sysname);
120+
}
121+
122+
/* Print nodename */
123+
if (set & UNAME_NODE) {
124+
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.nodename);
125+
}
126+
127+
/* Print the kernel release */
128+
if (set & UNAME_RELEASE) {
129+
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.release);
130+
}
131+
132+
/* Print the kernel version */
133+
if (set & UNAME_VERSION) {
134+
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.version);
135+
}
136+
137+
/* Print the machine hardware name */
138+
if (set & UNAME_MACHINE) {
139+
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.machine);
140+
}
141+
142+
/* Print the machine platform name */
143+
if (set & UNAME_PLATFORM) {
144+
shell_fprintf(sh, SHELL_NORMAL, "%s ", CONFIG_BOARD);
145+
}
146+
147+
/* Print "unknown" */
148+
if (set & UNAME_UNKNOWN) {
149+
shell_fprintf(sh, SHELL_NORMAL, "%s ", "unknown");
150+
}
151+
152+
shell_fprintf(sh, SHELL_NORMAL, "\n");
153+
154+
return 0;
155+
}
156+
157+
SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler);

samples/posix/uname/prj.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
CONFIG_POSIX_API=y
22
CONFIG_SHELL=y
3-
CONFIG_SHELL_GETOPT=y
3+
CONFIG_POSIX_UNAME_SHELL=y
44
CONFIG_MP_MAX_NUM_CPUS=1

samples/posix/uname/src/main.c

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
*/
66

77
#include <stdio.h>
8-
#include <unistd.h>
98
#include <sys/utsname.h>
109

11-
#include <zephyr/shell/shell.h>
12-
1310
int main(void)
1411
{
1512
struct utsname info;
@@ -25,150 +22,3 @@ int main(void)
2522

2623
return 0;
2724
}
28-
29-
#define UNAME_KERNEL BIT(0)
30-
#define UNAME_NODE BIT(1)
31-
#define UNAME_RELEASE BIT(2)
32-
#define UNAME_VERSION BIT(3)
33-
#define UNAME_MACHINE BIT(4)
34-
#define UNAME_PLATFORM BIT(5)
35-
#define UNAME_UNKNOWN BIT(6)
36-
#define UNAME_ALL \
37-
(UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM)
38-
39-
static void uname_print_usage(const struct shell *sh)
40-
{
41-
shell_print(sh, "usage: uname [-asonrvmpi]");
42-
}
43-
44-
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
45-
{
46-
struct getopt_state *state = getopt_state_get();
47-
struct utsname info;
48-
unsigned int set;
49-
int option;
50-
char badarg = 0;
51-
int ret;
52-
53-
set = 0;
54-
55-
/* Get the uname options */
56-
57-
optind = 1;
58-
while ((option = getopt(argc, argv, "asonrvmpi")) != -1) {
59-
switch (option) {
60-
case 'a':
61-
set = UNAME_ALL;
62-
break;
63-
64-
case 'o':
65-
case 's':
66-
set |= UNAME_KERNEL;
67-
break;
68-
69-
case 'n':
70-
set |= UNAME_NODE;
71-
break;
72-
73-
case 'r':
74-
set |= UNAME_RELEASE;
75-
break;
76-
77-
case 'v':
78-
set |= UNAME_VERSION;
79-
break;
80-
81-
case 'm':
82-
set |= UNAME_MACHINE;
83-
break;
84-
85-
case 'p':
86-
if (set != UNAME_ALL) {
87-
set |= UNAME_UNKNOWN;
88-
}
89-
break;
90-
91-
case 'i':
92-
set |= UNAME_PLATFORM;
93-
break;
94-
95-
case '?':
96-
default:
97-
badarg = (char)state->optopt;
98-
break;
99-
}
100-
}
101-
102-
if (argc != optind) {
103-
shell_error(sh, "extra operand %s", argv[optind]);
104-
uname_print_usage(sh);
105-
return -1;
106-
}
107-
108-
/* If a bad argument was encountered, then return without processing the
109-
* command
110-
*/
111-
112-
if (badarg != 0) {
113-
shell_error(sh, "uname: illegal option -- %c", badarg);
114-
uname_print_usage(sh);
115-
return -1;
116-
}
117-
118-
/* If nothing is provided on the command line, the default is -s */
119-
120-
if (set == 0) {
121-
set = UNAME_KERNEL;
122-
}
123-
124-
/* Get uname data */
125-
126-
ret = uname(&info);
127-
if (ret < 0) {
128-
shell_error(sh, "cannot get system name");
129-
return -1;
130-
}
131-
132-
/* Process each option */
133-
134-
/* print the kernel/operating system name */
135-
if (set & UNAME_KERNEL) {
136-
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.sysname);
137-
}
138-
139-
/* Print nodename */
140-
if (set & UNAME_NODE) {
141-
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.nodename);
142-
}
143-
144-
/* Print the kernel release */
145-
if (set & UNAME_RELEASE) {
146-
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.release);
147-
}
148-
149-
/* Print the kernel version */
150-
if (set & UNAME_VERSION) {
151-
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.version);
152-
}
153-
154-
/* Print the machine hardware name */
155-
if (set & UNAME_MACHINE) {
156-
shell_fprintf(sh, SHELL_NORMAL, "%s ", info.machine);
157-
}
158-
159-
/* Print the machine platform name */
160-
if (set & UNAME_PLATFORM) {
161-
shell_fprintf(sh, SHELL_NORMAL, "%s ", CONFIG_BOARD);
162-
}
163-
164-
/* Print "unknown" */
165-
if (set & UNAME_UNKNOWN) {
166-
shell_fprintf(sh, SHELL_NORMAL, "%s ", "unknown");
167-
}
168-
169-
shell_fprintf(sh, SHELL_NORMAL, "\n");
170-
171-
return 0;
172-
}
173-
174-
SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler);

0 commit comments

Comments
 (0)