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, uint32_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 IncludeCreate(char *str, unsigned merge)
639 IncludeStmt *incl, *first;
640 char *file, *map, *stmt, *tmp, *extra_data;
648 stmt = _XkbDupString(str);
649 while ((tmp) && (*tmp))
651 if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
653 if ((file == NULL) && (map == NULL))
660 first = incl = uTypedAlloc(IncludeStmt);
663 incl->next = uTypedAlloc(IncludeStmt);
668 incl->common.stmtType = StmtInclude;
669 incl->common.next = NULL;
674 incl->modifier = extra_data;
680 WSGO("Allocation failure in IncludeCreate\n");
681 ACTION("Using only part of the include\n");
685 merge = MergeAugment;
687 merge = MergeOverride;
700 ERROR("Illegal include statement \"%s\"\n", stmt);
707 free(first->modifier);
709 first->file = first->map = first->path = NULL;
719 PrintStmtAddrs(ParseCommon * stmt)
721 fprintf(stderr, "0x%x", stmt);
726 fprintf(stderr, "->0x%x", stmt->next);
731 fprintf(stderr, "\n");
736 CheckDefaultMap(XkbFile * maps)
741 for (tmp = maps, dflt = NULL; tmp != NULL;
742 tmp = (XkbFile *) tmp->common.next)
744 if (tmp->flags & XkbLC_Default)
750 if (warningLevel > 2)
752 WARN("Multiple default components in %s\n",
753 (scanFile ? scanFile : "(unknown)"));
754 ACTION("Using %s, ignoring %s\n",
755 (dflt->name ? dflt->name : "(first)"),
756 (tmp->name ? tmp->name : "(subsequent)"));
758 tmp->flags &= (~XkbLC_Default);
765 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
770 file = uTypedAlloc(XkbFile);
773 XkbcEnsureSafeMapName(name);
774 memset(file, 0, sizeof(XkbFile));
776 file->topName = _XkbDupString(name);
780 file->compiled = False;
787 StmtSetMerge(ParseCommon * stmt, unsigned merge)
789 if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
791 yyerror("illegal use of 'alternate' merge mode");
792 merge = MergeDefault;
798 FreeStmt(ParseCommon *stmt);
801 FreeExpr(ExprDef *expr)
815 FreeStmt(&expr->value.child->common);
822 FreeStmt(&expr->value.binary.left->common);
823 FreeStmt(&expr->value.binary.right->common);
826 FreeStmt(&expr->value.action.args->common);
829 FreeStmt(&expr->value.array.entry->common);
832 for (i = 0; i < expr->value.list.nSyms; i++)
833 free(expr->value.list.syms[i]);
834 free(expr->value.list.syms);
842 FreeInclude(IncludeStmt *incl)
852 free(incl->modifier);
862 FreeStmt(ParseCommon *stmt)
872 switch (stmt->stmtType)
875 FreeInclude((IncludeStmt *)stmt);
882 FreeStmt(&u.var->name->common);
883 FreeStmt(&u.var->value->common);
886 FreeStmt(&u.keyType->body->common);
890 FreeStmt(&u.interp->match->common);
891 FreeStmt(&u.interp->def->common);
894 FreeStmt(&u.vmod->value->common);
897 FreeStmt(&u.syms->symbols->common);
900 FreeStmt(&u.modMask->keys->common);
902 case StmtGroupCompatDef:
903 FreeStmt(&u.groupCompat->def->common);
905 case StmtIndicatorMapDef:
906 FreeStmt(&u.ledMap->body->common);
908 case StmtIndicatorNameDef:
909 FreeStmt(&u.ledName->name->common);
912 FreeStmt(&u.outline->points->common);
915 FreeStmt(&u.shape->outlines->common);
919 FreeStmt(&u.key->expr->common);
922 FreeStmt(&u.row->keys->common);
925 FreeStmt(&u.section->rows->common);
927 case StmtOverlayKeyDef:
930 FreeStmt(&u.overlay->keys->common);
933 FreeStmt(&u.doodad->body->common);
945 FreeXKBFile(XkbFile *file)
951 next = (XkbFile *)file->common.next;
956 case XkmSemanticsFile:
958 FreeXKBFile((XkbFile *)file->defs);
961 case XkmCompatMapIndex:
962 case XkmSymbolsIndex:
963 case XkmKeyNamesIndex:
964 case XkmGeometryIndex:
965 FreeStmt(file->defs);
970 /* free(file->topName); */