From 557ad8e45c68c053b0fab8e6ac5e48a610903a60 Mon Sep 17 00:00:00 2001 From: Pisit Sawangvonganan Date: Mon, 19 Feb 2024 01:55:12 +0700 Subject: [PATCH] shell: devmem: address `cmd_dump` multiple call problem Call `getopt_init()` to reset `state->optind` instead of directly setting `optind = 1`. The `getopt()` function uses `getopt_state` from `getopt_state_get` for state handling, so `getopt_init()` should be called to correctly run the command again. Signed-off-by: Pisit Sawangvonganan --- lib/posix/options/getopt/getopt.h | 2 +- subsys/shell/modules/devmem_service.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/posix/options/getopt/getopt.h b/lib/posix/options/getopt/getopt.h index 4dfcc8b5f57..3ae474f4304 100644 --- a/lib/posix/options/getopt/getopt.h +++ b/lib/posix/options/getopt/getopt.h @@ -48,7 +48,7 @@ struct option { int val; }; -/* Function intializes getopt_state structure for current thread */ +/* Function initializes getopt_state structure for current thread */ void getopt_init(void); /* Function returns getopt_state structure for the current thread. */ diff --git a/subsys/shell/modules/devmem_service.c b/subsys/shell/modules/devmem_service.c index 503ab57b2f5..8b649e8093b 100644 --- a/subsys/shell/modules/devmem_service.c +++ b/subsys/shell/modules/devmem_service.c @@ -19,6 +19,10 @@ #include #include +#ifndef CONFIG_NATIVE_LIBC +extern void getopt_init(void); +#endif + static inline bool is_ascii(uint8_t data) { return (data >= 0x30 && data <= 0x39) || (data >= 0x61 && data <= 0x66) || @@ -101,6 +105,9 @@ static int cmd_dump(const struct shell *sh, size_t argc, char **argv) mem_addr_t addr = -1; optind = 1; +#ifndef CONFIG_NATIVE_LIBC + getopt_init(); +#endif while ((rv = getopt(argc, argv, "a:s:w:")) != -1) { switch (rv) { case 'a':