Remove unused debugging function
[platform/upstream/libxkbcommon.git] / src / xkbcomp / parseutils.c
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
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.
15
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.
24
25  ********************************************************/
26
27 #include "parseutils.h"
28 #include "xkbmisc.h"
29 #include "xkbpath.h"
30 #include "xkbparse.h"
31
32 #include <X11/keysym.h>
33
34 ParseCommon *
35 AppendStmt(ParseCommon * to, ParseCommon * append)
36 {
37     ParseCommon *start = to;
38
39     if (append == NULL)
40         return to;
41     while ((to != NULL) && (to->next != NULL))
42     {
43         to = to->next;
44     }
45     if (to)
46     {
47         to->next = append;
48         return start;
49     }
50     return append;
51 }
52
53 ExprDef *
54 ExprCreate(unsigned op, unsigned type)
55 {
56     ExprDef *expr;
57     expr = uTypedAlloc(ExprDef);
58     if (expr)
59     {
60         expr->common.stmtType = StmtExpr;
61         expr->common.next = NULL;
62         expr->op = op;
63         expr->type = type;
64     }
65     else
66     {
67         FATAL("Couldn't allocate expression in parser\n");
68         /* NOTREACHED */
69     }
70     return expr;
71 }
72
73 ExprDef *
74 ExprCreateUnary(unsigned op, unsigned type, ExprDef * child)
75 {
76     ExprDef *expr;
77     expr = uTypedAlloc(ExprDef);
78     if (expr)
79     {
80         expr->common.stmtType = StmtExpr;
81         expr->common.next = NULL;
82         expr->op = op;
83         expr->type = type;
84         expr->value.child = child;
85     }
86     else
87     {
88         FATAL("Couldn't allocate expression in parser\n");
89         /* NOTREACHED */
90     }
91     return expr;
92 }
93
94 ExprDef *
95 ExprCreateBinary(unsigned op, ExprDef * left, ExprDef * right)
96 {
97     ExprDef *expr;
98     expr = uTypedAlloc(ExprDef);
99     if (expr)
100     {
101         expr->common.stmtType = StmtExpr;
102         expr->common.next = NULL;
103         expr->op = op;
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;
108         else
109             expr->type = TypeUnknown;
110         expr->value.binary.left = left;
111         expr->value.binary.right = right;
112     }
113     else
114     {
115         FATAL("Couldn't allocate expression in parser\n");
116         /* NOTREACHED */
117     }
118     return expr;
119 }
120
121 KeycodeDef *
122 KeycodeCreate(const char *name, unsigned long value)
123 {
124     KeycodeDef *def;
125
126     def = uTypedAlloc(KeycodeDef);
127     if (def)
128     {
129         def->common.stmtType = StmtKeycodeDef;
130         def->common.next = NULL;
131         strncpy(def->name, name, XkbKeyNameLength);
132         def->name[XkbKeyNameLength] = '\0';
133         def->value = value;
134     }
135     else
136     {
137         FATAL("Couldn't allocate key name definition in parser\n");
138         /* NOTREACHED */
139     }
140     return def;
141 }
142
143 KeyAliasDef *
144 KeyAliasCreate(const char *alias, const char *real)
145 {
146     KeyAliasDef *def;
147
148     def = uTypedAlloc(KeyAliasDef);
149     if (def)
150     {
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';
157     }
158     else
159     {
160         FATAL("Couldn't allocate key alias definition in parser\n");
161         /* NOTREACHED */
162     }
163     return def;
164 }
165
166 VModDef *
167 VModCreate(xkb_atom_t name, ExprDef * value)
168 {
169     VModDef *def;
170     def = uTypedAlloc(VModDef);
171     if (def)
172     {
173         def->common.stmtType = StmtVModDef;
174         def->common.next = NULL;
175         def->name = name;
176         def->value = value;
177     }
178     else
179     {
180         FATAL("Couldn't allocate variable definition in parser\n");
181         /* NOTREACHED */
182     }
183     return def;
184 }
185
186 VarDef *
187 VarCreate(ExprDef * name, ExprDef * value)
188 {
189     VarDef *def;
190     def = uTypedAlloc(VarDef);
191     if (def)
192     {
193         def->common.stmtType = StmtVarDef;
194         def->common.next = NULL;
195         def->name = name;
196         def->value = value;
197     }
198     else
199     {
200         FATAL("Couldn't allocate variable definition in parser\n");
201         /* NOTREACHED */
202     }
203     return def;
204 }
205
206 VarDef *
207 BoolVarCreate(xkb_atom_t nameToken, unsigned set)
208 {
209     ExprDef *name, *value;
210
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);
216 }
217
218 InterpDef *
219 InterpCreate(char *sym, ExprDef * match)
220 {
221     InterpDef *def;
222
223     def = uTypedAlloc(InterpDef);
224     if (def)
225     {
226         def->common.stmtType = StmtInterpDef;
227         def->common.next = NULL;
228         def->sym = sym;
229         def->match = match;
230     }
231     else
232     {
233         FATAL("Couldn't allocate interp definition in parser\n");
234         /* NOTREACHED */
235     }
236     return def;
237 }
238
239 KeyTypeDef *
240 KeyTypeCreate(xkb_atom_t name, VarDef * body)
241 {
242     KeyTypeDef *def;
243
244     def = uTypedAlloc(KeyTypeDef);
245     if (def)
246     {
247         def->common.stmtType = StmtKeyTypeDef;
248         def->common.next = NULL;
249         def->merge = MergeDefault;
250         def->name = name;
251         def->body = body;
252     }
253     else
254     {
255         FATAL("Couldn't allocate key type definition in parser\n");
256         /* NOTREACHED */
257     }
258     return def;
259 }
260
261 SymbolsDef *
262 SymbolsCreate(const char *keyName, ExprDef *symbols)
263 {
264     SymbolsDef *def;
265
266     def = uTypedAlloc(SymbolsDef);
267     if (def)
268     {
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;
275     }
276     else
277     {
278         FATAL("Couldn't allocate symbols definition in parser\n");
279         /* NOTREACHED */
280     }
281     return def;
282 }
283
284 GroupCompatDef *
285 GroupCompatCreate(int group, ExprDef * val)
286 {
287     GroupCompatDef *def;
288
289     def = uTypedAlloc(GroupCompatDef);
290     if (def)
291     {
292         def->common.stmtType = StmtGroupCompatDef;
293         def->common.next = NULL;
294         def->merge = MergeDefault;
295         def->group = group;
296         def->def = val;
297     }
298     else
299     {
300         FATAL("Couldn't allocate group compat definition in parser\n");
301         /* NOTREACHED */
302     }
303     return def;
304 }
305
306 ModMapDef *
307 ModMapCreate(uint32_t modifier, ExprDef * keys)
308 {
309     ModMapDef *def;
310
311     def = uTypedAlloc(ModMapDef);
312     if (def)
313     {
314         def->common.stmtType = StmtModMapDef;
315         def->common.next = NULL;
316         def->merge = MergeDefault;
317         def->modifier = modifier;
318         def->keys = keys;
319     }
320     else
321     {
322         FATAL("Couldn't allocate mod mask definition in parser\n");
323         /* NOTREACHED */
324     }
325     return def;
326 }
327
328 IndicatorMapDef *
329 IndicatorMapCreate(xkb_atom_t name, VarDef * body)
330 {
331     IndicatorMapDef *def;
332
333     def = uTypedAlloc(IndicatorMapDef);
334     if (def)
335     {
336         def->common.stmtType = StmtIndicatorMapDef;
337         def->common.next = NULL;
338         def->merge = MergeDefault;
339         def->name = name;
340         def->body = body;
341     }
342     else
343     {
344         FATAL("Couldn't allocate indicator map definition in parser\n");
345         /* NOTREACHED */
346     }
347     return def;
348 }
349
350 IndicatorNameDef *
351 IndicatorNameCreate(int ndx, ExprDef * name, bool virtual)
352 {
353     IndicatorNameDef *def;
354
355     def = uTypedAlloc(IndicatorNameDef);
356     if (def)
357     {
358         def->common.stmtType = StmtIndicatorNameDef;
359         def->common.next = NULL;
360         def->merge = MergeDefault;
361         def->ndx = ndx;
362         def->name = name;
363         def->virtual = virtual;
364     }
365     else
366     {
367         FATAL("Couldn't allocate indicator index definition in parser\n");
368         /* NOTREACHED */
369     }
370     return def;
371 }
372
373 ExprDef *
374 ActionCreate(xkb_atom_t name, ExprDef * args)
375 {
376     ExprDef *act;
377
378     act = uTypedAlloc(ExprDef);
379     if (act)
380     {
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;
386         return act;
387     }
388     FATAL("Couldn't allocate ActionDef in parser\n");
389     return NULL;
390 }
391
392 static bool
393 ResizeKeysymList(ExprDef *list, unsigned int extra)
394 {
395     int i;
396
397     if (list->value.list.nSyms + extra > list->value.list.szSyms)
398     {
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,
406                                                char *);
407         if (list->value.list.syms == NULL)
408         {
409             FATAL("Couldn't resize list of symbols for append\n");
410             return false;
411         }
412     }
413     if (list->value.list.nLevels >= list->value.list.szLevels)
414     {
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,
422                            int);
423         if (list->value.list.symsMapIndex == NULL)
424         {
425             FATAL("Couldn't resize keysym index map for append\n");
426             return false;
427         }
428         list->value.list.symsNumEntries =
429             uTypedRecalloc(list->value.list.symsNumEntries,
430                            list->value.list.nLevels,
431                            list->value.list.szLevels,
432                            unsigned int);
433         if (list->value.list.symsNumEntries == NULL)
434         {
435             FATAL("Couldn't resize num keysym entries for append\n");
436             return false;
437         }
438         for (i = list->value.list.nLevels; i < list->value.list.szLevels; i++)
439             list->value.list.symsMapIndex[i] = -1;
440     }
441
442     return true;
443 }
444
445 ExprDef *
446 CreateKeysymList(char *sym)
447 {
448     ExprDef *def;
449
450     def = ExprCreate(ExprKeysymList, TypeSymbols);
451     if (!def)
452     {
453         FATAL("Couldn't allocate expression for keysym list in parser\n");
454         return NULL;
455     }
456
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;
464
465     if (!ResizeKeysymList(def, 1))
466     {
467         FreeStmt(&def->common);
468         return NULL;
469     }
470
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;
476
477     return def;
478 }
479
480 ExprDef *
481 CreateMultiKeysymList(ExprDef *list)
482 {
483     int i;
484
485     for (i = 1; i < list->value.list.szLevels; i++)
486     {
487         list->value.list.symsMapIndex[i] = -1;
488         list->value.list.symsNumEntries[i] = 0;
489     }
490     list->value.list.symsMapIndex[0] = 0;
491     list->value.list.symsNumEntries[0] = list->value.list.nLevels;
492     list->value.list.nLevels = 1;
493
494     return list;
495 }
496
497 ExprDef *
498 AppendKeysymList(ExprDef * list, char *sym)
499 {
500     if (!ResizeKeysymList(list, 1))
501         return NULL;
502
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++;
508     return list;
509 }
510
511 ExprDef *
512 AppendMultiKeysymList(ExprDef * list, ExprDef * append)
513 {
514     int i;
515
516     if (!ResizeKeysymList(list, append->value.list.nSyms))
517         return NULL;
518
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;
527     }
528     list->value.list.nLevels++;
529
530     FreeStmt(&append->common);
531
532     return list;
533 }
534
535 int
536 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
537 {
538     xkb_keysym_t sym;
539
540     if ((!str) || (strcasecmp(str, "any") == 0) ||
541         (strcasecmp(str, "nosymbol") == 0))
542     {
543         *sym_rtrn = XKB_KEYSYM_NO_SYMBOL;
544         return 1;
545     }
546     else if ((strcasecmp(str, "none") == 0) ||
547              (strcasecmp(str, "voidsymbol") == 0))
548     {
549         *sym_rtrn = XK_VoidSymbol;
550         return 1;
551     }
552     sym = xkb_string_to_keysym(str);
553     if (sym != XKB_KEYSYM_NO_SYMBOL)
554     {
555         *sym_rtrn = sym;
556         return 1;
557     }
558     return 0;
559 }
560
561 static void
562 FreeInclude(IncludeStmt *incl);
563
564 IncludeStmt *
565 IncludeCreate(char *str, unsigned merge)
566 {
567     IncludeStmt *incl, *first;
568     char *file, *map, *stmt, *tmp, *extra_data;
569     char nextop;
570     bool haveSelf;
571
572     haveSelf = false;
573     incl = first = NULL;
574     file = map = NULL;
575     tmp = str;
576     stmt = uDupString(str);
577     while ((tmp) && (*tmp))
578     {
579         if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
580         {
581             if ((file == NULL) && (map == NULL))
582             {
583                 if (haveSelf)
584                     goto BAIL;
585                 haveSelf = true;
586             }
587             if (first == NULL)
588                 first = incl = uTypedAlloc(IncludeStmt);
589             else
590             {
591                 incl->next = uTypedAlloc(IncludeStmt);
592                 incl = incl->next;
593             }
594             if (incl)
595             {
596                 incl->common.stmtType = StmtInclude;
597                 incl->common.next = NULL;
598                 incl->merge = merge;
599                 incl->stmt = NULL;
600                 incl->file = file;
601                 incl->map = map;
602                 incl->modifier = extra_data;
603                 incl->path = NULL;
604                 incl->next = NULL;
605             }
606             else
607             {
608                 WSGO("Allocation failure in IncludeCreate\n");
609                 ACTION("Using only part of the include\n");
610                 break;
611             }
612             if (nextop == '|')
613                 merge = MergeAugment;
614             else
615                 merge = MergeOverride;
616         }
617         else
618         {
619             goto BAIL;
620         }
621     }
622     if (first)
623         first->stmt = stmt;
624     else
625         free(stmt);
626     return first;
627
628 BAIL:
629     ERROR("Illegal include statement \"%s\"\n", stmt);
630     ACTION("Ignored\n");
631     FreeInclude(first);
632     free(stmt);
633     return NULL;
634 }
635
636 void
637 CheckDefaultMap(XkbFile * maps, const char *fileName)
638 {
639     XkbFile *dflt, *tmp;
640
641     dflt = NULL;
642     for (tmp = maps, dflt = NULL; tmp != NULL;
643          tmp = (XkbFile *) tmp->common.next)
644     {
645         if (tmp->flags & XkbLC_Default)
646         {
647             if (dflt == NULL)
648                 dflt = tmp;
649             else
650             {
651                 if (warningLevel > 2)
652                 {
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)"));
658                 }
659                 tmp->flags &= (~XkbLC_Default);
660             }
661         }
662     }
663 }
664
665 XkbFile *
666 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
667 {
668     XkbFile *file;
669     static int fileID;
670
671     file = uTypedAlloc(XkbFile);
672     if (file)
673     {
674         XkbcEnsureSafeMapName(name);
675         memset(file, 0, sizeof(XkbFile));
676         file->type = type;
677         file->topName = uDupString(name);
678         file->name = name;
679         file->defs = defs;
680         file->id = fileID++;
681         file->compiled = false;
682         file->flags = flags;
683     }
684     return file;
685 }
686
687 unsigned
688 StmtSetMerge(ParseCommon * stmt, unsigned merge, YYLTYPE *loc, void *scanner)
689 {
690     if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
691     {
692         yyerror(loc, scanner, "illegal use of 'alternate' merge mode");
693         merge = MergeDefault;
694     }
695     return merge;
696 }
697
698 static void
699 FreeExpr(ExprDef *expr)
700 {
701     int i;
702
703     if (!expr)
704         return;
705
706     switch (expr->op)
707     {
708     case ExprActionList:
709     case OpNegate:
710     case OpUnaryPlus:
711     case OpNot:
712     case OpInvert:
713         FreeStmt(&expr->value.child->common);
714         break;
715     case OpDivide:
716     case OpAdd:
717     case OpSubtract:
718     case OpMultiply:
719     case OpAssign:
720         FreeStmt(&expr->value.binary.left->common);
721         FreeStmt(&expr->value.binary.right->common);
722         break;
723     case ExprActionDecl:
724         FreeStmt(&expr->value.action.args->common);
725         break;
726     case ExprArrayRef:
727         FreeStmt(&expr->value.array.entry->common);
728         break;
729     case ExprKeysymList:
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);
735         break;
736     default:
737         break;
738     }
739 }
740
741 static void
742 FreeInclude(IncludeStmt *incl)
743 {
744     IncludeStmt *next;
745
746     while (incl)
747     {
748         next = incl->next;
749
750         free(incl->file);
751         free(incl->map);
752         free(incl->modifier);
753         free(incl->path);
754         free(incl->stmt);
755
756         free(incl);
757         incl = next;
758     }
759 }
760
761 void
762 FreeStmt(ParseCommon *stmt)
763 {
764     ParseCommon *next;
765     YYSTYPE u;
766
767     while (stmt)
768     {
769         next = stmt->next;
770         u.any = stmt;
771
772         switch (stmt->stmtType)
773         {
774         case StmtInclude:
775             FreeInclude((IncludeStmt *)stmt);
776             /* stmt is already free'd here. */
777             stmt = NULL;
778             break;
779         case StmtExpr:
780             FreeExpr(u.expr);
781             break;
782         case StmtVarDef:
783             FreeStmt(&u.var->name->common);
784             FreeStmt(&u.var->value->common);
785             break;
786         case StmtKeyTypeDef:
787             FreeStmt(&u.keyType->body->common);
788             break;
789         case StmtInterpDef:
790             free(u.interp->sym);
791             FreeStmt(&u.interp->match->common);
792             FreeStmt(&u.interp->def->common);
793             break;
794         case StmtVModDef:
795             FreeStmt(&u.vmod->value->common);
796             break;
797         case StmtSymbolsDef:
798             FreeStmt(&u.syms->symbols->common);
799             break;
800         case StmtModMapDef:
801             FreeStmt(&u.modMask->keys->common);
802             break;
803         case StmtGroupCompatDef:
804             FreeStmt(&u.groupCompat->def->common);
805             break;
806         case StmtIndicatorMapDef:
807             FreeStmt(&u.ledMap->body->common);
808             break;
809         case StmtIndicatorNameDef:
810             FreeStmt(&u.ledName->name->common);
811             break;
812         default:
813             break;
814         }
815
816         free(stmt);
817         stmt = next;
818     }
819 }
820
821 void
822 FreeXKBFile(XkbFile *file)
823 {
824     XkbFile *next;
825
826     while (file)
827     {
828         next = (XkbFile *)file->common.next;
829
830         switch (file->type)
831         {
832         case XkmKeymapFile:
833         case XkmSemanticsFile:
834         case XkmLayoutFile:
835             FreeXKBFile((XkbFile *)file->defs);
836             break;
837         case XkmTypesIndex:
838         case XkmCompatMapIndex:
839         case XkmSymbolsIndex:
840         case XkmKeyNamesIndex:
841         case XkmGeometryIndex:
842         case XkmGeometryFile:
843             FreeStmt(file->defs);
844             break;
845         }
846
847         free(file->name);
848         free(file->topName);
849         free(file);
850         file = next;
851     }
852 }