[Iftop-users] [PATCH] unblock text UI for automated readout

Roman Hoog Antink rha at open.ch
Mon, 16 Jun 2014 14:25:37 +0200


This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
--FAXX5mo96hfjCsrRoAiLLt3rtWQ7UH8b5
Content-Type: multipart/mixed;
 boundary="------------010705040300040301010708"

This is a multi-part message in MIME format.
--------------010705040300040301010708
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi

Please accept the attached patch. It fixes blocking text output (-t) in
case iftop is run as subprocess (without ptty).

It also contains a few whitespace cleanups in tui.c.

Author: Georg Schoelly <ges@open.ch>

Cheers,
Roman

PGP key: http://pgp.mit.edu/pks/lookup?search=3Drha%40open.ch&op=3Dindex

--------------010705040300040301010708
Content-Type: text/x-patch;
 name="iftop-unblock_text_output.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
 filename="iftop-unblock_text_output.patch"

--- a/tui.c	2014-01-19 21:07:26.000000000 +0100
+++ b/tui.c	2014-06-12 16:44:12.000000000 +0200
@@ -15,6 +15,7 @@
 #include <signal.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
=20
 #if defined(HAVE_TERMIOS_H)
 #include <termios.h>
@@ -158,6 +159,7 @@ void tui_print() {
     printf("=3D");
   }
   printf("\n\n");
+  fflush(stdout);
 }
=20
=20
@@ -190,6 +192,9 @@ void tui_tick(int print) {
 void tui_loop() {
   int i;
   extern sig_atomic_t foad;
+  struct timeval input_timeout;
+  fd_set fd_set_stdin;
+  int retval;
=20
 #if defined(HAVE_TERMIOS_H)
   struct termios new_termios, old_termios;
@@ -220,8 +225,36 @@ void tui_loop() {
   system("/bin/stty cbreak -echo >/dev/null 2>&1");
 #endif
=20
-  while ((i =3D getchar()) !=3D 'q' && foad =3D=3D 0) {
+  while (!foad) {
+    /* wait for max 200ms on input */
+    FD_ZERO(&fd_set_stdin);
+    FD_SET(STDIN_FILENO, &fd_set_stdin);
+    input_timeout.tv_sec =3D 0;
+    input_timeout.tv_usec =3D 200;
+
+    /* and wait ... */
+    retval =3D select(1, &fd_set_stdin, NULL, NULL, &input_timeout);
+
+    if (retval < 0) {
+      /* interrupt, probably ctrl-c */
+      if (errno =3D=3D EINTR) {
+        foad =3D 1;
+        continue;
+      } else {
+        perror("select()");
+      }
+    /* no input */
+    } else if (retval =3D=3D 0) {
+      tick(0);
+      continue;
+    }
+    /* input found */
+
+    i =3D getchar();
     switch (i) {
+      case 'q':
+        foad =3D 1;
+        break;
       case 'u':
         tick(1);
         break;
@@ -302,20 +335,20 @@ void tui_loop() {
         if (options.paused) {
           printf("Pausing... press 'P' again to continue.\n");
         }
-	else {
-	  printf("Continuing.\n\n");
-	  tick(1);
-	}
-	break;
+        else {
+          printf("Continuing.\n\n");
+          tick(1);
+        }
+        break;
       case 'o':
         options.freezeorder ^=3D 1;
         printf("Order %s.\n\n", options.freezeorder ? "frozen" : "unfroz=
en");
-	tick(1);
-	break;
+        tick(1);
+        break;
       case '1':
         options.sort =3D OPTION_SORT_DIV1;
         printf("Sorting by column 1.\n\n");
-	tick(1);
+        tick(1);
         break;
       case '2':
         options.sort =3D OPTION_SORT_DIV2;
@@ -340,6 +373,7 @@ void tui_loop() {
       default:
         break;
     }
+    tick(0);
   }
=20
 #if defined(HAVE_TERMIOS_H)

--------------010705040300040301010708--

--FAXX5mo96hfjCsrRoAiLLt3rtWQ7UH8b5
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iF4EAREIAAYFAlOe4kQACgkQv0jhV4x3cc/LygEAnbYITlwKZIvR+h1Ahsi0HZR7
lqAV6YWl9ey7uvF6/84A/R5IPzgO509BEC8bKkQ+LvUXlHH1NbqBcLh62xTIYwCO
=NL9o
-----END PGP SIGNATURE-----

--FAXX5mo96hfjCsrRoAiLLt3rtWQ7UH8b5--