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