Remove the no longer needed -m option, ignore -r for compatibility
[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 "../wpp/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            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         "   --version           Print version and exit\n"
92         "Input is taken from stdin if no sourcefile specified.\n"
93         "Debug level 'n' is a bitmask with following meaning:\n"
94         "    * 0x01 Tell which resource is parsed (verbose mode)\n"
95         "    * 0x02 Dump internal structures\n"
96         "    * 0x04 Create a parser trace (yydebug=1)\n"
97         "    * 0x08 Preprocessor messages\n"
98         "    * 0x10 Preprocessor lex messages\n"
99         "    * 0x20 Preprocessor yacc trace\n"
100         "If no input filename is given and the output name is not overridden\n"
101         "with -o, then the output is written to \"wrc.tab.res\"\n"
102         ;
103
104 char version_string[] = "Wine Resource Compiler Version " WRC_FULLVERSION "\n"
105                         "Copyright 1998-2000 Bertho A. Stultiens\n"
106                         "          1994 Martin von Loewis\n";
107
108 /*
109  * External functions
110  */
111 void write_resfile(char *outname, resource_t *top);
112
113 /*
114  * Set if compiling in 32bit mode (default).
115  */
116 int win32 = 1;
117
118 /*
119  * debuglevel == DEBUGLEVEL_NONE        Don't bother
120  * debuglevel & DEBUGLEVEL_CHAT         Say whats done
121  * debuglevel & DEBUGLEVEL_DUMP         Dump internal structures
122  * debuglevel & DEBUGLEVEL_TRACE        Create parser trace
123  * debuglevel & DEBUGLEVEL_PPMSG        Preprocessor messages
124  * debuglevel & DEBUGLEVEL_PPLEX        Preprocessor lex trace
125  * debuglevel & DEBUGLEVEL_PPTRACE      Preprocessor yacc trace
126  */
127 int debuglevel = DEBUGLEVEL_NONE;
128
129 /*
130  * Recognize win32 keywords if set (-w 32 enforces this),
131  * otherwise set with -e option.
132  */
133 int extensions = 1;
134
135 /*
136  * Language setting for resources (-l option)
137  */
138 language_t *currentlanguage = NULL;
139
140 /*
141  * Set when extra warnings should be generated (-W option)
142  */
143 int pedantic = 0;
144
145 /*
146  * The output byte-order of resources (set with -B)
147  */
148 int byteorder = WRC_BO_NATIVE;
149
150 /*
151  * Set when _only_ to run the preprocessor (-E option)
152  */
153 int preprocess_only = 0;
154
155 /*
156  * Set when _not_ to run the preprocessor (-P cat option)
157  */
158 int no_preprocess = 0;
159
160 char *output_name;              /* The name given by the -o option */
161 char *input_name;               /* The name given on the command-line */
162 char *temp_name;                /* Temporary file for preprocess pipe */
163
164 int line_number = 1;            /* The current line */
165 int char_number = 1;            /* The current char pos within the line */
166
167 char *cmdline;                  /* The entire commandline */
168 time_t now;                     /* The time of start of wrc */
169
170 resource_t *resource_top;       /* The top of the parsed resources */
171
172 int getopt (int argc, char *const *argv, const char *optstring);
173 static void rm_tempfile(void);
174 static void segvhandler(int sig);
175
176 static const char* short_options = 
177         "D:EF:hi:I:J:l:o:O:rU:v";
178 static struct option long_options[] = {
179         { "debug", 1, 0, 6 },
180         { "define", 1, 0, 'D' },
181         { "endianess", 1, 0, 7 },
182         { "help", 0, 0, 'h' },
183         { "include-dir", 1, 0, 'I' },
184         { "input", 1, 0, 'i' },
185         { "input-format", 1, 0, 'J' },
186         { "language", 1, 0, 'l' },
187         { "no-use-temp-file", 0, 0, 3 },
188         { "nostdinc", 0, 0, 1 },
189         { "output", 1, 0, 'o' },
190         { "output-format", 1, 0, 'O' },
191         { "pendantic", 0, 0, 8 },
192         { "preprocessor", 1, 0, 4 },
193         { "target", 1, 0, 'F' },
194         { "undefine", 1, 0, 'U' },
195         { "use-temp-file", 0, 0, 2 },
196         { "version", 0, 0, 5 },
197         { 0, 0, 0, 0 }
198 };
199
200 int main(int argc,char *argv[])
201 {
202         extern char* optarg;
203         extern int   optind;
204         int optc;
205         int opti = 0;
206         int stdinc = 1;
207         int lose = 0;
208         int ret;
209         int i;
210         int cmdlen;
211
212         signal(SIGSEGV, segvhandler);
213
214         now = time(NULL);
215
216         /* Set the default defined stuff */
217         wpp_add_cmdline_define("__WRC__=" WRC_EXP_STRINGIZE(WRC_MAJOR_VERSION));
218         wpp_add_cmdline_define("__WRC_MINOR__=" WRC_EXP_STRINGIZE(WRC_MINOR_VERSION));
219         wpp_add_cmdline_define("__WRC_MICRO__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
220         wpp_add_cmdline_define("__WRC_PATCH__=" WRC_EXP_STRINGIZE(WRC_MICRO_VERSION));
221
222         wpp_add_cmdline_define("RC_INVOKED=1");
223         wpp_add_cmdline_define("__WIN32__=1");
224         wpp_add_cmdline_define("__FLAT__=1");
225
226         /* First rebuild the commandline to put in destination */
227         /* Could be done through env[], but not all OS-es support it */
228         cmdlen = 4; /* for "wrc " */
229         for(i = 1; i < argc; i++)
230                 cmdlen += strlen(argv[i]) + 1;
231         cmdline = (char *)xmalloc(cmdlen);
232         strcpy(cmdline, "wrc ");
233         for(i = 1; i < argc; i++)
234         {
235                 strcat(cmdline, argv[i]);
236                 if(i < argc-1)
237                         strcat(cmdline, " ");
238         }
239
240         while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
241         {
242                 switch(optc)
243                 {
244                 case 1:
245                         stdinc = 0;
246                         break;
247                 case 2:
248                         if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
249                         break;
250                 case 3:
251                         if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
252                         break;
253                 case 4:
254                         if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
255                         else fprintf(stderr, "-P option not yet supported, ignored.\n");
256                         break;
257                 case 5:
258                         printf(version_string);
259                         exit(0);
260                         break;
261                 case 6:
262                         debuglevel = strtol(optarg, NULL, 0);
263                         break;
264                 case 7:
265                         switch(optarg[0])
266                         {
267                         case 'n':
268                         case 'N':
269                                 byteorder = WRC_BO_NATIVE;
270                                 break;
271                         case 'l':
272                         case 'L':
273                                 byteorder = WRC_BO_LITTLE;
274                                 break;
275                         case 'b':
276                         case 'B':
277                                 byteorder = WRC_BO_BIG;
278                                 break;
279                         default:
280                                 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
281                                 lose++;
282                         }
283                         break;
284                 case 8:
285                         pedantic = 1;
286                         wpp_set_pedantic(1);
287                         break;
288                 case 'D':
289                         wpp_add_cmdline_define(optarg);
290                         break;
291                 case 'E':
292                         preprocess_only = 1;
293                         break;
294                 case 'F':
295                         /* ignored for compatibility with windres */
296                         break;
297                 case 'h':
298                         printf(usage);
299                         exit(0);
300                 case 'i':
301                         if (!input_name) input_name = strdup(optarg);
302                         else error("Too many input files.\n");
303                         break;
304                 case 'I':
305                         wpp_add_include_path(optarg);
306                         break;
307                 case 'J':
308                         if (strcmp(optarg, "rc16") == 0)  extensions = 0;
309                         else if (strcmp(optarg, "rc")) error("Output format %s not supported.", optarg);
310                         break;
311                 case 'l':
312                         {
313                                 int lan;
314                                 lan = strtol(optarg, NULL, 0);
315                                 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
316                                         error("Language %04x is not supported",lan);
317                                 currentlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
318                         }
319                         break;
320                 case 'o':
321                         if (!output_name) output_name = strdup(optarg);
322                         else error("Too many output files.\n");
323                         break;
324                 case 'O':
325                         if (strcmp(optarg, "res16") == 0)
326                         {
327                                 win32 = 0;
328                                 wpp_del_define("__WIN32__");
329                                 wpp_del_define("__FLAT__");
330                         }
331                         else if (strcmp(optarg, "res")) warning("Output format %s not supported.", optarg);
332                         break;
333                 case 'r':
334                         /* ignored for compatibility with rc */
335                         break;
336                 case 'U':
337                         wpp_del_define(optarg);
338                         break;
339                 case 'v':
340                         debuglevel = DEBUGLEVEL_CHAT;
341                         break;
342                 default:
343                         lose++;
344                         break;
345                 }
346         }
347
348         if(lose)
349         {
350                 fprintf(stderr, usage);
351                 return 1;
352         }
353
354         /* If we do need to search standard includes, add them to the path */
355         if (stdinc)
356         {
357                 wpp_add_include_path(INCLUDEDIR"/msvcrt");
358                 wpp_add_include_path(INCLUDEDIR"/windows");
359         }
360         
361         /* Check for input file on command-line */
362         if(optind < argc)
363         {
364                 if (!input_name) input_name = argv[optind++];
365                 else error("Too many input files.\n");
366         }
367
368         /* Check for output file on command-line */
369         if(optind < argc)
370         {
371                 if (!output_name) output_name = argv[optind++];
372                 else error("Too many output files.\n");
373         }
374
375         /* Kill io buffering when some kind of debuglevel is enabled */
376         if(debuglevel)
377         {
378                 setbuf(stdout,0);
379                 setbuf(stderr,0);
380         }
381
382         yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
383         yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
384
385         wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
386                        (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
387                        (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
388
389         /* Check if the user set a language, else set default */
390         if(!currentlanguage)
391                 currentlanguage = new_language(0, 0);
392
393         /* Generate appropriate outfile names */
394         if(!output_name && !preprocess_only)
395         {
396                 output_name = dup_basename(input_name, ".rc");
397                 strcat(output_name, ".res");
398         }
399
400         /* Run the preprocessor on the input */
401         if(!no_preprocess)
402         {
403                 /*
404                  * Preprocess the input to a temp-file, or stdout if
405                  * no output was given.
406                  */
407
408                 chat("Starting preprocess");
409
410                 if (!preprocess_only)
411                 {
412                     atexit(rm_tempfile);
413                     ret = wpp_parse_temp( input_name, output_name, &temp_name );
414                 }
415                 else if (output_name)
416                 {
417                     FILE *output;
418
419                     if (!(output = fopen( output_name, "w" )))
420                         error( "Could not open %s for writing\n", output_name );
421                     ret = wpp_parse( input_name, output );
422                     fclose( output );
423                 }
424                 else
425                 {
426                     ret = wpp_parse( input_name, stdout );
427                 }
428
429                 if(ret)
430                         exit(1);        /* Error during preprocess */
431
432                 if(preprocess_only)
433                         exit(0);
434
435                 input_name = temp_name;
436         }
437
438         /* Go from .rc to .res */
439         chat("Starting parse");
440
441         if(!(yyin = fopen(input_name, "rb")))
442                 error("Could not open %s for input\n", input_name);
443
444         ret = yyparse();
445
446         if(input_name) fclose(yyin);
447
448         if(ret) exit(1); /* Error during parse */
449
450         if(debuglevel & DEBUGLEVEL_DUMP)
451                 dump_resources(resource_top);
452
453         /* Convert the internal lists to binary data */
454         resources2res(resource_top);
455
456         chat("Writing .res-file");
457         write_resfile(output_name, resource_top);
458
459         return 0;
460 }
461
462
463 static void rm_tempfile(void)
464 {
465         if(temp_name)
466                 unlink(temp_name);
467 }
468
469 static void segvhandler(int sig)
470 {
471         fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
472         fflush(stdout);
473         fflush(stderr);
474         abort();
475 }