wrc.c 13.9 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2
 * Copyright 1994 Martin von Loewis
Austin English's avatar
Austin English committed
3
 * Copyright 1998 Bertho A. Stultiens (BS)
4
 * Copyright 2003 Dimitrie O. Paun
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

22
#include "config.h"
23
#include "wine/port.h"
24

Alexandre Julliard's avatar
Alexandre Julliard committed
25 26
#include <stdio.h>
#include <stdlib.h>
27 28 29
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
30 31 32
#include <string.h>
#include <assert.h>
#include <ctype.h>
33
#include <signal.h>
34 35 36
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
37 38 39 40 41 42 43 44

#include "wrc.h"
#include "utils.h"
#include "readres.h"
#include "dumpres.h"
#include "genres.h"
#include "newstruc.h"
#include "parser.h"
45
#include "wine/wpp.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
46

47 48 49 50
#ifndef INCLUDEDIR
#define INCLUDEDIR "/usr/local/include/wine"
#endif

51 52 53 54 55 56
#ifdef WORDS_BIGENDIAN
#define ENDIAN	"big"
#else
#define ENDIAN	"little"
#endif

57
static const char usage[] =
58
	"Usage: wrc [options...] [infile[.rc|.res]] [outfile]\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
59
	"   -D id[=val] Define preprocessor identifier id=val\n"
60
	"   -E          Preprocess only\n"
61 62 63
	"   -F target   Ignored for compatibility with windres\n"
	"   -h          Prints this summary\n"
	"   -i file     The name of the input file\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
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 69
	"   -O format   The output format (either `res' or `res16`)\n"
	"   -r          Ignored for compatibility with rc\n"
70 71
	"   -U id       Undefine preprocessor identifier id\n"
	"   -v          Enable verbose mode\n"
72
	"The following long options are supported:\n"
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	"   --debug=nn            Set debug level to 'nn'\n"
	"   --define              Synonym for -D\n"
	"   --endianess=e         Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
	"                         (win32 only; default is " ENDIAN "-endian)\n"
	"   --help                Synonym for -h\n"
	"   --include-dir         Synonym for -I\n"
	"   --input               Synonym for -i\n"
	"   --input-format        Synonym for -J\n"
	"   --language            Synonym for -l\n"
	"   --no-use-temp-file    Ignored for compatibility with windres\n"
	"   --nostdinc            Disables searching the standard include path\n"
	"   --output -fo          Synonym for -o\n"
	"   --output-format       Synonym for -O\n"
	"   --pedantic            Enable pedantic warnings\n"
	"   --preprocessor        Specifies the preprocessor to use, including arguments\n"
	"   --target              Synonym for -F\n"
	"   --undefine            Synonym for -U\n"
	"   --use-temp-file       Ignored for compatibility with windres\n"
91
	"   --verbose             Synonym for -v\n"
92 93
	"   --verify-translations Check the status of the various translations\n"
	"   --version             Print version and exit\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
94 95 96 97 98
	"Input is taken from stdin if no sourcefile specified.\n"
	"Debug level 'n' is a bitmask with following meaning:\n"
	"    * 0x01 Tell which resource is parsed (verbose mode)\n"
	"    * 0x02 Dump internal structures\n"
	"    * 0x04 Create a parser trace (yydebug=1)\n"
99 100 101
	"    * 0x08 Preprocessor messages\n"
	"    * 0x10 Preprocessor lex messages\n"
	"    * 0x20 Preprocessor yacc trace\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
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"
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
	;

106
static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
107
			"Copyright 1998-2000 Bertho A. Stultiens\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
108 109 110 111 112 113 114 115 116
			"          1994 Martin von Loewis\n";

/*
 * Set if compiling in 32bit mode (default).
 */
int win32 = 1;

/*
 * debuglevel == DEBUGLEVEL_NONE	Don't bother
Austin English's avatar
Austin English committed
117
 * debuglevel & DEBUGLEVEL_CHAT		Say what's done
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119
 * debuglevel & DEBUGLEVEL_DUMP		Dump internal structures
 * debuglevel & DEBUGLEVEL_TRACE	Create parser trace
120 121 122
 * debuglevel & DEBUGLEVEL_PPMSG	Preprocessor messages
 * debuglevel & DEBUGLEVEL_PPLEX	Preprocessor lex trace
 * debuglevel & DEBUGLEVEL_PPTRACE	Preprocessor yacc trace
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
 */
