Changeset 5523 for trunk/tools/wrc/wrc.c


Ignore:
Timestamp:
Apr 16, 2001, 7:12:53 PM (24 years ago)
Author:
sandervl
Message:

updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/wrc/wrc.c

    r3426 r5523  
    44 * Copyrignt 1998 Bertho A. Stultiens (BS)
    55 *
     6 * 30-Apr-2000 BS       - Integrated a new preprocessor (-E and -N)
    67 * 20-Jun-1998 BS       - Added -L option to prevent case conversion
    78 *                        of embedded filenames.
     
    3738#include <stdio.h>
    3839#include <stdlib.h>
     40#include <unistd.h>
    3941#include <string.h>
    4042#include <assert.h>
    4143#include <ctype.h>
     44#include <signal.h>
    4245
    4346#include "wrc.h"
     
    5154#include "parser.h"
    5255
    53 char usage[] = "Usage: wrc [options...] [infile[.rc|.res]]\n"
     56static char usage[] =
     57        "Usage: wrc [options...] [infile[.rc|.res]]\n"
    5458        "   -a n        Alignment of resource (win16 only, default is 4)\n"
    5559        "   -A          Auto register resources (only with gcc 2.7 and better)\n"
    56         "   -b          Create a C array from a binary .res file\n"
     60        "   -b          Create an assembly array from a binary .res file\n"
     61        "   -B x        Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
     62        "               (win32 only; default is n[ative] which equals "
     63#ifdef WORDS_BIGENDIAN
     64        "big"
     65#else
     66        "little"
     67#endif
     68        "-endian)\n"
    5769        "   -c          Add 'const' prefix to C constants\n"
    5870        "   -C cp       Set the resource's codepage to cp (default is 0)\n"
     
    6072        "   -D id[=val] Define preprocessor identifier id=val\n"
    6173        "   -e          Disable recognition of win32 keywords in 16bit compile\n"
     74        "   -E          Preprocess only\n"
    6275        "   -g          Add symbols to the global c namespace\n"
    6376        "   -h          Also generate a .h file\n"
    6477        "   -H file     Same as -h but written to file\n"
    6578        "   -I path     Set include search dir to path (multiple -I allowed)\n"
    66         "   -l lan      Set default language to lan (default is neutral {0})\n"
     79        "   -l lan      Set default language to lan (default is neutral {0, 0})\n"
    6780        "   -L          Leave case of embedded filenames as is\n"
     81        "   -m          Do not remap numerical resource IDs\n"
    6882        "   -n          Do not generate .s file\n"
     83        "   -N          Do not preprocess input\n"
    6984        "   -o file     Output to file (default is infile.[res|s|h]\n"
    7085        "   -p prefix   Give a prefix for the generated names\n"
     
    8196        "    * 0x02 Dump internal structures\n"
    8297        "    * 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"
    83101        "The -o option only applies to the final destination file, which is\n"
    84102        "in case of normal compile a .s file. You must use the '-H header.h'\n"
     
    89107
    90108char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
    91                         "Copyright 1998,1999 Bertho A. Stultiens\n"
     109                        "Copyright 1998-2000 Bertho A. Stultiens\n"
    92110                        "          1994 Martin von Loewis\n";
    93111
     
    122140 * debuglevel & DEBUGLEVEL_DUMP         Dump internal structures
    123141 * debuglevel & DEBUGLEVEL_TRACE        Create parser trace
     142 * debuglevel & DEBUGLEVEL_PPMSG        Preprocessor messages
     143 * debuglevel & DEBUGLEVEL_PPLEX        Preprocessor lex trace
     144 * debuglevel & DEBUGLEVEL_PPTRACE      Preprocessor yacc trace
    124145 */
    125146int debuglevel = DEBUGLEVEL_NONE;
     
    198219 */
    199220int leave_case = 0;
     221
     222/*
     223 * The output byte-order of resources (set with -B)
     224 */
     225int byteorder = WRC_BO_NATIVE;
     226
     227/*
     228 * Set when _only_ to run the preprocessor (-E option)
     229 */
     230int preprocess_only = 0;
     231
     232/*
     233 * Set when _not_ to run the preprocessor (-N option)
     234 */
     235int no_preprocess = 0;
     236
     237/*
     238 * Cleared when _not_ to remap resource types (-m option)
     239 */
     240int remap = 1;
    200241
    201242char *output_name;              /* The name given by the -o option */
    202243char *input_name;               /* The name given on the command-line */
    203244char *header_name;              /* The name given by the -H option */
     245char *temp_name;                /* Temporary file for preprocess pipe */
     246
     247int line_number = 1;            /* The current line */
     248int char_number = 1;            /* The current char pos within the line */
    204249
    205250char *cmdline;                  /* The entire commandline */
     251time_t now;                     /* The time of start of wrc */
    206252
    207253resource_t *resource_top;       /* The top of the parsed resources */
    208254
     255#ifdef __EMX__
     256int getopt (int argc, char **argv, const char *optstring);
     257#else
    209258int getopt (int argc, char *const *argv, const char *optstring);
     259#endif
     260
     261static void rm_tempfile(void);
     262static void segvhandler(int sig);
    210263
    211264int main(int argc,char *argv[])
     
    220273        int cmdlen;
    221274
     275        signal(SIGSEGV, segvhandler);
     276
     277        now = time(NULL);
     278
    222279        /* First rebuild the commandline to put in destination */
    223280        /* Could be done through env[], but not all OS-es support it */
     
    234291        }
    235292
    236         while((optc = getopt(argc, argv, "a:AbcC:d:D:eghH:I:l:Lno:p:rstTVw:W")) != EOF)
     293        while((optc = getopt(argc, argv, "a:AbB:cC:d:D:eEghH:I:l:LmnNo:p:rstTVw:W")) != EOF)
    237294        {
    238295                switch(optc)
     
    247304                        binary = 1;
    248305                        break;
     306                case 'B':
     307                        switch(optarg[0])
     308                        {
     309                        case 'n':
     310                        case 'N':
     311                                byteorder = WRC_BO_NATIVE;
     312                                break;
     313                        case 'l':
     314                        case 'L':
     315                                byteorder = WRC_BO_LITTLE;
     316                                break;
     317                        case 'b':
     318                        case 'B':
     319                                byteorder = WRC_BO_BIG;
     320                                break;
     321                        default:
     322                                fprintf(stderr, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
     323                                lose++;
     324                        }
     325                        break;
    249326                case 'c':
    250327                        constant = 1;
     
    261338                case 'e':
    262339                        extensions = 0;
     340                        break;
     341                case 'E':
     342                        preprocess_only = 1;
    263343                        break;
    264344                case 'g':
     
    284364                        leave_case = 1;
    285365                        break;
     366                case 'm':
     367                        remap = 0;
     368                        break;
    286369                case 'n':
    287370                        create_s = 0;
     371                        break;
     372                case 'N':
     373                        no_preprocess = 1;
    288374                        break;
    289375                case 'o':
     
    391477        }
    392478
     479        if(byteorder != WRC_BO_NATIVE)
     480        {
     481                if(binary)
     482                        error("Forced byteordering not supported for binary resources\n");
     483        }
     484
     485        if(preprocess_only)
     486        {
     487                if(constant)
     488                {
     489                        warning("Option -c ignored with preprocess only\n");
     490                        constant = 0;
     491                }
     492
     493                if(create_header)
     494                {
     495                        warning("Option -[h|H] ignored with preprocess only\n");
     496                        create_header = 0;
     497                }
     498
     499                if(indirect)
     500                {
     501                        warning("Option -l ignored with preprocess only\n");
     502                        indirect = 0;
     503                }
     504
     505                if(indirect_only)
     506                {
     507                        error("Option -E and -L cannot be used together\n");
     508                }
     509
     510                if(global)
     511                {
     512                        warning("Option -g ignored with preprocess only\n");
     513                        global = 0;
     514                }
     515
     516                if(create_dir)
     517                {
     518                        warning("Option -s ignored with preprocess only\n");
     519                        create_dir = 0;
     520                }
     521
     522                if(binary)
     523                {
     524                        error("Option -E and -b cannot be used together\n");
     525                }
     526
     527                if(no_preprocess)
     528                {
     529                        error("Option -E and -N cannot be used together\n");
     530                }
     531        }
     532
     533#if !defined(HAVE_WINE_CONSTRUCTOR)
     534        if(auto_register)
     535        {
     536                warning("Autoregister code non-operable (HAVE_WINE_CONSTRUCTOR not defined)");
     537                auto_register = 0;
     538        }
     539#endif
     540
    393541        /* Set alignment power */
    394542        a = alignment;
     
    414562
    415563        yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
     564        yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
     565        ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
     566        pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;
    416567
    417568        /* Set the default defined stuff */
     569        add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
     570        add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
     571        add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
     572        add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
     573
    418574        add_cmdline_define("RC_INVOKED=1");
    419         add_cmdline_define("__WRC__=1");
     575
    420576        if(win32)
    421577        {
     
    423579                add_cmdline_define("__FLAT__=1");
    424580        }
     581
     582        add_special_define("__FILE__");
     583        add_special_define("__LINE__");
     584        add_special_define("__DATE__");
     585        add_special_define("__TIME__");
    425586
    426587        /* Check if the user set a language, else set default */
     
    432593        {
    433594                input_name = argv[optind];
    434                 yyin = fopen(input_name, "rb");
    435                 if(!yyin)
    436                 {
    437                         error("Could not open %s\n", input_name);
    438                 }
    439         }
    440         else
    441         {
    442                 yyin = stdin;
    443595        }
    444596
    445597        if(binary && !input_name)
    446598        {
    447                 error("Binary mode requires .res file as input");
    448         }
    449 
    450         if(!output_name)
     599                error("Binary mode requires .res file as input\n");
     600        }
     601
     602        /* Generate appropriate outfile names */
     603        if(!output_name && !preprocess_only)
    451604        {
    452605                output_name = dup_basename(input_name, binary ? ".res" : ".rc");
     
    460613        }
    461614
     615        /* Run the preprocessor on the input */
     616        if(!no_preprocess && !binary)
     617        {
     618                char *real_name;
     619                /*
     620                 * Preprocess the input to a temp-file, or stdout if
     621                 * no output was given.
     622                 */
     623
     624                chat("Starting preprocess");
     625
     626                if(preprocess_only && !output_name)
     627                {
     628                        ppout = stdout;
     629                }
     630                else if(preprocess_only && output_name)
     631                {
     632                        if(!(ppout = fopen(output_name, "wb")))
     633                                error("Could not open %s for writing\n", output_name);
     634                }
     635                else
     636                {
     637                        if(!(temp_name = tmpnam(NULL)))
     638                                error("Could nor generate a temp-name\n");
     639                        temp_name = xstrdup(temp_name);
     640                        if(!(ppout = fopen(temp_name, "wb")))
     641                                error("Could not create a temp-file\n");
     642
     643                        atexit(rm_tempfile);
     644                }
     645
     646                real_name = input_name; /* Because it gets overwritten */
     647
     648                if(!input_name)
     649                        ppin = stdin;
     650                else
     651                {
     652                        if(!(ppin = fopen(input_name, "rb")))
     653                                error("Could not open %s\n", input_name);
     654                }
     655
     656                fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
     657
     658                ret = ppparse();
     659
     660                input_name = real_name;
     661
     662                if(input_name)
     663                        fclose(ppin);
     664
     665                fclose(ppout);
     666
     667                input_name = temp_name;
     668
     669                if(ret)
     670                        exit(1);        /* Error during preprocess */
     671
     672                if(preprocess_only)
     673                        exit(0);
     674        }
     675
    462676        if(!binary)
    463677        {
    464678                /* Go from .rc to .res or .s */
    465679                chat("Starting parse");
     680
     681                if(!(yyin = fopen(input_name, "rb")))
     682                        error("Could not open %s for input\n", input_name);
     683
    466684                ret = yyparse();
    467685
     
    522740
    523741
    524 
    525 
     742static void rm_tempfile(void)
     743{
     744        if(temp_name)
     745                unlink(temp_name);
     746}
     747
     748static void segvhandler(int sig)
     749{
     750        fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
     751        fflush(stdout);
     752        fflush(stderr);
     753        abort();
     754}
     755
Note: See TracChangeset for help on using the changeset viewer.