Introduce xkb_keycode_t for keycodes
[platform/upstream/libxkbcommon.git] / src / xkbcomp / utils.c
1
2   /*\
3    *
4    *                          COPYRIGHT 1990
5    *                    DIGITAL EQUIPMENT CORPORATION
6    *                       MAYNARD, MASSACHUSETTS
7    *                        ALL RIGHTS RESERVED.
8    *
9    * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
10    * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
11    * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
12    * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
13    * WARRANTY.
14    *
15    * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
16    * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
17    * ADDITION TO THAT SET FORTH ABOVE.
18    *
19    * Permission to use, copy, modify, and distribute this software and its
20    * documentation for any purpose and without fee is hereby granted, provided
21    * that the above copyright notice appear in all copies and that both that
22    * copyright notice and this permission notice appear in supporting
23    * documentation, and that the name of Digital Equipment Corporation not be
24    * used in advertising or publicity pertaining to distribution of the
25    * software without specific, written prior permission.
26    \*/
27
28 #include        "utils.h"
29 #include        <ctype.h>
30 #include        <stdlib.h>
31 #include        <stdarg.h>
32
33 void *
34 recalloc(void * old, unsigned nOld, unsigned nNew, unsigned itemSize)
35 {
36     char *rtrn;
37
38     if (old == NULL)
39         rtrn = (char *) calloc(nNew, itemSize);
40     else
41     {
42         rtrn = (char *) realloc((char *) old, nNew * itemSize);
43         if ((rtrn) && (nNew > nOld))
44         {
45             bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize);
46         }
47     }
48     return (void *) rtrn;
49 }
50
51 static FILE *errorFile = NULL;
52 static int outCount = 0;
53 static char *preMsg = NULL;
54 static char *prefix = NULL;
55
56 Boolean
57 uSetErrorFile(char *name)
58 {
59     if ((errorFile != NULL) && (errorFile != stderr))
60     {
61         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
62         fclose(errorFile);
63     }
64     if (name != NullString)
65         errorFile = fopen(name, "w");
66     else
67         errorFile = stderr;
68     if (errorFile == NULL)
69     {
70         errorFile = stderr;
71         return (False);
72     }
73     return (True);
74 }
75
76 void
77 uInformation(const char *s, ...)
78 {
79     va_list args;
80
81     if (!errorFile)
82         return;
83
84     va_start(args, s);
85     vfprintf(errorFile, s, args);
86     va_end(args);
87     fflush(errorFile);
88 }
89
90 /***====================================================================***/
91
92 void
93 uAction(const char *s, ...)
94 {
95     va_list args;
96
97     if (!errorFile)
98         return;
99
100     if (prefix != NULL)
101         fprintf(errorFile, "%s", prefix);
102     fprintf(errorFile, "                  ");
103     va_start(args, s);
104     vfprintf(errorFile, s, args);
105     va_end(args);
106     fflush(errorFile);
107 }
108
109 /***====================================================================***/
110
111 void
112 uWarning(const char *s, ...)
113 {
114     va_list args;
115
116     if (!errorFile)
117         return;
118
119     if ((outCount == 0) && (preMsg != NULL))
120         fprintf(errorFile, "%s\n", preMsg);
121     if (prefix != NULL)
122         fprintf(errorFile, "%s", prefix);
123     fprintf(errorFile, "Warning:          ");
124     va_start(args, s);
125     vfprintf(errorFile, s, args);
126     va_end(args);
127     fflush(errorFile);
128     outCount++;
129 }
130
131 /***====================================================================***/
132
133 void
134 uError(const char *s, ...)
135 {
136     va_list args;
137
138     if (!errorFile)
139         return;
140
141     if ((outCount == 0) && (preMsg != NULL))
142         fprintf(errorFile, "%s\n", preMsg);
143     if (prefix != NULL)
144         fprintf(errorFile, "%s", prefix);
145     fprintf(errorFile, "Error:            ");
146     va_start(args, s);
147     vfprintf(errorFile, s, args);
148     va_end(args);
149     fflush(errorFile);
150     outCount++;
151 }
152
153 /***====================================================================***/
154
155 void
156 uFatalError(const char *s, ...)
157 {
158     va_list args;
159
160     if (!errorFile)
161         return;
162
163     if ((outCount == 0) && (preMsg != NULL))
164         fprintf(errorFile, "%s\n", preMsg);
165     if (prefix != NULL)
166         fprintf(errorFile, "%s", prefix);
167     fprintf(errorFile, "Fatal Error:      ");
168     va_start(args, s);
169     vfprintf(errorFile, s, args);
170     va_end(args);
171     fprintf(errorFile, "                  Exiting\n");
172     fflush(errorFile);
173     outCount++;
174     exit(1);
175     /* NOTREACHED */
176 }
177
178 /***====================================================================***/
179
180 void
181 uInternalError(const char *s, ...)
182 {
183     va_list args;
184
185     if (!errorFile)
186         return;
187
188     if ((outCount == 0) && (preMsg != NULL))
189         fprintf(errorFile, "%s\n", preMsg);
190     if (prefix != NULL)
191         fprintf(errorFile, "%s", prefix);
192     fprintf(errorFile, "Internal error:   ");
193     va_start(args, s);
194     vfprintf(errorFile, s, args);
195     va_end(args);
196     fflush(errorFile);
197     outCount++;
198 }
199
200 /***====================================================================***/
201
202 #ifndef HAVE_STRCASECMP
203 int
204 uStrCaseCmp(const char *str1, const char *str2)
205 {
206     char buf1[512], buf2[512];
207     char c, *s;
208     register int n;
209
210     for (n = 0, s = buf1; (c = *str1++); n++)
211     {
212         if (isupper(c))
213             c = tolower(c);
214         if (n > 510)
215             break;
216         *s++ = c;
217     }
218     *s = '\0';
219     for (n = 0, s = buf2; (c = *str2++); n++)
220     {
221         if (isupper(c))
222             c = tolower(c);
223         if (n > 510)
224             break;
225         *s++ = c;
226     }
227     *s = '\0';
228     return (strcmp(buf1, buf2));
229 }
230
231 int
232 uStrCasePrefix(const char *my_prefix, char *str)
233 {
234     char c1;
235     char c2;
236     while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0'))
237     {
238         if (isupper(c1))
239             c1 = tolower(c1);
240         if (isupper(c2))
241             c2 = tolower(c2);
242         if (c1 != c2)
243             return 0;
244         my_prefix++;
245         str++;
246     }
247     if (c1 != '\0')
248         return 0;
249     return 1;
250 }
251
252 #endif