source: trunk/tools/wrc/wrc.c@ 5523

Last change on this file since 5523 was 5523, checked in by sandervl, 24 years ago

updates

File size: 16.3 KB
Line 
1/*
2 *
3 * Copyright Martin von Loewis, 1994
4 * Copyrignt 1998 Bertho A. Stultiens (BS)
5 *
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.
9 *
10 * 08-Jun-1998 BS - Added -A option to generate autoregister code
11 * for winelib operation.
12 *
13 * 21-May-1998 BS - Removed the CPP option. Its internal now.
14 * - Added implementations for defines and includes
15 * on the commandline.
16 *
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.
22 *
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.
26 *
27 * 17-Apr-1998 BS - Added many new command-line options but took care
28 * that it would not break old scripts (sigh).
29 *
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..
33 *
34 */
35
36#include "config.h"
37
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>
45
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"
55
56static char usage[] =
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
64 "big"
65#else
66 "little"
67#endif
68 "-endian)\n"
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 " -m Do not remap numerical resource IDs\n"
82 " -n Do not generate .s file\n"
83 " -N Do not preprocess input\n"
84 " -o file Output to file (default is infile.[res|s|h]\n"
85 " -p prefix Give a prefix for the generated names\n"
86 " -r Create binary .res file (compile only)\n"
87 " -s Add structure with win32/16 (PE/NE) resource directory\n"
88 " -t Generate indirect loadable resource tables\n"
89 " -T Generate only indirect loadable resources tables\n"
90 " -V Print version end exit\n"
91 " -w 16|32 Select win16 or win32 output (default is win32)\n"
92 " -W Enable pedantic warnings\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 "The -o option only applies to the final destination file, which is\n"
102 "in case of normal compile a .s file. You must use the '-H header.h'\n"
103 "option to override the header-filename.\n"
104 "If no input filename is given and the output name is not overridden\n"
105 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
106 ;
107
108char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
109 "Copyright 1998-2000 Bertho A. Stultiens\n"
110 " 1994 Martin von Loewis\n";
111
112/*
113 * Default prefix for resource names used in the C array.
114 * Option '-p name' sets it to 'name'
115 */
116#ifdef NEED_UNDERSCORE_PREFIX
117char *prefix = "__Resource";
118#else
119char *prefix = "_Resource";
120#endif
121
122/*
123 * Set if compiling in 32bit mode (default).
124 */
125int win32 = 1;
126
127/*
128 * Set when generated C variables should be prefixed with 'const'
129 */
130int constant = 0;
131
132/*
133 * Create a .res file from the source and exit (-r option).
134 */
135int create_res = 0;
136
137/*
138 * debuglevel == DEBUGLEVEL_NONE Don't bother
139 * debuglevel & DEBUGLEVEL_CHAT Say whats done
140 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
141 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
142 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
143 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
144 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
145 */
146int debuglevel = DEBUGLEVEL_NONE;
147
148/*
149 * Recognize win32 keywords if set (-w 32 enforces this),
150 * otherwise set with -e option.
151 */
152int extensions = 1;
153
154/*
155 * Set when creating C array from .res file (-b option).
156 */
157int binary = 0;
158
159/*
160 * Set when an additional C-header is to be created in compile (-h option).
161 */
162int create_header = 0;
163
164/*
165 * Set when the NE/PE resource directory should be dumped into
166 * the output file.
167 */
168int create_dir = 0;
169
170/*
171 * Set when all symbols should be added to the global namespace (-g option)
172 */
173int global = 0;
174
175/*
176 * Set when indirect loadable resource tables should be created (-t)
177 */
178int indirect = 0;
179
180/*
181 * Set when _only_ indirect loadable resource tables should be created (-T)
182 */
183int indirect_only = 0;
184
185/*
186 * NE segment resource aligment (-a option)
187 */
188int alignment = 4;
189int alignment_pwr;
190
191/*
192 * Cleared when the assembly file must be suppressed (-n option)
193 */
194int create_s = 1;
195
196/*
197 * Language setting for resources (-l option)
198 */
199language_t *currentlanguage = NULL;
200
201/*
202 * The codepage to write in win32 PE resource segment (-C option)
203 */
204DWORD codepage = 0;
205
206/*
207 * Set when extra warnings should be generated (-W option)
208 */
209int pedantic = 0;
210
211/*
212 * Set when autoregister code must be added to the output (-A option)
213 */
214int auto_register = 0;
215
216/*
217 * Set when the case of embedded filenames should not be converted
218 * to lower case (-L option)
219 */
220int leave_case = 0;
221
222/*
223 * The output byte-order of resources (set with -B)
224 */
225int byteorder = WRC_BO_NATIVE;
226
227/*
228 * Set when _only_ to run the preprocessor (-E option)
229 */
230int preprocess_only = 0;
231
232/*
233 * Set when _not_ to run the preprocessor (-N option)
234 */
235int no_preprocess = 0;
236
237/*
238 * Cleared when _not_ to remap resource types (-m option)
239 */
240int remap = 1;
241
242char *output_name; /* The name given by the -o option */
243char *input_name; /* The name given on the command-line */
244char *header_name; /* The name given by the -H option */
245char *temp_name; /* Temporary file for preprocess pipe */
246
247int line_number = 1; /* The current line */
248int char_number = 1; /* The current char pos within the line */
249
250char *cmdline; /* The entire commandline */
251time_t now; /* The time of start of wrc */
252
253resource_t *resource_top; /* The top of the parsed resources */
254
255#ifdef __EMX__
256int getopt (int argc, char **argv, const char *optstring);
257#else
258int getopt (int argc, char *const *argv, const char *optstring);
259#endif
260
261static void rm_tempfile(void);
262static void segvhandler(int sig);
263
264int main(int argc,char *argv[])
265{
266 extern char* optarg;
267 extern int optind;
268 int optc;
269 int lose = 0;
270 int ret;
271 int a;
272 int i;
273 int cmdlen;
274
275 signal(SIGSEGV, segvhandler);
276
277 now = time(NULL);
278
279 /* First rebuild the commandline to put in destination */
280 /* Could be done through env[], but not all OS-es support it */
281 cmdlen = 4; /* for "wrc " */
282 for(i = 1; i < argc; i++)
283 cmdlen += strlen(argv[i]) + 1;
284 cmdline = (char *)xmalloc(cmdlen);
285 strcpy(cmdline, "wrc ");
286 for(i = 1; i < argc; i++)
287 {
288 strcat(cmdline, argv[i]);
289 if(i < argc-1)
290 strcat(cmdline, " ");
291 }
292
293 while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF)
294 {
295 switch(optc)
296 {
297 case 'a':
298 alignment = atoi(optarg);
299 break;
300 case 'A':
301 auto_register = 1;
302 break;
303 case 'b':
304 binary = 1;
305 break;
306 case 'B':
307 switch(optarg[0])
308 {
309 case 'n':
310 case 'N':
311 byteorder = WRC_BO_NATIVE;
312 break;
313 case 'l':
314 case 'L':
315 byteorder = WRC_BO_LITTLE;
316 break;
317 case 'b':
318 case 'B':
319 byteorder = WRC_BO_BIG;
320 break;
321 default:
322 fprintf(stderr, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
323 lose++;
324 }
325 break;
326 case 'c':
327 constant = 1;
328 break;
329 case 'C':
330 codepage = strtol(optarg, NULL, 0);
331 break;
332 case 'd':
333 debuglevel = strtol(optarg, NULL, 0);
334 break;
335 case 'D':
336 add_cmdline_define(optarg);
337 break;
338 case 'e':
339 extensions = 0;
340 break;
341 case 'E':
342 preprocess_only = 1;
343 break;
344 case 'g':
345 global = 1;
346 break;
347 case 'H':
348 header_name = strdup(optarg);
349 /* Fall through */
350 case 'h':
351 create_header = 1;
352 break;
353 case 'I':
354 add_include_path(optarg);
355 break;
356 case 'l':
357 {
358 int lan;
359 lan = strtol(optarg, NULL, 0);
360 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
361 }
362 break;
363 case 'L':
364 leave_case = 1;
365 break;
366 case 'm':
367 remap = 0;
368 break;
369 case 'n':
370 create_s = 0;
371 break;
372 case 'N':
373 no_preprocess = 1;
374 break;
375 case 'o':
376 output_name = strdup(optarg);
377 break;
378 case 'p':
379#ifdef NEED_UNDERSCORE_PREFIX
380 prefix = (char *)xmalloc(strlen(optarg)+2);
381 prefix[0] = '_';
382 strcpy(prefix+1, optarg);
383#else
384 prefix = xstrdup(optarg);
385#endif
386 break;
387 case 'r':
388 create_res = 1;
389 break;
390 case 's':
391 create_dir = 1;
392 break;
393 case 'T':
394 indirect_only = 1;
395 /* Fall through */
396 case 't':
397 indirect = 1;
398 break;
399 case 'V':
400 printf(version_string);
401 exit(0);
402 break;
403 case 'w':
404 if(!strcmp(optarg, "16"))
405 win32 = 0;
406 else if(!strcmp(optarg, "32"))
407 win32 = 1;
408 else
409 lose++;
410 break;
411 case 'W':
412 pedantic = 1;
413 break;
414 default:
415 lose++;
416 break;
417 }
418 }
419
420 if(lose)
421 {
422 fprintf(stderr, usage);
423 return 1;
424 }
425
426 /* Check the command line options for invalid combinations */
427 if(win32)
428 {
429 if(!extensions)
430 {
431 warning("Option -e ignored with 32bit compile\n");
432 extensions = 1;
433 }
434 }
435
436 if(create_res)
437 {
438 if(constant)
439 {
440 warning("Option -c ignored with compile to .res\n");
441 constant = 0;
442 }
443
444 if(create_header)
445 {
446 warning("Option -[h|H] ignored with compile to .res\n");
447 create_header = 0;
448 }
449
450 if(indirect)
451 {
452 warning("Option -l ignored with compile to .res\n");
453 indirect = 0;
454 }
455
456 if(indirect_only)
457 {
458 warning("Option -L ignored with compile to .res\n");
459 indirect_only = 0;
460 }
461
462 if(global)
463 {
464 warning("Option -g ignored with compile to .res\n");
465 global = 0;
466 }
467
468 if(create_dir)
469 {
470 error("Option -r and -s cannot be used together\n");
471 }
472
473 if(binary)
474 {
475 error("Option -r and -b cannot be used together\n");
476 }
477 }
478
479 if(byteorder != WRC_BO_NATIVE)
480 {
481 if(binary)
482 error("Forced byteordering not supported for binary resources\n");
483 }
484
485 if(preprocess_only)
486 {
487 if(constant)
488 {
489 warning("Option -c ignored with preprocess only\n");
490 constant = 0;
491 }
492
493 if(create_header)
494 {
495 warning("Option -[h|H] ignored with preprocess only\n");
496 create_header = 0;
497 }
498
499 if(indirect)
500 {
501 warning("Option -l ignored with preprocess only\n");
502 indirect = 0;
503 }
504
505 if(indirect_only)
506 {
507 error("Option -E and -L cannot be used together\n");
508 }
509
510 if(global)
511 {
512 warning("Option -g ignored with preprocess only\n");
513 global = 0;
514 }
515
516 if(create_dir)
517 {
518 warning("Option -s ignored with preprocess only\n");
519 create_dir = 0;
520 }
521
522 if(binary)
523 {
524 error("Option -E and -b cannot be used together\n");
525 }
526
527 if(no_preprocess)
528 {
529 error("Option -E and -N cannot be used together\n");
530 }
531 }
532
533#if !defined(HAVE_WINE_CONSTRUCTOR)
534 if(auto_register)
535 {
536 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
537 auto_register = 0;
538 }
539#endif
540
541 /* Set alignment power */
542 a = alignment;
543 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
544 {
545 a >>= 1;
546 }
547 if(a != 1)
548 {
549 error("Alignment must be between 1 and 1024");
550 }
551 if((1 << alignment_pwr) != alignment)
552 {
553 error("Alignment must be a power of 2");
554 }
555
556 /* Kill io buffering when some kind of debuglevel is enabled */
557 if(debuglevel)
558 {
559 setbuf(stdout,0);
560 setbuf(stderr,0);
561 }
562
563 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
564 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
565 ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
566 pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;
567
568 /* Set the default defined stuff */
569 add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
570 add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
571 add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
572 add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
573
574 add_cmdline_define("RC_INVOKED=1");
575
576 if(win32)
577 {
578 add_cmdline_define("__WIN32__=1");
579 add_cmdline_define("__FLAT__=1");
580 }
581
582 add_special_define("__FILE__");
583 add_special_define("__LINE__");
584 add_special_define("__DATE__");
585 add_special_define("__TIME__");
586
587 /* Check if the user set a language, else set default */
588 if(!currentlanguage)
589 currentlanguage = new_language(0, 0);
590
591 /* Check for input file on command-line */
592 if(optind < argc)
593 {
594 input_name = argv[optind];
595 }
596
597 if(binary && !input_name)
598 {
599 error("Binary mode requires .res file as input\n");
600 }
601
602 /* Generate appropriate outfile names */
603 if(!output_name && !preprocess_only)
604 {
605 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
606 strcat(output_name, create_res ? ".res" : ".s");
607 }
608
609 if(!header_name && !create_res)
610 {
611 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
612 strcat(header_name, ".h");
613 }
614
615 /* Run the preprocessor on the input */
616 if(!no_preprocess && !binary)
617 {
618 char *real_name;
619 /*
620 * Preprocess the input to a temp-file, or stdout if
621 * no output was given.
622 */
623
624 chat("Starting preprocess");
625
626 if(preprocess_only && !output_name)
627 {
628 ppout = stdout;
629 }
630 else if(preprocess_only && output_name)
631 {
632 if(!(ppout = fopen(output_name, "wb")))
633 error("Could not open %s for writing\n", output_name);
634 }
635 else
636 {
637 if(!(temp_name = tmpnam(NULL)))
638 error("Could nor generate a temp-name\n");
639 temp_name = xstrdup(temp_name);
640 if(!(ppout = fopen(temp_name, "wb")))
641 error("Could not create a temp-file\n");
642
643 atexit(rm_tempfile);
644 }
645
646 real_name = input_name; /* Because it gets overwritten */
647
648 if(!input_name)
649 ppin = stdin;
650 else
651 {
652 if(!(ppin = fopen(input_name, "rb")))
653 error("Could not open %s\n", input_name);
654 }
655
656 fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
657
658 ret = ppparse();
659
660 input_name = real_name;
661
662 if(input_name)
663 fclose(ppin);
664
665 fclose(ppout);
666
667 input_name = temp_name;
668
669 if(ret)
670 exit(1); /* Error during preprocess */
671
672 if(preprocess_only)
673 exit(0);
674 }
675
676 if(!binary)
677 {
678 /* Go from .rc to .res or .s */
679 chat("Starting parse");
680
681 if(!(yyin = fopen(input_name, "rb")))
682 error("Could not open %s for input\n", input_name);
683
684 ret = yyparse();
685
686 if(input_name)
687 fclose(yyin);
688
689 if(ret)
690 {
691 /* Error during parse */
692 exit(1);
693 }
694
695 if(debuglevel & DEBUGLEVEL_DUMP)
696 dump_resources(resource_top);
697
698 /* Convert the internal lists to binary data */
699 resources2res(resource_top);
700
701 if(create_res)
702 {
703 chat("Writing .res-file");
704 write_resfile(output_name, resource_top);
705 }
706 else
707 {
708 if(create_s)
709 {
710 chat("Writing .s-file");
711 write_s_file(output_name, resource_top);
712 }
713 if(create_header)
714 {
715 chat("Writing .h-file");
716 write_h_file(header_name, resource_top);
717 }
718 }
719
720 }
721 else
722 {
723 /* Go from .res to .s */
724 chat("Reading .res-file");
725 resource_top = read_resfile(input_name);
726 if(create_s)
727 {
728 chat("Writing .s-file");
729 write_s_file(output_name, resource_top);
730 }
731 if(create_header)
732 {
733 chat("Writing .h-file");
734 write_h_file(header_name, resource_top);
735 }
736 }
737
738 return 0;
739}
740
741
742static void rm_tempfile(void)
743{
744 if(temp_name)
745 unlink(temp_name);
746}
747
748static void segvhandler(int sig)
749{
750 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
751 fflush(stdout);
752 fflush(stderr);
753 abort();
754}
755
Note: See TracBrowser for help on using the repository browser.