Release 1.1.14.
[wine/gsoc-2012-control.git] / tools / wrc / wrc.c
blob15f30a3e7d5a39d6a95ba79cb8a09086a00d816f
1 /*
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
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 const 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 " PACKAGE_VERSION "\n"
107 "Copyright 1998-2000 Bertho A. Stultiens\n"
108 " 1994 Martin von Loewis\n";
111 * Set if compiling in 32bit mode (default).
113 int win32 = 1;
116 * debuglevel == DEBUGLEVEL_NONE Don't bother
117 * debuglevel & DEBUGLEVEL_CHAT Say what's done
118 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
119 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
120 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
121 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
122 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
124 int debuglevel = DEBUGLEVEL_NONE;
127 * Recognize win32 keywords if set (-w 32 enforces this),
128 * otherwise set with -e option.
130 int extensions = 1;
133 * Language setting for resources (-l option)
135 language_t *currentlanguage = NULL;
138 * Set when extra warnings should be generated (-W option)
140 int pedantic = 0;
143 * The output byte-order of resources (set with -B)
145 int byteorder = WRC_BO_NATIVE;
148 * Set when _only_ to run the preprocessor (-E option)
150 int preprocess_only = 0;
153 * Set when _not_ to run the preprocessor (-P cat option)
155 int no_preprocess = 0;
157 static int verify_translations_mode;
159 char *output_name = NULL; /* The name given by the -o option */
160 char *input_name = NULL; /* The name given on the command-line */
161 char *temp_name = NULL; /* Temporary file for preprocess pipe */
163 int line_number = 1; /* The current line */
164 int char_number = 1; /* The current char pos within the line */
166 char *cmdline; /* The entire commandline */
167 time_t now; /* The time of start of wrc */
169 int parser_debug, yy_flex_debug;
171 resource_t *resource_top; /* The top of the parsed resources */
173 int getopt (int argc, char *const *argv, const char *optstring);
174 static void cleanup_files(void);
175 static void segvhandler(int sig);
177 static const char short_options[] =
178 "D:Ef:F:hi:I:J:l:o:O:rU:v";
179 static const struct option long_options[] = {
180 { "debug", 1, 0, 6 },
181 { "define", 1, 0, 'D' },
182 { "endianess", 1, 0, 7 },
183 { "help", 0, 0, 'h' },
184 { "include-dir", 1, 0, 'I' },
185 { "input", 1, 0, 'i' },
186 { "input-format", 1, 0, 'J' },
187 { "language", 1, 0, 'l' },
188 { "no-use-temp-file", 0, 0, 3 },
189 { "nostdinc", 0, 0, 1 },
190 { "output", 1, 0, 'o' },
191 { "output-format", 1, 0, 'O' },
192 { "pedantic", 0, 0, 8 },
193 { "preprocessor", 1, 0, 4 },
194 { "target", 1, 0, 'F' },
195 { "undefine", 1, 0, 'U' },
196 { "use-temp-file", 0, 0, 2 },
197 { "verbose", 0, 0, 'v' },
198 { "verify-translations", 0, 0, 9 },
199 { "version", 0, 0, 5 },
200 { 0, 0, 0, 0 }
203 static void set_version_defines(void)
205 char *version = xstrdup( PACKAGE_VERSION );
206 char *major, *minor, *patchlevel;
207 char buffer[100];
209 if ((minor = strchr( version, '.' )))
211 major = version;
212 *minor++ = 0;
213 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
215 else /* pre 0.9 version */
217 major = NULL;
218 patchlevel = version;
220 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
221 wpp_add_cmdline_define(buffer);
222 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
223 wpp_add_cmdline_define(buffer);
224 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
225 wpp_add_cmdline_define(buffer);
226 free( version );
229 /* clean things up when aborting on a signal */
230 static void exit_on_signal( int sig )
232 exit(1); /* this will call the atexit functions */
235 int main(int argc,char *argv[])
237 extern char* optarg;
238 extern int optind;
239 int optc;
240 int opti = 0;
241 int stdinc = 1;
242 int lose = 0;
243 int ret;
244 int i;
245 int cmdlen;
247 signal(SIGSEGV, segvhandler);
248 signal( SIGTERM, exit_on_signal );
249 signal( SIGINT, exit_on_signal );
250 #ifdef SIGHUP
251 signal( SIGHUP, exit_on_signal );
252 #endif
254 now = time(NULL);
256 /* Set the default defined stuff */
257 set_version_defines();
258 wpp_add_cmdline_define("RC_INVOKED=1");
259 wpp_add_cmdline_define("__WIN32__=1");
260 wpp_add_cmdline_define("__FLAT__=1");
261 /* Microsoft RC always searches current directory */
262 wpp_add_include_path(".");
264 /* First rebuild the commandline to put in destination */
265 /* Could be done through env[], but not all OS-es support it */
266 cmdlen = 4; /* for "wrc " */
267 for(i = 1; i < argc; i++)
268 cmdlen += strlen(argv[i]) + 1;
269 cmdline = xmalloc(cmdlen);
270 strcpy(cmdline, "wrc ");
271 for(i = 1; i < argc; i++)
273 strcat(cmdline, argv[i]);
274 if(i < argc-1)
275 strcat(cmdline, " ");
278 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
280 switch(optc)
282 case 1:
283 stdinc = 0;
284 break;
285 case 2:
286 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
287 break;
288 case 3:
289 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
290 break;
291 case 4:
292 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
293 else fprintf(stderr, "-P option not yet supported, ignored.\n");
294 break;
295 case 5:
296 printf(version_string);
297 exit(0);
298 break;
299 case 6:
300 debuglevel = strtol(optarg, NULL, 0);
301 break;
302 case 7:
303 switch(optarg[0])
305 case 'n':
306 case 'N':
307 byteorder = WRC_BO_NATIVE;
308 break;
309 case 'l':
310 case 'L':
311 byteorder = WRC_BO_LITTLE;
312 break;
313 case 'b':
314 case 'B':
315 byteorder = WRC_BO_BIG;
316 break;
317 default:
318 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
319 lose++;
321 break;
322 case 8:
323 pedantic = 1;
324 wpp_set_pedantic(1);
325 break;
326 case 9:
327 verify_translations_mode = 1;
328 break;
329 case 'D':
330 wpp_add_cmdline_define(optarg);
331 break;
332 case 'E':
333 preprocess_only = 1;
334 break;
335 case 'F':
336 /* ignored for compatibility with windres */
337 break;
338 case 'h':
339 printf(usage);
340 exit(0);
341 case 'i':
342 if (!input_name) input_name = strdup(optarg);
343 else error("Too many input files.\n");
344 break;
345 case 'I':
346 wpp_add_include_path(optarg);
347 break;
348 case 'J':
349 if (strcmp(optarg, "rc16") == 0) extensions = 0;
350 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
351 break;
352 case 'l':
354 int lan;
355 lan = strtol(optarg, NULL, 0);
356 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
357 error("Language %04x is not supported\n", lan);
358 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
360 break;
361 case 'f':
362 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
363 optarg++;
364 /* fall through */
365 case 'o':
366 if (!output_name) output_name = strdup(optarg);
367 else error("Too many output files.\n");
368 break;
369 case 'O':
370 if (strcmp(optarg, "res16") == 0)
372 win32 = 0;
373 wpp_del_define("__WIN32__");
374 wpp_del_define("__FLAT__");
376 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
377 break;
378 case 'r':
379 /* ignored for compatibility with rc */
380 break;
381 case 'U':
382 wpp_del_define(optarg);
383 break;
384 case 'v':
385 debuglevel = DEBUGLEVEL_CHAT;
386 break;
387 default:
388 lose++;
389 break;
393 if(lose)
395 fprintf(stderr, usage);
396 return 1;
399 /* If we do need to search standard includes, add them to the path */
400 if (stdinc)
402 wpp_add_include_path(INCLUDEDIR"/msvcrt");
403 wpp_add_include_path(INCLUDEDIR"/windows");
406 /* Check for input file on command-line */
407 if(optind < argc)
409 if (!input_name) input_name = argv[optind++];
410 else error("Too many input files.\n");
413 /* Check for output file on command-line */
414 if(optind < argc)
416 if (!output_name) output_name = argv[optind++];
417 else error("Too many output files.\n");
420 /* Kill io buffering when some kind of debuglevel is enabled */
421 if(debuglevel)
423 setbuf(stdout,0);
424 setbuf(stderr,0);
427 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
428 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
430 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
431 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
432 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
434 /* Check if the user set a language, else set default */
435 if(!currentlanguage)
436 currentlanguage = new_language(0, 0);
438 /* Generate appropriate outfile names */
439 if(!output_name && !preprocess_only)
441 output_name = dup_basename(input_name, ".rc");
442 strcat(output_name, ".res");
444 atexit(cleanup_files);
446 /* Run the preprocessor on the input */
447 if(!no_preprocess)
450 * Preprocess the input to a temp-file, or stdout if
451 * no output was given.
454 chat("Starting preprocess\n");
456 if (!preprocess_only)
458 ret = wpp_parse_temp( input_name, output_name, &temp_name );
460 else if (output_name)
462 FILE *output;
464 if (!(output = fopen( output_name, "w" )))
465 error( "Could not open %s for writing\n", output_name );
466 ret = wpp_parse( input_name, output );
467 fclose( output );
469 else
471 ret = wpp_parse( input_name, stdout );
474 if(ret)
475 exit(1); /* Error during preprocess */
477 if(preprocess_only)
479 output_name = NULL;
480 exit(0);
483 input_name = temp_name;
486 /* Go from .rc to .res */
487 chat("Starting parse\n");
489 if(!(parser_in = fopen(input_name, "rb")))
490 error("Could not open %s for input\n", input_name);
492 ret = parser_parse();
494 if(input_name) fclose(parser_in);
496 if(ret) exit(1); /* Error during parse */
498 if(debuglevel & DEBUGLEVEL_DUMP)
499 dump_resources(resource_top);
501 if(verify_translations_mode)
503 verify_translations(resource_top);
504 exit(0);
507 /* Convert the internal lists to binary data */
508 resources2res(resource_top);
510 chat("Writing .res-file\n");
511 write_resfile(output_name, resource_top);
512 output_name = NULL;
514 return 0;
518 static void cleanup_files(void)
520 if (output_name) unlink(output_name);
521 if (temp_name) unlink(temp_name);
524 static void segvhandler(int sig)
526 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
527 fflush(stdout);
528 fflush(stderr);
529 abort();