push 715329f5a8f1967d0599203de97483c8ca73ae2c
[wine/hacks.git] / tools / wmc / utils.c
blobbdcd6c04d658ac287fb5d6f2390be32fb4847f9c
1 /*
2 * Utility routines
4 * Copyright 1998,2000 Bertho A. Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wmctypes.h"
32 #include "utils.h"
33 #include "wmc.h"
35 #define SUPPRESS_YACC_ERROR_MESSAGE
37 static void generic_msg(const char *s, const char *t, va_list ap)
39 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
40 vfprintf(stderr, s, ap);
41 fprintf(stderr, "\n");
45 * The yyerror routine should not exit because we use the error-token
46 * to determine the syntactic error in the source. However, YACC
47 * uses the same routine to print an error just before the error
48 * token is reduced.
49 * The extra routine 'xyyerror' is used to exit after giving a real
50 * message.
52 int mcy_error(const char *s, ...)
54 #ifndef SUPPRESS_YACC_ERROR_MESSAGE
55 va_list ap;
56 va_start(ap, s);
57 generic_msg(s, "Yacc error", ap);
58 va_end(ap);
59 #endif
60 return 1;
63 int xyyerror(const char *s, ...)
65 va_list ap;
66 va_start(ap, s);
67 generic_msg(s, "Error", ap);
68 va_end(ap);
69 exit(1);
70 return 1;
73 int mcy_warning(const char *s, ...)
75 va_list ap;
76 va_start(ap, s);
77 generic_msg(s, "Warning", ap);
78 va_end(ap);
79 return 0;
82 void internal_error(const char *file, int line, const char *s, ...)
84 va_list ap;
85 va_start(ap, s);
86 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
87 vfprintf(stderr, s, ap);
88 fprintf(stderr, "\n");
89 va_end(ap);
90 exit(3);
93 void error(const char *s, ...)
95 va_list ap;
96 va_start(ap, s);
97 fprintf(stderr, "Error: ");
98 vfprintf(stderr, s, ap);
99 fprintf(stderr, "\n");
100 va_end(ap);
101 exit(2);
104 void warning(const char *s, ...)
106 va_list ap;
107 va_start(ap, s);
108 fprintf(stderr, "Warning: ");
109 vfprintf(stderr, s, ap);
110 fprintf(stderr, "\n");
111 va_end(ap);
114 char *dup_basename(const char *name, const char *ext)
116 int namelen;
117 int extlen = strlen(ext);
118 char *base;
119 char *slash;
121 if(!name)
122 name = "wmc.tab";
124 slash = strrchr(name, '/');
125 if (slash)
126 name = slash + 1;
128 namelen = strlen(name);
130 /* +4 for later extension and +1 for '\0' */
131 base = xmalloc(namelen +4 +1);
132 strcpy(base, name);
133 if(!strcasecmp(name + namelen-extlen, ext))
135 base[namelen - extlen] = '\0';
137 return base;
140 void *xmalloc(size_t size)
142 void *res;
144 assert(size > 0);
145 assert(size < 102400);
146 res = malloc(size);
147 if(res == NULL)
149 error("Virtual memory exhausted.\n");
151 memset(res, 0x55, size);
152 return res;
156 void *xrealloc(void *p, size_t size)
158 void *res;
160 assert(size > 0);
161 assert(size < 102400);
162 res = realloc(p, size);
163 if(res == NULL)
165 error("Virtual memory exhausted.\n");
167 return res;
170 char *xstrdup(const char *str)
172 char *s;
174 assert(str != NULL);
175 s = xmalloc(strlen(str)+1);
176 return strcpy(s, str);
179 int unistrlen(const WCHAR *s)
181 int n;
182 for(n = 0; *s; n++, s++)
184 return n;
187 WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
189 WCHAR *t = dst;
190 while(*src)
191 *t++ = *src++;
192 *t = 0;
193 return dst;
196 WCHAR *xunistrdup(const WCHAR * str)
198 WCHAR *s;
200 assert(str != NULL);
201 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
202 return unistrcpy(s, str);
205 int unistricmp(const WCHAR *s1, const WCHAR *s2)
207 int i;
208 int once = 0;
209 static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
210 "comparison might yield wrong results";
211 while(*s1 && *s2)
213 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
215 if(!once)
217 once++;
218 mcy_warning(warn);
220 i = *s1++ - *s2++;
222 else
223 i = toupper(*s1++) - toupper(*s2++);
224 if(i)
225 return i;
228 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
230 if(!once)
231 mcy_warning(warn);
232 return *s1 - *s2;
234 else
235 return toupper(*s1) - toupper(*s2);
238 int unistrcmp(const WCHAR *s1, const WCHAR *s2)
240 int i;
241 while(*s1 && *s2)
243 i = *s1++ - *s2++;
244 if(i)
245 return i;
248 return *s1 - *s2;