Commit 2eb5d2c81db908ff691b0ac58f2361dce6f7c005

Peter Hutterer 2020-06-23T16:06:10

test: simplify error handling in interactive-evdev Passing -errno around and having separate labels depending on failure types is superfluous here. All the unref calls can handle NULL and nothing cares about errno once we're out of the immediate scope. So let's simplify this and deal with 0 and 1 only. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>

diff --git a/test/interactive-evdev.c b/test/interactive-evdev.c
index d10c82e..7c96272 100644
--- a/test/interactive-evdev.c
+++ b/test/interactive-evdev.c
@@ -297,7 +297,7 @@ read_keyboard(struct keyboard *kbd)
 
     if (len < 0 && errno != EWOULDBLOCK) {
         fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
-        return -errno;
+        return 1;
     }
 
     return 0;
@@ -306,8 +306,8 @@ read_keyboard(struct keyboard *kbd)
 static int
 loop(struct keyboard *kbds)
 {
-    int i, ret;
-    int epfd;
+    int i, ret = 1;
+    int epfd = -1;
     struct keyboard *kbd;
     struct epoll_event ev;
     struct epoll_event evs[16];
@@ -316,7 +316,7 @@ loop(struct keyboard *kbds)
     if (epfd < 0) {
         fprintf(stderr, "Couldn't create epoll instance: %s\n",
                 strerror(errno));
-        return -errno;
+        goto out;
     }
 
     for (kbd = kbds; kbd; kbd = kbd->next) {
@@ -325,10 +325,9 @@ loop(struct keyboard *kbds)
         ev.data.ptr = kbd;
         ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
         if (ret) {
-            ret = -errno;
             fprintf(stderr, "Couldn't add %s to epoll: %s\n",
                     kbd->path, strerror(errno));
-            goto err_epoll;
+            goto out;
         }
     }
 
@@ -337,25 +336,22 @@ loop(struct keyboard *kbds)
         if (ret < 0) {
             if (errno == EINTR)
                 continue;
-            ret = -errno;
             fprintf(stderr, "Couldn't poll for events: %s\n",
                     strerror(errno));
-            goto err_epoll;
+            goto out;
         }
 
         for (i = 0; i < ret; i++) {
             kbd = evs[i].data.ptr;
             ret = read_keyboard(kbd);
             if (ret) {
-                goto err_epoll;
+                goto out;
             }
         }
     }
 
-    close(epfd);
-    return 0;
-
-err_epoll:
+    ret = 0;
+out:
     close(epfd);
     return ret;
 }
