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"
59 ATTR_MALLOC static void *
60 malloc_or_die(size_t size)
62 void *p = malloc(size);
64 fprintf(stderr, "Out of memory\n");
71 AppendStmt(ParseCommon * to, ParseCommon * append)
73 ParseCommon *start = to;
77 while ((to != NULL) && (to->next != NULL))
89 ExprCreate(enum expr_op_type op, enum expr_value_type type)
93 expr = malloc_or_die(sizeof(*expr));
95 expr->common.type = STMT_EXPR;
96 expr->common.next = NULL;
98 expr->value_type = type;
103 ExprCreateUnary(enum expr_op_type op, enum expr_value_type type,
107 expr = malloc_or_die(sizeof(*expr));
109 expr->common.type = STMT_EXPR;
110 expr->common.next = NULL;
112 expr->value_type = type;
113 expr->value.child = child;
118 ExprCreateBinary(enum expr_op_type op, ExprDef *left, ExprDef *right)
122 expr = malloc_or_die(sizeof(*expr));
124 expr->common.type = STMT_EXPR;
125 expr->common.next = NULL;
127 if (op == EXPR_ASSIGN || left->value_type == EXPR_TYPE_UNKNOWN)
128 expr->value_type = right->value_type;
129 else if (left->value_type == right->value_type ||
130 right->value_type == EXPR_TYPE_UNKNOWN)
131 expr->value_type = left->value_type;
133 expr->value_type = EXPR_TYPE_UNKNOWN;
134 expr->value.binary.left = left;
135 expr->value.binary.right = right;
140 KeycodeCreate(char keyName[XKB_KEY_NAME_LENGTH], unsigned long value)
144 def = malloc_or_die(sizeof(*def));
146 def->common.type = STMT_KEYCODE;
147 def->common.next = NULL;
148 strncpy(def->name, keyName, XKB_KEY_NAME_LENGTH);
154 KeyAliasCreate(char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
158 def = malloc_or_die(sizeof(*def));
160 def->common.type = STMT_ALIAS;
161 def->common.next = NULL;
162 strncpy(def->alias, alias, XKB_KEY_NAME_LENGTH);
163 strncpy(def->real, real, XKB_KEY_NAME_LENGTH);
168 VModCreate(xkb_atom_t name, ExprDef * value)
172 def = malloc_or_die(sizeof(*def));
174 def->common.type = STMT_VMOD;
175 def->common.next = NULL;
182 VarCreate(ExprDef * name, ExprDef * value)
185 def = malloc_or_die(sizeof(*def));
187 def->common.type = STMT_VAR;
188 def->common.next = NULL;
195 BoolVarCreate(xkb_atom_t nameToken, unsigned set)
197 ExprDef *name, *value;
199 name = ExprCreate(EXPR_IDENT, EXPR_TYPE_UNKNOWN);
200 name->value.str = nameToken;
201 value = ExprCreate(EXPR_VALUE, EXPR_TYPE_BOOLEAN);
202 value->value.uval = set;
203 return VarCreate(name, value);
207 InterpCreate(char *sym, ExprDef * match)
211 def = malloc_or_die(sizeof(*def));
213 def->common.type = STMT_INTERP;
214 def->common.next = NULL;
221 KeyTypeCreate(xkb_atom_t name, VarDef * body)
225 def = malloc_or_die(sizeof(*def));
227 def->common.type = STMT_TYPE;
228 def->common.next = NULL;
229 def->merge = MERGE_DEFAULT;
236 SymbolsCreate(char keyName[XKB_KEY_NAME_LENGTH], ExprDef *symbols)
240 def = malloc_or_die(sizeof(*def));
242 def->common.type = STMT_SYMBOLS;
243 def->common.next = NULL;
244 def->merge = MERGE_DEFAULT;
245 strncpy(def->keyName, keyName, XKB_KEY_NAME_LENGTH);
246 def->symbols = symbols;
251 GroupCompatCreate(int group, ExprDef * val)
255 def = malloc_or_die(sizeof(*def));
257 def->common.type = STMT_GROUP_COMPAT;
258 def->common.next = NULL;
259 def->merge = MERGE_DEFAULT;
266 ModMapCreate(uint32_t modifier, ExprDef * keys)
270 def = malloc_or_die(sizeof(*def));
272 def->common.type = STMT_MODMAP;
273 def->common.next = NULL;
274 def->merge = MERGE_DEFAULT;
275 def->modifier = modifier;
281 IndicatorMapCreate(xkb_atom_t name, VarDef * body)
283 IndicatorMapDef *def;
285 def = malloc_or_die(sizeof(*def));
287 def->common.type = STMT_INDICATOR_MAP;
288 def->common.next = NULL;
289 def->merge = MERGE_DEFAULT;
296 IndicatorNameCreate(int ndx, ExprDef * name, bool virtual)
298 IndicatorNameDef *def;
300 def = malloc_or_die(sizeof(*def));
302 def->common.type = STMT_INDICATOR_NAME;
303 def->common.next = NULL;
304 def->merge = MERGE_DEFAULT;
307 def->virtual = virtual;
312 ActionCreate(xkb_atom_t name, ExprDef * args)
316 act = malloc_or_die(sizeof(*act));
318 act->common.type = STMT_EXPR;
319 act->common.next = NULL;
320 act->op = EXPR_ACTION_DECL;
321 act->value.action.name = name;
322 act->value.action.args = args;
327 CreateKeysymList(char *sym)
331 def = ExprCreate(EXPR_KEYSYM_LIST, EXPR_TYPE_SYMBOLS);
333 darray_init(def->value.list.syms);
334 darray_init(def->value.list.symsMapIndex);
335 darray_init(def->value.list.symsNumEntries);
337 darray_append(def->value.list.syms, sym);
338 darray_append(def->value.list.symsMapIndex, 0);
339 darray_append(def->value.list.symsNumEntries, 1);
345 CreateMultiKeysymList(ExprDef *list)
347 size_t nLevels = darray_size(list->value.list.symsMapIndex);
349 darray_resize(list->value.list.symsMapIndex, 1);
350 darray_resize(list->value.list.symsNumEntries, 1);
351 darray_item(list->value.list.symsMapIndex, 0) = 0;
352 darray_item(list->value.list.symsNumEntries, 0) = nLevels;
358 AppendKeysymList(ExprDef * list, char *sym)
360 size_t nSyms = darray_size(list->value.list.syms);
362 darray_append(list->value.list.symsMapIndex, nSyms);
363 darray_append(list->value.list.symsNumEntries, 1);
364 darray_append(list->value.list.syms, sym);
370 AppendMultiKeysymList(ExprDef * list, ExprDef * append)
372 size_t nSyms = darray_size(list->value.list.syms);
373 size_t numEntries = darray_size(append->value.list.syms);
375 darray_append(list->value.list.symsMapIndex, nSyms);
376 darray_append(list->value.list.symsNumEntries, numEntries);
377 darray_append_items(list->value.list.syms,
378 darray_mem(append->value.list.syms, 0),
381 darray_resize(append->value.list.syms, 0);
382 FreeStmt(&append->common);
388 FreeInclude(IncludeStmt *incl);
391 IncludeCreate(struct xkb_context *ctx, char *str, enum merge_mode merge)
393 IncludeStmt *incl, *first;
394 char *file, *map, *stmt, *tmp, *extra_data;
400 stmt = strdup_safe(str);
403 if (!ParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
407 first = incl = malloc(sizeof(*first));
409 incl->next_incl = malloc(sizeof(*first));
410 incl = incl->next_incl;
415 "Allocation failure in IncludeCreate; "
416 "Using only part of the include\n");
420 incl->common.type = STMT_INCLUDE;
421 incl->common.next = NULL;
426 incl->modifier = extra_data;
427 incl->next_incl = NULL;
430 merge = MERGE_AUGMENT;
432 merge = MERGE_OVERRIDE;
443 log_err(ctx, "Illegal include statement \"%s\"; Ignored\n", stmt);
450 * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
453 static const unsigned char componentSpecLegal[] = {
454 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
455 0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
456 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
457 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
461 EnsureSafeMapName(char *name)
466 while (*name != '\0') {
467 if ((componentSpecLegal[(*name) / 8] & (1 << ((*name) % 8))) == 0)
474 XkbFileCreate(struct xkb_context *ctx, enum xkb_file_type type, char *name,
475 ParseCommon *defs, enum xkb_map_flags flags)
479 file = calloc(1, sizeof(*file));
483 EnsureSafeMapName(name);
484 file->file_type = type;
485 file->topName = strdup_safe(name);
488 file->id = xkb_context_take_file_id(ctx);
494 XkbFileFromComponents(struct xkb_context *ctx,
495 struct xkb_component_names *kkctgs)
498 XkbFile *keycodes, *types, *compat, *symbols;
500 inc = IncludeCreate(ctx, kkctgs->keycodes, MERGE_DEFAULT);
501 keycodes = XkbFileCreate(ctx, FILE_TYPE_KEYCODES, NULL,
502 (ParseCommon *) inc, 0);
504 inc = IncludeCreate(ctx, kkctgs->types, MERGE_DEFAULT);
505 types = XkbFileCreate(ctx, FILE_TYPE_TYPES, NULL,
506 (ParseCommon *) inc, 0);
507 AppendStmt(&keycodes->common, &types->common);
509 inc = IncludeCreate(ctx, kkctgs->compat, MERGE_DEFAULT);
510 compat = XkbFileCreate(ctx, FILE_TYPE_COMPAT, NULL,
511 (ParseCommon *) inc, 0);
512 AppendStmt(&keycodes->common, &compat->common);
514 inc = IncludeCreate(ctx, kkctgs->symbols, MERGE_DEFAULT);
515 symbols = XkbFileCreate(ctx, FILE_TYPE_SYMBOLS, NULL,
516 (ParseCommon *) inc, 0);
517 AppendStmt(&keycodes->common, &symbols->common);
519 return XkbFileCreate(ctx, FILE_TYPE_KEYMAP, NULL,
520 &keycodes->common, 0);
524 FreeExpr(ExprDef *expr)
532 case EXPR_ACTION_LIST:
534 case EXPR_UNARY_PLUS:
537 FreeStmt(&expr->value.child->common);
545 FreeStmt(&expr->value.binary.left->common);
546 FreeStmt(&expr->value.binary.right->common);
549 case EXPR_ACTION_DECL:
550 FreeStmt(&expr->value.action.args->common);
554 FreeStmt(&expr->value.array.entry->common);
557 case EXPR_KEYSYM_LIST:
558 darray_foreach(sym, expr->value.list.syms)
560 darray_free(expr->value.list.syms);
561 darray_free(expr->value.list.symsMapIndex);
562 darray_free(expr->value.list.symsNumEntries);
571 FreeInclude(IncludeStmt *incl)
577 next = incl->next_incl;
581 free(incl->modifier);
590 FreeStmt(ParseCommon *stmt)
600 switch (stmt->type) {
602 FreeInclude((IncludeStmt *) stmt);
603 /* stmt is already free'd here. */
610 FreeStmt(&u.var->name->common);
611 FreeStmt(&u.var->value->common);
614 FreeStmt(&u.keyType->body->common);
618 FreeStmt(&u.interp->match->common);
619 FreeStmt(&u.interp->def->common);
622 FreeStmt(&u.vmod->value->common);
625 FreeStmt(&u.syms->symbols->common);
628 FreeStmt(&u.modMask->keys->common);
630 case STMT_GROUP_COMPAT:
631 FreeStmt(&u.groupCompat->def->common);
633 case STMT_INDICATOR_MAP:
634 FreeStmt(&u.ledMap->body->common);
636 case STMT_INDICATOR_NAME:
637 FreeStmt(&u.ledName->name->common);
649 FreeXkbFile(XkbFile *file)
655 next = (XkbFile *) file->common.next;
657 switch (file->file_type) {
658 case FILE_TYPE_KEYMAP:
659 FreeXkbFile((XkbFile *) file->defs);
662 case FILE_TYPE_TYPES:
663 case FILE_TYPE_COMPAT:
664 case FILE_TYPE_SYMBOLS:
665 case FILE_TYPE_KEYCODES:
666 case FILE_TYPE_GEOMETRY:
667 FreeStmt(file->defs);
681 static const char *xkb_file_type_strings[_FILE_TYPE_NUM_ENTRIES] = {
682 [FILE_TYPE_KEYCODES] = "xkb_keycodes",
683 [FILE_TYPE_TYPES] = "xkb_types",
684 [FILE_TYPE_COMPAT] = "xkb_compatibility",
685 [FILE_TYPE_SYMBOLS] = "xkb_symbols",
686 [FILE_TYPE_KEYMAP] = "xkb_keymap",
687 [FILE_TYPE_RULES] = "rules",
691 xkb_file_type_to_string(enum xkb_file_type type)
693 if (type > _FILE_TYPE_NUM_ENTRIES)
695 return xkb_file_type_strings[type];
698 static const char *stmt_type_strings[_STMT_NUM_VALUES] = {
699 [STMT_UNKNOWN] = "unknown statement",
700 [STMT_INCLUDE] = "include statement",
701 [STMT_KEYCODE] = "key name definition",
702 [STMT_ALIAS] = "key alias definition",
703 [STMT_EXPR] = "expression",
704 [STMT_VAR] = "variable definition",
705 [STMT_TYPE] = "key type definition",
706 [STMT_INTERP] = "symbol interpretation definition",
707 [STMT_VMOD] = "virtual modifiers definition",
708 [STMT_SYMBOLS] = "key symbols definition",
709 [STMT_MODMAP] = "modifier map declaration",
710 [STMT_GROUP_COMPAT] = "group declaration",
711 [STMT_INDICATOR_MAP] = "indicator map declaration",
712 [STMT_INDICATOR_NAME] = "indicator name declaration",
716 stmt_type_to_string(enum stmt_type type)
718 if (type >= _STMT_NUM_VALUES)
720 return stmt_type_strings[type];
723 static const char *expr_op_type_strings[_EXPR_NUM_VALUES] = {
724 [EXPR_VALUE] = "literal",
725 [EXPR_IDENT] = "identifier",
726 [EXPR_ACTION_DECL] = "action declaration",
727 [EXPR_FIELD_REF] = "field reference",
728 [EXPR_ARRAY_REF] = "array reference",
729 [EXPR_KEYSYM_LIST] = "list of keysyms",
730 [EXPR_ACTION_LIST] = "list of actions",
731 [EXPR_ADD] = "addition",
732 [EXPR_SUBTRACT] = "subtraction",
733 [EXPR_MULTIPLY] = "multiplication",
734 [EXPR_DIVIDE] = "division",
735 [EXPR_ASSIGN] = "assignment",
736 [EXPR_NOT] = "logical negation",
737 [EXPR_NEGATE] = "arithmetic negation",
738 [EXPR_INVERT] = "bitwise inversion",
739 [EXPR_UNARY_PLUS] = "unary plus",
743 expr_op_type_to_string(enum expr_op_type type)
745 if (type >= _EXPR_NUM_VALUES)
747 return expr_op_type_strings[type];
750 static const char *expr_value_type_strings[_EXPR_TYPE_NUM_VALUES] = {
751 [EXPR_TYPE_UNKNOWN] = "unknown",
752 [EXPR_TYPE_BOOLEAN] = "boolean",
753 [EXPR_TYPE_INT] = "int",
754 [EXPR_TYPE_STRING] = "string",
755 [EXPR_TYPE_ACTION] = "action",
756 [EXPR_TYPE_KEYNAME] = "keyname",
757 [EXPR_TYPE_SYMBOLS] = "symbols",
761 expr_value_type_to_string(enum expr_value_type type)
763 if (type >= _EXPR_TYPE_NUM_VALUES)
765 return expr_value_type_strings[type];