nsh: support watch command

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2024-08-08 12:04:00 +08:00 committed by Xiang Xiao
parent e558c8e028
commit 59c21c7aee
4 changed files with 75 additions and 0 deletions

View file

@ -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

View file

@ -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);

View file

@ -664,6 +664,11 @@ static const struct cmdmap_s g_cmdmap[] =
CMD_MAP("usleep", cmd_usleep, 2, 2, "<usec>"),
#endif
#ifndef CONFIG_NSH_DISABLE_WATCH
CMD_MAP("watch", cmd_watch,
2, 6, "[-n] interval [-c] count <command>"),
#endif
#ifdef CONFIG_NET_TCP
# ifndef CONFIG_NSH_DISABLE_WGET
CMD_MAP("wget", cmd_wget, 2, 4, "[-o <local-path>] <url>"),

View file

@ -25,6 +25,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>
@ -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