| 1 |
/* |
| 2 |
* Copyright © 2025 Pierre Le Marre <[email protected]> |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <limits.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "xkbcommon/xkbcommon.h" |
| 12 |
#include "context.h" |
| 13 |
#include "darray.h" |
| 14 |
#include "messages-codes.h" |
| 15 |
#include "utils.h" |
| 16 |
#include "utils-paths.h" |
| 17 |
#include "keymap-file-iterator.h" |
| 18 |
#include "xkbcomp-priv.h" |
| 19 |
#include "include.h" |
| 20 |
#include "xkbcomp/ast.h" |
| 21 |
|
| 22 |
const char * |
| 23 |
xkb_file_type_name(enum xkb_file_type type) |
| 24 |
{ |
| 25 |
if (type > FILE_TYPE_KEYMAP) |
| 26 |
return "unknown"; |
| 27 |
|
| 28 |
static const char *xkb_file_type_strings[_FILE_TYPE_NUM_ENTRIES] = { |
| 29 |
[FILE_TYPE_KEYCODES] = "keycodes", |
| 30 |
[FILE_TYPE_TYPES] = "types", |
| 31 |
[FILE_TYPE_COMPAT] = "compatibility", |
| 32 |
[FILE_TYPE_SYMBOLS] = "symbols", |
| 33 |
[FILE_TYPE_GEOMETRY] = "geometry", |
| 34 |
[FILE_TYPE_KEYMAP] = "keymap", |
| 35 |
}; |
| 36 |
return xkb_file_type_strings[type]; |
| 37 |
} |
| 38 |
|
| 39 |
const char * |
| 40 |
xkb_merge_mode_name(enum merge_mode merge) |
| 41 |
{ |
| 42 |
if (merge >= _MERGE_MODE_NUM_ENTRIES) |
| 43 |
return "unknown"; |
| 44 |
|
| 45 |
static const char *merge_mode_strings[_MERGE_MODE_NUM_ENTRIES] = { |
| 46 |
[MERGE_DEFAULT] = "default", |
| 47 |
[MERGE_AUGMENT] = "augment", |
| 48 |
[MERGE_OVERRIDE] = "override", |
| 49 |
[MERGE_REPLACE] = "replace", |
| 50 |
}; |
| 51 |
return merge_mode_strings[merge]; |
| 52 |
} |
| 53 |
|
| 54 |
/* Stateful lookup of map flags names */ |
| 55 |
const char * |
| 56 |
xkb_map_flags_string_iter(unsigned int *index, enum xkb_map_flags flags) |
| 57 |
{ |
| 58 |
if (!flags) |
| 59 |
return NULL; |
| 60 |
|
| 61 |
static const struct { |
| 62 |
enum xkb_map_flags flag; |
| 63 |
const char *name; |
| 64 |
} names[] = { |
| 65 |
{ MAP_IS_DEFAULT, "default" }, |
| 66 |
{ MAP_IS_PARTIAL, "partial" }, |
| 67 |
{ MAP_IS_HIDDEN, "hidden" }, |
| 68 |
{ MAP_HAS_ALPHANUMERIC, "alphanumeric" }, |
| 69 |
{ MAP_HAS_MODIFIER, "modifiers" }, |
| 70 |
{ MAP_HAS_KEYPAD, "keypad" }, |
| 71 |
{ MAP_HAS_FN, "fn" }, |
| 72 |
{ MAP_IS_ALTGR, "altgr" }, |
| 73 |
}; |
| 74 |
|
| 75 |
while (*index < ARRAY_SIZE(names)) { |
| 76 |
if (flags & names[*index].flag) |
| 77 |
return names[(*index)++].name; |
| 78 |
(*index)++; |
| 79 |
} |
| 80 |
|
| 81 |
return NULL; |
| 82 |
}; |
| 83 |
|
| 84 |
FILE * |
| 85 |
xkb_resolve_file(struct xkb_context *ctx, |
| 86 |
enum xkb_file_type file_type, |
| 87 |
const char *path, const char *map, |
| 88 |
char *resolved_path, size_t resolved_path_size, |
| 89 |
char *resolved_map, size_t resolved_map_size) |
| 90 |
{ |
| 91 |
unsigned int offset = 0; |
| 92 |
FILE *file = NULL; /* Exact match */ |
| 93 |
FILE *candidate = NULL; /* Weak match */ |
| 94 |
const size_t path_len = strlen(path); |
| 95 |
|
| 96 |
const bool absolute_path = is_absolute_path(path); |
| 97 |
if (absolute_path) { |
| 98 |
/* Absolute path: no need for lookup in XKB paths */ |
| 99 |
file = fopen(path, "rb"); |
| 100 |
} else { |
| 101 |
/* Relative path: lookup the first XKB path */ |
| 102 |
file = FindFileInXkbPath(ctx, "(unknown)", path, path_len, file_type, |
| 103 |
resolved_path, resolved_path_size, &offset, |
| 104 |
true); |
| 105 |
} |
| 106 |
|
| 107 |
while (file) { |
| 108 |
XkbFile * const xkb_file = XkbParseFile(ctx, file, path, map); |
| 109 |
if (xkb_file) { |
| 110 |
if (file_type < _FILE_TYPE_NUM_ENTRIES && |
| 111 |
xkb_file->file_type != file_type) { |
| 112 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 113 |
"File of wrong type (expected %s, got %s); " |
| 114 |
"file \"%s\" ignored\n", |
| 115 |
xkb_file_type_to_string(file_type), |
| 116 |
xkb_file_type_to_string(xkb_file->file_type), |
| 117 |
(absolute_path ? path : resolved_path)); |
| 118 |
goto invalid_file; |
| 119 |
} else if (map || (xkb_file->flags && MAP_IS_DEFAULT)) { |
| 120 |
/* |
| 121 |
* Exact match: explicit map name or explicit default map. |
| 122 |
* Lookup stops in this iteration. |
| 123 |
*/ |
| 124 |
if (!strcpy_safe(resolved_map, resolved_map_size, |
| 125 |
(xkb_file->name ? xkb_file->name : ""))) { |
| 126 |
FreeXkbFile(xkb_file); |
| 127 |
goto error; |
| 128 |
} |
| 129 |
} else if (!candidate) { |
| 130 |
/* |
| 131 |
* Weak match: looking for an explicit default map, but found |
| 132 |
* only the first *implicit* default map (e.g. first map of the |
| 133 |
* file). Use as fallback, but keep looking for an exact match. |
| 134 |
*/ |
| 135 |
candidate = file; |
| 136 |
if (!strcpy_safe(resolved_map, resolved_map_size, |
| 137 |
(xkb_file->name ? xkb_file->name : ""))) { |
| 138 |
FreeXkbFile(xkb_file); |
| 139 |
goto error; |
| 140 |
} |
| 141 |
} else { |
| 142 |
/* |
| 143 |
* Weak match, but we already have a previous candidate. |
| 144 |
* Keep looking for an exact match. |
| 145 |
*/ |
| 146 |
goto invalid_file; |
| 147 |
} |
| 148 |
} else { |
| 149 |
invalid_file: |
| 150 |
fclose(file); |
| 151 |
file = NULL; |
| 152 |
} |
| 153 |
|
| 154 |
FreeXkbFile(xkb_file); |
| 155 |
|
| 156 |
if (file != NULL || absolute_path) { |
| 157 |
/* Exact match or absolute path with no further file to search */ |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
offset++; |
| 162 |
file = FindFileInXkbPath(ctx, "(unknown)", |
| 163 |
path, path_len, file_type, |
| 164 |
resolved_path, resolved_path_size, |
| 165 |
&offset, true); |
| 166 |
} |
| 167 |
|
| 168 |
if (!file) { |
| 169 |
/* No exact match: use weak match, if any */ |
| 170 |
file = candidate; |
| 171 |
} else if (candidate) { |
| 172 |
/* Found exact match: discard weak match, if any */ |
| 173 |
fclose(candidate); |
| 174 |
} |
| 175 |
|
| 176 |
if (absolute_path && file && |
| 177 |
!strcpy_safe(resolved_path, resolved_path_size, path)) { |
| 178 |
goto error; |
| 179 |
} |
| 180 |
|
| 181 |
return file; |
| 182 |
|
| 183 |
error: |
| 184 |
log_err(ctx, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 185 |
"Cannot copy resolved path or section\n"); |
| 186 |
fclose(file); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
xkb_file_section_init(struct xkb_file_section *section) |
| 192 |
{ |
| 193 |
darray_init(section->include_groups); |
| 194 |
darray_init(section->includes); |
| 195 |
darray_init(section->buffer); |
| 196 |
darray_append(section->buffer, '\0'); |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
xkb_file_section_reset(struct xkb_file_section *section) |
| 201 |
{ |
| 202 |
darray_size(section->include_groups) = 0; |
| 203 |
darray_size(section->includes) = 0; |
| 204 |
darray_size(section->buffer) = 1; /* keep the dummy string */ |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
xkb_file_section_free(struct xkb_file_section *section) |
| 209 |
{ |
| 210 |
if (!section) |
| 211 |
return; |
| 212 |
|
| 213 |
darray_free(section->include_groups); |
| 214 |
darray_free(section->includes); |
| 215 |
darray_free(section->buffer); |
| 216 |
} |
| 217 |
|
| 218 |
static bool |
| 219 |
xkb_file_section_set_meta_data(struct xkb_context *ctx, |
| 220 |
struct xkb_file_section *section, |
| 221 |
const XkbFile *xkb_file) |
| 222 |
{ |
| 223 |
section->file_type = xkb_file->file_type; |
| 224 |
section->flags = xkb_file->flags; |
| 225 |
if (xkb_file->name) { |
| 226 |
darray_size_t idx = darray_size(section->buffer); |
| 227 |
darray_append_string0(section->buffer, xkb_file->name); |
| 228 |
section->name = idx; |
| 229 |
} else { |
| 230 |
section->name = 0; |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
/** Process a list of include statements */ |
| 236 |
static bool |
| 237 |
xkb_file_section_append_includes(struct xkb_context *ctx, |
| 238 |
enum xkb_file_iterator_flags flags, |
| 239 |
const char *section_path, |
| 240 |
struct xkb_file_section *section, |
| 241 |
enum xkb_file_type file_type, |
| 242 |
IncludeStmt * include) |
| 243 |
{ |
| 244 |
struct xkb_file_include_group *group = NULL; |
| 245 |
/* Note: this will flatten statements such as `include "pc+de"` */ |
| 246 |
for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) { |
| 247 |
char buf[PATH_MAX]; |
| 248 |
/* Parse the included file to check the include validity */ |
| 249 |
XkbFile *xkb_file = ProcessIncludeFile(ctx, stmt, file_type, buf, sizeof(buf)); |
| 250 |
const bool valid = (xkb_file != NULL); |
| 251 |
if (valid || !(flags & XKB_FILE_ITERATOR_FAIL_ON_INCLUDE_ERROR)) { |
| 252 |
/* Collect the strings of the statement properties */ |
| 253 |
|
| 254 |
const darray_size_t path = darray_size(section->buffer); |
| 255 |
darray_append_string0(section->buffer, buf); |
| 256 |
|
| 257 |
const darray_size_t file = darray_size(section->buffer); |
| 258 |
darray_append_string0(section->buffer, stmt->file); |
| 259 |
|
| 260 |
const darray_size_t section_name = |
| 261 |
(stmt->map != NULL || (valid && xkb_file->name)) |
| 262 |
? darray_size(section->buffer) |
| 263 |
: 0; |
| 264 |
if (section_name) { |
| 265 |
darray_append_string0(section->buffer, |
| 266 |
(stmt->map) ? stmt->map : xkb_file->name); |
| 267 |
} |
| 268 |
|
| 269 |
const darray_size_t modifier = (stmt->modifier) |
| 270 |
? darray_size(section->buffer) |
| 271 |
: 0; |
| 272 |
if (modifier) { |
| 273 |
darray_append_string0(section->buffer, stmt->modifier); |
| 274 |
} |
| 275 |
|
| 276 |
const enum xkb_map_flags section_flags = (valid) |
| 277 |
? xkb_file->flags |
| 278 |
: 0; |
| 279 |
|
| 280 |
/* Create and append the include statement */ |
| 281 |
const struct xkb_file_include inc = { |
| 282 |
.valid = valid, |
| 283 |
.explicit_section = (stmt->map != NULL), |
| 284 |
.merge = stmt->merge, |
| 285 |
.path = path, |
| 286 |
.file = file, |
| 287 |
.section = section_name, |
| 288 |
.modifier = modifier, |
| 289 |
.flags = section_flags |
| 290 |
}; |
| 291 |
const darray_size_t idx = darray_size(section->includes); |
| 292 |
darray_append(section->includes, inc); |
| 293 |
|
| 294 |
/* Update include group */ |
| 295 |
if (group == NULL) { |
| 296 |
const darray_size_t group_idx = |
| 297 |
darray_size(section->include_groups); |
| 298 |
darray_append( |
| 299 |
section->include_groups, |
| 300 |
(struct xkb_file_include_group) {.start = idx, .end = idx} |
| 301 |
); |
| 302 |
group = &darray_item(section->include_groups, group_idx); |
| 303 |
} else { |
| 304 |
group->end = idx; |
| 305 |
} |
| 306 |
|
| 307 |
FreeXkbFile(xkb_file); |
| 308 |
} else { |
| 309 |
const char * const name = |
| 310 |
xkb_file_section_get_string(section, section->name); |
| 311 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 312 |
"%s include failure in: %s%s%s%s\n", |
| 313 |
xkb_file_type_name(file_type), section_path, |
| 314 |
(section->name ? " (section: \"": ""), name, |
| 315 |
(section->name ? "\")": "")); |
| 316 |
FreeXkbFile(xkb_file); |
| 317 |
return false; |
| 318 |
} |
| 319 |
}; |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
/* Process the AST of a section */ |
| 324 |
static bool |
| 325 |
xkb_file_section_process(struct xkb_context *ctx, |
| 326 |
enum xkb_file_iterator_flags flags, |
| 327 |
const char *path, |
| 328 |
struct xkb_file_section *section, |
| 329 |
const XkbFile *xkb_file) |
| 330 |
{ |
| 331 |
bool ok = true; |
| 332 |
for (ParseCommon *stmt = xkb_file->defs; stmt; stmt = stmt->next) { |
| 333 |
if (stmt->type == STMT_INCLUDE) { |
| 334 |
ok = xkb_file_section_append_includes(ctx, flags, path, section, |
| 335 |
xkb_file->file_type, |
| 336 |
(IncludeStmt *) stmt); |
| 337 |
if (!ok) |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
return ok; |
| 342 |
} |
| 343 |
|
| 344 |
bool |
| 345 |
xkb_file_section_parse(struct xkb_context *ctx, |
| 346 |
enum xkb_file_iterator_flags iterator_flags, |
| 347 |
enum xkb_keymap_format format, |
| 348 |
enum xkb_keymap_compile_flags compile_flags, |
| 349 |
unsigned int include_depth, |
| 350 |
const char *path, const char *map, |
| 351 |
struct xkb_file_section *section) |
| 352 |
{ |
| 353 |
if (ExceedsIncludeMaxDepth(ctx, include_depth)) |
| 354 |
return false; |
| 355 |
|
| 356 |
FILE *file = fopen(path, "rb"); |
| 357 |
if (!file) { |
| 358 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 359 |
"Cannot open file: %s\n", path); |
| 360 |
return false; |
| 361 |
} |
| 362 |
|
| 363 |
XkbFile *xkb_file = XkbParseFile(ctx, file, path, map); |
| 364 |
fclose(file); |
| 365 |
if (!xkb_file) { |
| 366 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 367 |
"Cannot parse map \"%s\" in file: %s\n", |
| 368 |
(map ? map : "(no map)"), path); |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
xkb_file_section_reset(section); |
| 373 |
const bool no_includes = iterator_flags & XKB_FILE_ITERATOR_NO_INCLUDES; |
| 374 |
const bool ok = ( |
| 375 |
xkb_file_section_set_meta_data(ctx, section, xkb_file) && |
| 376 |
(no_includes || |
| 377 |
xkb_file_section_process(ctx, iterator_flags, path, section, xkb_file)) |
| 378 |
); |
| 379 |
FreeXkbFile(xkb_file); |
| 380 |
return ok; |
| 381 |
} |
| 382 |
|
| 383 |
struct xkb_file_iterator * |
| 384 |
xkb_file_iterator_new_from_buffer(struct xkb_context *ctx, |
| 385 |
enum xkb_file_iterator_flags iterator_flags, |
| 386 |
enum xkb_keymap_format format, |
| 387 |
enum xkb_keymap_compile_flags compile_flags, |
| 388 |
const char *path, const char *map, |
| 389 |
enum xkb_file_type file_type, |
| 390 |
const char *string, size_t length) |
| 391 |
{ |
| 392 |
struct xkb_file_iterator * const iter = calloc(1, sizeof(*iter)); |
| 393 |
if (!iter) { |
| 394 |
log_err(ctx, XKB_ERROR_ALLOCATION_ERROR, |
| 395 |
"Cannot allocate file iterator\n"); |
| 396 |
return NULL; |
| 397 |
} |
| 398 |
|
| 399 |
iter->flags = iterator_flags; |
| 400 |
iter->ctx = ctx; |
| 401 |
iter->path = path; |
| 402 |
iter->map = map; |
| 403 |
iter->type = file_type; |
| 404 |
xkb_file_section_init(&iter->section); |
| 405 |
|
| 406 |
if (!XkbParseStringInit(ctx, &iter->scanner, string, length, path, NULL)) { |
| 407 |
xkb_file_iterator_free(iter); |
| 408 |
return NULL; |
| 409 |
} |
| 410 |
|
| 411 |
return iter; |
| 412 |
} |
| 413 |
|
| 414 |
void |
| 415 |
xkb_file_iterator_free(struct xkb_file_iterator *iter) |
| 416 |
{ |
| 417 |
if (!iter) |
| 418 |
return; |
| 419 |
|
| 420 |
xkb_file_section_free(&iter->section); |
| 421 |
FreeXkbFile(iter->pending_xkb_file); |
| 422 |
free(iter); |
| 423 |
} |
| 424 |
|
| 425 |
bool |
| 426 |
xkb_file_iterator_next(struct xkb_file_iterator *iter, |
| 427 |
const struct xkb_file_section **section) |
| 428 |
{ |
| 429 |
if (iter->finished) { |
| 430 |
*section = NULL; |
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
next: |
| 435 |
{ /* C11 compatibility: label cannot be followed by a declaration */ } |
| 436 |
/* Parse next section in the file */ |
| 437 |
XkbFile *xkb_file = NULL; |
| 438 |
|
| 439 |
if (iter->pending_xkb_file) { |
| 440 |
/* We are parsing the components of a keymap */ |
| 441 |
if (iter->pending_section) { |
| 442 |
/* Parse next component */ |
| 443 |
xkb_file = iter->pending_section; |
| 444 |
goto parse_components; |
| 445 |
} else { |
| 446 |
/* No more components, try next keymap */ |
| 447 |
FreeXkbFile(iter->pending_xkb_file); |
| 448 |
iter->pending_xkb_file = NULL; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
if (!XkbParseStringNext(iter->ctx, &iter->scanner, iter->map, &xkb_file)) { |
| 453 |
log_err(iter->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 454 |
"Error while parsing section in file: %s\n", iter->path); |
| 455 |
goto error; |
| 456 |
} else if (!xkb_file) { |
| 457 |
/* No more sections */ |
| 458 |
iter->finished = true; |
| 459 |
*section = NULL; |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
parse_components: |
| 464 |
/* Reset section */ |
| 465 |
xkb_file_section_reset(&iter->section); |
| 466 |
|
| 467 |
/* Meta data */ |
| 468 |
if (!xkb_file_section_set_meta_data(iter->ctx, &iter->section, xkb_file)) |
| 469 |
goto error; |
| 470 |
|
| 471 |
/* Return current section */ |
| 472 |
*section = &iter->section; |
| 473 |
|
| 474 |
if (xkb_file->file_type == FILE_TYPE_KEYMAP) { |
| 475 |
/* |
| 476 |
* If it’s a keymap, then stop here. |
| 477 |
* Next iteration will process its components. |
| 478 |
*/ |
| 479 |
iter->pending_xkb_file = xkb_file; |
| 480 |
iter->pending_section = (XkbFile *) xkb_file->defs; |
| 481 |
iter->map = NULL; |
| 482 |
return true; |
| 483 |
} else { |
| 484 |
if (iter->type != FILE_TYPE_INVALID && |
| 485 |
xkb_file->file_type != iter->type) { |
| 486 |
if (iter->pending_xkb_file) { |
| 487 |
/* Within a keymap: filter out component */ |
| 488 |
iter->pending_section = (XkbFile *) xkb_file->common.next; |
| 489 |
goto next; |
| 490 |
} else { |
| 491 |
/* Component-specific file: type mismatch */ |
| 492 |
log_err(iter->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 493 |
"File type mismatch: %s, section: %s\n", |
| 494 |
iter->path, |
| 495 |
(xkb_file->name ? xkb_file->name : "(no name)")); |
| 496 |
goto error; |
| 497 |
} |
| 498 |
} |
| 499 |
if (iter->map) { |
| 500 |
iter->finished = true; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/* Collect include statements of current section */ |
| 505 |
const bool process_includes = !(iter->flags & XKB_FILE_ITERATOR_NO_INCLUDES); |
| 506 |
if (process_includes && |
| 507 |
!xkb_file_section_process(iter->ctx, iter->flags, iter->path, |
| 508 |
&iter->section, xkb_file)) { |
| 509 |
goto error; |
| 510 |
} else if (iter->pending_section) { |
| 511 |
/* Next component */ |
| 512 |
iter->pending_section = (XkbFile *) xkb_file->common.next; |
| 513 |
} else { |
| 514 |
/* No more component, free the file containing the components */ |
| 515 |
FreeXkbFile(xkb_file); |
| 516 |
} |
| 517 |
|
| 518 |
return true; |
| 519 |
|
| 520 |
error: |
| 521 |
FreeXkbFile(xkb_file); |
| 522 |
*section = NULL; |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
/* Lookup a string by its index */ |
| 527 |
const char * |
| 528 |
xkb_file_section_get_string(const struct xkb_file_section *section, darray_size_t idx) |
| 529 |
{ |
| 530 |
return &darray_item(section->buffer, idx); |
| 531 |
} |