Include protection for <unistd.h>, <sys/types.h> and <sys/stat.h>.
[wine.git] / tools / wrc / wrc.c
blobde3a0ccc6adc07a48658b92aad966d83bc9b8ac4
1 /*
2 * Copyright Martin von Loewis, 1994
3 * Copyrignt 1998 Bertho A. Stultiens (BS)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * History:
20 * 30-Apr-2000 BS - Integrated a new preprocessor (-E and -N)
21 * 20-Jun-1998 BS - Added -L option to prevent case conversion
22 * of embedded filenames.
24 * 08-Jun-1998 BS - Added -A option to generate autoregister code
25 * for winelib operation.
27 * 21-May-1998 BS - Removed the CPP option. Its internal now.
28 * - Added implementations for defines and includes
29 * on the commandline.
31 * 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
32 * Seems to be a sign for too much freedom. I implemeted
33 * most of them as a user choice possibility for things
34 * that I do not know what to put there by default.
35 * - -l and -L options are now known as -t and -T.
37 * 23-Apr-1998 BS - Finally gave up on backward compatibility on the
38 * commandline (after a blessing from the newsgroup).
39 * So, I changed the lot.
41 * 17-Apr-1998 BS - Added many new command-line options but took care
42 * that it would not break old scripts (sigh).
44 * 16-Apr-1998 BS - There is not much left of the original source...
45 * I had to rewrite most of it because the parser
46 * changed completely with all the types etc..
50 #include "config.h"
51 #include "wine/port.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55 #ifdef HAVE_UNISTD_H
56 # include <unistd.h>
57 #endif
58 #include <string.h>
59 #include <assert.h>
60 #include <ctype.h>
61 #include <signal.h>
63 #include "wrc.h"
64 #include "utils.h"
65 #include "writeres.h"
66 #include "readres.h"
67 #include "dumpres.h"
68 #include "genres.h"
69 #include "newstruc.h"
70 #include "parser.h"
71 #include "../wpp/wpp.h"
73 static char usage[] =
74 "Usage: wrc [options...] [infile[.rc|.res]]\n"
75 " -a n Alignment of resource (win16 only, default is 4)\n"
76 " -A Auto register resources (only with gcc 2.7 and better)\n"
77 " -b Create an assembly array from a binary .res file\n"
78 " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
79 " (win32 only; default is n[ative] which equals "
80 #ifdef WORDS_BIGENDIAN
81 "big"
82 #else
83 "little"
84 #endif
85 "-endian)\n"
86 " -c Add 'const' prefix to C constants\n"
87 " -C cp Set the resource's codepage to cp (default is 0)\n"
88 " -d n Set debug level to 'n'\n"
89 " -D id[=val] Define preprocessor identifier id=val\n"
90 " -e Disable recognition of win32 keywords in 16bit compile\n"
91 " -E Preprocess only\n"
92 " -g Add symbols to the global c namespace\n"
93 " -h Also generate a .h file\n"
94 " -H file Same as -h but written to file\n"
95 " -I path Set include search dir to path (multiple -I allowed)\n"
96 " -l lan Set default language to lan (default is neutral {0, 0})\n"
97 " -L Leave case of embedded filenames as is\n"
98 " -m Do not remap numerical resource IDs\n"
99 " -n Do not generate .s file\n"
100 " -N Do not preprocess input\n"
101 " -o file Output to file (default is infile.[res|s|h]\n"
102 " -p prefix Give a prefix for the generated names\n"
103 " -r Create binary .res file (compile only)\n"
104 " -s Add structure with win32/16 (PE/NE) resource directory\n"
105 " -t Generate indirect loadable resource tables\n"
106 " -T Generate only indirect loadable resources tables\n"
107 " -V Print version end exit\n"
108 " -w 16|32 Select win16 or win32 output (default is win32)\n"
109 " -W Enable pedantic warnings\n"
110 "Input is taken from stdin if no sourcefile specified.\n"
111 "Debug level 'n' is a bitmask with following meaning:\n"
112 " * 0x01 Tell which resource is parsed (verbose mode)\n"
113 " * 0x02 Dump internal structures\n"
114 " * 0x04 Create a parser trace (yydebug=1)\n"
115 " * 0x08 Preprocessor messages\n"
116 " * 0x10 Preprocessor lex messages\n"
117 " * 0x20 Preprocessor yacc trace\n"
118 "The -o option only applies to the final destination file, which is\n"
119 "in case of normal compile a .s file. You must use the '-H header.h'\n"
120 "option to override the header-filename.\n"
121 "If no input filename is given and the output name is not overridden\n"
122 "with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
125 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
126 "Copyright 1998-2000 Bertho A. Stultiens\n"
127 " 1994 Martin von Loewis\n";
130 * Default prefix for resource names used in the C array.
131 * Option '-p name' sets it to 'name'
133 char *prefix = __ASM_NAME("_Resource");
136 * Set if compiling in 32bit mode (default).
138 int win32 = 1;
141 * Set when generated C variables should be prefixed with 'const'
143 int constant = 0;
146 * Create a .res file from the source and exit (-r option).
148 int create_res = 0;
151 * debuglevel == DEBUGLEVEL_NONE Don't bother
152 * debuglevel & DEBUGLEVEL_CHAT Say whats done
153 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
154 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
155 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
156 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
157 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
159 int debuglevel = DEBUGLEVEL_NONE;
162 * Recognize win32 keywords if set (-w 32 enforces this),
163 * otherwise set with -e option.
165 int extensions = 1;
168 * Set when creating C array from .res file (-b option).
170 int binary = 0;
173 * Set when an additional C-header is to be created in compile (-h option).
175 int create_header = 0;
178 * Set when the NE/PE resource directory should be dumped into
179 * the output file.
181 int create_dir = 0;
184 * Set when all symbols should be added to the global namespace (-g option)
186 int global = 0;
189 * Set when indirect loadable resource tables should be created (-t)
191 int indirect = 0;
194 * Set when _only_ indirect loadable resource tables should be created (-T)
196 int indirect_only = 0;
199 * NE segment resource aligment (-a option)
201 int alignment = 4;
202 int alignment_pwr;
205 * Cleared when the assembly file must be suppressed (-n option)
207 int create_s = 1;
210 * Language setting for resources (-l option)
212 language_t *currentlanguage = NULL;
215 * The codepage to write in win32 PE resource segment (-C option)
217 DWORD codepage = 0;
220 * Set when extra warnings should be generated (-W option)
222 int pedantic = 0;
225 * Set when autoregister code must be added to the output (-A option)
227 int auto_register = 0;
230 * Set when the case of embedded filenames should not be converted
231 * to lower case (-L option)
233 int leave_case = 0;
236 * The output byte-order of resources (set with -B)
238 int byteorder = WRC_BO_NATIVE;
241 * Set when _only_ to run the preprocessor (-E option)
243 int preprocess_only = 0;
246 * Set when _not_ to run the preprocessor (-N option)
248 int no_preprocess = 0;
251 * Cleared when _not_ to remap resource types (-m option)
253 int remap = 1;
255 char *output_name; /* The name given by the -o option */
256 char *input_name; /* The name given on the command-line */
257 char *header_name; /* The name given by the -H option */
258 char *temp_name; /* Temporary file for preprocess pipe */
260 int line_number = 1; /* The current line */
261 int char_number = 1; /* The current char pos within the line */
263 char *cmdline; /* The entire commandline */
264 time_t now; /* The time of start of wrc */
266 resource_t *resource_top; /* The top of the parsed resources */
268 int getopt (int argc, char *const *argv, const char *optstring);
269 static void rm_tempfile(void);
270 static void segvhandler(int sig);
272 int main(int argc,char *argv[])
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;
283 signal(SIGSEGV, segvhandler);
285 now = time(NULL);
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 " */
290 for(i = 1; i < argc; i++)
291 cmdlen += strlen(argv[i]) + 1;
292 cmdline = (char *)xmalloc(cmdlen);
293 strcpy(cmdline, "wrc ");
294 for(i = 1; i < argc; i++)
296 strcat(cmdline, argv[i]);
297 if(i < argc-1)
298 strcat(cmdline, " ");
301 while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF)
303 switch(optc)
305 case 'a':
306 alignment = atoi(optarg);
307 break;
308 case 'A':
309 auto_register = 1;
310 break;
311 case 'b':
312 binary = 1;
313 break;
314 case 'B':
315 switch(optarg[0])
317 case 'n':
318 case 'N':
319 byteorder = WRC_BO_NATIVE;
320 break;
321 case 'l':
322 case 'L':
323 byteorder = WRC_BO_LITTLE;
324 break;
325 case 'b':
326 case 'B':
327 byteorder = WRC_BO_BIG;
328 break;
329 default:
330 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
331 lose++;
333 break;
334 case 'c':
335 constant = 1;
336 break;
337 case 'C':
338 codepage = strtol(optarg, NULL, 0);
339 break;
340 case 'd':
341 debuglevel = strtol(optarg, NULL, 0);
342 break;
343 case 'D':
344 wpp_add_cmdline_define(optarg);
345 break;
346 case 'e':
347 extensions = 0;
348 break;
349 case 'E':
350 preprocess_only = 1;
351 break;
352 case 'g':
353 global = 1;
354 break;
355 case 'H':
356 header_name = strdup(optarg);
357 /* Fall through */
358 case 'h':
359 create_header = 1;
360 break;
361 case 'I':
362 wpp_add_include_path(optarg);
363 break;
364 case 'l':
366 int lan;
367 lan = strtol(optarg, NULL, 0);
368 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
369 error("Language %04x is not supported",lan);
370 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
372 break;
373 case 'L':
374 leave_case = 1;
375 break;
376 case 'm':
377 remap = 0;
378 break;
379 case 'n':
380 create_s = 0;
381 break;
382 case 'N':
383 no_preprocess = 1;
384 break;
385 case 'o':
386 output_name = strdup(optarg);
387 break;
388 case 'p':
389 prefix = xstrdup(optarg);
390 break;
391 case 'r':
392 create_res = 1;
393 break;
394 case 's':
395 create_dir = 1;
396 break;
397 case 'T':
398 indirect_only = 1;
399 /* Fall through */
400 case 't':
401 indirect = 1;
402 break;
403 case 'V':
404 printf(version_string);
405 exit(0);
406 break;
407 case 'w':
408 if(!strcmp(optarg, "16"))
409 win32 = 0;
410 else if(!strcmp(optarg, "32"))
411 win32 = 1;
412 else
413 lose++;
414 break;
415 case 'W':
416 pedantic = 1;
417 wpp_set_pedantic(1);
418 break;
419 default:
420 lose++;
421 break;
425 if(lose)
427 fprintf(stderr, usage);
428 return 1;
431 /* Check the command line options for invalid combinations */
432 if(win32)
434 if(!extensions)
436 warning("Option -e ignored with 32bit compile\n");
437 extensions = 1;
441 if(create_res)
443 if(constant)
445 warning("Option -c ignored with compile to .res\n");
446 constant = 0;
449 if(create_header)
451 warning("Option -[h|H] ignored with compile to .res\n");
452 create_header = 0;
455 if(indirect)
457 warning("Option -t ignored with compile to .res\n");
458 indirect = 0;
461 if(indirect_only)
463 warning("Option -T ignored with compile to .res\n");
464 indirect_only = 0;
467 if(global)
469 warning("Option -g ignored with compile to .res\n");
470 global = 0;
473 if(create_dir)
475 error("Option -r and -s cannot be used together\n");
478 if(binary)
480 error("Option -r and -b cannot be used together\n");
484 if(byteorder != WRC_BO_NATIVE)
486 if(binary)
487 error("Forced byteordering not supported for binary resources\n");
490 if(preprocess_only)
492 if(constant)
494 warning("Option -c ignored with preprocess only\n");
495 constant = 0;
498 if(create_header)
500 warning("Option -[h|H] ignored with preprocess only\n");
501 create_header = 0;
504 if(indirect)
506 warning("Option -t ignored with preprocess only\n");
507 indirect = 0;
510 if(indirect_only)
512 error("Option -E and -T cannot be used together\n");
515 if(global)
517 warning("Option -g ignored with preprocess only\n");
518 global = 0;
521 if(create_dir)
523 warning("Option -s ignored with preprocess only\n");
524 create_dir = 0;
527 if(binary)
529 error("Option -E and -b cannot be used together\n");
532 if(no_preprocess)
534 error("Option -E and -N cannot be used together\n");
538 #if !defined(HAVE_WINE_CONSTRUCTOR)
539 if(auto_register)
541 warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
542 auto_register = 0;
544 #endif
546 /* Set alignment power */
547 a = alignment;
548 for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
550 a >>= 1;
552 if(a != 1)
554 error("Alignment must be between 1 and 1024");
556 if((1 << alignment_pwr) != alignment)
558 error("Alignment must be a power of 2");
561 /* Kill io buffering when some kind of debuglevel is enabled */
562 if(debuglevel)
564 setbuf(stdout,0);
565 setbuf(stderr,0);
568 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
569 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
571 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
572 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
573 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
575 /* Set the default defined stuff */
576 wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
577 wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
578 wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
579 wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
581 wpp_add_cmdline_define("RC_INVOKED=1");
583 if(win32)
585 wpp_add_cmdline_define("__WIN32__=1");
586 wpp_add_cmdline_define("__FLAT__=1");
589 /* Check if the user set a language, else set default */
590 if(!currentlanguage)
591 currentlanguage = new_language(0, 0);
593 /* Check for input file on command-line */
594 if(optind < argc)
596 input_name = argv[optind];
599 if(binary && !input_name)
601 error("Binary mode requires .res file as input\n");
604 /* Generate appropriate outfile names */
605 if(!output_name && !preprocess_only)
607 output_name = dup_basename(input_name, binary ? ".res" : ".rc");
608 strcat(output_name, create_res ? ".res" : ".s");
611 if(!header_name && !create_res)
613 header_name = dup_basename(input_name, binary ? ".res" : ".rc");
614 strcat(header_name, ".h");
617 /* Run the preprocessor on the input */
618 if(!no_preprocess && !binary)
621 * Preprocess the input to a temp-file, or stdout if
622 * no output was given.
625 chat("Starting preprocess");
627 if (!preprocess_only)
629 atexit(rm_tempfile);
630 ret = wpp_parse_temp( input_name, &temp_name );
632 else if (output_name)
634 FILE *output;
636 if (!(output = fopen( output_name, "w" )))
637 error( "Could not open %s for writing\n", output_name );
638 ret = wpp_parse( input_name, output );
639 fclose( output );
641 else
643 ret = wpp_parse( input_name, stdout );
646 if(ret)
647 exit(1); /* Error during preprocess */
649 if(preprocess_only)
650 exit(0);
652 input_name = temp_name;
655 if(!binary)
657 /* Go from .rc to .res or .s */
658 chat("Starting parse");
660 if(!(yyin = fopen(input_name, "rb")))
661 error("Could not open %s for input\n", input_name);
663 ret = yyparse();
665 if(input_name)
666 fclose(yyin);
668 if(ret)
670 /* Error during parse */
671 exit(1);
674 if(debuglevel & DEBUGLEVEL_DUMP)
675 dump_resources(resource_top);
677 /* Convert the internal lists to binary data */
678 resources2res(resource_top);
680 if(create_res)
682 chat("Writing .res-file");
683 write_resfile(output_name, resource_top);
685 else
687 if(create_s)
689 chat("Writing .s-file");
690 write_s_file(output_name, resource_top);
692 if(create_header)
694 chat("Writing .h-file");
695 write_h_file(header_name, resource_top);
700 else
702 /* Go from .res to .s */
703 chat("Reading .res-file");
704 resource_top = read_resfile(input_name);
705 if(create_s)
707 chat("Writing .s-file");
708 write_s_file(output_name, resource_top);
710 if(create_header)
712 chat("Writing .h-file");
713 write_h_file(header_name, resource_top);
717 return 0;
721 static void rm_tempfile(void)
723 if(temp_name)
724 unlink(temp_name);
727 static void segvhandler(int sig)
729 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
730 fflush(stdout);
731 fflush(stderr);
732 abort();