Fix all -Wsign-compare warnings
[platform/upstream/libxkbcommon.git] / src / xkbcomp / misc.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 "xkbcomp.h"
28 #include "xkballoc.h"
29 #include "xkbmisc.h"
30 #include "xkbpath.h"
31 #include "keycodes.h"
32 #include "misc.h"
33 #include <X11/keysym.h>
34 #include "parseutils.h"
35
36 /***====================================================================***/
37
38 /**
39  * Open the file given in the include statement and parse it's content.
40  * If the statement defines a specific map to use, this map is returned in
41  * file_rtrn. Otherwise, the default map is returned.
42  *
43  * @param stmt The include statement, specifying the file name to look for.
44  * @param file_type Type of file (XkmKeyNamesIdx, etc.)
45  * @param file_rtrn Returns the key map to be used.
46  * @param merge_rtrn Always returns stmt->merge.
47  *
48  * @return True on success or False otherwise.
49  */
50 Bool
51 ProcessIncludeFile(IncludeStmt * stmt,
52                    unsigned file_type,
53                    XkbFile ** file_rtrn, unsigned *merge_rtrn)
54 {
55     FILE *file;
56     XkbFile *rtrn, *mapToUse, *next;
57     char oldFile[1024] = {0};
58     int oldLine = lineNum;
59
60     file = XkbFindFileInPath(stmt->file, file_type, &stmt->path);
61     if (file == NULL)
62     {
63         ERROR("Can't find file \"%s\" for %s include\n", stmt->file,
64                 XkbDirectoryForInclude(file_type));
65         return False;
66     }
67     if (scanFile)
68         strcpy(oldFile, scanFile);
69     else
70         memset(oldFile, 0, sizeof(oldFile));
71     oldLine = lineNum;
72     setScanState(stmt->file, 1);
73     /* parse the file */
74     if ((XKBParseFile(file, &rtrn) == 0) || (rtrn == NULL))
75     {
76         setScanState(oldFile, oldLine);
77         ERROR("Error interpreting include file \"%s\"\n", stmt->file);
78         fclose(file);
79         return False;
80     }
81     fclose(file);
82
83     mapToUse = rtrn;
84     if (stmt->map != NULL)
85     {
86         while (mapToUse)
87         {
88             next = (XkbFile *)mapToUse->common.next;
89             mapToUse->common.next = NULL;
90             if (strcmp(mapToUse->name, stmt->map) == 0 &&
91                 mapToUse->type == file_type)
92             {
93                     FreeXKBFile(next);
94                     break;
95             }
96             else
97             {
98                 FreeXKBFile(mapToUse);
99             }
100             mapToUse = next;
101         }
102         if (!mapToUse)
103         {
104             ERROR("No %s named \"%s\" in the include file \"%s\"\n",
105                    XkbcConfigText(file_type), stmt->map, stmt->file);
106             return False;
107         }
108     }
109     else if ((rtrn->common.next != NULL) && (warningLevel > 5))
110     {
111         WARN("No map in include statement, but \"%s\" contains several\n",
112               stmt->file);
113         ACTION("Using first defined map, \"%s\"\n", rtrn->name);
114     }
115     setScanState(oldFile, oldLine);
116     if (mapToUse->type != file_type)
117     {
118         ERROR("Include file wrong type (expected %s, got %s)\n",
119                XkbcConfigText(file_type), XkbcConfigText(mapToUse->type));
120         ACTION("Include file \"%s\" ignored\n", stmt->file);
121         return False;
122     }
123     /* FIXME: we have to check recursive includes here (or somewhere) */
124
125     mapToUse->compiled = True;
126     *file_rtrn = mapToUse;
127     *merge_rtrn = stmt->merge;
128     return True;
129 }
130
131 /***====================================================================***/
132
133 int
134 ReportNotArray(const char *type, const char *field, const char *name)
135 {
136     ERROR("The %s %s field is not an array\n", type, field);
137     ACTION("Ignoring illegal assignment in %s\n", name);
138     return False;
139 }
140
141 int
142 ReportShouldBeArray(const char *type, const char *field, const char *name)
143 {
144     ERROR("Missing subscript for %s %s\n", type, field);
145     ACTION("Ignoring illegal assignment in %s\n", name);
146     return False;
147 }
148
149 int
150 ReportBadType(const char *type, const char *field,
151               const char *name, const char *wanted)
152 {
153     ERROR("The %s %s field must be a %s\n", type, field, wanted);
154     ACTION("Ignoring illegal assignment in %s\n", name);
155     return False;
156 }
157
158 int
159 ReportBadField(const char *type, const char *field, const char *name)
160 {
161     ERROR("Unknown %s field %s in %s\n", type, field, name);
162     ACTION("Ignoring assignment to unknown field in %s\n", name);
163     return False;
164 }
165
166 /***====================================================================***/
167
168 Bool
169 UseNewField(unsigned field,
170             CommonInfo * oldDefs, CommonInfo * newDefs, unsigned *pCollide)
171 {
172     Bool useNew;
173
174     useNew = False;
175     if (oldDefs->defined & field)
176     {
177         if (newDefs->defined & field)
178         {
179             if (((oldDefs->fileID == newDefs->fileID)
180                  && (warningLevel > 0)) || (warningLevel > 9))
181             {
182                 *pCollide |= field;
183             }
184             if (newDefs->merge != MergeAugment)
185                 useNew = True;
186         }
187     }
188     else if (newDefs->defined & field)
189         useNew = True;
190     return useNew;
191 }
192
193 char *
194 ClearCommonInfo(CommonInfo * cmn)
195 {
196     if (cmn != NULL)
197     {
198         CommonInfo *this, *next;
199         for (this = cmn; this != NULL; this = next)
200         {
201             next = this->next;
202             free(this);
203         }
204     }
205     return NULL;
206 }
207
208 char *
209 AddCommonInfo(CommonInfo * old, CommonInfo * new)
210 {
211     CommonInfo *first;
212
213     first = old;
214     while (old && old->next)
215     {
216         old = old->next;
217     }
218     new->next = NULL;
219     if (old)
220     {
221         old->next = new;
222         return (char *) first;
223     }
224     return (char *) new;
225 }
226
227 /***====================================================================***/
228
229 typedef struct _KeyNameDesc
230 {
231     xkb_atom_t level1;
232     xkb_atom_t level2;
233     char name[5];
234     Bool used;
235 } KeyNameDesc;
236
237 /**
238  * Find the key with the given name and return its keycode in kc_rtrn.
239  *
240  * @param name The 4-letter name of the key as a long.
241  * @param kc_rtrn Set to the keycode if the key was found, otherwise 0.
242  * @param use_aliases True if the key aliases should be searched too.
243  * @param create If True and the key is not found, it is added to the
244  *        xkb->names at the first free keycode.
245  * @param start_from Keycode to start searching from.
246  *
247  * @return True if found, False otherwise.
248  */
249 Bool
250 FindNamedKey(struct xkb_desc * xkb,
251              unsigned long name,
252              xkb_keycode_t *kc_rtrn,
253              Bool use_aliases, Bool create, xkb_keycode_t start_from)
254 {
255     unsigned n;
256
257     if (start_from < xkb->min_key_code)
258     {
259         start_from = xkb->min_key_code;
260     }
261     else if (start_from > xkb->max_key_code)
262     {
263         return False;
264     }
265
266     *kc_rtrn = 0;               /* some callers rely on this */
267     if (xkb && xkb->names && xkb->names->keys)
268     {
269         for (n = start_from; n <= xkb->max_key_code; n++)
270         {
271             unsigned long tmp;
272             tmp = KeyNameToLong(xkb->names->keys[n].name);
273             if (tmp == name)
274             {
275                 *kc_rtrn = n;
276                 return True;
277             }
278         }
279         if (use_aliases)
280         {
281             unsigned long new_name;
282             if (FindKeyNameForAlias(xkb, name, &new_name))
283                 return FindNamedKey(xkb, new_name, kc_rtrn, False, create, 0);
284         }
285     }
286     if (create)
287     {
288         if ((!xkb->names) || (!xkb->names->keys))
289         {
290             if (XkbcAllocNames(xkb, XkbKeyNamesMask, 0) != Success)
291             {
292                 if (warningLevel > 0)
293                 {
294                     WARN("Couldn't allocate key names in FindNamedKey\n");
295                     ACTION("Key \"%s\" not automatically created\n",
296                             longText(name));
297                 }
298                 return False;
299             }
300         }
301         /* Find first unused keycode and store our key here */
302         for (n = xkb->min_key_code; n <= xkb->max_key_code; n++)
303         {
304             if (xkb->names->keys[n].name[0] == '\0')
305             {
306                 char buf[XkbKeyNameLength + 1];
307                 LongToKeyName(name, buf);
308                 memcpy(xkb->names->keys[n].name, buf, XkbKeyNameLength);
309                 *kc_rtrn = n;
310                 return True;
311             }
312         }
313     }
314     return False;
315 }
316
317 Bool
318 FindKeyNameForAlias(struct xkb_desc * xkb, unsigned long lname,
319                     unsigned long *real_name)
320 {
321     unsigned int i;
322     char name[XkbKeyNameLength + 1];
323
324     if (xkb && xkb->names && xkb->names->key_aliases)
325     {
326         struct xkb_key_alias * a;
327         a = xkb->names->key_aliases;
328         LongToKeyName(lname, name);
329         name[XkbKeyNameLength] = '\0';
330         for (i = 0; i < xkb->names->num_key_aliases; i++, a++)
331         {
332             if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
333             {
334                 *real_name = KeyNameToLong(a->real);
335                 return True;
336             }
337         }
338     }
339     return False;
340 }