configure: Make the libXi soname check depend on the header check.
[wine/dibdrv.git] / tools / wrc / utils.c
blob39d17641896516c37cc89bed62d5ed787b97835f
1 /*
2 * Utility routines
4 * Copyright 1998 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 <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <ctype.h>
32 #include "wine/unicode.h"
33 #include "wrc.h"
34 #include "utils.h"
35 #include "parser.h"
37 /* #define WANT_NEAR_INDICATION */
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str)
42 while(*str)
44 if(!isprint(*str))
45 *str = ' ';
46 str++;
49 #endif
51 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
53 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
54 vfprintf(stderr, s, ap);
55 #ifdef WANT_NEAR_INDICATION
57 char *cpy;
58 if(n)
60 cpy = xstrdup(n);
61 make_print(cpy);
62 fprintf(stderr, " near '%s'", cpy);
63 free(cpy);
66 #endif
67 fprintf(stderr, "\n");
71 int parser_error(const char *s, ...)
73 va_list ap;
74 va_start(ap, s);
75 generic_msg(s, "Error", parser_text, ap);
76 va_end(ap);
77 exit(1);
78 return 1;
81 int parser_warning(const char *s, ...)
83 va_list ap;
84 va_start(ap, s);
85 generic_msg(s, "Warning", parser_text, ap);
86 va_end(ap);
87 return 0;
90 void internal_error(const char *file, int line, const char *s, ...)
92 va_list ap;
93 va_start(ap, s);
94 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
95 vfprintf(stderr, s, ap);
96 fprintf(stderr, "\n");
97 va_end(ap);
98 exit(3);
101 void error(const char *s, ...)
103 va_list ap;
104 va_start(ap, s);
105 fprintf(stderr, "Error: ");
106 vfprintf(stderr, s, ap);
107 fprintf(stderr, "\n");
108 va_end(ap);
109 exit(2);
112 void warning(const char *s, ...)
114 va_list ap;
115 va_start(ap, s);
116 fprintf(stderr, "Warning: ");
117 vfprintf(stderr, s, ap);
118 fprintf(stderr, "\n");
119 va_end(ap);
122 void chat(const char *s, ...)
124 if(debuglevel & DEBUGLEVEL_CHAT)
126 va_list ap;
127 va_start(ap, s);
128 fprintf(stderr, "FYI: ");
129 vfprintf(stderr, s, ap);
130 fprintf(stderr, "\n");
131 va_end(ap);
135 char *dup_basename(const char *name, const char *ext)
137 int namelen;
138 int extlen = strlen(ext);
139 char *base;
140 char *slash;
142 if(!name)
143 name = "wrc.tab";
145 slash = strrchr(name, '/');
146 if (slash)
147 name = slash + 1;
149 namelen = strlen(name);
151 /* +4 for later extension and +1 for '\0' */
152 base = xmalloc(namelen +4 +1);
153 strcpy(base, name);
154 if(!strcasecmp(name + namelen-extlen, ext))
156 base[namelen - extlen] = '\0';
158 return base;
161 void *xmalloc(size_t size)
163 void *res;
165 assert(size > 0);
166 res = malloc(size);
167 if(res == NULL)
169 error("Virtual memory exhausted.\n");
171 memset(res, 0x55, size);
172 return res;
176 void *xrealloc(void *p, size_t size)
178 void *res;
180 assert(size > 0);
181 res = realloc(p, size);
182 if(res == NULL)
184 error("Virtual memory exhausted.\n");
186 return res;
189 char *xstrdup(const char *str)
191 char *s;
193 assert(str != NULL);
194 s = xmalloc(strlen(str)+1);
195 return strcpy(s, str);
200 *****************************************************************************
201 * Function : compare_name_id
202 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
203 * Input :
204 * Output :
205 * Description :
206 * Remarks :
207 *****************************************************************************
209 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
211 if(n1->type == name_ord && n2->type == name_ord)
213 return n1->name.i_name - n2->name.i_name;
215 else if(n1->type == name_str && n2->type == name_str)
217 if(n1->name.s_name->type == str_char
218 && n2->name.s_name->type == str_char)
220 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
222 else if(n1->name.s_name->type == str_unicode
223 && n2->name.s_name->type == str_unicode)
225 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
227 else
229 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
232 else if(n1->type == name_ord && n2->type == name_str)
233 return 1;
234 else if(n1->type == name_str && n2->type == name_ord)
235 return -1;
236 else
237 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
238 n1->type, n2->type);
240 return 0; /* Keep the compiler happy */
243 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
245 const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
246 string_t *ret = xmalloc(sizeof(*ret));
247 int res;
249 if (!codepage && str->type != type)
250 parser_error( "Current language is Unicode only, cannot convert string" );
252 if((str->type == str_char) && (type == str_unicode))
254 ret->type = str_unicode;
255 ret->size = cptable ? wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 )
256 : wine_utf8_mbstowcs( 0, str->str.cstr, str->size, NULL, 0 );
257 ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
258 if (cptable)
259 res = wine_cp_mbstowcs( cptable, MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
260 ret->str.wstr, ret->size );
261 else
262 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
263 ret->str.wstr, ret->size );
264 if (res == -2)
265 parser_error( "Invalid character in string '%.*s' for codepage %u\n",
266 str->size, str->str.cstr, codepage );
267 ret->str.wstr[ret->size] = 0;
269 else if((str->type == str_unicode) && (type == str_char))
271 ret->type = str_char;
272 ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL )
273 : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 );
274 ret->str.cstr = xmalloc( ret->size + 1 );
275 if (cptable)
276 wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL );
277 else
278 wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size );
279 ret->str.cstr[ret->size] = 0;
281 else if(str->type == str_unicode)
283 ret->type = str_unicode;
284 ret->size = str->size;
285 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
286 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
287 ret->str.wstr[ret->size] = 0;
289 else /* str->type == str_char */
291 ret->type = str_char;
292 ret->size = str->size;
293 ret->str.cstr = xmalloc( ret->size + 1 );
294 memcpy( ret->str.cstr, str->str.cstr, ret->size );
295 ret->str.cstr[ret->size] = 0;
297 return ret;
301 void free_string(string_t *str)
303 if (str->type == str_unicode) free( str->str.wstr );
304 else free( str->str.cstr );
305 free( str );
309 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
311 int ok;
312 string_t *teststr = convert_string( str_w, str_char, codepage );
314 ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
316 if (!ok)
318 int i;
320 fprintf( stderr, "Source: %s", str_a->str.cstr );
321 for (i = 0; i < str_a->size; i++)
322 fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
323 fprintf( stderr, "\nUnicode: " );
324 for (i = 0; i < str_w->size; i++)
325 fprintf( stderr, " %04x", str_w->str.wstr[i] );
326 fprintf( stderr, "\nBack: %s", teststr->str.cstr );
327 for (i = 0; i < teststr->size; i++)
328 fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
329 fprintf( stderr, "\n" );
331 free_string( teststr );
332 return ok;
336 struct lang2cp
338 unsigned short lang;
339 unsigned short sublang;
340 unsigned int cp;
341 } lang2cp_t;
343 /* language to codepage conversion table */
344 /* specific sublanguages need only be specified if their codepage */
345 /* differs from the default (SUBLANG_NEUTRAL) */
346 static const struct lang2cp lang2cps[] =
348 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
349 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
350 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
351 { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
352 { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
353 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
354 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
355 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
356 #ifdef LANG_BRETON
357 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
358 #endif /* LANG_BRETON */
359 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
360 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
361 { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
362 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
363 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
364 #ifdef LANG_CORNISH
365 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
366 #endif /* LANG_CORNISH */
367 { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
368 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
369 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
370 { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
371 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
372 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
373 #ifdef LANG_ESPERANTO
374 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
375 #endif /* LANG_ESPERANTO */
376 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
377 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
378 { LANG_FARSI, SUBLANG_NEUTRAL, 1256 },
379 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
380 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
381 #ifdef LANG_GAELIC
382 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
383 #endif /* LANG_GAELIC */
384 { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
385 { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
386 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
387 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
388 { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
389 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
390 { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
391 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
392 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
393 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
394 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
395 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
396 { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
397 { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
398 { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
399 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
400 { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
401 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
402 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
403 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
404 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
405 { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
406 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
407 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
408 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
409 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
410 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
411 { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
412 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
413 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
414 { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
415 { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
416 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
417 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
418 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
419 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
420 { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
421 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
422 { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
423 { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
424 { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
425 { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
426 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
427 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
428 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
429 { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
430 { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
431 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
432 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 }
433 #ifdef LANG_WALON
434 , { LANG_WALON, SUBLANG_NEUTRAL, 1252 }
435 #endif /* LANG_WALON */
436 #ifdef LANG_WELSH
437 , { LANG_WELSH, SUBLANG_NEUTRAL, 1252 }
438 #endif /* LANG_WELSH */
441 int get_language_codepage( unsigned short lang, unsigned short sublang )
443 unsigned int i;
444 int cp = -1, defcp = -1;
446 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
448 if (lang2cps[i].lang != lang) continue;
449 if (lang2cps[i].sublang == sublang)
451 cp = lang2cps[i].cp;
452 break;
454 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
457 if (cp == -1) cp = defcp;
458 assert( cp <= 0 || wine_cp_get_table(cp) );
459 return cp;