shell32: Return NULL-terminated list of arguments in CommandLineToArgvW.
[wine/wine-gecko.git] / tools / wrc / utils.c
blob4dc3364f782edb95d3d14b0410f6e50592bb8b4a
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 "wrc.h"
32 #include "utils.h"
33 #include "parser.h"
35 /* #define WANT_NEAR_INDICATION */
37 #ifdef WANT_NEAR_INDICATION
38 void make_print(char *str)
40 while(*str)
42 if(!isprint(*str))
43 *str = ' ';
44 str++;
47 #endif
49 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
51 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
52 vfprintf(stderr, s, ap);
53 #ifdef WANT_NEAR_INDICATION
55 char *cpy;
56 if(n)
58 cpy = xstrdup(n);
59 make_print(cpy);
60 fprintf(stderr, " near '%s'", cpy);
61 free(cpy);
64 #endif
68 int parser_error(const char *s, ...)
70 va_list ap;
71 va_start(ap, s);
72 generic_msg(s, "Error", parser_text, ap);
73 fputc( '\n', stderr );
74 va_end(ap);
75 exit(1);
76 return 1;
79 int parser_warning(const char *s, ...)
81 va_list ap;
82 va_start(ap, s);
83 generic_msg(s, "Warning", parser_text, ap);
84 va_end(ap);
85 return 0;
88 void internal_error(const char *file, int line, const char *s, ...)
90 va_list ap;
91 va_start(ap, s);
92 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
93 vfprintf(stderr, s, ap);
94 va_end(ap);
95 exit(3);
98 void fatal_perror( const char *msg, ... )
100 va_list valist;
101 va_start( valist, msg );
102 fprintf(stderr, "Error: ");
103 vfprintf( stderr, msg, valist );
104 perror( " " );
105 va_end( valist );
106 exit(2);
109 void error(const char *s, ...)
111 va_list ap;
112 va_start(ap, s);
113 fprintf(stderr, "Error: ");
114 vfprintf(stderr, s, ap);
115 va_end(ap);
116 exit(2);
119 void warning(const char *s, ...)
121 va_list ap;
122 va_start(ap, s);
123 fprintf(stderr, "Warning: ");
124 vfprintf(stderr, s, ap);
125 va_end(ap);
128 void chat(const char *s, ...)
130 if(debuglevel & DEBUGLEVEL_CHAT)
132 va_list ap;
133 va_start(ap, s);
134 fprintf(stderr, "FYI: ");
135 vfprintf(stderr, s, ap);
136 va_end(ap);
140 char *dup_basename(const char *name, const char *ext)
142 int namelen;
143 int extlen = strlen(ext);
144 char *base;
145 char *slash;
147 if(!name)
148 name = "wrc.tab";
150 slash = strrchr(name, '/');
151 if (slash)
152 name = slash + 1;
154 namelen = strlen(name);
156 /* +4 for later extension and +1 for '\0' */
157 base = xmalloc(namelen +4 +1);
158 strcpy(base, name);
159 if(!strcasecmp(name + namelen-extlen, ext))
161 base[namelen - extlen] = '\0';
163 return base;
166 void *xmalloc(size_t size)
168 void *res;
170 assert(size > 0);
171 res = malloc(size);
172 if(res == NULL)
174 error("Virtual memory exhausted.\n");
176 memset(res, 0x55, size);
177 return res;
181 void *xrealloc(void *p, size_t size)
183 void *res;
185 assert(size > 0);
186 res = realloc(p, size);
187 if(res == NULL)
189 error("Virtual memory exhausted.\n");
191 return res;
194 char *strmake( const char* fmt, ... )
196 int n;
197 size_t size = 100;
198 va_list ap;
200 for (;;)
202 char *p = xmalloc( size );
203 va_start( ap, fmt );
204 n = vsnprintf( p, size, fmt, ap );
205 va_end( ap );
206 if (n == -1) size *= 2;
207 else if ((size_t)n >= size) size = n + 1;
208 else return p;
209 free( p );
213 char *xstrdup(const char *str)
215 char *s;
217 assert(str != NULL);
218 s = xmalloc(strlen(str)+1);
219 return strcpy(s, str);
224 *****************************************************************************
225 * Function : compare_name_id
226 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
227 * Input :
228 * Output :
229 * Description :
230 * Remarks :
231 *****************************************************************************
233 int compare_name_id(const name_id_t *n1, const name_id_t *n2)
235 if(n1->type == name_ord && n2->type == name_ord)
237 return n1->name.i_name - n2->name.i_name;
239 else if(n1->type == name_str && n2->type == name_str)
241 if(n1->name.s_name->type == str_char
242 && n2->name.s_name->type == str_char)
244 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
246 else if(n1->name.s_name->type == str_unicode
247 && n2->name.s_name->type == str_unicode)
249 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
251 else
253 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type\n");
256 else if(n1->type == name_ord && n2->type == name_str)
257 return 1;
258 else if(n1->type == name_str && n2->type == name_ord)
259 return -1;
260 else
261 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)\n",
262 n1->type, n2->type);
264 return 0; /* Keep the compiler happy */
267 string_t *convert_string(const string_t *str, enum str_e type, int codepage)
269 const union cptable *cptable = codepage ? wine_cp_get_table( codepage ) : NULL;
270 string_t *ret = xmalloc(sizeof(*ret));
271 int res;
273 ret->loc = str->loc;
275 if (!codepage && str->type != type)
276 parser_error( "Current language is Unicode only, cannot convert string" );
278 if((str->type == str_char) && (type == str_unicode))
280 ret->type = str_unicode;
281 ret->size = cptable ? wine_cp_mbstowcs( cptable, 0, str->str.cstr, str->size, NULL, 0 )
282 : wine_utf8_mbstowcs( 0, str->str.cstr, str->size, NULL, 0 );
283 ret->str.wstr = xmalloc( (ret->size+1) * sizeof(WCHAR) );
284 if (cptable)
285 res = wine_cp_mbstowcs( cptable, MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
286 ret->str.wstr, ret->size );
287 else
288 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size,
289 ret->str.wstr, ret->size );
290 if (res == -2)
291 parser_error( "Invalid character in string '%.*s' for codepage %u",
292 str->size, str->str.cstr, codepage );
293 ret->str.wstr[ret->size] = 0;
295 else if((str->type == str_unicode) && (type == str_char))
297 ret->type = str_char;
298 ret->size = cptable ? wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, NULL, 0, NULL, NULL )
299 : wine_utf8_wcstombs( 0, str->str.wstr, str->size, NULL, 0 );
300 ret->str.cstr = xmalloc( ret->size + 1 );
301 if (cptable)
302 wine_cp_wcstombs( cptable, 0, str->str.wstr, str->size, ret->str.cstr, ret->size, NULL, NULL );
303 else
304 wine_utf8_wcstombs( 0, str->str.wstr, str->size, ret->str.cstr, ret->size );
305 ret->str.cstr[ret->size] = 0;
307 else if(str->type == str_unicode)
309 ret->type = str_unicode;
310 ret->size = str->size;
311 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
312 memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
313 ret->str.wstr[ret->size] = 0;
315 else /* str->type == str_char */
317 ret->type = str_char;
318 ret->size = str->size;
319 ret->str.cstr = xmalloc( ret->size + 1 );
320 memcpy( ret->str.cstr, str->str.cstr, ret->size );
321 ret->str.cstr[ret->size] = 0;
323 return ret;
327 void free_string(string_t *str)
329 if (str->type == str_unicode) free( str->str.wstr );
330 else free( str->str.cstr );
331 free( str );
334 /* check if the string is valid utf8 despite a different codepage being in use */
335 int check_valid_utf8( const string_t *str, int codepage )
337 unsigned int i;
339 if (!check_utf8) return 0;
340 if (!codepage) return 0;
341 if (!wine_cp_get_table( codepage )) return 0;
343 for (i = 0; i < str->size; i++)
345 if ((unsigned char)str->str.cstr[i] >= 0xf5) goto done;
346 if ((unsigned char)str->str.cstr[i] >= 0xc2) break;
347 if ((unsigned char)str->str.cstr[i] >= 0x80) goto done;
349 if (i == str->size) return 0; /* no 8-bit chars at all */
351 if (wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, str->str.cstr, str->size, NULL, 0 ) >= 0) return 1;
353 done:
354 check_utf8 = 0; /* at least one 8-bit non-utf8 string found, stop checking */
355 return 0;
358 int check_unicode_conversion( const string_t *str_a, const string_t *str_w, int codepage )
360 int ok;
361 string_t *teststr = convert_string( str_w, str_char, codepage );
363 ok = (teststr->size == str_a->size && !memcmp( teststr->str.cstr, str_a->str.cstr, str_a->size ));
365 if (!ok)
367 int i;
369 fprintf( stderr, "Source: %s", str_a->str.cstr );
370 for (i = 0; i < str_a->size; i++)
371 fprintf( stderr, " %02x", (unsigned char)str_a->str.cstr[i] );
372 fprintf( stderr, "\nUnicode: " );
373 for (i = 0; i < str_w->size; i++)
374 fprintf( stderr, " %04x", str_w->str.wstr[i] );
375 fprintf( stderr, "\nBack: %s", teststr->str.cstr );
376 for (i = 0; i < teststr->size; i++)
377 fprintf( stderr, " %02x", (unsigned char)teststr->str.cstr[i] );
378 fprintf( stderr, "\n" );
380 free_string( teststr );
381 return ok;
385 struct lang2cp
387 unsigned short lang;
388 unsigned short sublang;
389 unsigned int cp;
392 /* language to codepage conversion table */
393 /* specific sublanguages need only be specified if their codepage */
394 /* differs from the default (SUBLANG_NEUTRAL) */
395 static const struct lang2cp lang2cps[] =
397 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
398 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
399 { LANG_ALSATIAN, SUBLANG_NEUTRAL, 1252 },
400 { LANG_AMHARIC, SUBLANG_NEUTRAL, 0 },
401 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
402 { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
403 { LANG_ASSAMESE, SUBLANG_NEUTRAL, 0 },
404 { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
405 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
406 { LANG_BASHKIR, SUBLANG_NEUTRAL, 1251 },
407 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
408 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
409 { LANG_BENGALI, SUBLANG_NEUTRAL, 0 },
410 { LANG_BOSNIAN, SUBLANG_NEUTRAL, 1250 },
411 { LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, 1251 },
412 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
413 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
414 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
415 { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
416 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
417 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
418 #ifdef LANG_CORNISH
419 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
420 #endif /* LANG_CORNISH */
421 { LANG_CORSICAN, SUBLANG_NEUTRAL, 1252 },
422 { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
423 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
424 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
425 { LANG_DARI, SUBLANG_NEUTRAL, 1256 },
426 { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
427 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
428 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
429 #ifdef LANG_ESPERANTO
430 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
431 #endif /* LANG_ESPERANTO */
432 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
433 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
434 { LANG_FILIPINO, SUBLANG_NEUTRAL, 1252 },
435 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
436 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
437 { LANG_FRISIAN, SUBLANG_NEUTRAL, 1252 },
438 #ifdef LANG_MANX_GAELIC
439 { LANG_MANX_GAELIC, SUBLANG_NEUTRAL, 1252 },
440 #endif /* LANG_MANX_GAELIC */
441 { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
442 { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
443 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
444 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
445 { LANG_GREENLANDIC, SUBLANG_NEUTRAL, 1252 },
446 { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
447 { LANG_HAUSA, SUBLANG_NEUTRAL, 1252 },
448 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
449 { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
450 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
451 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
452 { LANG_IGBO, SUBLANG_NEUTRAL, 1252 },
453 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
454 { LANG_INUKTITUT, SUBLANG_NEUTRAL, 0 },
455 { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA_LATIN, 0 },
456 { LANG_IRISH, SUBLANG_NEUTRAL, 1252 },
457 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
458 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
459 { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
460 { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
461 { LANG_KHMER, SUBLANG_NEUTRAL, 0 },
462 { LANG_KICHE, SUBLANG_NEUTRAL, 1252 },
463 { LANG_KINYARWANDA, SUBLANG_NEUTRAL, 1252 },
464 { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
465 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
466 { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
467 { LANG_LAO, SUBLANG_NEUTRAL, 0 },
468 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
469 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
470 { LANG_LOWER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
471 { LANG_LUXEMBOURGISH, SUBLANG_NEUTRAL, 1252 },
472 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
473 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
474 { LANG_MALAYALAM, SUBLANG_NEUTRAL, 0 },
475 { LANG_MALTESE, SUBLANG_NEUTRAL, 0 },
476 { LANG_MAORI, SUBLANG_NEUTRAL, 0 },
477 { LANG_MAPUDUNGUN, SUBLANG_NEUTRAL, 1252 },
478 { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
479 { LANG_MOHAWK, SUBLANG_NEUTRAL, 1252 },
480 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
481 { LANG_NEPALI, SUBLANG_NEUTRAL, 0 },
482 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
483 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
484 { LANG_OCCITAN, SUBLANG_NEUTRAL, 1252 },
485 { LANG_ORIYA, SUBLANG_NEUTRAL, 0 },
486 { LANG_PASHTO, SUBLANG_NEUTRAL, 0 },
487 { LANG_PERSIAN, SUBLANG_NEUTRAL, 1256 },
488 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
489 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
490 { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
491 { LANG_QUECHUA, SUBLANG_NEUTRAL, 1252 },
492 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
493 { LANG_ROMANSH, SUBLANG_NEUTRAL, 1252 },
494 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
495 { LANG_SAMI, SUBLANG_NEUTRAL, 1252 },
496 { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
497 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, 1252 },
498 { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
499 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
500 { LANG_SINHALESE, SUBLANG_NEUTRAL, 0 },
501 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
502 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
503 { LANG_SOTHO, SUBLANG_NEUTRAL, 1252 },
504 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
505 { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
506 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
507 { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
508 { LANG_TAJIK, SUBLANG_NEUTRAL, 1251 },
509 { LANG_TAMAZIGHT, SUBLANG_NEUTRAL, 1252 },
510 { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
511 { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
512 { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
513 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
514 { LANG_TIBETAN, SUBLANG_NEUTRAL, 0 },
515 { LANG_TSWANA, SUBLANG_NEUTRAL, 1252 },
516 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
517 { LANG_TURKMEN, SUBLANG_NEUTRAL, 1250 },
518 { LANG_UIGHUR, SUBLANG_NEUTRAL, 1256 },
519 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
520 { LANG_UPPER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
521 { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
522 { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
523 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
524 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
525 #ifdef LANG_WALON
526 { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
527 #endif /* LANG_WALON */
528 { LANG_WELSH, SUBLANG_NEUTRAL, 1252 },
529 { LANG_WOLOF, SUBLANG_NEUTRAL, 1252 },
530 { LANG_XHOSA, SUBLANG_NEUTRAL, 1252 },
531 { LANG_YAKUT, SUBLANG_NEUTRAL, 1251 },
532 { LANG_YI, SUBLANG_NEUTRAL, 0 },
533 { LANG_YORUBA, SUBLANG_NEUTRAL, 1252 },
534 { LANG_ZULU, SUBLANG_NEUTRAL, 1252 }
537 int get_language_codepage( unsigned short lang, unsigned short sublang )
539 unsigned int i;
540 int cp = -1, defcp = -1;
542 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
544 if (lang2cps[i].lang != lang) continue;
545 if (lang2cps[i].sublang == sublang)
547 cp = lang2cps[i].cp;
548 break;
550 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
553 if (cp == -1) cp = defcp;
554 assert( cp <= 0 || wine_cp_get_table(cp) );
555 return cp;