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 " -n Do not generate .s file\n"
82 " -N Do not preprocess input\n"
83 " -o file Output to file (default is infile.[res|s|h]\n"
84 " -p prefix Give a prefix for the generated names\n"
85 " -r Create binary .res file (compile only)\n"
86 " -s Add structure with win32/16 (PE/NE) resource directory\n"
87 " -t Generate indirect loadable resource tables\n"
88 " -T Generate only indirect loadable resources tables\n"
89 " -V Print version end exit\n"
90 " -w 16|32 Select win16 or win32 output (default is win32)\n"
91 " -W Enable pedantic warnings\n"
92 "Input is taken from stdin if no sourcefile specified.\n"
93 "Debug level 'n' is a bitmask with following meaning:\n"
94 " * 0x01 Tell which resource is parsed (verbose mode)\n"
95 " * 0x02 Dump internal structures\n"
96 " * 0x04 Create a parser trace (yydebug=1)\n"
97 " * 0x08 Preprocessor messages\n"
98 " * 0x10 Preprocessor lex messages\n"
99 " * 0x20 Preprocessor yacc trace\n"
100 "The -o option only applies to the final destination file, which is\n"
101 "in case of normal compile a .s file. You must use the '-H header.h'\n"
102 "option to override the header-filename.\n"
103 "If no input filename is given and the output name is not overridden\n"
104 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
107 char version_string
[] = "Wine Resource Compiler Version " WRC_FULLVERSION
"\n"
108 "Copyright 1998-2000 Bertho A. Stultiens\n"
109 " 1994 Martin von Loewis\n";
112 * Default prefix for resource names used in the C array.
113 * Option '-p name' sets it to 'name'
115 #ifdef NEED_UNDERSCORE_PREFIX
116 char *prefix
= "__Resource";
118 char *prefix
= "_Resource";
122 * Set if compiling in 32bit mode (default).
127 * Set when generated C variables should be prefixed with 'const'
132 * Create a .res file from the source and exit (-r option).
137 * debuglevel == DEBUGLEVEL_NONE Don't bother
138 * debuglevel & DEBUGLEVEL_CHAT Say whats done
139 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
140 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
141 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
142 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
143 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
145 int debuglevel
= DEBUGLEVEL_NONE
;
148 * Recognize win32 keywords if set (-w 32 enforces this),
149 * otherwise set with -e option.
154 * Set when creating C array from .res file (-b option).
159 * Set when an additional C-header is to be created in compile (-h option).
161 int create_header
= 0;
164 * Set when the NE/PE resource directory should be dumped into
170 * Set when all symbols should be added to the global namespace (-g option)
175 * Set when indirect loadable resource tables should be created (-t)
180 * Set when _only_ indirect loadable resource tables should be created (-T)
182 int indirect_only
= 0;
185 * NE segment resource aligment (-a option)
191 * Cleared when the assembly file must be suppressed (-n option)
196 * Language setting for resources (-l option)
198 language_t
*currentlanguage
= NULL
;
201 * The codepage to write in win32 PE resource segment (-C option)
206 * Set when extra warnings should be generated (-W option)
211 * Set when autoregister code must be added to the output (-A option)
213 int auto_register
= 0;
216 * Set when the case of embedded filenames should not be converted
217 * to lower case (-L option)
222 * The output byte-order of resources (set with -B)
224 int byteorder
= WRC_BO_NATIVE
;
227 * Set when _only_ to run the preprocessor (-E option)
229 int preprocess_only
= 0;
232 * Set when _not_ to run the preprocessor (-N option)
234 int no_preprocess
= 0;
236 char *output_name
; /* The name given by the -o option */
237 char *input_name
; /* The name given on the command-line */
238 char *header_name
; /* The name given by the -H option */
239 char *temp_name
; /* Temporary file for preprocess pipe */
241 int line_number
= 1; /* The current line */
242 int char_number
= 1; /* The current char pos within the line */
244 char *cmdline
; /* The entire commandline */
245 time_t now
; /* The time of start of wrc */
247 resource_t
*resource_top
; /* The top of the parsed resources */
249 int getopt (int argc
, char *const *argv
, const char *optstring
);
250 static void rm_tempfile(void);
251 static void segvhandler(int sig
);
253 int main(int argc
,char *argv
[])
264 signal(SIGSEGV
, segvhandler
);
268 /* First rebuild the commandline to put in destination */
269 /* Could be done through env[], but not all OS-es support it */
270 cmdlen
= 4; /* for "wrc " */
271 for(i
= 1; i
< argc
; i
++)
272 cmdlen
+= strlen(argv
[i
]) + 1;
273 cmdline
= (char *)xmalloc(cmdlen
);
274 strcpy(cmdline
, "wrc ");
275 for(i
= 1; i
< argc
; i
++)
277 strcat(cmdline
, argv
[i
]);
279 strcat(cmdline
, " ");
282 while((optc
= getopt(argc
, argv
, "a:AbB:cC:d:D:eEghH:I:l:LnNo:p:rstTVw:W")) != EOF
)
287 alignment
= atoi(optarg
);
300 byteorder
= WRC_BO_NATIVE
;
304 byteorder
= WRC_BO_LITTLE
;
308 byteorder
= WRC_BO_BIG
;
311 fprintf(stderr
, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
319 codepage
= strtol(optarg
, NULL
, 0);
322 debuglevel
= strtol(optarg
, NULL
, 0);
325 add_cmdline_define(optarg
);
337 header_name
= strdup(optarg
);
343 add_include_path(optarg
);
348 lan
= strtol(optarg
, NULL
, 0);
349 currentlanguage
= new_language(PRIMARYLANGID(lan
), SUBLANGID(lan
));
362 output_name
= strdup(optarg
);
365 #ifdef NEED_UNDERSCORE_PREFIX
366 prefix
= (char *)xmalloc(strlen(optarg
)+2);
368 strcpy(prefix
+1, optarg
);
370 prefix
= xstrdup(optarg
);
386 printf(version_string
);
390 if(!strcmp(optarg
, "16"))
392 else if(!strcmp(optarg
, "32"))
408 fprintf(stderr
, usage
);
412 /* Check the command line options for invalid combinations */
417 warning("Option -e ignored with 32bit compile\n");
426 warning("Option -c ignored with compile to .res\n");
432 warning("Option -[h|H] ignored with compile to .res\n");
438 warning("Option -l ignored with compile to .res\n");
444 warning("Option -L ignored with compile to .res\n");
450 warning("Option -g ignored with compile to .res\n");
456 error("Option -r and -s cannot be used together\n");
461 error("Option -r and -b cannot be used together\n");
465 if(byteorder
!= WRC_BO_NATIVE
)
468 error("Forced byteordering not supported for binary resources\n");
475 warning("Option -c ignored with preprocess only\n");
481 warning("Option -[h|H] ignored with preprocess only\n");
487 warning("Option -l ignored with preprocess only\n");
493 error("Option -E and -L cannot be used together\n");
498 warning("Option -g ignored with preprocess only\n");
504 warning("Option -s ignored with preprocess only\n");
510 error("Option -E and -b cannot be used together\n");
515 error("Option -E and -N cannot be used together\n");
519 #if !defined(HAVE_WINE_CONSTRUCTOR)
522 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
527 /* Set alignment power */
529 for(alignment_pwr
= 0; alignment_pwr
< 10 && a
> 1; alignment_pwr
++)
535 error("Alignment must be between 1 and 1024");
537 if((1 << alignment_pwr
) != alignment
)
539 error("Alignment must be a power of 2");
542 /* Kill io buffering when some kind of debuglevel is enabled */
549 yydebug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
550 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
551 ppdebug
= debuglevel
& DEBUGLEVEL_PPTRACE
? 1 : 0;
552 pp_flex_debug
= debuglevel
& DEBUGLEVEL_PPLEX
? 1 : 0;
554 /* Set the default defined stuff */
555 add_cmdline_define("__WRC__=" WRC_STRINGIZE(WRC_MAJOR_VERSION
));
556 add_cmdline_define("__WRC_MINOR__=" WRC_STRINGIZE(WRC_MINOR_VERSION
));
557 add_cmdline_define("__WRC_MICRO__=" WRC_STRINGIZE(WRC_MICRO_VERSION
));
558 add_cmdline_define("__WRC_PATCH__=" WRC_STRINGIZE(WRC_MICRO_VERSION
));
560 add_cmdline_define("RC_INVOKED=1");
564 add_cmdline_define("__WIN32__=1");
565 add_cmdline_define("__FLAT__=1");
568 add_special_define("__FILE__");
569 add_special_define("__LINE__");
570 add_special_define("__DATE__");
571 add_special_define("__TIME__");
573 /* Check if the user set a language, else set default */
575 currentlanguage
= new_language(0, 0);
577 /* Check for input file on command-line */
580 input_name
= argv
[optind
];
583 if(binary
&& !input_name
)
585 error("Binary mode requires .res file as input\n");
588 /* Generate appropriate outfile names */
589 if(!output_name
&& !preprocess_only
)
591 output_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
592 strcat(output_name
, create_res
? ".res" : ".s");
595 if(!header_name
&& !create_res
)
597 header_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
598 strcat(header_name
, ".h");
601 /* Run the preprocessor on the input */
602 if(!no_preprocess
&& !binary
)
606 * Preprocess the input to a temp-file, or stdout if
607 * no output was given.
610 chat("Starting preprocess");
612 if(preprocess_only
&& !output_name
)
616 else if(preprocess_only
&& output_name
)
618 if(!(ppout
= fopen(output_name
, "wb")))
619 error("Could not open %s for writing\n", output_name
);
623 if(!(temp_name
= tmpnam(NULL
)))
624 error("Could nor generate a temp-name\n");
625 temp_name
= xstrdup(temp_name
);
626 if(!(ppout
= fopen(temp_name
, "wb")))
627 error("Could not create a temp-file\n");
632 real_name
= input_name
; /* Because it gets overwritten */
638 if(!(ppin
= fopen(input_name
, "rb")))
639 error("Could not open %s\n", input_name
);
642 fprintf(ppout
, "# 1 \"%s\" 1\n", input_name
? input_name
: "");
646 input_name
= real_name
;
653 input_name
= temp_name
;
656 exit(1); /* Error during preprocess */
664 /* Go from .rc to .res or .s */
665 chat("Starting parse");
667 if(!(yyin
= fopen(input_name
, "rb")))
668 error("Could not open %s for input\n", input_name
);
677 /* Error during parse */
681 if(debuglevel
& DEBUGLEVEL_DUMP
)
682 dump_resources(resource_top
);
684 /* Convert the internal lists to binary data */
685 resources2res(resource_top
);
689 chat("Writing .res-file");
690 write_resfile(output_name
, resource_top
);
696 chat("Writing .s-file");
697 write_s_file(output_name
, resource_top
);
701 chat("Writing .h-file");
702 write_h_file(header_name
, resource_top
);
709 /* Go from .res to .s */
710 chat("Reading .res-file");
711 resource_top
= read_resfile(input_name
);
714 chat("Writing .s-file");
715 write_s_file(output_name
, resource_top
);
719 chat("Writing .h-file");
720 write_h_file(header_name
, resource_top
);
728 static void rm_tempfile(void)
734 static void segvhandler(int sig
)
736 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);