int debuglevel = DEBUGLEVEL_NONE;

/*
 * Recognize win32 keywords if set (-w 32 enforces this),
 * otherwise set with -e option.
 */
int extensions = 1;

/*
 * Language setting for resources (-l option)
 */
language_t *currentlanguage = NULL;

/*
 * Set when extra warnings should be generated (-W option)
 */
int pedantic = 0;

142 143 144 145 146
/*
 * The output byte-order of resources (set with -B)
 */
int byteorder = WRC_BO_NATIVE;

147 148 149 150 151 152
/*
 * Set when _only_ to run the preprocessor (-E option)
 */
int preprocess_only = 0;

/*
153
 * Set when _not_ to run the preprocessor (-P cat option)
154 155 156
 */
int no_preprocess = 0;

157 158
static int verify_translations_mode;

159 160 161
char *output_name = NULL;	/* The name given by the -o option */
char *input_name = NULL;	/* The name given on the command-line */
char *temp_name = NULL;		/* Temporary file for preprocess pipe */
162 163 164

int line_number = 1;		/* The current line */
int char_number = 1;		/* The current char pos within the line */
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166

char *cmdline;			/* The entire commandline */
167
time_t now;			/* The time of start of wrc */
Alexandre Julliard's avatar
Alexandre Julliard committed
168

169
int parser_debug, yy_flex_debug;
170

Alexandre Julliard's avatar
Alexandre Julliard committed
171 172 173
resource_t *resource_top;	/* The top of the parsed resources */

int getopt (int argc, char *const *argv, const char *optstring);
174
static void cleanup_files(void);
175
static void segvhandler(int sig);
Alexandre Julliard's avatar
Alexandre Julliard committed
176

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 181 182 183 184
	{ "debug", 1, 0, 6 },
	{ "define", 1, 0, 'D' },
	{ "endianess", 1, 0, 7 },
	{ "help", 0, 0, 'h' },
	{ "include-dir", 1, 0, 'I' },
185
	{ "input", 1, 0, 'i' },
186
	{ "input-format", 1, 0, 'J' },
