1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
54 #include "xkbcomp-priv.h"
55 #include "ast-build.h"
56 #include "parser-priv.h"
60 AppendStmt(ParseCommon *to, ParseCommon *append)
67 for (iter = to; iter->next; iter = iter->next);
74 ExprCreate(enum expr_op_type op, enum expr_value_type type, size_t size)
76 ExprDef *expr = malloc(size);
80 expr->common.type = STMT_EXPR;
81 expr->common.next = NULL;
83 expr->expr.value_type = type;
89 ExprCreateString(xkb_atom_t str)
91 ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_STRING,
96 expr->string.str = str;
102 ExprCreateInteger(int ival)
104 ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_INT,
105 sizeof(ExprInteger));
109 expr->integer.ival = ival;
115 ExprCreateBoolean(bool set)
117 ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_BOOLEAN,
118 sizeof(ExprBoolean));
122 expr->boolean.set = set;
128 ExprCreateKeyName(xkb_atom_t key_name)
130 ExprDef *expr = ExprCreate(EXPR_VALUE, EXPR_TYPE_KEYNAME,
131 sizeof(ExprKeyName));
135 expr->key_name.key_name = key_name;
141 ExprCreateIdent(xkb_atom_t ident)
143 ExprDef *expr = ExprCreate(EXPR_IDENT, EXPR_TYPE_UNKNOWN,
148 expr->ident.ident = ident;
154 ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
157 ExprDef *expr = ExprCreate(op, type,
162 expr->unary.child = child;
168 ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right)
170 ExprDef *expr = ExprCreate(op, EXPR_TYPE_UNKNOWN,
175 if (op == EXPR_ASSIGN || left->expr.value_type == EXPR_TYPE_UNKNOWN)
176 expr->expr.value_type = right->expr.value_type;
177 else if (left->expr.value_type == right->expr.value_type ||
178 right->expr.value_type == EXPR_TYPE_UNKNOWN)
179 expr->expr.value_type = left->expr.value_type;
180 expr->binary.left = left;
181 expr->binary.right = right;
187 ExprCreateFieldRef(xkb_atom_t element, xkb_atom_t field)
189 ExprDef *expr = ExprCreate(EXPR_FIELD_REF, EXPR_TYPE_UNKNOWN,
190 sizeof(ExprFieldRef));
194 expr->field_ref.element = element;
195 expr->field_ref.field = field;
201 ExprCreateArrayRef(xkb_atom_t element, xkb_atom_t field, ExprDef *entry)
203 ExprDef *expr = ExprCreate(EXPR_ARRAY_REF, EXPR_TYPE_UNKNOWN,
204 sizeof(ExprArrayRef));
208 expr->array_ref.element = element;
209 expr->array_ref.field = field;
210 expr->array_ref.entry = entry;
216 KeycodeCreate(xkb_atom_t name, int64_t value)
218 KeycodeDef *def = malloc(sizeof(*def));
222 def->common.type = STMT_KEYCODE;
223 def->common.next = NULL;
231 KeyAliasCreate(xkb_atom_t alias, xkb_atom_t real)
233 KeyAliasDef *def = malloc(sizeof(*def));
237 def->common.type = STMT_ALIAS;
238 def->common.next = NULL;
246 VModCreate(xkb_atom_t name, ExprDef *value)
248 VModDef *def = malloc(sizeof(*def));
252 def->common.type = STMT_VMOD;
253 def->common.next = NULL;
261 VarCreate(ExprDef *name, ExprDef *value)
263 VarDef *def = malloc(sizeof(*def));
267 def->common.type = STMT_VAR;
268 def->common.next = NULL;
276 BoolVarCreate(xkb_atom_t ident, bool set)
278 return VarCreate((ExprDef *) ExprCreateIdent(ident),
279 (ExprDef *) ExprCreateBoolean(set));
283 InterpCreate(xkb_keysym_t sym, ExprDef *match)
285 InterpDef *def = malloc(sizeof(*def));
289 def->common.type = STMT_INTERP;
290 def->common.next = NULL;
298 KeyTypeCreate(xkb_atom_t name, VarDef *body)
300 KeyTypeDef *def = malloc(sizeof(*def));
304 def->common.type = STMT_TYPE;
305 def->common.next = NULL;
306 def->merge = MERGE_DEFAULT;
314 SymbolsCreate(xkb_atom_t keyName, VarDef *symbols)
316 SymbolsDef *def = malloc(sizeof(*def));
320 def->common.type = STMT_SYMBOLS;
321 def->common.next = NULL;
322 def->merge = MERGE_DEFAULT;
323 def->keyName = keyName;
324 def->symbols = symbols;
330 GroupCompatCreate(int group, ExprDef *val)
332 GroupCompatDef *def = malloc(sizeof(*def));
336 def->common.type = STMT_GROUP_COMPAT;
337 def->common.next = NULL;
338 def->merge = MERGE_DEFAULT;
346 ModMapCreate(uint32_t modifier, ExprDef *keys)
348 ModMapDef *def = malloc(sizeof(*def));
352 def->common.type = STMT_MODMAP;
353 def->common.next = NULL;
354 def->merge = MERGE_DEFAULT;
355 def->modifier = modifier;
362 LedMapCreate(xkb_atom_t name, VarDef *body)
364 LedMapDef *def = malloc(sizeof(*def));
368 def->common.type = STMT_LED_MAP;
369 def->common.next = NULL;
370 def->merge = MERGE_DEFAULT;
378 LedNameCreate(int ndx, ExprDef *name, bool virtual)
380 LedNameDef *def = malloc(sizeof(*def));
384 def->common.type = STMT_LED_NAME;
385 def->common.next = NULL;
386 def->merge = MERGE_DEFAULT;
389 def->virtual = virtual;
395 ActionCreate(xkb_atom_t name, ExprDef *args)
397 ExprDef *expr = ExprCreate(EXPR_ACTION_DECL, EXPR_TYPE_UNKNOWN,
402 expr->action.name = name;
403 expr->action.args = args;
409 CreateKeysymList(xkb_keysym_t sym)
411 ExprDef *expr = ExprCreate(EXPR_KEYSYM_LIST, EXPR_TYPE_SYMBOLS,
412 sizeof(ExprKeysymList));
416 darray_init(expr->keysym_list.syms);
417 darray_init(expr->keysym_list.symsMapIndex);
418 darray_init(expr->keysym_list.symsNumEntries);
420 darray_append(expr->keysym_list.syms, sym);
421 darray_append(expr->keysym_list.symsMapIndex, 0);
422 darray_append(expr->keysym_list.symsNumEntries, 1);
428 CreateMultiKeysymList(ExprDef *expr)
430 size_t nLevels = darray_size(expr->keysym_list.symsMapIndex);
432 darray_resize(expr->keysym_list.symsMapIndex, 1);
433 darray_resize(expr->keysym_list.symsNumEntries, 1);
434 darray_item(expr->keysym_list.symsMapIndex, 0) = 0;
435 darray_item(expr->keysym_list.symsNumEntries, 0) = nLevels;
441 AppendKeysymList(ExprDef *expr, xkb_keysym_t sym)
443 size_t nSyms = darray_size(expr->keysym_list.syms);
445 darray_append(expr->keysym_list.symsMapIndex, nSyms);
446 darray_append(expr->keysym_list.symsNumEntries, 1);
447 darray_append(expr->keysym_list.syms, sym);
453 AppendMultiKeysymList(ExprDef *expr, ExprDef *append)
455 size_t nSyms = darray_size(expr->keysym_list.syms);
456 size_t numEntries = darray_size(append->keysym_list.syms);
458 darray_append(expr->keysym_list.symsMapIndex, nSyms);
459 darray_append(expr->keysym_list.symsNumEntries, numEntries);
460 darray_append_items(expr->keysym_list.syms,
461 darray_mem(append->keysym_list.syms, 0), numEntries);
463 darray_resize(append->keysym_list.syms, 0);
464 FreeStmt(&append->common);
470 FreeInclude(IncludeStmt *incl);
473 IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge)
475 IncludeStmt *incl, *first;
476 char *file, *map, *stmt, *tmp, *extra_data;
482 stmt = strdup_safe(str);
485 if (!ParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
489 * Given an RMLVO (here layout) like 'us,,fr', the rules parser
490 * will give out something like 'pc+us+:2+fr:3+inet(evdev)'.
491 * We should just skip the ':2' in this case and leave it to the
492 * appropriate section to deal with the empty group.
502 first = incl = malloc(sizeof(*first));
504 incl->next_incl = malloc(sizeof(*first));
505 incl = incl->next_incl;
510 "Allocation failure in IncludeCreate; "
511 "Using only part of the include\n");
515 incl->common.type = STMT_INCLUDE;
516 incl->common.next = NULL;
521 incl->modifier = extra_data;
522 incl->next_incl = NULL;
525 merge = MERGE_AUGMENT;
527 merge = MERGE_OVERRIDE;
538 log_err(ctx, "Illegal include statement \"%s\"; Ignored\n", stmt);
545 XkbEscapeMapName(char *name)
548 * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
551 static const unsigned char legal[] = {
552 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
553 0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
554 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
555 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
562 if (!(legal[*name / 8] & (1 << (*name % 8))))
569 XkbFileCreate(struct xkb_context *ctx, enum xkb_file_type type, char *name,
570 ParseCommon *defs, enum xkb_map_flags flags)
574 file = calloc(1, sizeof(*file));
578 XkbEscapeMapName(name);
579 file->file_type = type;
580 file->topName = strdup_safe(name);
589 XkbFileFromComponents(struct xkb_context *ctx,
590 const struct xkb_component_names *kkctgs)
592 char *const components[] = {
593 kkctgs->keycodes, kkctgs->types,
594 kkctgs->compat, kkctgs->symbols,
596 enum xkb_file_type type;
597 IncludeStmt *include = NULL;
598 XkbFile *file = NULL;
599 ParseCommon *defs = NULL;
601 for (type = FIRST_KEYMAP_FILE_TYPE; type <= LAST_KEYMAP_FILE_TYPE; type++) {
602 include = IncludeCreate(ctx, components[type], MERGE_DEFAULT);
606 file = XkbFileCreate(ctx, type, NULL, &include->common, 0);
608 FreeInclude(include);
612 defs = AppendStmt(defs, &file->common);
615 file = XkbFileCreate(ctx, FILE_TYPE_KEYMAP, NULL, defs, 0);
622 FreeXkbFile((XkbFile *) defs);
627 FreeExpr(ExprDef *expr)
632 switch (expr->expr.op) {
633 case EXPR_ACTION_LIST:
635 case EXPR_UNARY_PLUS:
638 FreeStmt(&expr->unary.child->common);
646 FreeStmt(&expr->binary.left->common);
647 FreeStmt(&expr->binary.right->common);
650 case EXPR_ACTION_DECL:
651 FreeStmt(&expr->action.args->common);
655 FreeStmt(&expr->array_ref.entry->common);
658 case EXPR_KEYSYM_LIST:
659 darray_free(expr->keysym_list.syms);
660 darray_free(expr->keysym_list.symsMapIndex);
661 darray_free(expr->keysym_list.symsNumEntries);
670 FreeInclude(IncludeStmt *incl)
676 next = incl->next_incl;
680 free(incl->modifier);
689 FreeStmt(ParseCommon *stmt)
699 switch (stmt->type) {
701 FreeInclude((IncludeStmt *) stmt);
702 /* stmt is already free'd here. */
709 FreeStmt(&u.var->name->common);
710 FreeStmt(&u.var->value->common);
713 FreeStmt(&u.keyType->body->common);
716 FreeStmt(&u.interp->match->common);
717 FreeStmt(&u.interp->def->common);
720 FreeStmt(&u.vmod->value->common);
723 FreeStmt(&u.syms->symbols->common);
726 FreeStmt(&u.modMask->keys->common);
728 case STMT_GROUP_COMPAT:
729 FreeStmt(&u.groupCompat->def->common);
732 FreeStmt(&u.ledMap->body->common);
735 FreeStmt(&u.ledName->name->common);
747 FreeXkbFile(XkbFile *file)
753 next = (XkbFile *) file->common.next;
755 switch (file->file_type) {
756 case FILE_TYPE_KEYMAP:
757 FreeXkbFile((XkbFile *) file->defs);
760 case FILE_TYPE_TYPES:
761 case FILE_TYPE_COMPAT:
762 case FILE_TYPE_SYMBOLS:
763 case FILE_TYPE_KEYCODES:
764 case FILE_TYPE_GEOMETRY:
765 FreeStmt(file->defs);
779 static const char *xkb_file_type_strings[_FILE_TYPE_NUM_ENTRIES] = {
780 [FILE_TYPE_KEYCODES] = "xkb_keycodes",
781 [FILE_TYPE_TYPES] = "xkb_types",
782 [FILE_TYPE_COMPAT] = "xkb_compatibility",
783 [FILE_TYPE_SYMBOLS] = "xkb_symbols",
784 [FILE_TYPE_GEOMETRY] = "xkb_geometry",
785 [FILE_TYPE_KEYMAP] = "xkb_keymap",
786 [FILE_TYPE_RULES] = "rules",
790 xkb_file_type_to_string(enum xkb_file_type type)
792 if (type > _FILE_TYPE_NUM_ENTRIES)
794 return xkb_file_type_strings[type];
797 static const char *stmt_type_strings[_STMT_NUM_VALUES] = {
798 [STMT_UNKNOWN] = "unknown statement",
799 [STMT_INCLUDE] = "include statement",
800 [STMT_KEYCODE] = "key name definition",
801 [STMT_ALIAS] = "key alias definition",
802 [STMT_EXPR] = "expression",
803 [STMT_VAR] = "variable definition",
804 [STMT_TYPE] = "key type definition",
805 [STMT_INTERP] = "symbol interpretation definition",
806 [STMT_VMOD] = "virtual modifiers definition",
807 [STMT_SYMBOLS] = "key symbols definition",
808 [STMT_MODMAP] = "modifier map declaration",
809 [STMT_GROUP_COMPAT] = "group declaration",
810 [STMT_LED_MAP] = "indicator map declaration",
811 [STMT_LED_NAME] = "indicator name declaration",
815 stmt_type_to_string(enum stmt_type type)
817 if (type >= _STMT_NUM_VALUES)
819 return stmt_type_strings[type];
822 static const char *expr_op_type_strings[_EXPR_NUM_VALUES] = {
823 [EXPR_VALUE] = "literal",
824 [EXPR_IDENT] = "identifier",
825 [EXPR_ACTION_DECL] = "action declaration",
826 [EXPR_FIELD_REF] = "field reference",
827 [EXPR_ARRAY_REF] = "array reference",
828 [EXPR_KEYSYM_LIST] = "list of keysyms",
829 [EXPR_ACTION_LIST] = "list of actions",
830 [EXPR_ADD] = "addition",
831 [EXPR_SUBTRACT] = "subtraction",
832 [EXPR_MULTIPLY] = "multiplication",
833 [EXPR_DIVIDE] = "division",
834 [EXPR_ASSIGN] = "assignment",
835 [EXPR_NOT] = "logical negation",
836 [EXPR_NEGATE] = "arithmetic negation",
837 [EXPR_INVERT] = "bitwise inversion",
838 [EXPR_UNARY_PLUS] = "unary plus",
842 expr_op_type_to_string(enum expr_op_type type)
844 if (type >= _EXPR_NUM_VALUES)
846 return expr_op_type_strings[type];
849 static const char *expr_value_type_strings[_EXPR_TYPE_NUM_VALUES] = {
850 [EXPR_TYPE_UNKNOWN] = "unknown",
851 [EXPR_TYPE_BOOLEAN] = "boolean",
852 [EXPR_TYPE_INT] = "int",
853 [EXPR_TYPE_STRING] = "string",
854 [EXPR_TYPE_ACTION] = "action",
855 [EXPR_TYPE_KEYNAME] = "keyname",
856 [EXPR_TYPE_SYMBOLS] = "symbols",
860 expr_value_type_to_string(enum expr_value_type type)
862 if (type >= _EXPR_TYPE_NUM_VALUES)
864 return expr_value_type_strings[type];