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

Last change on this file since 21589 was 21589, checked in by dmik, 15 years ago

tools/wrc: Restored generating GAS output by default. MASM output is now generated by specifying the -M option.

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