test: let rmlvo-to-kccgst take long options like rmlvo-to-keymap
[platform/upstream/libxkbcommon.git] / test / rmlvo-to-keymap.c
1 /*
2  * Copyright © 2018 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
25 #include <assert.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <stdio.h>
29 #include <stdbool.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "xkbcommon/xkbcommon.h"
33
34 static bool print = false;
35
36 static void
37 usage(char **argv)
38 {
39     printf("Usage: %s [--print] [--rules <rules>] [--layout <layout>] [--variant <variant>] [--options <option>]\n",
40            argv[0]);
41     printf("This tool tests the compilation from RMLVO to a keymap.\n");
42     printf("--print  print the resulting keymap\n");
43 }
44
45 static bool
46 parse_options(int argc, char **argv, struct xkb_rule_names *names)
47 {
48     enum options {
49         OPT_PRINT,
50         OPT_RULES,
51         OPT_MODEL,
52         OPT_LAYOUT,
53         OPT_VARIANT,
54         OPT_OPTION,
55     };
56     static struct option opts[] = {
57         {"help",        no_argument,            0, 'h'},
58         {"print",       no_argument,            0, OPT_PRINT},
59         {"rules",       required_argument,      0, OPT_RULES},
60         {"model",       required_argument,      0, OPT_MODEL},
61         {"layout",      required_argument,      0, OPT_LAYOUT},
62         {"variant",     required_argument,      0, OPT_VARIANT},
63         {"options",     required_argument,      0, OPT_OPTION},
64         {0, 0, 0, 0},
65     };
66
67     while (1) {
68         int c;
69         int option_index = 0;
70         c = getopt_long(argc, argv, "h", opts, &option_index);
71         if (c == -1)
72             break;
73
74         switch (c) {
75         case 'h':
76             usage(argv);
77             exit(0);
78         case OPT_PRINT:
79             print = true;
80             break;
81         case OPT_RULES:
82             names->rules = optarg;
83             break;
84         case OPT_MODEL:
85             names->model = optarg;
86             break;
87         case OPT_LAYOUT:
88             names->layout = optarg;
89             break;
90         case OPT_VARIANT:
91             names->variant = optarg;
92             break;
93         case OPT_OPTION:
94             names->options = optarg;
95             break;
96         default:
97             usage(argv);
98             exit(1);
99         }
100
101     }
102
103     return true;
104 }
105
106 int
107 main(int argc, char **argv)
108 {
109     struct xkb_context *ctx;
110     struct xkb_keymap *keymap;
111     struct xkb_rule_names names = {
112         .rules = NULL,
113         .model = NULL,
114         .layout = NULL,
115         .variant = NULL,
116         .options = NULL,
117     };
118     int rc;
119
120     if (argc <= 1) {
121         usage(argv);
122         return 1;
123     }
124
125     if (!parse_options(argc, argv, &names))
126         return 1;
127
128     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
129     assert(ctx);
130
131     keymap = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
132     rc = (keymap == NULL);
133
134     if (rc == 0 && print)
135         printf("%s\n", xkb_keymap_get_as_string(keymap,
136                                                 XKB_KEYMAP_FORMAT_TEXT_V1));
137
138     xkb_keymap_unref(keymap);
139     xkb_context_unref(ctx);
140
141     return rc;
142 }