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..
51 #include "wine/port.h"
71 #include "../wpp/wpp.h"
74 "Usage: wrc [options...] [infile[.rc|.res]]\n"
75 " -a n Alignment of resource (win16 only, default is 4)\n"
76 " -A Auto register resources (only with gcc 2.7 and better)\n"
77 " -b Create an assembly array from a binary .res file\n"
78 " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
79 " (win32 only; default is n[ative] which equals "
80 #ifdef WORDS_BIGENDIAN
86 " -c Add 'const' prefix to C constants\n"
87 " -C cp Set the resource's codepage to cp (default is 0)\n"
88 " -d n Set debug level to 'n'\n"
89 " -D id[=val] Define preprocessor identifier id=val\n"
90 " -e Disable recognition of win32 keywords in 16bit compile\n"
91 " -E Preprocess only\n"
92 " -g Add symbols to the global c namespace\n"
93 " -h Also generate a .h file\n"
94 " -H file Same as -h but written to file\n"
95 " -I path Set include search dir to path (multiple -I allowed)\n"
96 " -l lan Set default language to lan (default is neutral {0, 0})\n"
97 " -L Leave case of embedded filenames as is\n"
98 " -m Do not remap numerical resource IDs\n"
99 " -n Do not generate .s file\n"
100 " -N Do not preprocess input\n"
101 " -o file Output to file (default is infile.[res|s|h]\n"
102 " -p prefix Give a prefix for the generated names\n"
103 " -r Create binary .res file (compile only)\n"
104 " -s Add structure with win32/16 (PE/NE) resource directory\n"
105 " -t Generate indirect loadable resource tables\n"
106 " -T Generate only indirect loadable resources tables\n"
107 " -V Print version end exit\n"
108 " -w 16|32 Select win16 or win32 output (default is win32)\n"
109 " -W Enable pedantic warnings\n"
110 "Input is taken from stdin if no sourcefile specified.\n"
111 "Debug level 'n' is a bitmask with following meaning:\n"
112 " * 0x01 Tell which resource is parsed (verbose mode)\n"
113 " * 0x02 Dump internal structures\n"
114 " * 0x04 Create a parser trace (yydebug=1)\n"
115 " * 0x08 Preprocessor messages\n"
116 " * 0x10 Preprocessor lex messages\n"
117 " * 0x20 Preprocessor yacc trace\n"
118 "The -o option only applies to the final destination file, which is\n"
119 "in case of normal compile a .s file. You must use the '-H header.h'\n"
120 "option to override the header-filename.\n"
121 "If no input filename is given and the output name is not overridden\n"
122 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
125 char version_string
[] = "Wine Resource Compiler Version " WRC_FULLVERSION
"\n"
126 "Copyright 1998-2000 Bertho A. Stultiens\n"
127 " 1994 Martin von Loewis\n";
130 * Default prefix for resource names used in the C array.
131 * Option '-p name' sets it to 'name'
133 char *prefix
= __ASM_NAME("_Resource");
136 * Set if compiling in 32bit mode (default).
141 * Set when generated C variables should be prefixed with 'const'
146 * Create a .res file from the source and exit (-r option).
151 * debuglevel == DEBUGLEVEL_NONE Don't bother
152 * debuglevel & DEBUGLEVEL_CHAT Say whats done
153 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
154 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
155 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
156 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
157 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
159 int debuglevel
= DEBUGLEVEL_NONE
;
162 * Recognize win32 keywords if set (-w 32 enforces this),
163 * otherwise set with -e option.
168 * Set when creating C array from .res file (-b option).
173 * Set when an additional C-header is to be created in compile (-h option).
175 int create_header
= 0;
178 * Set when the NE/PE resource directory should be dumped into
184 * Set when all symbols should be added to the global namespace (-g option)
189 * Set when indirect loadable resource tables should be created (-t)
194 * Set when _only_ indirect loadable resource tables should be created (-T)
196 int indirect_only
= 0;
199 * NE segment resource aligment (-a option)
205 * Cleared when the assembly file must be suppressed (-n option)
210 * Language setting for resources (-l option)
212 language_t
*currentlanguage
= NULL
;
215 * The codepage to write in win32 PE resource segment (-C option)
220 * Set when extra warnings should be generated (-W option)
225 * Set when autoregister code must be added to the output (-A option)
227 int auto_register
= 0;
230 * Set when the case of embedded filenames should not be converted
231 * to lower case (-L option)
236 * The output byte-order of resources (set with -B)
238 int byteorder
= WRC_BO_NATIVE
;
241 * Set when _only_ to run the preprocessor (-E option)
243 int preprocess_only
= 0;
246 * Set when _not_ to run the preprocessor (-N option)
248 int no_preprocess
= 0;
251 * Cleared when _not_ to remap resource types (-m option)
255 char *output_name
; /* The name given by the -o option */
256 char *input_name
; /* The name given on the command-line */
257 char *header_name
; /* The name given by the -H option */
258 char *temp_name
; /* Temporary file for preprocess pipe */
260 int line_number
= 1; /* The current line */
261 int char_number
= 1; /* The current char pos within the line */
263 char *cmdline
; /* The entire commandline */
264 time_t now
; /* The time of start of wrc */
266 resource_t
*resource_top
; /* The top of the parsed resources */
268 int getopt (int argc
, char *const *argv
, const char *optstring
);
269 static void rm_tempfile(void);
270 static void segvhandler(int sig
);
272 int main(int argc
,char *argv
[])
283 signal(SIGSEGV
, segvhandler
);
287 /* First rebuild the commandline to put in destination */
288 /* Could be done through env[], but not all OS-es support it */
289 cmdlen
= 4; /* for "wrc " */
290 for(i
= 1; i
< argc
; i
++)
291 cmdlen
+= strlen(argv
[i
]) + 1;
292 cmdline
= (char *)xmalloc(cmdlen
);
293 strcpy(cmdline
, "wrc ");
294 for(i
= 1; i
< argc
; i
++)
296 strcat(cmdline
, argv
[i
]);
298 strcat(cmdline
, " ");
301 while((optc
= getopt(argc
, argv
, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF
)
306 alignment
= atoi(optarg
);
319 byteorder
= WRC_BO_NATIVE
;
323 byteorder
= WRC_BO_LITTLE
;
327 byteorder
= WRC_BO_BIG
;
330 fprintf(stderr
, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
338 codepage
= strtol(optarg
, NULL
, 0);
341 debuglevel
= strtol(optarg
, NULL
, 0);
344 wpp_add_cmdline_define(optarg
);
356 header_name
= strdup(optarg
);
362 wpp_add_include_path(optarg
);
367 lan
= strtol(optarg
, NULL
, 0);
368 if (get_language_codepage(PRIMARYLANGID(lan
), SUBLANGID(lan
)) == -1)
369 error("Language %04x is not supported",lan
);
370 currentlanguage
= new_language(PRIMARYLANGID(lan
), SUBLANGID(lan
));
386 output_name
= strdup(optarg
);
389 prefix
= xstrdup(optarg
);
404 printf(version_string
);
408 if(!strcmp(optarg
, "16"))
410 else if(!strcmp(optarg
, "32"))
427 fprintf(stderr
, usage
);
431 /* Check the command line options for invalid combinations */
436 warning("Option -e ignored with 32bit compile\n");
445 warning("Option -c ignored with compile to .res\n");
451 warning("Option -[h|H] ignored with compile to .res\n");
457 warning("Option -t ignored with compile to .res\n");
463 warning("Option -T ignored with compile to .res\n");
469 warning("Option -g ignored with compile to .res\n");
475 error("Option -r and -s cannot be used together\n");
480 error("Option -r and -b cannot be used together\n");
484 if(byteorder
!= WRC_BO_NATIVE
)
487 error("Forced byteordering not supported for binary resources\n");
494 warning("Option -c ignored with preprocess only\n");
500 warning("Option -[h|H] ignored with preprocess only\n");
506 warning("Option -t ignored with preprocess only\n");
512 error("Option -E and -T cannot be used together\n");
517 warning("Option -g ignored with preprocess only\n");
523 warning("Option -s ignored with preprocess only\n");
529 error("Option -E and -b cannot be used together\n");
534 error("Option -E and -N cannot be used together\n");
538 #if !defined(HAVE_WINE_CONSTRUCTOR)
541 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
546 /* Set alignment power */
548 for(alignment_pwr
= 0; alignment_pwr
< 10 && a
> 1; alignment_pwr
++)
554 error("Alignment must be between 1 and 1024");
556 if((1 << alignment_pwr
) != alignment
)
558 error("Alignment must be a power of 2");
561 /* Kill io buffering when some kind of debuglevel is enabled */
568 yydebug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
569 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
571 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
572 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
573 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
575 /* Set the default defined stuff */
576 wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION
));
577 wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION
));
578 wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION
));
579 wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION
));
581 wpp_add_cmdline_define("RC_INVOKED=1");
585 wpp_add_cmdline_define("__WIN32__=1");
586 wpp_add_cmdline_define("__FLAT__=1");
589 /* Check if the user set a language, else set default */
591 currentlanguage
= new_language(0, 0);
593 /* Check for input file on command-line */
596 input_name
= argv
[optind
];
599 if(binary
&& !input_name
)
601 error("Binary mode requires .res file as input\n");
604 /* Generate appropriate outfile names */
605 if(!output_name
&& !preprocess_only
)
607 output_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
608 strcat(output_name
, create_res
? ".res" : ".s");
611 if(!header_name
&& !create_res
)
613 header_name
= dup_basename(input_name
, binary
? ".res" : ".rc");
614 strcat(header_name
, ".h");
617 /* Run the preprocessor on the input */
618 if(!no_preprocess
&& !binary
)
621 * Preprocess the input to a temp-file, or stdout if
622 * no output was given.
625 chat("Starting preprocess");
627 if (!preprocess_only
)
630 ret
= wpp_parse_temp( input_name
, &temp_name
);
632 else if (output_name
)
636 if (!(output
= fopen( output_name
, "w" )))
637 error( "Could not open %s for writing\n", output_name
);
638 ret
= wpp_parse( input_name
, output
);
643 ret
= wpp_parse( input_name
, stdout
);
647 exit(1); /* Error during preprocess */
652 input_name
= temp_name
;
657 /* Go from .rc to .res or .s */
658 chat("Starting parse");
660 if(!(yyin
= fopen(input_name
, "rb")))
661 error("Could not open %s for input\n", input_name
);
670 /* Error during parse */
674 if(debuglevel
& DEBUGLEVEL_DUMP
)
675 dump_resources(resource_top
);
677 /* Convert the internal lists to binary data */
678 resources2res(resource_top
);
682 chat("Writing .res-file");
683 write_resfile(output_name
, resource_top
);
689 chat("Writing .s-file");
690 write_s_file(output_name
, resource_top
);
694 chat("Writing .h-file");
695 write_h_file(header_name
, resource_top
);
702 /* Go from .res to .s */
703 chat("Reading .res-file");
704 resource_top
= read_resfile(input_name
);
707 chat("Writing .s-file");
708 write_s_file(output_name
, resource_top
);
712 chat("Writing .h-file");
713 write_h_file(header_name
, resource_top
);
721 static void rm_tempfile(void)
727 static void segvhandler(int sig
)
729 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);