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
23 #include "wine/port.h"
47 #ifdef WORDS_BIGENDIAN
50 #define ENDIAN "little"
53 static const char usage[] =
54 "Usage: wrc [options...] [infile[.rc|.res]]\n"
55 " -b, --target=TARGET Specify target CPU and platform when cross-compiling\n"
56 " -D, --define id[=val] Define preprocessor identifier id=val\n"
57 " --debug=nn Set debug level to 'nn'\n"
58 " -E Preprocess only\n"
59 " --endianness=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
60 " (win32 only; default is " ENDIAN "-endian)\n"
61 " -F TARGET Synonym for -b for compatibility with windres\n"
62 " -fo FILE Synonym for -o for compatibility with windres\n"
63 " -h, --help Prints this summary\n"
64 " -i, --input=FILE The name of the input file\n"
65 " -I, --include-dir=PATH Set include search dir to path (multiple -I allowed)\n"
66 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
67 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
68 " -m16, -m32, -m64 Build for 16-bit, 32-bit resp. 64-bit platforms\n"
69 " --no-use-temp-file Ignored for compatibility with windres\n"
70 " --nostdinc Disables searching the standard include path\n"
71 " -o, --output=FILE Output to file (default is infile.res)\n"
72 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
73 " --pedantic Enable pedantic warnings\n"
74 " --po-dir=DIR Directory containing po files for translations\n"
75 " --preprocessor Specifies the preprocessor to use, including arguments\n"
76 " -r Ignored for compatibility with rc\n"
77 " -U, --undefine id Undefine preprocessor identifier id\n"
78 " --use-temp-file Ignored for compatibility with windres\n"
79 " -v, --verbose Enable verbose mode\n"
80 " --verify-translations Check the status of the various translations\n"
81 " --version Print version and exit\n"
82 "Input is taken from stdin if no sourcefile specified.\n"
83 "Debug level 'n' is a bitmask with following meaning:\n"
84 " * 0x01 Tell which resource is parsed (verbose mode)\n"
85 " * 0x02 Dump internal structures\n"
86 " * 0x04 Create a parser trace (yydebug=1)\n"
87 " * 0x08 Preprocessor messages\n"
88 " * 0x10 Preprocessor lex messages\n"
89 " * 0x20 Preprocessor yacc trace\n"
90 "If no input filename is given and the output name is not overridden\n"
91 "with -o, then the output is written to \"wrc.tab.res\"\n"
94 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
95 "Copyright 1998-2000 Bertho A. Stultiens\n"
96 " 1994 Martin von Loewis\n";
99 * Set if compiling in 32bit mode (default).
104 * debuglevel == DEBUGLEVEL_NONE Don't bother
105 * debuglevel & DEBUGLEVEL_CHAT Say what's done
106 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
107 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
108 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
109 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
110 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
112 int debuglevel = DEBUGLEVEL_NONE;
115 * Recognize win32 keywords if set (-w 32 enforces this),
116 * otherwise set with -e option.
121 * Language setting for resources (-l option)
123 static language_t *defaultlanguage;
124 language_t *currentlanguage = NULL;
127 * Set when extra warnings should be generated (-W option)
132 * The output byte-order of resources (set with -B)
134 int byteorder = WRC_BO_NATIVE;
137 * Set when _only_ to run the preprocessor (-E option)
139 int preprocess_only = 0;
142 * Set when _not_ to run the preprocessor (-P cat option)
144 int no_preprocess = 0;
146 int check_utf8 = 1; /* whether to check for valid utf8 */
148 static int pointer_size = sizeof(void *);
150 static int verify_translations_mode;
152 static char *output_name; /* The name given by the -o option */
153 char *input_name = NULL; /* The name given on the command-line */
154 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
156 int line_number = 1; /* The current line */
157 int char_number = 1; /* The current char pos within the line */
159 char *cmdline; /* The entire commandline */
160 time_t now; /* The time of start of wrc */
162 int parser_debug, yy_flex_debug;
164 resource_t *resource_top; /* The top of the parsed resources */
166 int getopt (int argc, char *const *argv, const char *optstring);
167 static void cleanup_files(void);
168 static void segvhandler(int sig);
170 enum long_options_values
172 LONG_OPT_NOSTDINC = 1,
176 LONG_OPT_PREPROCESSOR,
181 LONG_OPT_VERIFY_TRANSL
184 static const char short_options[] =
185 "b:D:Ef:F:hi:I:J:l:m:o:O:rU:v";
186 static const struct option long_options[] = {
187 { "debug", 1, NULL, LONG_OPT_DEBUG },
188 { "define", 1, NULL, 'D' },
189 { "endianness", 1, NULL, LONG_OPT_ENDIANNESS },
190 { "help", 0, NULL, 'h' },
191 { "include-dir", 1, NULL, 'I' },
192 { "input", 1, NULL, 'i' },
193 { "input-format", 1, NULL, 'J' },
194 { "language", 1, NULL, 'l' },
195 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
196 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
197 { "output", 1, NULL, 'o' },
198 { "output-format", 1, NULL, 'O' },
199 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
200 { "po-dir", 1, NULL, LONG_OPT_PO_DIR },
201 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
202 { "target", 1, NULL, 'F' },
203 { "undefine", 1, NULL, 'U' },
204 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
205 { "verbose", 0, NULL, 'v' },
206 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
207 { "version", 0, NULL, LONG_OPT_VERSION },
211 static void set_version_defines(void)
213 char *version = xstrdup( PACKAGE_VERSION );
214 char *major, *minor, *patchlevel;
217 if ((minor = strchr( version, '.' )))
221 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
223 else /* pre 0.9 version */
226 patchlevel = version;
228 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
229 wpp_add_cmdline_define(buffer);
230 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
231 wpp_add_cmdline_define(buffer);
232 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
233 wpp_add_cmdline_define(buffer);
237 /* clean things up when aborting on a signal */
238 static void exit_on_signal( int sig )
240 exit(1); /* this will call the atexit functions */
243 /* load a single input file */
244 static int load_file( const char *input_name, const char *output_name )
248 /* Run the preprocessor on the input */
256 * Preprocess the input to a temp-file, or stdout if
257 * no output was given.
264 if (!(output = fopen( output_name, "w" )))
265 fatal_perror( "Could not open %s for writing", output_name );
266 ret = wpp_parse( input_name, output );
269 else ret = wpp_parse( input_name, stdout );
276 if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
277 else name = xstrdup( "wrc.XXXXXX" );
279 if ((fd = mkstemps( name, 0 )) == -1)
280 error("Could not generate a temp name from %s\n", name);
283 if (!(output = fdopen(fd, "wt")))
284 error("Could not open fd %s for writing\n", name);
286 ret = wpp_parse( input_name, output );
292 /* Reset the language */
293 currentlanguage = dup_language( defaultlanguage );
296 /* Go from .rc to .res */
297 chat("Starting parse\n");
299 if(!(parser_in = fopen(input_name, "rb")))
300 fatal_perror("Could not open %s for input", input_name);
302 ret = parser_parse();
304 parser_lex_destroy();
310 free( currentlanguage );
314 static void set_target( const char *target )
316 char *p, *cpu = xstrdup( target );
318 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
319 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
321 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) || !strcmp( cpu, "ia64" ))
328 int main(int argc,char *argv[])
341 char **files = xmalloc( argc * sizeof(*files) );
343 signal(SIGSEGV, segvhandler);
344 signal( SIGTERM, exit_on_signal );
345 signal( SIGINT, exit_on_signal );
347 signal( SIGHUP, exit_on_signal );
352 /* Set the default defined stuff */
353 set_version_defines();
354 wpp_add_cmdline_define("RC_INVOKED=1");
355 /* Microsoft RC always searches current directory */
356 wpp_add_include_path(".");
358 /* First rebuild the commandline to put in destination */
359 /* Could be done through env[], but not all OS-es support it */
360 cmdlen = 4; /* for "wrc " */
361 for(i = 1; i < argc; i++)
362 cmdlen += strlen(argv[i]) + 1;
363 cmdline = xmalloc(cmdlen);
364 strcpy(cmdline, "wrc ");
365 for(i = 1; i < argc; i++)
367 strcat(cmdline, argv[i]);
369 strcat(cmdline, " ");
372 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
376 case LONG_OPT_NOSTDINC:
379 case LONG_OPT_TMPFILE:
380 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
382 case LONG_OPT_NOTMPFILE:
383 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
385 case LONG_OPT_PO_DIR:
386 po_dir = xstrdup( optarg );
388 case LONG_OPT_PREPROCESSOR:
389 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
390 else fprintf(stderr, "-P option not yet supported, ignored.\n");
392 case LONG_OPT_VERSION:
393 printf(version_string);
397 debuglevel = strtol(optarg, NULL, 0);
399 case LONG_OPT_ENDIANNESS:
404 byteorder = WRC_BO_NATIVE;
408 byteorder = WRC_BO_LITTLE;
412 byteorder = WRC_BO_BIG;
415 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
419 case LONG_OPT_PEDANTIC:
423 case LONG_OPT_VERIFY_TRANSL:
424 verify_translations_mode = 1;
427 wpp_add_cmdline_define(optarg);
434 set_target( optarg );
440 files[nb_files++] = optarg;
443 wpp_add_include_path(optarg);
446 if (strcmp(optarg, "rc16") == 0) extensions = 0;
447 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
452 lan = strtol(optarg, NULL, 0);
453 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
454 error("Language %04x is not supported\n", lan);
455 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
459 if (!strcmp( optarg, "16" )) win32 = 0;
460 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
461 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
462 else error( "Invalid option: -m%s\n", optarg );
465 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
469 if (!output_name) output_name = strdup(optarg);
470 else error("Too many output files.\n");
473 if (strcmp(optarg, "po") == 0) po_mode = 1;
474 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
475 else if (strcmp(optarg, "res16") == 0) win32 = 0;
476 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
479 /* ignored for compatibility with rc */
482 wpp_del_define(optarg);
485 debuglevel = DEBUGLEVEL_CHAT;
495 fprintf(stderr, usage);
501 wpp_add_cmdline_define("_WIN32=1");
502 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
505 /* If we do need to search standard includes, add them to the path */
508 wpp_add_include_path(INCLUDEDIR"/msvcrt");
509 wpp_add_include_path(INCLUDEDIR"/windows");
512 /* Kill io buffering when some kind of debuglevel is enabled */
515 setbuf(stdout, NULL);
516 setbuf(stderr, NULL);
519 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
520 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
522 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
523 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
524 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
526 /* Check if the user set a language, else set default */
528 defaultlanguage = new_language(0, 0);
530 atexit(cleanup_files);
532 while (optind < argc) files[nb_files++] = argv[optind++];
534 for (i = 0; i < nb_files; i++)
536 input_name = files[i];
537 if (load_file( input_name, output_name )) exit(1);
539 /* stdin special case. NULL means "stdin" for wpp. */
540 if (nb_files == 0 && load_file( NULL, output_name )) exit(1);
542 if(debuglevel & DEBUGLEVEL_DUMP)
543 dump_resources(resource_top);
545 if(verify_translations_mode)
547 verify_translations(resource_top);
552 if (po_mode == 2) /* pot file */
556 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
557 strcat( output_name, ".pot" );
559 write_pot_file( output_name );
561 else write_po_files( output_name );
565 add_translations( po_dir );
567 /* Convert the internal lists to binary data */
568 resources2res(resource_top);
570 chat("Writing .res-file\n");
573 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
574 strcat(output_name, ".res");
576 write_resfile(output_name, resource_top);
583 static void cleanup_files(void)
585 if (output_name) unlink(output_name);
586 if (temp_name) unlink(temp_name);
589 static void segvhandler(int sig)
591 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);