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"
32 #include <X11/keysym.h>
35 AppendStmt(ParseCommon * to, ParseCommon * append)
37 ParseCommon *start = to;
41 while ((to != NULL) && (to->next != NULL))
54 ExprCreate(unsigned op, unsigned type)
57 expr = uTypedAlloc(ExprDef);
60 expr->common.stmtType = StmtExpr;
61 expr->common.next = NULL;
67 FATAL("Couldn't allocate expression in parser\n");
74 ExprCreateUnary(unsigned op, unsigned type, ExprDef * child)
77 expr = uTypedAlloc(ExprDef);
80 expr->common.stmtType = StmtExpr;
81 expr->common.next = NULL;
84 expr->value.child = child;
88 FATAL("Couldn't allocate expression in parser\n");
95 ExprCreateBinary(unsigned op, ExprDef * left, ExprDef * right)
98 expr = uTypedAlloc(ExprDef);
101 expr->common.stmtType = StmtExpr;
102 expr->common.next = NULL;
104 if ((op == OpAssign) || (left->type == TypeUnknown))
105 expr->type = right->type;
106 else if ((left->type == right->type) || (right->type == TypeUnknown))
107 expr->type = left->type;
109 expr->type = TypeUnknown;
110 expr->value.binary.left = left;
111 expr->value.binary.right = right;
115 FATAL("Couldn't allocate expression in parser\n");
122 KeycodeCreate(const char *name, unsigned long value)
126 def = uTypedAlloc(KeycodeDef);
129 def->common.stmtType = StmtKeycodeDef;
130 def->common.next = NULL;
131 strncpy(def->name, name, XkbKeyNameLength);
132 def->name[XkbKeyNameLength] = '\0';
137 FATAL("Couldn't allocate key name definition in parser\n");
144 KeyAliasCreate(const char *alias, const char *real)
148 def = uTypedAlloc(KeyAliasDef);
151 def->common.stmtType = StmtKeyAliasDef;
152 def->common.next = NULL;
153 strncpy(def->alias, alias, XkbKeyNameLength);
154 def->alias[XkbKeyNameLength] = '\0';
155 strncpy(def->real, real, XkbKeyNameLength);
156 def->real[XkbKeyNameLength] = '\0';
160 FATAL("Couldn't allocate key alias definition in parser\n");
167 VModCreate(xkb_atom_t name, ExprDef * value)
170 def = uTypedAlloc(VModDef);
173 def->common.stmtType = StmtVModDef;
174 def->common.next = NULL;
180 FATAL("Couldn't allocate variable definition in parser\n");
187 VarCreate(ExprDef * name, ExprDef * value)
190 def = uTypedAlloc(VarDef);
193 def->common.stmtType = StmtVarDef;
194 def->common.next = NULL;
200 FATAL("Couldn't allocate variable definition in parser\n");
207 BoolVarCreate(xkb_atom_t nameToken, unsigned set)
209 ExprDef *name, *value;
211 name = ExprCreate(ExprIdent, TypeUnknown);
212 name->value.str = nameToken;
213 value = ExprCreate(ExprValue, TypeBoolean);
214 value->value.uval = set;
215 return VarCreate(name, value);
219 InterpCreate(char *sym, ExprDef * match)
223 def = uTypedAlloc(InterpDef);
226 def->common.stmtType = StmtInterpDef;
227 def->common.next = NULL;
233 FATAL("Couldn't allocate interp definition in parser\n");
240 KeyTypeCreate(xkb_atom_t name, VarDef * body)
244 def = uTypedAlloc(KeyTypeDef);
247 def->common.stmtType = StmtKeyTypeDef;
248 def->common.next = NULL;
249 def->merge = MergeDefault;
255 FATAL("Couldn't allocate key type definition in parser\n");
262 SymbolsCreate(const char *keyName, ExprDef *symbols)
266 def = uTypedAlloc(SymbolsDef);
269 def->common.stmtType = StmtSymbolsDef;
270 def->common.next = NULL;
271 def->merge = MergeDefault;
272 memset(def->keyName, 0, 5);
273 strncpy(def->keyName, keyName, 4);
274 def->symbols = symbols;
278 FATAL("Couldn't allocate symbols definition in parser\n");
285 GroupCompatCreate(int group, ExprDef * val)
289 def = uTypedAlloc(GroupCompatDef);
292 def->common.stmtType = StmtGroupCompatDef;
293 def->common.next = NULL;
294 def->merge = MergeDefault;
300 FATAL("Couldn't allocate group compat definition in parser\n");
307 ModMapCreate(uint32_t modifier, ExprDef * keys)
311 def = uTypedAlloc(ModMapDef);
314 def->common.stmtType = StmtModMapDef;
315 def->common.next = NULL;
316 def->merge = MergeDefault;
317 def->modifier = modifier;
322 FATAL("Couldn't allocate mod mask definition in parser\n");
329 IndicatorMapCreate(xkb_atom_t name, VarDef * body)
331 IndicatorMapDef *def;
333 def = uTypedAlloc(IndicatorMapDef);
336 def->common.stmtType = StmtIndicatorMapDef;
337 def->common.next = NULL;
338 def->merge = MergeDefault;
344 FATAL("Couldn't allocate indicator map definition in parser\n");
351 IndicatorNameCreate(int ndx, ExprDef * name, bool virtual)
353 IndicatorNameDef *def;
355 def = uTypedAlloc(IndicatorNameDef);
358 def->common.stmtType = StmtIndicatorNameDef;
359 def->common.next = NULL;
360 def->merge = MergeDefault;
363 def->virtual = virtual;
367 FATAL("Couldn't allocate indicator index definition in parser\n");
374 ActionCreate(xkb_atom_t name, ExprDef * args)
378 act = uTypedAlloc(ExprDef);
381 act->common.stmtType = StmtExpr;
382 act->common.next = NULL;
383 act->op = ExprActionDecl;
384 act->value.action.name = name;
385 act->value.action.args = args;
388 FATAL("Couldn't allocate ActionDef in parser\n");
393 ResizeKeysymList(ExprDef *list, unsigned int extra)
397 if (list->value.list.nSyms + extra > list->value.list.szSyms)
399 list->value.list.szSyms *= 2;
400 list->value.list.szSyms += extra;
401 if (list->value.list.szSyms == 1)
402 list->value.list.szSyms = 4;
403 list->value.list.syms = uTypedRecalloc(list->value.list.syms,
404 list->value.list.nSyms,
405 list->value.list.szSyms,
407 if (list->value.list.syms == NULL)
409 FATAL("Couldn't resize list of symbols for append\n");
413 if (list->value.list.nLevels >= list->value.list.szLevels)
415 list->value.list.szLevels *= 2;
416 if (list->value.list.szLevels == 0)
417 list->value.list.szLevels = 4;
418 list->value.list.symsMapIndex =
419 uTypedRecalloc(list->value.list.symsMapIndex,
420 list->value.list.nLevels,
421 list->value.list.szLevels,
423 if (list->value.list.symsMapIndex == NULL)
425 FATAL("Couldn't resize keysym index map for append\n");
428 list->value.list.symsNumEntries =
429 uTypedRecalloc(list->value.list.symsNumEntries,
430 list->value.list.nLevels,
431 list->value.list.szLevels,
433 if (list->value.list.symsNumEntries == NULL)
435 FATAL("Couldn't resize num keysym entries for append\n");
438 for (i = list->value.list.nLevels; i < list->value.list.szLevels; i++)
439 list->value.list.symsMapIndex[i] = -1;
446 CreateKeysymList(char *sym)
450 def = ExprCreate(ExprKeysymList, TypeSymbols);
453 FATAL("Couldn't allocate expression for keysym list in parser\n");
457 def->value.list.nSyms = 0;
458 def->value.list.szSyms = 0;
459 def->value.list.nLevels = 0;
460 def->value.list.szLevels = 0;
461 def->value.list.syms = NULL;
462 def->value.list.symsMapIndex = NULL;
463 def->value.list.symsNumEntries = NULL;
465 if (!ResizeKeysymList(def, 1))
467 FreeStmt(&def->common);
471 def->value.list.syms[0] = sym;
472 def->value.list.symsMapIndex[0] = 0;
473 def->value.list.symsNumEntries[0] = 1;
474 def->value.list.nLevels = 1;
475 def->value.list.nSyms = 1;
481 CreateMultiKeysymList(ExprDef *list)
485 for (i = 1; i < list->value.list.szLevels; i++)
487 list->value.list.symsMapIndex[i] = -1;
488 list->value.list.symsNumEntries[i] = 0;
490 list->value.list.symsMapIndex[0] = 0;
491 list->value.list.symsNumEntries[0] = list->value.list.nLevels;
492 list->value.list.nLevels = 1;
498 AppendKeysymList(ExprDef * list, char *sym)
500 if (!ResizeKeysymList(list, 1))
503 list->value.list.symsMapIndex[list->value.list.nLevels] =
504 list->value.list.nSyms;
505 list->value.list.symsNumEntries[list->value.list.nLevels] = 1;
506 list->value.list.syms[list->value.list.nSyms++] = sym;
507 list->value.list.nLevels++;
512 AppendMultiKeysymList(ExprDef * list, ExprDef * append)
516 if (!ResizeKeysymList(list, append->value.list.nSyms))
519 list->value.list.symsMapIndex[list->value.list.nLevels] =
520 list->value.list.nSyms;
521 list->value.list.symsNumEntries[list->value.list.nLevels] =
522 append->value.list.nSyms;
523 for (i = 0; i < append->value.list.nSyms; i++) {
524 list->value.list.syms[list->value.list.nSyms++] =
525 append->value.list.syms[i];
526 append->value.list.syms[i] = NULL;
528 list->value.list.nLevels++;
530 FreeStmt(&append->common);
536 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
540 if ((!str) || (strcasecmp(str, "any") == 0) ||
541 (strcasecmp(str, "nosymbol") == 0))
543 *sym_rtrn = XKB_KEYSYM_NO_SYMBOL;
546 else if ((strcasecmp(str, "none") == 0) ||
547 (strcasecmp(str, "voidsymbol") == 0))
549 *sym_rtrn = XK_VoidSymbol;
552 sym = xkb_string_to_keysym(str);
553 if (sym != XKB_KEYSYM_NO_SYMBOL)
562 FreeInclude(IncludeStmt *incl);
565 IncludeCreate(char *str, unsigned merge)
567 IncludeStmt *incl, *first;
568 char *file, *map, *stmt, *tmp, *extra_data;
576 stmt = uDupString(str);
577 while ((tmp) && (*tmp))
579 if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
581 if ((file == NULL) && (map == NULL))
588 first = incl = uTypedAlloc(IncludeStmt);
591 incl->next = uTypedAlloc(IncludeStmt);
596 incl->common.stmtType = StmtInclude;
597 incl->common.next = NULL;
602 incl->modifier = extra_data;
608 WSGO("Allocation failure in IncludeCreate\n");
609 ACTION("Using only part of the include\n");
613 merge = MergeAugment;
615 merge = MergeOverride;
629 ERROR("Illegal include statement \"%s\"\n", stmt);
637 CheckDefaultMap(XkbFile * maps, const char *fileName)
642 for (tmp = maps, dflt = NULL; tmp != NULL;
643 tmp = (XkbFile *) tmp->common.next)
645 if (tmp->flags & XkbLC_Default)
651 if (warningLevel > 2)
653 WARN("Multiple default components in %s\n",
654 (fileName ? fileName : "(unknown)"));
655 ACTION("Using %s, ignoring %s\n",
656 (dflt->name ? dflt->name : "(first)"),
657 (tmp->name ? tmp->name : "(subsequent)"));
659 tmp->flags &= (~XkbLC_Default);
666 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
671 file = uTypedAlloc(XkbFile);
674 XkbcEnsureSafeMapName(name);
675 memset(file, 0, sizeof(XkbFile));
677 file->topName = uDupString(name);
681 file->compiled = false;
688 StmtSetMerge(ParseCommon * stmt, unsigned merge, YYLTYPE *loc, void *scanner)
690 if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
692 yyerror(loc, scanner, "illegal use of 'alternate' merge mode");
693 merge = MergeDefault;
699 FreeExpr(ExprDef *expr)
713 FreeStmt(&expr->value.child->common);
720 FreeStmt(&expr->value.binary.left->common);
721 FreeStmt(&expr->value.binary.right->common);
724 FreeStmt(&expr->value.action.args->common);
727 FreeStmt(&expr->value.array.entry->common);
730 for (i = 0; i < expr->value.list.nSyms; i++)
731 free(expr->value.list.syms[i]);
732 free(expr->value.list.syms);
733 free(expr->value.list.symsMapIndex);
734 free(expr->value.list.symsNumEntries);
742 FreeInclude(IncludeStmt *incl)
752 free(incl->modifier);
762 FreeStmt(ParseCommon *stmt)
772 switch (stmt->stmtType)
775 FreeInclude((IncludeStmt *)stmt);
776 /* stmt is already free'd here. */
783 FreeStmt(&u.var->name->common);
784 FreeStmt(&u.var->value->common);
787 FreeStmt(&u.keyType->body->common);
791 FreeStmt(&u.interp->match->common);
792 FreeStmt(&u.interp->def->common);
795 FreeStmt(&u.vmod->value->common);
798 FreeStmt(&u.syms->symbols->common);
801 FreeStmt(&u.modMask->keys->common);
803 case StmtGroupCompatDef:
804 FreeStmt(&u.groupCompat->def->common);
806 case StmtIndicatorMapDef:
807 FreeStmt(&u.ledMap->body->common);
809 case StmtIndicatorNameDef:
810 FreeStmt(&u.ledName->name->common);
822 FreeXKBFile(XkbFile *file)
828 next = (XkbFile *)file->common.next;
833 case XkmSemanticsFile:
835 FreeXKBFile((XkbFile *)file->defs);
838 case XkmCompatMapIndex:
839 case XkmSymbolsIndex:
840 case XkmKeyNamesIndex:
841 case XkmGeometryIndex:
842 case XkmGeometryFile:
843 FreeStmt(file->defs);