[PATCH v2 3/3] commands: watch: add new command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 hwmon`.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
v1 -> v2:
  - return success when command is interrupted (Sascha)
  - free all allocated buffers on exit (Sascha)
  - Print title by default (Sascha)
  - Remove useless duplicate ctrlc() in outer loop condition
---
 commands/Kconfig  |  6 +++
 commands/Makefile |  1 +
 commands/watch.c  | 99 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 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..64b59abb107d
--- /dev/null
+++ b/commands/watch.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Port of Mini watch implementation from busybox
+ *
+ * Copyright (C) 2001 by Michael Habermann <mhabermann@xxxxxx>
+ * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III   (mjn3@xxxxxxxxxxxx)
+ */
+
+#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 = true;
+	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 = false;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if (argc < 1)
+		return COMMAND_ERROR_USAGE;
+
+	period_ns = simple_strtofract(period_str, &end, NSEC_PER_SEC);
+	if (*end)
+		return -EINVAL;
+
+	cmd = strjoin(" ", argv, argc);
+
+	width = (unsigned)-1; // make sure first time new_width != width
+	header = NULL;
+
+	while (true) {
+		/* 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())
+				goto out;
+		}
+	}
+
+out:
+	free(header);
+	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





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux