dmsynth: Add error handling to the synth GetPortCaps method.
[wine.git] / tools / wrc / wrc.c
blob41207120237a75b855ee563118dd3eb74a1e33c9
1 /*
2 * Copyright 1994 Martin von Loewis
3 * Copyright 1998 Bertho A. Stultiens (BS)
4 * Copyright 2003 Dimitrie O. Paun
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 "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SYSCTL_H
33 # include <sys/sysctl.h>
34 #endif
36 #include "../tools.h"
37 #include "wrc.h"
38 #include "utils.h"
39 #include "dumpres.h"
40 #include "newstruc.h"
41 #include "parser.h"
42 #include "wpp_private.h"
44 static const char usage[] =
45 "Usage: wrc [options...] [infile[.rc|.res]]\n"
46 " -D, --define id[=val] Define preprocessor identifier id=val\n"
47 " --debug=nn Set debug level to 'nn'\n"
48 " -E Preprocess only\n"
49 " -F TARGET Ignored, for compatibility with windres\n"
50 " -fo FILE Synonym for -o for compatibility with windres\n"
51 " -h, --help Prints this summary\n"
52 " -i, --input=FILE The name of the input file\n"
53 " -I, --include-dir=DIR Add dir to the include search path (multiple -I allowed)\n"
54 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
55 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
56 " -m16 Build a 16-bit resource file\n"
57 " --nls-dir=DIR Directory containing the NLS codepage mappings\n"
58 " --no-use-temp-file Ignored for compatibility with windres\n"
59 " --nostdinc Disables searching the standard include path\n"
60 " -o, --output=FILE Output to file (default is infile.res)\n"
61 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
62 " --pedantic Enable pedantic warnings\n"
63 " --po-dir=DIR Directory containing po files for translations\n"
64 " --preprocessor Specifies the preprocessor to use, including arguments\n"
65 " -r Ignored for compatibility with rc\n"
66 " --sysroot=DIR Prefix include paths with DIR\n"
67 " -U, --undefine id Undefine preprocessor identifier id\n"
68 " --use-temp-file Ignored for compatibility with windres\n"
69 " -v, --verbose Enable verbose mode\n"
70 " --verify-translations Check the status of the various translations\n"
71 " --version Print version and exit\n"
72 "Input is taken from stdin if no sourcefile specified.\n"
73 "Debug level 'n' is a bitmask with following meaning:\n"
74 " * 0x01 Tell which resource is parsed (verbose mode)\n"
75 " * 0x02 Dump internal structures\n"
76 " * 0x04 Create a parser trace (yydebug=1)\n"
77 " * 0x08 Preprocessor messages\n"
78 " * 0x10 Preprocessor lex messages\n"
79 " * 0x20 Preprocessor yacc trace\n"
80 "If no input filename is given and the output name is not overridden\n"
81 "with -o, then the output is written to \"wrc.tab.res\"\n"
84 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
85 "Copyright 1998-2000 Bertho A. Stultiens\n"
86 " 1994 Martin von Loewis\n";
89 * Set if compiling in 32bit mode (default).
91 int win32 = 1;
94 * debuglevel == DEBUGLEVEL_NONE Don't bother
95 * debuglevel & DEBUGLEVEL_CHAT Say what's done
96 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
97 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
98 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
99 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
100 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
102 int debuglevel = DEBUGLEVEL_NONE;
105 * Recognize win32 keywords if set (-w 32 enforces this),
106 * otherwise set with -e option.
108 int extensions = 1;
111 * Language setting for resources (-l option)
113 static language_t *defaultlanguage;
114 language_t *currentlanguage = NULL;
117 * Set when extra warnings should be generated (-W option)
119 int pedantic = 0;
122 * Set when _only_ to run the preprocessor (-E option)
124 int preprocess_only = 0;
127 * Set when _not_ to run the preprocessor (-P cat option)
129 int no_preprocess = 0;
131 int utf8_input = 0;
133 int check_utf8 = 1; /* whether to check for valid utf8 */
135 static int verify_translations_mode;
137 static char *output_name; /* The name given by the -o option */
138 const char *input_name = NULL; /* The name given on the command-line */
139 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
140 static struct strarray input_files;
142 static int stdinc = 1;
143 static int po_mode;
144 static const char *po_dir;
145 static const char *sysroot = "";
146 static const char *includedir;
147 const char *nlsdirs[3] = { NULL, NLSDIR, NULL };
149 int line_number = 1; /* The current line */
150 int char_number = 1; /* The current char pos within the line */
152 int parser_debug, yy_flex_debug;
154 resource_t *resource_top; /* The top of the parsed resources */
156 static void cleanup_files(void);
158 enum long_options_values
160 LONG_OPT_NOSTDINC = 1,
161 LONG_OPT_TMPFILE,
162 LONG_OPT_NOTMPFILE,
163 LONG_OPT_NLS_DIR,
164 LONG_OPT_PO_DIR,
165 LONG_OPT_PREPROCESSOR,
166 LONG_OPT_SYSROOT,
167 LONG_OPT_VERSION,
168 LONG_OPT_DEBUG,
169 LONG_OPT_PEDANTIC,
170 LONG_OPT_VERIFY_TRANSL
173 static const char short_options[] =
174 "b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
175 static const struct long_option long_options[] = {
176 { "debug", 1, LONG_OPT_DEBUG },
177 { "define", 1, 'D' },
178 { "help", 0, 'h' },
179 { "include-dir", 1, 'I' },
180 { "input", 1, 'i' },
181 { "input-format", 1, 'J' },
182 { "language", 1, 'l' },
183 { "nls-dir", 1, LONG_OPT_NLS_DIR },
184 { "no-use-temp-file", 0, LONG_OPT_NOTMPFILE },
185 { "nostdinc", 0, LONG_OPT_NOSTDINC },
186 { "output", 1, 'o' },
187 { "output-format", 1, 'O' },
188 { "pedantic", 0, LONG_OPT_PEDANTIC },
189 { "po-dir", 1, LONG_OPT_PO_DIR },
190 { "preprocessor", 1, LONG_OPT_PREPROCESSOR },
191 { "sysroot", 1, LONG_OPT_SYSROOT },
192 { "target", 1, 'F' },
193 { "utf8", 0, 'u' },
194 { "undefine", 1, 'U' },
195 { "use-temp-file", 0, LONG_OPT_TMPFILE },
196 { "verbose", 0, 'v' },
197 { "verify-translations", 0, LONG_OPT_VERIFY_TRANSL },
198 { "version", 0, LONG_OPT_VERSION },
199 { NULL }
202 static void set_version_defines(void)
204 char *version = xstrdup( PACKAGE_VERSION );
205 char *major, *minor, *patchlevel;
206 char buffer[100];
208 if ((minor = strchr( version, '.' )))
210 major = version;
211 *minor++ = 0;
212 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
214 else /* pre 0.9 version */
216 major = NULL;
217 patchlevel = version;
219 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
220 wpp_add_cmdline_define(buffer);
221 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
222 wpp_add_cmdline_define(buffer);
223 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
224 wpp_add_cmdline_define(buffer);
225 free( version );
228 /* clean things up when aborting on a signal */
229 static void exit_on_signal( int sig )
231 exit(1); /* this will call the atexit functions */
234 /* load a single input file */
235 static int load_file( const char *input_name, const char *output_name )
237 int ret;
239 /* Run the preprocessor on the input */
240 if(!no_preprocess)
242 FILE *output;
243 int ret, fd;
244 char *name;
247 * Preprocess the input to a temp-file, or stdout if
248 * no output was given.
251 if (preprocess_only)
253 if (output_name)
255 if (!(output = fopen( output_name, "w" )))
256 fatal_perror( "Could not open %s for writing", output_name );
257 ret = wpp_parse( input_name, output );
258 fclose( output );
260 else ret = wpp_parse( input_name, stdout );
262 if (ret) return ret;
263 output_name = NULL;
264 exit(0);
267 fd = make_temp_file( output_name, "", &name );
268 temp_name = name;
269 if (!(output = fdopen(fd, "wt")))
270 error("Could not open fd %s for writing\n", name);
272 ret = wpp_parse( input_name, output );
273 fclose( output );
274 if (ret) return ret;
275 input_name = name;
278 /* Reset the language */
279 currentlanguage = dup_language( defaultlanguage );
280 check_utf8 = 1;
282 /* Go from .rc to .res */
283 chat("Starting parse\n");
285 if(!(parser_in = fopen(input_name, "rb")))
286 fatal_perror("Could not open %s for input", input_name);
288 ret = parser_parse();
289 fclose(parser_in);
290 parser_lex_destroy();
291 if (temp_name)
293 unlink( temp_name );
294 temp_name = NULL;
296 free( currentlanguage );
297 return ret;
300 static void init_argv0_dir( const char *argv0 )
302 #ifndef _WIN32
303 char *dir;
305 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
306 dir = realpath( "/proc/self/exe", NULL );
307 #elif defined (__FreeBSD__) || defined(__DragonFly__)
308 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
309 size_t path_size = PATH_MAX;
310 char *path = malloc( path_size );
311 if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
312 dir = realpath( path, NULL );
313 free( path );
314 #else
315 dir = realpath( argv0, NULL );
316 #endif
317 if (!dir) return;
318 dir = get_dirname( dir );
319 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
320 if (strendswith( dir, "/tools/wrc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
321 else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
322 #endif
325 static void option_callback( int optc, char *optarg )
327 switch(optc)
329 case LONG_OPT_NOSTDINC:
330 stdinc = 0;
331 break;
332 case LONG_OPT_TMPFILE:
333 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
334 break;
335 case LONG_OPT_NOTMPFILE:
336 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
337 break;
338 case LONG_OPT_NLS_DIR:
339 nlsdirs[0] = xstrdup( optarg );
340 break;
341 case LONG_OPT_PO_DIR:
342 po_dir = xstrdup( optarg );
343 break;
344 case LONG_OPT_PREPROCESSOR:
345 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
346 else fprintf(stderr, "-P option not yet supported, ignored.\n");
347 break;
348 case LONG_OPT_SYSROOT:
349 sysroot = xstrdup( optarg );
350 break;
351 case LONG_OPT_VERSION:
352 printf(version_string);
353 exit(0);
354 break;
355 case LONG_OPT_DEBUG:
356 debuglevel = strtol(optarg, NULL, 0);
357 break;
358 case LONG_OPT_PEDANTIC:
359 pedantic = 1;
360 break;
361 case LONG_OPT_VERIFY_TRANSL:
362 verify_translations_mode = 1;
363 break;
364 case 'D':
365 wpp_add_cmdline_define(optarg);
366 break;
367 case 'E':
368 preprocess_only = 1;
369 break;
370 case 'b':
371 case 'F':
372 break;
373 case 'h':
374 printf(usage);
375 exit(0);
376 case 'i':
377 strarray_add( &input_files, optarg );
378 break;
379 case 'I':
380 wpp_add_include_path(optarg);
381 break;
382 case 'J':
383 if (strcmp(optarg, "rc16") == 0) extensions = 0;
384 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
385 break;
386 case 'l':
388 int lan;
389 lan = strtol(optarg, NULL, 0);
390 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
391 error("Language %04x is not supported\n", lan);
392 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
394 break;
395 case 'm':
396 if (!strcmp( optarg, "16" )) win32 = 0;
397 else win32 = 1;
398 break;
399 case 'f':
400 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
401 optarg++;
402 /* fall through */
403 case 'o':
404 if (!output_name) output_name = strdup(optarg);
405 else error("Too many output files.\n");
406 break;
407 case 'O':
408 if (strcmp(optarg, "po") == 0) po_mode = 1;
409 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
410 else if (strcmp(optarg, "res16") == 0) win32 = 0;
411 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
412 break;
413 case 'r':
414 /* ignored for compatibility with rc */
415 break;
416 case 'u':
417 utf8_input = 1;
418 break;
419 case 'U':
420 wpp_del_define(optarg);
421 break;
422 case 'v':
423 debuglevel = DEBUGLEVEL_CHAT;
424 break;
425 case '?':
426 fprintf(stderr, "wrc: %s\n\n%s", optarg, usage);
427 exit(1);
431 int main(int argc,char *argv[])
433 int i;
435 signal( SIGTERM, exit_on_signal );
436 signal( SIGINT, exit_on_signal );
437 #ifdef SIGHUP
438 signal( SIGHUP, exit_on_signal );
439 #endif
440 init_argv0_dir( argv[0] );
442 /* Set the default defined stuff */
443 set_version_defines();
444 wpp_add_cmdline_define("RC_INVOKED=1");
445 /* Microsoft RC always searches current directory */
446 wpp_add_include_path(".");
448 strarray_addall( &input_files,
449 parse_options( argc, argv, short_options, long_options, 0, option_callback ));
451 if (win32) wpp_add_cmdline_define("_WIN32=1");
453 /* If we do need to search standard includes, add them to the path */
454 if (stdinc)
456 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
458 if (includedir)
460 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
461 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
463 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
465 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
466 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
467 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
471 /* Kill io buffering when some kind of debuglevel is enabled */
472 if(debuglevel)
474 setbuf(stdout, NULL);
475 setbuf(stderr, NULL);
478 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
479 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
481 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
482 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
483 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
485 /* Check if the user set a language, else set default */
486 if(!defaultlanguage)
487 defaultlanguage = new_language(0, 0);
489 atexit(cleanup_files);
491 for (i = 0; i < input_files.count; i++)
493 input_name = input_files.str[i];
494 if (load_file( input_name, output_name )) exit(1);
496 /* stdin special case. NULL means "stdin" for wpp. */
497 if (input_files.count == 0 && load_file( NULL, output_name )) exit(1);
499 if(debuglevel & DEBUGLEVEL_DUMP)
500 dump_resources(resource_top);
502 if(verify_translations_mode)
504 verify_translations(resource_top);
505 exit(0);
507 if (!po_mode && output_name)
509 if (strendswith( output_name, ".po" )) po_mode = 1;
510 else if (strendswith( output_name, ".pot" )) po_mode = 2;
512 if (po_mode)
514 if (!win32) error( "PO files are not supported in 16-bit mode\n" );
515 if (po_mode == 2) /* pot file */
517 if (!output_name)
519 const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
520 output_name = replace_extension( name, ".rc", ".pot" );
522 write_pot_file( output_name );
524 else write_po_files( output_name );
525 output_name = NULL;
526 exit(0);
528 if (win32) add_translations( po_dir );
530 chat("Writing .res-file\n");
531 if (!output_name)
533 const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
534 output_name = replace_extension( name, ".rc", ".res" );
536 write_resfile(output_name, resource_top);
537 output_name = NULL;
539 return 0;
543 static void cleanup_files(void)
545 if (output_name) unlink(output_name);
546 if (temp_name) unlink(temp_name);