Git init
[framework/uifw/xorg/util/x11-xkb-utils.git] / 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 /***====================================================================***/
34
35 Opaque
36 uAlloc(unsigned size)
37 {
38     return ((Opaque) malloc(size));
39 }
40
41 /***====================================================================***/
42
43 Opaque
44 uCalloc(unsigned n, unsigned size)
45 {
46     return ((Opaque) calloc(n, size));
47 }
48
49 /***====================================================================***/
50
51 Opaque
52 uRealloc(Opaque old, unsigned newSize)
53 {
54     if (old == NULL)
55         return ((Opaque) malloc(newSize));
56     else
57         return ((Opaque) realloc((char *) old, newSize));
58 }
59
60 /***====================================================================***/
61
62 Opaque
63 uRecalloc(Opaque old, unsigned nOld, unsigned nNew, unsigned itemSize)
64 {
65     char *rtrn;
66
67     if (old == NULL)
68         rtrn = (char *) calloc(nNew, itemSize);
69     else
70     {
71         rtrn = (char *) realloc((char *) old, nNew * itemSize);
72         if ((rtrn) && (nNew > nOld))
73         {
74             bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize);
75         }
76     }
77     return (Opaque) rtrn;
78 }
79
80 /***====================================================================***/
81
82 void
83 uFree(Opaque ptr)
84 {
85     if (ptr != (Opaque) NULL)
86         free((char *) ptr);
87     return;
88 }
89
90 /***====================================================================***/
91 /***                  FUNCTION ENTRY TRACKING                           ***/
92 /***====================================================================***/
93
94 static FILE *entryFile = NULL;
95 int uEntryLevel;
96
97 Boolean
98 uSetEntryFile(char *name)
99 {
100     if ((entryFile != NULL) && (entryFile != stderr))
101     {
102         fprintf(entryFile, "switching to %s\n", name ? name : "stderr");
103         fclose(entryFile);
104     }
105     if (name != NullString)
106         entryFile = fopen(name, "w");
107     else
108         entryFile = stderr;
109     if (entryFile == NULL)
110     {
111         entryFile = stderr;
112         return (False);
113     }
114     return (True);
115 }
116
117 void
118 uEntry(int l, char *s, ...)
119 {
120     int i;
121     va_list args;
122
123     for (i = 0; i < uEntryLevel; i++)
124     {
125         putc(' ', entryFile);
126     }
127     va_start(args, s);
128     vfprintf(entryFile, s, args);
129     va_end(args);
130     uEntryLevel += l;
131 }
132
133 void
134 uExit(int l, char *rtVal)
135 {
136     int i;
137
138     uEntryLevel -= l;
139     if (uEntryLevel < 0)
140         uEntryLevel = 0;
141     for (i = 0; i < uEntryLevel; i++)
142     {
143         putc(' ', entryFile);
144     }
145     fprintf(entryFile, "---> %p\n", rtVal);
146     return;
147 }
148
149 /***====================================================================***/
150 /***                    PRINT FUNCTIONS                                 ***/
151 /***====================================================================***/
152
153 FILE *uDebugFile = NULL;
154 int uDebugIndentLevel = 0;
155 int uDebugIndentSize = 4;
156
157 Boolean
158 uSetDebugFile(char *name)
159 {
160     if ((uDebugFile != NULL) && (uDebugFile != stderr))
161     {
162         fprintf(uDebugFile, "switching to %s\n", name ? name : "stderr");
163         fclose(uDebugFile);
164     }
165     if (name != NullString)
166         uDebugFile = fopen(name, "w");
167     else
168         uDebugFile = stderr;
169     if (uDebugFile == NULL)
170     {
171         uDebugFile = stderr;
172         return (False);
173     }
174     return (True);
175 }
176
177 void
178 uDebug(char *s, ...)
179 {
180     int i;
181     va_list args;
182
183     for (i = (uDebugIndentLevel * uDebugIndentSize); i > 0; i--)
184     {
185         putc(' ', uDebugFile);
186     }
187     va_start(args, s);
188     vfprintf(uDebugFile, s, args);
189     va_end(args);
190     fflush(uDebugFile);
191 }
192
193 void
194 uDebugNOI(char *s, ...)
195 {
196     va_list args;
197
198     va_start(args, s);
199     vfprintf(uDebugFile, s, args);
200     va_end(args);
201     fflush(uDebugFile);
202 }
203
204 /***====================================================================***/
205
206 static FILE *errorFile = NULL;
207 static int outCount = 0;
208 static char *preMsg = NULL;
209 static char *postMsg = NULL;
210 static char *prefix = NULL;
211
212 Boolean
213 uSetErrorFile(char *name)
214 {
215     if ((errorFile != NULL) && (errorFile != stderr))
216     {
217         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
218         fclose(errorFile);
219     }
220     if (name != NullString)
221         errorFile = fopen(name, "w");
222     else
223         errorFile = stderr;
224     if (errorFile == NULL)
225     {
226         errorFile = stderr;
227         return (False);
228     }
229     return (True);
230 }
231
232 void
233 uInformation(const char *s, ...)
234 {
235     va_list args;
236
237     va_start(args, s);
238     vfprintf(errorFile, s, args);
239     va_end(args);
240     fflush(errorFile);
241 }
242
243 /***====================================================================***/
244
245 void
246 uAction(const char *s, ...)
247 {
248     va_list args;
249
250     if (prefix != NULL)
251         fprintf(errorFile, "%s", prefix);
252     fprintf(errorFile, "                  ");
253     va_start(args, s);
254     vfprintf(errorFile, s, args);
255     va_end(args);
256     fflush(errorFile);
257 }
258
259 /***====================================================================***/
260
261 void
262 uWarning(const char *s, ...)
263 {
264     va_list args;
265
266     if ((outCount == 0) && (preMsg != NULL))
267         fprintf(errorFile, "%s\n", preMsg);
268     if (prefix != NULL)
269         fprintf(errorFile, "%s", prefix);
270     fprintf(errorFile, "Warning:          ");
271     va_start(args, s);
272     vfprintf(errorFile, s, args);
273     va_end(args);
274     fflush(errorFile);
275     outCount++;
276 }
277
278 /***====================================================================***/
279
280 void
281 uError(const char *s, ...)
282 {
283     va_list args;
284
285     if ((outCount == 0) && (preMsg != NULL))
286         fprintf(errorFile, "%s\n", preMsg);
287     if (prefix != NULL)
288         fprintf(errorFile, "%s", prefix);
289     fprintf(errorFile, "Error:            ");
290     va_start(args, s);
291     vfprintf(errorFile, s, args);
292     va_end(args);
293     fflush(errorFile);
294     outCount++;
295 }
296
297 /***====================================================================***/
298
299 void
300 uFatalError(const char *s, ...)
301 {
302     va_list args;
303
304     if ((outCount == 0) && (preMsg != NULL))
305         fprintf(errorFile, "%s\n", preMsg);
306     if (prefix != NULL)
307         fprintf(errorFile, "%s", prefix);
308     fprintf(errorFile, "Fatal Error:      ");
309     va_start(args, s);
310     vfprintf(errorFile, s, args);
311     va_end(args);
312     fprintf(errorFile, "                  Exiting\n");
313     fflush(errorFile);
314     outCount++;
315     exit(1);
316     /* NOTREACHED */
317 }
318
319 /***====================================================================***/
320
321 void
322 uInternalError(const char *s, ...)
323 {
324     va_list args;
325
326     if ((outCount == 0) && (preMsg != NULL))
327         fprintf(errorFile, "%s\n", preMsg);
328     if (prefix != NULL)
329         fprintf(errorFile, "%s", prefix);
330     fprintf(errorFile, "Internal error:   ");
331     va_start(args, s);
332     vfprintf(errorFile, s, args);
333     va_end(args);
334     fflush(errorFile);
335     outCount++;
336 }
337
338 void
339 uSetPreErrorMessage(char *msg)
340 {
341     outCount = 0;
342     preMsg = msg;
343     return;
344 }
345
346 void
347 uSetPostErrorMessage(char *msg)
348 {
349     postMsg = msg;
350     return;
351 }
352
353 void
354 uSetErrorPrefix(char *pre)
355 {
356     prefix = pre;
357     return;
358 }
359
360 void
361 uFinishUp(void)
362 {
363     if ((outCount > 0) && (postMsg != NULL))
364         fprintf(errorFile, "%s\n", postMsg);
365     return;
366 }
367
368 /***====================================================================***/
369
370 #ifndef HAVE_STRDUP
371 char *
372 uStringDup(const char *str)
373 {
374     char *rtrn;
375
376     if (str == NULL)
377         return NULL;
378     rtrn = (char *) uAlloc(strlen(str) + 1);
379     strcpy(rtrn, str);
380     return rtrn;
381 }
382 #endif
383
384 #ifndef HAVE_STRCASECMP
385 int
386 uStrCaseCmp(const char *str1, const char *str2)
387 {
388     char buf1[512], buf2[512];
389     char c, *s;
390     register int n;
391
392     for (n = 0, s = buf1; (c = *str1++); n++)
393     {
394         if (isupper(c))
395             c = tolower(c);
396         if (n > 510)
397             break;
398         *s++ = c;
399     }
400     *s = '\0';
401     for (n = 0, s = buf2; (c = *str2++); n++)
402     {
403         if (isupper(c))
404             c = tolower(c);
405         if (n > 510)
406             break;
407         *s++ = c;
408     }
409     *s = '\0';
410     return (strcmp(buf1, buf2));
411 }
412
413 int
414 uStrCasePrefix(const char *my_prefix, char *str)
415 {
416     char c1;
417     char c2;
418     while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0'))
419     {
420         if (isupper(c1))
421             c1 = tolower(c1);
422         if (isupper(c2))
423             c2 = tolower(c2);
424         if (c1 != c2)
425             return 0;
426         my_prefix++;
427         str++;
428     }
429     if (c1 != '\0')
430         return 0;
431     return 1;
432 }
433
434 #endif