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 #include <X11/keysym.h>
32 #include <X11/Xalloca.h>
37 AppendStmt(ParseCommon * to, ParseCommon * append)
39 ParseCommon *start = to;
43 while ((to != NULL) && (to->next != NULL))
56 ExprCreate(unsigned op, unsigned type)
59 expr = uTypedAlloc(ExprDef);
62 expr->common.stmtType = StmtExpr;
63 expr->common.next = NULL;
69 FATAL("Couldn't allocate expression in parser\n");
76 ExprCreateUnary(unsigned op, unsigned type, ExprDef * child)
79 expr = uTypedAlloc(ExprDef);
82 expr->common.stmtType = StmtExpr;
83 expr->common.next = NULL;
86 expr->value.child = child;
90 FATAL("Couldn't allocate expression in parser\n");
97 ExprCreateBinary(unsigned op, ExprDef * left, ExprDef * right)
100 expr = uTypedAlloc(ExprDef);
103 expr->common.stmtType = StmtExpr;
104 expr->common.next = NULL;
106 if ((op == OpAssign) || (left->type == TypeUnknown))
107 expr->type = right->type;
108 else if ((left->type == right->type) || (right->type == TypeUnknown))
109 expr->type = left->type;
111 expr->type = TypeUnknown;
112 expr->value.binary.left = left;
113 expr->value.binary.right = right;
117 FATAL("Couldn't allocate expression in parser\n");
124 KeycodeCreate(char *name, unsigned long value)
128 def = uTypedAlloc(KeycodeDef);
131 def->common.stmtType = StmtKeycodeDef;
132 def->common.next = NULL;
133 strncpy(def->name, name, XkbKeyNameLength);
134 def->name[XkbKeyNameLength] = '\0';
139 FATAL("Couldn't allocate key name definition in parser\n");
146 KeyAliasCreate(char *alias, char *real)
150 def = uTypedAlloc(KeyAliasDef);
153 def->common.stmtType = StmtKeyAliasDef;
154 def->common.next = NULL;
155 strncpy(def->alias, alias, XkbKeyNameLength);
156 def->alias[XkbKeyNameLength] = '\0';
157 strncpy(def->real, real, XkbKeyNameLength);
158 def->real[XkbKeyNameLength] = '\0';
162 FATAL("Couldn't allocate key alias definition in parser\n");
169 VModCreate(uint32_t name, ExprDef * value)
172 def = uTypedAlloc(VModDef);
175 def->common.stmtType = StmtVModDef;
176 def->common.next = NULL;
182 FATAL("Couldn't allocate variable definition in parser\n");
189 VarCreate(ExprDef * name, ExprDef * value)
192 def = uTypedAlloc(VarDef);
195 def->common.stmtType = StmtVarDef;
196 def->common.next = NULL;
202 FATAL("Couldn't allocate variable definition in parser\n");
209 BoolVarCreate(uint32_t nameToken, unsigned set)
211 ExprDef *name, *value;
213 name = ExprCreate(ExprIdent, TypeUnknown);
214 name->value.str = nameToken;
215 value = ExprCreate(ExprValue, TypeBoolean);
216 value->value.uval = set;
217 return VarCreate(name, value);
221 InterpCreate(char *sym, ExprDef * match)
225 def = uTypedAlloc(InterpDef);
228 def->common.stmtType = StmtInterpDef;
229 def->common.next = NULL;
235 FATAL("Couldn't allocate interp definition in parser\n");
242 KeyTypeCreate(uint32_t name, VarDef * body)
246 def = uTypedAlloc(KeyTypeDef);
249 def->common.stmtType = StmtKeyTypeDef;
250 def->common.next = NULL;
251 def->merge = MergeDefault;
257 FATAL("Couldn't allocate key type definition in parser\n");
264 SymbolsCreate(char *keyName, ExprDef * symbols)
268 def = uTypedAlloc(SymbolsDef);
271 def->common.stmtType = StmtSymbolsDef;
272 def->common.next = NULL;
273 def->merge = MergeDefault;
274 memset(def->keyName, 0, 5);
275 strncpy(def->keyName, keyName, 4);
276 def->symbols = symbols;
280 FATAL("Couldn't allocate symbols definition in parser\n");
287 GroupCompatCreate(int group, ExprDef * val)
291 def = uTypedAlloc(GroupCompatDef);
294 def->common.stmtType = StmtGroupCompatDef;
295 def->common.next = NULL;
296 def->merge = MergeDefault;
302 FATAL("Couldn't allocate group compat definition in parser\n");
309 ModMapCreate(uint32_t modifier, ExprDef * keys)
313 def = uTypedAlloc(ModMapDef);
316 def->common.stmtType = StmtModMapDef;
317 def->common.next = NULL;
318 def->merge = MergeDefault;
319 def->modifier = modifier;
324 FATAL("Couldn't allocate mod mask definition in parser\n");
331 IndicatorMapCreate(uint32_t name, VarDef * body)
333 IndicatorMapDef *def;
335 def = uTypedAlloc(IndicatorMapDef);
338 def->common.stmtType = StmtIndicatorMapDef;
339 def->common.next = NULL;
340 def->merge = MergeDefault;
346 FATAL("Couldn't allocate indicator map definition in parser\n");
353 IndicatorNameCreate(int ndx, ExprDef * name, Bool virtual)
355 IndicatorNameDef *def;
357 def = uTypedAlloc(IndicatorNameDef);
360 def->common.stmtType = StmtIndicatorNameDef;
361 def->common.next = NULL;
362 def->merge = MergeDefault;
365 def->virtual = virtual;
369 FATAL("Couldn't allocate indicator index definition in parser\n");
376 ActionCreate(uint32_t name, ExprDef * args)
380 act = uTypedAlloc(ExprDef);
383 act->common.stmtType = StmtExpr;
384 act->common.next = NULL;
385 act->op = ExprActionDecl;
386 act->value.action.name = name;
387 act->value.action.args = args;
390 FATAL("Couldn't allocate ActionDef in parser\n");
395 CreateKeysymList(char *sym)
399 def = ExprCreate(ExprKeysymList, TypeSymbols);
402 def->value.list.nSyms = 1;
403 def->value.list.szSyms = 4;
404 def->value.list.syms = uTypedCalloc(4, char *);
405 if (def->value.list.syms != NULL)
407 def->value.list.syms[0] = sym;
411 FATAL("Couldn't allocate expression for keysym list in parser\n");
416 ShapeDeclCreate(uint32_t name, OutlineDef * outlines)
421 shape = uTypedAlloc(ShapeDef);
424 memset(shape, 0, sizeof(ShapeDef));
425 shape->common.stmtType = StmtShapeDef;
426 shape->common.next = NULL;
427 shape->merge = MergeDefault;
429 shape->nOutlines = 0;
430 shape->outlines = outlines;
431 for (ol = outlines; ol != NULL; ol = (OutlineDef *) ol->common.next)
441 OutlineCreate(uint32_t field, ExprDef * points)
446 outline = uTypedAlloc(OutlineDef);
449 memset(outline, 0, sizeof(OutlineDef));
450 outline->common.stmtType = StmtOutlineDef;
451 outline->common.next = NULL;
452 outline->field = field;
453 outline->nPoints = 0;
454 if (points->op == ExprCoord)
456 for (pt = points; pt != NULL; pt = (ExprDef *) pt->common.next)
461 outline->points = points;
467 KeyDeclCreate(char *name, ExprDef * expr)
471 key = uTypedAlloc(KeyDef);
474 memset(key, 0, sizeof(KeyDef));
475 key->common.stmtType = StmtKeyDef;
476 key->common.next = NULL;
486 RowDeclCreate(KeyDef * keys)
491 row = uTypedAlloc(RowDef);
494 memset(row, 0, sizeof(RowDef));
495 row->common.stmtType = StmtRowDef;
496 row->common.next = NULL;
499 for (key = keys; key != NULL; key = (KeyDef *) key->common.next)
501 if (key->common.stmtType == StmtKeyDef)
509 SectionDeclCreate(uint32_t name, RowDef * rows)
514 section = uTypedAlloc(SectionDef);
517 memset(section, 0, sizeof(SectionDef));
518 section->common.stmtType = StmtSectionDef;
519 section->common.next = NULL;
520 section->name = name;
522 section->rows = rows;
523 for (row = rows; row != NULL; row = (RowDef *) row->common.next)
525 if (row->common.stmtType == StmtRowDef)
533 OverlayKeyCreate(char *under, char *over)
537 key = uTypedAlloc(OverlayKeyDef);
540 memset(key, 0, sizeof(OverlayKeyDef));
541 key->common.stmtType = StmtOverlayKeyDef;
542 strncpy(key->over, over, XkbKeyNameLength);
543 strncpy(key->under, under, XkbKeyNameLength);
551 OverlayDeclCreate(uint32_t name, OverlayKeyDef * keys)
556 ol = uTypedAlloc(OverlayDef);
559 memset(ol, 0, sizeof(OverlayDef));
560 ol->common.stmtType = StmtOverlayDef;
563 for (key = keys; key != NULL;
564 key = (OverlayKeyDef *) key->common.next)
573 DoodadCreate(unsigned type, uint32_t name, VarDef * body)
577 doodad = uTypedAlloc(DoodadDef);
580 memset(doodad, 0, sizeof(DoodadDef));
581 doodad->common.stmtType = StmtDoodadDef;
582 doodad->common.next = NULL;
591 AppendKeysymList(ExprDef * list, char *sym)
593 if (list->value.list.nSyms >= list->value.list.szSyms)
595 list->value.list.szSyms *= 2;
596 list->value.list.syms = uTypedRecalloc(list->value.list.syms,
597 list->value.list.nSyms,
598 list->value.list.szSyms,
600 if (list->value.list.syms == NULL)
602 FATAL("Couldn't resize list of symbols for append\n");
606 list->value.list.syms[list->value.list.nSyms++] = sym;
611 LookupKeysym(char *str, xkb_keysym_t * sym_rtrn)
615 if ((!str) || (uStrCaseCmp(str, "any") == 0)
616 || (uStrCaseCmp(str, "nosymbol") == 0))
618 *sym_rtrn = NoSymbol;
621 else if ((uStrCaseCmp(str, "none") == 0)
622 || (uStrCaseCmp(str, "voidsymbol") == 0))
624 *sym_rtrn = XK_VoidSymbol;
627 sym = xkb_string_to_keysym(str);
637 FreeInclude(IncludeStmt *incl);
640 IncludeCreate(char *str, unsigned merge)
642 IncludeStmt *incl, *first;
643 char *file, *map, *stmt, *tmp, *extra_data;
651 stmt = _XkbDupString(str);
652 while ((tmp) && (*tmp))
654 if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
656 if ((file == NULL) && (map == NULL))
663 first = incl = uTypedAlloc(IncludeStmt);
666 incl->next = uTypedAlloc(IncludeStmt);
671 incl->common.stmtType = StmtInclude;
672 incl->common.next = NULL;
677 incl->modifier = extra_data;
683 WSGO("Allocation failure in IncludeCreate\n");
684 ACTION("Using only part of the include\n");
688 merge = MergeAugment;
690 merge = MergeOverride;
704 ERROR("Illegal include statement \"%s\"\n", stmt);
713 PrintStmtAddrs(ParseCommon * stmt)
715 fprintf(stderr, "0x%x", stmt);
720 fprintf(stderr, "->0x%x", stmt->next);
725 fprintf(stderr, "\n");
730 CheckDefaultMap(XkbFile * maps)
735 for (tmp = maps, dflt = NULL; tmp != NULL;
736 tmp = (XkbFile *) tmp->common.next)
738 if (tmp->flags & XkbLC_Default)
744 if (warningLevel > 2)
746 WARN("Multiple default components in %s\n",
747 (scanFile ? scanFile : "(unknown)"));
748 ACTION("Using %s, ignoring %s\n",
749 (dflt->name ? dflt->name : "(first)"),
750 (tmp->name ? tmp->name : "(subsequent)"));
752 tmp->flags &= (~XkbLC_Default);
759 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
764 file = uTypedAlloc(XkbFile);
767 XkbcEnsureSafeMapName(name);
768 memset(file, 0, sizeof(XkbFile));
770 file->topName = _XkbDupString(name);
774 file->compiled = False;
781 StmtSetMerge(ParseCommon * stmt, unsigned merge)
783 if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
785 yyerror("illegal use of 'alternate' merge mode");
786 merge = MergeDefault;
792 FreeStmt(ParseCommon *stmt);
795 FreeExpr(ExprDef *expr)
809 FreeStmt(&expr->value.child->common);
816 FreeStmt(&expr->value.binary.left->common);
817 FreeStmt(&expr->value.binary.right->common);
820 FreeStmt(&expr->value.action.args->common);
823 FreeStmt(&expr->value.array.entry->common);
826 for (i = 0; i < expr->value.list.nSyms; i++)
827 free(expr->value.list.syms[i]);
828 free(expr->value.list.syms);
836 FreeInclude(IncludeStmt *incl)
846 free(incl->modifier);
856 FreeStmt(ParseCommon *stmt)
866 switch (stmt->stmtType)
869 FreeInclude((IncludeStmt *)stmt);
870 /* stmt is already free'd here. */
877 FreeStmt(&u.var->name->common);
878 FreeStmt(&u.var->value->common);
881 FreeStmt(&u.keyType->body->common);
885 FreeStmt(&u.interp->match->common);
886 FreeStmt(&u.interp->def->common);
889 FreeStmt(&u.vmod->value->common);
892 FreeStmt(&u.syms->symbols->common);
895 FreeStmt(&u.modMask->keys->common);
897 case StmtGroupCompatDef:
898 FreeStmt(&u.groupCompat->def->common);
900 case StmtIndicatorMapDef:
901 FreeStmt(&u.ledMap->body->common);
903 case StmtIndicatorNameDef:
904 FreeStmt(&u.ledName->name->common);
907 FreeStmt(&u.outline->points->common);
910 FreeStmt(&u.shape->outlines->common);
914 FreeStmt(&u.key->expr->common);
917 FreeStmt(&u.row->keys->common);
920 FreeStmt(&u.section->rows->common);
922 case StmtOverlayKeyDef:
925 FreeStmt(&u.overlay->keys->common);
928 FreeStmt(&u.doodad->body->common);
940 FreeXKBFile(XkbFile *file)
946 next = (XkbFile *)file->common.next;
951 case XkmSemanticsFile:
953 FreeXKBFile((XkbFile *)file->defs);
956 case XkmCompatMapIndex:
957 case XkmSymbolsIndex:
958 case XkmKeyNamesIndex:
959 case XkmGeometryIndex:
960 case XkmGeometryFile:
961 FreeStmt(file->defs);