- Documentation update.
[wine.git] / tools / wrc / wrc.c
blob2635939c97c71a41cb29ffd7c730cea350fc4080
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 " --verify-translations Check the status of the various translations\n"
92 " --version Print version and exit\n"
93 "Input is taken from stdin if no sourcefile specified.\n"
94 "Debug level 'n' is a bitmask with following meaning:\n"
95 " * 0x01 Tell which resource is parsed (verbose mode)\n"
96 " * 0x02 Dump internal structures\n"
97 " * 0x04 Create a parser trace (yydebug=1)\n"
98 " * 0x08 Preprocessor messages\n"
99 " * 0x10 Preprocessor lex messages\n"
100 " * 0x20 Preprocessor yacc trace\n"
101 "If no input filename is given and the output name is not overridden\n"
102 "with -o, then the output is written to \"wrc.tab.res\"\n"
105 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
106 "Copyright 1998-2000 Bertho A. Stultiens\n"
107 " 1994 Martin von Loewis\n";
110 * External functions
112 void write_resfile(char *outname, resource_t *top);
113 void verify_translations(resource_t *top);
116 * Set if compiling in 32bit mode (default).
118 int win32 = 1;
121 * debuglevel == DEBUGLEVEL_NONE Don't bother
122 * debuglevel & DEBUGLEVEL_CHAT Say whats done
123 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
124 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
125 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
126 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
127 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
129 int debuglevel = DEBUGLEVEL_NONE;
132 * Recognize win32 keywords if set (-w 32 enforces this),
133 * otherwise set with -e option.
135 int extensions = 1;
138 * Language setting for resources (-l option)
140 language_t *currentlanguage = NULL;
143 * Set when extra warnings should be generated (-W option)
145 int pedantic = 0;
148 * The output byte-order of resources (set with -B)
150 int byteorder = WRC_BO_NATIVE;
153 * Set when _only_ to run the preprocessor (-E option)
155 int preprocess_only = 0;
158 * Set when _not_ to run the preprocessor (-P cat option)
160 int no_preprocess = 0;
162 static int verify_translations_mode;
164 char *output_name = NULL; /* The name given by the -o option */
165 char *input_name = NULL; /* The name given on the command-line */
166 char *temp_name = NULL; /* Temporary file for preprocess pipe */
168 int line_number = 1; /* The current line */
169 int char_number = 1; /* The current char pos within the line */
171 char *cmdline; /* The entire commandline */
172 time_t now; /* The time of start of wrc */
174 resource_t *resource_top; /* The top of the parsed resources */
176 int getopt (int argc, char *const *argv, const char *optstring);
177 static void rm_tempfile(void);
178 static void segvhandler(int sig);
180 static const char* short_options =
181 "D:Ef:F:hi:I:J:l:o:O:rU:v";
182 static struct option long_options[] = {
183 { "debug", 1, 0, 6 },
184 { "define", 1, 0, 'D' },
185 { "endianess", 1, 0, 7 },
186 { "help", 0, 0, 'h' },
187 { "include-dir", 1, 0, 'I' },
188 { "input", 1, 0, 'i' },
189 { "input-format", 1, 0, 'J' },
190 { "language", 1, 0, 'l' },
191 { "no-use-temp-file", 0, 0, 3 },
192 { "nostdinc", 0, 0, 1 },
193 { "output", 1, 0, 'o' },
194 { "output-format", 1, 0, 'O' },
195 { "pendantic", 0, 0, 8 },
196 { "preprocessor", 1, 0, 4 },
197 { "target", 1, 0, 'F' },
198 { "undefine", 1, 0, 'U' },
199 { "use-temp-file", 0, 0, 2 },
200 { "verify-translations", 0, 0, 9 },
201 { "version", 0, 0, 5 },
202 { 0, 0, 0, 0 }
205 int main(int argc,char *argv[])
207 extern char* optarg;
208 extern int optind;
209 int optc;
210 int opti = 0;
211 int stdinc = 1;
212 int lose = 0;
213 int ret;
214 int i;
215 int cmdlen;
217 signal(SIGSEGV, segvhandler);
219 now = time(NULL);
221 /* Set the default defined stuff */
222 wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
223 wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
224 wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
225 wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
227 wpp_add_cmdline_define("RC_INVOKED=1");
228 wpp_add_cmdline_define("__WIN32__=1");
229 wpp_add_cmdline_define("__FLAT__=1");
231 /* First rebuild the commandline to put in destination */
232 /* Could be done through env[], but not all OS-es support it */
233 cmdlen = 4; /* for "wrc " */
234 for(i = 1; i < argc; i++)
235 cmdlen += strlen(argv[i]) + 1;
236 cmdline = (char *)xmalloc(cmdlen);
237 strcpy(cmdline, "wrc ");
238 for(i = 1; i < argc; i++)
240 strcat(cmdline, argv[i]);
241 if(i < argc-1)
242 strcat(cmdline, " ");
245 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
247 switch(optc)
249 case 1:
250 stdinc = 0;
251 break;
252 case 2:
253 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
254 break;
255 case 3:
256 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
257 break;
258 case 4:
259 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
260 else fprintf(stderr, "-P option not yet supported, ignored.\n");
261 break;
262 case 5:
263 printf(version_string);
264 exit(0);
265 break;
266 case 6:
267 debuglevel = strtol(optarg, NULL, 0);
268 break;
269 case 7:
270 switch(optarg[0])
272 case 'n':
273 case 'N':
274 byteorder = WRC_BO_NATIVE;
275 break;
276 case 'l':
277 case 'L':
278 byteorder = WRC_BO_LITTLE;
279 break;
280 case 'b':
281 case 'B':
282 byteorder = WRC_BO_BIG;
283 break;
284 default:
285 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
286 lose++;
288 break;
289 case 8:
290 pedantic = 1;
291 wpp_set_pedantic(1);
292 break;
293 case 9:
294 verify_translations_mode = 1;
295 break;
296 case 'D':
297 wpp_add_cmdline_define(optarg);
298 break;
299 case 'E':
300 preprocess_only = 1;
301 break;
302 case 'F':
303 /* ignored for compatibility with windres */
304 break;
305 case 'h':
306 printf(usage);
307 exit(0);
308 case 'i':
309 if (!input_name) input_name = strdup(optarg);
310 else error("Too many input files.\n");
311 break;
312 case 'I':
313 wpp_add_include_path(optarg);
314 break;
315 case 'J':
316 if (strcmp(optarg, "rc16") == 0) extensions = 0;
317 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
318 break;
319 case 'l':
321 int lan;
322 lan = strtol(optarg, NULL, 0);
323 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
324 error("Language %04x is not supported\n", lan);
325 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
327 break;
328 case 'f':
329 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
330 optarg++;
331 /* fall through */
332 case 'o':
333 if (!output_name) output_name = strdup(optarg);
334 else error("Too many output files.\n");
335 break;
336 case 'O':
337 if (strcmp(optarg, "res16") == 0)
339 win32 = 0;
340 wpp_del_define("__WIN32__");
341 wpp_del_define("__FLAT__");
343 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
344 break;
345 case 'r':
346 /* ignored for compatibility with rc */
347 break;
348 case 'U':
349 wpp_del_define(optarg);
350 break;
351 case 'v':
352 debuglevel = DEBUGLEVEL_CHAT;
353 break;
354 default:
355 lose++;
356 break;
360 if(lose)
362 fprintf(stderr, usage);
363 return 1;
366 /* If we do need to search standard includes, add them to the path */
367 if (stdinc)
369 wpp_add_include_path(INCLUDEDIR"/msvcrt");
370 wpp_add_include_path(INCLUDEDIR"/windows");
373 /* Check for input file on command-line */
374 if(optind < argc)
376 if (!input_name) input_name = argv[optind++];
377 else error("Too many input files.\n");
380 /* Check for output file on command-line */
381 if(optind < argc)
383 if (!output_name) output_name = argv[optind++];
384 else error("Too many output files.\n");
387 /* Kill io buffering when some kind of debuglevel is enabled */
388 if(debuglevel)
390 setbuf(stdout,0);
391 setbuf(stderr,0);
394 yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
395 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
397 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
398 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
399 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
401 /* Check if the user set a language, else set default */
402 if(!currentlanguage)
403 currentlanguage = new_language(0, 0);
405 /* Generate appropriate outfile names */
406 if(!output_name && !preprocess_only)
408 output_name = dup_basename(input_name, ".rc");
409 strcat(output_name, ".res");
412 /* Run the preprocessor on the input */
413 if(!no_preprocess)
416 * Preprocess the input to a temp-file, or stdout if
417 * no output was given.
420 chat("Starting preprocess");
422 if (!preprocess_only)
424 atexit(rm_tempfile);
425 ret = wpp_parse_temp( input_name, output_name, &temp_name );
427 else if (output_name)
429 FILE *output;
431 if (!(output = fopen( output_name, "w" )))
432 error( "Could not open %s for writing\n", output_name );
433 ret = wpp_parse( input_name, output );
434 fclose( output );
436 else
438 ret = wpp_parse( input_name, stdout );
441 if(ret)
442 exit(1); /* Error during preprocess */
444 if(preprocess_only)
445 exit(0);
447 input_name = temp_name;
450 /* Go from .rc to .res */
451 chat("Starting parse");
453 if(!(yyin = fopen(input_name, "rb")))
454 error("Could not open %s for input\n", input_name);
456 ret = yyparse();
458 if(input_name) fclose(yyin);
460 if(ret) exit(1); /* Error during parse */
462 if(debuglevel & DEBUGLEVEL_DUMP)
463 dump_resources(resource_top);
465 if(verify_translations_mode)
467 verify_translations(resource_top);
468 exit(0);
471 /* Convert the internal lists to binary data */
472 resources2res(resource_top);
474 chat("Writing .res-file");
475 write_resfile(output_name, resource_top);
477 return 0;
481 static void rm_tempfile(void)
483 if(temp_name)
484 unlink(temp_name);
487 static void segvhandler(int sig)
489 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
490 fflush(stdout);
491 fflush(stderr);
492 abort();