4 * Copyright 1998 Bertho A. Stultiens
18 #include "wine/unicode.h"
24 /* #define WANT_NEAR_INDICATION */
26 static const union cptable *current_codepage;
28 #ifdef WANT_NEAR_INDICATION
29 void make_print(char *str)
40 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
42 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
43 vfprintf(stderr, s, ap);
44 #ifdef WANT_NEAR_INDICATION
51 fprintf(stderr, " near '%s'", cpy);
56 fprintf(stderr, "\n");
60 int yyerror(const char *s, ...)
64 generic_msg(s, "Error", yytext, ap);
70 int yywarning(const char *s, ...)
74 generic_msg(s, "Warning", yytext, ap);
79 int pperror(const char *s, ...)
83 generic_msg(s, "Error", pptext, ap);
89 int ppwarning(const char *s, ...)
93 generic_msg(s, "Warning", pptext, ap);
99 void internal_error(const char *file, int line, const char *s, ...)
103 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
104 vfprintf(stderr, s, ap);
105 fprintf(stderr, "\n");
110 void error(const char *s, ...)
114 fprintf(stderr, "Error: ");
115 vfprintf(stderr, s, ap);
116 fprintf(stderr, "\n");
121 void warning(const char *s, ...)
125 fprintf(stderr, "Warning: ");
126 vfprintf(stderr, s, ap);
127 fprintf(stderr, "\n");
131 void chat(const char *s, ...)
133 if(debuglevel & DEBUGLEVEL_CHAT)
137 fprintf(stderr, "FYI: ");
138 vfprintf(stderr, s, ap);
139 fprintf(stderr, "\n");
144 char *dup_basename(const char *name, const char *ext)
147 int extlen = strlen(ext);
154 slash = strrchr(name, '/');
158 namelen = strlen(name);
160 /* +4 for later extension and +1 for '\0' */
161 base = (char *)xmalloc(namelen +4 +1);
163 if(!strcasecmp(name + namelen-extlen, ext))
165 base[namelen - extlen] = '\0';
170 void *xmalloc(size_t size)
178 error("Virtual memory exhausted.\n");
182 * This is *paramount* because we depend on it
183 * just about everywhere in the rest of the code.
185 memset(res, 0, size);
190 void *xrealloc(void *p, size_t size)
195 res = realloc(p, size);
198 error("Virtual memory exhausted.\n");
203 char *xstrdup(const char *str)
208 s = (char *)xmalloc(strlen(str)+1);
209 return strcpy(s, str);
212 short *dupcstr2wstr(const char *str)
217 if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
218 len = cp_mbstowcs( current_codepage, 0, str, strlen(str), NULL, 0 );
219 ws = xmalloc( sizeof(WCHAR) * (len + 1) );
220 len = cp_mbstowcs( current_codepage, 0, str, strlen(str), ws, len );
225 char *dupwstr2cstr(const short *str)
230 if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
231 len = cp_wcstombs( current_codepage, 0, str, strlenW(str), NULL, 0, NULL, NULL );
232 cs = xmalloc( len + 1 );
233 len = cp_wcstombs( current_codepage, 0, str, strlenW(str), cs, len, NULL, NULL );
239 *****************************************************************************
240 * Function : compare_name_id
241 * Syntax : int compare_name_id(name_id_t *n1, name_id_t *n2)
246 *****************************************************************************
248 int compare_name_id(name_id_t *n1, name_id_t *n2)
250 if(n1->type == name_ord && n2->type == name_ord)
252 return n1->name.i_name - n2->name.i_name;
254 else if(n1->type == name_str && n2->type == name_str)
256 if(n1->name.s_name->type == str_char
257 && n2->name.s_name->type == str_char)
259 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
261 else if(n1->name.s_name->type == str_unicode
262 && n2->name.s_name->type == str_unicode)
264 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
268 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
271 else if(n1->type == name_ord && n2->type == name_str)
273 else if(n1->type == name_str && n2->type == name_ord)
276 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
279 return 0; /* Keep the compiler happy */
282 string_t *convert_string(const string_t *str, enum str_e type)
284 string_t *ret = xmalloc(sizeof(*ret));
286 if((str->type == str_char) && (type == str_unicode))
288 ret->str.wstr = dupcstr2wstr(str->str.cstr);
289 ret->type = str_unicode;
290 ret->size = strlenW(ret->str.wstr);
292 else if((str->type == str_unicode) && (type == str_char))
294 ret->str.cstr = dupwstr2cstr(str->str.wstr);
295 ret->type = str_char;
296 ret->size = strlen(ret->str.cstr);
298 else if(str->type == str_unicode)
300 ret->type = str_unicode;
301 ret->size = strlenW(str->str.wstr);
302 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
303 strcpyW(ret->str.wstr, str->str.wstr);
305 else /* str->type == str_char */
307 ret->type = str_char;
308 ret->size = strlen(str->str.cstr);
309 ret->str.cstr = xmalloc( ret->size + 1 );
310 strcpy(ret->str.cstr, str->str.cstr);
319 unsigned short sublang;
323 /* language to codepage conversion table */
324 /* specific sublanguages need only be specified if their codepage */
325 /* differs from the default (SUBLANG_NEUTRAL) */
326 static const struct lang2cp lang2cps[] =
328 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
329 { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
330 { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
331 { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
332 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
333 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
334 { LANG_BYELORUSSIAN, SUBLANG_NEUTRAL, 1251 },
335 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
336 { LANG_CHINESE, SUBLANG_NEUTRAL, 936 },
337 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
338 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
339 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
340 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
341 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
342 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
343 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
344 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
345 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
346 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
347 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
348 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
349 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
350 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
351 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
352 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
353 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
354 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
355 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
356 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
357 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
358 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
359 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
360 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
361 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
362 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
363 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
364 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
365 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
366 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
367 { LANG_SERBO_CROATIAN, SUBLANG_NEUTRAL, 1250 },
368 { LANG_SERBO_CROATIAN, SUBLANG_SERBIAN, 1251 },
369 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
370 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
371 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
372 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
373 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
374 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
375 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
376 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
377 { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
378 { LANG_WELSH, SUBLANG_NEUTRAL, 1252 }
381 void set_language( unsigned short lang, unsigned short sublang )
384 unsigned int cp = 0, defcp = 0;
386 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
388 if (lang2cps[i].lang != lang) continue;
389 if (lang2cps[i].sublang == sublang)
394 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
399 error( "No codepage value for language %04x", MAKELANGID(lang,sublang) );
400 if (!(current_codepage = cp_get_table( cp )))
401 error( "Bad codepage %d for language %04x", cp, MAKELANGID(lang,sublang) );