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

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