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
22 #include "wine/port.h"
31 #include "wine/unicode.h"
36 /* #define WANT_NEAR_INDICATION */
38 #ifdef WANT_NEAR_INDICATION
39 void make_print(char *str
)
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
61 fprintf(stderr
, " near '%s'", cpy
);
69 int parser_error(const char *s
, ...)
73 generic_msg(s
, "Error", parser_text
, ap
);
74 fputc( '\n', stderr
);
80 int parser_warning(const char *s
, ...)
84 generic_msg(s
, "Warning", parser_text
, ap
);
89 void internal_error(const char *file
, int line
, const char *s
, ...)
93 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
94 vfprintf(stderr
, s
, ap
);
99 void fatal_perror( const char *msg
, ... )
102 va_start( valist
, msg
);
103 fprintf(stderr
, "Error: ");
104 vfprintf( stderr
, msg
, valist
);
110 void error(const char *s
, ...)
114 fprintf(stderr
, "Error: ");
115 vfprintf(stderr
, s
, ap
);
120 void warning(const char *s
, ...)
124 fprintf(stderr
, "Warning: ");
125 vfprintf(stderr
, s
, ap
);
129 void chat(const char *s
, ...)
131 if(debuglevel
& DEBUGLEVEL_CHAT
)
135 fprintf(stderr
, "FYI: ");
136 vfprintf(stderr
, s
, ap
);
141 char *dup_basename(const char *name
, const char *ext
)
144 int extlen
= strlen(ext
);
151 slash
= strrchr(name
, '/');
155 namelen
= strlen(name
);
157 /* +4 for later extension and +1 for '\0' */
158 base
= xmalloc(namelen
+4 +1);
160 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
162 base
[namelen
- extlen
] = '\0';
167 void *xmalloc(size_t size
)
175 error("Virtual memory exhausted.\n");
177 memset(res
, 0x55, size
);
182 void *xrealloc(void *p
, size_t size
)
187 res
= realloc(p
, size
);
190 error("Virtual memory exhausted.\n");
195 char *strmake( const char* fmt
, ... )
203 char *p
= xmalloc( size
);
205 n
= vsnprintf( p
, size
, fmt
, ap
);
207 if (n
== -1) size
*= 2;
208 else if ((size_t)n
>= size
) size
= n
+ 1;
214 char *xstrdup(const char *str
)
219 s
= xmalloc(strlen(str
)+1);
220 return strcpy(s
, str
);
225 *****************************************************************************
226 * Function : compare_name_id
227 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
232 *****************************************************************************
234 int compare_name_id(const name_id_t
*n1
, const name_id_t
*n2
)
236 if(n1
->type
== name_ord
&& n2
->type
== name_ord
)
238 return n1
->name
.i_name
- n2
->name
.i_name
;
240 else if(n1
->type
== name_str
&& n2
->type
== name_str
)
242 if(n1
->name
.s_name
->type
== str_char
243 && n2
->name
.s_name
->type
== str_char
)
245 return strcasecmp(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.cstr
);
247 else if(n1
->name
.s_name
->type
== str_unicode
248 && n2
->name
.s_name
->type
== str_unicode
)
250 return strcmpiW(n1
->name
.s_name
->str
.wstr
, n2
->name
.s_name
->str
.wstr
);
254 internal_error(__FILE__
, __LINE__
, "Can't yet compare strings of mixed type\n");
257 else if(n1
->type
== name_ord
&& n2
->type
== name_str
)
259 else if(n1
->type
== name_str
&& n2
->type
== name_ord
)
262 internal_error(__FILE__
, __LINE__
, "Comparing name-ids with unknown types (%d, %d)\n",
265 return 0; /* Keep the compiler happy */
268 string_t
*convert_string(const string_t
*str
, enum str_e type
, int codepage
)
270 const union cptable
*cptable
= codepage
? wine_cp_get_table( codepage
) : NULL
;
271 string_t
*ret
= xmalloc(sizeof(*ret
));
276 if (!codepage
&& str
->type
!= type
)
277 parser_error( "Current language is Unicode only, cannot convert string" );
279 if((str
->type
== str_char
) && (type
== str_unicode
))
281 ret
->type
= str_unicode
;
282 ret
->size
= cptable
? wine_cp_mbstowcs( cptable
, 0, str
->str
.cstr
, str
->size
, NULL
, 0 )
283 : wine_utf8_mbstowcs( 0, str
->str
.cstr
, str
->size
, NULL
, 0 );
284 ret
->str
.wstr
= xmalloc( (ret
->size
+1) * sizeof(WCHAR
) );
286 res
= wine_cp_mbstowcs( cptable
, MB_ERR_INVALID_CHARS
, str
->str
.cstr
, str
->size
,
287 ret
->str
.wstr
, ret
->size
);
289 res
= wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS
, str
->str
.cstr
, str
->size
,
290 ret
->str
.wstr
, ret
->size
);
292 parser_error( "Invalid character in string '%.*s' for codepage %u",
293 str
->size
, str
->str
.cstr
, codepage
);
294 ret
->str
.wstr
[ret
->size
] = 0;
296 else if((str
->type
== str_unicode
) && (type
== str_char
))
298 ret
->type
= str_char
;
299 ret
->size
= cptable
? wine_cp_wcstombs( cptable
, 0, str
->str
.wstr
, str
->size
, NULL
, 0, NULL
, NULL
)
300 : wine_utf8_wcstombs( 0, str
->str
.wstr
, str
->size
, NULL
, 0 );
301 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
303 wine_cp_wcstombs( cptable
, 0, str
->str
.wstr
, str
->size
, ret
->str
.cstr
, ret
->size
, NULL
, NULL
);
305 wine_utf8_wcstombs( 0, str
->str
.wstr
, str
->size
, ret
->str
.cstr
, ret
->size
);
306 ret
->str
.cstr
[ret
->size
] = 0;
308 else if(str
->type
== str_unicode
)
310 ret
->type
= str_unicode
;
311 ret
->size
= str
->size
;
312 ret
->str
.wstr
= xmalloc(sizeof(WCHAR
)*(ret
->size
+1));
313 memcpy( ret
->str
.wstr
, str
->str
.wstr
, ret
->size
* sizeof(WCHAR
) );
314 ret
->str
.wstr
[ret
->size
] = 0;
316 else /* str->type == str_char */
318 ret
->type
= str_char
;
319 ret
->size
= str
->size
;
320 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
321 memcpy( ret
->str
.cstr
, str
->str
.cstr
, ret
->size
);
322 ret
->str
.cstr
[ret
->size
] = 0;
328 void free_string(string_t
*str
)
330 if (str
->type
== str_unicode
) free( str
->str
.wstr
);
331 else free( str
->str
.cstr
);
335 /* check if the string is valid utf8 despite a different codepage being in use */
336 int check_valid_utf8( const string_t
*str
, int codepage
)
340 if (!check_utf8
) return 0;
341 if (!codepage
) return 0;
342 if (!wine_cp_get_table( codepage
)) return 0;
344 for (i
= 0; i
< str
->size
; i
++)
346 if ((unsigned char)str
->str
.cstr
[i
] >= 0xf5) goto done
;
347 if ((unsigned char)str
->str
.cstr
[i
] >= 0xc2) break;
348 if ((unsigned char)str
->str
.cstr
[i
] >= 0x80) goto done
;
350 if (i
== str
->size
) return 0; /* no 8-bit chars at all */
352 if (wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS
, str
->str
.cstr
, str
->size
, NULL
, 0 ) >= 0) return 1;
355 check_utf8
= 0; /* at least one 8-bit non-utf8 string found, stop checking */
359 int check_unicode_conversion( const string_t
*str_a
, const string_t
*str_w
, int codepage
)
362 string_t
*teststr
= convert_string( str_w
, str_char
, codepage
);
364 ok
= (teststr
->size
== str_a
->size
&& !memcmp( teststr
->str
.cstr
, str_a
->str
.cstr
, str_a
->size
));
370 fprintf( stderr
, "Source: %s", str_a
->str
.cstr
);
371 for (i
= 0; i
< str_a
->size
; i
++)
372 fprintf( stderr
, " %02x", (unsigned char)str_a
->str
.cstr
[i
] );
373 fprintf( stderr
, "\nUnicode: " );
374 for (i
= 0; i
< str_w
->size
; i
++)
375 fprintf( stderr
, " %04x", str_w
->str
.wstr
[i
] );
376 fprintf( stderr
, "\nBack: %s", teststr
->str
.cstr
);
377 for (i
= 0; i
< teststr
->size
; i
++)
378 fprintf( stderr
, " %02x", (unsigned char)teststr
->str
.cstr
[i
] );
379 fprintf( stderr
, "\n" );
381 free_string( teststr
);
389 unsigned short sublang
;
393 /* language to codepage conversion table */
394 /* specific sublanguages need only be specified if their codepage */
395 /* differs from the default (SUBLANG_NEUTRAL) */
396 static const struct lang2cp lang2cps
[] =
398 { LANG_AFRIKAANS
, SUBLANG_NEUTRAL
, 1252 },
399 { LANG_ALBANIAN
, SUBLANG_NEUTRAL
, 1250 },
400 { LANG_ALSATIAN
, SUBLANG_NEUTRAL
, 1252 },
401 { LANG_AMHARIC
, SUBLANG_NEUTRAL
, 0 },
402 { LANG_ARABIC
, SUBLANG_NEUTRAL
, 1256 },
403 { LANG_ARMENIAN
, SUBLANG_NEUTRAL
, 0 },
404 { LANG_ASSAMESE
, SUBLANG_NEUTRAL
, 0 },
405 { LANG_AZERI
, SUBLANG_NEUTRAL
, 1254 },
406 { LANG_AZERI
, SUBLANG_AZERI_CYRILLIC
, 1251 },
407 { LANG_BASHKIR
, SUBLANG_NEUTRAL
, 1251 },
408 { LANG_BASQUE
, SUBLANG_NEUTRAL
, 1252 },
409 { LANG_BELARUSIAN
, SUBLANG_NEUTRAL
, 1251 },
410 { LANG_BENGALI
, SUBLANG_NEUTRAL
, 0 },
411 { LANG_BOSNIAN
, SUBLANG_NEUTRAL
, 1250 },
412 { LANG_BOSNIAN
, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC
, 1251 },
413 { LANG_BRETON
, SUBLANG_NEUTRAL
, 1252 },
414 { LANG_BULGARIAN
, SUBLANG_NEUTRAL
, 1251 },
415 { LANG_CATALAN
, SUBLANG_NEUTRAL
, 1252 },
416 { LANG_CHINESE
, SUBLANG_NEUTRAL
, 950 },
417 { LANG_CHINESE
, SUBLANG_CHINESE_SIMPLIFIED
, 936 },
418 { LANG_CHINESE
, SUBLANG_CHINESE_SINGAPORE
, 936 },
420 { LANG_CORNISH
, SUBLANG_NEUTRAL
, 1252 },
421 #endif /* LANG_CORNISH */
422 { LANG_CORSICAN
, SUBLANG_NEUTRAL
, 1252 },
423 { LANG_CROATIAN
, SUBLANG_NEUTRAL
, 1250 },
424 { LANG_CZECH
, SUBLANG_NEUTRAL
, 1250 },
425 { LANG_DANISH
, SUBLANG_NEUTRAL
, 1252 },
426 { LANG_DARI
, SUBLANG_NEUTRAL
, 1256 },
427 { LANG_DIVEHI
, SUBLANG_NEUTRAL
, 0 },
428 { LANG_DUTCH
, SUBLANG_NEUTRAL
, 1252 },
429 { LANG_ENGLISH
, SUBLANG_NEUTRAL
, 1252 },
430 #ifdef LANG_ESPERANTO
431 { LANG_ESPERANTO
, SUBLANG_NEUTRAL
, 1252 },
432 #endif /* LANG_ESPERANTO */
433 { LANG_ESTONIAN
, SUBLANG_NEUTRAL
, 1257 },
434 { LANG_FAEROESE
, SUBLANG_NEUTRAL
, 1252 },
435 { LANG_FILIPINO
, SUBLANG_NEUTRAL
, 1252 },
436 { LANG_FINNISH
, SUBLANG_NEUTRAL
, 1252 },
437 { LANG_FRENCH
, SUBLANG_NEUTRAL
, 1252 },
438 { LANG_FRISIAN
, SUBLANG_NEUTRAL
, 1252 },
440 { LANG_GAELIC
, SUBLANG_NEUTRAL
, 1252 },
441 #endif /* LANG_GAELIC */
442 { LANG_GALICIAN
, SUBLANG_NEUTRAL
, 1252 },
443 { LANG_GEORGIAN
, SUBLANG_NEUTRAL
, 0 },
444 { LANG_GERMAN
, SUBLANG_NEUTRAL
, 1252 },
445 { LANG_GREEK
, SUBLANG_NEUTRAL
, 1253 },
446 { LANG_GREENLANDIC
, SUBLANG_NEUTRAL
, 1252 },
447 { LANG_GUJARATI
, SUBLANG_NEUTRAL
, 0 },
448 { LANG_HAUSA
, SUBLANG_NEUTRAL
, 1252 },
449 { LANG_HEBREW
, SUBLANG_NEUTRAL
, 1255 },
450 { LANG_HINDI
, SUBLANG_NEUTRAL
, 0 },
451 { LANG_HUNGARIAN
, SUBLANG_NEUTRAL
, 1250 },
452 { LANG_ICELANDIC
, SUBLANG_NEUTRAL
, 1252 },
453 { LANG_IGBO
, SUBLANG_NEUTRAL
, 1252 },
454 { LANG_INDONESIAN
, SUBLANG_NEUTRAL
, 1252 },
455 { LANG_INUKTITUT
, SUBLANG_NEUTRAL
, 0 },
456 { LANG_INUKTITUT
, SUBLANG_INUKTITUT_CANADA_LATIN
, 0 },
457 { LANG_IRISH
, SUBLANG_NEUTRAL
, 1252 },
458 { LANG_ITALIAN
, SUBLANG_NEUTRAL
, 1252 },
459 { LANG_JAPANESE
, SUBLANG_NEUTRAL
, 932 },
460 { LANG_KANNADA
, SUBLANG_NEUTRAL
, 0 },
461 { LANG_KAZAK
, SUBLANG_NEUTRAL
, 1251 },
462 { LANG_KHMER
, SUBLANG_NEUTRAL
, 0 },
463 { LANG_KICHE
, SUBLANG_NEUTRAL
, 1252 },
464 { LANG_KINYARWANDA
, SUBLANG_NEUTRAL
, 1252 },
465 { LANG_KONKANI
, SUBLANG_NEUTRAL
, 0 },
466 { LANG_KOREAN
, SUBLANG_NEUTRAL
, 949 },
467 { LANG_KYRGYZ
, SUBLANG_NEUTRAL
, 1251 },
468 { LANG_LAO
, SUBLANG_NEUTRAL
, 0 },
469 { LANG_LATVIAN
, SUBLANG_NEUTRAL
, 1257 },
470 { LANG_LITHUANIAN
, SUBLANG_NEUTRAL
, 1257 },
471 { LANG_LOWER_SORBIAN
, SUBLANG_NEUTRAL
, 1252 },
472 { LANG_LUXEMBOURGISH
, SUBLANG_NEUTRAL
, 1252 },
473 { LANG_MACEDONIAN
, SUBLANG_NEUTRAL
, 1251 },
474 { LANG_MALAY
, SUBLANG_NEUTRAL
, 1252 },
475 { LANG_MALAYALAM
, SUBLANG_NEUTRAL
, 0 },
476 { LANG_MALTESE
, SUBLANG_NEUTRAL
, 0 },
477 { LANG_MAORI
, SUBLANG_NEUTRAL
, 0 },
478 { LANG_MAPUDUNGUN
, SUBLANG_NEUTRAL
, 1252 },
479 { LANG_MARATHI
, SUBLANG_NEUTRAL
, 0 },
480 { LANG_MOHAWK
, SUBLANG_NEUTRAL
, 1252 },
481 { LANG_MONGOLIAN
, SUBLANG_NEUTRAL
, 1251 },
482 { LANG_NEPALI
, SUBLANG_NEUTRAL
, 0 },
483 { LANG_NEUTRAL
, SUBLANG_NEUTRAL
, 1252 },
484 { LANG_NORWEGIAN
, SUBLANG_NEUTRAL
, 1252 },
485 { LANG_OCCITAN
, SUBLANG_NEUTRAL
, 1252 },
486 { LANG_ORIYA
, SUBLANG_NEUTRAL
, 0 },
487 { LANG_PASHTO
, SUBLANG_NEUTRAL
, 0 },
488 { LANG_PERSIAN
, SUBLANG_NEUTRAL
, 1256 },
489 { LANG_POLISH
, SUBLANG_NEUTRAL
, 1250 },
490 { LANG_PORTUGUESE
, SUBLANG_NEUTRAL
, 1252 },
491 { LANG_PUNJABI
, SUBLANG_NEUTRAL
, 0 },
492 { LANG_QUECHUA
, SUBLANG_NEUTRAL
, 1252 },
493 { LANG_ROMANIAN
, SUBLANG_NEUTRAL
, 1250 },
494 { LANG_ROMANSH
, SUBLANG_NEUTRAL
, 1252 },
495 { LANG_RUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
496 { LANG_SAMI
, SUBLANG_NEUTRAL
, 1252 },
497 { LANG_SANSKRIT
, SUBLANG_NEUTRAL
, 0 },
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 },
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
)
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
)
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
) );