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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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);