Skip to content

Commit af750b9

Browse files
committed
treewide: Use sys_getopt _r variants
Switch to the newlib-inspired API where users provide explicit storage for the getopt state. Signed-off-by: Keith Packard <[email protected]>
1 parent fd2ed94 commit af750b9

File tree

9 files changed

+235
-271
lines changed

9 files changed

+235
-271
lines changed

lib/posix/shell/uname.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void uname_print_usage(const struct shell *sh)
4242

4343
static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
4444
{
45-
struct sys_getopt_state *state = sys_getopt_state_get();
45+
struct sys_getopt_state state = SYS_GETOPT_STATE_INITIALIZER;
4646
struct utsname info;
4747
unsigned int set;
4848
int option;
@@ -53,8 +53,7 @@ static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
5353

5454
/* Get the uname options */
5555

56-
optind = 1;
57-
while ((option = sys_getopt(argc, argv, "asonrvmpi")) != -1) {
56+
while ((option = sys_getopt_r(argc, argv, "asonrvmpi", &state)) != -1) {
5857
switch (option) {
5958
case 'a':
6059
set = UNAME_ALL;
@@ -93,13 +92,13 @@ static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv)
9392

9493
case '?':
9594
default:
96-
badarg = (char)state->optopt;
95+
badarg = (char)state.optopt;
9796
break;
9897
}
9998
}
10099

101-
if (argc != optind) {
102-
shell_error(sh, "uname: extra operand %s", argv[optind]);
100+
if (argc != state.optind) {
101+
shell_error(sh, "uname: extra operand %s", argv[state.optind]);
103102
uname_print_usage(sh);
104103
return -1;
105104
}

samples/shields/npm6001_ek/src/main.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include <zephyr/shell/shell.h>
1616
#include <zephyr/sys/printk.h>
1717

18-
#include <zephyr/sys/sys_getopt.h>
19-
2018
struct regulators_map {
2119
const char *name;
2220
const struct device *dev;
@@ -312,27 +310,29 @@ static int cmd_gpio_configure(const struct shell *sh, size_t argc, char **argv)
312310
{NULL, 0, NULL, 0},
313311
};
314312

313+
struct sys_getopt_state state = SYS_GETOPT_STATE_INITIALIZER;
314+
315315
high_drive = 0;
316316
pull_down = 0;
317317
cmos = 0;
318318

319-
while ((opt = sys_getopt_long(argc, argv, "p:d:", long_options,
320-
&long_index)) != -1) {
319+
while ((opt = sys_getopt_long_r(argc, argv, "p:d:", long_options,
320+
&long_index, &state)) != -1) {
321321
switch (opt) {
322322
case 0:
323323
/* options setting a flag, do nothing */
324324
break;
325325
case 'p':
326-
pin = atoi(sys_getopt_optarg);
326+
pin = atoi(state.optarg);
327327
break;
328328
case 'd':
329-
if (strcmp(sys_getopt_optarg, "in") == 0) {
329+
if (strcmp(state.optarg, "in") == 0) {
330330
flags |= GPIO_INPUT;
331-
} else if (strcmp(sys_getopt_optarg, "out") == 0) {
331+
} else if (strcmp(state.optarg, "out") == 0) {
332332
flags |= GPIO_OUTPUT;
333-
} else if (strcmp(sys_getopt_optarg, "outh") == 0) {
333+
} else if (strcmp(state.optarg, "outh") == 0) {
334334
flags |= GPIO_OUTPUT_HIGH;
335-
} else if (strcmp(sys_getopt_optarg, "outl") == 0) {
335+
} else if (strcmp(state.optarg, "outl") == 0) {
336336
flags |= GPIO_OUTPUT_LOW;
337337
}
338338
break;

samples/subsys/shell/shell_module/src/main.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
#include <zephyr/drivers/uart.h>
1313
#include <ctype.h>
1414

15-
#ifdef CONFIG_NATIVE_LIBC
16-
#include <unistd.h>
17-
#else
18-
#include <zephyr/posix/unistd.h>
19-
#endif
20-
2115
LOG_MODULE_REGISTER(app);
2216

2317
extern void foo(void);
@@ -109,14 +103,13 @@ static int cmd_demo_board(const struct shell *sh, size_t argc, char **argv)
109103
static int cmd_demo_getopt_ts(const struct shell *sh, size_t argc,
110104
char **argv)
111105
{
112-
struct sys_getopt_state *state;
113106
char *cvalue = NULL;
114107
int aflag = 0;
115108
int bflag = 0;
116109
int c;
110+
struct sys_getopt_state state = SYS_GETOPT_STATE_INITIALIZER;
117111

118-
while ((c = sys_getopt(argc, argv, "abhc:")) != -1) {
119-
state = sys_getopt_state_get();
112+
while ((c = sys_getopt_r(argc, argv, "abhc:", &state)) != -1) {
120113
switch (c) {
121114
case 'a':
122115
aflag = 1;
@@ -125,7 +118,7 @@ static int cmd_demo_getopt_ts(const struct shell *sh, size_t argc,
125118
bflag = 1;
126119
break;
127120
case 'c':
128-
cvalue = state->optarg;
121+
cvalue = state.optarg;
129122
break;
130123
case 'h':
131124
/* When getopt is active shell is not parsing
@@ -135,18 +128,18 @@ static int cmd_demo_getopt_ts(const struct shell *sh, size_t argc,
135128
shell_help(sh);
136129
return SHELL_CMD_HELP_PRINTED;
137130
case '?':
138-
if (state->optopt == 'c') {
131+
if (state.optopt == 'c') {
139132
shell_print(sh,
140133
"Option -%c requires an argument.",
141-
state->optopt);
142-
} else if (isprint(state->optopt) != 0) {
134+
state.optopt);
135+
} else if (isprint(state.optopt) != 0) {
143136
shell_print(sh,
144137
"Unknown option `-%c'.",
145-
state->optopt);
138+
state.optopt);
146139
} else {
147140
shell_print(sh,
148141
"Unknown option character `\\x%x'.",
149-
state->optopt);
142+
state.optopt);
150143
}
151144
return 1;
152145
default:
@@ -165,8 +158,9 @@ static int cmd_demo_getopt(const struct shell *sh, size_t argc,
165158
int aflag = 0;
166159
int bflag = 0;
167160
int c;
161+
struct sys_getopt_state state = SYS_GETOPT_STATE_INITIALIZER;
168162

169-
while ((c = sys_getopt(argc, argv, "abhc:")) != -1) {
163+
while ((c = sys_getopt_r(argc, argv, "abhc:", &state)) != -1) {
170164
switch (c) {
171165
case 'a':
172166
aflag = 1;
@@ -175,7 +169,7 @@ static int cmd_demo_getopt(const struct shell *sh, size_t argc,
175169
bflag = 1;
176170
break;
177171
case 'c':
178-
cvalue = sys_getopt_optarg;
172+
cvalue = state.optarg;
179173
break;
180174
case 'h':
181175
/* When getopt is active shell is not parsing
@@ -185,17 +179,17 @@ static int cmd_demo_getopt(const struct shell *sh, size_t argc,
185179
shell_help(sh);
186180
return SHELL_CMD_HELP_PRINTED;
187181
case '?':
188-
if (sys_getopt_optopt == 'c') {
182+
if (state.optopt == 'c') {
189183
shell_print(sh,
190184
"Option -%c requires an argument.",
191-
sys_getopt_optopt);
192-
} else if (isprint(sys_getopt_optopt) != 0) {
185+
state.optopt);
186+
} else if (isprint(state.optopt) != 0) {
193187
shell_print(sh, "Unknown option `-%c'.",
194-
sys_getopt_optopt);
188+
state.optopt);
195189
} else {
196190
shell_print(sh,
197191
"Unknown option character `\\x%x'.",
198-
sys_getopt_optopt);
192+
state.optopt);
199193
}
200194
return 1;
201195
default:

subsys/crc/crc_shell.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
#include <stdbool.h>
99
#include <stdint.h>
1010
#include <stdlib.h>
11-
#ifdef CONFIG_NATIVE_LIBC
12-
#include <unistd.h>
13-
#else
14-
#include <zephyr/posix/unistd.h>
15-
#endif
1611

1712
#include <zephyr/kernel.h>
1813
#include <zephyr/shell/shell.h>
@@ -79,9 +74,9 @@ static int cmd_crc(const struct shell *sh, size_t argc, char **argv)
7974
void *addr = (void *)-1;
8075
enum crc_type type = CRC32_IEEE;
8176

82-
sys_getopt_optind = 1;
77+
struct sys_getopt_state state = SYS_GETOPT_STATE_INITIALIZER;
8378

84-
while ((rv = sys_getopt(argc, argv, "fhlp:rs:t:")) != -1) {
79+
while ((rv = sys_getopt_r(argc, argv, "fhlp:rs:t:", &state)) != -1) {
8580
switch (rv) {
8681
case 'f':
8782
first = true;
@@ -93,26 +88,26 @@ static int cmd_crc(const struct shell *sh, size_t argc, char **argv)
9388
last = true;
9489
break;
9590
case 'p':
96-
poly = (size_t)strtoul(sys_getopt_optarg, NULL, 16);
91+
poly = (size_t)strtoul(state.optarg, NULL, 16);
9792
if (poly == 0 && errno == EINVAL) {
98-
shell_error(sh, "invalid seed '%s'", sys_getopt_optarg);
93+
shell_error(sh, "invalid seed '%s'", state.optarg);
9994
return -EINVAL;
10095
}
10196
break;
10297
case 'r':
10398
reflect = true;
10499
break;
105100
case 's':
106-
seed = (size_t)strtoul(sys_getopt_optarg, NULL, 16);
101+
seed = (size_t)strtoul(state.optarg, NULL, 16);
107102
if (seed == 0 && errno == EINVAL) {
108-
shell_error(sh, "invalid seed '%s'", sys_getopt_optarg);
103+
shell_error(sh, "invalid seed '%s'", state.optarg);
109104
return -EINVAL;
110105
}
111106
break;
112107
case 't':
113-
type = string_to_crc_type(sys_getopt_optarg);
108+
type = string_to_crc_type(state.optarg);
114109
if (type == -1) {
115-
shell_error(sh, "invalid type '%s'", sys_getopt_optarg);
110+
shell_error(sh, "invalid type '%s'", state.optarg);
116111
return -EINVAL;
117112
}
118113
break;
@@ -123,21 +118,21 @@ static int cmd_crc(const struct shell *sh, size_t argc, char **argv)
123118
}
124119
}
125120

126-
if (sys_getopt_optind + 2 > argc) {
121+
if (state.optind + 2 > argc) {
127122
shell_error(sh, "'address' and 'size' arguments are mandatory");
128123
usage(sh);
129124
return -EINVAL;
130125
}
131126

132-
addr = (void *)strtoul(argv[sys_getopt_optind], NULL, 16);
127+
addr = (void *)strtoul(argv[state.optind], NULL, 16);
133128
if (addr == 0 && errno == EINVAL) {
134-
shell_error(sh, "invalid address '%s'", argv[sys_getopt_optind]);
129+
shell_error(sh, "invalid address '%s'", argv[state.optind]);
135130
return -EINVAL;
136131
}
137132

138-
size = (size_t)strtoul(argv[sys_getopt_optind + 1], NULL, 0);
133+
size = (size_t)strtoul(argv[state.optind + 1], NULL, 0);
139134
if (size == 0 && errno == EINVAL) {
140-
shell_error(sh, "invalid size '%s'", argv[sys_getopt_optind + 1]);
135+
shell_error(sh, "invalid size '%s'", argv[state.optind + 1]);
141136
return -EINVAL;
142137
}
143138

0 commit comments

Comments
 (0)