Added a few more Unicode digits from Unicode version 4.1.
[wine] / tools / wrc / wrc.c
1 /*
2  * Copyright 1994 Martin von Loewis
3  * Copyrignt 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 char usage[] =
58         "Usage: wrc [options...] [infile[.rc|.res]] [outfile]\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 whats 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 language_t *currentlanguage = NULL;
136
137 /*
138  * Set when extra warnings should be generated (-W option)
139  */
140 int pedantic = 0;
141
142 /*
143  * The output byte-order of resources (set with -B)
144  */
145 int byteorder = WRC_BO_NATIVE;
146
147 /*
148  * Set when _only_ to run the preprocessor (-E option)
149  */
150 int preprocess_only = 0;
151
152 /*
153  * Set when _not_ to run the preprocessor (-P cat option)
154  */
155 int no_preprocess = 0;
156
157 static int verify_translations_mode;
158
159 char *output_name = NULL;       /* The name given by the -o option */
160 char *input_name = NULL;        /* The name given on the command-line */
161 char *temp_name = NULL;         /* Temporary file for preprocess pipe */
162
163 int line_number = 1;            /* The current line */
164 int char_number = 1;            /* The current char pos within the line */
165
166 char *cmdline;                  /* The entire commandline */
167 time_t now;                     /* The time of start of wrc */
168
169 resource_t *resource_top;       /* The top of the parsed resources */
170
171 int getopt (int argc, char *const *argv, const char *optstring);
172 static void rm_tempfile(void);
173 static void segvhandler(int sig);
174
175 static const char* short_options = 
176         "D:Ef:F:hi:I:J:l:o:O:rU:v";
177 static struct option long_options[] = {
178         { "debug", 1, 0, 6 },
179         { "define", 1, 0, 'D' },
180         { "endianess", 1, 0, 7 },
181         { "help", 0, 0, 'h' },
182         { "include-dir", 1, 0, 'I' },
183         { "input", 1, 0, 'i' },
184         { "input-format", 1, 0, 'J' },
185         { "language", 1, 0, 'l' },
186         { "no-use-temp-file", 0, 0, 3 },
187         { "nostdinc", 0, 0, 1 },
188         { "output", 1, 0, 'o' },
189         { "output-format", 1, 0, 'O' },
190         { "pendantic", 0, 0, 8 },
191         { "preprocessor", 1, 0, 4 },
192         { "target", 1, 0, 'F' },
193         { "undefine", 1, 0, 'U' },
194         { "use-temp-file", 0, 0, 2 },
195         { "verbose", 0, 0, 'v' },
196         { "verify-translations", 0, 0, 9 },
197         { "version", 0, 0, 5 },
198         { 0, 0, 0, 0 }
199 };
200
201 static void set_version_defines(void)
202 {
203     char *version = xstrdup( PACKAGE_VERSION );
204     char *major, *minor, *patchlevel;
205     char buffer[100];
206
207     if ((minor = strchr( version, '.' )))
208     {
209         major = version;
210         *minor++ = 0;
211         if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
212     }
213     else  /* pre 0.9 version */
214     {
215         major = NULL;
216         patchlevel = version;
217     }
218     sprintf( buffer, "__WRC__=%s", major ? major : "0" );
219     wpp_add_cmdline_define(buffer);
220     sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
221     wpp_add_cmdline_define(buffer);
222     sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
223     wpp_add_cmdline_define(buffer);
224     free( version );
225 }
226
227 int main(int argc,char *argv[])
228 {
229         extern char* optarg;
230         extern int   optind;
231         int optc;
232         int opti = 0;
233         int stdinc = 1;
234         int lose = 0;
235         int ret;
236         int i;
237         int cmdlen;
238
239         signal(SIGSEGV, segvhandler);
240
241         now = time(NULL);
242
243         /* Set the default defined stuff */
244         set_version_defines();
245         wpp_add_cmdline_define("RC_INVOKED=1");
246         wpp_add_cmdline_define("__WIN32__=1");
247         wpp_add_cmdline_define("__FLAT__=1");
248         /* Microsoft RC always searches current directory */
249         wpp_add_include_path(".");
250
251         /* First rebuild the commandline to put in destination */
252         /* Could be done through env[], but not all OS-es support it */
253         cmdlen = 4; /* for "wrc " */
254         for(i = 1; i < argc; i++)
255                 cmdlen += strlen(argv[i]) + 1;
256         cmdline = (char *)xmalloc(cmdlen);
257         strcpy(cmdline, "wrc ");
258         for(i = 1; i < argc; i++)
259         {
260                 strcat(cmdline, argv[i]);
261                 if(i < argc-1)
262                         strcat(cmdline, " ");
263         }
264
265         while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
266         {
267                 switch(optc)
268                 {
269                 case 1:
270                         stdinc = 0;
271                         break;
272                 case 2:
273                         if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
274                         break;
275                 case 3:
276                         if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
277                         break;
278                 case 4:
279                         if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
280                         else fprintf(stderr, "-P option not yet supported, ignored.\n");
281                         break;
282                 case 5:
283                         printf(version_string);
284                         exit(0);
285                         break;
286                 case 6:
287                         debuglevel = strtol(optarg, NULL, 0);
288                         break;
289                 case 7:
290                         switch(optarg[0])
291                         {
292                         case 'n':
293                         case 'N':
294                                 byteorder = WRC_BO_NATIVE;
295                                 break;
296                         case 'l':
297                         case 'L':
298                                 byteorder = WRC_BO_LITTLE;
299                                 break;
300                         case 'b':
301                         case 'B':
302                                 byteorder = WRC_BO_BIG;
303                                 break;
304                         default:
305                                 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
306                                 lose++;
307                         }
308                         break;
309                 case 8:
310                         pedantic = 1;
311                         wpp_set_pedantic(1);
312                         break;
313                 case 9:
314                         verify_translations_mode = 1;
315                         break;
316                 case 'D':
317                         wpp_add_cmdline_define(optarg);
318                         break;
319                 case 'E':
320                         preprocess_only = 1;
321                         break;
322                 case 'F':
323                         /* ignored for compatibility with windres */
324                         break;
325                 case 'h':
326                         printf(usage);
327                         exit(0);
328                 case 'i':
329                         if (!input_name) input_name = strdup(optarg);
330                         else error("Too many input files.\n");
331                         break;
332                 case 'I':
333                         wpp_add_include_path(optarg);
334                         break;
335                 case 'J':
336                         if (strcmp(optarg, "rc16") == 0)  extensions = 0;
337                         else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
338                         break;
339                 case 'l':
340                         {
341                                 int lan;
342                                 lan = strtol(optarg, NULL, 0);
343                                 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
344                                         error("Language %04x is not supported\n", lan);
345                                 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
346                         }
347                         break;
348                 case 'f':
349                         if (*optarg != 'o') error("Unknown option: -f%s\n",  optarg);
350                         optarg++;
351                         /* fall through */
352                 case 'o':
353                         if (!output_name) output_name = strdup(optarg);
354                         else error("Too many output files.\n");
355                         break;
356                 case 'O':
357                         if (strcmp(optarg, "res16") == 0)
358                         {
359                                 win32 = 0;
360                                 wpp_del_define("__WIN32__");
361                                 wpp_del_define("__FLAT__");
362                         }
363                         else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
364                         break;
365                 case 'r':
366                         /* ignored for compatibility with rc */
367                         break;
368                 case 'U':
369                         wpp_del_define(optarg);
370                         break;
371                 case 'v':
372                         debuglevel = DEBUGLEVEL_CHAT;
373                         break;
374                 default:
375                         lose++;
376                         break;
377                 }
378         }
379
380         if(lose)
381         {
382                 fprintf(stderr, usage);
383                 return 1;
384         }
385
386         /* If we do need to search standard includes, add them to the path */
387         if (stdinc)
388         {
389                 wpp_add_include_path(INCLUDEDIR"/msvcrt");
390                 wpp_add_include_path(INCLUDEDIR"/windows");
391         }
392         
393         /* Check for input file on command-line */
394         if(optind < argc)
395         {
396                 if (!input_name) input_name = argv[optind++];
397                 else error("Too many input files.\n");
398         }
399
400         /* Check for output file on command-line */
401         if(optind < argc)
402         {
403                 if (!output_name) output_name = argv[optind++];
404                 else error("Too many output files.\n");
405         }
406
407         /* Kill io buffering when some kind of debuglevel is enabled */
408         if(debuglevel)
409         {
410                 setbuf(stdout,0);
411                 setbuf(stderr,0);
412         }
413
414         yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
415         yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
416
417         wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
418                        (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
419                        (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
420
421         /* Check if the user set a language, else set default */
422         if(!currentlanguage)
423                 currentlanguage = new_language(0, 0);
424
425         /* Generate appropriate outfile names */
426         if(!output_name && !preprocess_only)
427         {
428                 output_name = dup_basename(input_name, ".rc");
429                 strcat(output_name, ".res");
430         }
431
432         /* Run the preprocessor on the input */
433         if(!no_preprocess)
434         {
435                 /*
436                  * Preprocess the input to a temp-file, or stdout if
437                  * no output was given.
438                  */
439
440                 chat("Starting preprocess");
441
442                 if (!preprocess_only)
443                 {
444                     atexit(rm_tempfile);
445                     ret = wpp_parse_temp( input_name, output_name, &temp_name );
446                 }
447                 else if (output_name)
448                 {
449                     FILE *output;
450
451                     if (!(output = fopen( output_name, "w" )))
452                         error( "Could not open %s for writing\n", output_name );
453                     ret = wpp_parse( input_name, output );
454                     fclose( output );
455                 }
456                 else
457                 {
458                     ret = wpp_parse( input_name, stdout );
459                 }
460
461                 if(ret)
462                         exit(1);        /* Error during preprocess */
463
464                 if(preprocess_only)
465                         exit(0);
466
467                 input_name = temp_name;
468         }
469
470         /* Go from .rc to .res */
471         chat("Starting parse");
472
473         if(!(yyin = fopen(input_name, "rb")))
474                 error("Could not open %s for input\n", input_name);
475
476         ret = yyparse();
477
478         if(input_name) fclose(yyin);
479
480         if(ret) exit(1); /* Error during parse */
481
482         if(debuglevel & DEBUGLEVEL_DUMP)
483                 dump_resources(resource_top);
484
485         if(verify_translations_mode)
486         {
487                 verify_translations(resource_top);
488                 exit(0);
489         }
490
491         /* Convert the internal lists to binary data */
492         resources2res(resource_top);
493
494         chat("Writing .res-file");
495         write_resfile(output_name, resource_top);
496
497         return 0;
498 }
499
500
501 static void rm_tempfile(void)
502 {
503         if(temp_name)
504                 unlink(temp_name);
505 }
506
507 static void segvhandler(int sig)
508 {
509         fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
510         fflush(stdout);
511         fflush(stderr);
512         abort();
513 }