@@ -369,11 +365,11 @@ sigintr_handler(int signum)
 int
 main(int argc, char *argv[])
 {
-    int ret;
+    int ret = EXIT_FAILURE;
     int opt;
     struct keyboard *kbds;
-    struct xkb_context *ctx;
-    struct xkb_keymap *keymap;
+    struct xkb_context *ctx = NULL;
+    struct xkb_keymap *keymap = NULL;
     struct xkb_compose_table *compose_table = NULL;
     const char *rules = NULL;
     const char *model = NULL;
@@ -439,18 +435,16 @@ main(int argc, char *argv[])
 
     ctx = test_get_context(0);
     if (!ctx) {
-        ret = -1;
         fprintf(stderr, "Couldn't create xkb context\n");
-        goto err_out;
+        goto out;
     }
 
     if (keymap_path) {
         FILE *file = fopen(keymap_path, "rb");
         if (!file) {
-            ret = EXIT_FAILURE;
             fprintf(stderr, "Couldn't open '%s': %s\n",
                     keymap_path, strerror(errno));
-            goto err_ctx;
+            goto out;
         }
         keymap = xkb_keymap_new_from_file(ctx, file,
                                           XKB_KEYMAP_FORMAT_TEXT_V1,
@@ -463,9 +457,8 @@ main(int argc, char *argv[])
     }
 
     if (!keymap) {
-        ret = -1;
         fprintf(stderr, "Couldn't create xkb keymap\n");
-        goto err_ctx;
+        goto out;
     }
 
     if (with_compose) {
@@ -474,16 +467,14 @@ main(int argc, char *argv[])
             xkb_compose_table_new_from_locale(ctx, locale,
                                               XKB_COMPOSE_COMPILE_NO_FLAGS);
         if (!compose_table) {
-            ret = -1;
             fprintf(stderr, "Couldn't create compose from locale\n");
-            goto err_xkb;
+            goto out;
         }
     }
 
     kbds = get_keyboards(keymap, compose_table);
     if (!kbds) {
-        ret = -1;
-        goto err_compose;
+        goto out;
     }
 
     act.sa_handler = sigintr_handler;
@@ -497,12 +488,10 @@ main(int argc, char *argv[])
     test_enable_stdin_echo();
 
     free_keyboards(kbds);
-err_compose:
+out:
     xkb_compose_table_unref(compose_table);
-err_xkb:
     xkb_keymap_unref(keymap);
-err_ctx:
     xkb_context_unref(ctx);
-err_out:
-    exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+
+    return ret;
 }
diff --git a/tools/tools-common.c b/tools/tools-common.c
new file mode 100644
index 0000000..aecb2ab
--- /dev/null
+++ b/tools/tools-common.c
@@ -0,0 +1,89 @@
+/*
+ * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
+ * Copyright © 2012 Intel Corporation
+ * Copyright © 2012 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the names of the authors or their
+ * institutions shall not be used in advertising or otherwise to promote the
+ * sale, use or other dealings in this Software without prior written
+ * authorization from the authors.
+ *
+ * Author: Dan Nicholson <dbn.lists@gmail.com>
+ *         Daniel Stone <daniel@fooishbar.org>
+ *         Ran Benita <ran234@gmail.com>
+ */
+
+#include "config.h"
+
+#include <limits.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef _MSC_VER
+#include <io.h>
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <termios.h>
+#endif
+
+#include "tools-common.h"
+
+#ifdef _MSC_VER
+void
+test_disable_stdin_echo(void)
+{
+    HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
+    DWORD mode = 0;
+    GetConsoleMode(stdin_handle, &mode);
+    SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
+}
+
+void
+test_enable_stdin_echo(void)
+{
+    HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
+    DWORD mode = 0;
+    GetConsoleMode(stdin_handle, &mode);
+    SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
+}
+#else
+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);
+    }
+}
+#endif
diff --git a/tools/tools-common.h b/tools/tools-common.h
new file mode 100644
index 0000000..035c392
--- /dev/null
+++ b/tools/tools-common.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+#include <assert.h>
+
+/* Don't use compat names in internal code. */
+#define _XKBCOMMON_COMPAT_H
+#include "xkbcommon/xkbcommon.h"
+#include "xkbcommon/xkbcommon-compose.h"
+#include "utils.h"
+
+/* The offset between KEY_* numbering, and keycodes in the XKB evdev
+ * dataset. */
+#define EVDEV_OFFSET 8
+
+enum key_seq_state {
+    DOWN,
+    REPEAT,
+    UP,
+    BOTH,
+    NEXT,
+    FINISH,
+};
+
+int
+test_key_seq(struct xkb_keymap *keymap, ...);
+
+int
+test_key_seq_va(struct xkb_keymap *keymap, va_list args);
+
+char *
+test_get_path(const char *path_rel);
+
+char *
+test_read_file(const char *path_rel);
+
+enum test_context_flags {
+    CONTEXT_NO_FLAG = 0,
+    CONTEXT_ALLOW_ENVIRONMENT_NAMES = (1 << 0),
+};
+
+struct xkb_context *
+test_get_context(enum test_context_flags flags);
+
+struct xkb_keymap *
+test_compile_file(struct xkb_context *context, const char *path_rel);
+
+struct xkb_keymap *
+test_compile_string(struct xkb_context *context, const char *string);
+
+struct xkb_keymap *
+test_compile_buffer(struct xkb_context *context, const char *buf, size_t len);
+
+struct xkb_keymap *
+test_compile_rules(struct xkb_context *context, const char *rules,
+                   const char *model, const char *layout, const char *variant,
+                   const char *options);
+
+void
+test_print_keycode_state(struct xkb_state *state,
+                         struct xkb_compose_state *compose_state,
+                         xkb_keycode_t keycode,
+                         enum xkb_consumed_mode consumed_mode);
+
+void
+test_print_state_changes(enum xkb_state_component changed);
+
+void
+test_disable_stdin_echo(void);
+
+void
+test_enable_stdin_echo(void);
+
+#ifdef _MSC_VER
+#define setenv(varname, value, overwrite) _putenv_s((varname), (value))
+#define unsetenv(varname) _putenv_s(varname, "")
+#endif