Hash :
8ba5c453
Author :
Date :
2025-03-30T10:07:10
xkbcomp: Use section reference as default section name
Before this commit the following keymap:
```c
xkb_keymap {
xkb_keycode {};
};
```
would result in (boilerplate removed):
```c
xkb_keymap {
xkb_keycode "(unnamed)" {};
};
```
This is both useless and wasting allocation: section names are optional,
so we should just remove this default name altogether and keep it
undefined, as in the original keymap.
The situation is a bit different if there is an include, as for keymaps
created from RMLVO names. Before this commit, the following keymap:
```c
xkb_keymap {
xkb_keycode { include "evdev+aliases(qwerty)" };
};
```
would result in (boilerplate removed):
```c
xkb_keymap {
xkb_keycode "(unnamed)" { … };
};
```
With this commit we now follow the Xorg xkbcomp style by using the
section reference (the include string) as the *default* section name. So
the previous example would now result in:
```c
xkb_keymap {
xkb_keycode "evdev_aliases(qwerty)" { … };
};
```
which is useful to give a hint of the original include.
Note that if the original section had a name, it would preserve it:
```c
xkb_keymap {
xkb_keycode "test" { include "evdev+aliases(qwerty)" };
};
```
would compile to:
```c
xkb_keymap {
xkb_keycode "test" { … };
};
```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/*
* Copyright © 2012 Ran Benita <ran234@gmail.com>
* SPDX-License-Identifier: MIT
*/
#include "config.h"
#include <locale.h>
#include "test.h"
#include "context.h"
#include "messages-codes.h"
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
#endif
static const char *
log_level_to_string(enum xkb_log_level level)
{
switch (level) {
case XKB_LOG_LEVEL_CRITICAL:
return "critical";
case XKB_LOG_LEVEL_ERROR:
return "error";
case XKB_LOG_LEVEL_WARNING:
return "warning";
case XKB_LOG_LEVEL_INFO:
return "info";
case XKB_LOG_LEVEL_DEBUG:
return "debug";
}
return "unknown";
}
ATTR_PRINTF(3, 0) static void
log_fn(struct xkb_context *ctx, enum xkb_log_level level,
const char *fmt, va_list args)
{
char *s;
int size;
darray_char *ls = xkb_context_get_user_data(ctx);
assert(ls);
size = vasprintf(&s, fmt, args);
assert(size != -1);
darray_append_string(*ls, log_level_to_string(level));
darray_append_lit(*ls, ": ");
darray_append_string(*ls, s);
free(s);
}
static void
test_basic(void)
{
int ret;
ret = setenv("XKB_LOG_LEVEL", "warn", 1);
assert(ret == 0);
ret = setenv("XKB_LOG_VERBOSITY", "5", 1);
assert(ret == 0);
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
assert(ctx);
darray_char log_string;
darray_init(log_string);
xkb_context_set_user_data(ctx, &log_string);
xkb_context_set_log_fn(ctx, log_fn);
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "first warning: %d\n", 87);
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "first info\n");
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "first debug: %s\n", "hello");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "first error: %lu\n", 115415UL);
log_vrb(ctx, 5, XKB_LOG_MESSAGE_NO_ID, "first verbose 5\n");
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "second warning: %d\n", 87);
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "second debug: %s %s\n", "hello", "world");
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "second info\n");
log_err(ctx, XKB_ERROR_MALFORMED_NUMBER_LITERAL, "second error: %lu\n", 115415UL);
log_vrb(ctx, 6, XKB_LOG_MESSAGE_NO_ID, "second verbose 6\n");
xkb_context_set_log_verbosity(ctx, 0);
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "third warning: %d\n", 87);
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "third debug: %s %s\n", "hello", "world");
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "third info\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "third error: %lu\n", 115415UL);
log_vrb(ctx, 0, XKB_LOG_MESSAGE_NO_ID, "third verbose 0\n");
printf("%s", darray_items(log_string));
assert(streq(darray_items(log_string),
"warning: first warning: 87\n"
"error: first error: 115415\n"
"warning: first verbose 5\n"
"warning: second warning: 87\n"
"debug: second debug: hello world\n"
"info: second info\n"
"error: [XKB-034] second error: 115415\n"));
xkb_context_unref(ctx);
darray_free(log_string);
}
struct test_data {
const char * const input;
const char * const log;
bool error;
};
static void
test_keymaps(void)
{
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
assert(ctx);
darray_char log_string;
darray_init(log_string);
xkb_context_set_user_data(ctx, &log_string);
xkb_context_set_log_fn(ctx, log_fn);
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_WARNING);
xkb_context_set_log_verbosity(ctx, 10);
const struct test_data keymaps[] = {
{
.input = "",
.log = "error: [XKB-822] Failed to parse input xkb string\n",
.error = true
},
{
.input = " ",
.log = "error: [XKB-822] Failed to parse input xkb string\n",
.error = true
},
{
.input = "\n",
.log = "error: [XKB-822] Failed to parse input xkb string\n",
.error = true
},
{
.input = "xkb_keymap {\n",
.log =
"error: [XKB-769] (input string):1:12: syntax error\n"
"error: [XKB-822] Failed to parse input xkb string\n",
.error = true
},
{
.input =
"xkb_keymap \"\\j\"\n"
" { symbols = { };\n"
"};",
.log =
"warning: [XKB-645] (input string):1:12: unknown escape sequence (\\j) in string literal\n"
"error: [XKB-769] (input string):2:4: syntax error\n"
"error: [XKB-822] Failed to parse input xkb string\n",
.error = true
},
{
.input =
"xkb_keymap {\n"
" xkb_keycodes {\n"
" <> = 1;\n"
"\n"
" alias <1> = <>;\n"
" alias <1> =\n"
" <>;\n"
" };\n"
" xkb_types \"\\400x\\j\" { };\n"
" xkb_compat {\n"
" interpret invalidKeysym +\n"
" Any { repeat = true; };\n"
" };\n"
" xkb_symbols { key <> {[0x30, leftshoe]}; };\n"
"};",
.log =
"warning: [XKB-193] (input string):9:13: invalid octal escape sequence (\\400) in string literal\n"
"warning: [XKB-645] (input string):9:13: unknown escape sequence (\\j) in string literal\n"
"warning: [XKB-107] (input string):11:15: unrecognized keysym \"invalidKeysym\"\n"
"warning: [XKB-489] (input string):14:26: numeric keysym \"0x0030\" (48)\n"
"warning: [XKB-301] (input string):14:32: deprecated keysym \"leftshoe\".\n"
"warning: [XKB-433] No map in include statement, but \"(input string)\" contains several; Using first defined map, \"(unnamed map)\"\n"
"warning: [XKB-523] Alias of <1> for <> declared more than once; First definition ignored\n"
"warning: [XKB-286] The type \"TWO_LEVEL\" for key '<>' group 1 was not previously defined; Using the default type\n"
"warning: [XKB-516] Type \"default\" has 1 levels, but <> has 2 levels; Ignoring extra symbols\n",
.error = false
}
};
for (unsigned int k = 0; k < ARRAY_SIZE(keymaps); k++) {
fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
struct xkb_keymap *keymap =
test_compile_buffer(ctx, keymaps[k].input, strlen(keymaps[k].input));
assert(keymaps[k].error ^ !!keymap);
xkb_keymap_unref(keymap);
assert_printf(streq_not_null(darray_items(log_string), keymaps[k].log),
"Expected:\n%s\nGot:\n%s\n",
keymaps[k].log, darray_items(log_string));
darray_free(log_string);
}
xkb_context_unref(ctx);
}
static void
test_compose(void)
{
struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
assert(ctx);
darray_char log_string;
darray_init(log_string);
xkb_context_set_user_data(ctx, &log_string);
xkb_context_set_log_fn(ctx, log_fn);
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_WARNING);
xkb_context_set_log_verbosity(ctx, 10);
const struct test_data composes[] = {
{
.input = "",
.log = NULL,
.error = false
},
{
.input = "\n",
.log = NULL,
.error = false
},
{
.input = "\xff\n",
.log =
"error: [XKB-542] (input string):1:1: unexpected non-ASCII character.\n"
"error: [XKB-542] (input string):1:1: This could be a file encoding issue. Supported file encodings are ASCII and UTF-8.\n"
"error: (input string):1:1: failed to parse file\n",
.error = true
},
{
.input =
"<leftshoe> : x\n"
"include \"x\"\n",
.log =
"warning: [XKB-301] (input string):1:1: deprecated keysym \"leftshoe\".\n"
"error: (input string):2:9: failed to open included Compose file \"x\": No such file or directory\n"
"error: (input string):2:9: failed to parse file\n",
.error = true
},
{
.input =
"<a> : \"a\"\n"
"\n"
"<b> : \"i\\j\\xk\n"
"<0x30> : \"\\400\" invalidKeysym\n"
"<0> <1> <2> <3> <4> <5> <6> <7> <8> <9> <leftshoe> : \"\"\n",
.log =
"warning: [XKB-645] (input string):3:7: unknown escape sequence (\\j) in string literal\n"
"warning: [XKB-193] (input string):3:7: illegal hexadecimal escape sequence (\\x) in string literal\n"
"error: [XKB-685] (input string):3:7: unterminated string literal\n"
"warning: [XKB-193] (input string):4:10: illegal octal escape sequence (\\400) in string literal\n"
"error: (input string):4:17: unrecognized keysym \"invalidKeysym\" on right-hand side\n"
"warning: [XKB-301] (input string):5:41: deprecated keysym \"leftshoe\".\n"
"warning: [XKB-685] (input string):5:41: too many keysyms (11) on left-hand side; skipping line\n",
.error = false
},
{
.input =
":\n"
"<a> :\n"
"#\n"
"<c> : \"a\" \"b\"\n"
"<d> : a b\n",
.log =
"warning: (input string):1:1: expected at least one keysym on left-hand side; skipping line\n"
"warning: [XKB-685] (input string):2:5: right-hand side must have at least one of string or keysym; skipping line\n"
"warning: (input string):4:11: right-hand side can have at most one string; skipping line\n"
"error: [XKB-685] (input string):5:9: unrecognized modifier \"b\"\n",
.error = false
},
{
.input =
"<a> : a\n"
"<a> : a\n"
"<b> : b\n"
"<b> <c> : x\n"
"<c> <d> : y\n"
"<c> : c\n",
.log =
"warning: (input string):2:7: this compose sequence is a duplicate of another; skipping line\n"
"warning: (input string):4:11: a sequence already exists which is a prefix of this sequence; overriding\n"
"warning: (input string):6:11: this compose sequence is a prefix of another; skipping line\n",
.error = false
}
};
for (unsigned int k = 0; k < ARRAY_SIZE(composes); k++) {
fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, k);
struct xkb_compose_table *table = xkb_compose_table_new_from_buffer(
ctx, composes[k].input, strlen(composes[k].input), "",
XKB_COMPOSE_FORMAT_TEXT_V1,
XKB_COMPOSE_COMPILE_NO_FLAGS
);
assert(composes[k].error ^ !!table);
xkb_compose_table_unref(table);
assert_printf(streq_null(darray_items(log_string), composes[k].log),
"Expected:\n%s\nGot:\n%s\n",
darray_items(log_string), composes[k].log);
darray_free(log_string);
}
xkb_context_unref(ctx);
}
int
main(void)
{
test_init();
/* We really need to be locale-independent here */
setlocale(LC_ALL, "C");
test_basic();
test_keymaps();
test_compose();
return EXIT_SUCCESS;
}