| 1 |
/* |
| 2 |
* For HPND |
| 3 |
* Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc. |
| 4 |
* |
| 5 |
* For MIT: |
| 6 |
* Copyright © 2012 Intel Corporation |
| 7 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: HPND AND MIT |
| 10 |
* |
| 11 |
* Author: Daniel Stone <[email protected]> |
| 12 |
* Author: Ran Benita <[email protected]> |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "config.h" |
| 16 |
|
| 17 |
#include <stdint.h> |
| 18 |
|
| 19 |
#include "xkbcommon/xkbcommon-keysyms.h" |
| 20 |
#include "messages-codes.h" |
| 21 |
#include "xkbcomp-priv.h" |
| 22 |
#include "ast.h" |
| 23 |
#include "ast-build.h" |
| 24 |
#include "include.h" |
| 25 |
#include "keysym.h" |
| 26 |
#include "utf8-decoding.h" |
| 27 |
|
| 28 |
static ExprDef * |
| 29 |
ExprCreate(enum stmt_type op) |
| 30 |
{ |
| 31 |
ExprDef *expr = malloc(sizeof(*expr)); |
| 32 |
if (!expr) |
| 33 |
return NULL; |
| 34 |
|
| 35 |
expr->common.type = op; |
| 36 |
expr->common.next = NULL; |
| 37 |
|
| 38 |
return expr; |
| 39 |
} |
| 40 |
|
| 41 |
ExprDef * |
| 42 |
ExprCreateString(xkb_atom_t str) |
| 43 |
{ |
| 44 |
ExprDef *expr = ExprCreate(STMT_EXPR_STRING_LITERAL); |
| 45 |
if (!expr) |
| 46 |
return NULL; |
| 47 |
expr->string.str = str; |
| 48 |
return expr; |
| 49 |
} |
| 50 |
|
| 51 |
ExprDef * |
| 52 |
ExprCreateInteger(int64_t ival) |
| 53 |
{ |
| 54 |
ExprDef *expr = ExprCreate(STMT_EXPR_INTEGER_LITERAL); |
| 55 |
if (!expr) |
| 56 |
return NULL; |
| 57 |
expr->integer.ival = ival; |
| 58 |
return expr; |
| 59 |
} |
| 60 |
|
| 61 |
ExprDef * |
| 62 |
ExprCreateFloat(void) |
| 63 |
{ |
| 64 |
ExprDef *expr = ExprCreate(STMT_EXPR_FLOAT_LITERAL); |
| 65 |
if (!expr) |
| 66 |
return NULL; |
| 67 |
return expr; |
| 68 |
} |
| 69 |
|
| 70 |
ExprDef * |
| 71 |
ExprCreateBoolean(bool set) |
| 72 |
{ |
| 73 |
ExprDef *expr = ExprCreate(STMT_EXPR_BOOLEAN_LITERAL); |
| 74 |
if (!expr) |
| 75 |
return NULL; |
| 76 |
expr->boolean.set = set; |
| 77 |
return expr; |
| 78 |
} |
| 79 |
|
| 80 |
ExprDef * |
| 81 |
ExprCreateKeyName(xkb_atom_t key_name) |
| 82 |
{ |
| 83 |
ExprDef *expr = ExprCreate(STMT_EXPR_KEYNAME_LITERAL); |
| 84 |
if (!expr) |
| 85 |
return NULL; |
| 86 |
expr->key_name.key_name = key_name; |
| 87 |
return expr; |
| 88 |
} |
| 89 |
|
| 90 |
ExprDef * |
| 91 |
ExprCreateKeySym(xkb_keysym_t keysym) |
| 92 |
{ |
| 93 |
ExprDef *expr = ExprCreate(STMT_EXPR_KEYSYM_LITERAL); |
| 94 |
if (!expr) |
| 95 |
return NULL; |
| 96 |
expr->keysym.keysym = keysym; |
| 97 |
return expr; |
| 98 |
} |
| 99 |
|
| 100 |
ExprDef * |
| 101 |
ExprCreateIdent(xkb_atom_t ident) |
| 102 |
{ |
| 103 |
ExprDef *expr = ExprCreate(STMT_EXPR_IDENT); |
| 104 |
if (!expr) |
| 105 |
return NULL; |
| 106 |
expr->ident.ident = ident; |
| 107 |
return expr; |
| 108 |
} |
| 109 |
|
| 110 |
ExprDef * |
| 111 |
ExprCreateUnary(enum stmt_type op, ExprDef *child) |
| 112 |
{ |
| 113 |
ExprDef *expr = ExprCreate(op); |
| 114 |
if (!expr) |
| 115 |
return NULL; |
| 116 |
expr->unary.child = child; |
| 117 |
return expr; |
| 118 |
} |
| 119 |
|
| 120 |
ExprDef * |
| 121 |
ExprCreateBinary(enum stmt_type op, ExprDef *left, ExprDef *right) |
| 122 |
{ |
| 123 |
ExprDef *expr = ExprCreate(op); |
| 124 |
if (!expr) |
| 125 |
return NULL; |
| 126 |
|
| 127 |
expr->binary.left = left; |
| 128 |
expr->binary.right = right; |
| 129 |
|
| 130 |
return expr; |
| 131 |
} |
| 132 |
|
| 133 |
ExprDef * |
| 134 |
ExprCreateFieldRef(xkb_atom_t element, xkb_atom_t field) |
| 135 |
{ |
| 136 |
ExprDef *expr = ExprCreate(STMT_EXPR_FIELD_REF); |
| 137 |
if (!expr) |
| 138 |
return NULL; |
| 139 |
expr->field_ref.element = element; |
| 140 |
expr->field_ref.field = field; |
| 141 |
return expr; |
| 142 |
} |
| 143 |
|
| 144 |
ExprDef * |
| 145 |
ExprCreateArrayRef(xkb_atom_t element, xkb_atom_t field, ExprDef *entry) |
| 146 |
{ |
| 147 |
ExprDef *expr = ExprCreate(STMT_EXPR_ARRAY_REF); |
| 148 |
if (!expr) |
| 149 |
return NULL; |
| 150 |
expr->array_ref.element = element; |
| 151 |
expr->array_ref.field = field; |
| 152 |
expr->array_ref.entry = entry; |
| 153 |
return expr; |
| 154 |
} |
| 155 |
|
| 156 |
ExprDef * |
| 157 |
ExprEmptyList(void) |
| 158 |
{ |
| 159 |
return ExprCreate(STMT_EXPR_EMPTY_LIST); |
| 160 |
} |
| 161 |
|
| 162 |
ExprDef * |
| 163 |
ExprCreateAction(xkb_atom_t name, ExprDef *args) |
| 164 |
{ |
| 165 |
ExprDef *expr = ExprCreate(STMT_EXPR_ACTION_DECL); |
| 166 |
if (!expr) |
| 167 |
return NULL; |
| 168 |
expr->action.name = name; |
| 169 |
expr->action.args = args; |
| 170 |
return expr; |
| 171 |
} |
| 172 |
|
| 173 |
ExprDef * |
| 174 |
ExprCreateActionList(ExprDef *actions) |
| 175 |
{ |
| 176 |
ExprDef *expr = ExprCreate(STMT_EXPR_ACTION_LIST); |
| 177 |
if (!expr) |
| 178 |
return NULL; |
| 179 |
expr->actions.actions = actions; |
| 180 |
return expr; |
| 181 |
} |
| 182 |
|
| 183 |
ExprDef * |
| 184 |
ExprCreateKeySymList(xkb_keysym_t sym) |
| 185 |
{ |
| 186 |
ExprDef *expr = ExprCreate(STMT_EXPR_KEYSYM_LIST); |
| 187 |
if (!expr) |
| 188 |
return NULL; |
| 189 |
darray_init(expr->keysym_list.syms); |
| 190 |
if (sym == XKB_KEY_NoSymbol) { |
| 191 |
/* Discard NoSymbol */ |
| 192 |
} else { |
| 193 |
darray_append(expr->keysym_list.syms, sym); |
| 194 |
} |
| 195 |
return expr; |
| 196 |
} |
| 197 |
|
| 198 |
ExprDef * |
| 199 |
ExprAppendKeySymList(ExprDef *expr, xkb_keysym_t sym) |
| 200 |
{ |
| 201 |
if (sym == XKB_KEY_NoSymbol) { |
| 202 |
/* Discard NoSymbol */ |
| 203 |
} else { |
| 204 |
darray_append(expr->keysym_list.syms, sym); |
| 205 |
} |
| 206 |
return expr; |
| 207 |
} |
| 208 |
|
| 209 |
ExprDef * |
| 210 |
ExprKeySymListAppendString(struct scanner *scanner, |
| 211 |
ExprDef *expr, const char *string) |
| 212 |
{ |
| 213 |
/* TODO: use strnlen with max len = 4 * MAX_KEYSYMS_LIST_LENGTH */ |
| 214 |
const size_t len = strlen(string); |
| 215 |
size_t idx = 0; |
| 216 |
size_t idx_cp = 1; |
| 217 |
while (idx < len) { |
| 218 |
size_t count = 0; |
| 219 |
uint32_t cp = utf8_next_code_point(string + idx, len - idx, &count); |
| 220 |
if (cp == INVALID_UTF8_CODE_POINT) { |
| 221 |
scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 222 |
"Cannot convert string to keysyms: " |
| 223 |
"Invalid UTF-8 encoding starting at byte position %zu " |
| 224 |
"(code point position: %zu).", |
| 225 |
idx + 1, idx_cp); |
| 226 |
goto error; |
| 227 |
} |
| 228 |
const xkb_keysym_t sym = xkb_utf32_to_keysym(cp); |
| 229 |
if (sym == XKB_KEY_NoSymbol) { |
| 230 |
scanner_err(scanner, XKB_LOG_MESSAGE_NO_ID, |
| 231 |
"Cannot convert string to keysyms: Unicode code point " |
| 232 |
"U+04%"PRIX32" has no keysym equivalent" |
| 233 |
"(byte position: %zu, code point position: %zu).", |
| 234 |
cp, idx + 1, idx_cp); |
| 235 |
goto error; |
| 236 |
} |
| 237 |
darray_append(expr->keysym_list.syms, sym); |
| 238 |
idx += count; |
| 239 |
idx_cp++; |
| 240 |
} |
| 241 |
assert(string[idx] == '\0'); |
| 242 |
return expr; |
| 243 |
error: |
| 244 |
FreeStmt((ParseCommon*) expr); |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
|
| 248 |
xkb_keysym_t |
| 249 |
KeysymParseString(struct scanner *scanner, const char *string) |
| 250 |
{ |
| 251 |
const size_t len = strlen(string); |
| 252 |
if (len == 0) { |
| 253 |
scanner_err(scanner, XKB_LOG_MESSAGE_NO_ID, |
| 254 |
"Cannot convert string to single keysym: empty string."); |
| 255 |
return XKB_KEY_NoSymbol; |
| 256 |
} |
| 257 |
size_t count = 0; |
| 258 |
const uint32_t cp = utf8_next_code_point(string, len, &count); |
| 259 |
if (cp == INVALID_UTF8_CODE_POINT) { |
| 260 |
scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 261 |
"Cannot convert string to single keysym: " |
| 262 |
"Invalid UTF-8 encoding."); |
| 263 |
return XKB_KEY_NoSymbol; |
| 264 |
} else if (count != len) { |
| 265 |
scanner_err(scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 266 |
"Cannot convert string to single keysym: " |
| 267 |
"Expected a single Unicode code point, got: \"%s\".", |
| 268 |
string); |
| 269 |
return XKB_KEY_NoSymbol; |
| 270 |
} |
| 271 |
const xkb_keysym_t sym = xkb_utf32_to_keysym(cp); |
| 272 |
if (sym == XKB_KEY_NoSymbol) { |
| 273 |
scanner_err(scanner, XKB_LOG_MESSAGE_NO_ID, |
| 274 |
"Cannot convert string to single keysym: Unicode " |
| 275 |
"code point U+%04"PRIX32" has no keysym equivalent.", |
| 276 |
cp); |
| 277 |
} |
| 278 |
return sym; |
| 279 |
} |
| 280 |
|
| 281 |
KeycodeDef * |
| 282 |
KeycodeCreate(xkb_atom_t name, int64_t value) |
| 283 |
{ |
| 284 |
KeycodeDef *def = malloc(sizeof(*def)); |
| 285 |
if (!def) |
| 286 |
return NULL; |
| 287 |
|
| 288 |
def->common.type = STMT_KEYCODE; |
| 289 |
def->common.next = NULL; |
| 290 |
def->name = name; |
| 291 |
def->value = value; |
| 292 |
|
| 293 |
return def; |
| 294 |
} |
| 295 |
|
| 296 |
KeyAliasDef * |
| 297 |
KeyAliasCreate(xkb_atom_t alias, xkb_atom_t real) |
| 298 |
{ |
| 299 |
KeyAliasDef *def = malloc(sizeof(*def)); |
| 300 |
if (!def) |
| 301 |
return NULL; |
| 302 |
|
| 303 |
def->common.type = STMT_ALIAS; |
| 304 |
def->common.next = NULL; |
| 305 |
def->alias = alias; |
| 306 |
def->real = real; |
| 307 |
|
| 308 |
return def; |
| 309 |
} |
| 310 |
|
| 311 |
VModDef * |
| 312 |
VModCreate(xkb_atom_t name, ExprDef *value) |
| 313 |
{ |
| 314 |
VModDef *def = malloc(sizeof(*def)); |
| 315 |
if (!def) |
| 316 |
return NULL; |
| 317 |
|
| 318 |
def->common.type = STMT_VMOD; |
| 319 |
def->common.next = NULL; |
| 320 |
def->name = name; |
| 321 |
def->value = value; |
| 322 |
|
| 323 |
return def; |
| 324 |
} |
| 325 |
|
| 326 |
VarDef * |
| 327 |
VarCreate(ExprDef *name, ExprDef *value) |
| 328 |
{ |
| 329 |
VarDef *def = malloc(sizeof(*def)); |
| 330 |
if (!def) |
| 331 |
return NULL; |
| 332 |
|
| 333 |
def->common.type = STMT_VAR; |
| 334 |
def->common.next = NULL; |
| 335 |
def->name = name; |
| 336 |
def->value = value; |
| 337 |
|
| 338 |
return def; |
| 339 |
} |
| 340 |
|
| 341 |
VarDef * |
| 342 |
BoolVarCreate(xkb_atom_t ident, bool set) |
| 343 |
{ |
| 344 |
ExprDef *name, *value; |
| 345 |
VarDef *def; |
| 346 |
if (!(name = ExprCreateIdent(ident))) { |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
if (!(value = ExprCreateBoolean(set))) { |
| 350 |
FreeStmt((ParseCommon *) name); |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
if (!(def = VarCreate(name, value))) { |
| 354 |
FreeStmt((ParseCommon *) name); |
| 355 |
FreeStmt((ParseCommon *) value); |
| 356 |
return NULL; |
| 357 |
} |
| 358 |
return def; |
| 359 |
} |
| 360 |
|
| 361 |
InterpDef * |
| 362 |
InterpCreate(xkb_keysym_t sym, ExprDef *match) |
| 363 |
{ |
| 364 |
InterpDef *def = malloc(sizeof(*def)); |
| 365 |
if (!def) |
| 366 |
return NULL; |
| 367 |
|
| 368 |
def->common.type = STMT_INTERP; |
| 369 |
def->common.next = NULL; |
| 370 |
def->sym = sym; |
| 371 |
def->match = match; |
| 372 |
def->def = NULL; |
| 373 |
|
| 374 |
return def; |
| 375 |
} |
| 376 |
|
| 377 |
KeyTypeDef * |
| 378 |
KeyTypeCreate(xkb_atom_t name, VarDef *body) |
| 379 |
{ |
| 380 |
KeyTypeDef *def = malloc(sizeof(*def)); |
| 381 |
if (!def) |
| 382 |
return NULL; |
| 383 |
|
| 384 |
def->common.type = STMT_TYPE; |
| 385 |
def->common.next = NULL; |
| 386 |
def->merge = MERGE_DEFAULT; |
| 387 |
def->name = name; |
| 388 |
def->body = body; |
| 389 |
|
| 390 |
return def; |
| 391 |
} |
| 392 |
|
| 393 |
SymbolsDef * |
| 394 |
SymbolsCreate(xkb_atom_t keyName, VarDef *symbols) |
| 395 |
{ |
| 396 |
SymbolsDef *def = malloc(sizeof(*def)); |
| 397 |
if (!def) |
| 398 |
return NULL; |
| 399 |
|
| 400 |
def->common.type = STMT_SYMBOLS; |
| 401 |
def->common.next = NULL; |
| 402 |
def->merge = MERGE_DEFAULT; |
| 403 |
def->keyName = keyName; |
| 404 |
def->symbols = symbols; |
| 405 |
|
| 406 |
return def; |
| 407 |
} |
| 408 |
|
| 409 |
GroupCompatDef * |
| 410 |
GroupCompatCreate(int64_t group, ExprDef *val) |
| 411 |
{ |
| 412 |
GroupCompatDef *def = malloc(sizeof(*def)); |
| 413 |
if (!def) |
| 414 |
return NULL; |
| 415 |
|
| 416 |
def->common.type = STMT_GROUP_COMPAT; |
| 417 |
def->common.next = NULL; |
| 418 |
def->merge = MERGE_DEFAULT; |
| 419 |
def->group = group; |
| 420 |
def->def = val; |
| 421 |
|
| 422 |
return def; |
| 423 |
} |
| 424 |
|
| 425 |
ModMapDef * |
| 426 |
ModMapCreate(xkb_atom_t modifier, ExprDef *keys) |
| 427 |
{ |
| 428 |
ModMapDef *def = malloc(sizeof(*def)); |
| 429 |
if (!def) |
| 430 |
return NULL; |
| 431 |
|
| 432 |
def->common.type = STMT_MODMAP; |
| 433 |
def->common.next = NULL; |
| 434 |
def->merge = MERGE_DEFAULT; |
| 435 |
def->modifier = modifier; |
| 436 |
def->keys = keys; |
| 437 |
|
| 438 |
return def; |
| 439 |
} |
| 440 |
|
| 441 |
LedMapDef * |
| 442 |
LedMapCreate(xkb_atom_t name, VarDef *body) |
| 443 |
{ |
| 444 |
LedMapDef *def = malloc(sizeof(*def)); |
| 445 |
if (!def) |
| 446 |
return NULL; |
| 447 |
|
| 448 |
def->common.type = STMT_LED_MAP; |
| 449 |
def->common.next = NULL; |
| 450 |
def->merge = MERGE_DEFAULT; |
| 451 |
def->name = name; |
| 452 |
def->body = body; |
| 453 |
|
| 454 |
return def; |
| 455 |
} |
| 456 |
|
| 457 |
LedNameDef * |
| 458 |
LedNameCreate(int64_t ndx, ExprDef *name, bool virtual) |
| 459 |
{ |
| 460 |
LedNameDef *def = malloc(sizeof(*def)); |
| 461 |
if (!def) |
| 462 |
return NULL; |
| 463 |
|
| 464 |
def->common.type = STMT_LED_NAME; |
| 465 |
def->common.next = NULL; |
| 466 |
def->merge = MERGE_DEFAULT; |
| 467 |
def->ndx = ndx; |
| 468 |
def->name = name; |
| 469 |
def->virtual = virtual; |
| 470 |
|
| 471 |
return def; |
| 472 |
} |
| 473 |
|
| 474 |
static void |
| 475 |
FreeInclude(IncludeStmt *incl); |
| 476 |
|
| 477 |
IncludeStmt * |
| 478 |
IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge) |
| 479 |
{ |
| 480 |
IncludeStmt *incl, *first; |
| 481 |
char *stmt, *tmp; |
| 482 |
char nextop; |
| 483 |
|
| 484 |
incl = first = NULL; |
| 485 |
tmp = str; |
| 486 |
stmt = strdup_safe(str); |
| 487 |
while (tmp && *tmp) |
| 488 |
{ |
| 489 |
char *file = NULL, *map = NULL, *extra_data = NULL; |
| 490 |
|
| 491 |
if (!ParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data)) |
| 492 |
goto err; |
| 493 |
|
| 494 |
/* |
| 495 |
* Given an RMLVO (here layout) like 'us,,fr', the rules parser |
| 496 |
* will give out something like 'pc+us+:2+fr:3+inet(evdev)'. |
| 497 |
* We should just skip the ':2' in this case and leave it to the |
| 498 |
* appropriate section to deal with the empty group. |
| 499 |
*/ |
| 500 |
if (isempty(file)) { |
| 501 |
free(file); |
| 502 |
free(map); |
| 503 |
free(extra_data); |
| 504 |
continue; |
| 505 |
} |
| 506 |
|
| 507 |
if (first == NULL) { |
| 508 |
first = incl = malloc(sizeof(*first)); |
| 509 |
} else { |
| 510 |
incl->next_incl = malloc(sizeof(*first)); |
| 511 |
incl = incl->next_incl; |
| 512 |
} |
| 513 |
|
| 514 |
if (!incl) { |
| 515 |
free(file); |
| 516 |
free(map); |
| 517 |
free(extra_data); |
| 518 |
break; |
| 519 |
} |
| 520 |
|
| 521 |
incl->common.type = STMT_INCLUDE; |
| 522 |
incl->common.next = NULL; |
| 523 |
incl->merge = merge; |
| 524 |
incl->stmt = NULL; |
| 525 |
incl->file = file; |
| 526 |
incl->map = map; |
| 527 |
incl->modifier = extra_data; |
| 528 |
incl->next_incl = NULL; |
| 529 |
|
| 530 |
switch (nextop) { |
| 531 |
case MERGE_AUGMENT_PREFIX: |
| 532 |
merge = MERGE_AUGMENT; |
| 533 |
break; |
| 534 |
case MERGE_REPLACE_PREFIX: |
| 535 |
merge = MERGE_REPLACE; |
| 536 |
break; |
| 537 |
default: |
| 538 |
merge = MERGE_OVERRIDE; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
if (first) |
| 543 |
first->stmt = stmt; |
| 544 |
else |
| 545 |
free(stmt); |
| 546 |
|
| 547 |
return first; |
| 548 |
|
| 549 |
err: |
| 550 |
log_err(ctx, XKB_ERROR_INVALID_INCLUDE_STATEMENT, |
| 551 |
"Illegal include statement \"%s\"; Ignored\n", stmt); |
| 552 |
FreeInclude(first); |
| 553 |
free(stmt); |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
|
| 557 |
XkbFile * |
| 558 |
XkbFileCreate(enum xkb_file_type type, char *name, ParseCommon *defs, |
| 559 |
enum xkb_map_flags flags) |
| 560 |
{ |
| 561 |
XkbFile *file; |
| 562 |
|
| 563 |
file = calloc(1, sizeof(*file)); |
| 564 |
if (!file) |
| 565 |
return NULL; |
| 566 |
|
| 567 |
XkbEscapeMapName(name); |
| 568 |
file->file_type = type; |
| 569 |
file->name = name; |
| 570 |
file->defs = defs; |
| 571 |
file->flags = flags; |
| 572 |
|
| 573 |
return file; |
| 574 |
} |
| 575 |
|
| 576 |
XkbFile * |
| 577 |
XkbFileFromComponents(struct xkb_context *ctx, |
| 578 |
const struct xkb_component_names *kkctgs) |
| 579 |
{ |
| 580 |
char *const components[] = { |
| 581 |
kkctgs->keycodes, kkctgs->types, |
| 582 |
kkctgs->compatibility, kkctgs->symbols, |
| 583 |
}; |
| 584 |
enum xkb_file_type type; |
| 585 |
IncludeStmt *include = NULL; |
| 586 |
XkbFile *file = NULL; |
| 587 |
ParseCommon *defs = NULL, *defsLast = NULL; |
| 588 |
|
| 589 |
for (type = FIRST_KEYMAP_FILE_TYPE; type <= LAST_KEYMAP_FILE_TYPE; type++) { |
| 590 |
include = IncludeCreate(ctx, components[type], MERGE_DEFAULT); |
| 591 |
if (!include) |
| 592 |
goto err; |
| 593 |
|
| 594 |
file = XkbFileCreate(type, NULL, (ParseCommon *) include, 0); |
| 595 |
if (!file) { |
| 596 |
FreeInclude(include); |
| 597 |
goto err; |
| 598 |
} |
| 599 |
|
| 600 |
if (!defs) |
| 601 |
defsLast = defs = &file->common; |
| 602 |
else |
| 603 |
defsLast = defsLast->next = &file->common; |
| 604 |
} |
| 605 |
|
| 606 |
file = XkbFileCreate(FILE_TYPE_KEYMAP, NULL, defs, 0); |
| 607 |
if (!file) |
| 608 |
goto err; |
| 609 |
|
| 610 |
return file; |
| 611 |
|
| 612 |
err: |
| 613 |
FreeXkbFile((XkbFile *) defs); |
| 614 |
return NULL; |
| 615 |
} |
| 616 |
|
| 617 |
static void |
| 618 |
FreeInclude(IncludeStmt *incl) |
| 619 |
{ |
| 620 |
IncludeStmt *next; |
| 621 |
|
| 622 |
while (incl) |
| 623 |
{ |
| 624 |
next = incl->next_incl; |
| 625 |
|
| 626 |
free(incl->file); |
| 627 |
free(incl->map); |
| 628 |
free(incl->modifier); |
| 629 |
free(incl->stmt); |
| 630 |
|
| 631 |
free(incl); |
| 632 |
incl = next; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
void |
| 637 |
FreeStmt(ParseCommon *stmt) |
| 638 |
{ |
| 639 |
ParseCommon *next; |
| 640 |
|
| 641 |
while (stmt) |
| 642 |
{ |
| 643 |
next = stmt->next; |
| 644 |
|
| 645 |
switch (stmt->type) { |
| 646 |
case STMT_INCLUDE: |
| 647 |
FreeInclude((IncludeStmt *) stmt); |
| 648 |
/* stmt is already free'd here. */ |
| 649 |
stmt = NULL; |
| 650 |
break; |
| 651 |
|
| 652 |
case STMT_EXPR_NEGATE: |
| 653 |
case STMT_EXPR_UNARY_PLUS: |
| 654 |
case STMT_EXPR_NOT: |
| 655 |
case STMT_EXPR_INVERT: |
| 656 |
FreeStmt((ParseCommon *) ((ExprUnary *) stmt)->child); |
| 657 |
break; |
| 658 |
|
| 659 |
case STMT_EXPR_DIVIDE: |
| 660 |
case STMT_EXPR_ADD: |
| 661 |
case STMT_EXPR_SUBTRACT: |
| 662 |
case STMT_EXPR_MULTIPLY: |
| 663 |
case STMT_EXPR_ASSIGN: |
| 664 |
FreeStmt((ParseCommon *) ((ExprBinary *) stmt)->left); |
| 665 |
FreeStmt((ParseCommon *) ((ExprBinary *) stmt)->right); |
| 666 |
break; |
| 667 |
|
| 668 |
case STMT_EXPR_ACTION_DECL: |
| 669 |
FreeStmt((ParseCommon *) ((ExprAction *) stmt)->args); |
| 670 |
break; |
| 671 |
|
| 672 |
case STMT_EXPR_ACTION_LIST: |
| 673 |
FreeStmt((ParseCommon *) ((ExprActionList *) stmt)->actions); |
| 674 |
break; |
| 675 |
|
| 676 |
case STMT_EXPR_ARRAY_REF: |
| 677 |
FreeStmt((ParseCommon *) ((ExprArrayRef *) stmt)->entry); |
| 678 |
break; |
| 679 |
|
| 680 |
case STMT_EXPR_KEYSYM_LIST: |
| 681 |
darray_free(((ExprKeysymList *) stmt)->syms); |
| 682 |
break; |
| 683 |
|
| 684 |
case STMT_VAR: |
| 685 |
FreeStmt((ParseCommon *) ((VarDef *) stmt)->name); |
| 686 |
FreeStmt((ParseCommon *) ((VarDef *) stmt)->value); |
| 687 |
break; |
| 688 |
case STMT_TYPE: |
| 689 |
FreeStmt((ParseCommon *) ((KeyTypeDef *) stmt)->body); |
| 690 |
break; |
| 691 |
case STMT_INTERP: |
| 692 |
FreeStmt((ParseCommon *) ((InterpDef *) stmt)->match); |
| 693 |
FreeStmt((ParseCommon *) ((InterpDef *) stmt)->def); |
| 694 |
break; |
| 695 |
case STMT_VMOD: |
| 696 |
FreeStmt((ParseCommon *) ((VModDef *) stmt)->value); |
| 697 |
break; |
| 698 |
case STMT_SYMBOLS: |
| 699 |
FreeStmt((ParseCommon *) ((SymbolsDef *) stmt)->symbols); |
| 700 |
break; |
| 701 |
case STMT_MODMAP: |
| 702 |
FreeStmt((ParseCommon *) ((ModMapDef *) stmt)->keys); |
| 703 |
break; |
| 704 |
case STMT_GROUP_COMPAT: |
| 705 |
FreeStmt((ParseCommon *) ((GroupCompatDef *) stmt)->def); |
| 706 |
break; |
| 707 |
case STMT_LED_MAP: |
| 708 |
FreeStmt((ParseCommon *) ((LedMapDef *) stmt)->body); |
| 709 |
break; |
| 710 |
case STMT_LED_NAME: |
| 711 |
FreeStmt((ParseCommon *) ((LedNameDef *) stmt)->name); |
| 712 |
break; |
| 713 |
default: |
| 714 |
break; |
| 715 |
} |
| 716 |
|
| 717 |
free(stmt); |
| 718 |
stmt = next; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
void |
| 723 |
FreeXkbFile(XkbFile *file) |
| 724 |
{ |
| 725 |
XkbFile *next; |
| 726 |
|
| 727 |
while (file) |
| 728 |
{ |
| 729 |
next = (XkbFile *) file->common.next; |
| 730 |
|
| 731 |
switch (file->file_type) { |
| 732 |
case FILE_TYPE_KEYMAP: |
| 733 |
FreeXkbFile((XkbFile *) file->defs); |
| 734 |
break; |
| 735 |
|
| 736 |
case FILE_TYPE_TYPES: |
| 737 |
case FILE_TYPE_COMPAT: |
| 738 |
case FILE_TYPE_SYMBOLS: |
| 739 |
case FILE_TYPE_KEYCODES: |
| 740 |
case FILE_TYPE_GEOMETRY: |
| 741 |
FreeStmt(file->defs); |
| 742 |
break; |
| 743 |
|
| 744 |
default: |
| 745 |
break; |
| 746 |
} |
| 747 |
|
| 748 |
free(file->name); |
| 749 |
free(file); |
| 750 |
file = next; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
static const char *xkb_file_type_strings[_FILE_TYPE_NUM_ENTRIES] = { |
| 755 |
[FILE_TYPE_KEYCODES] = "xkb_keycodes", |
| 756 |
[FILE_TYPE_TYPES] = "xkb_types", |
| 757 |
[FILE_TYPE_COMPAT] = "xkb_compatibility", |
| 758 |
[FILE_TYPE_SYMBOLS] = "xkb_symbols", |
| 759 |
[FILE_TYPE_GEOMETRY] = "xkb_geometry", |
| 760 |
[FILE_TYPE_KEYMAP] = "xkb_keymap", |
| 761 |
[FILE_TYPE_RULES] = "rules", |
| 762 |
}; |
| 763 |
|
| 764 |
const char * |
| 765 |
xkb_file_type_to_string(enum xkb_file_type type) |
| 766 |
{ |
| 767 |
if (type >= _FILE_TYPE_NUM_ENTRIES) |
| 768 |
return "unknown"; |
| 769 |
return xkb_file_type_strings[type]; |
| 770 |
} |
| 771 |
|
| 772 |
static const char *stmt_type_strings[_STMT_NUM_VALUES] = { |
| 773 |
[STMT_UNKNOWN] = "unknown statement", |
| 774 |
[STMT_INCLUDE] = "include statement", |
| 775 |
[STMT_KEYCODE] = "key name definition", |
| 776 |
[STMT_ALIAS] = "key alias definition", |
| 777 |
[STMT_EXPR_STRING_LITERAL] = "string literal expression", |
| 778 |
[STMT_EXPR_INTEGER_LITERAL] = "integer literal expression", |
| 779 |
[STMT_EXPR_FLOAT_LITERAL] = "float literal expression", |
| 780 |
[STMT_EXPR_BOOLEAN_LITERAL] = "boolean literal expression", |
| 781 |
[STMT_EXPR_KEYNAME_LITERAL] = "key name expression", |
| 782 |
[STMT_EXPR_KEYSYM_LITERAL] = "keysym expression", |
| 783 |
[STMT_EXPR_IDENT] = "identifier expression", |
| 784 |
[STMT_EXPR_ACTION_DECL] = "action declaration expression", |
| 785 |
[STMT_EXPR_FIELD_REF] = "field reference expression", |
| 786 |
[STMT_EXPR_ARRAY_REF] = "array reference expression", |
| 787 |
[STMT_EXPR_EMPTY_LIST] = "empty list expression", |
| 788 |
[STMT_EXPR_KEYSYM_LIST] = "keysym list expression", |
| 789 |
[STMT_EXPR_ACTION_LIST] = "action list expression", |
| 790 |
[STMT_EXPR_ADD] = "addition expression", |
| 791 |
[STMT_EXPR_SUBTRACT] = "substraction expression", |
| 792 |
[STMT_EXPR_MULTIPLY] = "multiplication expression", |
| 793 |
[STMT_EXPR_DIVIDE] = "division expression", |
| 794 |
[STMT_EXPR_ASSIGN] = "assignment expression", |
| 795 |
[STMT_EXPR_NOT] = "logical negation expression", |
| 796 |
[STMT_EXPR_NEGATE] = "arithmetic negation expression", |
| 797 |
[STMT_EXPR_INVERT] = "bitwise inversion expression", |
| 798 |
[STMT_EXPR_UNARY_PLUS] = "unary plus expression", |
| 799 |
[STMT_VAR] = "variable definition", |
| 800 |
[STMT_TYPE] = "key type definition", |
| 801 |
[STMT_INTERP] = "symbol interpretation definition", |
| 802 |
[STMT_VMOD] = "virtual modifiers definition", |
| 803 |
[STMT_SYMBOLS] = "key symbols definition", |
| 804 |
[STMT_MODMAP] = "modifier map declaration", |
| 805 |
[STMT_GROUP_COMPAT] = "group declaration", |
| 806 |
[STMT_LED_MAP] = "indicator map declaration", |
| 807 |
[STMT_LED_NAME] = "indicator name declaration", |
| 808 |
}; |
| 809 |
|
| 810 |
const char * |
| 811 |
stmt_type_to_string(enum stmt_type type) |
| 812 |
{ |
| 813 |
if (type >= _STMT_NUM_VALUES) |
| 814 |
return NULL; |
| 815 |
return stmt_type_strings[type]; |
| 816 |
} |
| 817 |
|
| 818 |
char |
| 819 |
stmt_type_to_operator_char(enum stmt_type type) |
| 820 |
{ |
| 821 |
switch (type) { |
| 822 |
case STMT_EXPR_ADD: |
| 823 |
return '+'; |
| 824 |
case STMT_EXPR_SUBTRACT: |
| 825 |
return '-'; |
| 826 |
case STMT_EXPR_MULTIPLY: |
| 827 |
return '*'; |
| 828 |
case STMT_EXPR_DIVIDE: |
| 829 |
return '/'; |
| 830 |
case STMT_EXPR_NOT: |
| 831 |
return '!'; |
| 832 |
case STMT_EXPR_NEGATE: |
| 833 |
return '-'; |
| 834 |
case STMT_EXPR_INVERT: |
| 835 |
return '~'; |
| 836 |
case STMT_EXPR_UNARY_PLUS: |
| 837 |
return '+'; |
| 838 |
default: |
| 839 |
return '\0'; |
| 840 |
} |
| 841 |
} |