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..
56 char usage
[] = "Usage: wrc [options...] [infile[.rc|.res]]\n"
57 " -a n Alignment of resource (win16 only, default is 4)\n"
58 " -A Auto register resources (only with gcc 2.7 and better)\n"
59 " -b Create a C array from a binary .res file\n"
60 " -c Add 'const' prefix to C constants\n"
61 " -C cp Set the resource's codepage to cp (default is 0)\n"
62 " -d n Set debug level to 'n'\n"
63 " -D id[=val] Define preprocessor identifier id=val\n"
64 " -e Disable recognition of win32 keywords in 16bit compile\n"
65 " -E Preprocess only\n"
66 " -g Add symbols to the global c namespace\n"
67 " -h Also generate a .h file\n"
68 " -H file Same as -h but written to file\n"
69 " -I path Set include search dir to path (multiple -I allowed)\n"
70 " -l lan Set default language to lan (default is neutral {0, 0})\n"
71 " -L Leave case of embedded filenames as is\n"
72 " -n Do not generate .s file\n"
73 " -N Do not preprocess input\n"
74 " -o file Output to file (default is infile.[res|s|h]\n"
75 " -p prefix Give a prefix for the generated names\n"
76 " -r Create binary .res file (compile only)\n"
77 " -s Add structure with win32/16 (PE/NE) resource directory\n"
78 " -t Generate indirect loadable resource tables\n"
79 " -T Generate only indirect loadable resources tables\n"
80 " -V Print version end exit\n"
81 " -w 16|32 Select win16 or win32 output (default is win32)\n"
82 " -W Enable pedantic warnings\n"
83 "Input is taken from stdin if no sourcefile specified.\n"
84 "Debug level 'n' is a bitmask with following meaning:\n"
85 " * 0x01 Tell which resource is parsed (verbose mode)\n"
86 " * 0x02 Dump internal structures\n"
87 " * 0x04 Create a parser trace (yydebug=1)\n"
88 " * 0x08 Preprocessor messages\n"
89 " * 0x10 Preprocessor lex messages\n"
90 " * 0x20 Preprocessor yacc trace\n"
91 "The -o option only applies to the final destination file, which is\n"
92 "in case of normal compile a .s file. You must use the '-H header.h'\n"
93 "option to override the header-filename.\n"
94 "If no input filename is given and the output name is not overridden\n"
95 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
98 char version_string
[] = "Wine Resource Compiler Version " WRC_FULLVERSION
"\n"
99 "Copyright 1998-2000 Bertho A. Stultiens\n"
100 " 1994 Martin von Loewis\n";
103 * Default prefix for resource names used in the C array.
104 * Option '-p name' sets it to 'name'
106 #ifdef NEED_UNDERSCORE_PREFIX
107 char *prefix
= "__Resource";
109 char *prefix
= "_Resource";
113 * Set if compiling in 32bit mode (default).
118 * Set when generated C variables should be prefixed with 'const'
123 * Create a .res file from the source and exit (-r option).
128 * debuglevel == DEBUGLEVEL_NONE Don't bother
129 * debuglevel & DEBUGLEVEL_CHAT Say whats done
130 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
131 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
132 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
133 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
134 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
136 int debuglevel
= DEBUGLEVEL_NONE
;
139 * Recognize win32 keywords if set (-w 32 enforces this),
140 * otherwise set with -e option.
145 * Set when creating C array from .res file (-b option).
150 * Set when an additional C-header is to be created in compile (-h option).
152 int create_header
= 0;
155 * Set when the NE/PE resource directory should be dumped into
161 * Set when all symbols should be added to the global namespace (-g option)
166 * Set when indirect loadable resource tables should be created (-t)
171 * Set when _only_ indirect loadable resource tables should be created (-T)
173 int indirect_only
= 0;
176 * NE segment resource aligment (-a option)
182 * Cleared when the assembly file must be suppressed (-n option)
187 * Language setting for resources (-l option)
189 language_t
*currentlanguage
= NULL
;
192 * The codepage to write in win32 PE resource segment (-C option)
197 * Set when extra warnings should be generated (-W option)
202 * Set when autoregister code must be added to the output (-A option)
204 int auto_register
= 0;
207 * Set when the case of embedded filenames should not be converted
208 * to lower case (-L option)
213 * Set when _only_ to run the preprocessor (-E option)
215 int preprocess_only
= 0;
218 * Set when _not_ to run the preprocessor (-N option)
220 int no_preprocess
= 0;
222 char *output_name
; /* The name given by the -o option */
223 char *input_name
; /* The name given on the command-line */
224 char *header_name
; /* The name given by the -H option */
225 char *temp_name
; /* Temporary file for preprocess pipe */
227 int line_number
= 1; /* The current line */
228 int char_number
= 1; /* The current char pos within the line */
230 char *cmdline
; /* The entire commandline */
231 time_t now
; /* The time of start of wrc */
233 resource_t
*resource_top
; /* The top of the parsed resources */
235 int getopt (int argc
, char *const *argv
, const char *optstring
);
236 static void rm_tempfile(void);
237 static void segvhandler(int sig
);
239 int main(int argc
,char *argv
[])
250 signal(SIGSEGV
, segvhandler
);
254 /* First rebuild the commandline to put in destination */
255 /* Could be done through env[], but not all OS-es support it */
256 cmdlen
= 4; /* for "wrc " */
257 for(i
= 1; i
< argc
; i
++)
258 cmdlen
+= strlen(argv
[i
]) + 1;
259 cmdline
= (char *)xmalloc(cmdlen
);
260 strcpy(cmdline
, "wrc ");
261 for(i
= 1; i
< argc
; i
++)
263 strcat(cmdline
, argv
[i
]);
265 strcat(cmdline
, " ");
268 while((optc
= getopt(argc
, argv
, "a:AbcC:d:D:eEghH:I:l:LnNo:p:rstTVw:W")) != EOF
)
273 alignment
= atoi(optarg
);
285 codepage
= strtol(optarg
, NULL
, 0);
288 debuglevel
= strtol(optarg
, NULL
, 0);
291 add_cmdline_define(optarg
);
303 header_name
= strdup(optarg
);
309 add_include_path(optarg
);
314 lan
= strtol(optarg
, NULL
, 0);
315 currentlanguage
= new_language(PRIMARYLANGID(lan
), SUBLANGID(lan
));
328 output_name
= strdup(optarg
);
331 #ifdef NEED_UNDERSCORE_PREFIX
332 prefix
= (char *)xmalloc(strlen(optarg
)+2);
334 strcpy(prefix
+1, optarg
);
336 prefix
= xstrdup(optarg
);
352 printf(version_string
);
356 if(!strcmp(optarg
, "16"))
358 else if(!strcmp(optarg
, "32"))
374 fprintf(stderr
, usage
);
378 /* Check the command line options for invalid combinations */
383 warning("Option -e ignored with 32bit compile\n");
392 warning("Option -c ignored with compile to .res\n");
398 warning("Option -[h|H] ignored with compile to .res\n");
404 warning("Option -l ignored with compile to .res\n");
410 warning("Option -L ignored with compile to .res\n");
416 warning("Option -g ignored with compile to .res\n");
422 error("Option -r and -s cannot be used together\n");
427 error("Option -r and -b cannot be used together\n");
435 warning("Option -c ignored with preprocess only\n");
441 warning("Option -[h|H] ignored with preprocess only\n");
447 warning("Option -l ignored with preprocess only\n");
453 error("Option -E and -L cannot be used together\n");
458 warning("Option -g ignored with preprocess only\n");
464 warning("Option -s ignored with preprocess only\n");
470 error("Option -E and -b cannot be used together\n");
475 error("Option -E and -N cannot be used together\n");
479 #if !defined(HAVE_WINE_CONSTRUCTOR)
482 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
487 /* Set alignment power */
489 for(alignment_pwr
= 0; alignment_pwr
< 10 && a
> 1; alignment_pwr
++)
495 error("Alignment must be between 1 and 1024");
497 if((1 << alignment_pwr
) != alignment
)
499 error("Alignment must be a power of 2");
502 /* Kill io buffering when some kind of debuglevel is enabled */
509 yydebug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
510 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
511 ppdebug
= debuglevel
& DEBUGLEVEL_PPTRACE
? 1 : 0;
512 pp_flex_debug
= debuglevel
& DEBUGLEVEL_PPLEX
? 1 : 0;
514 /* Set the default defined stuff */
515 add_cmdline_define("__WRC__=" WRC_STRINGIZE(WRC_MAJOR_VERSION
));
516 add_cmdline_define("__WRC_MINOR__=" WRC_STRINGIZE(WRC_MINOR_VERSION
));
517 add_cmdline_define("__WRC_MICRO__=" WRC_STRINGIZE(WRC_MICRO_VERSION
));
518 add_cmdline_define("__WRC_PATCH__=" WRC_STRINGIZE(WRC_MICRO_VERSION
));
520 add_cmdline_define("RC_INVOKED=1");
524 add_cmdline_define("__WIN32__=1");
525 add_cmdline_define("__FLAT__=1");
528 add_special_define("__FILE__");
529 add_special_define("__LINE__");
530 add_special_define("__DATE__");
531 add_special_define("__TIME__");
533 /* Check if the user set a language, else set default */
535 currentlanguage
= new_language(0, 0);
537 /* Check for input file on command-line */
540 input_name
= argv
[optind
];
543 if(binary
&& !input_name
)
545 error("Binary mode requires .res file as input\n");
548 /* Generate appropriate outfile names */
549 if(!output_name
&& !preprocess_only
)
551 output_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
552 strcat(output_name
, create_res
? ".res" : ".s");
555 if(!header_name
&& !create_res
)
557 header_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
558 strcat(header_name
, ".h");
561 /* Run the preprocessor on the input */
562 if(!no_preprocess
&& !binary
)
566 * Preprocess the input to a temp-file, or stdout if
567 * no output was given.
570 chat("Starting preprocess");
572 if(preprocess_only
&& !output_name
)
576 else if(preprocess_only
&& output_name
)
578 if(!(ppout
= fopen(output_name
, "wb")))
579 error("Could not open %s for writing\n", output_name
);
583 if(!(temp_name
= tmpnam(NULL
)))
584 error("Could nor generate a temp-name\n");
585 temp_name
= xstrdup(temp_name
);
586 if(!(ppout
= fopen(temp_name
, "wb")))
587 error("Could not create a temp-file\n");
592 real_name
= input_name
; /* Because it gets overwritten */
598 if(!(ppin
= fopen(input_name
, "rb")))
599 error("Could not open %s\n", input_name
);
602 fprintf(ppout
, "# 1 \"%s\" 1\n", input_name
? input_name
: "");
606 input_name
= real_name
;
613 input_name
= temp_name
;
616 exit(1); /* Error during preprocess */
624 /* Go from .rc to .res or .s */
625 chat("Starting parse");
627 if(!(yyin
= fopen(input_name
, "rb")))
628 error("Could not open %s for input\n", input_name
);
637 /* Error during parse */
641 if(debuglevel
& DEBUGLEVEL_DUMP
)
642 dump_resources(resource_top
);
644 /* Convert the internal lists to binary data */
645 resources2res(resource_top
);
649 chat("Writing .res-file");
650 write_resfile(output_name
, resource_top
);
656 chat("Writing .s-file");
657 write_s_file(output_name
, resource_top
);
661 chat("Writing .h-file");
662 write_h_file(header_name
, resource_top
);
669 /* Go from .res to .s */
670 chat("Reading .res-file");
671 resource_top
= read_resfile(input_name
);
674 chat("Writing .s-file");
675 write_s_file(output_name
, resource_top
);
679 chat("Writing .h-file");
680 write_h_file(header_name
, resource_top
);
688 static void rm_tempfile(void)
694 static void segvhandler(int sig
)
696 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);