Sync to Wine-20050628:
[reactos.git] / reactos / tools / wrc / wrc.c
1 /*
2 * Copyright 1994 Martin von Loewis
3 * Copyrignt 1998 Bertho A. Stultiens (BS)
4 * Copyright 2003 Dimitrie O. Paun
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
33 #ifdef HAVE_GETOPT_H
34 # include <getopt.h>
35 #endif
36
37 #include "wrc.h"
38 #include "utils.h"
39 #include "readres.h"
40 #include "dumpres.h"
41 #include "genres.h"
42 #include "newstruc.h"
43 #include "parser.h"
44 #include "wine/wpp.h"
45
46 #ifndef INCLUDEDIR
47 #define INCLUDEDIR "/usr/local/include/wine"
48 #endif
49
50 #ifdef WORDS_BIGENDIAN
51 #define ENDIAN "big"
52 #else
53 #define ENDIAN "little"
54 #endif
55
56 static char usage[] =
57 "Usage: wrc [options...] [infile[.rc|.res]] [outfile]\n"
58 " -D id[=val] Define preprocessor identifier id=val\n"
59 " -E Preprocess only\n"
60 " -F target Ignored for compatibility with windres\n"
61 " -h Prints this summary\n"
62 " -i file The name of the input file\n"
63 " -I path Set include search dir to path (multiple -I allowed)\n"
64 " -J format The input format (either `rc' or `rc16')\n"
65 " -l lan Set default language to lan (default is neutral {0, 0})\n"
66 " -o file Output to file (default is infile.res)\n"
67 " -O format The output format (either `res' or `res16`)\n"
68 " -r Ignored for compatibility with rc\n"
69 " -U id Undefine preprocessor identifier id\n"
70 " -v Enable verbose mode\n"
71 "The following long options are supported:\n"
72 " --debug=nn Set debug level to 'nn'\n"
73 " --define Synonym for -D\n"
74 " --endianess=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
75 " (win32 only; default is " ENDIAN "-endian)\n"
76 " --help Synonym for -h\n"
77 " --include-dir Synonym for -I\n"
78 " --input Synonym for -i\n"
79 " --input-format Synonym for -J\n"
80 " --language Synonym for -l\n"
81 " --no-use-temp-file Ignored for compatibility with windres\n"
82 " --nostdinc Disables searching the standard include path\n"
83 " --output -fo Synonym for -o\n"
84 " --output-format Synonym for -O\n"
85 " --pedantic Enable pedantic warnings\n"
86 " --preprocessor Specifies the preprocessor to use, including arguments\n"
87 " --target Synonym for -F\n"
88 " --undefine Synonym for -U\n"
89 " --use-temp-file Ignored for compatibility with windres\n"
90 " --verbose Synonym for -v\n"
91 " --verify-translations Check the status of the various translations\n"
92 " --version Print version and exit\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 "If no input filename is given and the output name is not overridden\n"
102 "with -o, then the output is written to \"wrc.tab.res\"\n"
103 ;
104
105 static const char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
106 "Copyright 1998-2000 Bertho A. Stultiens\n"
107 " 1994 Martin von Loewis\n";
108
109 /*
110 * Set if compiling in 32bit mode (default).
111 */
112 int win32 = 1;
113
114 /*
115 * debuglevel == DEBUGLEVEL_NONE Don't bother
116 * debuglevel & DEBUGLEVEL_CHAT Say whats done
117 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
118 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
119 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
120 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
121 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
122 */
123 int debuglevel = DEBUGLEVEL_NONE;
124
125 /*
126 * Recognize win32 keywords if set (-w 32 enforces this),
127 * otherwise set with -e option.
128 */
129 int extensions = 1;
130
131 /*
132 * Language setting for resources (-l option)
133 */
134 language_t *currentlanguage = NULL;
135
136 /*
137 * Set when extra warnings should be generated (-W option)
138 */
139 int pedantic = 0;
140
141 /*
142 * The output byte-order of resources (set with -B)
143 */
144 int byteorder = WRC_BO_NATIVE;
145
146 /*
147 * Set when _only_ to run the preprocessor (-E option)
148 */
149 int preprocess_only = 0;
150
151 /*
152 * Set when _not_ to run the preprocessor (-P cat option)
153 */
154 int no_preprocess = 0;
155
156 static int verify_translations_mode;
157
158 char *output_name = NULL; /* The name given by the -o option */
159 char *input_name = NULL; /* The name given on the command-line */
160 char *temp_name = NULL; /* Temporary file for preprocess pipe */
161
162 int line_number = 1; /* The current line */
163 int char_number = 1; /* The current char pos within the line */
164
165 char *cmdline; /* The entire commandline */
166 time_t now; /* The time of start of wrc */
167
168 resource_t *resource_top; /* The top of the parsed resources */
169
170 int getopt (int argc, char *const *argv, const char *optstring);
171 static void rm_tempfile(void);
172 static void segvhandler(int sig);
173
174 static const char* short_options =
175 "D:Ef:F:hi:I:J:l:o:O:rU:v";
176 static struct option long_options[] = {
177 { "debug", 1, 0, 6 },
178 { "define", 1, 0, 'D' },
179 { "endianess", 1, 0, 7 },
180 { "help", 0, 0, 'h' },
181 { "include-dir", 1, 0, 'I' },
182 { "input", 1, 0, 'i' },
183 { "input-format", 1, 0, 'J' },
184 { "language", 1, 0, 'l' },
185 { "no-use-temp-file", 0, 0, 3 },
186 { "nostdinc", 0, 0, 1 },
187 { "output", 1, 0, 'o' },
188 { "output-format", 1, 0, 'O' },
189 { "pendantic", 0, 0, 8 },
190 { "preprocessor", 1, 0, 4 },
191 { "target", 1, 0, 'F' },
192 { "undefine", 1, 0, 'U' },
193 { "use-temp-file", 0, 0, 2 },
194 { "verbose", 0, 0, 'v' },
195 { "verify-translations", 0, 0, 9 },
196 { "version", 0, 0, 5 },
197 { 0, 0, 0, 0 }
198 };
199
200 int main(int argc,char *argv[])
201 {
202 extern char* optarg;
203 extern int optind;
204 int optc;
205 int opti = 0;
206 int stdinc = 1;
207 int lose = 0;
208 int ret;
209 int i;
210 int cmdlen;
211
212 signal(SIGSEGV, segvhandler);
213
214 now = time(NULL);
215
216 /* Set the default defined stuff */
217 wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
218 wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
219 wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
220 wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
221
222 wpp_add_cmdline_define("RC_INVOKED=1");
223 wpp_add_cmdline_define("__WIN32__=1");
224 wpp_add_cmdline_define("__FLAT__=1");
225
226 /* First rebuild the commandline to put in destination */
227 /* Could be done through env[], but not all OS-es support it */
228 cmdlen = 4; /* for "wrc " */
229 for(i = 1; i < argc; i++)
230 cmdlen += strlen(argv[i]) + 1;
231 cmdline = (char *)xmalloc(cmdlen);
232 strcpy(cmdline, "wrc ");
233 for(i = 1; i < argc; i++)
234 {
235 strcat(cmdline, argv[i]);
236 if(i < argc-1)
237 strcat(cmdline, " ");
238 }
239
240 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
241 {
242 switch(optc)
243 {
244 case 1:
245 stdinc = 0;
246 break;
247 case 2:
248 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
249 break;
250 case 3:
251 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
252 break;
253 case 4:
254 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
255 else fprintf(stderr, "-P option not yet supported, ignored.\n");
256 break;
257 case 5:
258 printf(version_string);
259 exit(0);
260 break;
261 case 6:
262 debuglevel = strtol(optarg, NULL, 0);
263 break;
264 case 7:
265 switch(optarg[0])
266 {
267 case 'n':
268 case 'N':
269 byteorder = WRC_BO_NATIVE;
270 break;
271 case 'l':
272 case 'L':
273 byteorder = WRC_BO_LITTLE;
274 break;
275 case 'b':
276 case 'B':
277 byteorder = WRC_BO_BIG;
278 break;
279 default:
280 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
281 lose++;
282 }
283 break;
284 case 8:
285 pedantic = 1;
286 wpp_set_pedantic(1);
287 break;
288 case 9:
289 verify_translations_mode = 1;
290 break;
291 case 'D':
292 wpp_add_cmdline_define(optarg);
293 break;
294 case 'E':
295 preprocess_only = 1;
296 break;
297 case 'F':
298 /* ignored for compatibility with windres */
299 break;
300 case 'h':
301 printf(usage);
302 exit(0);
303 case 'i':
304 if (!input_name) input_name = strdup(optarg);
305 else error("Too many input files.\n");
306 break;
307 case 'I':
308 wpp_add_include_path(optarg);
309 break;
310 case 'J':
311 if (strcmp(optarg, "rc16") == 0) extensions = 0;
312 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
313 break;
314 case 'l':
315 {
316 int lan;
317 lan = strtol(optarg, NULL, 0);
318 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
319 error("Language %04x is not supported\n", lan);
320 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
321 }
322 break;
323 case 'f':
324 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
325 optarg++;
326 /* fall through */
327 case 'o':
328 if (!output_name) output_name = strdup(optarg);
329 else error("Too many output files.\n");
330 break;
331 case 'O':
332 if (strcmp(optarg, "res16") == 0)
333 {
334 win32 = 0;
335 wpp_del_define("__WIN32__");
336 wpp_del_define("__FLAT__");
337 }
338 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
339 break;
340 case 'r':
341 /* ignored for compatibility with rc */
342 break;
343 case 'U':
344 wpp_del_define(optarg);
345 break;
346 case 'v':
347 debuglevel = DEBUGLEVEL_CHAT;
348 break;
349 default:
350 lose++;
351 break;
352 }
353 }
354
355 if(lose)
356 {
357 fprintf(stderr, usage);
358 return 1;
359 }
360
361 /* If we do need to search standard includes, add them to the path */
362 if (stdinc)
363 {
364 wpp_add_include_path(INCLUDEDIR"/msvcrt");
365 wpp_add_include_path(INCLUDEDIR"/windows");
366 }
367
368 /* Check for input file on command-line */
369 if(optind < argc)
370 {
371 if (!input_name) input_name = argv[optind++];
372 else error("Too many input files.\n");
373 }
374
375 /* Check for output file on command-line */
376 if(optind < argc)
377 {
378 if (!output_name) output_name = argv[optind++];
379 else error("Too many output files.\n");
380 }
381
382 /* Kill io buffering when some kind of debuglevel is enabled */
383 if(debuglevel)
384 {
385 setbuf(stdout,0);
386 setbuf(stderr,0);
387 }
388
389 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
390 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
391
392 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
393 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
394 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
395
396 /* Check if the user set a language, else set default */
397 if(!currentlanguage)
398 currentlanguage = new_language(0, 0);
399
400 /* Generate appropriate outfile names */
401 if(!output_name && !preprocess_only)
402 {
403 output_name = dup_basename(input_name, ".rc");
404 strcat(output_name, ".res");
405 }
406
407 /* Run the preprocessor on the input */
408 if(!no_preprocess)
409 {
410 /*
411 * Preprocess the input to a temp-file, or stdout if
412 * no output was given.
413 */
414
415 chat("Starting preprocess");
416
417 if (!preprocess_only)
418 {
419 atexit(rm_tempfile);
420 ret = wpp_parse_temp( input_name, output_name, &temp_name );
421 }
422 else if (output_name)
423 {
424 FILE *output;
425
426 if (!(output = fopen( output_name, "w" )))
427 error( "Could not open %s for writing\n", output_name );
428 ret = wpp_parse( input_name, output );
429 fclose( output );
430 }
431 else
432 {
433 ret = wpp_parse( input_name, stdout );
434 }
435
436 if(ret)
437 exit(1); /* Error during preprocess */
438
439 if(preprocess_only)
440 exit(0);
441
442 input_name = temp_name;
443 }
444
445 /* Go from .rc to .res */
446 chat("Starting parse");
447
448 if(!(yyin = fopen(input_name, "rb")))
449 error("Could not open %s for input\n", input_name);
450
451 ret = yyparse();
452
453 if(input_name) fclose(yyin);
454
455 if(ret) exit(1); /* Error during parse */
456
457 if(debuglevel & DEBUGLEVEL_DUMP)
458 dump_resources(resource_top);
459
460 if(verify_translations_mode)
461 {
462 verify_translations(resource_top);
463 exit(0);
464 }
465
466 /* Convert the internal lists to binary data */
467 resources2res(resource_top);
468
469 chat("Writing .res-file");
470 write_resfile(output_name, resource_top);
471
472 return 0;
473 }
474
475
476 static void rm_tempfile(void)
477 {
478 if(temp_name)
479 unlink(temp_name);
480 }
481
482 static void segvhandler(int sig)
483 {
484 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
485 fflush(stdout);
486 fflush(stderr);
487 abort();
488 }