Branch
Hash :
3203a010
Author :
Date :
2025-08-13T17:06:20
tools: Add internal introspection This tool enables simple analysis of XKB files with a YAML or DOT output: - resolve XKB paths; - list sections of a file; - list the includes of each section; - optionally process each include recursively. Additionally, the RDF Turtle output enables to query using the powerful SPARQL language. The tool is for internal use only for now, so that we can test it in various use cases before deciding if making it public.
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/*
* Copyright © 2025 Pierre Le Marre <dev@wismill.eu>
* SPDX-License-Identifier: MIT
*/
#include "config.h"
#include <limits.h>
#include <string.h>
#include "xkbcommon/xkbcommon.h"
#include "context.h"
#include "darray.h"
#include "messages-codes.h"
#include "utils.h"
#include "utils-paths.h"
#include "keymap-file-iterator.h"
#include "xkbcomp-priv.h"
#include "include.h"
#include "xkbcomp/ast.h"
const char *
xkb_file_type_name(enum xkb_file_type type)
{
if (type > FILE_TYPE_KEYMAP)
return "unknown";
static const char *xkb_file_type_strings[_FILE_TYPE_NUM_ENTRIES] = {
[FILE_TYPE_KEYCODES] = "keycodes",
[FILE_TYPE_TYPES] = "types",
[FILE_TYPE_COMPAT] = "compatibility",
[FILE_TYPE_SYMBOLS] = "symbols",
[FILE_TYPE_GEOMETRY] = "geometry",
[FILE_TYPE_KEYMAP] = "keymap",
};
return xkb_file_type_strings[type];
}
const char *
xkb_merge_mode_name(enum merge_mode merge)
{
if (merge >= _MERGE_MODE_NUM_ENTRIES)
return "unknown";
static const char *merge_mode_strings[_MERGE_MODE_NUM_ENTRIES] = {
[MERGE_DEFAULT] = "default",
[MERGE_AUGMENT] = "augment",
[MERGE_OVERRIDE] = "override",
[MERGE_REPLACE] = "replace",
};
return merge_mode_strings[merge];
}
/* Stateful lookup of map flags names */
const char *
xkb_map_flags_string_iter(unsigned int *index, enum xkb_map_flags flags)
{
if (!flags)
return NULL;
static const struct {
enum xkb_map_flags flag;
const char *name;
} names[] = {
{ MAP_IS_DEFAULT, "default" },
{ MAP_IS_PARTIAL, "partial" },
{ MAP_IS_HIDDEN, "hidden" },
{ MAP_HAS_ALPHANUMERIC, "alphanumeric" },
{ MAP_HAS_MODIFIER, "modifiers" },
{ MAP_HAS_KEYPAD, "keypad" },
{ MAP_HAS_FN, "fn" },
{ MAP_IS_ALTGR, "altgr" },
};
while (*index < ARRAY_SIZE(names)) {
if (flags & names[*index].flag)
return names[(*index)++].name;
(*index)++;
}
return NULL;
};
FILE *
xkb_resolve_file(struct xkb_context *ctx,
enum xkb_file_type file_type,
const char *path, const char *map,
char *resolved_path, size_t resolved_path_size,
char *resolved_map, size_t resolved_map_size)
{
unsigned int offset = 0;
FILE *file = NULL; /* Exact match */
FILE *candidate = NULL; /* Weak match */
const size_t path_len = strlen(path);
const bool absolute_path = is_absolute_path(path);
if (absolute_path) {
/* Absolute path: no need for lookup in XKB paths */
file = fopen(path, "rb");
} else {
/* Relative path: lookup the first XKB path */
file = FindFileInXkbPath(ctx, "(unknown)", path, path_len, file_type,
resolved_path, resolved_path_size, &offset);
}
while (file) {
XkbFile * const xkb_file = XkbParseFile(ctx, file, path, map);
if (xkb_file) {
if (file_type < _FILE_TYPE_NUM_ENTRIES &&
xkb_file->file_type != file_type) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"File of wrong type (expected %s, got %s); "
"file \"%s\" ignored\n",
xkb_file_type_to_string(file_type),
xkb_file_type_to_string(xkb_file->file_type),
(absolute_path ? path : resolved_path));
goto invalid_file;
} else if (map || (xkb_file->flags && MAP_IS_DEFAULT)) {
/*
* Exact match: explicit map name or explicit default map.
* Lookup stops in this iteration.
*/
if (!strcpy_safe(resolved_map, resolved_map_size,
(xkb_file->name ? xkb_file->name : ""))) {
FreeXkbFile(xkb_file);
goto error;
}
} else if (!candidate) {
/*
* Weak match: looking for an explicit default map, but found
* only the first *implicit* default map (e.g. first map of the
* file). Use as fallback, but keep looking for an exact match.
*/
candidate = file;
if (!strcpy_safe(resolved_map, resolved_map_size,
(xkb_file->name ? xkb_file->name : ""))) {
FreeXkbFile(xkb_file);
goto error;
}
} else {
/*
* Weak match, but we already have a previous candidate.
* Keep looking for an exact match.
*/
goto invalid_file;
}
} else {
invalid_file:
fclose(file);
file = NULL;
}
FreeXkbFile(xkb_file);
if (file != NULL || absolute_path) {
/* Exact match or absolute path with no further file to search */
break;
}
offset++;
file = FindFileInXkbPath(ctx, "(unknown)",
path, path_len, file_type,
resolved_path, resolved_path_size, &offset);
}
if (!file) {
/* No exact match: use weak match, if any */
file = candidate;
} else if (candidate) {
/* Found exact match: discard weak match, if any */
fclose(candidate);
}
if (absolute_path && file &&
!strcpy_safe(resolved_path, resolved_path_size, path)) {
goto error;
}
return file;
error:
log_err(ctx, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE,
"Cannot copy resolved path or section\n");
fclose(file);
return NULL;
}
void
xkb_file_section_init(struct xkb_file_section *section)
{
darray_init(section->include_groups);
darray_init(section->includes);
darray_init(section->buffer);
darray_append(section->buffer, '\0');
}
static void
xkb_file_section_reset(struct xkb_file_section *section)
{
darray_size(section->include_groups) = 0;
darray_size(section->includes) = 0;
darray_size(section->buffer) = 1; /* keep the dummy string */
}
void
xkb_file_section_free(struct xkb_file_section *section)
{
if (!section)
return;
darray_free(section->include_groups);
darray_free(section->includes);
darray_free(section->buffer);
}
static bool
xkb_file_section_set_meta_data(struct xkb_context *ctx,
struct xkb_file_section *section,
const XkbFile *xkb_file)
{
section->file_type = xkb_file->file_type;
section->flags = xkb_file->flags;
if (xkb_file->name) {
darray_size_t idx = darray_size(section->buffer);
darray_append_string0(section->buffer, xkb_file->name);
section->name = idx;
} else {
section->name = 0;
}
return true;
}
/** Process a list of include statements */
static bool
xkb_file_section_append_includes(struct xkb_context *ctx,
enum xkb_file_iterator_flags flags,
const char *section_path,
struct xkb_file_section *section,
enum xkb_file_type file_type,
IncludeStmt * include)
{
struct xkb_file_include_group *group = NULL;
/* Note: this will flatten statements such as `include "pc+de"` */
for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
char buf[PATH_MAX];
/* Parse the included file to check the include validity */
XkbFile *xkb_file = ProcessIncludeFile(ctx, stmt, file_type, buf, sizeof(buf));
const bool valid = (xkb_file != NULL);
if (valid || !(flags & XKB_FILE_ITERATOR_FAIL_ON_INCLUDE_ERROR)) {
/* Collect the strings of the statement properties */
const darray_size_t path = darray_size(section->buffer);
darray_append_string0(section->buffer, buf);
const darray_size_t file = darray_size(section->buffer);
darray_append_string0(section->buffer, stmt->file);
const darray_size_t section_name =
(stmt->map != NULL || (valid && xkb_file->name))
? darray_size(section->buffer)
: 0;
if (section_name) {
darray_append_string0(section->buffer,
(stmt->map) ? stmt->map : xkb_file->name);
}
const darray_size_t modifier = (stmt->modifier)
? darray_size(section->buffer)
: 0;
if (modifier) {
darray_append_string0(section->buffer, stmt->modifier);
}
const enum xkb_map_flags section_flags = (valid)
? xkb_file->flags
: 0;
/* Create and append the include statement */
const struct xkb_file_include inc = {
.valid = valid,
.explicit_section = (stmt->map != NULL),
.merge = stmt->merge,
.path = path,
.file = file,
.section = section_name,
.modifier = modifier,
.flags = section_flags
};
const darray_size_t idx = darray_size(section->includes);
darray_append(section->includes, inc);
/* Update include group */
if (group == NULL) {
const darray_size_t group_idx =
darray_size(section->include_groups);
darray_append(
section->include_groups,
(struct xkb_file_include_group) {.start = idx, .end = idx}
);
group = &darray_item(section->include_groups, group_idx);
} else {
group->end = idx;
}
FreeXkbFile(xkb_file);
} else {
const char * const name =
xkb_file_section_get_string(section, section->name);
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
"%s include failure in: %s%s%s%s\n",
xkb_file_type_name(file_type), section_path,
(section->name ? " (section: \"": ""), name,
(section->name ? "\")": ""));
FreeXkbFile(xkb_file);
return false;
}
};
return true;
}
/* Process the AST of a section */
static bool
xkb_file_section_process(struct xkb_context *ctx,
enum xkb_file_iterator_flags flags,
const char *path,
struct xkb_file_section *section,
const XkbFile *xkb_file)
{
bool ok = true;
for (ParseCommon *stmt = xkb_file->defs; stmt; stmt = stmt->next) {
if (stmt->type == STMT_INCLUDE) {
ok = xkb_file_section_append_includes(ctx, flags, path, section,
xkb_file->file_type,
(IncludeStmt *) stmt);
if (!ok)
break;
}
}
return ok;
}
bool
xkb_file_section_parse(struct xkb_context *ctx,
enum xkb_file_iterator_flags iterator_flags,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags compile_flags,
unsigned int include_depth,
const char *path, const char *map,
struct xkb_file_section *section)
{
if (ExceedsIncludeMaxDepth(ctx, include_depth))
return false;
FILE *file = fopen(path, "rb");
if (!file) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"Cannot open file: %s\n", path);
return false;
}
XkbFile *xkb_file = XkbParseFile(ctx, file, path, map);
fclose(file);
if (!xkb_file) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"Cannot parse map \"%s\" in file: %s\n",
(map ? map : "(no map)"), path);
return false;
}
xkb_file_section_reset(section);
const bool no_includes = iterator_flags & XKB_FILE_ITERATOR_NO_INCLUDES;
const bool ok = (
xkb_file_section_set_meta_data(ctx, section, xkb_file) &&
(no_includes ||
xkb_file_section_process(ctx, iterator_flags, path, section, xkb_file))
);
FreeXkbFile(xkb_file);
return ok;
}
struct xkb_file_iterator *
xkb_file_iterator_new_from_buffer(struct xkb_context *ctx,
enum xkb_file_iterator_flags iterator_flags,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags compile_flags,
const char *path, const char *map,
enum xkb_file_type file_type,
const char *string, size_t length)
{
struct xkb_file_iterator * const iter = calloc(1, sizeof(*iter));
if (!iter) {
log_err(ctx, XKB_ERROR_ALLOCATION_ERROR,
"Cannot allocate file iterator\n");
return NULL;
}
iter->flags = iterator_flags;
iter->ctx = ctx;
iter->path = path;
iter->map = map;
iter->type = file_type;
xkb_file_section_init(&iter->section);
if (!XkbParseStringInit(ctx, &iter->scanner, string, length, path, NULL)) {
xkb_file_iterator_free(iter);
return NULL;
}
return iter;
}
void
xkb_file_iterator_free(struct xkb_file_iterator *iter)
{
if (!iter)
return;
xkb_file_section_free(&iter->section);
FreeXkbFile(iter->pending_xkb_file);
free(iter);
}
bool
xkb_file_iterator_next(struct xkb_file_iterator *iter,
const struct xkb_file_section **section)
{
if (iter->finished) {
*section = NULL;
return true;
}
next:
{ /* C11 compatibility: label cannot be followed by a declaration */ }
/* Parse next section in the file */
XkbFile *xkb_file = NULL;
if (iter->pending_xkb_file) {
/* We are parsing the components of a keymap */
if (iter->pending_section) {
/* Parse next component */
xkb_file = iter->pending_section;
goto parse_components;
} else {
/* No more components, try next keymap */
FreeXkbFile(iter->pending_xkb_file);
iter->pending_xkb_file = NULL;
}
}
if (!XkbParseStringNext(iter->ctx, &iter->scanner, iter->map, &xkb_file)) {
log_err(iter->ctx, XKB_LOG_MESSAGE_NO_ID,
"Error while parsing section in file: %s\n", iter->path);
goto error;
} else if (!xkb_file) {
/* No more sections */
iter->finished = true;
*section = NULL;
return true;
}
parse_components:
/* Reset section */
xkb_file_section_reset(&iter->section);
/* Meta data */
if (!xkb_file_section_set_meta_data(iter->ctx, &iter->section, xkb_file))
goto error;
/* Return current section */
*section = &iter->section;
if (xkb_file->file_type == FILE_TYPE_KEYMAP) {
/*
* If it’s a keymap, then stop here.
* Next iteration will process its components.
*/
iter->pending_xkb_file = xkb_file;
iter->pending_section = (XkbFile *) xkb_file->defs;
iter->map = NULL;
return true;
} else {
if (iter->type != FILE_TYPE_INVALID &&
xkb_file->file_type != iter->type) {
if (iter->pending_xkb_file) {
/* Within a keymap: filter out component */
iter->pending_section = (XkbFile *) xkb_file->common.next;
goto next;
} else {
/* Component-specific file: type mismatch */
log_err(iter->ctx, XKB_LOG_MESSAGE_NO_ID,
"File type mismatch: %s, section: %s\n",
iter->path,
(xkb_file->name ? xkb_file->name : "(no name)"));
goto error;
}
}
if (iter->map) {
iter->finished = true;
}
}
/* Collect include statements of current section */
const bool process_includes = !(iter->flags & XKB_FILE_ITERATOR_NO_INCLUDES);
if (process_includes &&
!xkb_file_section_process(iter->ctx, iter->flags, iter->path,
&iter->section, xkb_file)) {
goto error;
} else if (iter->pending_section) {
/* Next component */
iter->pending_section = (XkbFile *) xkb_file->common.next;
} else {
/* No more component, free the file containing the components */
FreeXkbFile(xkb_file);
}
return true;
error:
FreeXkbFile(xkb_file);
*section = NULL;
return false;
}
/* Lookup a string by its index */
const char *
xkb_file_section_get_string(const struct xkb_file_section *section, darray_size_t idx)
{
return &darray_item(section->buffer, idx);
}