tools: consistently return 2 on invalid usage

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
master
Peter Hutterer 2020-07-09 12:15:45 +10:00 committed by Ran Benita
parent 29e80e7b76
commit 714182066d
6 changed files with 34 additions and 12 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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));

View File

@ -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);

View File

@ -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;