[PATCH 3/3] commands: watch: add new command
Ahmad Fatoum
a.fatoum at pengutronix.de
Mon Jul 1 00:54:34 PDT 2024
For testing proper operation of IIO devices, it can be useful to monitor
changes in the reading reported by the hwmon command. This is now
possible by using `watch -n 0.5 -t hwmon`.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
commands/Kconfig | 6 +++
commands/Makefile | 1 +
commands/watch.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 104 insertions(+)
create mode 100644 commands/watch.c
diff --git a/commands/Kconfig b/commands/Kconfig
index c9c4be67e098..5b512f1bbac7 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -2417,6 +2417,12 @@ config CMD_TIME
Note: This command depends on COMMAND being interruptible,
otherwise the timer may overrun resulting in incorrect results
+config CMD_WATCH
+ bool "watch"
+ help
+ watch is used to execute a command periodically, showing
+ output to the screen.
+
config CMD_UPTIME
bool "uptime"
help
diff --git a/commands/Makefile b/commands/Makefile
index f3e8e944a931..4ca7ba7eb609 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_CMD_WD) += wd.o
obj-$(CONFIG_CMD_LED_TRIGGER) += trigger.o
obj-$(CONFIG_CMD_USB) += usb.o
obj-$(CONFIG_CMD_TIME) += time.o
+obj-$(CONFIG_CMD_WATCH) += watch.o
obj-$(CONFIG_CMD_UPTIME) += uptime.o
obj-$(CONFIG_CMD_OFTREE) += oftree.o
obj-$(CONFIG_CMD_OF_COMPATIBLE) += of_compatible.o
diff --git a/commands/watch.c b/commands/watch.c
new file mode 100644
index 000000000000..84731eb8505a
--- /dev/null
+++ b/commands/watch.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Port of Mini watch implementation from busybox
+ *
+ * Copyright (C) 2001 by Michael Habermann <mhabermann at gmx.de>
+ * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III (mjn3 at codepoet.org)
+ */
+
+#include <common.h>
+#include <command.h>
+#include <clock.h>
+#include <linux/math64.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <term.h>
+#include <rtc.h>
+
+static int do_watch(int argc , char *argv[])
+{
+ const char *period_str = "2.0";
+ u64 period_ns, start;
+ bool print_header = false;
+ int opt;
+ unsigned width, new_width;
+ char *end, *header, *cmd;
+
+ while ((opt = getopt(argc, argv, "+n:t")) > 0) {
+ switch (opt) {
+ case 'n':
+ period_str = optarg;
+ break;
+ case 't':
+ print_header = true;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 1)
+ return COMMAND_ERROR_USAGE;
+
+ cmd = strjoin(" ", argv, argc);
+
+ period_ns = simple_strtofract(period_str, &end, NSEC_PER_SEC);
+ if (*end)
+ return -EINVAL;
+
+ width = (unsigned)-1; // make sure first time new_width != width
+ header = NULL;
+
+ while (!ctrlc()) {
+ /* home; clear to the end of screen */
+ printf("\e[H\e[J");
+
+ if (print_header) {
+ term_getsize(&new_width, NULL);
+ if (new_width != width) {
+ width = new_width;
+ free(header);
+ header = xasprintf("Every %ss: %-*s",
+ period_str, (int)width, cmd);
+ }
+
+ printf("%s\n\n", header);
+ }
+
+ run_command(cmd);
+
+ start = get_time_ns();
+ while (!is_timeout(start, period_ns)) {
+ if (ctrlc())
+ return 1;
+ }
+ }
+
+ free(cmd);
+
+ return 0;
+}
+
+BAREBOX_CMD_HELP_START(watch)
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-n SEC", "Period (default 2)")
+BAREBOX_CMD_HELP_OPT ("-t", "Don't print header")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(watch)
+ .cmd = do_watch,
+ BAREBOX_CMD_DESC("run program periodically")
+ BAREBOX_CMD_OPTS("[-n SEC] [-t] PROG ARGS")
+ BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+ BAREBOX_CMD_HELP(cmd_watch_help)
+BAREBOX_CMD_END
--
2.39.2
More information about the barebox
mailing list