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 ********************************************************/
27 #include "parseutils.h"
31 AppendStmt(ParseCommon * to, ParseCommon * append)
33 ParseCommon *start = to;
37 while ((to != NULL) && (to->next != NULL))
50 ExprCreate(unsigned op, unsigned type)
53 expr = uTypedAlloc(ExprDef);
56 expr->common.stmtType = StmtExpr;
57 expr->common.next = NULL;
63 FATAL("Couldn't allocate expression in parser\n");
70 ExprCreateUnary(unsigned op, unsigned type, ExprDef * child)
73 expr = uTypedAlloc(ExprDef);
76 expr->common.stmtType = StmtExpr;
77 expr->common.next = NULL;
80 expr->value.child = child;
84 FATAL("Couldn't allocate expression in parser\n");
91 ExprCreateBinary(unsigned op, ExprDef * left, ExprDef * right)
94 expr = uTypedAlloc(ExprDef);
97 expr->common.stmtType = StmtExpr;
98 expr->common.next = NULL;
100 if ((op == OpAssign) || (left->type == TypeUnknown))
101 expr->type = right->type;
102 else if ((left->type == right->type) || (right->type == TypeUnknown))
103 expr->type = left->type;
105 expr->type = TypeUnknown;
106 expr->value.binary.left = left;
107 expr->value.binary.right = right;
111 FATAL("Couldn't allocate expression in parser\n");
118 KeycodeCreate(const char *name, unsigned long value)
122 def = uTypedAlloc(KeycodeDef);
125 def->common.stmtType = StmtKeycodeDef;
126 def->common.next = NULL;
127 strncpy(def->name, name, XkbKeyNameLength);
128 def->name[XkbKeyNameLength] = '\0';
133 FATAL("Couldn't allocate key name definition in parser\n");
140 KeyAliasCreate(const char *alias, const char *real)
144 def = uTypedAlloc(KeyAliasDef);
147 def->common.stmtType = StmtKeyAliasDef;
148 def->common.next = NULL;
149 strncpy(def->alias, alias, XkbKeyNameLength);
150 def->alias[XkbKeyNameLength] = '\0';
151 strncpy(def->real, real, XkbKeyNameLength);
152 def->real[XkbKeyNameLength] = '\0';
156 FATAL("Couldn't allocate key alias definition in parser\n");
163 VModCreate(xkb_atom_t name, ExprDef * value)
166 def = uTypedAlloc(VModDef);
169 def->common.stmtType = StmtVModDef;
170 def->common.next = NULL;
176 FATAL("Couldn't allocate variable definition in parser\n");
183 VarCreate(ExprDef * name, ExprDef * value)
186 def = uTypedAlloc(VarDef);
189 def->common.stmtType = StmtVarDef;
190 def->common.next = NULL;
196 FATAL("Couldn't allocate variable definition in parser\n");
203 BoolVarCreate(xkb_atom_t nameToken, unsigned set)
205 ExprDef *name, *value;
207 name = ExprCreate(ExprIdent, TypeUnknown);
208 name->value.str = nameToken;
209 value = ExprCreate(ExprValue, TypeBoolean);
210 value->value.uval = set;
211 return VarCreate(name, value);
215 InterpCreate(char *sym, ExprDef * match)
219 def = uTypedAlloc(InterpDef);
222 def->common.stmtType = StmtInterpDef;
223 def->common.next = NULL;
229 FATAL("Couldn't allocate interp definition in parser\n");
236 KeyTypeCreate(xkb_atom_t name, VarDef * body)
240 def = uTypedAlloc(KeyTypeDef);
243 def->common.stmtType = StmtKeyTypeDef;
244 def->common.next = NULL;
245 def->merge = MergeDefault;
251 FATAL("Couldn't allocate key type definition in parser\n");
258 SymbolsCreate(const char *keyName, ExprDef *symbols)
262 def = uTypedAlloc(SymbolsDef);
265 def->common.stmtType = StmtSymbolsDef;
266 def->common.next = NULL;
267 def->merge = MergeDefault;
268 memset(def->keyName, 0, 5);
269 strncpy(def->keyName, keyName, 4);
270 def->symbols = symbols;
274 FATAL("Couldn't allocate symbols definition in parser\n");
281 GroupCompatCreate(int group, ExprDef * val)
285 def = uTypedAlloc(GroupCompatDef);
288 def->common.stmtType = StmtGroupCompatDef;
289 def->common.next = NULL;
290 def->merge = MergeDefault;
296 FATAL("Couldn't allocate group compat definition in parser\n");
303 ModMapCreate(uint32_t modifier, ExprDef * keys)
307 def = uTypedAlloc(ModMapDef);
310 def->common.stmtType = StmtModMapDef;
311 def->common.next = NULL;
312 def->merge = MergeDefault;
313 def->modifier = modifier;
318 FATAL("Couldn't allocate mod mask definition in parser\n");
325 IndicatorMapCreate(xkb_atom_t name, VarDef * body)
327 IndicatorMapDef *def;
329 def = uTypedAlloc(IndicatorMapDef);
332 def->common.stmtType = StmtIndicatorMapDef;
333 def->common.next = NULL;
334 def->merge = MergeDefault;
340 FATAL("Couldn't allocate indicator map definition in parser\n");
347 IndicatorNameCreate(int ndx, ExprDef * name, bool virtual)
349 IndicatorNameDef *def;
351 def = uTypedAlloc(IndicatorNameDef);
354 def->common.stmtType = StmtIndicatorNameDef;
355 def->common.next = NULL;
356 def->merge = MergeDefault;
359 def->virtual = virtual;
363 FATAL("Couldn't allocate indicator index definition in parser\n");
370 ActionCreate(xkb_atom_t name, ExprDef * args)
374 act = uTypedAlloc(ExprDef);
377 act->common.stmtType = StmtExpr;
378 act->common.next = NULL;
379 act->op = ExprActionDecl;
380 act->value.action.name = name;
381 act->value.action.args = args;
384 FATAL("Couldn't allocate ActionDef in parser\n");
389 CreateKeysymList(char *sym)
393 def = ExprCreate(ExprKeysymList, TypeSymbols);
396 FATAL("Couldn't allocate expression for keysym list in parser\n");
400 darray_init(def->value.list.syms);
401 darray_init(def->value.list.symsMapIndex);
402 darray_init(def->value.list.symsNumEntries);
404 darray_append(def->value.list.syms, sym);
405 darray_append(def->value.list.symsMapIndex, 0);
406 darray_append(def->value.list.symsNumEntries, 1);
412 CreateMultiKeysymList(ExprDef *list)
414 size_t nLevels = darray_size(list->value.list.symsMapIndex);
416 darray_resize(list->value.list.symsMapIndex, 1);
417 darray_resize(list->value.list.symsNumEntries, 1);
418 darray_item(list->value.list.symsMapIndex, 0) = 0;
419 darray_item(list->value.list.symsNumEntries, 0) = nLevels;
425 AppendKeysymList(ExprDef * list, char *sym)
427 size_t nSyms = darray_size(list->value.list.syms);
429 darray_append(list->value.list.symsMapIndex, nSyms);
430 darray_append(list->value.list.symsNumEntries, 1);
431 darray_append(list->value.list.syms, sym);
437 AppendMultiKeysymList(ExprDef * list, ExprDef * append)
439 size_t nSyms = darray_size(list->value.list.syms);
440 size_t numEntries = darray_size(append->value.list.syms);
442 darray_append(list->value.list.symsMapIndex, nSyms);
443 darray_append(list->value.list.symsNumEntries, numEntries);
444 darray_append_items(list->value.list.syms,
445 &darray_item(append->value.list.syms, 0),
448 darray_resize(append->value.list.syms, 0);
449 FreeStmt(&append->common);
455 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
459 if ((!str) || (strcasecmp(str, "any") == 0) ||
460 (strcasecmp(str, "nosymbol") == 0))
462 *sym_rtrn = XKB_KEY_NoSymbol;
465 else if ((strcasecmp(str, "none") == 0) ||
466 (strcasecmp(str, "voidsymbol") == 0))
468 *sym_rtrn = XKB_KEY_VoidSymbol;
471 sym = xkb_keysym_from_name(str);
472 if (sym != XKB_KEY_NoSymbol)
481 FreeInclude(IncludeStmt *incl);
484 IncludeCreate(char *str, unsigned merge)
486 IncludeStmt *incl, *first;
487 char *file, *map, *stmt, *tmp, *extra_data;
495 stmt = uDupString(str);
496 while ((tmp) && (*tmp))
498 if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
500 if ((file == NULL) && (map == NULL))
507 first = incl = uTypedAlloc(IncludeStmt);
510 incl->next = uTypedAlloc(IncludeStmt);
515 incl->common.stmtType = StmtInclude;
516 incl->common.next = NULL;
521 incl->modifier = extra_data;
527 WSGO("Allocation failure in IncludeCreate\n");
528 ACTION("Using only part of the include\n");
532 merge = MergeAugment;
534 merge = MergeOverride;
548 ERROR("Illegal include statement \"%s\"\n", stmt);
556 CheckDefaultMap(XkbFile * maps, const char *fileName)
561 for (tmp = maps, dflt = NULL; tmp != NULL;
562 tmp = (XkbFile *) tmp->common.next)
564 if (tmp->flags & XkbLC_Default)
570 if (warningLevel > 2)
572 WARN("Multiple default components in %s\n",
573 (fileName ? fileName : "(unknown)"));
574 ACTION("Using %s, ignoring %s\n",
575 (dflt->name ? dflt->name : "(first)"),
576 (tmp->name ? tmp->name : "(subsequent)"));
578 tmp->flags &= (~XkbLC_Default);
585 * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
588 static const unsigned char componentSpecLegal[] = {
589 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
590 0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
591 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
592 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
596 EnsureSafeMapName(char *name)
601 while (*name!='\0') {
602 if ((componentSpecLegal[(*name) / 8] & (1 << ((*name) % 8))) == 0)
609 CreateXKBFile(struct xkb_context *ctx, int type, char *name,
610 ParseCommon *defs, unsigned flags)
614 file = uTypedAlloc(XkbFile);
617 EnsureSafeMapName(name);
618 memset(file, 0, sizeof(XkbFile));
620 file->topName = uDupString(name);
623 file->id = xkb_context_take_file_id(ctx);
630 StmtSetMerge(ParseCommon * stmt, unsigned merge, struct YYLTYPE *loc, void *scanner)
632 if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
634 yyerror(loc, scanner, "illegal use of 'alternate' merge mode");
635 merge = MergeDefault;
641 FreeExpr(ExprDef *expr)
655 FreeStmt(&expr->value.child->common);
662 FreeStmt(&expr->value.binary.left->common);
663 FreeStmt(&expr->value.binary.right->common);
666 FreeStmt(&expr->value.action.args->common);
669 FreeStmt(&expr->value.array.entry->common);
672 darray_foreach(sym, expr->value.list.syms)
674 darray_free(expr->value.list.syms);
675 darray_free(expr->value.list.symsMapIndex);
676 darray_free(expr->value.list.symsNumEntries);
684 FreeInclude(IncludeStmt *incl)
694 free(incl->modifier);
704 FreeStmt(ParseCommon *stmt)
714 switch (stmt->stmtType)
717 FreeInclude((IncludeStmt *)stmt);
718 /* stmt is already free'd here. */
725 FreeStmt(&u.var->name->common);
726 FreeStmt(&u.var->value->common);
729 FreeStmt(&u.keyType->body->common);
733 FreeStmt(&u.interp->match->common);
734 FreeStmt(&u.interp->def->common);
737 FreeStmt(&u.vmod->value->common);
740 FreeStmt(&u.syms->symbols->common);
743 FreeStmt(&u.modMask->keys->common);
745 case StmtGroupCompatDef:
746 FreeStmt(&u.groupCompat->def->common);
748 case StmtIndicatorMapDef:
749 FreeStmt(&u.ledMap->body->common);
751 case StmtIndicatorNameDef:
752 FreeStmt(&u.ledName->name->common);
764 FreeXKBFile(XkbFile *file)
770 next = (XkbFile *)file->common.next;
775 FreeXKBFile((XkbFile *)file->defs);
778 case XkmCompatMapIndex:
779 case XkmSymbolsIndex:
780 case XkmKeyNamesIndex:
781 case XkmGeometryIndex:
782 FreeStmt(file->defs);