jscript: Update French translation.
[wine] / tools / wrc / wrc.c
1 /*
2  * Copyright 1994 Martin von Loewis
3  * Copyright 1998 Bertho A. Stultiens (BS)
4  * Copyright 2003 Dimitrie O. Paun
5  *
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.
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
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
37
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"
46
47 #ifndef INCLUDEDIR
48 #define INCLUDEDIR "/usr/local/include/wine"
49 #endif
50
51 #ifdef WORDS_BIGENDIAN
52 #define ENDIAN  "big"
53 #else
54 #define ENDIAN  "little"
55 #endif
56
57 static const char usage[] =
58         "Usage: wrc [options...] [infile[.rc|.res]]\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         "   --verbose             Synonym for -v\n"
92         "   --verify-translations Check the status of the various translations\n"
93         "   --version             Print version and exit\n"
94         "Input is taken from stdin if no sourcefile specified.\n"
95         "Debug level 'n' is a bitmask with following meaning:\n"
96         "    * 0x01 Tell which resource is parsed (verbose mode)\n"
97         "    * 0x02 Dump internal structures\n"
98         "    * 0x04 Create a parser trace (yydebug=1)\n"
99         "    * 0x08 Preprocessor messages\n"
100         "    * 0x10 Preprocessor lex messages\n"
101         "    * 0x20 Preprocessor yacc trace\n"
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"
104         ;
105
106 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
107                         "Copyright 1998-2000 Bertho A. Stultiens\n"
108                         "          1994 Martin von Loewis\n";
109
110 /*
111  * Set if compiling in 32bit mode (default).
112  */
113 int win32 = 1;
114
115 /*
116  * debuglevel == DEBUGLEVEL_NONE        Don't bother
117  * debuglevel & DEBUGLEVEL_CHAT         Say what's done
118  * debuglevel & DEBUGLEVEL_DUMP         Dump internal structures
119  * debuglevel & DEBUGLEVEL_TRACE        Create parser trace
120  * debuglevel & DEBUGLEVEL_PPMSG        Preprocessor messages
121  * debuglevel & DEBUGLEVEL_PPLEX        Preprocessor lex trace
122  * debuglevel & DEBUGLEVEL_PPTRACE      Preprocessor yacc trace
123  */
124 int debuglevel = DEBUGLEVEL_NONE;
125
126 /*
127  * Recognize win32 keywords if set (-w 32 enforces this),
128  * otherwise set with -e option.
129  */
130 int extensions = 1;
131
132 /*
133  * Language setting for resources (-l option)
134  */
135 static language_t *defaultlanguage;
136 language_t *currentlanguage = NULL;
137
138 /*
139  * Set when extra warnings should be generated (-W option)
140  */
141 int pedantic = 0;
142
143 /*
144  * The output byte-order of resources (set with -B)
145  */
146 int byteorder = WRC_BO_NATIVE;
147
148 /*
149  * Set when _only_ to run the preprocessor (-E option)
150  */
151 int preprocess_only = 0;
152
153 /*
154  * Set when _not_ to run the preprocessor (-P cat option)
155  */
156 int no_preprocess = 0;
157
158 int check_utf8 = 1;  /* whether to check for valid utf8 */
159
160 static int verify_translations_mode;
161
162 char *output_name = NULL;       /* The name given by the -o option */
163 char *input_name = NULL;        /* The name given on the command-line */
164 char *temp_name = NULL;         /* Temporary file for preprocess pipe */
165
166 int line_number = 1;            /* The current line */
167 int char_number = 1;            /* The current char pos within the line */
168
169 char *cmdline;                  /* The entire commandline */
170 time_t now;                     /* The time of start of wrc */
171
172 int parser_debug, yy_flex_debug;
173
174 resource_t *resource_top;       /* The top of the parsed resources */
175
176 int getopt (int argc, char *const *argv, const char *optstring);
177 static void cleanup_files(void);
178 static void segvhandler(int sig);
179
180 enum long_options_values
181 {
182     LONG_OPT_NOSTDINC = 1,
183     LONG_OPT_TMPFILE,
184     LONG_OPT_NOTMPFILE,
185     LONG_OPT_PREPROCESSOR,
186     LONG_OPT_VERSION,
187     LONG_OPT_DEBUG,
188     LONG_OPT_ENDIANESS,
189     LONG_OPT_PEDANTIC,
190     LONG_OPT_VERIFY_TRANSL
191 };
192
193 static const char short_options[] =
194         "D:Ef:F:hi:I:J:l:o:O:rU:v";
195 static const struct option long_options[] = {
196         { "debug", 1, 0, LONG_OPT_DEBUG },
197         { "define", 1, 0, 'D' },
198         { "endianess", 1, 0, LONG_OPT_ENDIANESS },
199         { "help", 0, 0, 'h' },
200         { "include-dir", 1, 0, 'I' },
201         { "input", 1, 0, 'i' },
202         { "input-format", 1, 0, 'J' },
203         { "language", 1, 0, 'l' },
204         { "no-use-temp-file", 0, 0, LONG_OPT_NOTMPFILE },
205         { "nostdinc", 0, 0, LONG_OPT_NOSTDINC },
206         { "output", 1, 0, 'o' },
207         { "output-format", 1, 0, 'O' },
208         { "pedantic", 0, 0, LONG_OPT_PEDANTIC },
209         { "preprocessor", 1, 0, LONG_OPT_PREPROCESSOR },
210         { "target", 1, 0, 'F' },
211         { "undefine", 1, 0, 'U' },
212         { "use-temp-file", 0, 0, LONG_OPT_TMPFILE },
213         { "verbose", 0, 0, 'v' },
214         { "verify-translations", 0, 0, LONG_OPT_VERIFY_TRANSL },
215         { "version", 0, 0, LONG_OPT_VERSION },
216         { 0, 0, 0, 0 }
217 };
218
219 static void set_version_defines(void)
220 {
221     char *version = xstrdup( PACKAGE_VERSION );
222     char *major, *minor, *patchlevel;
223     char buffer[100];
224
225     if ((minor = strchr( version, '.' )))
226     {
227         major = version;
228         *minor++ = 0;
229         if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
230     }
231     else  /* pre 0.9 version */
232     {
233         major = NULL;
234         patchlevel = version;
235     }
236     sprintf( buffer, "__WRC__=%s", major ? major : "0" );
237     wpp_add_cmdline_define(buffer);
238     sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
239     wpp_add_cmdline_define(buffer);
240     sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
241     wpp_add_cmdline_define(buffer);
242     free( version );
243 }
244
245 /* clean things up when aborting on a signal */
246 static void exit_on_signal( int sig )
247 {
248     exit(1);  /* this will call the atexit functions */
249 }
250
251 /* load a single input file */
252 static int load_file( const char *input_name, const char *output_name )
253 {
254     int ret;
255
256     /* Run the preprocessor on the input */
257     if(!no_preprocess)
258     {
259         /*
260          * Preprocess the input to a temp-file, or stdout if
261          * no output was given.
262          */
263
264         chat("Starting preprocess\n");
265
266         if (!preprocess_only)
267         {
268             ret = wpp_parse_temp( input_name, output_name, &temp_name );
269         }
270         else if (output_name)
271         {
272             FILE *output;
273
274             if (!(output = fopen( output_name, "w" )))
275                 fatal_perror( "Could not open %s for writing", output_name );
276             ret = wpp_parse( input_name, output );
277             fclose( output );
278         }
279         else
280         {
281             ret = wpp_parse( input_name, stdout );
282         }
283
284         if (ret) return ret;
285
286         if(preprocess_only)
287         {
288             output_name = NULL;
289             exit(0);
290         }
291
292         input_name = temp_name;
293     }
294
295     /* Reset the language */
296     currentlanguage = dup_language( defaultlanguage );
297     check_utf8 = 1;
298
299     /* Go from .rc to .res */
300     chat("Starting parse\n");
301
302     if(!(parser_in = fopen(input_name, "rb")))
303         fatal_perror("Could not open %s for input", input_name);
304
305     ret = parser_parse();
306     fclose(parser_in);
307     if (temp_name)
308     {
309         unlink( temp_name );
310         temp_name = NULL;
311     }
312     free( currentlanguage );
313     return ret;
314 }
315
316
317 int main(int argc,char *argv[])
318 {
319         extern char* optarg;
320         extern int   optind;
321         int optc;
322         int opti = 0;
323         int stdinc = 1;
324         int lose = 0;
325         int nb_files = 0;
326         int i;
327         int cmdlen;
328         char **files = xmalloc( argc * sizeof(*files) );
329
330         signal(SIGSEGV, segvhandler);
331         signal( SIGTERM, exit_on_signal );
332         signal( SIGINT, exit_on_signal );
333 #ifdef SIGHUP
334         signal( SIGHUP, exit_on_signal );
335 #endif
336
337         now = time(NULL);
338
339         /* Set the default defined stuff */
340         set_version_defines();
341         wpp_add_cmdline_define("RC_INVOKED=1");
342         wpp_add_cmdline_define("__WIN32__=1");
343         wpp_add_cmdline_define("__FLAT__=1");
344         /* Microsoft RC always searches current directory */
345         wpp_add_include_path(".");
346
347         /* First rebuild the commandline to put in destination */
348         /* Could be done through env[], but not all OS-es support it */
349         cmdlen = 4; /* for "wrc " */
350         for(i = 1; i < argc; i++)
351                 cmdlen += strlen(argv[i]) + 1;
352         cmdline = xmalloc(cmdlen);
353         strcpy(cmdline, "wrc ");
354         for(i = 1; i < argc; i++)
355         {
356                 strcat(cmdline, argv[i]);
357                 if(i < argc-1)
358                         strcat(cmdline, " ");
359         }
360
361         while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
362         {
363                 switch(optc)
364                 {
365                 case LONG_OPT_NOSTDINC:
366                         stdinc = 0;
367                         break;
368                 case LONG_OPT_TMPFILE:
369                         if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
370                         break;
371                 case LONG_OPT_NOTMPFILE:
372                         if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
373                         break;
374                 case LONG_OPT_PREPROCESSOR:
375                         if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
376                         else fprintf(stderr, "-P option not yet supported, ignored.\n");
377                         break;
378                 case LONG_OPT_VERSION:
379                         printf(version_string);
380                         exit(0);
381                         break;
382                 case LONG_OPT_DEBUG:
383                         debuglevel = strtol(optarg, NULL, 0);
384                         break;
385                 case LONG_OPT_ENDIANESS:
386                         switch(optarg[0])
387                         {
388                         case 'n':
389                         case 'N':
390                                 byteorder = WRC_BO_NATIVE;
391                                 break;
392                         case 'l':
393                         case 'L':
394                                 byteorder = WRC_BO_LITTLE;
395                                 break;
396                         case 'b':
397                         case 'B':
398                                 byteorder = WRC_BO_BIG;
399                                 break;
400                         default:
401                                 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
402                                 lose++;
403                         }
404                         break;
405                 case LONG_OPT_PEDANTIC:
406                         pedantic = 1;
407                         wpp_set_pedantic(1);
408                         break;
409                 case LONG_OPT_VERIFY_TRANSL:
410                         verify_translations_mode = 1;
411                         break;
412                 case 'D':
413                         wpp_add_cmdline_define(optarg);
414                         break;
415                 case 'E':
416                         preprocess_only = 1;
417                         break;
418                 case 'F':
419                         /* ignored for compatibility with windres */
420                         break;
421                 case 'h':
422                         printf(usage);
423                         exit(0);
424                 case 'i':
425                         files[nb_files++] = optarg;
426                         break;
427                 case 'I':
428                         wpp_add_include_path(optarg);
429                         break;
430                 case 'J':
431                         if (strcmp(optarg, "rc16") == 0)  extensions = 0;
432                         else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
433                         break;
434                 case 'l':
435                         {
436                                 int lan;
437                                 lan = strtol(optarg, NULL, 0);
438                                 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
439                                         error("Language %04x is not supported\n", lan);
440                                 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
441                         }
442                         break;
443                 case 'f':
444                         if (*optarg != 'o') error("Unknown option: -f%s\n",  optarg);
445                         optarg++;
446                         /* fall through */
447                 case 'o':
448                         if (!output_name) output_name = strdup(optarg);
449                         else error("Too many output files.\n");
450                         break;
451                 case 'O':
452                         if (strcmp(optarg, "res16") == 0)
453                         {
454                                 win32 = 0;
455                                 wpp_del_define("__WIN32__");
456                                 wpp_del_define("__FLAT__");
457                         }
458                         else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
459                         break;
460                 case 'r':
461                         /* ignored for compatibility with rc */
462                         break;
463                 case 'U':
464                         wpp_del_define(optarg);
465                         break;
466                 case 'v':
467                         debuglevel = DEBUGLEVEL_CHAT;
468                         break;
469                 default:
470                         lose++;
471                         break;
472                 }
473         }
474
475         if(lose)
476         {
477                 fprintf(stderr, usage);
478                 return 1;
479         }
480
481         /* If we do need to search standard includes, add them to the path */
482         if (stdinc)
483         {
484                 wpp_add_include_path(INCLUDEDIR"/msvcrt");
485                 wpp_add_include_path(INCLUDEDIR"/windows");
486         }
487
488         /* Kill io buffering when some kind of debuglevel is enabled */
489         if(debuglevel)
490         {
491                 setbuf(stdout,0);
492                 setbuf(stderr,0);
493         }
494
495         parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
496         yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
497
498         wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
499                        (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
500                        (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
501
502         /* Check if the user set a language, else set default */
503         if(!defaultlanguage)
504                 defaultlanguage = new_language(0, 0);
505
506         atexit(cleanup_files);
507
508         while (optind < argc) files[nb_files++] = argv[optind++];
509
510         for (i = 0; i < nb_files; i++)
511         {
512             input_name = files[i];
513             if(!output_name && !preprocess_only)
514             {
515                 output_name = dup_basename(input_name, ".rc");
516                 strcat(output_name, ".res");
517             }
518             if (load_file( input_name, output_name )) exit(1);
519         }
520         /* stdin special case. NULL means "stdin" for wpp. */
521         if (nb_files == 0)
522         {
523             if(!output_name && !preprocess_only)
524                 output_name = strdup("wrc.tab.res");
525             if (load_file( NULL, output_name )) exit(1);
526         }
527
528         if(debuglevel & DEBUGLEVEL_DUMP)
529                 dump_resources(resource_top);
530
531         if(verify_translations_mode)
532         {
533                 verify_translations(resource_top);
534                 exit(0);
535         }
536
537         /* Convert the internal lists to binary data */
538         resources2res(resource_top);
539
540         chat("Writing .res-file\n");
541         write_resfile(output_name, resource_top);
542         output_name = NULL;
543
544         return 0;
545 }
546
547
548 static void cleanup_files(void)
549 {
550         if (output_name) unlink(output_name);
551         if (temp_name) unlink(temp_name);
552 }
553
554 static void segvhandler(int sig)
555 {
556         fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
557         fflush(stdout);
558         fflush(stderr);
559         abort();
560 }