Added some stubs.
[wine/dcerpc.git] / tools / wrc / utils.c
blob372b9ee1de57071e64cff5570cc387be8fd37a7b
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wine/unicode.h"
32 #include "wrc.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "preproc.h"
37 /* #define WANT_NEAR_INDICATION */
39 static const union cptable *current_codepage;
41 #ifdef WANT_NEAR_INDICATION
42 void make_print(char *str)
44 while(*str)
46 if(!isprint(*str))
47 *str = ' ';
48 str++;
51 #endif
53 static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
55 fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
56 vfprintf(stderr, s, ap);
57 #ifdef WANT_NEAR_INDICATION
59 char *cpy;
60 if(n)
62 cpy = xstrdup(n);
63 make_print(cpy);
64 fprintf(stderr, " near '%s'", cpy);
65 free(cpy);
68 #endif
69 fprintf(stderr, "\n");
73 int yyerror(const char *s, ...)
75 va_list ap;
76 va_start(ap, s);
77 generic_msg(s, "Error", yytext, ap);
78 va_end(ap);
79 exit(1);
80 return 1;
83 int yywarning(const char *s, ...)
85 va_list ap;
86 va_start(ap, s);
87 generic_msg(s, "Warning", yytext, ap);
88 va_end(ap);
89 return 0;
92 int pperror(const char *s, ...)
94 va_list ap;
95 va_start(ap, s);
96 generic_msg(s, "Error", pptext, ap);
97 va_end(ap);
98 exit(1);
99 return 1;
102 int ppwarning(const char *s, ...)
104 va_list ap;
105 va_start(ap, s);
106 generic_msg(s, "Warning", pptext, ap);
107 va_end(ap);
108 return 0;
112 void internal_error(const char *file, int line, const char *s, ...)
114 va_list ap;
115 va_start(ap, s);
116 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
117 vfprintf(stderr, s, ap);
118 fprintf(stderr, "\n");
119 va_end(ap);
120 exit(3);
123 void error(const char *s, ...)
125 va_list ap;
126 va_start(ap, s);
127 fprintf(stderr, "Error: ");
128 vfprintf(stderr, s, ap);
129 fprintf(stderr, "\n");
130 va_end(ap);
131 exit(2);
134 void warning(const char *s, ...)
136 va_list ap;
137 va_start(ap, s);
138 fprintf(stderr, "Warning: ");
139 vfprintf(stderr, s, ap);
140 fprintf(stderr, "\n");
141 va_end(ap);
144 void chat(const char *s, ...)
146 if(debuglevel & DEBUGLEVEL_CHAT)
148 va_list ap;
149 va_start(ap, s);
150 fprintf(stderr, "FYI: ");
151 vfprintf(stderr, s, ap);
152 fprintf(stderr, "\n");
153 va_end(ap);
157 char *dup_basename(const char *name, const char *ext)
159 int namelen;
160 int extlen = strlen(ext);
161 char *base;
162 char *slash;
164 if(!name)
165 name = "wrc.tab";
167 slash = strrchr(name, '/');
168 if (slash)
169 name = slash + 1;
171 namelen = strlen(name);
173 /* +4 for later extension and +1 for '\0' */
174 base = (char *)xmalloc(namelen +4 +1);
175 strcpy(base, name);
176 if(!strcasecmp(name + namelen-extlen, ext))
178 base[namelen - extlen] = '\0';
180 return base;
183 void *xmalloc(size_t size)
185 void *res;
187 assert(size > 0);
188 res = malloc(size);
189 if(res == NULL)
191 error("Virtual memory exhausted.\n");
194 * We set it to 0.
195 * This is *paramount* because we depend on it
196 * just about everywhere in the rest of the code.
198 memset(res, 0, size);
199 return res;
203 void *xrealloc(void *p, size_t size)
205 void *res;
207 assert(size > 0);
208 res = realloc(p, size);
209 if(res == NULL)
211 error("Virtual memory exhausted.\n");
213 return res;
216 char *xstrdup(const char *str)
218 char *s;
220 assert(str != NULL);
221 s = (char *)xmalloc(strlen(str)+1);
222 return strcpy(s, str);
225 short *dupcstr2wstr(const char *str)
227 int len;
228 WCHAR *ws;
230 if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
231 len = cp_mbstowcs( current_codepage, 0, str, strlen(str), NULL, 0 );
232 ws = xmalloc( sizeof(WCHAR) * (len + 1) );
233 len = cp_mbstowcs( current_codepage, 0, str, strlen(str), ws, len );
234 ws[len] = 0;
235 return ws;
238 char *dupwstr2cstr(const short *str)
240 int len;
241 char *cs;
243 if (!current_codepage) set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
244 len = cp_wcstombs( current_codepage, 0, str, strlenW(str), NULL, 0, NULL, NULL );
245 cs = xmalloc( len + 1 );
246 len = cp_wcstombs( current_codepage, 0, str, strlenW(str), cs, len, NULL, NULL );
247 cs[len] = 0;
248 return cs;
252 *****************************************************************************
253 * Function : compare_name_id
254 * Syntax : int compare_name_id(name_id_t *n1, name_id_t *n2)
255 * Input :
256 * Output :
257 * Description :
258 * Remarks :
259 *****************************************************************************
261 int compare_name_id(name_id_t *n1, name_id_t *n2)
263 if(n1->type == name_ord && n2->type == name_ord)
265 return n1->name.i_name - n2->name.i_name;
267 else if(n1->type == name_str && n2->type == name_str)
269 if(n1->name.s_name->type == str_char
270 && n2->name.s_name->type == str_char)
272 return strcasecmp(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
274 else if(n1->name.s_name->type == str_unicode
275 && n2->name.s_name->type == str_unicode)
277 return strcmpiW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
279 else
281 internal_error(__FILE__, __LINE__, "Can't yet compare strings of mixed type");
284 else if(n1->type == name_ord && n2->type == name_str)
285 return 1;
286 else if(n1->type == name_str && n2->type == name_ord)
287 return -1;
288 else
289 internal_error(__FILE__, __LINE__, "Comparing name-ids with unknown types (%d, %d)",
290 n1->type, n2->type);
292 return 0; /* Keep the compiler happy */
295 string_t *convert_string(const string_t *str, enum str_e type)
297 string_t *ret = xmalloc(sizeof(*ret));
299 if((str->type == str_char) && (type == str_unicode))
301 ret->str.wstr = dupcstr2wstr(str->str.cstr);
302 ret->type = str_unicode;
303 ret->size = strlenW(ret->str.wstr);
305 else if((str->type == str_unicode) && (type == str_char))
307 ret->str.cstr = dupwstr2cstr(str->str.wstr);
308 ret->type = str_char;
309 ret->size = strlen(ret->str.cstr);
311 else if(str->type == str_unicode)
313 ret->type = str_unicode;
314 ret->size = strlenW(str->str.wstr);
315 ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
316 strcpyW(ret->str.wstr, str->str.wstr);
318 else /* str->type == str_char */
320 ret->type = str_char;
321 ret->size = strlen(str->str.cstr);
322 ret->str.cstr = xmalloc( ret->size + 1 );
323 strcpy(ret->str.cstr, str->str.cstr);
325 return ret;
329 struct lang2cp
331 unsigned short lang;
332 unsigned short sublang;
333 unsigned int cp;
334 } lang2cp_t;
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_BASQUE, SUBLANG_NEUTRAL, 1252 },
345 { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
346 { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
347 { LANG_BYELORUSSIAN, SUBLANG_NEUTRAL, 1251 },
348 { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
349 { LANG_CHINESE, SUBLANG_NEUTRAL, 936 },
350 { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
351 { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
352 { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
353 { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
354 { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
355 { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
356 { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
357 { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
358 { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
359 { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
360 { LANG_GAELIC, SUBLANG_NEUTRAL, 1252 },
361 { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
362 { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
363 { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
364 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
365 { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
366 { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
367 { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
368 { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
369 { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
370 { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
371 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
372 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
373 { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
374 { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
375 { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
376 { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
377 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
378 { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
379 { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
380 { LANG_SERBO_CROATIAN, SUBLANG_NEUTRAL, 1250 },
381 { LANG_SERBO_CROATIAN, SUBLANG_SERBIAN, 1251 },
382 { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
383 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
384 { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
385 { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
386 { LANG_THAI, SUBLANG_NEUTRAL, 874 },
387 { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
388 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
389 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
390 { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
391 { LANG_WELSH, SUBLANG_NEUTRAL, 1252 }
394 void set_language( unsigned short lang, unsigned short sublang )
396 unsigned int i;
397 unsigned int cp = 0, defcp = 0;
399 for (i = 0; i < sizeof(lang2cps)/sizeof(lang2cps[0]); i++)
401 if (lang2cps[i].lang != lang) continue;
402 if (lang2cps[i].sublang == sublang)
404 cp = lang2cps[i].cp;
405 break;
407 if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
410 if (!cp) cp = defcp;
411 if (!cp)
412 error( "No codepage value for language %04x", MAKELANGID(lang,sublang) );
413 if (!(current_codepage = cp_get_table( cp )))
414 error( "Bad codepage %d for language %04x", cp, MAKELANGID(lang,sublang) );