Use xkb_group_index_t for group variables throughout
[platform/upstream/libxkbcommon.git] / src / utils.c
1 /*\
2  *
3  *                          COPYRIGHT 1990
4  *                    DIGITAL EQUIPMENT CORPORATION
5  *                       MAYNARD, MASSACHUSETTS
6  *                        ALL RIGHTS RESERVED.
7  *
8  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
9  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
10  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
11  * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
12  * WARRANTY.
13  *
14  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
15  * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
16  * ADDITION TO THAT SET FORTH ABOVE.
17  *
18  * Permission to use, copy, modify, and distribute this software and its
19  * documentation for any purpose and without fee is hereby granted, provided
20  * that the above copyright notice appear in all copies and that both that
21  * copyright notice and this permission notice appear in supporting
22  * documentation, and that the name of Digital Equipment Corporation not be
23  * used in advertising or publicity pertaining to distribution of the
24  * software without specific, written prior permission.
25  \*/
26
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31
32 #include "utils.h"
33
34 static FILE *errorFile = NULL;
35 static int outCount = 0;
36 static char *preMsg = NULL;
37 static char *prefix = NULL;
38
39 bool
40 uSetErrorFile(char *name)
41 {
42     if ((errorFile != NULL) && (errorFile != stderr)) {
43         fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
44         fclose(errorFile);
45     }
46     if (name != NULL)
47         errorFile = fopen(name, "w");
48     else
49         errorFile = stderr;
50     if (errorFile == NULL) {
51         errorFile = stderr;
52         return false;
53     }
54     return true;
55 }
56
57 void
58 uInformation(const char *s, ...)
59 {
60     va_list args;
61
62     if (!errorFile)
63         errorFile = stderr;
64
65     va_start(args, s);
66     vfprintf(errorFile, s, args);
67     va_end(args);
68     fflush(errorFile);
69 }
70
71 /***====================================================================***/
72
73 void
74 uAction(const char *s, ...)
75 {
76     va_list args;
77
78     if (!errorFile)
79         errorFile = stderr;
80
81     if (prefix != NULL)
82         fprintf(errorFile, "%s", prefix);
83     fprintf(errorFile, "                  ");
84     va_start(args, s);
85     vfprintf(errorFile, s, args);
86     va_end(args);
87     fflush(errorFile);
88 }
89
90 /***====================================================================***/
91
92 void
93 uWarning(const char *s, ...)
94 {
95     va_list args;
96
97     if (!errorFile)
98         errorFile = stderr;
99
100     if ((outCount == 0) && (preMsg != NULL))
101         fprintf(errorFile, "%s\n", preMsg);
102     if (prefix != NULL)
103         fprintf(errorFile, "%s", prefix);
104     fprintf(errorFile, "Warning:          ");
105     va_start(args, s);
106     vfprintf(errorFile, s, args);
107     va_end(args);
108     fflush(errorFile);
109     outCount++;
110 }
111
112 /***====================================================================***/
113
114 void
115 uError(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, "Error:            ");
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 uFatalError(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, "Fatal Error:      ");
149     va_start(args, s);
150     vfprintf(errorFile, s, args);
151     va_end(args);
152     fprintf(errorFile, "                  Exiting\n");
153     fflush(errorFile);
154     outCount++;
155     exit(1);
156     /* NOTREACHED */
157 }
158
159 /***====================================================================***/
160
161 void
162 uInternalError(const char *s, ...)
163 {
164     va_list args;
165
166     if (!errorFile)
167         errorFile = stderr;
168
169     if ((outCount == 0) && (preMsg != NULL))
170         fprintf(errorFile, "%s\n", preMsg);
171     if (prefix != NULL)
172         fprintf(errorFile, "%s", prefix);
173     fprintf(errorFile, "Internal error:   ");
174     va_start(args, s);
175     vfprintf(errorFile, s, args);
176     va_end(args);
177     fflush(errorFile);
178     outCount++;
179 }