187 188 189
	{ "language", 1, 0, 'l' },
	{ "no-use-temp-file", 0, 0, 3 },
	{ "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 195
	{ "target", 1, 0, 'F' },
	{ "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 201 202
	{ 0, 0, 0, 0 }
};

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static void set_version_defines(void)
{
    char *version = xstrdup( PACKAGE_VERSION );
    char *major, *minor, *patchlevel;
    char buffer[100];

    if ((minor = strchr( version, '.' )))
    {
        major = version;
        *minor++ = 0;
        if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
    }
    else  /* pre 0.9 version */
    {
        major = NULL;
        patchlevel = version;
    }
    sprintf( buffer, "__WRC__=%s", major ? major : "0" );
    wpp_add_cmdline_define(buffer);
    sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
    wpp_add_cmdline_define(buffer);
    sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
    wpp_add_cmdline_define(buffer);
    free( version );
}

229 230 231 232 233 234
/* clean things up when aborting on a signal */
static void exit_on_signal( int sig )
{
    exit(1);  /* this will call the atexit functions */
}

Alexandre Julliard's avatar
Alexandre Julliard committed
235 236 237 238
int main(int argc,char *argv[])
{
	extern char* optarg;
	extern int   optind;
239 240
	int optc;
	int opti = 0;
241
	int stdinc = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
242 243 244 245 246
	int lose = 0;
	int ret;
	int i;
	int cmdlen;

247
	signal(SIGSEGV, segvhandler);
248 249 250 251 252
        signal( SIGTERM, exit_on_signal );
        signal( SIGINT, exit_on_signal );
#ifdef SIGHUP
        signal( SIGHUP, exit_on_signal );
#endif
253 254 255

	now = time(NULL);

256
	/* Set the default defined stuff */
257
        set_version_defines();
258 259 260
	wpp_add_cmdline_define("RC_INVOKED=1");
	wpp_add_cmdline_define("__WIN32__=1");
	wpp_add_cmdline_define("__FLAT__=1");
261 262
	/* Microsoft RC always searches current directory */
	wpp_add_include_path(".");
263

Alexandre Julliard's avatar
Alexandre Julliard committed
264 265 266 267 268
	/* First rebuild the commandline to put in destination */
	/* Could be done through env[], but not all OS-es support it */
	cmdlen = 4; /* for "wrc " */
	for(i = 1; i < argc; i++)
		cmdlen += strlen(argv[i]) + 1;
269
	cmdline = xmalloc(cmdlen);
Alexandre Julliard's avatar
Alexandre Julliard committed
270 271 272 273 274 275 276 277
	strcpy(cmdline, "wrc ");
	for(i = 1; i < argc; i++)
	{
		strcat(cmdline, argv[i]);
		if(i < argc-1)
			strcat(cmdline, " ");
	}

278
	while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
Alexandre Julliard's avatar
Alexandre Julliard committed
279 280 281
	{
		switch(optc)
		{
282 283 284
		case 1:
			stdinc = 0;
			break;
285
		case 2:
286
			if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
287 288
			break;
		case 3:
289 290 291 292 293 294 295 296 297
			if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
			break;
		case 4:
			if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
			else fprintf(stderr, "-P option not yet supported, ignored.\n");
			break;
		case 5:
			printf(version_string);
			exit(0);
298
			break;
299 300 301 302
		case 6:
			debuglevel = strtol(optarg, NULL, 0);
			break;
		case 7:
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
			switch(optarg[0])
			{
			case 'n':
			case 'N':
				byteorder = WRC_BO_NATIVE;
				break;
			case 'l':
			case 'L':
				byteorder = WRC_BO_LITTLE;
				break;
			case 'b':
			case 'B':
				byteorder = WRC_BO_BIG;
				break;
			default:
318
				fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
319 320 321
				lose++;
			}
			break;
322 323 324
		case 8:
			pedantic = 1;
			wpp_set_pedantic(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
325
			break;
326 327 328
		case 9:
			verify_translations_mode = 1;
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
329
		case 'D':
330
			wpp_add_cmdline_define(optarg);
Alexandre Julliard's avatar
Alexandre Julliard committed
331
			break;
332 333 334
		case 'E':
			preprocess_only = 1;
			break;
335 336 337
		case 'F':
			/* ignored for compatibility with windres */
			break;
338 339 340
		case 'h':
			printf(usage);
			exit(0);
341 342 343 344
		case 'i':
			if (!input_name) input_name = strdup(optarg);
			else error("Too many input files.\n");
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
345
		case 'I':
346 347 348 349
			wpp_add_include_path(optarg);
			break;
		case 'J':
			if (strcmp(optarg, "rc16") == 0)  extensions = 0;
350
			else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
351
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
352 353 354 355
		case 'l':
			{
				int lan;
				lan = strtol(optarg, NULL, 0);
356
				if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
357
					error("Language %04x is not supported\n", lan);
Alexandre Julliard's avatar
Alexandre Julliard committed
358 359 360
				currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
			}
			break;
361 362 363 364
		case 'f':
			if (*optarg != 'o') error("Unknown option: -f%s\n",  optarg);
			optarg++;
			/* fall through */
Alexandre Julliard's avatar
Alexandre Julliard committed
365
		case 'o':
366 367 368 369
			if (!output_name) output_name = strdup(optarg);
			else error("Too many output files.\n");
			break;
		case 'O':
370 371 372 373 374 375
			if (strcmp(optarg, "res16") == 0)
			{
				win32 = 0;
				wpp_del_define("__WIN32__");
				wpp_del_define("__FLAT__");
			}
376
			else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
377
			break;
378 379 380 381 382 383
		case 'r':
			/* ignored for compatibility with rc */
			break;
		case 'U':
			wpp_del_define(optarg);
			break;
384 385 386
		case 'v':
			debuglevel = DEBUGLEVEL_CHAT;
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
387 388 389 390 391 392 393 394 395 396 397 398
		default:
			lose++;
			break;
		}
	}

	if(lose)
	{
		fprintf(stderr, usage);
		return 1;
	}

399 400 401 402 403 404 405
	/* If we do need to search standard includes, add them to the path */
	if (stdinc)
	{
		wpp_add_include_path(INCLUDEDIR"/msvcrt");
		wpp_add_include_path(INCLUDEDIR"/windows");
	}
	
406 407 408 409 410 411 412 413 414 415 416 417 418 419
	/* Check for input file on command-line */
	if(optind < argc)
	{
		if (!input_name) input_name = argv[optind++];
		else error("Too many input files.\n");
	}

	/* Check for output file on command-line */
	if(optind < argc)
	{
		if (!output_name) output_name = argv[optind++];
		else error("Too many output files.\n");
	}

Alexandre Julliard's avatar
Alexandre Julliard committed
420 421 422 423 424 425 426
	/* Kill io buffering when some kind of debuglevel is enabled */
	if(debuglevel)
	{
		setbuf(stdout,0);
		setbuf(stderr,0);
	}

427
	parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
428
	yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
429 430 431 432

        wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
                       (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
                       (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
433 434 435 436 437

	/* Check if the user set a language, else set default */
	if(!currentlanguage)
		currentlanguage = new_language(0, 0);

438 439
	/* Generate appropriate outfile names */
	if(!output_name && !preprocess_only)
Alexandre Julliard's avatar
Alexandre Julliard committed
440
	{
441 442
		output_name = dup_basename(input_name, ".rc");
		strcat(output_name, ".res");
Alexandre Julliard's avatar
Alexandre Julliard committed
443
	}
444
	atexit(cleanup_files);
Alexandre Julliard's avatar
Alexandre Julliard committed
445

446
	/* Run the preprocessor on the input */
447
	if(!no_preprocess)
448 449 450 451 452 453
	{
		/*
		 * Preprocess the input to a temp-file, or stdout if
		 * no output was given.
		 */

454
		chat("Starting preprocess\n");
455

456 457
                if (!preprocess_only)
                {
458
                    ret = wpp_parse_temp( input_name, output_name, &temp_name );
459 460 461 462 463 464 465 466 467 468 469 470 471 472
                }
                else if (output_name)
                {
                    FILE *output;

                    if (!(output = fopen( output_name, "w" )))
                        error( "Could not open %s for writing\n", output_name );
                    ret = wpp_parse( input_name, output );
                    fclose( output );
                }
                else
                {
                    ret = wpp_parse( input_name, stdout );
                }
473 474 475 476 477

		if(ret)
			exit(1);	/* Error during preprocess */

		if(preprocess_only)
478 479
		{
			output_name = NULL;
480
			exit(0);
481
		}
482 483

		input_name = temp_name;
484 485
	}

486
	/* Go from .rc to .res */
487
	chat("Starting parse\n");
488

489
	if(!(parser_in = fopen(input_name, "rb")))
490
		error("Could not open %s for input\n", input_name);
Alexandre Julliard's avatar
Alexandre Julliard committed
491

492
	ret = parser_parse();
Alexandre Julliard's avatar
Alexandre Julliard committed
493

494
	if(input_name) fclose(parser_in);
Alexandre Julliard's avatar
Alexandre Julliard committed
495

496
	if(ret) exit(1); /* Error during parse */
Alexandre Julliard's avatar
Alexandre Julliard committed
497

498 499
	if(debuglevel & DEBUGLEVEL_DUMP)
		dump_resources(resource_top);
Alexandre Julliard's avatar
Alexandre Julliard committed
500

501 502 503 504 505 506
	if(verify_translations_mode)
	{
		verify_translations(resource_top);
		exit(0);
	}

507 508
	/* Convert the internal lists to binary data */
	resources2res(resource_top);
Alexandre Julliard's avatar
Alexandre Julliard committed
509

510
	chat("Writing .res-file\n");
511
	write_resfile(output_name, resource_top);
512
	output_name = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
513 514 515 516 517

	return 0;
}


518
static void cleanup_files(void)
519
{
520 521
	if (output_name) unlink(output_name);
	if (temp_name) unlink(temp_name);
522
}
Alexandre Julliard's avatar
Alexandre Julliard committed
523

524 525 526 527 528 529 530
static void segvhandler(int sig)
{
	fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
	fflush(stdout);
	fflush(stderr);
	abort();
}