Avoid use of partly initialized VModInfo in error path
[platform/upstream/libxkbcommon.git] / src / xkbcomp / vmod.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 <stdio.h>
28 #include "xkbcomp.h"
29 #include "xkballoc.h"
30 #include "xkbmisc.h"
31 #include "expr.h"
32 #include "misc.h"
33
34 #include <X11/extensions/XKB.h>
35
36 #include "vmod.h"
37
38 void
39 InitVModInfo(VModInfo * info, struct xkb_desc * xkb)
40 {
41     ClearVModInfo(info, xkb);
42     info->errorCount = 0;
43 }
44
45 void
46 ClearVModInfo(VModInfo * info, struct xkb_desc * xkb)
47 {
48     int i;
49
50     info->newlyDefined = info->defined = info->available = 0;
51
52     if (XkbcAllocNames(xkb, XkbVirtualModNamesMask, 0) != Success)
53         return;
54
55     if (XkbcAllocServerMap(xkb, XkbVirtualModsMask, 0) != Success)
56         return;
57
58     info->xkb = xkb;
59     if (xkb && xkb->names)
60     {
61         int bit;
62         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
63         {
64             if (xkb->names->vmods[i] != NULL)
65                 info->defined |= bit;
66         }
67     }
68 }
69
70 /***====================================================================***/
71
72 /**
73  * Handle one entry in the virtualModifiers line (e.g. NumLock).
74  * If the entry is e.g. NumLock=Mod1, stmt->value is not NULL, and the
75  * XkbServerMap's vmod is set to the given modifier. Otherwise, the vmod is 0.
76  *
77  * @param stmt The statement specifying the name and (if any the value).
78  * @param mergeMode Merge strategy (e.g. MergeOverride)
79  */
80 Bool
81 HandleVModDef(VModDef * stmt, struct xkb_desc *xkb, unsigned mergeMode,
82               VModInfo * info)
83 {
84     int i, bit, nextFree;
85     ExprResult mod;
86     struct xkb_server_map * srv;
87     struct xkb_names * names;
88
89     srv = xkb->server;
90     names = xkb->names;
91     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<= 1)
92     {
93         if (info->defined & bit)
94         {
95             if (names->vmods[i] &&
96                 strcmp(names->vmods[i], XkbcAtomText(stmt->name)) == 0)
97             {                   /* already defined */
98                 info->available |= bit;
99                 if (stmt->value == NULL)
100                     return True;
101                 else
102                 {
103                     const char *str1;
104                     const char *str2 = "";
105                     if (!ExprResolveModMask(stmt->value, &mod))
106                     {
107                         str1 = XkbcAtomText(stmt->name);
108                         ACTION("Declaration of %s ignored\n", str1);
109                         return False;
110                     }
111                     if (mod.uval == srv->vmods[i])
112                         return True;
113
114                     str1 = XkbcAtomText(stmt->name);
115                     WARN("Virtual modifier %s multiply defined\n", str1);
116                     str1 = XkbcModMaskText(srv->vmods[i], True);
117                     if (mergeMode == MergeOverride)
118                     {
119                         str2 = str1;
120                         str1 = XkbcModMaskText(mod.uval, True);
121                     }
122                     ACTION("Using %s, ignoring %s\n", str1, str2);
123                     if (mergeMode == MergeOverride)
124                         srv->vmods[i] = mod.uval;
125                     return True;
126                 }
127             }
128         }
129         else if (nextFree < 0)
130             nextFree = i;
131     }
132     if (nextFree < 0)
133     {
134         ERROR("Too many virtual modifiers defined (maximum %d)\n",
135                XkbNumVirtualMods);
136         return False;
137     }
138     info->defined |= (1 << nextFree);
139     info->newlyDefined |= (1 << nextFree);
140     info->available |= (1 << nextFree);
141     names->vmods[nextFree] = XkbcAtomGetString(stmt->name);
142     if (stmt->value == NULL)
143         return True;
144     if (ExprResolveModMask(stmt->value, &mod))
145     {
146         srv->vmods[nextFree] = mod.uval;
147         return True;
148     }
149     ACTION("Declaration of %s ignored\n", XkbcAtomText(stmt->name));
150     return False;
151 }
152
153 /**
154  * Returns the index of the given modifier in the xkb->names->vmods array.
155  *
156  * @param priv Pointer to the xkb data structure.
157  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
158  * @param type Must be TypeInt, otherwise return False.
159  * @param val_rtrn Set to the index of the modifier that matches.
160  *
161  * @return True on success, False otherwise. If False is returned, val_rtrn is
162  * undefined.
163  */
164 static int
165 LookupVModIndex(const struct xkb_desc *xkb, xkb_atom_t field, unsigned type,
166                 ExprResult * val_rtrn)
167 {
168     int i;
169     const char *name = XkbcAtomText(field);
170
171     if ((xkb == NULL) || (xkb->names == NULL) || (type != TypeInt))
172     {
173         return False;
174     }
175     /* For each named modifier, get the name and compare it to the one passed
176      * in. If we get a match, return the index of the modifier.
177      * The order of modifiers is the same as in the virtual_modifiers line in
178      * the xkb_types section.
179      */
180     for (i = 0; i < XkbNumVirtualMods; i++)
181     {
182         if (xkb->names->vmods[i] && strcmp(xkb->names->vmods[i], name) == 0)
183         {
184             val_rtrn->uval = i;
185             return True;
186         }
187     }
188     return False;
189 }
190
191 /**
192  * Get the mask for the given (virtual or core) modifier and set
193  * val_rtrn.uval to the mask value.
194  *
195  * @param priv Pointer to xkb data structure.
196  * @param val_rtrn Member uval is set to the mask returned.
197  *
198  * @return True on success, False otherwise. If False is returned, val_rtrn is
199  * undefined.
200  */
201 int
202 LookupVModMask(const void * priv, xkb_atom_t field, unsigned type,
203                ExprResult * val_rtrn)
204 {
205     if (LookupModMask(NULL, field, type, val_rtrn))
206     {
207         return True;
208     }
209     else if (LookupVModIndex(priv, field, type, val_rtrn))
210     {
211         unsigned ndx = val_rtrn->uval;
212         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
213         return True;
214     }
215     return False;
216 }
217
218 int
219 FindKeypadVMod(struct xkb_desc * xkb)
220 {
221     xkb_atom_t name;
222     ExprResult rtrn;
223
224     name = xkb_intern_atom("NumLock");
225     if ((xkb) && LookupVModIndex(xkb, name, TypeInt, &rtrn))
226     {
227         return rtrn.ival;
228     }
229     return -1;
230 }
231
232 Bool
233 ResolveVirtualModifier(ExprDef * def, struct xkb_desc *xkb,
234                        ExprResult * val_rtrn, VModInfo * info)
235 {
236     struct xkb_names * names;
237
238     names = xkb->names;
239     if (def->op == ExprIdent)
240     {
241         int i, bit;
242         const char *name = XkbcAtomText(def->value.str);
243         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
244         {
245             if ((info->available & bit) && strcmp(names->vmods[i], name) == 0)
246             {
247                 val_rtrn->uval = i;
248                 return True;
249             }
250         }
251     }
252     if (ExprResolveInteger(def, val_rtrn))
253     {
254         if (val_rtrn->uval < XkbNumVirtualMods)
255             return True;
256         ERROR("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
257                val_rtrn->uval, XkbNumVirtualMods - 1);
258     }
259     return False;
260 }