mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/3] commands: watch: add new command
Date: Mon,  1 Jul 2024 09:54:34 +0200	[thread overview]
Message-ID: <20240701075434.220378-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240701075434.220378-1-a.fatoum@pengutronix.de>

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@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@gmx.de>
+ * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III   (mjn3@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




  parent reply	other threads:[~2024-07-01  7:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-01  7:54 [PATCH 0/3] " Ahmad Fatoum
2024-07-01  7:54 ` [PATCH 1/3] commands: edit: factor out getwinsize and export it for reuse Ahmad Fatoum
2024-07-01  7:54 ` [PATCH 2/3] lib: strtox: implement new simple_strtofract Ahmad Fatoum
2024-07-01  7:54 ` Ahmad Fatoum [this message]
2024-07-01  9:26   ` [PATCH 3/3] commands: watch: add new command Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240701075434.220378-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox