3 * Copyright Martin von Loewis, 1994
4 * Copyrignt 1998 Bertho A. Stultiens (BS)
6 * 30-Apr-2000 BS - Integrated a new preprocessor (-E and -N)
7 * 20-Jun-1998 BS - Added -L option to prevent case conversion
8 * of embedded filenames.
10 * 08-Jun-1998 BS - Added -A option to generate autoregister code
11 * for winelib operation.
13 * 21-May-1998 BS - Removed the CPP option. Its internal now.
14 * - Added implementations for defines and includes
17 * 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
18 * Seems to be a sign for too much freedom. I implemeted
19 * most of them as a user choice possibility for things
20 * that I do not know what to put there by default.
21 * - -l and -L options are now known as -t and -T.
23 * 23-Apr-1998 BS - Finally gave up on backward compatibility on the
24 * commandline (after a blessing from the newsgroup).
25 * So, I changed the lot.
27 * 17-Apr-1998 BS - Added many new command-line options but took care
28 * that it would not break old scripts (sigh).
30 * 16-Apr-1998 BS - There is not much left of the original source...
31 * I had to rewrite most of it because the parser
32 * changed completely with all the types etc..
57 "Usage: wrc [options...] [infile[.rc|.res]]\n"
58 " -a n Alignment of resource (win16 only, default is 4)\n"
59 " -A Auto register resources (only with gcc 2.7 and better)\n"
60 " -b Create an assembly array from a binary .res file\n"
61 " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
62 " (win32 only; default is n[ative] which equals "
63 #ifdef WORDS_BIGENDIAN
69 " -c Add 'const' prefix to C constants\n"
70 " -C cp Set the resource's codepage to cp (default is 0)\n"
71 " -d n Set debug level to 'n'\n"
72 " -D id[=val] Define preprocessor identifier id=val\n"
73 " -e Disable recognition of win32 keywords in 16bit compile\n"
74 " -E Preprocess only\n"
75 " -g Add symbols to the global c namespace\n"
76 " -h Also generate a .h file\n"
77 " -H file Same as -h but written to file\n"
78 " -I path Set include search dir to path (multiple -I allowed)\n"
79 " -l lan Set default language to lan (default is neutral {0, 0})\n"
80 " -L Leave case of embedded filenames as is\n"
81 " -m Do not remap numerical resource IDs\n"
82 " -n Do not generate .s file\n"
83 " -N Do not preprocess input\n"
84 " -o file Output to file (default is infile.[res|s|h]\n"
85 " -p prefix Give a prefix for the generated names\n"
86 " -r Create binary .res file (compile only)\n"
87 " -s Add structure with win32/16 (PE/NE) resource directory\n"
88 " -t Generate indirect loadable resource tables\n"
89 " -T Generate only indirect loadable resources tables\n"
90 " -V Print version end exit\n"
91 " -w 16|32 Select win16 or win32 output (default is win32)\n"
92 " -W Enable pedantic warnings\n"
93 "Input is taken from stdin if no sourcefile specified.\n"
94 "Debug level 'n' is a bitmask with following meaning:\n"
95 " * 0x01 Tell which resource is parsed (verbose mode)\n"
96 " * 0x02 Dump internal structures\n"
97 " * 0x04 Create a parser trace (yydebug=1)\n"
98 " * 0x08 Preprocessor messages\n"
99 " * 0x10 Preprocessor lex messages\n"
100 " * 0x20 Preprocessor yacc trace\n"
101 "The -o option only applies to the final destination file, which is\n"
102 "in case of normal compile a .s file. You must use the '-H header.h'\n"
103 "option to override the header-filename.\n"
104 "If no input filename is given and the output name is not overridden\n"
105 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
108 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
109 "Copyright 1998-2000 Bertho A. Stultiens\n"
110 " 1994 Martin von Loewis\n";
113 * Default prefix for resource names used in the C array.
114 * Option '-p name' sets it to 'name'
116 #ifdef NEED_UNDERSCORE_PREFIX
117 char *prefix = "__Resource";
119 char *prefix = "_Resource";
123 * Set if compiling in 32bit mode (default).
128 * Set when generated C variables should be prefixed with 'const'
133 * Create a .res file from the source and exit (-r option).
138 * debuglevel == DEBUGLEVEL_NONE Don't bother
139 * debuglevel & DEBUGLEVEL_CHAT Say whats done
140 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
141 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
142 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
143 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
144 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
146 int debuglevel = DEBUGLEVEL_NONE;
149 * Recognize win32 keywords if set (-w 32 enforces this),
150 * otherwise set with -e option.
155 * Set when creating C array from .res file (-b option).
160 * Set when an additional C-header is to be created in compile (-h option).
162 int create_header = 0;
165 * Set when the NE/PE resource directory should be dumped into
171 * Set when all symbols should be added to the global namespace (-g option)
176 * Set when indirect loadable resource tables should be created (-t)
181 * Set when _only_ indirect loadable resource tables should be created (-T)
183 int indirect_only = 0;
186 * NE segment resource aligment (-a option)
192 * Cleared when the assembly file must be suppressed (-n option)
197 * Language setting for resources (-l option)
199 language_t *currentlanguage = NULL;
202 * The codepage to write in win32 PE resource segment (-C option)
207 * Set when extra warnings should be generated (-W option)
212 * Set when autoregister code must be added to the output (-A option)
214 int auto_register = 0;
217 * Set when the case of embedded filenames should not be converted
218 * to lower case (-L option)
223 * The output byte-order of resources (set with -B)
225 int byteorder = WRC_BO_NATIVE;
228 * Set when _only_ to run the preprocessor (-E option)
230 int preprocess_only = 0;
233 * Set when _not_ to run the preprocessor (-N option)
235 int no_preprocess = 0;
238 * Cleared when _not_ to remap resource types (-m option)
242 char *output_name; /* The name given by the -o option */
243 char *input_name; /* The name given on the command-line */
244 char *header_name; /* The name given by the -H option */
245 char *temp_name; /* Temporary file for preprocess pipe */
247 int line_number = 1; /* The current line */
248 int char_number = 1; /* The current char pos within the line */
250 char *cmdline; /* The entire commandline */
251 time_t now; /* The time of start of wrc */
253 resource_t *resource_top; /* The top of the parsed resources */
255 int getopt (int argc, char *const *argv, const char *optstring);
256 static void rm_tempfile(void);
257 static void segvhandler(int sig);
259 int main(int argc,char *argv[])
270 signal(SIGSEGV, segvhandler);
274 /* First rebuild the commandline to put in destination */
275 /* Could be done through env[], but not all OS-es support it */
276 cmdlen = 4; /* for "wrc " */
277 for(i = 1; i < argc; i++)
278 cmdlen += strlen(argv[i]) + 1;
279 cmdline = (char *)xmalloc(cmdlen);
280 strcpy(cmdline, "wrc ");
281 for(i = 1; i < argc; i++)
283 strcat(cmdline, argv[i]);
285 strcat(cmdline, " ");
288 while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF)
293 alignment = atoi(optarg);
306 byteorder = WRC_BO_NATIVE;
310 byteorder = WRC_BO_LITTLE;
314 byteorder = WRC_BO_BIG;
317 fprintf(stderr, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
325 codepage = strtol(optarg, NULL, 0);
328 debuglevel = strtol(optarg, NULL, 0);
331 add_cmdline_define(optarg);
343 header_name = strdup(optarg);
349 add_include_path(optarg);
354 lan = strtol(optarg, NULL, 0);
355 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
371 output_name = strdup(optarg);
374 #ifdef NEED_UNDERSCORE_PREFIX
375 prefix = (char *)xmalloc(strlen(optarg)+2);
377 strcpy(prefix+1, optarg);
379 prefix = xstrdup(optarg);
395 printf(version_string);
399 if(!strcmp(optarg, "16"))
401 else if(!strcmp(optarg, "32"))
417 fprintf(stderr, usage);
421 /* Check the command line options for invalid combinations */
426 warning("Option -e ignored with 32bit compile\n");
435 warning("Option -c ignored with compile to .res\n");
441 warning("Option -[h|H] ignored with compile to .res\n");
447 warning("Option -l ignored with compile to .res\n");
453 warning("Option -L ignored with compile to .res\n");
459 warning("Option -g ignored with compile to .res\n");
465 error("Option -r and -s cannot be used together\n");
470 error("Option -r and -b cannot be used together\n");
474 if(byteorder != WRC_BO_NATIVE)
477 error("Forced byteordering not supported for binary resources\n");
484 warning("Option -c ignored with preprocess only\n");
490 warning("Option -[h|H] ignored with preprocess only\n");
496 warning("Option -l ignored with preprocess only\n");
502 error("Option -E and -L cannot be used together\n");
507 warning("Option -g ignored with preprocess only\n");
513 warning("Option -s ignored with preprocess only\n");
519 error("Option -E and -b cannot be used together\n");
524 error("Option -E and -N cannot be used together\n");
528 #if !defined(HAVE_WINE_CONSTRUCTOR)
531 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
536 /* Set alignment power */
538 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
544 error("Alignment must be between 1 and 1024");
546 if((1 << alignment_pwr) != alignment)
548 error("Alignment must be a power of 2");
551 /* Kill io buffering when some kind of debuglevel is enabled */
558 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
559 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
560 ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
561 pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;
563 /* Set the default defined stuff */
564 add_cmdline_define("__WRC__=" WRC_STRINGIZE(WRC_MAJOR_VERSION));
565 add_cmdline_define("__WRC_MINOR__=" WRC_STRINGIZE(WRC_MINOR_VERSION));
566 add_cmdline_define("__WRC_MICRO__=" WRC_STRINGIZE(WRC_MICRO_VERSION));
567 add_cmdline_define("__WRC_PATCH__=" WRC_STRINGIZE(WRC_MICRO_VERSION));
569 add_cmdline_define("RC_INVOKED=1");
573 add_cmdline_define("__WIN32__=1");
574 add_cmdline_define("__FLAT__=1");
577 add_special_define("__FILE__");
578 add_special_define("__LINE__");
579 add_special_define("__DATE__");
580 add_special_define("__TIME__");
582 /* Check if the user set a language, else set default */
584 currentlanguage = new_language(0, 0);
586 /* Check for input file on command-line */
589 input_name = argv[optind];
592 if(binary && !input_name)
594 error("Binary mode requires .res file as input\n");
597 /* Generate appropriate outfile names */
598 if(!output_name && !preprocess_only)
600 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
601 strcat(output_name, create_res ? ".res" : ".s");
604 if(!header_name && !create_res)
606 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
607 strcat(header_name, ".h");
610 /* Run the preprocessor on the input */
611 if(!no_preprocess && !binary)
615 * Preprocess the input to a temp-file, or stdout if
616 * no output was given.
619 chat("Starting preprocess");
621 if(preprocess_only && !output_name)
625 else if(preprocess_only && output_name)
627 if(!(ppout = fopen(output_name, "wb")))
628 error("Could not open %s for writing\n", output_name);
632 if(!(temp_name = tmpnam(NULL)))
633 error("Could nor generate a temp-name\n");
634 temp_name = xstrdup(temp_name);
635 if(!(ppout = fopen(temp_name, "wb")))
636 error("Could not create a temp-file\n");
641 real_name = input_name; /* Because it gets overwritten */
647 if(!(ppin = fopen(input_name, "rb")))
648 error("Could not open %s\n", input_name);
651 fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
655 input_name = real_name;
662 input_name = temp_name;
665 exit(1); /* Error during preprocess */
673 /* Go from .rc to .res or .s */
674 chat("Starting parse");
676 if(!(yyin = fopen(input_name, "rb")))
677 error("Could not open %s for input\n", input_name);
686 /* Error during parse */
690 if(debuglevel & DEBUGLEVEL_DUMP)
691 dump_resources(resource_top);
693 /* Convert the internal lists to binary data */
694 resources2res(resource_top);
698 chat("Writing .res-file");
699 write_resfile(output_name, resource_top);
705 chat("Writing .s-file");
706 write_s_file(output_name, resource_top);
710 chat("Writing .h-file");
711 write_h_file(header_name, resource_top);
718 /* Go from .res to .s */
719 chat("Reading .res-file");
720 resource_top = read_resfile(input_name);
723 chat("Writing .s-file");
724 write_s_file(output_name, resource_top);
728 chat("Writing .h-file");
729 write_h_file(header_name, resource_top);
737 static void rm_tempfile(void)
743 static void segvhandler(int sig)
745 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);