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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
32 #include "wine/unicode.h"
37 /* #define WANT_NEAR_INDICATION */
39 #ifdef WANT_NEAR_INDICATION
40 void make_print(char *str
)
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
62 fprintf(stderr
, " near '%s'", cpy
);
67 fprintf(stderr
, "\n");
71 int yyerror(const char *s
, ...)
75 generic_msg(s
, "Error", yytext
, ap
);
81 int yywarning(const char *s
, ...)
85 generic_msg(s
, "Warning", yytext
, ap
);
90 void internal_error(const char *file
, int line
, const char *s
, ...)
94 fprintf(stderr
, "Internal error (please report) %s %d: ", file
, line
);
95 vfprintf(stderr
, s
, ap
);
96 fprintf(stderr
, "\n");
101 void error(const char *s
, ...)
105 fprintf(stderr
, "Error: ");
106 vfprintf(stderr
, s
, ap
);
107 fprintf(stderr
, "\n");
112 void warning(const char *s
, ...)
116 fprintf(stderr
, "Warning: ");
117 vfprintf(stderr
, s
, ap
);
118 fprintf(stderr
, "\n");
122 void chat(const char *s
, ...)
124 if(debuglevel
& DEBUGLEVEL_CHAT
)
128 fprintf(stderr
, "FYI: ");
129 vfprintf(stderr
, s
, ap
);
130 fprintf(stderr
, "\n");
135 char *dup_basename(const char *name
, const char *ext
)
138 int extlen
= strlen(ext
);
145 slash
= strrchr(name
, '/');
149 namelen
= strlen(name
);
151 /* +4 for later extension and +1 for '\0' */
152 base
= (char *)xmalloc(namelen
+4 +1);
154 if(!strcasecmp(name
+ namelen
-extlen
, ext
))
156 base
[namelen
- extlen
] = '\0';
161 void *xmalloc(size_t size
)
169 error("Virtual memory exhausted.\n");
173 * This is *paramount* because we depend on it
174 * just about everywhere in the rest of the code.
176 memset(res
, 0, size
);
181 void *xrealloc(void *p
, size_t size
)
186 res
= realloc(p
, size
);
189 error("Virtual memory exhausted.\n");
194 char *xstrdup(const char *str
)
199 s
= (char *)xmalloc(strlen(str
)+1);
200 return strcpy(s
, str
);
205 *****************************************************************************
206 * Function : compare_name_id
207 * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
212 *****************************************************************************
214 int compare_name_id(const name_id_t
*n1
, const name_id_t
*n2
)
216 if(n1
->type
== name_ord
&& n2
->type
== name_ord
)
218 return n1
->name
.i_name
- n2
->name
.i_name
;
220 else if(n1
->type
== name_str
&& n2
->type
== name_str
)
222 if(n1
->name
.s_name
->type
== str_char
223 && n2
->name
.s_name
->type
== str_char
)
225 return strcasecmp(n1
->name
.s_name
->str
.cstr
, n2
->name
.s_name
->str
.cstr
);
227 else if(n1
->name
.s_name
->type
== str_unicode
228 && n2
->name
.s_name
->type
== str_unicode
)
230 return strcmpiW(n1
->name
.s_name
->str
.wstr
, n2
->name
.s_name
->str
.wstr
);
234 internal_error(__FILE__
, __LINE__
, "Can't yet compare strings of mixed type");
237 else if(n1
->type
== name_ord
&& n2
->type
== name_str
)
239 else if(n1
->type
== name_str
&& n2
->type
== name_ord
)
242 internal_error(__FILE__
, __LINE__
, "Comparing name-ids with unknown types (%d, %d)",
245 return 0; /* Keep the compiler happy */
248 string_t
*convert_string(const string_t
*str
, enum str_e type
, int codepage
)
250 const union cptable
*cptable
= codepage
? wine_cp_get_table( codepage
) : NULL
;
251 string_t
*ret
= xmalloc(sizeof(*ret
));
253 if (!cptable
&& str
->type
!= type
)
254 error( "Current language is Unicode only, cannot convert strings" );
256 if((str
->type
== str_char
) && (type
== str_unicode
))
258 ret
->type
= str_unicode
;
259 ret
->size
= wine_cp_mbstowcs( cptable
, 0, str
->str
.cstr
, str
->size
, NULL
, 0 );
260 ret
->str
.wstr
= xmalloc( (ret
->size
+1) * sizeof(WCHAR
) );
261 wine_cp_mbstowcs( cptable
, 0, str
->str
.cstr
, str
->size
, ret
->str
.wstr
, ret
->size
);
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
= wine_cp_wcstombs( cptable
, 0, str
->str
.wstr
, str
->size
,
268 NULL
, 0, NULL
, NULL
);
269 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
270 wine_cp_wcstombs( cptable
, 0, str
->str
.wstr
, str
->size
, ret
->str
.cstr
, ret
->size
,
272 ret
->str
.cstr
[ret
->size
] = 0;
274 else if(str
->type
== str_unicode
)
276 ret
->type
= str_unicode
;
277 ret
->size
= str
->size
;
278 ret
->str
.wstr
= xmalloc(sizeof(WCHAR
)*(ret
->size
+1));
279 memcpy( ret
->str
.wstr
, str
->str
.wstr
, ret
->size
* sizeof(WCHAR
) );
280 ret
->str
.wstr
[ret
->size
] = 0;
282 else /* str->type == str_char */
284 ret
->type
= str_char
;
285 ret
->size
= str
->size
;
286 ret
->str
.cstr
= xmalloc( ret
->size
+ 1 );
287 memcpy( ret
->str
.cstr
, str
->str
.cstr
, ret
->size
);
288 ret
->str
.cstr
[ret
->size
] = 0;
294 void free_string(string_t
*str
)
296 if (str
->type
== str_unicode
) free( str
->str
.wstr
);
297 else free( str
->str
.cstr
);
302 int check_unicode_conversion( const string_t
*str_a
, const string_t
*str_w
, int codepage
)
305 string_t
*teststr
= convert_string( str_w
, str_char
, codepage
);
307 ok
= (teststr
->size
== str_a
->size
&& !memcmp( teststr
->str
.cstr
, str_a
->str
.cstr
, str_a
->size
));
313 fprintf( stderr
, "Source: %s", str_a
->str
.cstr
);
314 for (i
= 0; i
< str_a
->size
; i
++)
315 fprintf( stderr
, " %02x", (unsigned char)str_a
->str
.cstr
[i
] );
316 fprintf( stderr
, "\nUnicode: " );
317 for (i
= 0; i
< str_w
->size
; i
++)
318 fprintf( stderr
, " %04x", str_w
->str
.wstr
[i
] );
319 fprintf( stderr
, "\nBack: %s", teststr
->str
.cstr
);
320 for (i
= 0; i
< teststr
->size
; i
++)
321 fprintf( stderr
, " %02x", (unsigned char)teststr
->str
.cstr
[i
] );
322 fprintf( stderr
, "\n" );
324 free_string( teststr
);
332 unsigned short sublang
;
336 /* language to codepage conversion table */
337 /* specific sublanguages need only be specified if their codepage */
338 /* differs from the default (SUBLANG_NEUTRAL) */
339 static const struct lang2cp lang2cps
[] =
341 { LANG_AFRIKAANS
, SUBLANG_NEUTRAL
, 1252 },
342 { LANG_ALBANIAN
, SUBLANG_NEUTRAL
, 1250 },
343 { LANG_ARABIC
, SUBLANG_NEUTRAL
, 1256 },
344 { LANG_ARMENIAN
, SUBLANG_NEUTRAL
, 0 },
345 { LANG_AZERI
, SUBLANG_NEUTRAL
, 1254 },
346 { LANG_AZERI
, SUBLANG_AZERI_CYRILLIC
, 1251 },
347 { LANG_BASQUE
, SUBLANG_NEUTRAL
, 1252 },
348 { LANG_BELARUSIAN
, SUBLANG_NEUTRAL
, 1251 },
350 { LANG_BRETON
, SUBLANG_NEUTRAL
, 1252 },
351 #endif /* LANG_BRETON */
352 { LANG_BULGARIAN
, SUBLANG_NEUTRAL
, 1251 },
353 { LANG_CATALAN
, SUBLANG_NEUTRAL
, 1252 },
354 { LANG_CHINESE
, SUBLANG_NEUTRAL
, 950 },
355 { LANG_CHINESE
, SUBLANG_CHINESE_SINGAPORE
, 936 },
356 { LANG_CHINESE
, SUBLANG_CHINESE_SIMPLIFIED
, 936 },
358 { LANG_CORNISH
, SUBLANG_NEUTRAL
, 1252 },
359 #endif /* LANG_CORNISH */
360 { LANG_CROATIAN
, SUBLANG_NEUTRAL
, 1250 },
361 { LANG_CZECH
, SUBLANG_NEUTRAL
, 1250 },
362 { LANG_DANISH
, SUBLANG_NEUTRAL
, 1252 },
363 { LANG_DIVEHI
, SUBLANG_NEUTRAL
, 0 },
364 { LANG_DUTCH
, SUBLANG_NEUTRAL
, 1252 },
365 { LANG_ENGLISH
, SUBLANG_NEUTRAL
, 1252 },
366 #ifdef LANG_ESPERANTO
367 { LANG_ESPERANTO
, SUBLANG_NEUTRAL
, 1252 },
368 #endif /* LANG_ESPERANTO */
369 { LANG_ESTONIAN
, SUBLANG_NEUTRAL
, 1257 },
370 { LANG_FAEROESE
, SUBLANG_NEUTRAL
, 1252 },
371 { LANG_FARSI
, SUBLANG_NEUTRAL
, 1256 },
372 { LANG_FINNISH
, SUBLANG_NEUTRAL
, 1252 },
373 { LANG_FRENCH
, SUBLANG_NEUTRAL
, 1252 },
375 { LANG_GAELIC
, SUBLANG_NEUTRAL
, 1252 },
376 #endif /* LANG_GAELIC */
377 { LANG_GALICIAN
, SUBLANG_NEUTRAL
, 1252 },
378 { LANG_GEORGIAN
, SUBLANG_NEUTRAL
, 0 },
379 { LANG_GERMAN
, SUBLANG_NEUTRAL
, 1252 },
380 { LANG_GREEK
, SUBLANG_NEUTRAL
, 1253 },
381 { LANG_GUJARATI
, SUBLANG_NEUTRAL
, 0 },
382 { LANG_HEBREW
, SUBLANG_NEUTRAL
, 1255 },
383 { LANG_HINDI
, SUBLANG_NEUTRAL
, 0 },
384 { LANG_HUNGARIAN
, SUBLANG_NEUTRAL
, 1250 },
385 { LANG_ICELANDIC
, SUBLANG_NEUTRAL
, 1252 },
386 { LANG_INDONESIAN
, SUBLANG_NEUTRAL
, 1252 },
387 { LANG_ITALIAN
, SUBLANG_NEUTRAL
, 1252 },
388 { LANG_JAPANESE
, SUBLANG_NEUTRAL
, 932 },
389 { LANG_KANNADA
, SUBLANG_NEUTRAL
, 0 },
390 { LANG_KAZAK
, SUBLANG_NEUTRAL
, 1251 },
391 { LANG_KONKANI
, SUBLANG_NEUTRAL
, 0 },
392 { LANG_KOREAN
, SUBLANG_NEUTRAL
, 949 },
393 { LANG_KYRGYZ
, SUBLANG_NEUTRAL
, 1251 },
394 { LANG_LATVIAN
, SUBLANG_NEUTRAL
, 1257 },
395 { LANG_LITHUANIAN
, SUBLANG_NEUTRAL
, 1257 },
396 { LANG_MACEDONIAN
, SUBLANG_NEUTRAL
, 1251 },
397 { LANG_MALAY
, SUBLANG_NEUTRAL
, 1252 },
398 { LANG_MARATHI
, SUBLANG_NEUTRAL
, 0 },
399 { LANG_MONGOLIAN
, SUBLANG_NEUTRAL
, 1251 },
400 { LANG_NEUTRAL
, SUBLANG_NEUTRAL
, 1252 },
401 { LANG_NORWEGIAN
, SUBLANG_NEUTRAL
, 1252 },
402 { LANG_POLISH
, SUBLANG_NEUTRAL
, 1250 },
403 { LANG_PORTUGUESE
, SUBLANG_NEUTRAL
, 1252 },
404 { LANG_PUNJABI
, SUBLANG_NEUTRAL
, 0 },
405 { LANG_ROMANIAN
, SUBLANG_NEUTRAL
, 1250 },
406 { LANG_RUSSIAN
, SUBLANG_NEUTRAL
, 1251 },
407 { LANG_SANSKRIT
, SUBLANG_NEUTRAL
, 0 },
408 { LANG_SERBIAN
, SUBLANG_NEUTRAL
, 1250 },
409 { LANG_SERBIAN
, SUBLANG_SERBIAN_CYRILLIC
, 1251 },
410 { LANG_SLOVAK
, SUBLANG_NEUTRAL
, 1250 },
411 { LANG_SLOVENIAN
, SUBLANG_NEUTRAL
, 1250 },
412 { LANG_SPANISH
, SUBLANG_NEUTRAL
, 1252 },
413 { LANG_SWAHILI
, SUBLANG_NEUTRAL
, 1252 },
414 { LANG_SWEDISH
, SUBLANG_NEUTRAL
, 1252 },
415 { LANG_SYRIAC
, SUBLANG_NEUTRAL
, 0 },
416 { LANG_TAMIL
, SUBLANG_NEUTRAL
, 0 },
417 { LANG_TATAR
, SUBLANG_NEUTRAL
, 1251 },
418 { LANG_TELUGU
, SUBLANG_NEUTRAL
, 0 },
419 { LANG_THAI
, SUBLANG_NEUTRAL
, 874 },
420 { LANG_TURKISH
, SUBLANG_NEUTRAL
, 1254 },
421 { LANG_UKRAINIAN
, SUBLANG_NEUTRAL
, 1251 },
422 { LANG_URDU
, SUBLANG_NEUTRAL
, 1256 },
423 { LANG_UZBEK
, SUBLANG_NEUTRAL
, 1254 },
424 { LANG_UZBEK
, SUBLANG_UZBEK_CYRILLIC
, 1251 },
425 { LANG_VIETNAMESE
, SUBLANG_NEUTRAL
, 1258 }
427 , { LANG_WALON
, SUBLANG_NEUTRAL
, 1252 }
428 #endif /* LANG_WALON */
430 , { LANG_WELSH
, SUBLANG_NEUTRAL
, 1252 }
431 #endif /* LANG_WELSH */
434 int get_language_codepage( unsigned short lang
, unsigned short sublang
)
437 int cp
= -1, defcp
= -1;
439 for (i
= 0; i
< sizeof(lang2cps
)/sizeof(lang2cps
[0]); i
++)
441 if (lang2cps
[i
].lang
!= lang
) continue;
442 if (lang2cps
[i
].sublang
== sublang
)
447 if (lang2cps
[i
].sublang
== SUBLANG_NEUTRAL
) defcp
= lang2cps
[i
].cp
;
450 if (cp
== -1) cp
= defcp
;
451 assert( cp
<= 0 || wine_cp_get_table(cp
) );