push ca931951dc5f3f050707cce013d2412130f45f11
[wine/hacks.git] / tools / wrc / wrc.c
blob61a37347ceec258e0550a64363b48dfbf6f6a17e
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 #ifndef INCLUDEDIR
48 #define INCLUDEDIR "/usr/local/include/wine"
49 #endif
51 #ifdef WORDS_BIGENDIAN
52 #define ENDIAN "big"
53 #else
54 #define ENDIAN "little"
55 #endif
57 static const char usage[] =
58 "Usage: wrc [options...] [infile[.rc|.res]]\n"
59 " -D id[=val] Define preprocessor identifier id=val\n"
60 " -E Preprocess only\n"
61 " -F target Ignored for compatibility with windres\n"
62 " -h Prints this summary\n"
63 " -i file The name of the input file\n"
64 " -I path Set include search dir to path (multiple -I allowed)\n"
65 " -J format The input format (either `rc' or `rc16')\n"
66 " -l lan Set default language to lan (default is neutral {0, 0})\n"
67 " -o file Output to file (default is infile.res)\n"
68 " -O format The output format (either `res' or `res16`)\n"
69 " -r Ignored for compatibility with rc\n"
70 " -U id Undefine preprocessor identifier id\n"
71 " -v Enable verbose mode\n"
72 "The following long options are supported:\n"
73 " --debug=nn Set debug level to 'nn'\n"
74 " --define Synonym for -D\n"
75 " --endianess=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
76 " (win32 only; default is " ENDIAN "-endian)\n"
77 " --help Synonym for -h\n"
78 " --include-dir Synonym for -I\n"
79 " --input Synonym for -i\n"
80 " --input-format Synonym for -J\n"
81 " --language Synonym for -l\n"
82 " --no-use-temp-file Ignored for compatibility with windres\n"
83 " --nostdinc Disables searching the standard include path\n"
84 " --output -fo Synonym for -o\n"
85 " --output-format Synonym for -O\n"
86 " --pedantic Enable pedantic warnings\n"
87 " --preprocessor Specifies the preprocessor to use, including arguments\n"
88 " --target Synonym for -F\n"
89 " --undefine Synonym for -U\n"
90 " --use-temp-file Ignored for compatibility with windres\n"
91 " --verbose Synonym for -v\n"
92 " --verify-translations Check the status of the various translations\n"
93 " --version Print version and exit\n"
94 "Input is taken from stdin if no sourcefile specified.\n"
95 "Debug level 'n' is a bitmask with following meaning:\n"
96 " * 0x01 Tell which resource is parsed (verbose mode)\n"
97 " * 0x02 Dump internal structures\n"
98 " * 0x04 Create a parser trace (yydebug=1)\n"
99 " * 0x08 Preprocessor messages\n"
100 " * 0x10 Preprocessor lex messages\n"
101 " * 0x20 Preprocessor yacc trace\n"
102 "If no input filename is given and the output name is not overridden\n"
103 "with -o, then the output is written to \"wrc.tab.res\"\n"
106 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
107 "Copyright 1998-2000 Bertho A. Stultiens\n"
108 " 1994 Martin von Loewis\n";
111 * Set if compiling in 32bit mode (default).
113 int win32 = 1;
116 * debuglevel == DEBUGLEVEL_NONE Don't bother
117 * debuglevel & DEBUGLEVEL_CHAT Say what's done
118 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
119 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
120 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
121 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
122 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
124 int debuglevel = DEBUGLEVEL_NONE;
127 * Recognize win32 keywords if set (-w 32 enforces this),
128 * otherwise set with -e option.
130 int extensions = 1;
133 * Language setting for resources (-l option)
135 language_t *currentlanguage = NULL;
138 * Set when extra warnings should be generated (-W option)
140 int pedantic = 0;
143 * The output byte-order of resources (set with -B)
145 int byteorder = WRC_BO_NATIVE;
148 * Set when _only_ to run the preprocessor (-E option)
150 int preprocess_only = 0;
153 * Set when _not_ to run the preprocessor (-P cat option)
155 int no_preprocess = 0;
157 static int verify_translations_mode;
159 char *output_name = NULL; /* The name given by the -o option */
160 char *input_name = NULL; /* The name given on the command-line */
161 char *temp_name = NULL; /* Temporary file for preprocess pipe */
163 int line_number = 1; /* The current line */
164 int char_number = 1; /* The current char pos within the line */
166 char *cmdline; /* The entire commandline */
167 time_t now; /* The time of start of wrc */
169 int parser_debug, yy_flex_debug;
171 resource_t *resource_top; /* The top of the parsed resources */
173 int getopt (int argc, char *const *argv, const char *optstring);
174 static void cleanup_files(void);
175 static void segvhandler(int sig);
177 enum long_options_values
179 LONG_OPT_NOSTDINC = 1,
180 LONG_OPT_TMPFILE,
181 LONG_OPT_NOTMPFILE,
182 LONG_OPT_PREPROCESSOR,
183 LONG_OPT_VERSION,
184 LONG_OPT_DEBUG,
185 LONG_OPT_ENDIANESS,
186 LONG_OPT_PEDANTIC,
187 LONG_OPT_VERIFY_TRANSL
190 static const char short_options[] =
191 "D:Ef:F:hi:I:J:l:o:O:rU:v";
192 static const struct option long_options[] = {
193 { "debug", 1, 0, LONG_OPT_DEBUG },
194 { "define", 1, 0, 'D' },
195 { "endianess", 1, 0, LONG_OPT_ENDIANESS },
196 { "help", 0, 0, 'h' },
197 { "include-dir", 1, 0, 'I' },
198 { "input", 1, 0, 'i' },
199 { "input-format", 1, 0, 'J' },
200 { "language", 1, 0, 'l' },
201 { "no-use-temp-file", 0, 0, LONG_OPT_NOTMPFILE },
202 { "nostdinc", 0, 0, LONG_OPT_NOSTDINC },
203 { "output", 1, 0, 'o' },
204 { "output-format", 1, 0, 'O' },
205 { "pedantic", 0, 0, LONG_OPT_PEDANTIC },
206 { "preprocessor", 1, 0, LONG_OPT_PREPROCESSOR },
207 { "target", 1, 0, 'F' },
208 { "undefine", 1, 0, 'U' },
209 { "use-temp-file", 0, 0, LONG_OPT_TMPFILE },
210 { "verbose", 0, 0, 'v' },
211 { "verify-translations", 0, 0, LONG_OPT_VERIFY_TRANSL },
212 { "version", 0, 0, LONG_OPT_VERSION },
213 { 0, 0, 0, 0 }
216 static void set_version_defines(void)
218 char *version = xstrdup( PACKAGE_VERSION );
219 char *major, *minor, *patchlevel;
220 char buffer[100];
222 if ((minor = strchr( version, '.' )))
224 major = version;
225 *minor++ = 0;
226 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
228 else /* pre 0.9 version */
230 major = NULL;
231 patchlevel = version;
233 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
234 wpp_add_cmdline_define(buffer);
235 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
236 wpp_add_cmdline_define(buffer);
237 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
238 wpp_add_cmdline_define(buffer);
239 free( version );
242 /* clean things up when aborting on a signal */
243 static void exit_on_signal( int sig )
245 exit(1); /* this will call the atexit functions */
248 /* load a single input file */
249 static int load_file( const char *input_name, const char *output_name )
251 int ret;
253 /* Run the preprocessor on the input */
254 if(!no_preprocess)
257 * Preprocess the input to a temp-file, or stdout if
258 * no output was given.
261 chat("Starting preprocess\n");
263 if (!preprocess_only)
265 ret = wpp_parse_temp( input_name, output_name, &temp_name );
267 else if (output_name)
269 FILE *output;
271 if (!(output = fopen( output_name, "w" )))
272 fatal_perror( "Could not open %s for writing", output_name );
273 ret = wpp_parse( input_name, output );
274 fclose( output );
276 else
278 ret = wpp_parse( input_name, stdout );
281 if (ret) return ret;
283 if(preprocess_only)
285 output_name = NULL;
286 exit(0);
289 input_name = temp_name;
292 /* Go from .rc to .res */
293 chat("Starting parse\n");
295 if(!(parser_in = fopen(input_name, "rb")))
296 fatal_perror("Could not open %s for input", input_name);
298 ret = parser_parse();
299 fclose(parser_in);
300 if (temp_name)
302 unlink( temp_name );
303 temp_name = NULL;
305 return ret;
309 int main(int argc,char *argv[])
311 extern char* optarg;
312 extern int optind;
313 int optc;
314 int opti = 0;
315 int stdinc = 1;
316 int lose = 0;
317 int nb_files = 0;
318 int i;
319 int cmdlen;
320 char **files = xmalloc( argc * sizeof(*files) );
322 signal(SIGSEGV, segvhandler);
323 signal( SIGTERM, exit_on_signal );
324 signal( SIGINT, exit_on_signal );
325 #ifdef SIGHUP
326 signal( SIGHUP, exit_on_signal );
327 #endif
329 now = time(NULL);
331 /* Set the default defined stuff */
332 set_version_defines();
333 wpp_add_cmdline_define("RC_INVOKED=1");
334 wpp_add_cmdline_define("__WIN32__=1");
335 wpp_add_cmdline_define("__FLAT__=1");
336 /* Microsoft RC always searches current directory */
337 wpp_add_include_path(".");
339 /* First rebuild the commandline to put in destination */
340 /* Could be done through env[], but not all OS-es support it */
341 cmdlen = 4; /* for "wrc " */
342 for(i = 1; i < argc; i++)
343 cmdlen += strlen(argv[i]) + 1;
344 cmdline = xmalloc(cmdlen);
345 strcpy(cmdline, "wrc ");
346 for(i = 1; i < argc; i++)
348 strcat(cmdline, argv[i]);
349 if(i < argc-1)
350 strcat(cmdline, " ");
353 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
355 switch(optc)
357 case LONG_OPT_NOSTDINC:
358 stdinc = 0;
359 break;
360 case LONG_OPT_TMPFILE:
361 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
362 break;
363 case LONG_OPT_NOTMPFILE:
364 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
365 break;
366 case LONG_OPT_PREPROCESSOR:
367 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
368 else fprintf(stderr, "-P option not yet supported, ignored.\n");
369 break;
370 case LONG_OPT_VERSION:
371 printf(version_string);
372 exit(0);
373 break;
374 case LONG_OPT_DEBUG:
375 debuglevel = strtol(optarg, NULL, 0);
376 break;
377 case LONG_OPT_ENDIANESS:
378 switch(optarg[0])
380 case 'n':
381 case 'N':
382 byteorder = WRC_BO_NATIVE;
383 break;
384 case 'l':
385 case 'L':
386 byteorder = WRC_BO_LITTLE;
387 break;
388 case 'b':
389 case 'B':
390 byteorder = WRC_BO_BIG;
391 break;
392 default:
393 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
394 lose++;
396 break;
397 case LONG_OPT_PEDANTIC:
398 pedantic = 1;
399 wpp_set_pedantic(1);
400 break;
401 case LONG_OPT_VERIFY_TRANSL:
402 verify_translations_mode = 1;
403 break;
404 case 'D':
405 wpp_add_cmdline_define(optarg);
406 break;
407 case 'E':
408 preprocess_only = 1;
409 break;
410 case 'F':
411 /* ignored for compatibility with windres */
412 break;
413 case 'h':
414 printf(usage);
415 exit(0);
416 case 'i':
417 files[nb_files++] = optarg;
418 break;
419 case 'I':
420 wpp_add_include_path(optarg);
421 break;
422 case 'J':
423 if (strcmp(optarg, "rc16") == 0) extensions = 0;
424 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
425 break;
426 case 'l':
428 int lan;
429 lan = strtol(optarg, NULL, 0);
430 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
431 error("Language %04x is not supported\n", lan);
432 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
434 break;
435 case 'f':
436 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
437 optarg++;
438 /* fall through */
439 case 'o':
440 if (!output_name) output_name = strdup(optarg);
441 else error("Too many output files.\n");
442 break;
443 case 'O':
444 if (strcmp(optarg, "res16") == 0)
446 win32 = 0;
447 wpp_del_define("__WIN32__");
448 wpp_del_define("__FLAT__");
450 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
451 break;
452 case 'r':
453 /* ignored for compatibility with rc */
454 break;
455 case 'U':
456 wpp_del_define(optarg);
457 break;
458 case 'v':
459 debuglevel = DEBUGLEVEL_CHAT;
460 break;
461 default:
462 lose++;
463 break;
467 if(lose)
469 fprintf(stderr, usage);
470 return 1;
473 /* If we do need to search standard includes, add them to the path */
474 if (stdinc)
476 wpp_add_include_path(INCLUDEDIR"/msvcrt");
477 wpp_add_include_path(INCLUDEDIR"/windows");
480 /* Kill io buffering when some kind of debuglevel is enabled */
481 if(debuglevel)
483 setbuf(stdout,0);
484 setbuf(stderr,0);
487 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
488 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
490 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
491 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
492 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
494 /* Check if the user set a language, else set default */
495 if(!currentlanguage)
496 currentlanguage = new_language(0, 0);
498 atexit(cleanup_files);
500 while (optind < argc) files[nb_files++] = argv[optind++];
502 for (i = 0; i < nb_files; i++)
504 input_name = files[i];
505 if(!output_name && !preprocess_only)
507 output_name = dup_basename(input_name, ".rc");
508 strcat(output_name, ".res");
510 if (load_file( input_name, output_name )) exit(1);
513 if(debuglevel & DEBUGLEVEL_DUMP)
514 dump_resources(resource_top);
516 if(verify_translations_mode)
518 verify_translations(resource_top);
519 exit(0);
522 /* Convert the internal lists to binary data */
523 resources2res(resource_top);
525 chat("Writing .res-file\n");
526 write_resfile(output_name, resource_top);
527 output_name = NULL;
529 return 0;
533 static void cleanup_files(void)
535 if (output_name) unlink(output_name);
536 if (temp_name) unlink(temp_name);
539 static void segvhandler(int sig)
541 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
542 fflush(stdout);
543 fflush(stderr);
544 abort();