From 59c21c7aeec7401e5dca3b5c0c689ea21be9f5dc Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 8 Aug 2024 12:04:00 +0800 Subject: [PATCH] nsh: support watch command Signed-off-by: yinshengkai --- nshlib/Kconfig | 4 +++ nshlib/nsh.h | 4 +++ nshlib/nsh_command.c | 5 ++++ nshlib/nsh_timcmds.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/nshlib/Kconfig b/nshlib/Kconfig index 2492673f5..57328e2af 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -709,6 +709,10 @@ config NSH_DISABLE_WAIT default DEFAULT_SMALL depends on SCHED_WAITPID +config NSH_DISABLE_WATCH + bool "Disable watch" + default DEFAULT_SMALL + endmenu if MMCSD diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 204308368..5347ef015 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -1234,6 +1234,10 @@ int cmd_alias(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); int cmd_unalias(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); #endif +#ifndef CONFIG_NSH_DISABLE_WATCH +int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); +#endif + #if !defined(CONFIG_NSH_DISABLE_WAIT) && defined(CONFIG_SCHED_WAITPID) && \ !defined(CONFIG_DISABLE_PTHREAD) int cmd_wait(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 1cff20bd5..155b0fa6e 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -664,6 +664,11 @@ static const struct cmdmap_s g_cmdmap[] = CMD_MAP("usleep", cmd_usleep, 2, 2, ""), #endif +#ifndef CONFIG_NSH_DISABLE_WATCH + CMD_MAP("watch", cmd_watch, + 2, 6, "[-n] interval [-c] count "), +#endif + #ifdef CONFIG_NET_TCP # ifndef CONFIG_NSH_DISABLE_WGET CMD_MAP("wget", cmd_wget, 2, 4, "[-o ] "), diff --git a/nshlib/nsh_timcmds.c b/nshlib/nsh_timcmds.c index e34dd7fba..3c54ca538 100644 --- a/nshlib/nsh_timcmds.c +++ b/nshlib/nsh_timcmds.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -545,3 +546,64 @@ int cmd_timedatectl(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) return ret; } #endif + +#ifndef CONFIG_NSH_DISABLE_WATCH +int cmd_watch(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) +{ + char buffer[CONFIG_NSH_LINELEN]; + int interval = 2; + int count = -1; + FAR char *cmd; + int option; + int ret; + int i; + + while ((option = getopt(argc, argv, "n:c:")) != ERROR) + { + switch (option) + { + case 'n': + interval = atoi(optarg); + break; + + case 'c': + count = atoi(optarg); + break; + + default: + nsh_error(vtbl, g_fmtarginvalid, argv[0]); + return ERROR; + } + } + + if (optind < argc) + { + cmd = argv[optind]; + } + else + { + nsh_error(vtbl, g_fmtarginvalid, argv[0]); + return ERROR; + } + + if (count < 0) + { + count = INT_MAX; + } + + for (i = 0; i < count; i++) + { + strlcpy(buffer, cmd, CONFIG_NSH_LINELEN); + ret = nsh_parse(vtbl, buffer); + if (ret < 0) + { + nsh_error(vtbl, g_fmtcmdfailed, argv[0], cmd, NSH_ERRNO); + return ERROR; + } + + sleep(interval); + } + + return OK; +} +#endif