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