blob: 7d5ad89be80d2c2cac1b90051a897fe1c5237d7b [file] [log] [blame]
Alexandre Julliarda845b881998-06-01 10:44:35 +00001/*
Dimitrie O. Paunfea01642003-03-20 19:23:08 +00002 * Copyright 1994 Martin von Loewis
Austin English90f89fa2008-01-15 19:28:45 -06003 * Copyright 1998 Bertho A. Stultiens (BS)
Dimitrie O. Paunfea01642003-03-20 19:23:08 +00004 * Copyright 2003 Dimitrie O. Paun
Alexandre Julliarda845b881998-06-01 10:44:35 +00005 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * 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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000019 *
Alexandre Julliarda845b881998-06-01 10:44:35 +000020 */
21
Patrik Stridvall021bd851999-07-18 18:40:11 +000022#include "config.h"
Alexandre Julliard65c6d382002-07-22 20:47:11 +000023#include "wine/port.h"
Patrik Stridvall021bd851999-07-18 18:40:11 +000024
Alexandre Julliarda845b881998-06-01 10:44:35 +000025#include <stdio.h>
26#include <stdlib.h>
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +000027#ifdef HAVE_UNISTD_H
28# include <unistd.h>
29#endif
Alexandre Julliarda845b881998-06-01 10:44:35 +000030#include <string.h>
31#include <assert.h>
32#include <ctype.h>
Bertho Stultiens27337af2000-05-01 20:05:58 +000033#include <signal.h>
Dimitrie O. Paun647c1a32002-12-10 19:16:24 +000034#ifdef HAVE_GETOPT_H
35# include <getopt.h>
36#endif
Alexandre Julliarda845b881998-06-01 10:44:35 +000037
Alexandre Julliarda845b881998-06-01 10:44:35 +000038#include "wrc.h"
39#include "utils.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000040#include "readres.h"
41#include "dumpres.h"
42#include "genres.h"
43#include "newstruc.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000044#include "parser.h"
Alexandre Julliarde1a9b102003-05-01 03:16:21 +000045#include "wine/wpp.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000046
Dimitrie O. Paund26933e2003-01-11 20:55:18 +000047#ifdef WORDS_BIGENDIAN
48#define ENDIAN "big"
49#else
50#define ENDIAN "little"
51#endif
52
Dmitry Timoshkov57a579f2006-01-24 14:00:32 +010053static const char usage[] =
Alexandre Julliard683dc302009-06-29 13:12:59 +020054 "Usage: wrc [options...] [infile[.rc|.res]]\n"
Alexandre Julliarde6340f12010-09-29 20:13:28 +020055 " -b, --target=TARGET Specify target CPU and platform when cross-compiling\n"
56 " -D, --define id[=val] Define preprocessor identifier id=val\n"
57 " --debug=nn Set debug level to 'nn'\n"
58 " -E Preprocess only\n"
Francois Gouget9c11faf2011-08-24 15:22:52 +020059 " --endianness=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
Alexandre Julliarde6340f12010-09-29 20:13:28 +020060 " (win32 only; default is " ENDIAN "-endian)\n"
61 " -F TARGET Synonym for -b for compatibility with windres\n"
62 " -fo FILE Synonym for -o for compatibility with windres\n"
63 " -h, --help Prints this summary\n"
64 " -i, --input=FILE The name of the input file\n"
65 " -I, --include-dir=PATH Set include search dir to path (multiple -I allowed)\n"
66 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
67 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
68 " -m16, -m32, -m64 Build for 16-bit, 32-bit resp. 64-bit platforms\n"
69 " --no-use-temp-file Ignored for compatibility with windres\n"
70 " --nostdinc Disables searching the standard include path\n"
71 " -o, --output=FILE Output to file (default is infile.res)\n"
Alexandre Julliard10700bf2011-01-04 11:18:10 +010072 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
Alexandre Julliarde6340f12010-09-29 20:13:28 +020073 " --pedantic Enable pedantic warnings\n"
Alexandre Julliardd8c986e2011-01-04 11:18:45 +010074 " --po-dir=DIR Directory containing po files for translations\n"
Alexandre Julliarde6340f12010-09-29 20:13:28 +020075 " --preprocessor Specifies the preprocessor to use, including arguments\n"
76 " -r Ignored for compatibility with rc\n"
77 " -U, --undefine id Undefine preprocessor identifier id\n"
78 " --use-temp-file Ignored for compatibility with windres\n"
79 " -v, --verbose Enable verbose mode\n"
80 " --verify-translations Check the status of the various translations\n"
81 " --version Print version and exit\n"
Alexandre Julliarda845b881998-06-01 10:44:35 +000082 "Input is taken from stdin if no sourcefile specified.\n"
83 "Debug level 'n' is a bitmask with following meaning:\n"
84 " * 0x01 Tell which resource is parsed (verbose mode)\n"
85 " * 0x02 Dump internal structures\n"
86 " * 0x04 Create a parser trace (yydebug=1)\n"
Bertho Stultiens27337af2000-05-01 20:05:58 +000087 " * 0x08 Preprocessor messages\n"
88 " * 0x10 Preprocessor lex messages\n"
89 " * 0x20 Preprocessor yacc trace\n"
Alexandre Julliarda845b881998-06-01 10:44:35 +000090 "If no input filename is given and the output name is not overridden\n"
Dimitrie O. Paundeaf02a2003-02-26 05:04:12 +000091 "with -o, then the output is written to \"wrc.tab.res\"\n"
Alexandre Julliarda845b881998-06-01 10:44:35 +000092 ;
93
Alexandre Julliardac21d342005-10-10 10:43:41 +000094static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
Bertho Stultiens27337af2000-05-01 20:05:58 +000095 "Copyright 1998-2000 Bertho A. Stultiens\n"
Alexandre Julliarda845b881998-06-01 10:44:35 +000096 " 1994 Martin von Loewis\n";
97
98/*
Alexandre Julliarda845b881998-06-01 10:44:35 +000099 * Set if compiling in 32bit mode (default).
100 */
101int win32 = 1;
102
103/*
Alexandre Julliarda845b881998-06-01 10:44:35 +0000104 * debuglevel == DEBUGLEVEL_NONE Don't bother
Austin English90f89fa2008-01-15 19:28:45 -0600105 * debuglevel & DEBUGLEVEL_CHAT Say what's done
Alexandre Julliarda845b881998-06-01 10:44:35 +0000106 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
107 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
Bertho Stultiens27337af2000-05-01 20:05:58 +0000108 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
109 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
110 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
Alexandre Julliarda845b881998-06-01 10:44:35 +0000111 */
112int debuglevel = DEBUGLEVEL_NONE;
113
114/*
115 * Recognize win32 keywords if set (-w 32 enforces this),
116 * otherwise set with -e option.
117 */
118int extensions = 1;
119
120/*
Alexandre Julliarda845b881998-06-01 10:44:35 +0000121 * Language setting for resources (-l option)
122 */
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200123static language_t *defaultlanguage;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000124language_t *currentlanguage = NULL;
125
126/*
Alexandre Julliarda845b881998-06-01 10:44:35 +0000127 * Set when extra warnings should be generated (-W option)
128 */
129int pedantic = 0;
130
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000131/*
Bertho Stultiens3d455c92000-05-09 22:35:10 +0000132 * The output byte-order of resources (set with -B)
133 */
134int byteorder = WRC_BO_NATIVE;
135
136/*
Bertho Stultiens27337af2000-05-01 20:05:58 +0000137 * Set when _only_ to run the preprocessor (-E option)
138 */
139int preprocess_only = 0;
140
141/*
Dimitrie O. Paun095322e2003-02-11 21:58:46 +0000142 * Set when _not_ to run the preprocessor (-P cat option)
Bertho Stultiens27337af2000-05-01 20:05:58 +0000143 */
144int no_preprocess = 0;
145
Alexandre Julliardbe7558f2009-08-20 15:33:03 +0200146int check_utf8 = 1; /* whether to check for valid utf8 */
147
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200148static int pointer_size = sizeof(void *);
149
Vincent Béron84512402004-01-18 23:24:04 +0000150static int verify_translations_mode;
151
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100152static char *output_name; /* The name given by the -o option */
Jon Griffiths3e12aba2003-08-18 19:48:58 +0000153char *input_name = NULL; /* The name given on the command-line */
Rob Shearman903f6862010-01-21 11:57:09 +0000154static char *temp_name = NULL; /* Temporary file for preprocess pipe */
Bertho Stultiens27337af2000-05-01 20:05:58 +0000155
156int line_number = 1; /* The current line */
157int char_number = 1; /* The current char pos within the line */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000158
159char *cmdline; /* The entire commandline */
Bertho Stultiens27337af2000-05-01 20:05:58 +0000160time_t now; /* The time of start of wrc */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000161
Alexandre Julliard42418fb2006-09-12 09:05:20 +0200162int parser_debug, yy_flex_debug;
Alexandre Julliard88991e22006-01-03 15:11:46 +0100163
Alexandre Julliarda845b881998-06-01 10:44:35 +0000164resource_t *resource_top; /* The top of the parsed resources */
165
166int getopt (int argc, char *const *argv, const char *optstring);
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200167static void cleanup_files(void);
Bertho Stultiens27337af2000-05-01 20:05:58 +0000168static void segvhandler(int sig);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000169
Alexandre Julliard683dc302009-06-29 13:12:59 +0200170enum long_options_values
171{
172 LONG_OPT_NOSTDINC = 1,
173 LONG_OPT_TMPFILE,
174 LONG_OPT_NOTMPFILE,
Alexandre Julliardd8c986e2011-01-04 11:18:45 +0100175 LONG_OPT_PO_DIR,
Alexandre Julliard683dc302009-06-29 13:12:59 +0200176 LONG_OPT_PREPROCESSOR,
177 LONG_OPT_VERSION,
178 LONG_OPT_DEBUG,
Francois Gouget9c11faf2011-08-24 15:22:52 +0200179 LONG_OPT_ENDIANNESS,
Alexandre Julliard683dc302009-06-29 13:12:59 +0200180 LONG_OPT_PEDANTIC,
181 LONG_OPT_VERIFY_TRANSL
182};
183
Dmitry Timoshkovfcd6f662007-08-30 17:18:02 +0900184static const char short_options[] =
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200185 "b:D:Ef:F:hi:I:J:l:m:o:O:rU:v";
Dmitry Timoshkovfcd6f662007-08-30 17:18:02 +0900186static const struct option long_options[] = {
Rob Shearman903f6862010-01-21 11:57:09 +0000187 { "debug", 1, NULL, LONG_OPT_DEBUG },
188 { "define", 1, NULL, 'D' },
Francois Gouget9c11faf2011-08-24 15:22:52 +0200189 { "endianness", 1, NULL, LONG_OPT_ENDIANNESS },
Rob Shearman903f6862010-01-21 11:57:09 +0000190 { "help", 0, NULL, 'h' },
191 { "include-dir", 1, NULL, 'I' },
192 { "input", 1, NULL, 'i' },
193 { "input-format", 1, NULL, 'J' },
194 { "language", 1, NULL, 'l' },
195 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
196 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
197 { "output", 1, NULL, 'o' },
198 { "output-format", 1, NULL, 'O' },
199 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
Alexandre Julliardd8c986e2011-01-04 11:18:45 +0100200 { "po-dir", 1, NULL, LONG_OPT_PO_DIR },
Rob Shearman903f6862010-01-21 11:57:09 +0000201 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
202 { "target", 1, NULL, 'F' },
203 { "undefine", 1, NULL, 'U' },
204 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
205 { "verbose", 0, NULL, 'v' },
206 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
207 { "version", 0, NULL, LONG_OPT_VERSION },
208 { NULL, 0, NULL, 0 }
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000209};
210
Alexandre Julliardac21d342005-10-10 10:43:41 +0000211static void set_version_defines(void)
212{
213 char *version = xstrdup( PACKAGE_VERSION );
214 char *major, *minor, *patchlevel;
215 char buffer[100];
216
217 if ((minor = strchr( version, '.' )))
218 {
219 major = version;
220 *minor++ = 0;
221 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
222 }
223 else /* pre 0.9 version */
224 {
225 major = NULL;
226 patchlevel = version;
227 }
228 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
229 wpp_add_cmdline_define(buffer);
230 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
231 wpp_add_cmdline_define(buffer);
232 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
233 wpp_add_cmdline_define(buffer);
234 free( version );
235}
236
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200237/* clean things up when aborting on a signal */
238static void exit_on_signal( int sig )
239{
240 exit(1); /* this will call the atexit functions */
241}
242
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200243/* load a single input file */
244static int load_file( const char *input_name, const char *output_name )
245{
246 int ret;
247
248 /* Run the preprocessor on the input */
249 if(!no_preprocess)
250 {
Alexandre Julliardcb9be962010-03-30 15:33:54 +0200251 FILE *output;
252 int ret, fd;
253 char *name;
254
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200255 /*
256 * Preprocess the input to a temp-file, or stdout if
257 * no output was given.
258 */
259
Alexandre Julliardcb9be962010-03-30 15:33:54 +0200260 if (preprocess_only)
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200261 {
Alexandre Julliardcb9be962010-03-30 15:33:54 +0200262 if (output_name)
263 {
264 if (!(output = fopen( output_name, "w" )))
265 fatal_perror( "Could not open %s for writing", output_name );
266 ret = wpp_parse( input_name, output );
267 fclose( output );
268 }
269 else ret = wpp_parse( input_name, stdout );
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200270
Alexandre Julliardcb9be962010-03-30 15:33:54 +0200271 if (ret) return ret;
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200272 output_name = NULL;
273 exit(0);
274 }
275
Alexandre Julliard5fabc5c2010-12-29 19:54:54 +0100276 if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
Alexandre Julliardcb9be962010-03-30 15:33:54 +0200277 else name = xstrdup( "wrc.XXXXXX" );
278
279 if ((fd = mkstemps( name, 0 )) == -1)
280 error("Could not generate a temp name from %s\n", name);
281
282 temp_name = name;
283 if (!(output = fdopen(fd, "wt")))
284 error("Could not open fd %s for writing\n", name);
285
286 ret = wpp_parse( input_name, output );
287 fclose( output );
288 if (ret) return ret;
289 input_name = name;
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200290 }
291
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200292 /* Reset the language */
293 currentlanguage = dup_language( defaultlanguage );
Alexandre Julliardbe7558f2009-08-20 15:33:03 +0200294 check_utf8 = 1;
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200295
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200296 /* Go from .rc to .res */
297 chat("Starting parse\n");
298
299 if(!(parser_in = fopen(input_name, "rb")))
300 fatal_perror("Could not open %s for input", input_name);
301
302 ret = parser_parse();
303 fclose(parser_in);
Michael Stefaniucb90a74c2009-12-29 18:07:22 +0100304 parser_lex_destroy();
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200305 if (temp_name)
306 {
307 unlink( temp_name );
308 temp_name = NULL;
309 }
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200310 free( currentlanguage );
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200311 return ret;
312}
313
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200314static void set_target( const char *target )
315{
316 char *p, *cpu = xstrdup( target );
317
318 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
319 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
320 *p = 0;
321 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) || !strcmp( cpu, "ia64" ))
322 pointer_size = 8;
323 else
324 pointer_size = 4;
325 free( cpu );
326}
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200327
Alexandre Julliarda845b881998-06-01 10:44:35 +0000328int main(int argc,char *argv[])
329{
330 extern char* optarg;
331 extern int optind;
Gerald Pfeifer714148c2003-01-23 21:21:50 +0000332 int optc;
Gerald Pfeifer714148c2003-01-23 21:21:50 +0000333 int opti = 0;
Dimitrie O. Paun6f264582002-12-11 01:31:22 +0000334 int stdinc = 1;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000335 int lose = 0;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200336 int nb_files = 0;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000337 int i;
338 int cmdlen;
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100339 int po_mode = 0;
Alexandre Julliardd8c986e2011-01-04 11:18:45 +0100340 char *po_dir = NULL;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200341 char **files = xmalloc( argc * sizeof(*files) );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000342
Bertho Stultiens27337af2000-05-01 20:05:58 +0000343 signal(SIGSEGV, segvhandler);
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200344 signal( SIGTERM, exit_on_signal );
345 signal( SIGINT, exit_on_signal );
346#ifdef SIGHUP
347 signal( SIGHUP, exit_on_signal );
348#endif
Bertho Stultiens27337af2000-05-01 20:05:58 +0000349
350 now = time(NULL);
351
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000352 /* Set the default defined stuff */
Alexandre Julliardac21d342005-10-10 10:43:41 +0000353 set_version_defines();
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000354 wpp_add_cmdline_define("RC_INVOKED=1");
Alexandre Julliarda8d69d62005-10-10 18:07:34 +0000355 /* Microsoft RC always searches current directory */
356 wpp_add_include_path(".");
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000357
Alexandre Julliarda845b881998-06-01 10:44:35 +0000358 /* First rebuild the commandline to put in destination */
359 /* Could be done through env[], but not all OS-es support it */
360 cmdlen = 4; /* for "wrc " */
361 for(i = 1; i < argc; i++)
362 cmdlen += strlen(argv[i]) + 1;
Alexandre Julliardfce74f72006-08-26 21:33:29 +0200363 cmdline = xmalloc(cmdlen);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000364 strcpy(cmdline, "wrc ");
365 for(i = 1; i < argc; i++)
366 {
367 strcat(cmdline, argv[i]);
368 if(i < argc-1)
369 strcat(cmdline, " ");
370 }
371
Dimitrie O. Paund26933e2003-01-11 20:55:18 +0000372 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000373 {
374 switch(optc)
375 {
Alexandre Julliard683dc302009-06-29 13:12:59 +0200376 case LONG_OPT_NOSTDINC:
Dimitrie O. Paund5a194b2003-03-19 22:45:51 +0000377 stdinc = 0;
378 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200379 case LONG_OPT_TMPFILE:
Dimitrie O. Paund5a194b2003-03-19 22:45:51 +0000380 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
Dimitrie O. Paund26933e2003-01-11 20:55:18 +0000381 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200382 case LONG_OPT_NOTMPFILE:
Dimitrie O. Paund5a194b2003-03-19 22:45:51 +0000383 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
384 break;
Alexandre Julliardd8c986e2011-01-04 11:18:45 +0100385 case LONG_OPT_PO_DIR:
386 po_dir = xstrdup( optarg );
387 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200388 case LONG_OPT_PREPROCESSOR:
Dimitrie O. Paund5a194b2003-03-19 22:45:51 +0000389 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
390 else fprintf(stderr, "-P option not yet supported, ignored.\n");
391 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200392 case LONG_OPT_VERSION:
Dimitrie O. Paund5a194b2003-03-19 22:45:51 +0000393 printf(version_string);
394 exit(0);
Dimitrie O. Paune41ec932002-12-12 22:23:55 +0000395 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200396 case LONG_OPT_DEBUG:
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000397 debuglevel = strtol(optarg, NULL, 0);
398 break;
Francois Gouget9c11faf2011-08-24 15:22:52 +0200399 case LONG_OPT_ENDIANNESS:
Bertho Stultiens3d455c92000-05-09 22:35:10 +0000400 switch(optarg[0])
401 {
402 case 'n':
403 case 'N':
404 byteorder = WRC_BO_NATIVE;
405 break;
406 case 'l':
407 case 'L':
408 byteorder = WRC_BO_LITTLE;
409 break;
410 case 'b':
411 case 'B':
412 byteorder = WRC_BO_BIG;
413 break;
414 default:
Francois Gouget0e85f542001-05-07 18:17:58 +0000415 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
Bertho Stultiens3d455c92000-05-09 22:35:10 +0000416 lose++;
417 }
418 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200419 case LONG_OPT_PEDANTIC:
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000420 pedantic = 1;
421 wpp_set_pedantic(1);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000422 break;
Alexandre Julliard683dc302009-06-29 13:12:59 +0200423 case LONG_OPT_VERIFY_TRANSL:
Vincent Béron84512402004-01-18 23:24:04 +0000424 verify_translations_mode = 1;
425 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000426 case 'D':
Alexandre Julliardb7d3c792002-07-16 02:33:10 +0000427 wpp_add_cmdline_define(optarg);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000428 break;
Bertho Stultiens27337af2000-05-01 20:05:58 +0000429 case 'E':
430 preprocess_only = 1;
431 break;
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200432 case 'b':
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000433 case 'F':
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200434 set_target( optarg );
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000435 break;
Dimitrie O. Paund26933e2003-01-11 20:55:18 +0000436 case 'h':
437 printf(usage);
438 exit(0);
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000439 case 'i':
Alexandre Julliard683dc302009-06-29 13:12:59 +0200440 files[nb_files++] = optarg;
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000441 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000442 case 'I':
Dimitrie O. Paunfea01642003-03-20 19:23:08 +0000443 wpp_add_include_path(optarg);
444 break;
445 case 'J':
446 if (strcmp(optarg, "rc16") == 0) extensions = 0;
Dimitrie O. Paun45864a72003-04-19 02:47:28 +0000447 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
Dimitrie O. Paun6f264582002-12-11 01:31:22 +0000448 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000449 case 'l':
450 {
451 int lan;
452 lan = strtol(optarg, NULL, 0);
Alexandre Julliardf22ba052002-08-15 21:57:36 +0000453 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
Dimitrie O. Paun45864a72003-04-19 02:47:28 +0000454 error("Language %04x is not supported\n", lan);
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200455 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
Alexandre Julliarda845b881998-06-01 10:44:35 +0000456 }
457 break;
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200458 case 'm':
459 if (!strcmp( optarg, "16" )) win32 = 0;
460 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
461 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
462 else error( "Invalid option: -m%s\n", optarg );
463 break;
Dimitrie O. Paun45864a72003-04-19 02:47:28 +0000464 case 'f':
465 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
466 optarg++;
467 /* fall through */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000468 case 'o':
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000469 if (!output_name) output_name = strdup(optarg);
470 else error("Too many output files.\n");
471 break;
472 case 'O':
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100473 if (strcmp(optarg, "po") == 0) po_mode = 1;
474 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
475 else if (strcmp(optarg, "res16") == 0) win32 = 0;
Dimitrie O. Paun45864a72003-04-19 02:47:28 +0000476 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
Dimitrie O. Paun095322e2003-02-11 21:58:46 +0000477 break;
Dimitrie O. Paun29ba6162003-03-28 19:31:49 +0000478 case 'r':
479 /* ignored for compatibility with rc */
480 break;
481 case 'U':
482 wpp_del_define(optarg);
483 break;
Dimitrie O. Paund26933e2003-01-11 20:55:18 +0000484 case 'v':
485 debuglevel = DEBUGLEVEL_CHAT;
486 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000487 default:
488 lose++;
489 break;
490 }
491 }
492
493 if(lose)
494 {
495 fprintf(stderr, usage);
496 return 1;
497 }
498
Alexandre Julliarde6340f12010-09-29 20:13:28 +0200499 if (win32)
500 {
501 wpp_add_cmdline_define("_WIN32=1");
502 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
503 }
504
Dimitrie O. Paun6f264582002-12-11 01:31:22 +0000505 /* If we do need to search standard includes, add them to the path */
506 if (stdinc)
507 {
508 wpp_add_include_path(INCLUDEDIR"/msvcrt");
509 wpp_add_include_path(INCLUDEDIR"/windows");
510 }
Dimitrie O. Paun9d8b1492002-12-06 19:49:36 +0000511
Alexandre Julliarda845b881998-06-01 10:44:35 +0000512 /* Kill io buffering when some kind of debuglevel is enabled */
513 if(debuglevel)
514 {
Rob Shearman903f6862010-01-21 11:57:09 +0000515 setbuf(stdout, NULL);
516 setbuf(stderr, NULL);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000517 }
518
Alexandre Julliard42418fb2006-09-12 09:05:20 +0200519 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
Bertho Stultiens27337af2000-05-01 20:05:58 +0000520 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
Alexandre Julliardb7d3c792002-07-16 02:33:10 +0000521
522 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
523 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
524 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000525
Alexandre Julliarda845b881998-06-01 10:44:35 +0000526 /* Check if the user set a language, else set default */
Michael Stefaniucd67a9d92009-07-10 11:48:34 +0200527 if(!defaultlanguage)
528 defaultlanguage = new_language(0, 0);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000529
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200530 atexit(cleanup_files);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000531
Alexandre Julliard683dc302009-06-29 13:12:59 +0200532 while (optind < argc) files[nb_files++] = argv[optind++];
Bertho Stultiens27337af2000-05-01 20:05:58 +0000533
Alexandre Julliard683dc302009-06-29 13:12:59 +0200534 for (i = 0; i < nb_files; i++)
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200535 {
Alexandre Julliard683dc302009-06-29 13:12:59 +0200536 input_name = files[i];
Alexandre Julliarde25bc792009-06-22 12:05:57 +0200537 if (load_file( input_name, output_name )) exit(1);
538 }
Marcus Meissner39d4d122009-08-08 11:00:36 +0200539 /* stdin special case. NULL means "stdin" for wpp. */
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100540 if (nb_files == 0 && load_file( NULL, output_name )) exit(1);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000541
Dimitrie O. Paundeaf02a2003-02-26 05:04:12 +0000542 if(debuglevel & DEBUGLEVEL_DUMP)
543 dump_resources(resource_top);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000544
Vincent Béron84512402004-01-18 23:24:04 +0000545 if(verify_translations_mode)
546 {
547 verify_translations(resource_top);
548 exit(0);
549 }
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100550 if (po_mode)
551 {
552 if (po_mode == 2) /* pot file */
553 {
554 if (!output_name)
555 {
556 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
557 strcat( output_name, ".pot" );
558 }
559 write_pot_file( output_name );
560 }
561 else write_po_files( output_name );
562 output_name = NULL;
563 exit(0);
564 }
Alexandre Julliardf4ec0972011-10-22 18:21:04 +0200565 add_translations( po_dir );
Vincent Béron84512402004-01-18 23:24:04 +0000566
Dimitrie O. Paundeaf02a2003-02-26 05:04:12 +0000567 /* Convert the internal lists to binary data */
568 resources2res(resource_top);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000569
Francois Gouget7e922832007-10-18 17:11:52 +0200570 chat("Writing .res-file\n");
Alexandre Julliard10700bf2011-01-04 11:18:10 +0100571 if (!output_name)
572 {
573 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
574 strcat(output_name, ".res");
575 }
Dimitrie O. Paundeaf02a2003-02-26 05:04:12 +0000576 write_resfile(output_name, resource_top);
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200577 output_name = NULL;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000578
579 return 0;
580}
581
582
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200583static void cleanup_files(void)
Bertho Stultiens27337af2000-05-01 20:05:58 +0000584{
Alexandre Julliardf1de64d2006-05-27 13:23:37 +0200585 if (output_name) unlink(output_name);
586 if (temp_name) unlink(temp_name);
Bertho Stultiens27337af2000-05-01 20:05:58 +0000587}
Alexandre Julliarda845b881998-06-01 10:44:35 +0000588
Bertho Stultiens27337af2000-05-01 20:05:58 +0000589static void segvhandler(int sig)
590{
591 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
592 fflush(stdout);
593 fflush(stderr);
594 abort();
595}