text.c: remove unreachable code
[platform/upstream/libxkbcommon.git] / tools / check-messages.c
1 /*
2  * Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "utils.h"
31 #include "messages-codes.h"
32 #include "messages.h"
33
34 static xkb_message_code_t
35 parse_message_code(char *raw_code) {
36     xkb_message_code_t code;
37     code = atoi(raw_code);
38     if (!code && strstr(raw_code, "XKB-")) {
39         return atoi(&(raw_code[4]));
40     } else {
41         return code;
42     }
43 }
44
45 static void
46 usage(char **argv)
47 {
48     printf("Usage: %s MESSAGE_CODES\n"
49            "\n"
50            "Check whether the given message codes are supported.\n",
51            argv[0]);
52
53     const struct xkb_message_entry *xkb_messages;
54     size_t count = xkb_message_get_all(&xkb_messages);
55
56     printf("\nCurrent supported messages:\n");
57     for (size_t idx = 0; idx < count; idx++) {
58         printf("- XKB-%03u: %s\n", xkb_messages[idx].code, xkb_messages[idx].label);
59     }
60 }
61
62 #define XKB_CHECK_MSG_ERROR_PREFIX "xkb-check-messages: ERROR: "
63 #define MALFORMED_MESSAGE   (1 << 2)
64 #define UNSUPPORTED_MESSAGE (1 << 3)
65
66 int main(int argc, char **argv) {
67     if (argc <= 1) {
68         usage(argv);
69         return EXIT_INVALID_USAGE;
70     }
71
72     int rc = 0;
73     xkb_message_code_t code;
74     const struct xkb_message_entry* entry;
75     for (int k=1; k < argc; k++) {
76         code = parse_message_code(argv[k]);
77         if (!code) {
78             fprintf(stderr,
79                     XKB_CHECK_MSG_ERROR_PREFIX "Malformed message code: %s\n",
80                     argv[k]);
81             rc |= MALFORMED_MESSAGE;
82             continue;
83         }
84         entry = xkb_message_get(code);
85         if (entry == NULL) {
86             fprintf(stderr,
87                     XKB_CHECK_MSG_ERROR_PREFIX "Unsupported message code: %s\n",
88                     argv[k]);
89             rc |= UNSUPPORTED_MESSAGE;
90         }
91     }
92
93     return rc;
94 }