tools: add a xkbcli tool as entry point for the various tools we have
[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            "\n");
41 }
42
43 int
44 main(int argc, char **argv)
45 {
46     enum options {
47         OPT_HELP = 1,
48         OPT_VERSION,
49     };
50     int option_index = 0;
51
52     while (1) {
53         int c;
54 #if HAVE_GETOPT_LONG
55         static struct option opts[] = {
56             { "help",    no_argument, 0, OPT_HELP },
57             { "version", no_argument, 0, OPT_VERSION },
58             { 0, 0, 0, 0}
59         };
60
61         c = getopt_long(argc, argv, "+hV", opts, &option_index);
62 #else
63         c = getopt(argc, argv, "+hV");
64 #endif
65         if (c == -1)
66             break;
67
68         switch(c) {
69             case 'h':
70             case OPT_HELP:
71                 usage();
72                 return EXIT_SUCCESS;
73             case 'V':
74             case OPT_VERSION:
75                 printf("%s\n", LIBXKBCOMMON_VERSION);
76                 return EXIT_SUCCESS;
77             default:
78                 usage();
79                 return EXIT_INVALID_USAGE;
80         }
81     }
82
83     if (optind >= argc) {
84         usage();
85         return EXIT_INVALID_USAGE;
86     }
87
88     argv += optind;
89     argc -= optind;
90
91     return tools_exec_command("xkbcli", argc, argv);
92 }