Fixed potential crash in fd_dump function.
[wine/multimedia.git] / tools / wrc / wrc.c
blob460392461c1c05decc8d37b8ebe813bdffbac6ff
1 /*
2 * Copyright 1994 Martin von Loewis
3 * Copyrignt 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #ifdef HAVE_GETOPT_H
35 # include <getopt.h>
36 #endif
38 #include "wrc.h"
39 #include "utils.h"
40 #include "readres.h"
41 #include "dumpres.h"
42 #include "genres.h"
43 #include "newstruc.h"
44 #include "parser.h"
45 #include "wine/wpp.h"
47 #ifndef INCLUDEDIR
48 #define INCLUDEDIR "/usr/local/include/wine"
49 #endif
51 #ifdef WORDS_BIGENDIAN
52 #define ENDIAN "big"
53 #else
54 #define ENDIAN "little"
55 #endif
57 static char usage[] =
58 "Usage: wrc [options...] [infile[.rc|.res]] [outfile]\n"
59 " -D id[=val] Define preprocessor identifier id=val\n"
60 " -E Preprocess only\n"
61 " -F target Ignored for compatibility with windres\n"
62 " -h Prints this summary\n"
63 " -i file The name of the input file\n"
64 " -I path Set include search dir to path (multiple -I allowed)\n"
65 " -J format The input format (either `rc' or `rc16')\n"
66 " -l lan Set default language to lan (default is neutral {0, 0})\n"
67 " -o file Output to file (default is infile.res)\n"
68 " -O format The output format (either `res' or `res16`)\n"
69 " -r Ignored for compatibility with rc\n"
70 " -U id Undefine preprocessor identifier id\n"
71 " -v Enable verbose mode\n"
72 "The following long options are supported:\n"
73 " --debug=nn Set debug level to 'nn'\n"
74 " --define Synonym for -D\n"
75 " --endianess=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
76 " (win32 only; default is " ENDIAN "-endian)\n"
77 " --help Synonym for -h\n"
78 " --include-dir Synonym for -I\n"
79 " --input Synonym for -i\n"
80 " --input-format Synonym for -J\n"
81 " --language Synonym for -l\n"
82 " --no-use-temp-file Ignored for compatibility with windres\n"
83 " --nostdinc Disables searching the standard include path\n"
84 " --output -fo Synonym for -o\n"
85 " --output-format Synonym for -O\n"
86 " --pedantic Enable pedantic warnings\n"
87 " --preprocessor Specifies the preprocessor to use, including arguments\n"
88 " --target Synonym for -F\n"
89 " --undefine Synonym for -U\n"
90 " --use-temp-file Ignored for compatibility with windres\n"
91 " --verbose Synonym for -v\n"
92 " --verify-translations Check the status of the various translations\n"
93 " --version Print version and exit\n"
94 "Input is taken from stdin if no sourcefile specified.\n"
95 "Debug level 'n' is a bitmask with following meaning:\n"
96 " * 0x01 Tell which resource is parsed (verbose mode)\n"
97 " * 0x02 Dump internal structures\n"
98 " * 0x04 Create a parser trace (yydebug=1)\n"
99 " * 0x08 Preprocessor messages\n"
100 " * 0x10 Preprocessor lex messages\n"
101 " * 0x20 Preprocessor yacc trace\n"
102 "If no input filename is given and the output name is not overridden\n"
103 "with -o, then the output is written to \"wrc.tab.res\"\n"
106 static const char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
107 "Copyright 1998-2000 Bertho A. Stultiens\n"
108 " 1994 Martin von Loewis\n";
111 * External functions
113 void write_resfile(char *outname, resource_t *top);
114 void verify_translations(resource_t *top);
117 * Set if compiling in 32bit mode (default).
119 int win32 = 1;
122 * debuglevel == DEBUGLEVEL_NONE Don't bother
123 * debuglevel & DEBUGLEVEL_CHAT Say whats done
124 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
125 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
126 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
127 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
128 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
130 int debuglevel = DEBUGLEVEL_NONE;
133 * Recognize win32 keywords if set (-w 32 enforces this),
134 * otherwise set with -e option.
136 int extensions = 1;
139 * Language setting for resources (-l option)
141 language_t *currentlanguage = NULL;
144 * Set when extra warnings should be generated (-W option)
146 int pedantic = 0;
149 * The output byte-order of resources (set with -B)
151 int byteorder = WRC_BO_NATIVE;
154 * Set when _only_ to run the preprocessor (-E option)
156 int preprocess_only = 0;
159 * Set when _not_ to run the preprocessor (-P cat option)
161 int no_preprocess = 0;
163 static int verify_translations_mode;
165 char *output_name = NULL; /* The name given by the -o option */
166 char *input_name = NULL; /* The name given on the command-line */
167 char *temp_name = NULL; /* Temporary file for preprocess pipe */
169 int line_number = 1; /* The current line */
170 int char_number = 1; /* The current char pos within the line */
172 char *cmdline; /* The entire commandline */
173 time_t now; /* The time of start of wrc */
175 resource_t *resource_top; /* The top of the parsed resources */
177 int getopt (int argc, char *const *argv, const char *optstring);
178 static void rm_tempfile(void);
179 static void segvhandler(int sig);
181 static const char* short_options =
182 "D:Ef:F:hi:I:J:l:o:O:rU:v";
183 static struct option long_options[] = {
184 { "debug", 1, 0, 6 },
185 { "define", 1, 0, 'D' },
186 { "endianess", 1, 0, 7 },
187 { "help", 0, 0, 'h' },
188 { "include-dir", 1, 0, 'I' },
189 { "input", 1, 0, 'i' },
190 { "input-format", 1, 0, 'J' },
191 { "language", 1, 0, 'l' },
192 { "no-use-temp-file", 0, 0, 3 },
193 { "nostdinc", 0, 0, 1 },
194 { "output", 1, 0, 'o' },
195 { "output-format", 1, 0, 'O' },
196 { "pendantic", 0, 0, 8 },
197 { "preprocessor", 1, 0, 4 },
198 { "target", 1, 0, 'F' },
199 { "undefine", 1, 0, 'U' },
200 { "use-temp-file", 0, 0, 2 },
201 { "verbose", 0, 0, 'v' },
202 { "verify-translations", 0, 0, 9 },
203 { "version", 0, 0, 5 },
204 { 0, 0, 0, 0 }
207 int main(int argc,char *argv[])
209 extern char* optarg;
210 extern int optind;
211 int optc;
212 int opti = 0;
213 int stdinc = 1;
214 int lose = 0;
215 int ret;
216 int i;
217 int cmdlen;
219 signal(SIGSEGV, segvhandler);
221 now = time(NULL);
223 /* Set the default defined stuff */
224 wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
225 wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
226 wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
227 wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
229 wpp_add_cmdline_define("RC_INVOKED=1");
230 wpp_add_cmdline_define("__WIN32__=1");
231 wpp_add_cmdline_define("__FLAT__=1");
233 /* First rebuild the commandline to put in destination */
234 /* Could be done through env[], but not all OS-es support it */
235 cmdlen = 4; /* for "wrc " */
236 for(i = 1; i < argc; i++)
237 cmdlen += strlen(argv[i]) + 1;
238 cmdline = (char *)xmalloc(cmdlen);
239 strcpy(cmdline, "wrc ");
240 for(i = 1; i < argc; i++)
242 strcat(cmdline, argv[i]);
243 if(i < argc-1)
244 strcat(cmdline, " ");
247 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
249 switch(optc)
251 case 1:
252 stdinc = 0;
253 break;
254 case 2:
255 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
256 break;
257 case 3:
258 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
259 break;
260 case 4:
261 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
262 else fprintf(stderr, "-P option not yet supported, ignored.\n");
263 break;
264 case 5:
265 printf(version_string);
266 exit(0);
267 break;
268 case 6:
269 debuglevel = strtol(optarg, NULL, 0);
270 break;
271 case 7:
272 switch(optarg[0])
274 case 'n':
275 case 'N':
276 byteorder = WRC_BO_NATIVE;
277 break;
278 case 'l':
279 case 'L':
280 byteorder = WRC_BO_LITTLE;
281 break;
282 case 'b':
283 case 'B':
284 byteorder = WRC_BO_BIG;
285 break;
286 default:
287 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
288 lose++;
290 break;
291 case 8:
292 pedantic = 1;
293 wpp_set_pedantic(1);
294 break;
295 case 9:
296 verify_translations_mode = 1;
297 break;
298 case 'D':
299 wpp_add_cmdline_define(optarg);
300 break;
301 case 'E':
302 preprocess_only = 1;
303 break;
304 case 'F':
305 /* ignored for compatibility with windres */
306 break;
307 case 'h':
308 printf(usage);
309 exit(0);
310 case 'i':
311 if (!input_name) input_name = strdup(optarg);
312 else error("Too many input files.\n");
313 break;
314 case 'I':
315 wpp_add_include_path(optarg);
316 break;
317 case 'J':
318 if (strcmp(optarg, "rc16") == 0) extensions = 0;
319 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
320 break;
321 case 'l':
323 int lan;
324 lan = strtol(optarg, NULL, 0);
325 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
326 error("Language %04x is not supported\n", lan);
327 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
329 break;
330 case 'f':
331 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
332 optarg++;
333 /* fall through */
334 case 'o':
335 if (!output_name) output_name = strdup(optarg);
336 else error("Too many output files.\n");
337 break;
338 case 'O':
339 if (strcmp(optarg, "res16") == 0)
341 win32 = 0;
342 wpp_del_define("__WIN32__");
343 wpp_del_define("__FLAT__");
345 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
346 break;
347 case 'r':
348 /* ignored for compatibility with rc */
349 break;
350 case 'U':
351 wpp_del_define(optarg);
352 break;
353 case 'v':
354 debuglevel = DEBUGLEVEL_CHAT;
355 break;
356 default:
357 lose++;
358 break;
362 if(lose)
364 fprintf(stderr, usage);
365 return 1;
368 /* If we do need to search standard includes, add them to the path */
369 if (stdinc)
371 wpp_add_include_path(INCLUDEDIR"/msvcrt");
372 wpp_add_include_path(INCLUDEDIR"/windows");
375 /* Check for input file on command-line */
376 if(optind < argc)
378 if (!input_name) input_name = argv[optind++];
379 else error("Too many input files.\n");
382 /* Check for output file on command-line */
383 if(optind < argc)
385 if (!output_name) output_name = argv[optind++];
386 else error("Too many output files.\n");
389 /* Kill io buffering when some kind of debuglevel is enabled */
390 if(debuglevel)
392 setbuf(stdout,0);
393 setbuf(stderr,0);
396 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
397 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
399 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
400 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
401 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
403 /* Check if the user set a language, else set default */
404 if(!currentlanguage)
405 currentlanguage = new_language(0, 0);
407 /* Generate appropriate outfile names */
408 if(!output_name && !preprocess_only)
410 output_name = dup_basename(input_name, ".rc");
411 strcat(output_name, ".res");
414 /* Run the preprocessor on the input */
415 if(!no_preprocess)
418 * Preprocess the input to a temp-file, or stdout if
419 * no output was given.
422 chat("Starting preprocess");
424 if (!preprocess_only)
426 atexit(rm_tempfile);
427 ret = wpp_parse_temp( input_name, output_name, &temp_name );
429 else if (output_name)
431 FILE *output;
433 if (!(output = fopen( output_name, "w" )))
434 error( "Could not open %s for writing\n", output_name );
435 ret = wpp_parse( input_name, output );
436 fclose( output );
438 else
440 ret = wpp_parse( input_name, stdout );
443 if(ret)
444 exit(1); /* Error during preprocess */
446 if(preprocess_only)
447 exit(0);
449 input_name = temp_name;
452 /* Go from .rc to .res */
453 chat("Starting parse");
455 if(!(yyin = fopen(input_name, "rb")))
456 error("Could not open %s for input\n", input_name);
458 ret = yyparse();
460 if(input_name) fclose(yyin);
462 if(ret) exit(1); /* Error during parse */
464 if(debuglevel & DEBUGLEVEL_DUMP)
465 dump_resources(resource_top);
467 if(verify_translations_mode)
469 verify_translations(resource_top);
470 exit(0);
473 /* Convert the internal lists to binary data */
474 resources2res(resource_top);
476 chat("Writing .res-file");
477 write_resfile(output_name, resource_top);
479 return 0;
483 static void rm_tempfile(void)
485 if(temp_name)
486 unlink(temp_name);
489 static void segvhandler(int sig)
491 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
492 fflush(stdout);
493 fflush(stderr);
494 abort();