Sweden-Number/tools/wrc/wrc.c

818 lines
19 KiB
C

/*
* Copyright Martin von Loewis, 1994
* Copyrignt 1998 Bertho A. Stultiens (BS)
*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* 30-Apr-2000 BS - Integrated a new preprocessor (-E and -N)
* 20-Jun-1998 BS - Added -L option to prevent case conversion
* of embedded filenames.
*
* 08-Jun-1998 BS - Added -A option to generate autoregister code
* for winelib operation.
*
* 21-May-1998 BS - Removed the CPP option. Its internal now.
* - Added implementations for defines and includes
* on the commandline.
*
* 30-Apr-1998 BS - The options now contain nearly the entire alphabet.
* Seems to be a sign for too much freedom. I implemeted
* most of them as a user choice possibility for things
* that I do not know what to put there by default.
* - -l and -L options are now known as -t and -T.
*
* 23-Apr-1998 BS - Finally gave up on backward compatibility on the
* commandline (after a blessing from the newsgroup).
* So, I changed the lot.
*
* 17-Apr-1998 BS - Added many new command-line options but took care
* that it would not break old scripts (sigh).
*
* 16-Apr-1998 BS - There is not much left of the original source...
* I had to rewrite most of it because the parser
* changed completely with all the types etc..
*
*/
#include "config.h"
#include "wine/port.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include "wrc.h"
#include "utils.h"
#include "writeres.h"
#include "readres.h"
#include "dumpres.h"
#include "genres.h"
#include "newstruc.h"
#include "parser.h"
#include "../wpp/wpp.h"
#ifndef INCLUDEDIR
#define INCLUDEDIR "/usr/local/include/wine"
#endif
static char usage[] =
"Usage: wrc [options...] [infile[.rc|.res]] [outfile]\n"
" -a n Alignment of resource (win16 only, default is 4)\n"
" -A Auto register resources (only with gcc 2.7 and better)\n"
" -b Create an assembly array from a binary .res file\n"
" -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
" (win32 only; default is n[ative] which equals "
#ifdef WORDS_BIGENDIAN
"big"
#else
"little"
#endif
"-endian)\n"
" -c Add 'const' prefix to C constants\n"
" -C cp Set the resource's codepage to cp (default is 0)\n"
" -d n Set debug level to 'n'\n"
" -D id[=val] Define preprocessor identifier id=val\n"
" -e Disable recognition of win32 keywords in 16bit compile\n"
" -E Preprocess only\n"
" -F target Ignored for compatibility with windres\n"
" -g Add symbols to the global c namespace\n"
" -h Also generate a .h file\n"
" -H file Same as -h but written to file\n"
" -i file The name of the input file.\n"
" -I path Set include search dir to path (multiple -I allowed)\n"
" -J Do not search the standard include path\n"
" -l lan Set default language to lan (default is neutral {0, 0})\n"
" -m Do not remap numerical resource IDs\n"
" -n Do not generate .s file\n"
" -N Do not preprocess input\n"
" -o file Output to file (default is infile.[res|s|h]\n"
" -O format The output format to generate. format may be `res' only.\n"
" -p prefix Give a prefix for the generated names\n"
" -r Create binary .res file (compile only)\n"
" -s Add structure with win32/16 (PE/NE) resource directory\n"
" -t Generate indirect loadable resource tables\n"
" -T Generate only indirect loadable resources tables\n"
" -V Print version and exit\n"
" -w 16|32 Select win16 or win32 output (default is win32)\n"
" -W Enable pedantic warnings\n"
#ifdef HAVE_GETOPT_LONG
"The following long options are supported:\n"
" --input Synonym for -i.\n"
" --output Synonym for -o.\n"
" --target Synonym for -F.\n"
" --format Synonym for -O.\n"
" --include-dir Synonym for -I.\n"
" --nostdinc Synonym for -J.\n"
" --define Synonym for -D.\n"
" --language Synonym for -l.\n"
" --preprocessor Specify the preprocessor to use, including arguments.\n"
" --help Prints a usage summary.\n"
" --version Synonym for -V.\n"
#endif
"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"
" * 0x08 Preprocessor messages\n"
" * 0x10 Preprocessor lex messages\n"
" * 0x20 Preprocessor yacc trace\n"
"The -o option only applies to the final destination file, which is\n"
"in case of normal compile a .s file. You must use the '-H header.h'\n"
"option to override the header-filename.\n"
"If no input filename is given and the output name is not overridden\n"
"with -o and/or -H, then the output is written to \"wrc.tab.[sh]\"\n"
;
char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
"Copyright 1998-2000 Bertho A. Stultiens\n"
" 1994 Martin von Loewis\n";
/*
* Default prefix for resource names used in the C array.
* Option '-p name' sets it to 'name'
*/
char *prefix = __ASM_NAME("_Resource");
/*
* Set if compiling in 32bit mode (default).
*/
int win32 = 1;
/*
* Set when generated C variables should be prefixed with 'const'
*/
int constant = 0;
/*
* Create a .res file from the source and exit (-r option).
*/
int create_res = 0;
/*
* debuglevel == DEBUGLEVEL_NONE Don't bother
* debuglevel & DEBUGLEVEL_CHAT Say whats done
* debuglevel & DEBUGLEVEL_DUMP Dump internal structures
* debuglevel & DEBUGLEVEL_TRACE Create parser trace
* debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
* debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
* debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
*/
int debuglevel = DEBUGLEVEL_NONE;
/*
* Recognize win32 keywords if set (-w 32 enforces this),
* otherwise set with -e option.
*/
int extensions = 1;
/*
* Set when creating C array from .res file (-b option).
*/
int binary = 0;
/*
* Set when an additional C-header is to be created in compile (-h option).
*/
int create_header = 0;
/*
* Set when the NE/PE resource directory should be dumped into
* the output file.
*/
int create_dir = 0;
/*
* Set when all symbols should be added to the global namespace (-g option)
*/
int global = 0;
/*
* Set when indirect loadable resource tables should be created (-t)
*/
int indirect = 0;
/*
* Set when _only_ indirect loadable resource tables should be created (-T)
*/
int indirect_only = 0;
/*
* NE segment resource aligment (-a option)
*/
int alignment = 4;
int alignment_pwr;
/*
* Cleared when the assembly file must be suppressed (-n option)
*/
int create_s = 1;
/*
* Language setting for resources (-l option)
*/
language_t *currentlanguage = NULL;
/*
* The codepage to write in win32 PE resource segment (-C option)
*/
DWORD codepage = 0;
/*
* Set when extra warnings should be generated (-W option)
*/
int pedantic = 0;
/*
* Set when autoregister code must be added to the output (-A option)
*/
int auto_register = 0;
/*
* The output byte-order of resources (set with -B)
*/
int byteorder = WRC_BO_NATIVE;
/*
* Set when _only_ to run the preprocessor (-E option)
*/
int preprocess_only = 0;
/*
* Set when _not_ to run the preprocessor (-N option)
*/
int no_preprocess = 0;
/*
* Cleared when _not_ to remap resource types (-m option)
*/
int remap = 1;
char *output_name; /* The name given by the -o option */
char *input_name; /* The name given on the command-line */
char *header_name; /* The name given by the -H option */
char *temp_name; /* Temporary file for preprocess pipe */
int line_number = 1; /* The current line */
int char_number = 1; /* The current char pos within the line */
char *cmdline; /* The entire commandline */
time_t now; /* The time of start of wrc */
resource_t *resource_top; /* The top of the parsed resources */
int getopt (int argc, char *const *argv, const char *optstring);
static void rm_tempfile(void);
static void segvhandler(int sig);
#ifdef HAVE_GETOPT_LONG
static struct option long_options[] = {
{ "input", 1, 0, 'i' },
{ "output", 1, 0, 'o' },
{ "target", 1, 0, 'F' },
{ "format", 1, 0, 'O' },
{ "include-dir", 1, 0, 'I' },
{ "nostdinc", 0, 0, 'J' },
{ "define", 1, 0, 'D' },
{ "language", 1, 0, 'l' },
{ "preprocessor", 1, 0, 1 },
{ "help", 0, 0, 2 },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
};
#endif
int main(int argc,char *argv[])
{
extern char* optarg;
extern int optind;
int optc, opti = 0;
int stdinc = 1;
int lose = 0;
int ret;
int a;
int i;
int cmdlen;
signal(SIGSEGV, segvhandler);
now = time(NULL);
/* 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;
cmdline = (char *)xmalloc(cmdlen);
strcpy(cmdline, "wrc ");
for(i = 1; i < argc; i++)
{
strcat(cmdline, argv[i]);
if(i < argc-1)
strcat(cmdline, " ");
}
#ifdef HAVE_GETOPT_LONG
while((optc = getopt_long(argc, argv, "a:AbB:cC:d:D:eEF:ghH:i:I:Jl:LmnNo:O:p:rstTVw:W", long_options, &opti)) != EOF)
#else
while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEF:ghH:i:I:Jl:LmnNo:O:p:rstTVw:W")) != EOF)
#endif
{
switch(optc)
{
case 1:
fprintf(stderr, "--preprocessor option not yet supported, ignored.\n");
break;
case 2:
printf(usage);
exit(0);
break;
case 'a':
alignment = atoi(optarg);
break;
case 'A':
auto_register = 1;
break;
case 'b':
binary = 1;
break;
case 'B':
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:
fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
lose++;
}
break;
case 'c':
constant = 1;
break;
case 'C':
codepage = strtol(optarg, NULL, 0);
break;
case 'd':
debuglevel = strtol(optarg, NULL, 0);
break;
case 'D':
wpp_add_cmdline_define(optarg);
break;
case 'e':
extensions = 0;
break;
case 'E':
preprocess_only = 1;
break;
case 'F':
/* ignored for compatibility with windres */
break;
case 'g':
global = 1;
break;
case 'H':
header_name = strdup(optarg);
/* Fall through */
case 'h':
create_header = 1;
break;
case 'i':
if (!input_name) input_name = strdup(optarg);
else error("Too many input files.\n");
break;
case 'I':
wpp_add_include_path(optarg);
break;
case 'J':
stdinc = 0;
break;
case 'l':
{
int lan;
lan = strtol(optarg, NULL, 0);
if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
error("Language %04x is not supported",lan);
currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
}
break;
case 'm':
remap = 0;
break;
case 'n':
create_s = 0;
break;
case 'N':
no_preprocess = 1;
break;
case 'o':
if (!output_name) output_name = strdup(optarg);
else error("Too many output files.\n");
break;
case 'O':
if (strcmp(optarg, "res"))
error("Output format %s not supported.", optarg);
break;
case 'p':
prefix = xstrdup(optarg);
break;
case 'r':
create_res = 1;
break;
case 's':
create_dir = 1;
break;
case 'T':
indirect_only = 1;
/* Fall through */
case 't':
indirect = 1;
break;
case 'V':
printf(version_string);
exit(0);
break;
case 'w':
if(!strcmp(optarg, "16"))
win32 = 0;
else if(!strcmp(optarg, "32"))
win32 = 1;
else
lose++;
break;
case 'W':
pedantic = 1;
wpp_set_pedantic(1);
break;
default:
lose++;
break;
}
}
if(lose)
{
fprintf(stderr, usage);
return 1;
}
/* 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");
}
/* 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");
}
/* Try to guess the output format based on output name */
if (output_name)
{
char *dotstr = strrchr(output_name, '.');
if (dotstr && (strcmp(dotstr+1, "res") == 0 ||
strcmp(dotstr+1, "o") == 0))
create_res = 1;
}
/* Check the command line options for invalid combinations */
if(win32)
{
if(!extensions)
{
warning("Option -e ignored with 32bit compile\n");
extensions = 1;
}
}
if(create_res)
{
if(constant)
{
warning("Option -c ignored with compile to .res\n");
constant = 0;
}
if(create_header)
{
warning("Option -[h|H] ignored with compile to .res\n");
create_header = 0;
}
if(indirect)
{
warning("Option -t ignored with compile to .res\n");
indirect = 0;
}
if(indirect_only)
{
warning("Option -T ignored with compile to .res\n");
indirect_only = 0;
}
if(global)
{
warning("Option -g ignored with compile to .res\n");
global = 0;
}
if(create_dir)
{
error("Option -r and -s cannot be used together\n");
}
if(binary)
{
error("Option -r and -b cannot be used together\n");
}
}
if(byteorder != WRC_BO_NATIVE)
{
if(binary)
error("Forced byteordering not supported for binary resources\n");
}
if(preprocess_only)
{
if(constant)
{
warning("Option -c ignored with preprocess only\n");
constant = 0;
}
if(create_header)
{
warning("Option -[h|H] ignored with preprocess only\n");
create_header = 0;
}
if(indirect)
{
warning("Option -t ignored with preprocess only\n");
indirect = 0;
}
if(indirect_only)
{
error("Option -E and -T cannot be used together\n");
}
if(global)
{
warning("Option -g ignored with preprocess only\n");
global = 0;
}
if(create_dir)
{
warning("Option -s ignored with preprocess only\n");
create_dir = 0;
}
if(binary)
{
error("Option -E and -b cannot be used together\n");
}
if(no_preprocess)
{
error("Option -E and -N cannot be used together\n");
}
}
#if !defined(HAVE_WINE_CONSTRUCTOR)
if(auto_register)
{
warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
auto_register = 0;
}
#endif
/* Set alignment power */
a = alignment;
for(alignment_pwr = 0; alignment_pwr < 10 && a > 1; alignment_pwr++)
{
a >>= 1;
}
if(a != 1)
{
error("Alignment must be between 1 and 1024");
}
if((1 << alignment_pwr) != alignment)
{
error("Alignment must be a power of 2");
}
/* Kill io buffering when some kind of debuglevel is enabled */
if(debuglevel)
{
setbuf(stdout,0);
setbuf(stderr,0);
}
yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
(debuglevel & DEBUGLEVEL_PPTRACE) != 0,
(debuglevel & DEBUGLEVEL_PPMSG) != 0 );
/* Set the default defined stuff */
wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
wpp_add_cmdline_define("RC_INVOKED=1");
if(win32)
{
wpp_add_cmdline_define("__WIN32__=1");
wpp_add_cmdline_define("__FLAT__=1");
}
/* Check if the user set a language, else set default */
if(!currentlanguage)
currentlanguage = new_language(0, 0);
if(binary && !input_name)
{
error("Binary mode requires .res file as input\n");
}
/* Generate appropriate outfile names */
if(!output_name && !preprocess_only)
{
output_name = dup_basename(input_name, binary ? ".res" : ".rc");
strcat(output_name, create_res ? ".res" : ".s");
}
if(!header_name && !create_res)
{
header_name = dup_basename(input_name, binary ? ".res" : ".rc");
strcat(header_name, ".h");
}
/* Run the preprocessor on the input */
if(!no_preprocess && !binary)
{
/*
* Preprocess the input to a temp-file, or stdout if
* no output was given.
*/
chat("Starting preprocess");
if (!preprocess_only)
{
atexit(rm_tempfile);
ret = wpp_parse_temp( input_name, output_name, &temp_name );
}
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 );
}
if(ret)
exit(1); /* Error during preprocess */
if(preprocess_only)
exit(0);
input_name = temp_name;
}
if(!binary)
{
/* Go from .rc to .res or .s */
chat("Starting parse");
if(!(yyin = fopen(input_name, "rb")))
error("Could not open %s for input\n", input_name);
ret = yyparse();
if(input_name)
fclose(yyin);
if(ret)
{
/* Error during parse */
exit(1);
}
if(debuglevel & DEBUGLEVEL_DUMP)
dump_resources(resource_top);
/* Convert the internal lists to binary data */
resources2res(resource_top);
if(create_res)
{
chat("Writing .res-file");
write_resfile(output_name, resource_top);
}
else
{
if(create_s)
{
chat("Writing .s-file");
write_s_file(output_name, resource_top);
}
if(create_header)
{
chat("Writing .h-file");
write_h_file(header_name, resource_top);
}
}
}
else
{
/* Go from .res to .s */
chat("Reading .res-file");
resource_top = read_resfile(input_name);
if(create_s)
{
chat("Writing .s-file");
write_s_file(output_name, resource_top);
}
if(create_header)
{
chat("Writing .h-file");
write_h_file(header_name, resource_top);
}
}
return 0;
}
static void rm_tempfile(void)
{
if(temp_name)
unlink(temp_name);
}
static void segvhandler(int sig)
{
fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
fflush(stdout);
fflush(stderr);
abort();
}