Don't leak strings in merge declarations
[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 *ptr, size_t old_size, size_t new_size)
38 {
39     char *rtrn;
40
41     if (ptr == NULL)
42         return calloc(1, new_size);
43
44     rtrn = realloc(ptr, new_size);
45     if (rtrn && new_size > old_size)
46         memset(&rtrn[old_size], 0, new_size - old_size);
47
48     return 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 Bool
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 != NULL)
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         errorFile = stderr;
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         errorFile = stderr;
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         errorFile = stderr;
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         errorFile = stderr;
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         errorFile = stderr;
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         errorFile = stderr;
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 }