Messages: merge macros with and without message code
[platform/upstream/libxkbcommon.git] / tools / compose.c
1 /*
2  * Copyright © 2021 Ran Benita <ran@unusedvar.com>
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 <locale.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "xkbcommon/xkbcommon.h"
32 #include "xkbcommon/xkbcommon-compose.h"
33
34 static void
35 usage(FILE *fp, char *progname)
36 {
37     fprintf(fp,
38             "Usage: %s [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n",
39             progname);
40     fprintf(fp,
41             "   --locale - specify the locale directly\n"
42             "   --locale-from-env - get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n"
43             "   --locale-from-setlocale - get the locale using setlocale(3)\n"
44     );
45 }
46
47 int
48 main(int argc, char *argv[])
49 {
50     int ret = EXIT_FAILURE;
51     struct xkb_context *ctx = NULL;
52     struct xkb_compose_table *compose_table = NULL;
53     const char *locale = NULL;
54     enum options {
55         OPT_LOCALE,
56         OPT_LOCALE_FROM_ENV,
57         OPT_LOCALE_FROM_SETLOCALE,
58     };
59     static struct option opts[] = {
60         {"locale",                required_argument,      0, OPT_LOCALE},
61         {"locale-from-env",       no_argument,            0, OPT_LOCALE_FROM_ENV},
62         {"locale-from-setlocale", no_argument,            0, OPT_LOCALE_FROM_SETLOCALE},
63         {0, 0, 0, 0},
64     };
65
66     setlocale(LC_ALL, "");
67
68     while (1) {
69         int opt;
70         int option_index = 0;
71
72         opt = getopt_long(argc, argv, "h", opts, &option_index);
73         if (opt == -1)
74             break;
75
76         switch (opt) {
77         case OPT_LOCALE:
78             locale = optarg;
79             break;
80         case OPT_LOCALE_FROM_ENV:
81             locale = getenv("LC_ALL");
82             if (!locale)
83                 locale = getenv("LC_CTYPE");
84             if (!locale)
85                 locale = getenv("LANG");
86             if (!locale)
87                 locale = "C";
88             break;
89         case OPT_LOCALE_FROM_SETLOCALE:
90             locale = setlocale(LC_CTYPE, NULL);
91             break;
92         case 'h':
93             usage(stdout, argv[0]);
94             return EXIT_SUCCESS;
95         case '?':
96             usage(stderr, argv[0]);
97             return EXIT_INVALID_USAGE;
98         }
99     }
100     if (locale == NULL) {
101         usage(stderr, argv[0]);
102         return EXIT_INVALID_USAGE;
103     }
104
105     ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
106     if (!ctx) {
107         fprintf(stderr, "Couldn't create xkb context\n");
108         goto out;
109     }
110
111     compose_table =
112         xkb_compose_table_new_from_locale(ctx, locale,
113                                           XKB_COMPOSE_COMPILE_NO_FLAGS);
114     if (!compose_table) {
115         fprintf(stderr, "Couldn't create compose from locale\n");
116         goto out;
117     }
118
119     printf("Compiled successfully from locale %s\n", locale);
120
121 out:
122     xkb_compose_table_unref(compose_table);
123     xkb_context_unref(ctx);
124
125     return ret;
126 }