2 * Copyright 1994 Martin von Loewis
3 * Copyright 1998 Bertho A. Stultiens (BS)
4 * Copyright 2003 Dimitrie O. Paun
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
47 #ifdef WORDS_BIGENDIAN
50 #define ENDIAN "little"
53 static const char usage
[] =
54 "Usage: wrc [options...] [infile[.rc|.res]]\n"
55 " -D id[=val] Define preprocessor identifier id=val\n"
56 " -E Preprocess only\n"
57 " -F target Ignored for compatibility with windres\n"
58 " -h Prints this summary\n"
59 " -i file The name of the input file\n"
60 " -I path Set include search dir to path (multiple -I allowed)\n"
61 " -J format The input format (either `rc' or `rc16')\n"
62 " -l lan Set default language to lan (default is neutral {0, 0})\n"
63 " -o file Output to file (default is infile.res)\n"
64 " -O format The output format (either `res' or `res16`)\n"
65 " -r Ignored for compatibility with rc\n"
66 " -U id Undefine preprocessor identifier id\n"
67 " -v Enable verbose mode\n"
68 "The following long options are supported:\n"
69 " --debug=nn Set debug level to 'nn'\n"
70 " --define Synonym for -D\n"
71 " --endianess=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
72 " (win32 only; default is " ENDIAN
"-endian)\n"
73 " --help Synonym for -h\n"
74 " --include-dir Synonym for -I\n"
75 " --input Synonym for -i\n"
76 " --input-format Synonym for -J\n"
77 " --language Synonym for -l\n"
78 " --no-use-temp-file Ignored for compatibility with windres\n"
79 " --nostdinc Disables searching the standard include path\n"
80 " --output -fo Synonym for -o\n"
81 " --output-format Synonym for -O\n"
82 " --pedantic Enable pedantic warnings\n"
83 " --preprocessor Specifies the preprocessor to use, including arguments\n"
84 " --target Synonym for -F\n"
85 " --undefine Synonym for -U\n"
86 " --use-temp-file Ignored for compatibility with windres\n"
87 " --verbose Synonym for -v\n"
88 " --verify-translations Check the status of the various translations\n"
89 " --version Print version and exit\n"
90 "Input is taken from stdin if no sourcefile specified.\n"
91 "Debug level 'n' is a bitmask with following meaning:\n"
92 " * 0x01 Tell which resource is parsed (verbose mode)\n"
93 " * 0x02 Dump internal structures\n"
94 " * 0x04 Create a parser trace (yydebug=1)\n"
95 " * 0x08 Preprocessor messages\n"
96 " * 0x10 Preprocessor lex messages\n"
97 " * 0x20 Preprocessor yacc trace\n"
98 "If no input filename is given and the output name is not overridden\n"
99 "with -o, then the output is written to \"wrc.tab.res\"\n"
102 static const char version_string
[] = "Wine Resource Compiler version " PACKAGE_VERSION
"\n"
103 "Copyright 1998-2000 Bertho A. Stultiens\n"
104 " 1994 Martin von Loewis\n";
107 * Set if compiling in 32bit mode (default).
112 * debuglevel == DEBUGLEVEL_NONE Don't bother
113 * debuglevel & DEBUGLEVEL_CHAT Say what's done
114 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
115 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
116 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
117 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
118 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
120 int debuglevel
= DEBUGLEVEL_NONE
;
123 * Recognize win32 keywords if set (-w 32 enforces this),
124 * otherwise set with -e option.
129 * Language setting for resources (-l option)
131 static language_t
*defaultlanguage
;
132 language_t
*currentlanguage
= NULL
;
135 * Set when extra warnings should be generated (-W option)
140 * The output byte-order of resources (set with -B)
142 int byteorder
= WRC_BO_NATIVE
;
145 * Set when _only_ to run the preprocessor (-E option)
147 int preprocess_only
= 0;
150 * Set when _not_ to run the preprocessor (-P cat option)
152 int no_preprocess
= 0;
154 int check_utf8
= 1; /* whether to check for valid utf8 */
156 static int verify_translations_mode
;
158 char *output_name
= NULL
; /* The name given by the -o option */
159 char *input_name
= NULL
; /* The name given on the command-line */
160 static char *temp_name
= NULL
; /* Temporary file for preprocess pipe */
162 int line_number
= 1; /* The current line */
163 int char_number
= 1; /* The current char pos within the line */
165 char *cmdline
; /* The entire commandline */
166 time_t now
; /* The time of start of wrc */
168 int parser_debug
, yy_flex_debug
;
170 resource_t
*resource_top
; /* The top of the parsed resources */
172 int getopt (int argc
, char *const *argv
, const char *optstring
);
173 static void cleanup_files(void);
174 static void segvhandler(int sig
);
176 enum long_options_values
178 LONG_OPT_NOSTDINC
= 1,
181 LONG_OPT_PREPROCESSOR
,
186 LONG_OPT_VERIFY_TRANSL
189 static const char short_options
[] =
190 "D:Ef:F:hi:I:J:l:o:O:rU:v";
191 static const struct option long_options
[] = {
192 { "debug", 1, NULL
, LONG_OPT_DEBUG
},
193 { "define", 1, NULL
, 'D' },
194 { "endianess", 1, NULL
, LONG_OPT_ENDIANESS
},
195 { "help", 0, NULL
, 'h' },
196 { "include-dir", 1, NULL
, 'I' },
197 { "input", 1, NULL
, 'i' },
198 { "input-format", 1, NULL
, 'J' },
199 { "language", 1, NULL
, 'l' },
200 { "no-use-temp-file", 0, NULL
, LONG_OPT_NOTMPFILE
},
201 { "nostdinc", 0, NULL
, LONG_OPT_NOSTDINC
},
202 { "output", 1, NULL
, 'o' },
203 { "output-format", 1, NULL
, 'O' },
204 { "pedantic", 0, NULL
, LONG_OPT_PEDANTIC
},
205 { "preprocessor", 1, NULL
, LONG_OPT_PREPROCESSOR
},
206 { "target", 1, NULL
, 'F' },
207 { "undefine", 1, NULL
, 'U' },
208 { "use-temp-file", 0, NULL
, LONG_OPT_TMPFILE
},
209 { "verbose", 0, NULL
, 'v' },
210 { "verify-translations", 0, NULL
, LONG_OPT_VERIFY_TRANSL
},
211 { "version", 0, NULL
, LONG_OPT_VERSION
},
215 static void set_version_defines(void)
217 char *version
= xstrdup( PACKAGE_VERSION
);
218 char *major
, *minor
, *patchlevel
;
221 if ((minor
= strchr( version
, '.' )))
225 if ((patchlevel
= strchr( minor
, '.' ))) *patchlevel
++ = 0;
227 else /* pre 0.9 version */
230 patchlevel
= version
;
232 sprintf( buffer
, "__WRC__=%s", major
? major
: "0" );
233 wpp_add_cmdline_define(buffer
);
234 sprintf( buffer
, "__WRC_MINOR__=%s", minor
? minor
: "0" );
235 wpp_add_cmdline_define(buffer
);
236 sprintf( buffer
, "__WRC_PATCHLEVEL__=%s", patchlevel
? patchlevel
: "0" );
237 wpp_add_cmdline_define(buffer
);
241 /* clean things up when aborting on a signal */
242 static void exit_on_signal( int sig
)
244 exit(1); /* this will call the atexit functions */
247 /* load a single input file */
248 static int load_file( const char *input_name
, const char *output_name
)
252 /* Run the preprocessor on the input */
260 * Preprocess the input to a temp-file, or stdout if
261 * no output was given.
268 if (!(output
= fopen( output_name
, "w" )))
269 fatal_perror( "Could not open %s for writing", output_name
);
270 ret
= wpp_parse( input_name
, output
);
273 else ret
= wpp_parse( input_name
, stdout
);
280 if (output_name
&& output_name
[0])
282 name
= xmalloc( strlen(output_name
) + 8 );
283 strcpy( name
, output_name
);
284 strcat( name
, ".XXXXXX" );
286 else name
= xstrdup( "wrc.XXXXXX" );
288 if ((fd
= mkstemps( name
, 0 )) == -1)
289 error("Could not generate a temp name from %s\n", name
);
292 if (!(output
= fdopen(fd
, "wt")))
293 error("Could not open fd %s for writing\n", name
);
295 ret
= wpp_parse( input_name
, output
);
301 /* Reset the language */
302 currentlanguage
= dup_language( defaultlanguage
);
305 /* Go from .rc to .res */
306 chat("Starting parse\n");
308 if(!(parser_in
= fopen(input_name
, "rb")))
309 fatal_perror("Could not open %s for input", input_name
);
311 ret
= parser_parse();
313 parser_lex_destroy();
319 free( currentlanguage
);
324 int main(int argc
,char *argv
[])
335 char **files
= xmalloc( argc
* sizeof(*files
) );
337 signal(SIGSEGV
, segvhandler
);
338 signal( SIGTERM
, exit_on_signal
);
339 signal( SIGINT
, exit_on_signal
);
341 signal( SIGHUP
, exit_on_signal
);
346 /* Set the default defined stuff */
347 set_version_defines();
348 wpp_add_cmdline_define("RC_INVOKED=1");
349 wpp_add_cmdline_define("__WIN32__=1");
350 wpp_add_cmdline_define("__FLAT__=1");
351 /* Microsoft RC always searches current directory */
352 wpp_add_include_path(".");
354 /* First rebuild the commandline to put in destination */
355 /* Could be done through env[], but not all OS-es support it */
356 cmdlen
= 4; /* for "wrc " */
357 for(i
= 1; i
< argc
; i
++)
358 cmdlen
+= strlen(argv
[i
]) + 1;
359 cmdline
= xmalloc(cmdlen
);
360 strcpy(cmdline
, "wrc ");
361 for(i
= 1; i
< argc
; i
++)
363 strcat(cmdline
, argv
[i
]);
365 strcat(cmdline
, " ");
368 while((optc
= getopt_long(argc
, argv
, short_options
, long_options
, &opti
)) != EOF
)
372 case LONG_OPT_NOSTDINC
:
375 case LONG_OPT_TMPFILE
:
376 if (debuglevel
) warning("--use-temp-file option not yet supported, ignored.\n");
378 case LONG_OPT_NOTMPFILE
:
379 if (debuglevel
) warning("--no-use-temp-file option not yet supported, ignored.\n");
381 case LONG_OPT_PREPROCESSOR
:
382 if (strcmp(optarg
, "cat") == 0) no_preprocess
= 1;
383 else fprintf(stderr
, "-P option not yet supported, ignored.\n");
385 case LONG_OPT_VERSION
:
386 printf(version_string
);
390 debuglevel
= strtol(optarg
, NULL
, 0);
392 case LONG_OPT_ENDIANESS
:
397 byteorder
= WRC_BO_NATIVE
;
401 byteorder
= WRC_BO_LITTLE
;
405 byteorder
= WRC_BO_BIG
;
408 fprintf(stderr
, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
412 case LONG_OPT_PEDANTIC
:
416 case LONG_OPT_VERIFY_TRANSL
:
417 verify_translations_mode
= 1;
420 wpp_add_cmdline_define(optarg
);
426 /* ignored for compatibility with windres */
432 files
[nb_files
++] = optarg
;
435 wpp_add_include_path(optarg
);
438 if (strcmp(optarg
, "rc16") == 0) extensions
= 0;
439 else if (strcmp(optarg
, "rc")) error("Output format %s not supported.\n", optarg
);
444 lan
= strtol(optarg
, NULL
, 0);
445 if (get_language_codepage(PRIMARYLANGID(lan
), SUBLANGID(lan
)) == -1)
446 error("Language %04x is not supported\n", lan
);
447 defaultlanguage
= new_language(PRIMARYLANGID(lan
), SUBLANGID(lan
));
451 if (*optarg
!= 'o') error("Unknown option: -f%s\n", optarg
);
455 if (!output_name
) output_name
= strdup(optarg
);
456 else error("Too many output files.\n");
459 if (strcmp(optarg
, "res16") == 0)
462 wpp_del_define("__WIN32__");
463 wpp_del_define("__FLAT__");
465 else if (strcmp(optarg
, "res")) warning("Output format %s not supported.\n", optarg
);
468 /* ignored for compatibility with rc */
471 wpp_del_define(optarg
);
474 debuglevel
= DEBUGLEVEL_CHAT
;
484 fprintf(stderr
, usage
);
488 /* If we do need to search standard includes, add them to the path */
491 wpp_add_include_path(INCLUDEDIR
"/msvcrt");
492 wpp_add_include_path(INCLUDEDIR
"/windows");
495 /* Kill io buffering when some kind of debuglevel is enabled */
498 setbuf(stdout
, NULL
);
499 setbuf(stderr
, NULL
);
502 parser_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
503 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
505 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
506 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
507 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
509 /* Check if the user set a language, else set default */
511 defaultlanguage
= new_language(0, 0);
513 atexit(cleanup_files
);
515 while (optind
< argc
) files
[nb_files
++] = argv
[optind
++];
517 for (i
= 0; i
< nb_files
; i
++)
519 input_name
= files
[i
];
520 if(!output_name
&& !preprocess_only
)
522 output_name
= dup_basename(input_name
, ".rc");
523 strcat(output_name
, ".res");
525 if (load_file( input_name
, output_name
)) exit(1);
527 /* stdin special case. NULL means "stdin" for wpp. */
530 if(!output_name
&& !preprocess_only
)
531 output_name
= strdup("wrc.tab.res");
532 if (load_file( NULL
, output_name
)) exit(1);
535 if(debuglevel
& DEBUGLEVEL_DUMP
)
536 dump_resources(resource_top
);
538 if(verify_translations_mode
)
540 verify_translations(resource_top
);
544 /* Convert the internal lists to binary data */
545 resources2res(resource_top
);
547 chat("Writing .res-file\n");
548 write_resfile(output_name
, resource_top
);
555 static void cleanup_files(void)
557 if (output_name
) unlink(output_name
);
558 if (temp_name
) unlink(temp_name
);
561 static void segvhandler(int sig
)
563 fprintf(stderr
, "\n%s:%d: Oops, segment violation\n", input_name
, line_number
);