Release 1.3.7.
[wine/gsoc-2012-control.git] / tools / wrc / wrc.c
blob99285dbc991adc1f0f57d2ecb6bab4cd8a643f85
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 " -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 " --endianess=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 (either `res' or `res16`)\n"
73 " --pedantic Enable pedantic warnings\n"
74 " --preprocessor Specifies the preprocessor to use, including arguments\n"
75 " -r Ignored for compatibility with rc\n"
76 " -U, --undefine id Undefine preprocessor identifier id\n"
77 " --use-temp-file Ignored for compatibility with windres\n"
78 " -v, --verbose Enable verbose mode\n"
79 " --verify-translations Check the status of the various translations\n"
80 " --version Print version and exit\n"
81 "Input is taken from stdin if no sourcefile specified.\n"
82 "Debug level 'n' is a bitmask with following meaning:\n"
83 " * 0x01 Tell which resource is parsed (verbose mode)\n"
84 " * 0x02 Dump internal structures\n"
85 " * 0x04 Create a parser trace (yydebug=1)\n"
86 " * 0x08 Preprocessor messages\n"
87 " * 0x10 Preprocessor lex messages\n"
88 " * 0x20 Preprocessor yacc trace\n"
89 "If no input filename is given and the output name is not overridden\n"
90 "with -o, then the output is written to \"wrc.tab.res\"\n"
93 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
94 "Copyright 1998-2000 Bertho A. Stultiens\n"
95 " 1994 Martin von Loewis\n";
98 * Set if compiling in 32bit mode (default).
100 int win32 = 1;
103 * debuglevel == DEBUGLEVEL_NONE Don't bother
104 * debuglevel & DEBUGLEVEL_CHAT Say what's done
105 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
106 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
107 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
108 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
109 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
111 int debuglevel = DEBUGLEVEL_NONE;
114 * Recognize win32 keywords if set (-w 32 enforces this),
115 * otherwise set with -e option.
117 int extensions = 1;
120 * Language setting for resources (-l option)
122 static language_t *defaultlanguage;
123 language_t *currentlanguage = NULL;
126 * Set when extra warnings should be generated (-W option)
128 int pedantic = 0;
131 * The output byte-order of resources (set with -B)
133 int byteorder = WRC_BO_NATIVE;
136 * Set when _only_ to run the preprocessor (-E option)
138 int preprocess_only = 0;
141 * Set when _not_ to run the preprocessor (-P cat option)
143 int no_preprocess = 0;
145 int check_utf8 = 1; /* whether to check for valid utf8 */
147 static int pointer_size = sizeof(void *);
149 static int verify_translations_mode;
151 char *output_name = NULL; /* The name given by the -o option */
152 char *input_name = NULL; /* The name given on the command-line */
153 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
155 int line_number = 1; /* The current line */
156 int char_number = 1; /* The current char pos within the line */
158 char *cmdline; /* The entire commandline */
159 time_t now; /* The time of start of wrc */
161 int parser_debug, yy_flex_debug;
163 resource_t *resource_top; /* The top of the parsed resources */
165 int getopt (int argc, char *const *argv, const char *optstring);
166 static void cleanup_files(void);
167 static void segvhandler(int sig);
169 enum long_options_values
171 LONG_OPT_NOSTDINC = 1,
172 LONG_OPT_TMPFILE,
173 LONG_OPT_NOTMPFILE,
174 LONG_OPT_PREPROCESSOR,
175 LONG_OPT_VERSION,
176 LONG_OPT_DEBUG,
177 LONG_OPT_ENDIANESS,
178 LONG_OPT_PEDANTIC,
179 LONG_OPT_VERIFY_TRANSL
182 static const char short_options[] =
183 "b:D:Ef:F:hi:I:J:l:m:o:O:rU:v";
184 static const struct option long_options[] = {
185 { "debug", 1, NULL, LONG_OPT_DEBUG },
186 { "define", 1, NULL, 'D' },
187 { "endianess", 1, NULL, LONG_OPT_ENDIANESS },
188 { "help", 0, NULL, 'h' },
189 { "include-dir", 1, NULL, 'I' },
190 { "input", 1, NULL, 'i' },
191 { "input-format", 1, NULL, 'J' },
192 { "language", 1, NULL, 'l' },
193 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
194 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
195 { "output", 1, NULL, 'o' },
196 { "output-format", 1, NULL, 'O' },
197 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
198 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
199 { "target", 1, NULL, 'F' },
200 { "undefine", 1, NULL, 'U' },
201 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
202 { "verbose", 0, NULL, 'v' },
203 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
204 { "version", 0, NULL, LONG_OPT_VERSION },
205 { NULL, 0, NULL, 0 }
208 static void set_version_defines(void)
210 char *version = xstrdup( PACKAGE_VERSION );
211 char *major, *minor, *patchlevel;
212 char buffer[100];
214 if ((minor = strchr( version, '.' )))
216 major = version;
217 *minor++ = 0;
218 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
220 else /* pre 0.9 version */
222 major = NULL;
223 patchlevel = version;
225 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
226 wpp_add_cmdline_define(buffer);
227 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
228 wpp_add_cmdline_define(buffer);
229 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
230 wpp_add_cmdline_define(buffer);
231 free( version );
234 /* clean things up when aborting on a signal */
235 static void exit_on_signal( int sig )
237 exit(1); /* this will call the atexit functions */
240 /* load a single input file */
241 static int load_file( const char *input_name, const char *output_name )
243 int ret;
245 /* Run the preprocessor on the input */
246 if(!no_preprocess)
248 FILE *output;
249 int ret, fd;
250 char *name;
253 * Preprocess the input to a temp-file, or stdout if
254 * no output was given.
257 if (preprocess_only)
259 if (output_name)
261 if (!(output = fopen( output_name, "w" )))
262 fatal_perror( "Could not open %s for writing", output_name );
263 ret = wpp_parse( input_name, output );
264 fclose( output );
266 else ret = wpp_parse( input_name, stdout );
268 if (ret) return ret;
269 output_name = NULL;
270 exit(0);
273 if (output_name && output_name[0])
275 name = xmalloc( strlen(output_name) + 8 );
276 strcpy( name, output_name );
277 strcat( name, ".XXXXXX" );
279 else name = xstrdup( "wrc.XXXXXX" );
281 if ((fd = mkstemps( name, 0 )) == -1)
282 error("Could not generate a temp name from %s\n", name);
284 temp_name = name;
285 if (!(output = fdopen(fd, "wt")))
286 error("Could not open fd %s for writing\n", name);
288 ret = wpp_parse( input_name, output );
289 fclose( output );
290 if (ret) return ret;
291 input_name = name;
294 /* Reset the language */
295 currentlanguage = dup_language( defaultlanguage );
296 check_utf8 = 1;
298 /* Go from .rc to .res */
299 chat("Starting parse\n");
301 if(!(parser_in = fopen(input_name, "rb")))
302 fatal_perror("Could not open %s for input", input_name);
304 ret = parser_parse();
305 fclose(parser_in);
306 parser_lex_destroy();
307 if (temp_name)
309 unlink( temp_name );
310 temp_name = NULL;
312 free( currentlanguage );
313 return ret;
316 static void set_target( const char *target )
318 char *p, *cpu = xstrdup( target );
320 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
321 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
322 *p = 0;
323 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) || !strcmp( cpu, "ia64" ))
324 pointer_size = 8;
325 else
326 pointer_size = 4;
327 free( cpu );
330 int main(int argc,char *argv[])
332 extern char* optarg;
333 extern int optind;
334 int optc;
335 int opti = 0;
336 int stdinc = 1;
337 int lose = 0;
338 int nb_files = 0;
339 int i;
340 int cmdlen;
341 char **files = xmalloc( argc * sizeof(*files) );
343 signal(SIGSEGV, segvhandler);
344 signal( SIGTERM, exit_on_signal );
345 signal( SIGINT, exit_on_signal );
346 #ifdef SIGHUP
347 signal( SIGHUP, exit_on_signal );
348 #endif
350 now = time(NULL);
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]);
368 if(i < argc-1)
369 strcat(cmdline, " ");
372 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
374 switch(optc)
376 case LONG_OPT_NOSTDINC:
377 stdinc = 0;
378 break;
379 case LONG_OPT_TMPFILE:
380 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
381 break;
382 case LONG_OPT_NOTMPFILE:
383 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
384 break;
385 case LONG_OPT_PREPROCESSOR:
386 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
387 else fprintf(stderr, "-P option not yet supported, ignored.\n");
388 break;
389 case LONG_OPT_VERSION:
390 printf(version_string);
391 exit(0);
392 break;
393 case LONG_OPT_DEBUG:
394 debuglevel = strtol(optarg, NULL, 0);
395 break;
396 case LONG_OPT_ENDIANESS:
397 switch(optarg[0])
399 case 'n':
400 case 'N':
401 byteorder = WRC_BO_NATIVE;
402 break;
403 case 'l':
404 case 'L':
405 byteorder = WRC_BO_LITTLE;
406 break;
407 case 'b':
408 case 'B':
409 byteorder = WRC_BO_BIG;
410 break;
411 default:
412 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
413 lose++;
415 break;
416 case LONG_OPT_PEDANTIC:
417 pedantic = 1;
418 wpp_set_pedantic(1);
419 break;
420 case LONG_OPT_VERIFY_TRANSL:
421 verify_translations_mode = 1;
422 break;
423 case 'D':
424 wpp_add_cmdline_define(optarg);
425 break;
426 case 'E':
427 preprocess_only = 1;
428 break;
429 case 'b':
430 case 'F':
431 set_target( optarg );
432 break;
433 case 'h':
434 printf(usage);
435 exit(0);
436 case 'i':
437 files[nb_files++] = optarg;
438 break;
439 case 'I':
440 wpp_add_include_path(optarg);
441 break;
442 case 'J':
443 if (strcmp(optarg, "rc16") == 0) extensions = 0;
444 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
445 break;
446 case 'l':
448 int lan;
449 lan = strtol(optarg, NULL, 0);
450 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
451 error("Language %04x is not supported\n", lan);
452 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
454 break;
455 case 'm':
456 if (!strcmp( optarg, "16" )) win32 = 0;
457 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
458 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
459 else error( "Invalid option: -m%s\n", optarg );
460 break;
461 case 'f':
462 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
463 optarg++;
464 /* fall through */
465 case 'o':
466 if (!output_name) output_name = strdup(optarg);
467 else error("Too many output files.\n");
468 break;
469 case 'O':
470 if (strcmp(optarg, "res16") == 0) win32 = 0;
471 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
472 break;
473 case 'r':
474 /* ignored for compatibility with rc */
475 break;
476 case 'U':
477 wpp_del_define(optarg);
478 break;
479 case 'v':
480 debuglevel = DEBUGLEVEL_CHAT;
481 break;
482 default:
483 lose++;
484 break;
488 if(lose)
490 fprintf(stderr, usage);
491 return 1;
494 if (win32)
496 wpp_add_cmdline_define("_WIN32=1");
497 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
500 /* If we do need to search standard includes, add them to the path */
501 if (stdinc)
503 wpp_add_include_path(INCLUDEDIR"/msvcrt");
504 wpp_add_include_path(INCLUDEDIR"/windows");
507 /* Kill io buffering when some kind of debuglevel is enabled */
508 if(debuglevel)
510 setbuf(stdout, NULL);
511 setbuf(stderr, NULL);
514 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
515 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
517 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
518 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
519 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
521 /* Check if the user set a language, else set default */
522 if(!defaultlanguage)
523 defaultlanguage = new_language(0, 0);
525 atexit(cleanup_files);
527 while (optind < argc) files[nb_files++] = argv[optind++];
529 for (i = 0; i < nb_files; i++)
531 input_name = files[i];
532 if(!output_name && !preprocess_only)
534 output_name = dup_basename(input_name, ".rc");
535 strcat(output_name, ".res");
537 if (load_file( input_name, output_name )) exit(1);
539 /* stdin special case. NULL means "stdin" for wpp. */
540 if (nb_files == 0)
542 if(!output_name && !preprocess_only)
543 output_name = strdup("wrc.tab.res");
544 if (load_file( NULL, output_name )) exit(1);
547 if(debuglevel & DEBUGLEVEL_DUMP)
548 dump_resources(resource_top);
550 if(verify_translations_mode)
552 verify_translations(resource_top);
553 exit(0);
556 /* Convert the internal lists to binary data */
557 resources2res(resource_top);
559 chat("Writing .res-file\n");
560 write_resfile(output_name, resource_top);
561 output_name = NULL;
563 return 0;
567 static void cleanup_files(void)
569 if (output_name) unlink(output_name);
570 if (temp_name) unlink(temp_name);
573 static void segvhandler(int sig)
575 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
576 fflush(stdout);
577 fflush(stderr);
578 abort();