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

Last change on this file since 10367 was 6113, checked in by bird, 24 years ago

Don't include unistd.h for VAC (inlcudes TCPIP stuff). Bad length assumption.

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