Commit 714182066d086baf848c22258136c8dd94a4edca

Peter Hutterer 2020-07-09T12:15:45

tools: consistently return 2 on invalid usage Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>

diff --git a/tools/compile-keymap.c b/tools/compile-keymap.c
index e090de1..9458f6a 100644
--- a/tools/compile-keymap.c
+++ b/tools/compile-keymap.c
@@ -170,7 +170,7 @@ parse_options(int argc, char **argv, struct xkb_rule_names *names)
             break;
         default:
             usage(argv);
-            exit(1);
+            exit(EXIT_INVALID_USAGE);
         }
 
     }
@@ -301,11 +301,11 @@ main(int argc, char **argv)
 
     if (argc <= 1) {
         usage(argv);
-        return 1;
+        return EXIT_INVALID_USAGE;
     }
 
     if (!parse_options(argc, argv, &names))
-        return 1;
+        return EXIT_INVALID_USAGE;
 
     ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
     assert(ctx);
diff --git a/tools/how-to-type.c b/tools/how-to-type.c
index 6fef401..441db38 100644
--- a/tools/how-to-type.c
+++ b/tools/how-to-type.c
@@ -50,7 +50,7 @@ main(int argc, char *argv[])
     const char *layout_ = NULL;
     const char *variant = NULL;
     const char *options = NULL;
-    int exit = EXIT_FAILURE;
+    int err = EXIT_FAILURE;
     struct xkb_context *ctx = NULL;
     char *endp;
     long val;
@@ -81,16 +81,19 @@ main(int argc, char *argv[])
             break;
         default:
             usage(argv[0]);
+            exit(EXIT_INVALID_USAGE);
         }
     }
     if (argc - optind != 1) {
         usage(argv[0]);
+        exit(EXIT_INVALID_USAGE);
     }
 
     errno = 0;
     val = strtol(argv[optind], &endp, 0);
     if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
         usage(argv[0]);
+        exit(EXIT_INVALID_USAGE);
     }
     codepoint = (uint32_t) val;
 
@@ -188,9 +191,9 @@ main(int argc, char *argv[])
         }
     }
 
-    exit = EXIT_SUCCESS;
+    err = EXIT_SUCCESS;
 err:
     xkb_keymap_unref(keymap);
     xkb_context_unref(ctx);
-    return exit;
+    return err;
 }
diff --git a/tools/interactive-evdev.c b/tools/interactive-evdev.c
index e8afb5e..a49b932 100644
--- a/tools/interactive-evdev.c
+++ b/tools/interactive-evdev.c
@@ -413,7 +413,7 @@ main(int argc, char *argv[])
             evdev_offset = strtol(optarg, NULL, 10);
             if (errno) {
                 fprintf(stderr, "error: -n option expects a number\n");
-                exit(EXIT_FAILURE);
+                exit(EXIT_INVALID_USAGE);
             }
             break;
         case 'c':
@@ -435,7 +435,7 @@ main(int argc, char *argv[])
                             "          -c (to report changes to the state)\n"
                             "          -d (to enable compose)\n"
                             "          -g (to use GTK consumed mode)\n");
-            exit(2);
+            exit(EXIT_INVALID_USAGE);
         }
     }
 
diff --git a/tools/interactive-wayland.c b/tools/interactive-wayland.c
index 8cc41de..3ac7ad7 100644
--- a/tools/interactive-wayland.c
+++ b/tools/interactive-wayland.c
@@ -662,6 +662,14 @@ main(int argc, char *argv[])
     struct interactive_dpy inter;
     struct wl_registry *registry;
 
+    if (argc != 1) {
+        ret = strcmp(argv[1], "--help");
+        fprintf(ret ? stderr : stdout, "Usage: %s [--help]\n", argv[0]);
+        if (ret)
+            fprintf(stderr, "unrecognized option: %s\n", argv[1]);
+        return ret ? EXIT_INVALID_USAGE : EXIT_SUCCESS;
+    }
+
     setlocale(LC_ALL, "");
 
     memset(&inter, 0, sizeof(inter));
diff --git a/tools/interactive-x11.c b/tools/interactive-x11.c
index d98433e..c7ca9aa 100644
--- a/tools/interactive-x11.c
+++ b/tools/interactive-x11.c
@@ -26,6 +26,7 @@
 #include <locale.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <xcb/xkb.h>
 
@@ -336,6 +337,14 @@ main(int argc, char *argv[])
     struct xkb_context *ctx;
     struct keyboard core_kbd;
 
+    if (argc != 1) {
+        ret = strcmp(argv[1], "--help");
+        fprintf(ret ? stderr : stdout, "Usage: %s [--help]\n", argv[0]);
+        if (ret)
+            fprintf(stderr, "unrecognized option: %s\n", argv[1]);
+        return ret ? EXIT_INVALID_USAGE : EXIT_SUCCESS;
+    }
+
     setlocale(LC_ALL, "");
 
     conn = xcb_connect(NULL, NULL);
diff --git a/tools/registry-list.c b/tools/registry-list.c
index e51c41f..0dc71d1 100644
--- a/tools/registry-list.c
+++ b/tools/registry-list.c
@@ -30,9 +30,9 @@
 #include "xkbcommon/xkbregistry.h"
 
 static void
-usage(const char *progname)
+usage(const char *progname, FILE *fp)
 {
-    fprintf(stderr,
+    fprintf(fp,
             "Usage: %s [OPTIONS] [/path/to/xkb_base_directory [/path2]...]\n"
             "\n"
             "Options:\n"
@@ -77,9 +77,11 @@ main(int argc, char **argv)
 
         switch (c) {
             case 'h':
+                usage(argv[0], stdout);
+                return 0;
             case '?':
-                usage(argv[0]);
-                return 1;
+                usage(argv[0], stderr);
+                return EXIT_INVALID_USAGE;
             case 'd':
                 load_defaults = false;
                 break;