| 1 |
/* |
| 2 |
* For HPND: |
| 3 |
* Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc. |
| 4 |
* |
| 5 |
* For MIT: |
| 6 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: HPND AND MIT |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <errno.h> |
| 14 |
#include <limits.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include "messages-codes.h" |
| 19 |
#include "utils.h" |
| 20 |
#include "xkbcomp-priv.h" |
| 21 |
#include "include.h" |
| 22 |
#include "scanner-utils.h" |
| 23 |
#include "utils-paths.h" |
| 24 |
|
| 25 |
/** |
| 26 |
* Parse an include statement. Each call returns a file name, along with |
| 27 |
* (possibly) a specific map in the file, an explicit group designator, and |
| 28 |
* the separator from the next file, used to determine the merge mode. |
| 29 |
* |
| 30 |
* @param str_inout Input statement, modified in-place. Should be passed in |
| 31 |
* repeatedly. If str_inout is NULL, the parsing has completed. |
| 32 |
* |
| 33 |
* @param file_rtrn Set to the name of the include file to be used. Combined |
| 34 |
* with an enum xkb_file_type, this determines which file to look for in the |
| 35 |
* include path. |
| 36 |
* |
| 37 |
* @param map_rtrn Set to the string between '(' and ')', if any. This will |
| 38 |
* result in the compilation of a specific named map within the file (e.g. |
| 39 |
* xkb_symbols "basic" { ... }) , as opposed to the default map of the file. |
| 40 |
* |
| 41 |
* @param nextop_rtrn Set to the next operation in the complete statement, |
| 42 |
* which is '\0' if it's the last file or '+' or '|' or '^' if there are more. |
| 43 |
* Separating the files with '+' sets the merge mode to `MERGE_MODE_OVERRIDE`, |
| 44 |
* while '|' sets the merge mode to `MERGE_MODE_AUGMENT` and '^' sets the merge |
| 45 |
* mode to `MERGE_REPLACE`. |
| 46 |
* |
| 47 |
* @param extra_data Set to the string after ':', if any. Currently the |
| 48 |
* extra data is only used for setting an explicit group index for a symbols |
| 49 |
* file. |
| 50 |
* |
| 51 |
* @return true if parsing was successful, false for an illegal string. |
| 52 |
* |
| 53 |
* Example: "evdev+aliases(qwerty):2" |
| 54 |
* str_inout = "aliases(qwerty):2" |
| 55 |
* file_rtrn = "evdev" |
| 56 |
* map_rtrn = NULL |
| 57 |
* nextop_retrn = "+" |
| 58 |
* extra_data = NULL |
| 59 |
* |
| 60 |
* 2nd run with "aliases(qwerty):2" |
| 61 |
* str_inout = NULL |
| 62 |
* file_rtrn = "aliases" |
| 63 |
* map_rtrn = "qwerty" |
| 64 |
* nextop_retrn = "" |
| 65 |
* extra_data = "2" |
| 66 |
* |
| 67 |
*/ |
| 68 |
bool |
| 69 |
ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn, |
| 70 |
char *nextop_rtrn, char **extra_data) |
| 71 |
{ |
| 72 |
char *tmp, *str, *next; |
| 73 |
|
| 74 |
str = *str_inout; |
| 75 |
|
| 76 |
/* |
| 77 |
* Find the position in the string where the next file is included, |
| 78 |
* if there is more than one left in the statement. |
| 79 |
*/ |
| 80 |
next = strpbrk(str, MERGE_MODE_PREFIXES); |
| 81 |
if (next) { |
| 82 |
/* Got more files, this function will be called again. */ |
| 83 |
*nextop_rtrn = *next; |
| 84 |
/* Separate the string, for strchr etc. to work on this file only. */ |
| 85 |
*next++ = '\0'; |
| 86 |
} |
| 87 |
else { |
| 88 |
/* This is the last file in this statement, won't be called again. */ |
| 89 |
*nextop_rtrn = '\0'; |
| 90 |
next = NULL; |
| 91 |
} |
| 92 |
|
| 93 |
/* |
| 94 |
* Search for the explicit group designator, if any. If it's there, |
| 95 |
* it goes after the file name and map. |
| 96 |
*/ |
| 97 |
tmp = strchr(str, ':'); |
| 98 |
if (tmp != NULL) { |
| 99 |
*tmp++ = '\0'; |
| 100 |
*extra_data = strdup(tmp); |
| 101 |
} |
| 102 |
else { |
| 103 |
*extra_data = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
/* Look for a map, if any. */ |
| 107 |
tmp = strchr(str, '('); |
| 108 |
if (tmp == NULL) { |
| 109 |
/* No map. */ |
| 110 |
*file_rtrn = strdup(str); |
| 111 |
*map_rtrn = NULL; |
| 112 |
} |
| 113 |
else if (str[0] == '(') { |
| 114 |
/* Map without file - invalid. */ |
| 115 |
free(*extra_data); |
| 116 |
return false; |
| 117 |
} |
| 118 |
else { |
| 119 |
/* Got a map; separate the file and the map for the strdup's. */ |
| 120 |
*tmp++ = '\0'; |
| 121 |
*file_rtrn = strdup(str); |
| 122 |
str = tmp; |
| 123 |
tmp = strchr(str, ')'); |
| 124 |
if (tmp == NULL || tmp[1] != '\0') { |
| 125 |
free(*file_rtrn); |
| 126 |
free(*extra_data); |
| 127 |
return false; |
| 128 |
} |
| 129 |
*tmp++ = '\0'; |
| 130 |
*map_rtrn = strdup(str); |
| 131 |
} |
| 132 |
|
| 133 |
/* Set up the next file for the next call, if any. */ |
| 134 |
if (*nextop_rtrn == '\0') |
| 135 |
*str_inout = NULL; |
| 136 |
else if (is_merge_mode_prefix(*nextop_rtrn)) |
| 137 |
*str_inout = next; |
| 138 |
else |
| 139 |
return false; |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
static const char *xkb_file_type_include_dirs[_FILE_TYPE_NUM_ENTRIES] = { |
| 145 |
[FILE_TYPE_KEYCODES] = "keycodes", |
| 146 |
[FILE_TYPE_TYPES] = "types", |
| 147 |
[FILE_TYPE_COMPAT] = "compat", |
| 148 |
[FILE_TYPE_SYMBOLS] = "symbols", |
| 149 |
[FILE_TYPE_GEOMETRY] = "geometry", |
| 150 |
[FILE_TYPE_KEYMAP] = "keymap", |
| 151 |
[FILE_TYPE_RULES] = "rules", |
| 152 |
}; |
| 153 |
|
| 154 |
/** |
| 155 |
* Return the xkb directory based on the type. |
| 156 |
*/ |
| 157 |
static const char * |
| 158 |
DirectoryForInclude(enum xkb_file_type type) |
| 159 |
{ |
| 160 |
if (type >= _FILE_TYPE_NUM_ENTRIES) |
| 161 |
return ""; |
| 162 |
return xkb_file_type_include_dirs[type]; |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
LogIncludePaths(struct xkb_context *ctx) |
| 167 |
{ |
| 168 |
if (xkb_context_num_include_paths(ctx) > 0) { |
| 169 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 170 |
"%u include paths searched:\n", |
| 171 |
xkb_context_num_include_paths(ctx)); |
| 172 |
for (unsigned int i = 0; i < xkb_context_num_include_paths(ctx); i++) |
| 173 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 174 |
"\t%s\n", |
| 175 |
xkb_context_include_path_get(ctx, i)); |
| 176 |
} |
| 177 |
else { |
| 178 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 179 |
"There are no include paths to search\n"); |
| 180 |
} |
| 181 |
|
| 182 |
if (xkb_context_num_failed_include_paths(ctx) > 0) { |
| 183 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 184 |
"%u include paths could not be added:\n", |
| 185 |
xkb_context_num_failed_include_paths(ctx)); |
| 186 |
for (darray_size_t i = 0; |
| 187 |
i < xkb_context_num_failed_include_paths(ctx); |
| 188 |
i++) |
| 189 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 190 |
"\t%s\n", |
| 191 |
xkb_context_failed_include_path_get(ctx, i)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
static size_t |
| 196 |
expand_percent(struct xkb_context *ctx, const char* parent_file_name, |
| 197 |
const char* typeDir, char *buf, size_t buf_size, |
| 198 |
const char *name, size_t name_len) |
| 199 |
{ |
| 200 |
struct scanner s; |
| 201 |
scanner_init(&s, ctx, name, name_len, parent_file_name, NULL); |
| 202 |
s.buf_pos = 0; |
| 203 |
|
| 204 |
while (!scanner_eof(&s) && !scanner_eol(&s)) { |
| 205 |
if (scanner_chr(&s, '%')) { |
| 206 |
if (scanner_chr(&s, '%')) { |
| 207 |
scanner_buf_append(&s, '%'); |
| 208 |
} |
| 209 |
else if (scanner_chr(&s, 'H')) { |
| 210 |
const char *home = xkb_context_getenv(ctx, "HOME"); |
| 211 |
if (!home) { |
| 212 |
scanner_err(&s, XKB_LOG_MESSAGE_NO_ID, |
| 213 |
"%%H was used in an include statement, but the " |
| 214 |
"HOME environment variable is not set"); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
if (!scanner_buf_appends(&s, home)) { |
| 218 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 219 |
"include path after expanding %%H is too long"); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
} |
| 223 |
else if (scanner_chr(&s, 'S')) { |
| 224 |
const char *default_root = |
| 225 |
xkb_context_include_path_get_system_path(ctx); |
| 226 |
if (!scanner_buf_appends(&s, default_root) || |
| 227 |
!scanner_buf_append(&s, '/') || |
| 228 |
!scanner_buf_appends(&s, typeDir)) { |
| 229 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 230 |
"include path after expanding %%S is too long"); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
} |
| 234 |
else if (scanner_chr(&s, 'E')) { |
| 235 |
const char *default_root = |
| 236 |
xkb_context_include_path_get_extra_path(ctx); |
| 237 |
if (!scanner_buf_appends(&s, default_root) || |
| 238 |
!scanner_buf_append(&s, '/') || |
| 239 |
!scanner_buf_appends(&s, typeDir)) { |
| 240 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 241 |
"include path after expanding %%E is too long"); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
} |
| 245 |
else { |
| 246 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 247 |
"unknown %% format (%c) in include statement", |
| 248 |
scanner_peek(&s)); |
| 249 |
return 0; |
| 250 |
} |
| 251 |
} |
| 252 |
else { |
| 253 |
scanner_buf_append(&s, scanner_next(&s)); |
| 254 |
} |
| 255 |
} |
| 256 |
if (!scanner_buf_append(&s, '\0')) { |
| 257 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 258 |
"include path is too long; max: %zu", sizeof(s.buf)); |
| 259 |
return 0; |
| 260 |
} |
| 261 |
if (unlikely(s.buf_pos > buf_size)) { |
| 262 |
scanner_err(&s, XKB_ERROR_INSUFFICIENT_BUFFER_SIZE, |
| 263 |
"include path is too long: %zu > %zu", s.buf_pos, buf_size); |
| 264 |
return 0; |
| 265 |
} |
| 266 |
memcpy(buf, s.buf, s.buf_pos); |
| 267 |
return s.buf_pos; |
| 268 |
} |
| 269 |
|
| 270 |
ssize_t |
| 271 |
expand_path(struct xkb_context *ctx, const char* parent_file_name, |
| 272 |
const char *name, size_t name_len, enum xkb_file_type type, |
| 273 |
char *buf, size_t buf_size) |
| 274 |
{ |
| 275 |
/* Detect %-expansion */ |
| 276 |
size_t k; |
| 277 |
for (k = 0; k < name_len; k++) { |
| 278 |
if (name[k] == '%') { |
| 279 |
goto expand; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return 0; |
| 284 |
|
| 285 |
expand: |
| 286 |
/* Process %-expansion */ |
| 287 |
|
| 288 |
/* Copy prefix (usual case: k == 0) */ |
| 289 |
if (unlikely(k >= buf_size)) { |
| 290 |
log_err(ctx, XKB_ERROR_INVALID_PATH, |
| 291 |
"Path is too long: %zu > %zu, " |
| 292 |
"got raw path: %.*s\n", |
| 293 |
k, buf_size, (unsigned int) name_len, name); |
| 294 |
return -1; |
| 295 |
} |
| 296 |
memcpy(buf, name, k); |
| 297 |
|
| 298 |
/* Proceed to %-expansion */ |
| 299 |
const char *typeDir = DirectoryForInclude(type); |
| 300 |
size_t count = expand_percent(ctx, parent_file_name, typeDir, |
| 301 |
buf + k, buf_size - k, |
| 302 |
name + k, name_len - k); |
| 303 |
if (!count) |
| 304 |
return -1; |
| 305 |
count += k; |
| 306 |
assert(buf[count - 1] == '\0'); |
| 307 |
return (ssize_t) count - 1; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Return an open file handle to the first file (counting from offset) with the |
| 312 |
* given name in the include paths, starting at the offset. |
| 313 |
* |
| 314 |
* offset must be zero the first time this is called and is set to the index the |
| 315 |
* file was found. Call again with offset+1 to keep searching through the |
| 316 |
* include paths. |
| 317 |
* |
| 318 |
* If this function returns NULL, no more files are available. |
| 319 |
*/ |
| 320 |
FILE * |
| 321 |
FindFileInXkbPath(struct xkb_context *ctx, const char* parent_file_name, |
| 322 |
const char *name, size_t name_len, enum xkb_file_type type, |
| 323 |
char *buf, size_t buf_size, unsigned int *offset, |
| 324 |
bool required) |
| 325 |
{ |
| 326 |
/* We do not handle absolute paths here */ |
| 327 |
assert(!is_absolute_path(name)); |
| 328 |
|
| 329 |
FILE *file = NULL; |
| 330 |
char *name_buffer = NULL; |
| 331 |
const char *typeDir = DirectoryForInclude(type); |
| 332 |
|
| 333 |
for (unsigned int i = *offset; i < xkb_context_num_include_paths(ctx); i++) { |
| 334 |
if (!snprintf_safe(buf, buf_size, "%s/%s/%.*s", |
| 335 |
xkb_context_include_path_get(ctx, i), |
| 336 |
typeDir, (unsigned int) name_len, name)) { |
| 337 |
log_err(ctx, XKB_ERROR_INVALID_PATH, |
| 338 |
"Path is too long: expected max length of %zu, " |
| 339 |
"got: %s/%s/%.*s\n", |
| 340 |
buf_size, xkb_context_include_path_get(ctx, i), |
| 341 |
typeDir, (unsigned int) name_len, name); |
| 342 |
continue; |
| 343 |
} |
| 344 |
|
| 345 |
file = fopen(buf, "rb"); |
| 346 |
if (file) { |
| 347 |
*offset = i; |
| 348 |
goto out; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/* We only print warnings if we can’t find the file on the first lookup */ |
| 353 |
if (required && *offset == 0) { |
| 354 |
log_err(ctx, XKB_ERROR_INCLUDED_FILE_NOT_FOUND, |
| 355 |
"Couldn't find file \"%s/%.*s\" in include paths\n", |
| 356 |
typeDir, (unsigned int) name_len, name); |
| 357 |
LogIncludePaths(ctx); |
| 358 |
} |
| 359 |
|
| 360 |
out: |
| 361 |
free(name_buffer); |
| 362 |
return file; |
| 363 |
} |
| 364 |
|
| 365 |
bool |
| 366 |
ExceedsIncludeMaxDepth(struct xkb_context *ctx, unsigned int include_depth) |
| 367 |
{ |
| 368 |
if (include_depth >= INCLUDE_MAX_DEPTH) { |
| 369 |
log_err(ctx, XKB_ERROR_RECURSIVE_INCLUDE, |
| 370 |
"Exceeded include depth threshold (%u)", |
| 371 |
INCLUDE_MAX_DEPTH); |
| 372 |
return true; |
| 373 |
} else { |
| 374 |
return false; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
XkbFile * |
| 379 |
ProcessIncludeFile(struct xkb_context *ctx, const IncludeStmt *stmt, |
| 380 |
enum xkb_file_type file_type, char *path, size_t path_size) |
| 381 |
{ |
| 382 |
/* |
| 383 |
* Resolve include statement: |
| 384 |
* - Explicit map name: look for an *exact match* only. |
| 385 |
* - Default map: look for an *explicit default* map (i.e. tagged with |
| 386 |
* `default`: exact match), else fallback to the first *implicit* default |
| 387 |
* map (weak match). |
| 388 |
*/ |
| 389 |
XkbFile *xkb_file = NULL; /* Exact match */ |
| 390 |
XkbFile *candidate = NULL; /* Weak match */ |
| 391 |
|
| 392 |
const char *stmt_file = stmt->file; |
| 393 |
size_t stmt_file_len = strlen(stmt_file); |
| 394 |
|
| 395 |
/* Process %-expansion, if any */ |
| 396 |
// FIXME: use parent file name instead of “(unknow)”. |
| 397 |
const ssize_t expanded = expand_path(ctx, "(unknown)", |
| 398 |
stmt_file, stmt_file_len, file_type, |
| 399 |
path, path_size); |
| 400 |
if (expanded < 0) { |
| 401 |
/* Error */ |
| 402 |
return NULL; |
| 403 |
} else if (expanded > 0) { |
| 404 |
/* %-expanded */ |
| 405 |
stmt_file = path; |
| 406 |
stmt_file_len = (size_t) expanded; |
| 407 |
assert(stmt_file[stmt_file_len] == '\0'); |
| 408 |
} |
| 409 |
|
| 410 |
/* Lookup the first candidate */ |
| 411 |
FILE* file; |
| 412 |
unsigned int offset = 0; |
| 413 |
const bool absolute_path = is_absolute_path(stmt_file); |
| 414 |
if (absolute_path) { |
| 415 |
/* Absolute path: no need for lookup in XKB paths */ |
| 416 |
assert(stmt_file[stmt_file_len] == '\0'); |
| 417 |
file = fopen(stmt_file, "rb"); |
| 418 |
} else { |
| 419 |
/* Relative path: lookup the first XKB path */ |
| 420 |
if (unlikely(expanded)) { |
| 421 |
/* |
| 422 |
* Relative path after expansion |
| 423 |
* |
| 424 |
* Unlikely to happen, because %-expansion is meant to use absolute |
| 425 |
* paths. Considering that: |
| 426 |
* - we do not resolve paths before expansion, leading to unexpected |
| 427 |
* result here; |
| 428 |
* - we need the buffer afterwards, but it currently contains the |
| 429 |
* expanded path; |
| 430 |
* - this is an edge case; |
| 431 |
* we simply make the lookup fail. |
| 432 |
*/ |
| 433 |
file = NULL; |
| 434 |
} else { |
| 435 |
// FIXME: use parent file name instead of “(unknow)”. |
| 436 |
file = FindFileInXkbPath(ctx, "(unknown)", |
| 437 |
stmt_file, stmt_file_len, file_type, |
| 438 |
path, path_size, &offset, true); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
while (file) { |
| 443 |
xkb_file = XkbParseFile(ctx, file, stmt->file, stmt->map); |
| 444 |
fclose(file); |
| 445 |
|
| 446 |
if (xkb_file) { |
| 447 |
if (xkb_file->file_type != file_type) { |
| 448 |
log_err(ctx, XKB_ERROR_INVALID_INCLUDED_FILE, |
| 449 |
"Include file of wrong type (expected %s, got %s); " |
| 450 |
"Include file \"%s\" ignored\n", |
| 451 |
xkb_file_type_to_string(file_type), |
| 452 |
xkb_file_type_to_string(xkb_file->file_type), stmt->file); |
| 453 |
FreeXkbFile(xkb_file); |
| 454 |
xkb_file = NULL; |
| 455 |
} else if (stmt->map || (xkb_file->flags && MAP_IS_DEFAULT)) { |
| 456 |
/* |
| 457 |
* Exact match: explicit map name or explicit default map. |
| 458 |
* Lookup stops here. |
| 459 |
*/ |
| 460 |
break; |
| 461 |
} else if (!candidate) { |
| 462 |
/* |
| 463 |
* Weak match: looking for an explicit default map, but found |
| 464 |
* only the first *implicit* default map (e.g. first map of the |
| 465 |
* file). Use as fallback, but keep looking for an exact match. |
| 466 |
*/ |
| 467 |
candidate = xkb_file; |
| 468 |
xkb_file = NULL; |
| 469 |
} else { |
| 470 |
/* |
| 471 |
* Weak match, but we already have a previous candidate. |
| 472 |
* Keep looking for an exact match. |
| 473 |
*/ |
| 474 |
FreeXkbFile(xkb_file); |
| 475 |
xkb_file = NULL; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if (absolute_path) { |
| 480 |
/* There is no point to search further if the path is absolute */ |
| 481 |
break; |
| 482 |
} |
| 483 |
|
| 484 |
offset++; |
| 485 |
file = FindFileInXkbPath(ctx, "(unknown)", |
| 486 |
stmt_file, stmt_file_len, file_type, |
| 487 |
path, path_size, &offset, true); |
| 488 |
} |
| 489 |
|
| 490 |
if (!xkb_file) { |
| 491 |
/* No exact match: use weak match, if any */ |
| 492 |
xkb_file = candidate; |
| 493 |
} else { |
| 494 |
/* Found exact match: discard weak match, if any */ |
| 495 |
FreeXkbFile(candidate); |
| 496 |
} |
| 497 |
|
| 498 |
if (!xkb_file) { |
| 499 |
if (stmt->map) |
| 500 |
log_err(ctx, XKB_ERROR_INVALID_INCLUDED_FILE, |
| 501 |
"Couldn't process include statement for '%s(%s)'\n", |
| 502 |
stmt->file, stmt->map); |
| 503 |
else |
| 504 |
log_err(ctx, XKB_ERROR_INVALID_INCLUDED_FILE, |
| 505 |
"Couldn't process include statement for '%s'\n", |
| 506 |
stmt->file); |
| 507 |
} |
| 508 |
|
| 509 |
return xkb_file; |
| 510 |
} |