push 98299ad1251baa2727aab065c47e9d935978c732
[wine/hacks.git] / tools / wrc / utils.c
blobeb079ee5da4da11ad5376d1bc796cf4cf7f8afb6
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 <ctype.h>
31 #include "wine/unicode.h"
32 #include "wrc.h"
33 #include "utils.h"
34 #include "parser.h"
36 /* #define WANT_NEAR_INDICATION */
38 #ifdef WANT_NEAR_INDICATION
39 void make_print(char *str)
41 while(*str)
43 if(!isprint(*str))
44 *str = ' ';
45 str++;
48 #endif
50 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
52 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
53 vfprintf(stderr, s, ap);
54 #ifdef WANT_NEAR_INDICATION
56 char *cpy;
57 if(n)
59 cpy = xstrdup(n);
60 make_print(cpy);
61 fprintf(stderr, " near '%s'", cpy);
62 free(cpy);
65 #endif
69 int parser_error(const char *s, ...)
71 va_list ap;
72 va_start(ap, s);
73 generic_msg(s, "Error", parser_text, ap);
74 fputc( '\n', stderr );
75 va_end(ap);
76 exit(1);
77 return 1;
80 int parser_warning(const char *s, ...)
82 va_list ap;
83 va_start(ap, s);
84 generic_msg(s, "Warning", parser_text, ap);
85 va_end(ap);
86 return 0;
89 void internal_error(const char *file, int line, const char *s, ...)
91 va_list ap;
92 va_start(ap, s);
93 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
94 vfprintf(stderr, s, ap);
95 va_end(ap);
96 exit(3);
99 void error(const char *s, ...)
101 va_list ap;
102 va_start(ap, s);
103 fprintf(stderr, "Error: ");
104 vfprintf(stderr, s, ap);
105 va_end(ap);
106 exit(2);
109 void warning(const char *s, ...)
111 va_list ap;
112 va_start(ap, s);
113 fprintf(stderr, "Warning: ");
114 vfprintf(stderr, s, ap);
115 va_end(ap);
118 void chat(const char *s, ...)
120 if(debuglevel & DEBUGLEVEL_CHAT)
122 va_list ap;
123 va_start(ap, s);
124 fprintf(stderr, "FYI: ");
125 vfprintf(stderr, s, ap);
126 va_end(ap);
130 char *dup_basename(const char *name, const char *ext)
132 int namelen;
133 int extlen = strlen(ext);
134 char *base;
135 char *slash;
137 if(!name)
138 name = "wrc.tab";
140 slash = strrchr(name, '/');
141 if (slash)
142 name = slash + 1;
144 namelen = strlen(name);
146 /* +4 for later extension and +1 for '\0' */
147 base = xmalloc(namelen +4 +1);
148 strcpy(base, name);
149 if(!strcasecmp(name + namelen-extlen, ext))
151 base[namelen - extlen] = '\0';
153 return base;
156 void *xmalloc(size_t size)
158 void *res;
160 assert(size > 0);
161 res = malloc(size);
162 if(res == NULL)
164 error("Virtual memory exhausted.\n");
166 memset(res, 0x55, size);
167 return res;
171 void *xrealloc(void *p, size_t size)
173 void *res;
175 assert(size > 0);
176 res = realloc(p, size);
177 if(res == NULL)
179 error("Virtual memory exhausted.\n");
181 return res;
184 char *xstrdup(const char *str)
186 char *s;
188 assert(str != NULL);
189 s = xmalloc(strlen(str)+1);
190 return strcpy(s, str);
195 *****************************************************************************
196 * Function : compare_name_id
197 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
198 * Input :
199 * Output :
200 * Description :
201 * Remarks :
202 *****************************************************************************
204 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
206 if(n1->type == name_ord && n2->type == name_ord)
208 return n1->name.i_name - n2->name.i_name;
210 else if(n1->type == name_str && n2->type == name_str)
212 if(n1->name.s_name->type == str_char
213 && n2->name.s_name->type == str_char)
215 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
217 else if(n1->name.s_name->type == str_unicode
218 && n2->name.s_name->type == str_unicode)
220 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
222 else
224 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type\n");
227 else if(n1->type == name_ord && n2->type == name_str)
228 return 1;
229 else if(n1->type == name_str && n2->type == name_ord)
230 return -1;
231 else
232 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)\n",
233 n1->type, n2->type);
235 return 0; /* Keep the compiler happy */
238 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
240 const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
241 string_t *ret = xmalloc(sizeof(*ret));
242 int res;
244 if (!codepage && str->type != type)
245 parser_error( "Current language is Unicode only, cannot convert string" );
247 if((str->type == str_char) && (type == str_unicode))
249 ret->type = str_unicode;
250 ret->size = cptable ? wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 )
251 : wine_utf8_mbstowcs( 0, str->str.cstr, str->size, NULL, 0 );
252 ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
253 if (cptable)
254 res = wine_cp_mbstowcs( cptable, MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
255 ret->str.wstr, ret->size );
256 else
257 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
258 ret->str.wstr, ret->size );
259 if (res == -2)
260 parser_error( "Invalid character in string '%.*s' for codepage %u",
261 str->size, str->str.cstr, codepage );
262 ret->str.wstr[ret->size] = 0;
264 else if((str->type == str_unicode) && (type == str_char))
266 ret->type = str_char;
267 ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL )
268 : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 );
269 ret->str.cstr = xmalloc( ret->size + 1 );
270 if (cptable)
271 wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL );
272 else
273 wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size );
274 ret->str.cstr[ret->size] = 0;
276 else if(str->type == str_unicode)
278 ret->type = str_unicode;
279 ret->size = str->size;
280 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
281 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
282 ret->str.wstr[ret->size] = 0;
284 else /* str->type == str_char */
286 ret->type = str_char;
287 ret->size = str->size;
288 ret->str.cstr = xmalloc( ret->size + 1 );
289 memcpy( ret->str.cstr, str->str.cstr, ret->size );
290 ret->str.cstr[ret->size] = 0;
292 return ret;
296 void free_string(string_t *str)
298 if (str->type == str_unicode) free( str->str.wstr );
299 else free( str->str.cstr );
300 free( str );
304 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
306 int ok;
307 string_t *teststr = convert_string( str_w, str_char, codepage );
309 ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
311 if (!ok)
313 int i;
315 fprintf( stderr, "Source: %s", str_a->str.cstr );
316 for (i = 0; i < str_a->size; i++)
317 fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
318 fprintf( stderr, "\nUnicode: " );
319 for (i = 0; i < str_w->size; i++)
320 fprintf( stderr, " %04x", str_w->str.wstr[i] );
321 fprintf( stderr, "\nBack: %s", teststr->str.cstr );
322 for (i = 0; i < teststr->size; i++)
323 fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
324 fprintf( stderr, "\n" );
326 free_string( teststr );
327 return ok;
331 struct lang2cp
333 unsigned short lang;
334 unsigned short sublang;
335 unsigned int cp;
336 } lang2cp_t;
338 /* language to codepage conversion table */
339 /* specific sublanguages need only be specified if their codepage */
340 /* differs from the default (SUBLANG_NEUTRAL) */
341 static const struct lang2cp lang2cps[] =
343 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
344 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
345 { LANG_ALSATIAN, SUBLANG_NEUTRAL, 1252 },
346 { LANG_AMHARIC, SUBLANG_NEUTRAL, 0 },
347 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
348 { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
349 { LANG_ASSAMESE, SUBLANG_NEUTRAL, 0 },
350 { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
351 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
352 { LANG_BASHKIR, SUBLANG_NEUTRAL, 1251 },
353 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
354 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
355 { LANG_BENGALI, SUBLANG_NEUTRAL, 0 },
356 { LANG_BOSNIAN, SUBLANG_NEUTRAL, 1250 },
357 { LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, 1251 },
358 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
359 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
360 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
361 { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
362 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
363 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
364 #ifdef LANG_CORNISH
365 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
366 #endif /* LANG_CORNISH */
367 { LANG_CORSICAN, SUBLANG_NEUTRAL, 1252 },
368 { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
369 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
370 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
371 { LANG_DARI, SUBLANG_NEUTRAL, 1256 },
372 { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
373 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
374 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
375 #ifdef LANG_ESPERANTO
376 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
377 #endif /* LANG_ESPERANTO */
378 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
379 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
380 { LANG_FILIPINO, SUBLANG_NEUTRAL, 1252 },
381 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
382 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
383 { LANG_FRISIAN, SUBLANG_NEUTRAL, 1252 },
384 #ifdef LANG_GAELIC
385 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
386 #endif /* LANG_GAELIC */
387 { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
388 { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
389 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
390 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
391 { LANG_GREENLANDIC, SUBLANG_NEUTRAL, 1252 },
392 { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
393 { LANG_HAUSA, SUBLANG_NEUTRAL, 1252 },
394 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
395 { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
396 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
397 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
398 { LANG_IGBO, SUBLANG_NEUTRAL, 1252 },
399 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
400 { LANG_INUKTITUT, SUBLANG_NEUTRAL, 0 },
401 { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA_LATIN, 0 },
402 { LANG_IRISH, SUBLANG_NEUTRAL, 1252 },
403 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
404 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
405 { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
406 { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
407 { LANG_KHMER, SUBLANG_NEUTRAL, 0 },
408 { LANG_KICHE, SUBLANG_NEUTRAL, 1252 },
409 { LANG_KINYARWANDA, SUBLANG_NEUTRAL, 1252 },
410 { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
411 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
412 { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
413 { LANG_LAO, SUBLANG_NEUTRAL, 0 },
414 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
415 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
416 { LANG_LOWER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
417 { LANG_LUXEMBOURGISH, SUBLANG_NEUTRAL, 1252 },
418 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
419 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
420 { LANG_MALAYALAM, SUBLANG_NEUTRAL, 0 },
421 { LANG_MALTESE, SUBLANG_NEUTRAL, 0 },
422 { LANG_MAORI, SUBLANG_NEUTRAL, 0 },
423 { LANG_MAPUDUNGUN, SUBLANG_NEUTRAL, 1252 },
424 { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
425 { LANG_MOHAWK, SUBLANG_NEUTRAL, 1252 },
426 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
427 { LANG_NEPALI, SUBLANG_NEUTRAL, 0 },
428 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
429 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
430 { LANG_OCCITAN, SUBLANG_NEUTRAL, 1252 },
431 { LANG_ORIYA, SUBLANG_NEUTRAL, 0 },
432 { LANG_PASHTO, SUBLANG_NEUTRAL, 0 },
433 { LANG_PERSIAN, SUBLANG_NEUTRAL, 1256 },
434 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
435 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
436 { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
437 { LANG_QUECHUA, SUBLANG_NEUTRAL, 1252 },
438 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
439 { LANG_ROMANSH, SUBLANG_NEUTRAL, 1252 },
440 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
441 { LANG_SAMI, SUBLANG_NEUTRAL, 1252 },
442 { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
443 { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
444 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
445 { LANG_SINHALESE, SUBLANG_NEUTRAL, 0 },
446 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
447 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
448 { LANG_SOTHO, SUBLANG_NEUTRAL, 1252 },
449 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
450 { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
451 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
452 { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
453 { LANG_TAJIK, SUBLANG_NEUTRAL, 1251 },
454 { LANG_TAMAZIGHT, SUBLANG_NEUTRAL, 1252 },
455 { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
456 { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
457 { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
458 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
459 { LANG_TIBETAN, SUBLANG_NEUTRAL, 0 },
460 { LANG_TSWANA, SUBLANG_NEUTRAL, 1252 },
461 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
462 { LANG_TURKMEN, SUBLANG_NEUTRAL, 1250 },
463 { LANG_UIGHUR, SUBLANG_NEUTRAL, 1256 },
464 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
465 { LANG_UPPER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
466 { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
467 { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
468 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
469 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
470 #ifdef LANG_WALON
471 { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
472 #endif /* LANG_WALON */
473 { LANG_WELSH, SUBLANG_NEUTRAL, 1252 },
474 { LANG_WOLOF, SUBLANG_NEUTRAL, 1252 },
475 { LANG_XHOSA, SUBLANG_NEUTRAL, 1252 },
476 { LANG_YAKUT, SUBLANG_NEUTRAL, 1251 },
477 { LANG_YI, SUBLANG_NEUTRAL, 0 },
478 { LANG_YORUBA, SUBLANG_NEUTRAL, 1252 },
479 { LANG_ZULU, SUBLANG_NEUTRAL, 1252 }
482 int get_language_codepage( unsigned short lang, unsigned short sublang )
484 unsigned int i;
485 int cp = -1, defcp = -1;
487 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
489 if (lang2cps[i].lang != lang) continue;
490 if (lang2cps[i].sublang == sublang)
492 cp = lang2cps[i].cp;
493 break;
495 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
498 if (cp == -1) cp = defcp;
499 assert( cp <= 0 || wine_cp_get_table(cp) );
500 return cp;