2 * Copyright Martin von Loewis, 1994
3 * Copyrignt 1998 Bertho A. Stultiens (BS)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * 30-Apr-2000 BS - Integrated a new preprocessor (-E and -N)
21 * 20-Jun-1998 BS - Added -L option to prevent case conversion
22 * of embedded filenames.
24 * 08-Jun-1998 BS - Added -A option to generate autoregister code
25 * for winelib operation.
27 * 21-May-1998 BS - Removed the CPP option. Its internal now.
28 * - Added implementations for defines and includes
31 * 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
32 * Seems to be a sign for too much freedom. I implemeted
33 * most of them as a user choice possibility for things
34 * that I do not know what to put there by default.
35 * - -l and -L options are now known as -t and -T.
37 * 23-Apr-1998 BS - Finally gave up on backward compatibility on the
38 * commandline (after a blessing from the newsgroup).
39 * So, I changed the lot.
41 * 17-Apr-1998 BS - Added many new command-line options but took care
42 * that it would not break old scripts (sigh).
44 * 16-Apr-1998 BS - There is not much left of the original source...
45 * I had to rewrite most of it because the parser
46 * changed completely with all the types etc..
71 "Usage: wrc [options...] [infile[.rc|.res]]\n"
72 " -a n Alignment of resource (win16 only, default is 4)\n"
73 " -A Auto register resources (only with gcc 2.7 and better)\n"
74 " -b Create an assembly array from a binary .res file\n"
75 " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
76 " (win32 only; default is n[ative] which equals "
77 #ifdef WORDS_BIGENDIAN
83 " -c Add 'const' prefix to C constants\n"
84 " -C cp Set the resource's codepage to cp (default is 0)\n"
85 " -d n Set debug level to 'n'\n"
86 " -D id[=val] Define preprocessor identifier id=val\n"
87 " -e Disable recognition of win32 keywords in 16bit compile\n"
88 " -E Preprocess only\n"
89 " -g Add symbols to the global c namespace\n"
90 " -h Also generate a .h file\n"
91 " -H file Same as -h but written to file\n"
92 " -I path Set include search dir to path (multiple -I allowed)\n"
93 " -l lan Set default language to lan (default is neutral {0, 0})\n"
94 " -L Leave case of embedded filenames as is\n"
95 " -m Do not remap numerical resource IDs\n"
96 " -n Do not generate .s file\n"
97 " -N Do not preprocess input\n"
98 " -o file Output to file (default is infile.[res|s|h]\n"
99 " -p prefix Give a prefix for the generated names\n"
100 " -r Create binary .res file (compile only)\n"
101 " -s Add structure with win32/16 (PE/NE) resource directory\n"
102 " -t Generate indirect loadable resource tables\n"
103 " -T Generate only indirect loadable resources tables\n"
104 " -V Print version end exit\n"
105 " -w 16|32 Select win16 or win32 output (default is win32)\n"
106 " -W Enable pedantic warnings\n"
107 "Input is taken from stdin if no sourcefile specified.\n"
108 "Debug level 'n' is a bitmask with following meaning:\n"
109 " * 0x01 Tell which resource is parsed (verbose mode)\n"
110 " * 0x02 Dump internal structures\n"
111 " * 0x04 Create a parser trace (yydebug=1)\n"
112 " * 0x08 Preprocessor messages\n"
113 " * 0x10 Preprocessor lex messages\n"
114 " * 0x20 Preprocessor yacc trace\n"
115 "The -o option only applies to the final destination file, which is\n"
116 "in case of normal compile a .s file. You must use the '-H header.h'\n"
117 "option to override the header-filename.\n"
118 "If no input filename is given and the output name is not overridden\n"
119 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
122 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
123 "Copyright 1998-2000 Bertho A. Stultiens\n"
124 " 1994 Martin von Loewis\n";
127 * Default prefix for resource names used in the C array.
128 * Option '-p name' sets it to 'name'
130 #ifdef NEED_UNDERSCORE_PREFIX
131 char *prefix = "__Resource";
133 char *prefix = "_Resource";
137 * Set if compiling in 32bit mode (default).
142 * Set when generated C variables should be prefixed with 'const'
147 * Create a .res file from the source and exit (-r option).
152 * debuglevel == DEBUGLEVEL_NONE Don't bother
153 * debuglevel & DEBUGLEVEL_CHAT Say whats done
154 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
155 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
156 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
157 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
158 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
160 int debuglevel = DEBUGLEVEL_NONE;
163 * Recognize win32 keywords if set (-w 32 enforces this),
164 * otherwise set with -e option.
169 * Set when creating C array from .res file (-b option).
174 * Set when an additional C-header is to be created in compile (-h option).
176 int create_header = 0;
179 * Set when the NE/PE resource directory should be dumped into
185 * Set when all symbols should be added to the global namespace (-g option)
190 * Set when indirect loadable resource tables should be created (-t)
195 * Set when _only_ indirect loadable resource tables should be created (-T)
197 int indirect_only = 0;
200 * NE segment resource aligment (-a option)
206 * Cleared when the assembly file must be suppressed (-n option)
211 * Language setting for resources (-l option)
213 language_t *currentlanguage = NULL;
216 * The codepage to write in win32 PE resource segment (-C option)
221 * Set when extra warnings should be generated (-W option)
226 * Set when autoregister code must be added to the output (-A option)
228 int auto_register = 0;
231 * Set when the case of embedded filenames should not be converted
232 * to lower case (-L option)
237 * The output byte-order of resources (set with -B)
239 int byteorder = WRC_BO_NATIVE;
242 * Set when _only_ to run the preprocessor (-E option)
244 int preprocess_only = 0;
247 * Set when _not_ to run the preprocessor (-N option)
249 int no_preprocess = 0;
252 * Cleared when _not_ to remap resource types (-m option)
256 char *output_name; /* The name given by the -o option */
257 char *input_name; /* The name given on the command-line */
258 char *header_name; /* The name given by the -H option */
259 char *temp_name; /* Temporary file for preprocess pipe */
261 int line_number = 1; /* The current line */
262 int char_number = 1; /* The current char pos within the line */
264 char *cmdline; /* The entire commandline */
265 time_t now; /* The time of start of wrc */
267 resource_t *resource_top; /* The top of the parsed resources */
269 int getopt (int argc, char *const *argv, const char *optstring);
270 static void rm_tempfile(void);
271 static void segvhandler(int sig);
273 int main(int argc,char *argv[])
284 signal(SIGSEGV, segvhandler);
288 /* First rebuild the commandline to put in destination */
289 /* Could be done through env[], but not all OS-es support it */
290 cmdlen = 4; /* for "wrc " */
291 for(i = 1; i < argc; i++)
292 cmdlen += strlen(argv[i]) + 1;
293 cmdline = (char *)xmalloc(cmdlen);
294 strcpy(cmdline, "wrc ");
295 for(i = 1; i < argc; i++)
297 strcat(cmdline, argv[i]);
299 strcat(cmdline, " ");
302 while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF)
307 alignment = atoi(optarg);
320 byteorder = WRC_BO_NATIVE;
324 byteorder = WRC_BO_LITTLE;
328 byteorder = WRC_BO_BIG;
331 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
339 codepage = strtol(optarg, NULL, 0);
342 debuglevel = strtol(optarg, NULL, 0);
345 add_cmdline_define(optarg);
357 header_name = strdup(optarg);
363 add_include_path(optarg);
368 lan = strtol(optarg, NULL, 0);
369 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
385 output_name = strdup(optarg);
388 #ifdef NEED_UNDERSCORE_PREFIX
389 prefix = (char *)xmalloc(strlen(optarg)+2);
391 strcpy(prefix+1, optarg);
393 prefix = xstrdup(optarg);
409 printf(version_string);
413 if(!strcmp(optarg, "16"))
415 else if(!strcmp(optarg, "32"))
431 fprintf(stderr, usage);
435 /* Check the command line options for invalid combinations */
440 warning("Option -e ignored with 32bit compile\n");
449 warning("Option -c ignored with compile to .res\n");
455 warning("Option -[h|H] ignored with compile to .res\n");
461 warning("Option -t ignored with compile to .res\n");
467 warning("Option -T ignored with compile to .res\n");
473 warning("Option -g ignored with compile to .res\n");
479 error("Option -r and -s cannot be used together\n");
484 error("Option -r and -b cannot be used together\n");
488 if(byteorder != WRC_BO_NATIVE)
491 error("Forced byteordering not supported for binary resources\n");
498 warning("Option -c ignored with preprocess only\n");
504 warning("Option -[h|H] ignored with preprocess only\n");
510 warning("Option -t ignored with preprocess only\n");
516 error("Option -E and -T cannot be used together\n");
521 warning("Option -g ignored with preprocess only\n");
527 warning("Option -s ignored with preprocess only\n");
533 error("Option -E and -b cannot be used together\n");
538 error("Option -E and -N cannot be used together\n");
542 #if !defined(HAVE_WINE_CONSTRUCTOR)
545 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
550 /* Set alignment power */
552 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
558 error("Alignment must be between 1 and 1024");
560 if((1 << alignment_pwr) != alignment)
562 error("Alignment must be a power of 2");
565 /* Kill io buffering when some kind of debuglevel is enabled */
572 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
573 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
574 ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
575 pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;
577 /* Set the default defined stuff */
578 add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
579 add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
580 add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
581 add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
583 add_cmdline_define("RC_INVOKED=1");
587 add_cmdline_define("__WIN32__=1");
588 add_cmdline_define("__FLAT__=1");
591 add_special_define("__FILE__");
592 add_special_define("__LINE__");
593 add_special_define("__DATE__");
594 add_special_define("__TIME__");
596 /* Check if the user set a language, else set default */
598 currentlanguage = new_language(0, 0);
600 /* Check for input file on command-line */
603 input_name = argv[optind];
606 if(binary && !input_name)
608 error("Binary mode requires .res file as input\n");
611 /* Generate appropriate outfile names */
612 if(!output_name && !preprocess_only)
614 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
615 strcat(output_name, create_res ? ".res" : ".s");
618 if(!header_name && !create_res)
620 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
621 strcat(header_name, ".h");
624 /* Run the preprocessor on the input */
625 if(!no_preprocess && !binary)
629 * Preprocess the input to a temp-file, or stdout if
630 * no output was given.
633 chat("Starting preprocess");
635 if(preprocess_only && !output_name)
639 else if(preprocess_only && output_name)
641 if(!(ppout = fopen(output_name, "wb")))
642 error("Could not open %s for writing\n", output_name);
646 if(!(temp_name = tmpnam(NULL)))
647 error("Could nor generate a temp-name\n");
648 temp_name = xstrdup(temp_name);
649 if(!(ppout = fopen(temp_name, "wb")))
650 error("Could not create a temp-file\n");
655 real_name = input_name; /* Because it gets overwritten */
661 if(!(ppin = fopen(input_name, "rb")))
662 error("Could not open %s\n", input_name);
665 fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
669 input_name = real_name;
676 input_name = temp_name;
679 exit(1); /* Error during preprocess */
687 /* Go from .rc to .res or .s */
688 chat("Starting parse");
690 if(!(yyin = fopen(input_name, "rb")))
691 error("Could not open %s for input\n", input_name);
700 /* Error during parse */
704 if(debuglevel & DEBUGLEVEL_DUMP)
705 dump_resources(resource_top);
707 /* Convert the internal lists to binary data */
708 resources2res(resource_top);
712 chat("Writing .res-file");
713 write_resfile(output_name, resource_top);
719 chat("Writing .s-file");
720 write_s_file(output_name, resource_top);
724 chat("Writing .h-file");
725 write_h_file(header_name, resource_top);
732 /* Go from .res to .s */
733 chat("Reading .res-file");
734 resource_top = read_resfile(input_name);
737 chat("Writing .s-file");
738 write_s_file(output_name, resource_top);
742 chat("Writing .h-file");
743 write_h_file(header_name, resource_top);
751 static void rm_tempfile(void)
757 static void segvhandler(int sig)
759 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);