blob: 2b29db9bb1f2848789584e393a8005b4f0bb302b [file] [log] [blame]
Bertho Stultiens30855912000-06-13 04:34:41 +00001/*
2 * Utility routines
3 *
4 * Copyright 1998,2000 Bertho A. Stultiens
5 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * 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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Bertho Stultiens30855912000-06-13 04:34:41 +000019 */
20
21#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000022#include "wine/port.h"
Bertho Stultiens30855912000-06-13 04:34:41 +000023
24#include <stdio.h>
25#include <stdlib.h>
26#include <stdarg.h>
27#include <string.h>
28#include <assert.h>
29#include <ctype.h>
30
31#include "wmctypes.h"
32#include "utils.h"
33#include "wmc.h"
34
35#define SUPPRESS_YACC_ERROR_MESSAGE
36
37static void generic_msg(const char *s, const char *t, va_list ap)
38{
Alexandre Julliard04ac4be2007-01-16 16:00:55 +010039 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
Bertho Stultiens30855912000-06-13 04:34:41 +000040 vfprintf(stderr, s, ap);
Bertho Stultiens30855912000-06-13 04:34:41 +000041}
42
43/*
44 * The yyerror routine should not exit because we use the error-token
45 * to determine the syntactic error in the source. However, YACC
46 * uses the same routine to print an error just before the error
47 * token is reduced.
48 * The extra routine 'xyyerror' is used to exit after giving a real
49 * message.
50 */
Alexandre Julliard4f0fa262006-09-12 09:05:15 +020051int mcy_error(const char *s, ...)
Bertho Stultiens30855912000-06-13 04:34:41 +000052{
53#ifndef SUPPRESS_YACC_ERROR_MESSAGE
54 va_list ap;
55 va_start(ap, s);
56 generic_msg(s, "Yacc error", ap);
57 va_end(ap);
58#endif
59 return 1;
60}
61
62int xyyerror(const char *s, ...)
63{
64 va_list ap;
65 va_start(ap, s);
66 generic_msg(s, "Error", ap);
67 va_end(ap);
68 exit(1);
69 return 1;
70}
71
Alexandre Julliard4f0fa262006-09-12 09:05:15 +020072int mcy_warning(const char *s, ...)
Bertho Stultiens30855912000-06-13 04:34:41 +000073{
74 va_list ap;
75 va_start(ap, s);
76 generic_msg(s, "Warning", ap);
77 va_end(ap);
78 return 0;
79}
80
81void internal_error(const char *file, int line, const char *s, ...)
82{
83 va_list ap;
84 va_start(ap, s);
85 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
86 vfprintf(stderr, s, ap);
Bertho Stultiens30855912000-06-13 04:34:41 +000087 va_end(ap);
88 exit(3);
89}
90
91void error(const char *s, ...)
92{
93 va_list ap;
94 va_start(ap, s);
95 fprintf(stderr, "Error: ");
96 vfprintf(stderr, s, ap);
Bertho Stultiens30855912000-06-13 04:34:41 +000097 va_end(ap);
98 exit(2);
99}
100
101void warning(const char *s, ...)
102{
103 va_list ap;
104 va_start(ap, s);
105 fprintf(stderr, "Warning: ");
106 vfprintf(stderr, s, ap);
Bertho Stultiens30855912000-06-13 04:34:41 +0000107 va_end(ap);
108}
109
110char *dup_basename(const char *name, const char *ext)
111{
112 int namelen;
113 int extlen = strlen(ext);
114 char *base;
115 char *slash;
116
117 if(!name)
118 name = "wmc.tab";
119
120 slash = strrchr(name, '/');
121 if (slash)
122 name = slash + 1;
123
124 namelen = strlen(name);
125
126 /* +4 for later extension and +1 for '\0' */
Alexandre Julliard1916cb12006-08-26 21:35:32 +0200127 base = xmalloc(namelen +4 +1);
Bertho Stultiens30855912000-06-13 04:34:41 +0000128 strcpy(base, name);
129 if(!strcasecmp(name + namelen-extlen, ext))
130 {
131 base[namelen - extlen] = '\0';
132 }
133 return base;
134}
135
136void *xmalloc(size_t size)
137{
138 void *res;
139
140 assert(size > 0);
141 assert(size < 102400);
142 res = malloc(size);
143 if(res == NULL)
144 {
145 error("Virtual memory exhausted.\n");
146 }
Alexandre Julliard1659f532006-08-26 21:29:13 +0200147 memset(res, 0x55, size);
Bertho Stultiens30855912000-06-13 04:34:41 +0000148 return res;
149}
150
151
152void *xrealloc(void *p, size_t size)
153{
154 void *res;
155
156 assert(size > 0);
157 assert(size < 102400);
158 res = realloc(p, size);
159 if(res == NULL)
160 {
161 error("Virtual memory exhausted.\n");
162 }
163 return res;
164}
165
166char *xstrdup(const char *str)
167{
168 char *s;
169
170 assert(str != NULL);
Alexandre Julliard1916cb12006-08-26 21:35:32 +0200171 s = xmalloc(strlen(str)+1);
Bertho Stultiens30855912000-06-13 04:34:41 +0000172 return strcpy(s, str);
173}
174
175int unistrlen(const WCHAR *s)
176{
177 int n;
178 for(n = 0; *s; n++, s++)
179 ;
180 return n;
181}
182
183WCHAR *unistrcpy(WCHAR *dst, const WCHAR *src)
184{
185 WCHAR *t = dst;
186 while(*src)
187 *t++ = *src++;
188 *t = 0;
189 return dst;
190}
191
192WCHAR *xunistrdup(const WCHAR * str)
193{
194 WCHAR *s;
195
196 assert(str != NULL);
Alexandre Julliard1916cb12006-08-26 21:35:32 +0200197 s = xmalloc((unistrlen(str)+1) * sizeof(WCHAR));
Bertho Stultiens30855912000-06-13 04:34:41 +0000198 return unistrcpy(s, str);
199}
200
201int unistricmp(const WCHAR *s1, const WCHAR *s2)
202{
203 int i;
204 int once = 0;
Francois Gougetcfc39432004-05-04 04:13:05 +0000205 static const char warn[] = "Don't know the uppercase equivalent of non acsii characters;"
Bertho Stultiens30855912000-06-13 04:34:41 +0000206 "comparison might yield wrong results";
207 while(*s1 && *s2)
208 {
209 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
210 {
211 if(!once)
212 {
213 once++;
Alexandre Julliard4f0fa262006-09-12 09:05:15 +0200214 mcy_warning(warn);
Bertho Stultiens30855912000-06-13 04:34:41 +0000215 }
216 i = *s1++ - *s2++;
217 }
218 else
219 i = toupper(*s1++) - toupper(*s2++);
220 if(i)
221 return i;
222 }
223
224 if((*s1 & 0xffff) > 0x7f || (*s2 & 0xffff) > 0x7f)
225 {
226 if(!once)
Alexandre Julliard4f0fa262006-09-12 09:05:15 +0200227 mcy_warning(warn);
Bertho Stultiens30855912000-06-13 04:34:41 +0000228 return *s1 - *s2;
229 }
230 else
231 return toupper(*s1) - toupper(*s2);
232}
233
234int unistrcmp(const WCHAR *s1, const WCHAR *s2)
235{
236 int i;
237 while(*s1 && *s2)
238 {
239 i = *s1++ - *s2++;
240 if(i)
241 return i;
242 }
243
244 return *s1 - *s2;
245}