- Implemented a new preprocessor that is (nearly) ANSI-C compliant. The
[wine.git] / tools / wrc / wrc.c
blob77bf932a06a37e40c3ed2cb985f6bc4c101957e5
1 /*
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
15 * on the commandline.
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..
36 #include "config.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <assert.h>
43 #include <ctype.h>
44 #include <signal.h>
46 #include "wrc.h"
47 #include "utils.h"
48 #include "writeres.h"
49 #include "readres.h"
50 #include "dumpres.h"
51 #include "genres.h"
52 #include "newstruc.h"
53 #include "preproc.h"
54 #include "parser.h"
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";
108 #else
109 char *prefix = "_Resource";
110 #endif
113 * Set if compiling in 32bit mode (default).
115 int win32 = 1;
118 * Set when generated C variables should be prefixed with 'const'
120 int constant = 0;
123 * Create a .res file from the source and exit (-r option).
125 int create_res = 0;
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.
142 int extensions = 1;
145 * Set when creating C array from .res file (-b option).
147 int binary = 0;
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
156 * the output file.
158 int create_dir = 0;
161 * Set when all symbols should be added to the global namespace (-g option)
163 int global = 0;
166 * Set when indirect loadable resource tables should be created (-t)
168 int indirect = 0;
171 * Set when _only_ indirect loadable resource tables should be created (-T)
173 int indirect_only = 0;
176 * NE segment resource aligment (-a option)
178 int alignment = 4;
179 int alignment_pwr;
182 * Cleared when the assembly file must be suppressed (-n option)
184 int create_s = 1;
187 * Language setting for resources (-l option)
189 language_t *currentlanguage = NULL;
192 * The codepage to write in win32 PE resource segment (-C option)
194 DWORD codepage = 0;
197 * Set when extra warnings should be generated (-W option)
199 int pedantic = 0;
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)
210 int leave_case = 0;
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[])
241 extern char* optarg;
242 extern int optind;
243 int optc;
244 int lose = 0;
245 int ret;
246 int a;
247 int i;
248 int cmdlen;
250 signal(SIGSEGV, segvhandler);
252 now = time(NULL);
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]);
264 if(i < argc-1)
265 strcat(cmdline, " ");
268 while((optc = getopt(argc, argv, "a:AbcC:d:D:eEghH:I:l:LnNo:p:rstTVw:W")) != EOF)
270 switch(optc)
272 case 'a':
273 alignment = atoi(optarg);
274 break;
275 case 'A':
276 auto_register = 1;
277 break;
278 case 'b':
279 binary = 1;
280 break;
281 case 'c':
282 constant = 1;
283 break;
284 case 'C':
285 codepage = strtol(optarg, NULL, 0);
286 break;
287 case 'd':
288 debuglevel = strtol(optarg, NULL, 0);
289 break;
290 case 'D':
291 add_cmdline_define(optarg);
292 break;
293 case 'e':
294 extensions = 0;
295 break;
296 case 'E':
297 preprocess_only = 1;
298 break;
299 case 'g':
300 global = 1;
301 break;
302 case 'H':
303 header_name = strdup(optarg);
304 /* Fall through */
305 case 'h':
306 create_header = 1;
307 break;
308 case 'I':
309 add_include_path(optarg);
310 break;
311 case 'l':
313 int lan;
314 lan = strtol(optarg, NULL, 0);
315 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
317 break;
318 case 'L':
319 leave_case = 1;
320 break;
321 case 'n':
322 create_s = 0;
323 break;
324 case 'N':
325 no_preprocess = 1;
326 break;
327 case 'o':
328 output_name = strdup(optarg);
329 break;
330 case 'p':
331 #ifdef NEED_UNDERSCORE_PREFIX
332 prefix = (char *)xmalloc(strlen(optarg)+2);
333 prefix[0] = '_';
334 strcpy(prefix+1, optarg);
335 #else
336 prefix = xstrdup(optarg);
337 #endif
338 break;
339 case 'r':
340 create_res = 1;
341 break;
342 case 's':
343 create_dir = 1;
344 break;
345 case 'T':
346 indirect_only = 1;
347 /* Fall through */
348 case 't':
349 indirect = 1;
350 break;
351 case 'V':
352 printf(version_string);
353 exit(0);
354 break;
355 case 'w':
356 if(!strcmp(optarg, "16"))
357 win32 = 0;
358 else if(!strcmp(optarg, "32"))
359 win32 = 1;
360 else
361 lose++;
362 break;
363 case 'W':
364 pedantic = 1;
365 break;
366 default:
367 lose++;
368 break;
372 if(lose)
374 fprintf(stderr, usage);
375 return 1;
378 /* Check the command line options for invalid combinations */
379 if(win32)
381 if(!extensions)
383 warning("Option -e ignored with 32bit compile\n");
384 extensions = 1;
388 if(create_res)
390 if(constant)
392 warning("Option -c ignored with compile to .res\n");
393 constant = 0;
396 if(create_header)
398 warning("Option -[h|H] ignored with compile to .res\n");
399 create_header = 0;
402 if(indirect)
404 warning("Option -l ignored with compile to .res\n");
405 indirect = 0;
408 if(indirect_only)
410 warning("Option -L ignored with compile to .res\n");
411 indirect_only = 0;
414 if(global)
416 warning("Option -g ignored with compile to .res\n");
417 global = 0;
420 if(create_dir)
422 error("Option -r and -s cannot be used together\n");
425 if(binary)
427 error("Option -r and -b cannot be used together\n");
431 if(preprocess_only)
433 if(constant)
435 warning("Option -c ignored with preprocess only\n");
436 constant = 0;
439 if(create_header)
441 warning("Option -[h|H] ignored with preprocess only\n");
442 create_header = 0;
445 if(indirect)
447 warning("Option -l ignored with preprocess only\n");
448 indirect = 0;
451 if(indirect_only)
453 error("Option -E and -L cannot be used together\n");
456 if(global)
458 warning("Option -g ignored with preprocess only\n");
459 global = 0;
462 if(create_dir)
464 warning("Option -s ignored with preprocess only\n");
465 create_dir = 0;
468 if(binary)
470 error("Option -E and -b cannot be used together\n");
473 if(no_preprocess)
475 error("Option -E and -N cannot be used together\n");
479 #if !defined(HAVE_WINE_CONSTRUCTOR)
480 if(auto_register)
482 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
483 auto_register = 0;
485 #endif
487 /* Set alignment power */
488 a = alignment;
489 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
491 a >>= 1;
493 if(a != 1)
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 */
503 if(debuglevel)
505 setbuf(stdout,0);
506 setbuf(stderr,0);
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");
522 if(win32)
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 */
534 if(!currentlanguage)
535 currentlanguage = new_language(0, 0);
537 /* Check for input file on command-line */
538 if(optind < argc)
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)
564 char *real_name;
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)
574 ppout = stdout;
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);
581 else
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");
589 atexit(rm_tempfile);
592 real_name = input_name; /* Because it gets overwritten */
594 if(!input_name)
595 ppin = stdin;
596 else
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 : "");
604 ret = ppparse();
606 input_name = real_name;
608 if(input_name)
609 fclose(ppin);
611 fclose(ppout);
613 input_name = temp_name;
615 if(ret)
616 exit(1); /* Error during preprocess */
618 if(preprocess_only)
619 exit(0);
622 if(!binary)
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);
630 ret = yyparse();
632 if(input_name)
633 fclose(yyin);
635 if(ret)
637 /* Error during parse */
638 exit(1);
641 if(debuglevel & DEBUGLEVEL_DUMP)
642 dump_resources(resource_top);
644 /* Convert the internal lists to binary data */
645 resources2res(resource_top);
647 if(create_res)
649 chat("Writing .res-file");
650 write_resfile(output_name, resource_top);
652 else
654 if(create_s)
656 chat("Writing .s-file");
657 write_s_file(output_name, resource_top);
659 if(create_header)
661 chat("Writing .h-file");
662 write_h_file(header_name, resource_top);
667 else
669 /* Go from .res to .s */
670 chat("Reading .res-file");
671 resource_top = read_resfile(input_name);
672 if(create_s)
674 chat("Writing .s-file");
675 write_s_file(output_name, resource_top);
677 if(create_header)
679 chat("Writing .h-file");
680 write_h_file(header_name, resource_top);
684 return 0;
688 static void rm_tempfile(void)
690 if(temp_name)
691 unlink(temp_name);
694 static void segvhandler(int sig)
696 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
697 fflush(stdout);
698 fflush(stderr);
699 abort();