From 63d5f02a5c74c70ef5131cb1c02722f09afd95cf Mon Sep 17 00:00:00 2001
From: =?utf8?q?Martin=20Hundeb=C3=B8ll?= <martin@geanix.com>
Date: Mon, 25 Feb 2019 10:43:07 +0100
Subject: [PATCH] gpiomon: add option to set line buffered output
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Some applications call gpiomon in a sub process, in which case glibc
defaults to block buffered output on stdout. This makes the output
arrive to the calling process only when the (4kB) buffer is filled (or
when gpiomon exists), making the information obsolete and pretty much
useless.

Support such scenarios by adding a switch to configure line buffered
output on stdout. Similar switches are available in other applications
(e.g. `rsync`s --output argument).

Signed-off-by: Martin Hundebøll <martin@geanix.com>
[Bartosz:
  - use setlinebuf() instead of setvbuf(),
  - tweak the help string,
  - move the setlinebuf() call directly into the optarg switch statement]
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 src/tools/gpiomon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/tools/gpiomon.c b/src/tools/gpiomon.c
index 0d5fcbd..9a1843b 100644
--- a/src/tools/gpiomon.c
+++ b/src/tools/gpiomon.c
@@ -26,17 +26,18 @@ static const struct option longopts[] = {
 	{ "silent",		no_argument,		NULL,	's' },
 	{ "rising-edge",	no_argument,		NULL,	'r' },
 	{ "falling-edge",	no_argument,		NULL,	'f' },
+	{ "line-buffered",	no_argument,		NULL,	'b' },
 	{ "format",		required_argument,	NULL,	'F' },
 	{ GETOPT_NULL_LONGOPT },
 };
 
-static const char *const shortopts = "+hvln:srfF:";
+static const char *const shortopts = "+hvln:srfbF:";
 
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset 1> <offset 2> ...\n",
 	       get_progname());
-	printf("Wait for events on GPIO lines\n");
+	printf("Wait for events on GPIO lines and print them to standard output\n");
 	printf("\n");
 	printf("Options:\n");
 	printf("  -h, --help:\t\tdisplay this message and exit\n");
@@ -46,6 +47,7 @@ static void print_help(void)
 	printf("  -s, --silent:\t\tdon't print event info\n");
 	printf("  -r, --rising-edge:\tonly process rising edge events\n");
 	printf("  -f, --falling-edge:\tonly process falling edge events\n");
+	printf("  -b, --line-buffered:\tset standard output as line buffered\n");
 	printf("  -F, --format=FMT\tspecify custom output format\n");
 	printf("\n");
 	printf("Format specifiers:\n");
@@ -278,6 +280,9 @@ int main(int argc, char **argv)
 		case 'f':
 			watch_falling = true;
 			break;
+		case 'b':
+			setlinebuf(stdout);
+			break;
 		case 'F':
 			ctx.fmt = optarg;
 			break;
-- 
2.30.2