mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-10-19 11:23:07 +08:00
nsh: support watch command
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user