69b8a5c2330eac7faf08bac487cf043a77be4442
[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 <X11/keysym.h>
31 #include <X11/Xalloca.h>
32
33 XkbFile *rtrnValue;
34
35 ParseCommon *
36 AppendStmt(ParseCommon * to, ParseCommon * append)
37 {
38     ParseCommon *start = to;
39
40     if (append == NULL)
41         return to;
42     while ((to != NULL) && (to->next != NULL))
43     {
44         to = to->next;
45     }
46     if (to)
47     {
48         to->next = append;
49         return start;
50     }
51     return append;
52 }
53
54 ExprDef *
55 ExprCreate(unsigned op, unsigned type)
56 {
57     ExprDef *expr;
58     expr = uTypedAlloc(ExprDef);
59     if (expr)
60     {
61         expr->common.stmtType = StmtExpr;
62         expr->common.next = NULL;
63         expr->op = op;
64         expr->type = type;
65     }
66     else
67     {
68         FATAL("Couldn't allocate expression in parser\n");
69         /* NOTREACHED */
70     }
71     return expr;
72 }
73
74 ExprDef *
75 ExprCreateUnary(unsigned op, unsigned type, ExprDef * child)
76 {
77     ExprDef *expr;
78     expr = uTypedAlloc(ExprDef);
79     if (expr)
80     {
81         expr->common.stmtType = StmtExpr;
82         expr->common.next = NULL;
83         expr->op = op;
84         expr->type = type;
85         expr->value.child = child;
86     }
87     else
88     {
89         FATAL("Couldn't allocate expression in parser\n");
90         /* NOTREACHED */
91     }
92     return expr;
93 }
94
95 ExprDef *
96 ExprCreateBinary(unsigned op, ExprDef * left, ExprDef * right)
97 {
98     ExprDef *expr;
99     expr = uTypedAlloc(ExprDef);
100     if (expr)
101     {
102         expr->common.stmtType = StmtExpr;
103         expr->common.next = NULL;
104         expr->op = op;
105         if ((op == OpAssign) || (left->type == TypeUnknown))
106             expr->type = right->type;
107         else if ((left->type == right->type) || (right->type == TypeUnknown))
108             expr->type = left->type;
109         else
110             expr->type = TypeUnknown;
111         expr->value.binary.left = left;
112         expr->value.binary.right = right;
113     }
114     else
115     {
116         FATAL("Couldn't allocate expression in parser\n");
117         /* NOTREACHED */
118     }
119     return expr;
120 }
121
122 KeycodeDef *
123 KeycodeCreate(char *name, unsigned long value)
124 {
125     KeycodeDef *def;
126
127     def = uTypedAlloc(KeycodeDef);
128     if (def)
129     {
130         def->common.stmtType = StmtKeycodeDef;
131         def->common.next = NULL;
132         strncpy(def->name, name, XkbKeyNameLength);
133         def->name[XkbKeyNameLength] = '\0';
134         def->value = value;
135     }
136     else
137     {
138         FATAL("Couldn't allocate key name definition in parser\n");
139         /* NOTREACHED */
140     }
141     return def;
142 }
143
144 KeyAliasDef *
145 KeyAliasCreate(char *alias, char *real)
146 {
147     KeyAliasDef *def;
148
149     def = uTypedAlloc(KeyAliasDef);
150     if (def)
151     {
152         def->common.stmtType = StmtKeyAliasDef;
153         def->common.next = NULL;
154         strncpy(def->alias, alias, XkbKeyNameLength);
155         def->alias[XkbKeyNameLength] = '\0';
156         strncpy(def->real, real, XkbKeyNameLength);
157         def->real[XkbKeyNameLength] = '\0';
158     }
159     else
160     {
161         FATAL("Couldn't allocate key alias definition in parser\n");
162         /* NOTREACHED */
163     }
164     return def;
165 }
166
167 VModDef *
168 VModCreate(uint32_t name, ExprDef * value)
169 {
170     VModDef *def;
171     def = uTypedAlloc(VModDef);
172     if (def)
173     {
174         def->common.stmtType = StmtVModDef;
175         def->common.next = NULL;
176         def->name = name;
177         def->value = value;
178     }
179     else
180     {
181         FATAL("Couldn't allocate variable definition in parser\n");
182         /* NOTREACHED */
183     }
184     return def;
185 }
186
187 VarDef *
188 VarCreate(ExprDef * name, ExprDef * value)
189 {
190     VarDef *def;
191     def = uTypedAlloc(VarDef);
192     if (def)
193     {
194         def->common.stmtType = StmtVarDef;
195         def->common.next = NULL;
196         def->name = name;
197         def->value = value;
198     }
199     else
200     {
201         FATAL("Couldn't allocate variable definition in parser\n");
202         /* NOTREACHED */
203     }
204     return def;
205 }
206
207 VarDef *
208 BoolVarCreate(uint32_t nameToken, unsigned set)
209 {
210     ExprDef *name, *value;
211
212     name = ExprCreate(ExprIdent, TypeUnknown);
213     name->value.str = nameToken;
214     value = ExprCreate(ExprValue, TypeBoolean);
215     value->value.uval = set;
216     return VarCreate(name, value);
217 }
218
219 InterpDef *
220 InterpCreate(char *sym, ExprDef * match)
221 {
222     InterpDef *def;
223
224     def = uTypedAlloc(InterpDef);
225     if (def)
226     {
227         def->common.stmtType = StmtInterpDef;
228         def->common.next = NULL;
229         def->sym = strdup(sym);
230         def->match = match;
231     }
232     else
233     {
234         FATAL("Couldn't allocate interp definition in parser\n");
235         /* NOTREACHED */
236     }
237     return def;
238 }
239
240 KeyTypeDef *
241 KeyTypeCreate(uint32_t name, VarDef * body)
242 {
243     KeyTypeDef *def;
244
245     def = uTypedAlloc(KeyTypeDef);
246     if (def)
247     {
248         def->common.stmtType = StmtKeyTypeDef;
249         def->common.next = NULL;
250         def->merge = MergeDefault;
251         def->name = name;
252         def->body = body;
253     }
254     else
255     {
256         FATAL("Couldn't allocate key type definition in parser\n");
257         /* NOTREACHED */
258     }
259     return def;
260 }
261
262 SymbolsDef *
263 SymbolsCreate(char *keyName, ExprDef * symbols)
264 {
265     SymbolsDef *def;
266
267     def = uTypedAlloc(SymbolsDef);
268     if (def)
269     {
270         def->common.stmtType = StmtSymbolsDef;
271         def->common.next = NULL;
272         def->merge = MergeDefault;
273         bzero(def->keyName, 5);
274         strncpy(def->keyName, keyName, 4);
275         def->symbols = symbols;
276     }
277     else
278     {
279         FATAL("Couldn't allocate symbols definition in parser\n");
280         /* NOTREACHED */
281     }
282     return def;
283 }
284
285 GroupCompatDef *
286 GroupCompatCreate(int group, ExprDef * val)
287 {
288     GroupCompatDef *def;
289
290     def = uTypedAlloc(GroupCompatDef);
291     if (def)
292     {
293         def->common.stmtType = StmtGroupCompatDef;
294         def->common.next = NULL;
295         def->merge = MergeDefault;
296         def->group = group;
297         def->def = val;
298     }
299     else
300     {
301         FATAL("Couldn't allocate group compat definition in parser\n");
302         /* NOTREACHED */
303     }
304     return def;
305 }
306
307 ModMapDef *
308 ModMapCreate(uint32_t modifier, ExprDef * keys)
309 {
310     ModMapDef *def;
311
312     def = uTypedAlloc(ModMapDef);
313     if (def)
314     {
315         def->common.stmtType = StmtModMapDef;
316         def->common.next = NULL;
317         def->merge = MergeDefault;
318         def->modifier = modifier;
319         def->keys = keys;
320     }
321     else
322     {
323         FATAL("Couldn't allocate mod mask definition in parser\n");
324         /* NOTREACHED */
325     }
326     return def;
327 }
328
329 IndicatorMapDef *
330 IndicatorMapCreate(uint32_t name, VarDef * body)
331 {
332     IndicatorMapDef *def;
333
334     def = uTypedAlloc(IndicatorMapDef);
335     if (def)
336     {
337         def->common.stmtType = StmtIndicatorMapDef;
338         def->common.next = NULL;
339         def->merge = MergeDefault;
340         def->name = name;
341         def->body = body;
342     }
343     else
344     {
345         FATAL("Couldn't allocate indicator map definition in parser\n");
346         /* NOTREACHED */
347     }
348     return def;
349 }
350
351 IndicatorNameDef *
352 IndicatorNameCreate(int ndx, ExprDef * name, Bool virtual)
353 {
354     IndicatorNameDef *def;
355
356     def = uTypedAlloc(IndicatorNameDef);
357     if (def)
358     {
359         def->common.stmtType = StmtIndicatorNameDef;
360         def->common.next = NULL;
361         def->merge = MergeDefault;
362         def->ndx = ndx;
363         def->name = name;
364         def->virtual = virtual;
365     }
366     else
367     {
368         FATAL("Couldn't allocate indicator index definition in parser\n");
369         /* NOTREACHED */
370     }
371     return def;
372 }
373
374 ExprDef *
375 ActionCreate(uint32_t name, ExprDef * args)
376 {
377     ExprDef *act;
378
379     act = uTypedAlloc(ExprDef);
380     if (act)
381     {
382         act->common.stmtType = StmtExpr;
383         act->common.next = NULL;
384         act->op = ExprActionDecl;
385         act->value.action.name = name;
386         act->value.action.args = args;
387         return act;
388     }
389     FATAL("Couldn't allocate ActionDef in parser\n");
390     return NULL;
391 }
392
393 ExprDef *
394 CreateKeysymList(char *sym)
395 {
396     ExprDef *def;
397
398     def = ExprCreate(ExprKeysymList, TypeSymbols);
399     if (def)
400     {
401         def->value.list.nSyms = 1;
402         def->value.list.szSyms = 4;
403         def->value.list.syms = uTypedCalloc(4, char *);
404         if (def->value.list.syms != NULL)
405         {
406             def->value.list.syms[0] = sym;
407             return def;
408         }
409     }
410     FATAL("Couldn't allocate expression for keysym list in parser\n");
411     return NULL;
412 }
413
414 ShapeDef *
415 ShapeDeclCreate(uint32_t name, OutlineDef * outlines)
416 {
417     ShapeDef *shape;
418     OutlineDef *ol;
419
420     shape = uTypedAlloc(ShapeDef);
421     if (shape != NULL)
422     {
423         bzero(shape, sizeof(ShapeDef));
424         shape->common.stmtType = StmtShapeDef;
425         shape->common.next = NULL;
426         shape->merge = MergeDefault;
427         shape->name = name;
428         shape->nOutlines = 0;
429         shape->outlines = outlines;
430         for (ol = outlines; ol != NULL; ol = (OutlineDef *) ol->common.next)
431         {
432             if (ol->nPoints > 0)
433                 shape->nOutlines++;
434         }
435     }
436     return shape;
437 }
438
439 OutlineDef *
440 OutlineCreate(uint32_t field, ExprDef * points)
441 {
442     OutlineDef *outline;
443     ExprDef *pt;
444
445     outline = uTypedAlloc(OutlineDef);
446     if (outline != NULL)
447     {
448         bzero(outline, sizeof(OutlineDef));
449         outline->common.stmtType = StmtOutlineDef;
450         outline->common.next = NULL;
451         outline->field = field;
452         outline->nPoints = 0;
453         if (points->op == ExprCoord)
454         {
455             for (pt = points; pt != NULL; pt = (ExprDef *) pt->common.next)
456             {
457                 outline->nPoints++;
458             }
459         }
460         outline->points = points;
461     }
462     return outline;
463 }
464
465 KeyDef *
466 KeyDeclCreate(char *name, ExprDef * expr)
467 {
468     KeyDef *key;
469
470     key = uTypedAlloc(KeyDef);
471     if (key != NULL)
472     {
473         bzero(key, sizeof(KeyDef));
474         key->common.stmtType = StmtKeyDef;
475         key->common.next = NULL;
476         if (name)
477             key->name = name;
478         else
479             key->expr = expr;
480     }
481     return key;
482 }
483
484 RowDef *
485 RowDeclCreate(KeyDef * keys)
486 {
487     RowDef *row;
488     KeyDef *key;
489
490     row = uTypedAlloc(RowDef);
491     if (row != NULL)
492     {
493         bzero(row, sizeof(RowDef));
494         row->common.stmtType = StmtRowDef;
495         row->common.next = NULL;
496         row->nKeys = 0;
497         row->keys = keys;
498         for (key = keys; key != NULL; key = (KeyDef *) key->common.next)
499         {
500             if (key->common.stmtType == StmtKeyDef)
501                 row->nKeys++;
502         }
503     }
504     return row;
505 }
506
507 SectionDef *
508 SectionDeclCreate(uint32_t name, RowDef * rows)
509 {
510     SectionDef *section;
511     RowDef *row;
512
513     section = uTypedAlloc(SectionDef);
514     if (section != NULL)
515     {
516         bzero(section, sizeof(SectionDef));
517         section->common.stmtType = StmtSectionDef;
518         section->common.next = NULL;
519         section->name = name;
520         section->nRows = 0;
521         section->rows = rows;
522         for (row = rows; row != NULL; row = (RowDef *) row->common.next)
523         {
524             if (row->common.stmtType == StmtRowDef)
525                 section->nRows++;
526         }
527     }
528     return section;
529 }
530
531 OverlayKeyDef *
532 OverlayKeyCreate(char *under, char *over)
533 {
534     OverlayKeyDef *key;
535
536     key = uTypedAlloc(OverlayKeyDef);
537     if (key != NULL)
538     {
539         bzero(key, sizeof(OverlayKeyDef));
540         key->common.stmtType = StmtOverlayKeyDef;
541         strncpy(key->over, over, XkbKeyNameLength);
542         strncpy(key->under, under, XkbKeyNameLength);
543         if (over)
544             free(over);
545         if (under)
546             free(under);
547     }
548     return key;
549 }
550
551 OverlayDef *
552 OverlayDeclCreate(uint32_t name, OverlayKeyDef * keys)
553 {
554     OverlayDef *ol;
555     OverlayKeyDef *key;
556
557     ol = uTypedAlloc(OverlayDef);
558     if (ol != NULL)
559     {
560         bzero(ol, sizeof(OverlayDef));
561         ol->common.stmtType = StmtOverlayDef;
562         ol->name = name;
563         ol->keys = keys;
564         for (key = keys; key != NULL;
565              key = (OverlayKeyDef *) key->common.next)
566         {
567             ol->nKeys++;
568         }
569     }
570     return ol;
571 }
572
573 DoodadDef *
574 DoodadCreate(unsigned type, uint32_t name, VarDef * body)
575 {
576     DoodadDef *doodad;
577
578     doodad = uTypedAlloc(DoodadDef);
579     if (doodad != NULL)
580     {
581         bzero(doodad, sizeof(DoodadDef));
582         doodad->common.stmtType = StmtDoodadDef;
583         doodad->common.next = NULL;
584         doodad->type = type;
585         doodad->name = name;
586         doodad->body = body;
587     }
588     return doodad;
589 }
590
591 ExprDef *
592 AppendKeysymList(ExprDef * list, char *sym)
593 {
594     if (list->value.list.nSyms >= list->value.list.szSyms)
595     {
596         list->value.list.szSyms *= 2;
597         list->value.list.syms = uTypedRecalloc(list->value.list.syms,
598                                                list->value.list.nSyms,
599                                                list->value.list.szSyms,
600                                                char *);
601         if (list->value.list.syms == NULL)
602         {
603             FATAL("Couldn't resize list of symbols for append\n");
604             return NULL;
605         }
606     }
607     list->value.list.syms[list->value.list.nSyms++] = sym;
608     return list;
609 }
610
611 int
612 LookupKeysym(char *str, uint32_t * sym_rtrn)
613 {
614     uint32_t sym;
615
616     if ((!str) || (uStrCaseCmp(str, "any") == 0)
617         || (uStrCaseCmp(str, "nosymbol") == 0))
618     {
619         *sym_rtrn = NoSymbol;
620         return 1;
621     }
622     else if ((uStrCaseCmp(str, "none") == 0)
623              || (uStrCaseCmp(str, "voidsymbol") == 0))
624     {
625         *sym_rtrn = XK_VoidSymbol;
626         return 1;
627     }
628     sym = xkb_string_to_keysym(str);
629     if (sym != NoSymbol)
630     {
631         *sym_rtrn = sym;
632         return 1;
633     }
634     return 0;
635 }
636
637 IncludeStmt *
638 IncludeCreate(char *str, unsigned merge)
639 {
640     IncludeStmt *incl, *first;
641     char *file, *map, *stmt, *tmp, *extra_data;
642     char nextop;
643     Bool haveSelf;
644
645     haveSelf = False;
646     incl = first = NULL;
647     file = map = NULL;
648     tmp = str;
649     stmt = _XkbDupString(str);
650     while ((tmp) && (*tmp))
651     {
652         if (XkbParseIncludeMap(&tmp, &file, &map, &nextop, &extra_data))
653         {
654             if ((file == NULL) && (map == NULL))
655             {
656                 if (haveSelf)
657                     goto BAIL;
658                 haveSelf = True;
659             }
660             if (first == NULL)
661                 first = incl = uTypedAlloc(IncludeStmt);
662             else
663             {
664                 incl->next = uTypedAlloc(IncludeStmt);
665                 incl = incl->next;
666             }
667             if (incl)
668             {
669                 incl->common.stmtType = StmtInclude;
670                 incl->common.next = NULL;
671                 incl->merge = merge;
672                 incl->stmt = NULL;
673                 incl->file = file;
674                 incl->map = map;
675                 incl->modifier = extra_data;
676                 incl->path = NULL;
677                 incl->next = NULL;
678             }
679             else
680             {
681                 WSGO("Allocation failure in IncludeCreate\n");
682                 ACTION("Using only part of the include\n");
683                 break;
684             }
685             if (nextop == '|')
686                 merge = MergeAugment;
687             else
688                 merge = MergeOverride;
689         }
690         else
691         {
692             goto BAIL;
693         }
694     }
695     if (first)
696         first->stmt = stmt;
697     else if (stmt)
698         free(stmt);
699     return first;
700   BAIL:
701     ERROR("Illegal include statement \"%s\"\n", stmt);
702     ACTION("Ignored\n");
703     while (first)
704     {
705         incl = first->next;
706         if (first->file)
707             free(first->file);
708         if (first->map)
709             free(first->map);
710         if (first->modifier)
711             free(first->modifier);
712         if (first->path)
713             free(first->path);
714         first->file = first->map = first->path = NULL;
715         free(first);
716         first = incl;
717     }
718     if (stmt)
719         free(stmt);
720     return NULL;
721 }
722
723 #ifdef DEBUG
724 void
725 PrintStmtAddrs(ParseCommon * stmt)
726 {
727     fprintf(stderr, "0x%x", stmt);
728     if (stmt)
729     {
730         do
731         {
732             fprintf(stderr, "->0x%x", stmt->next);
733             stmt = stmt->next;
734         }
735         while (stmt);
736     }
737     fprintf(stderr, "\n");
738 }
739 #endif
740
741 void
742 CheckDefaultMap(XkbFile * maps)
743 {
744     XkbFile *dflt, *tmp;
745
746     dflt = NULL;
747     for (tmp = maps, dflt = NULL; tmp != NULL;
748          tmp = (XkbFile *) tmp->common.next)
749     {
750         if (tmp->flags & XkbLC_Default)
751         {
752             if (dflt == NULL)
753                 dflt = tmp;
754             else
755             {
756                 if (warningLevel > 2)
757                 {
758                     WARN("Multiple default components in %s\n",
759                           (scanFile ? scanFile : "(unknown)"));
760                     ACTION("Using %s, ignoring %s\n",
761                             (dflt->name ? dflt->name : "(first)"),
762                             (tmp->name ? tmp->name : "(subsequent)"));
763                 }
764                 tmp->flags &= (~XkbLC_Default);
765             }
766         }
767     }
768     return;
769 }
770
771 XkbFile *
772 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
773 {
774     XkbFile *file;
775     static int fileID;
776
777     file = uTypedAlloc(XkbFile);
778     if (file)
779     {
780         XkbcEnsureSafeMapName(name);
781         bzero(file, sizeof(XkbFile));
782         file->type = type;
783         file->topName = _XkbDupString(name);
784         file->name = name;
785         file->defs = defs;
786         file->id = fileID++;
787         file->compiled = False;
788         file->flags = flags;
789     }
790     return file;
791 }
792
793 unsigned
794 StmtSetMerge(ParseCommon * stmt, unsigned merge)
795 {
796     if ((merge == MergeAltForm) && (stmt->stmtType != StmtKeycodeDef))
797     {
798         yyerror("illegal use of 'alternate' merge mode");
799         merge = MergeDefault;
800     }
801     return merge;
802 }