tools: install our tools as xkbcli subcommands
[platform/upstream/libxkbcommon.git] / tools / xkbcli.c
1 /*
2  * Copyright © 2020 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <getopt.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "tools-common.h"
31
32 static void
33 usage(void)
34 {
35     printf("Usage: xkbcli [--help|-h] [--version|-V] <command> [<args>]\n"
36            "\n"
37            "Global options:\n"
38            "  -h, --help ...... show this help and exit\n"
39            "  -V, --version ... show version information and exit\n"
40            "Commands:\n"
41 #if HAVE_XKBCLI_LIST
42            "  list\n"
43            "    List available rules, models, layouts, variants and options\n"
44            "\n"
45 #endif
46 #if HAVE_XKBCLI_INTERACTIVE_WAYLAND
47            "  interactive-wayland\n"
48            "    Interactive debugger for XKB maps for wayland\n"
49            "\n"
50 #endif
51 #if HAVE_XKBCLI_INTERACTIVE_x11
52            "  interactive-x11\n"
53            "    Interactive debugger for XKB maps for X11\n"
54            "\n"
55 #endif
56 #if HAVE_XKBCLI_INTERACTIVE_EVDEV
57            "  interactive-evdev\n"
58            "    Interactive debugger for XKB maps for evdev\n"
59            "\n"
60 #endif
61 #if HAVE_XKBCLI_COMPILE_KEYMAP
62            "  compile-keymap\n"
63            "    Compile n XKB keymap\n"
64            "\n"
65 #endif
66 #if HAVE_XKBCLI_HOW_TO_TYPE
67            "  how-to-type\n"
68            "    Print key sequences to type a Unicode codepoint\n"
69            "\n"
70 #endif
71            );
72 }
73
74 int
75 main(int argc, char **argv)
76 {
77     enum options {
78         OPT_HELP = 1,
79         OPT_VERSION,
80     };
81     int option_index = 0;
82
83     while (1) {
84         int c;
85 #if HAVE_GETOPT_LONG
86         static struct option opts[] = {
87             { "help",    no_argument, 0, OPT_HELP },
88             { "version", no_argument, 0, OPT_VERSION },
89             { 0, 0, 0, 0}
90         };
91
92         c = getopt_long(argc, argv, "+hV", opts, &option_index);
93 #else
94         c = getopt(argc, argv, "+hV");
95 #endif
96         if (c == -1)
97             break;
98
99         switch(c) {
100             case 'h':
101             case OPT_HELP:
102                 usage();
103                 return EXIT_SUCCESS;
104             case 'V':
105             case OPT_VERSION:
106                 printf("%s\n", LIBXKBCOMMON_VERSION);
107                 return EXIT_SUCCESS;
108             default:
109                 usage();
110                 return EXIT_INVALID_USAGE;
111         }
112     }
113
114     if (optind >= argc) {
115         usage();
116         return EXIT_INVALID_USAGE;
117     }
118
119     argv += optind;
120     argc -= optind;
121
122     return tools_exec_command("xkbcli", argc, argv);
123 }