msxml3: Add IXMLDOMDocument3 stub support.
[wine.git] / tools / wrc / wrc.c
blob614cc840a91e06f6ffff417c732e112d276c945f
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"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #ifdef HAVE_GETOPT_H
35 # include <getopt.h>
36 #endif
38 #include "wrc.h"
39 #include "utils.h"
40 #include "readres.h"
41 #include "dumpres.h"
42 #include "genres.h"
43 #include "newstruc.h"
44 #include "parser.h"
45 #include "wine/wpp.h"
47 #ifdef WORDS_BIGENDIAN
48 #define ENDIAN "big"
49 #else
50 #define ENDIAN "little"
51 #endif
53 static const char usage[] =
54 "Usage: wrc [options...] [infile[.rc|.res]]\n"
55 " -D id[=val] Define preprocessor identifier id=val\n"
56 " -E Preprocess only\n"
57 " -F target Ignored for compatibility with windres\n"
58 " -h Prints this summary\n"
59 " -i file The name of the input file\n"
60 " -I path Set include search dir to path (multiple -I allowed)\n"
61 " -J format The input format (either `rc' or `rc16')\n"
62 " -l lan Set default language to lan (default is neutral {0, 0})\n"
63 " -o file Output to file (default is infile.res)\n"
64 " -O format The output format (either `res' or `res16`)\n"
65 " -r Ignored for compatibility with rc\n"
66 " -U id Undefine preprocessor identifier id\n"
67 " -v Enable verbose mode\n"
68 "The following long options are supported:\n"
69 " --debug=nn Set debug level to 'nn'\n"
70 " --define Synonym for -D\n"
71 " --endianess=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
72 " (win32 only; default is " ENDIAN "-endian)\n"
73 " --help Synonym for -h\n"
74 " --include-dir Synonym for -I\n"
75 " --input Synonym for -i\n"
76 " --input-format Synonym for -J\n"
77 " --language Synonym for -l\n"
78 " --no-use-temp-file Ignored for compatibility with windres\n"
79 " --nostdinc Disables searching the standard include path\n"
80 " --output -fo Synonym for -o\n"
81 " --output-format Synonym for -O\n"
82 " --pedantic Enable pedantic warnings\n"
83 " --preprocessor Specifies the preprocessor to use, including arguments\n"
84 " --target Synonym for -F\n"
85 " --undefine Synonym for -U\n"
86 " --use-temp-file Ignored for compatibility with windres\n"
87 " --verbose Synonym for -v\n"
88 " --verify-translations Check the status of the various translations\n"
89 " --version Print version and exit\n"
90 "Input is taken from stdin if no sourcefile specified.\n"
91 "Debug level 'n' is a bitmask with following meaning:\n"
92 " * 0x01 Tell which resource is parsed (verbose mode)\n"
93 " * 0x02 Dump internal structures\n"
94 " * 0x04 Create a parser trace (yydebug=1)\n"
95 " * 0x08 Preprocessor messages\n"
96 " * 0x10 Preprocessor lex messages\n"
97 " * 0x20 Preprocessor yacc trace\n"
98 "If no input filename is given and the output name is not overridden\n"
99 "with -o, then the output is written to \"wrc.tab.res\"\n"
102 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
103 "Copyright 1998-2000 Bertho A. Stultiens\n"
104 " 1994 Martin von Loewis\n";
107 * Set if compiling in 32bit mode (default).
109 int win32 = 1;
112 * debuglevel == DEBUGLEVEL_NONE Don't bother
113 * debuglevel & DEBUGLEVEL_CHAT Say what's done
114 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
115 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
116 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
117 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
118 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
120 int debuglevel = DEBUGLEVEL_NONE;
123 * Recognize win32 keywords if set (-w 32 enforces this),
124 * otherwise set with -e option.
126 int extensions = 1;
129 * Language setting for resources (-l option)
131 static language_t *defaultlanguage;
132 language_t *currentlanguage = NULL;
135 * Set when extra warnings should be generated (-W option)
137 int pedantic = 0;
140 * The output byte-order of resources (set with -B)
142 int byteorder = WRC_BO_NATIVE;
145 * Set when _only_ to run the preprocessor (-E option)
147 int preprocess_only = 0;
150 * Set when _not_ to run the preprocessor (-P cat option)
152 int no_preprocess = 0;
154 int check_utf8 = 1; /* whether to check for valid utf8 */
156 static int verify_translations_mode;
158 char *output_name = NULL; /* The name given by the -o option */
159 char *input_name = NULL; /* The name given on the command-line */
160 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
162 int line_number = 1; /* The current line */
163 int char_number = 1; /* The current char pos within the line */
165 char *cmdline; /* The entire commandline */
166 time_t now; /* The time of start of wrc */
168 int parser_debug, yy_flex_debug;
170 resource_t *resource_top; /* The top of the parsed resources */
172 int getopt (int argc, char *const *argv, const char *optstring);
173 static void cleanup_files(void);
174 static void segvhandler(int sig);
176 enum long_options_values
178 LONG_OPT_NOSTDINC = 1,
179 LONG_OPT_TMPFILE,
180 LONG_OPT_NOTMPFILE,
181 LONG_OPT_PREPROCESSOR,
182 LONG_OPT_VERSION,
183 LONG_OPT_DEBUG,
184 LONG_OPT_ENDIANESS,
185 LONG_OPT_PEDANTIC,
186 LONG_OPT_VERIFY_TRANSL
189 static const char short_options[] =
190 "D:Ef:F:hi:I:J:l:o:O:rU:v";
191 static const struct option long_options[] = {
192 { "debug", 1, NULL, LONG_OPT_DEBUG },
193 { "define", 1, NULL, 'D' },
194 { "endianess", 1, NULL, LONG_OPT_ENDIANESS },
195 { "help", 0, NULL, 'h' },
196 { "include-dir", 1, NULL, 'I' },
197 { "input", 1, NULL, 'i' },
198 { "input-format", 1, NULL, 'J' },
199 { "language", 1, NULL, 'l' },
200 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
201 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
202 { "output", 1, NULL, 'o' },
203 { "output-format", 1, NULL, 'O' },
204 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
205 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
206 { "target", 1, NULL, 'F' },
207 { "undefine", 1, NULL, 'U' },
208 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
209 { "verbose", 0, NULL, 'v' },
210 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
211 { "version", 0, NULL, LONG_OPT_VERSION },
212 { NULL, 0, NULL, 0 }
215 static void set_version_defines(void)
217 char *version = xstrdup( PACKAGE_VERSION );
218 char *major, *minor, *patchlevel;
219 char buffer[100];
221 if ((minor = strchr( version, '.' )))
223 major = version;
224 *minor++ = 0;
225 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
227 else /* pre 0.9 version */
229 major = NULL;
230 patchlevel = version;
232 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
233 wpp_add_cmdline_define(buffer);
234 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
235 wpp_add_cmdline_define(buffer);
236 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
237 wpp_add_cmdline_define(buffer);
238 free( version );
241 /* clean things up when aborting on a signal */
242 static void exit_on_signal( int sig )
244 exit(1); /* this will call the atexit functions */
247 /* load a single input file */
248 static int load_file( const char *input_name, const char *output_name )
250 int ret;
252 /* Run the preprocessor on the input */
253 if(!no_preprocess)
255 FILE *output;
256 int ret, fd;
257 char *name;
260 * Preprocess the input to a temp-file, or stdout if
261 * no output was given.
264 if (preprocess_only)
266 if (output_name)
268 if (!(output = fopen( output_name, "w" )))
269 fatal_perror( "Could not open %s for writing", output_name );
270 ret = wpp_parse( input_name, output );
271 fclose( output );
273 else ret = wpp_parse( input_name, stdout );
275 if (ret) return ret;
276 output_name = NULL;
277 exit(0);
280 if (output_name && output_name[0])
282 name = xmalloc( strlen(output_name) + 8 );
283 strcpy( name, output_name );
284 strcat( name, ".XXXXXX" );
286 else name = xstrdup( "wrc.XXXXXX" );
288 if ((fd = mkstemps( name, 0 )) == -1)
289 error("Could not generate a temp name from %s\n", name);
291 temp_name = name;
292 if (!(output = fdopen(fd, "wt")))
293 error("Could not open fd %s for writing\n", name);
295 ret = wpp_parse( input_name, output );
296 fclose( output );
297 if (ret) return ret;
298 input_name = name;
301 /* Reset the language */
302 currentlanguage = dup_language( defaultlanguage );
303 check_utf8 = 1;
305 /* Go from .rc to .res */
306 chat("Starting parse\n");
308 if(!(parser_in = fopen(input_name, "rb")))
309 fatal_perror("Could not open %s for input", input_name);
311 ret = parser_parse();
312 fclose(parser_in);
313 parser_lex_destroy();
314 if (temp_name)
316 unlink( temp_name );
317 temp_name = NULL;
319 free( currentlanguage );
320 return ret;
324 int main(int argc,char *argv[])
326 extern char* optarg;
327 extern int optind;
328 int optc;
329 int opti = 0;
330 int stdinc = 1;
331 int lose = 0;
332 int nb_files = 0;
333 int i;
334 int cmdlen;
335 char **files = xmalloc( argc * sizeof(*files) );
337 signal(SIGSEGV, segvhandler);
338 signal( SIGTERM, exit_on_signal );
339 signal( SIGINT, exit_on_signal );
340 #ifdef SIGHUP
341 signal( SIGHUP, exit_on_signal );
342 #endif
344 now = time(NULL);
346 /* Set the default defined stuff */
347 set_version_defines();
348 wpp_add_cmdline_define("RC_INVOKED=1");
349 wpp_add_cmdline_define("__WIN32__=1");
350 wpp_add_cmdline_define("__FLAT__=1");
351 /* Microsoft RC always searches current directory */
352 wpp_add_include_path(".");
354 /* First rebuild the commandline to put in destination */
355 /* Could be done through env[], but not all OS-es support it */
356 cmdlen = 4; /* for "wrc " */
357 for(i = 1; i < argc; i++)
358 cmdlen += strlen(argv[i]) + 1;
359 cmdline = xmalloc(cmdlen);
360 strcpy(cmdline, "wrc ");
361 for(i = 1; i < argc; i++)
363 strcat(cmdline, argv[i]);
364 if(i < argc-1)
365 strcat(cmdline, " ");
368 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
370 switch(optc)
372 case LONG_OPT_NOSTDINC:
373 stdinc = 0;
374 break;
375 case LONG_OPT_TMPFILE:
376 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
377 break;
378 case LONG_OPT_NOTMPFILE:
379 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
380 break;
381 case LONG_OPT_PREPROCESSOR:
382 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
383 else fprintf(stderr, "-P option not yet supported, ignored.\n");
384 break;
385 case LONG_OPT_VERSION:
386 printf(version_string);
387 exit(0);
388 break;
389 case LONG_OPT_DEBUG:
390 debuglevel = strtol(optarg, NULL, 0);
391 break;
392 case LONG_OPT_ENDIANESS:
393 switch(optarg[0])
395 case 'n':
396 case 'N':
397 byteorder = WRC_BO_NATIVE;
398 break;
399 case 'l':
400 case 'L':
401 byteorder = WRC_BO_LITTLE;
402 break;
403 case 'b':
404 case 'B':
405 byteorder = WRC_BO_BIG;
406 break;
407 default:
408 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
409 lose++;
411 break;
412 case LONG_OPT_PEDANTIC:
413 pedantic = 1;
414 wpp_set_pedantic(1);
415 break;
416 case LONG_OPT_VERIFY_TRANSL:
417 verify_translations_mode = 1;
418 break;
419 case 'D':
420 wpp_add_cmdline_define(optarg);
421 break;
422 case 'E':
423 preprocess_only = 1;
424 break;
425 case 'F':
426 /* ignored for compatibility with windres */
427 break;
428 case 'h':
429 printf(usage);
430 exit(0);
431 case 'i':
432 files[nb_files++] = optarg;
433 break;
434 case 'I':
435 wpp_add_include_path(optarg);
436 break;
437 case 'J':
438 if (strcmp(optarg, "rc16") == 0) extensions = 0;
439 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
440 break;
441 case 'l':
443 int lan;
444 lan = strtol(optarg, NULL, 0);
445 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
446 error("Language %04x is not supported\n", lan);
447 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
449 break;
450 case 'f':
451 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
452 optarg++;
453 /* fall through */
454 case 'o':
455 if (!output_name) output_name = strdup(optarg);
456 else error("Too many output files.\n");
457 break;
458 case 'O':
459 if (strcmp(optarg, "res16") == 0)
461 win32 = 0;
462 wpp_del_define("__WIN32__");
463 wpp_del_define("__FLAT__");
465 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
466 break;
467 case 'r':
468 /* ignored for compatibility with rc */
469 break;
470 case 'U':
471 wpp_del_define(optarg);
472 break;
473 case 'v':
474 debuglevel = DEBUGLEVEL_CHAT;
475 break;
476 default:
477 lose++;
478 break;
482 if(lose)
484 fprintf(stderr, usage);
485 return 1;
488 /* If we do need to search standard includes, add them to the path */
489 if (stdinc)
491 wpp_add_include_path(INCLUDEDIR"/msvcrt");
492 wpp_add_include_path(INCLUDEDIR"/windows");
495 /* Kill io buffering when some kind of debuglevel is enabled */
496 if(debuglevel)
498 setbuf(stdout, NULL);
499 setbuf(stderr, NULL);
502 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
503 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
505 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
506 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
507 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
509 /* Check if the user set a language, else set default */
510 if(!defaultlanguage)
511 defaultlanguage = new_language(0, 0);
513 atexit(cleanup_files);
515 while (optind < argc) files[nb_files++] = argv[optind++];
517 for (i = 0; i < nb_files; i++)
519 input_name = files[i];
520 if(!output_name && !preprocess_only)
522 output_name = dup_basename(input_name, ".rc");
523 strcat(output_name, ".res");
525 if (load_file( input_name, output_name )) exit(1);
527 /* stdin special case. NULL means "stdin" for wpp. */
528 if (nb_files == 0)
530 if(!output_name && !preprocess_only)
531 output_name = strdup("wrc.tab.res");
532 if (load_file( NULL, output_name )) exit(1);
535 if(debuglevel & DEBUGLEVEL_DUMP)
536 dump_resources(resource_top);
538 if(verify_translations_mode)
540 verify_translations(resource_top);
541 exit(0);
544 /* Convert the internal lists to binary data */
545 resources2res(resource_top);
547 chat("Writing .res-file\n");
548 write_resfile(output_name, resource_top);
549 output_name = NULL;
551 return 0;
555 static void cleanup_files(void)
557 if (output_name) unlink(output_name);
558 if (temp_name) unlink(temp_name);
561 static void segvhandler(int sig)
563 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
564 fflush(stdout);
565 fflush(stderr);
566 abort();