Remove useless stuff from utils
[profile/ivi/libxkbcommon.git] / src / 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 "XKBcommonint.h"
30
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35
36 void *
37 recalloc(void * old, unsigned nOld, unsigned nNew, unsigned itemSize)
38 {
39     char *rtrn;
40
41     if (old == NULL)
42         rtrn = calloc(nNew, itemSize);
43     else
44     {
45         rtrn = realloc(old, nNew * itemSize);
46         if ((rtrn) && (nNew > nOld))
47         {
48             memset(&rtrn[nOld * itemSize], 0, (nNew - nOld) * itemSize);
49         }
50     }
51     return rtrn;
52 }
53
54 static FILE *errorFile = NULL;
55 static int outCount = 0;
56 static char *preMsg = NULL;
57 static char *prefix = NULL;
58
59 Bool
60 uSetErrorFile(char *name)
61 {
62     if ((errorFile != NULL) && (errorFile != stderr))
63     {
64         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
65         fclose(errorFile);
66     }
67     if (name != NULL)
68         errorFile = fopen(name, "w");
69     else
70         errorFile = stderr;
71     if (errorFile == NULL)
72     {
73         errorFile = stderr;
74         return (False);
75     }
76     return (True);
77 }
78
79 void
80 uInformation(const char *s, ...)
81 {
82     va_list args;
83
84     if (!errorFile)
85         errorFile = stderr;
86
87     va_start(args, s);
88     vfprintf(errorFile, s, args);
89     va_end(args);
90     fflush(errorFile);
91 }
92
93 /***====================================================================***/
94
95 void
96 uAction(const char *s, ...)
97 {
98     va_list args;
99
100     if (!errorFile)
101         errorFile = stderr;
102
103     if (prefix != NULL)
104         fprintf(errorFile, "%s", prefix);
105     fprintf(errorFile, "                  ");
106     va_start(args, s);
107     vfprintf(errorFile, s, args);
108     va_end(args);
109     fflush(errorFile);
110 }
111
112 /***====================================================================***/
113
114 void
115 uWarning(const char *s, ...)
116 {
117     va_list args;
118
119     if (!errorFile)
120         errorFile = stderr;
121
122     if ((outCount == 0) && (preMsg != NULL))
123         fprintf(errorFile, "%s\n", preMsg);
124     if (prefix != NULL)
125         fprintf(errorFile, "%s", prefix);
126     fprintf(errorFile, "Warning:          ");
127     va_start(args, s);
128     vfprintf(errorFile, s, args);
129     va_end(args);
130     fflush(errorFile);
131     outCount++;
132 }
133
134 /***====================================================================***/
135
136 void
137 uError(const char *s, ...)
138 {
139     va_list args;
140
141     if (!errorFile)
142         errorFile = stderr;
143
144     if ((outCount == 0) && (preMsg != NULL))
145         fprintf(errorFile, "%s\n", preMsg);
146     if (prefix != NULL)
147         fprintf(errorFile, "%s", prefix);
148     fprintf(errorFile, "Error:            ");
149     va_start(args, s);
150     vfprintf(errorFile, s, args);
151     va_end(args);
152     fflush(errorFile);
153     outCount++;
154 }
155
156 /***====================================================================***/
157
158 void
159 uFatalError(const char *s, ...)
160 {
161     va_list args;
162
163     if (!errorFile)
164         errorFile = stderr;
165
166     if ((outCount == 0) && (preMsg != NULL))
167         fprintf(errorFile, "%s\n", preMsg);
168     if (prefix != NULL)
169         fprintf(errorFile, "%s", prefix);
170     fprintf(errorFile, "Fatal Error:      ");
171     va_start(args, s);
172     vfprintf(errorFile, s, args);
173     va_end(args);
174     fprintf(errorFile, "                  Exiting\n");
175     fflush(errorFile);
176     outCount++;
177     exit(1);
178     /* NOTREACHED */
179 }
180
181 /***====================================================================***/
182
183 void
184 uInternalError(const char *s, ...)
185 {
186     va_list args;
187
188     if (!errorFile)
189         errorFile = stderr;
190
191     if ((outCount == 0) && (preMsg != NULL))
192         fprintf(errorFile, "%s\n", preMsg);
193     if (prefix != NULL)
194         fprintf(errorFile, "%s", prefix);
195     fprintf(errorFile, "Internal error:   ");
196     va_start(args, s);
197     vfprintf(errorFile, s, args);
198     va_end(args);
199     fflush(errorFile);
200     outCount++;
201 }