Commit fc41d3d6051db9feaa2abb27b1ba4988483ab90b

Ran Benita 2016-05-05T15:41:13

test: use termios instead of system() for disabling terminal echo Takes care of GCC's annoyingly persistent warn_unused_result warnings. But it's better to avoid system() I suppose. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/test/common.c b/test/common.c
index 8a2b632..2dd10a0 100644
--- a/test/common.c
+++ b/test/common.c
@@ -35,6 +35,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <termios.h>
 
 #include "test.h"
 
@@ -454,3 +455,25 @@ test_print_state_changes(enum xkb_state_component changed)
         printf("leds ");
     printf("]\n");
 }
+
+void
+test_disable_stdin_echo(void)
+{
+    /* Same as `stty -echo`. */
+    struct termios termios;
+    if (tcgetattr(STDIN_FILENO, &termios) == 0) {
+        termios.c_lflag &= ~ECHO;
+        (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
+    }
+}
+
+void
+test_enable_stdin_echo(void)
+{
+    /* Same as `stty echo`. */
+    struct termios termios;
+    if (tcgetattr(STDIN_FILENO, &termios) == 0) {
+        termios.c_lflag |= ECHO;
+        (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
+    }
+}
diff --git a/test/interactive-evdev.c b/test/interactive-evdev.c
index 7fdc069..7853e59 100644
--- a/test/interactive-evdev.c
+++ b/test/interactive-evdev.c
@@ -482,15 +482,10 @@ main(int argc, char *argv[])
     sigaction(SIGINT, &act, NULL);
     sigaction(SIGTERM, &act, NULL);
 
-    /* Instead of fiddling with termios.. */
-    (void) system("stty -echo");
-
+    test_disable_stdin_echo();
     ret = loop(kbds);
-    if (ret)
-        goto err_stty;
+    test_enable_stdin_echo();
 
-err_stty:
-    (void) system("stty echo");
     free_keyboards(kbds);
 err_compose:
     xkb_compose_table_unref(compose_table);
diff --git a/test/interactive-wayland.c b/test/interactive-wayland.c
index 5716291..1f3f409 100644
--- a/test/interactive-wayland.c
+++ b/test/interactive-wayland.c
@@ -680,14 +680,13 @@ main(int argc, char *argv[])
 		goto err_conn;
 	}
 
-	(void) system("stty -echo");
+	test_disable_stdin_echo();
 	do {
 		ret = wl_display_dispatch(inter.dpy);
 	} while (ret >= 0 && !terminate);
-	(void) system("stty echo");
+	test_enable_stdin_echo();
 
 	wl_registry_destroy(registry);
-
 err_conn:
 	dpy_disconnect(&inter);
 err_out:
diff --git a/test/interactive-x11.c b/test/interactive-x11.c
index 34ad654..904136f 100644
--- a/test/interactive-x11.c
+++ b/test/interactive-x11.c
@@ -372,9 +372,9 @@ main(int argc, char *argv[])
         goto err_core_kbd;
     }
 
-    (void) system("stty -echo");
+    test_disable_stdin_echo();
     ret = loop(conn, &core_kbd);
-    (void) system("stty echo");
+    test_enable_stdin_echo();
 
 err_core_kbd:
     deinit_kbd(&core_kbd);
diff --git a/test/test.h b/test/test.h
index 628ec18..440c7ea 100644
--- a/test/test.h
+++ b/test/test.h
@@ -88,3 +88,9 @@ test_print_keycode_state(struct xkb_state *state,
 
 void
 test_print_state_changes(enum xkb_state_component changed);
+
+void
+test_disable_stdin_echo(void);
+
+void
+test_enable_stdin_echo(void);