po: Update Norwegian translation.
[wine.git] / tools / wrc / wrc.c
blobea8bfe07a7cb5715dbb1f79acdac735502bf96a9
1 /*
2 * Copyright 1994 Martin von Loewis
3 * Copyright 1998 Bertho A. Stultiens (BS)
4 * Copyright 2003 Dimitrie O. Paun
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
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
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"
47 #ifdef WORDS_BIGENDIAN
48 #define ENDIAN "big"
49 #else
50 #define ENDIAN "little"
51 #endif
53 static const char usage[] =
54 "Usage: wrc [options...] [infile[.rc|.res]]\n"
55 " -b, --target=TARGET Specify target CPU and platform when cross-compiling\n"
56 " -D, --define id[=val] Define preprocessor identifier id=val\n"
57 " --debug=nn Set debug level to 'nn'\n"
58 " -E Preprocess only\n"
59 " --endianness=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
60 " (win32 only; default is " ENDIAN "-endian)\n"
61 " -F TARGET Synonym for -b for compatibility with windres\n"
62 " -fo FILE Synonym for -o for compatibility with windres\n"
63 " -h, --help Prints this summary\n"
64 " -i, --input=FILE The name of the input file\n"
65 " -I, --include-dir=PATH Set include search dir to path (multiple -I allowed)\n"
66 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
67 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
68 " -m16, -m32, -m64 Build for 16-bit, 32-bit resp. 64-bit platforms\n"
69 " --no-use-temp-file Ignored for compatibility with windres\n"
70 " --nostdinc Disables searching the standard include path\n"
71 " -o, --output=FILE Output to file (default is infile.res)\n"
72 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
73 " --pedantic Enable pedantic warnings\n"
74 " --po-dir=DIR Directory containing po files for translations\n"
75 " --preprocessor Specifies the preprocessor to use, including arguments\n"
76 " -r Ignored for compatibility with rc\n"
77 " -U, --undefine id Undefine preprocessor identifier id\n"
78 " --use-temp-file Ignored for compatibility with windres\n"
79 " -v, --verbose Enable verbose mode\n"
80 " --verify-translations Check the status of the various translations\n"
81 " --version Print version and exit\n"
82 "Input is taken from stdin if no sourcefile specified.\n"
83 "Debug level 'n' is a bitmask with following meaning:\n"
84 " * 0x01 Tell which resource is parsed (verbose mode)\n"
85 " * 0x02 Dump internal structures\n"
86 " * 0x04 Create a parser trace (yydebug=1)\n"
87 " * 0x08 Preprocessor messages\n"
88 " * 0x10 Preprocessor lex messages\n"
89 " * 0x20 Preprocessor yacc trace\n"
90 "If no input filename is given and the output name is not overridden\n"
91 "with -o, then the output is written to \"wrc.tab.res\"\n"
94 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
95 "Copyright 1998-2000 Bertho A. Stultiens\n"
96 " 1994 Martin von Loewis\n";
99 * Set if compiling in 32bit mode (default).
101 int win32 = 1;
104 * debuglevel == DEBUGLEVEL_NONE Don't bother
105 * debuglevel & DEBUGLEVEL_CHAT Say what's done
106 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
107 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
108 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
109 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
110 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
112 int debuglevel = DEBUGLEVEL_NONE;
115 * Recognize win32 keywords if set (-w 32 enforces this),
116 * otherwise set with -e option.
118 int extensions = 1;
121 * Language setting for resources (-l option)
123 static language_t *defaultlanguage;
124 language_t *currentlanguage = NULL;
127 * Set when extra warnings should be generated (-W option)
129 int pedantic = 0;
132 * The output byte-order of resources (set with -B)
134 int byteorder = WRC_BO_NATIVE;
137 * Set when _only_ to run the preprocessor (-E option)
139 int preprocess_only = 0;
142 * Set when _not_ to run the preprocessor (-P cat option)
144 int no_preprocess = 0;
146 int check_utf8 = 1; /* whether to check for valid utf8 */
148 static int pointer_size = sizeof(void *);
150 static int verify_translations_mode;
152 static char *output_name; /* The name given by the -o option */
153 char *input_name = NULL; /* The name given on the command-line */
154 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
156 int line_number = 1; /* The current line */
157 int char_number = 1; /* The current char pos within the line */
159 char *cmdline; /* The entire commandline */
161 int parser_debug, yy_flex_debug;
163 resource_t *resource_top; /* The top of the parsed resources */
165 static void cleanup_files(void);
166 static void segvhandler(int sig);
168 enum long_options_values
170 LONG_OPT_NOSTDINC = 1,
171 LONG_OPT_TMPFILE,
172 LONG_OPT_NOTMPFILE,
173 LONG_OPT_PO_DIR,
174 LONG_OPT_PREPROCESSOR,
175 LONG_OPT_VERSION,
176 LONG_OPT_DEBUG,
177 LONG_OPT_ENDIANNESS,
178 LONG_OPT_PEDANTIC,
179 LONG_OPT_VERIFY_TRANSL
182 static const char short_options[] =
183 "b:D:Ef:F:hi:I:J:l:m:o:O:rU:v";
184 static const struct option long_options[] = {
185 { "debug", 1, NULL, LONG_OPT_DEBUG },
186 { "define", 1, NULL, 'D' },
187 { "endianness", 1, NULL, LONG_OPT_ENDIANNESS },
188 { "help", 0, NULL, 'h' },
189 { "include-dir", 1, NULL, 'I' },
190 { "input", 1, NULL, 'i' },
191 { "input-format", 1, NULL, 'J' },
192 { "language", 1, NULL, 'l' },
193 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
194 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
195 { "output", 1, NULL, 'o' },
196 { "output-format", 1, NULL, 'O' },
197 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
198 { "po-dir", 1, NULL, LONG_OPT_PO_DIR },
199 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
200 { "target", 1, NULL, 'F' },
201 { "undefine", 1, NULL, 'U' },
202 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
203 { "verbose", 0, NULL, 'v' },
204 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
205 { "version", 0, NULL, LONG_OPT_VERSION },
206 { NULL, 0, NULL, 0 }
209 static void set_version_defines(void)
211 char *version = xstrdup( PACKAGE_VERSION );
212 char *major, *minor, *patchlevel;
213 char buffer[100];
215 if ((minor = strchr( version, '.' )))
217 major = version;
218 *minor++ = 0;
219 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
221 else /* pre 0.9 version */
223 major = NULL;
224 patchlevel = version;
226 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
227 wpp_add_cmdline_define(buffer);
228 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
229 wpp_add_cmdline_define(buffer);
230 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
231 wpp_add_cmdline_define(buffer);
232 free( version );
235 /* clean things up when aborting on a signal */
236 static void exit_on_signal( int sig )
238 exit(1); /* this will call the atexit functions */
241 /* load a single input file */
242 static int load_file( const char *input_name, const char *output_name )
244 int ret;
246 /* Run the preprocessor on the input */
247 if(!no_preprocess)
249 FILE *output;
250 int ret, fd;
251 char *name;
254 * Preprocess the input to a temp-file, or stdout if
255 * no output was given.
258 if (preprocess_only)
260 if (output_name)
262 if (!(output = fopen( output_name, "w" )))
263 fatal_perror( "Could not open %s for writing", output_name );
264 ret = wpp_parse( input_name, output );
265 fclose( output );
267 else ret = wpp_parse( input_name, stdout );
269 if (ret) return ret;
270 output_name = NULL;
271 exit(0);
274 if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
275 else name = xstrdup( "wrc.XXXXXX" );
277 if ((fd = mkstemps( name, 0 )) == -1)
278 error("Could not generate a temp name from %s\n", name);
280 temp_name = name;
281 if (!(output = fdopen(fd, "wt")))
282 error("Could not open fd %s for writing\n", name);
284 ret = wpp_parse( input_name, output );
285 fclose( output );
286 if (ret) return ret;
287 input_name = name;
290 /* Reset the language */
291 currentlanguage = dup_language( defaultlanguage );
292 check_utf8 = 1;
294 /* Go from .rc to .res */
295 chat("Starting parse\n");
297 if(!(parser_in = fopen(input_name, "rb")))
298 fatal_perror("Could not open %s for input", input_name);
300 ret = parser_parse();
301 fclose(parser_in);
302 parser_lex_destroy();
303 if (temp_name)
305 unlink( temp_name );
306 temp_name = NULL;
308 free( currentlanguage );
309 return ret;
312 static void set_target( const char *target )
314 char *p, *cpu = xstrdup( target );
316 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
317 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
318 *p = 0;
319 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) ||
320 !strcmp( cpu, "ia64" ) || !strcmp( cpu, "aarch64" ))
321 pointer_size = 8;
322 else
323 pointer_size = 4;
324 free( cpu );
327 int main(int argc,char *argv[])
329 int optc;
330 int opti = 0;
331 int stdinc = 1;
332 int lose = 0;
333 int nb_files = 0;
334 int i;
335 int cmdlen;
336 int po_mode = 0;
337 char *po_dir = NULL;
338 char **files = xmalloc( argc * sizeof(*files) );
340 signal(SIGSEGV, segvhandler);
341 signal( SIGTERM, exit_on_signal );
342 signal( SIGINT, exit_on_signal );
343 #ifdef SIGHUP
344 signal( SIGHUP, exit_on_signal );
345 #endif
347 /* Set the default defined stuff */
348 set_version_defines();
349 wpp_add_cmdline_define("RC_INVOKED=1");
350 /* Microsoft RC always searches current directory */
351 wpp_add_include_path(".");
353 /* First rebuild the commandline to put in destination */
354 /* Could be done through env[], but not all OS-es support it */
355 cmdlen = 4; /* for "wrc " */
356 for(i = 1; i < argc; i++)
357 cmdlen += strlen(argv[i]) + 1;
358 cmdline = xmalloc(cmdlen);
359 strcpy(cmdline, "wrc ");
360 for(i = 1; i < argc; i++)
362 strcat(cmdline, argv[i]);
363 if(i < argc-1)
364 strcat(cmdline, " ");
367 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
369 switch(optc)
371 case LONG_OPT_NOSTDINC:
372 stdinc = 0;
373 break;
374 case LONG_OPT_TMPFILE:
375 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
376 break;
377 case LONG_OPT_NOTMPFILE:
378 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
379 break;
380 case LONG_OPT_PO_DIR:
381 po_dir = xstrdup( optarg );
382 break;
383 case LONG_OPT_PREPROCESSOR:
384 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
385 else fprintf(stderr, "-P option not yet supported, ignored.\n");
386 break;
387 case LONG_OPT_VERSION:
388 printf(version_string);
389 exit(0);
390 break;
391 case LONG_OPT_DEBUG:
392 debuglevel = strtol(optarg, NULL, 0);
393 break;
394 case LONG_OPT_ENDIANNESS:
395 switch(optarg[0])
397 case 'n':
398 case 'N':
399 byteorder = WRC_BO_NATIVE;
400 break;
401 case 'l':
402 case 'L':
403 byteorder = WRC_BO_LITTLE;
404 break;
405 case 'b':
406 case 'B':
407 byteorder = WRC_BO_BIG;
408 break;
409 default:
410 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
411 lose++;
413 break;
414 case LONG_OPT_PEDANTIC:
415 pedantic = 1;
416 wpp_set_pedantic(1);
417 break;
418 case LONG_OPT_VERIFY_TRANSL:
419 verify_translations_mode = 1;
420 break;
421 case 'D':
422 wpp_add_cmdline_define(optarg);
423 break;
424 case 'E':
425 preprocess_only = 1;
426 break;
427 case 'b':
428 case 'F':
429 set_target( optarg );
430 break;
431 case 'h':
432 printf(usage);
433 exit(0);
434 case 'i':
435 files[nb_files++] = optarg;
436 break;
437 case 'I':
438 wpp_add_include_path(optarg);
439 break;
440 case 'J':
441 if (strcmp(optarg, "rc16") == 0) extensions = 0;
442 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
443 break;
444 case 'l':
446 int lan;
447 lan = strtol(optarg, NULL, 0);
448 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
449 error("Language %04x is not supported\n", lan);
450 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
452 break;
453 case 'm':
454 if (!strcmp( optarg, "16" )) win32 = 0;
455 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
456 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
457 break;
458 case 'f':
459 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
460 optarg++;
461 /* fall through */
462 case 'o':
463 if (!output_name) output_name = strdup(optarg);
464 else error("Too many output files.\n");
465 break;
466 case 'O':
467 if (strcmp(optarg, "po") == 0) po_mode = 1;
468 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
469 else if (strcmp(optarg, "res16") == 0) win32 = 0;
470 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
471 break;
472 case 'r':
473 /* ignored for compatibility with rc */
474 break;
475 case 'U':
476 wpp_del_define(optarg);
477 break;
478 case 'v':
479 debuglevel = DEBUGLEVEL_CHAT;
480 break;
481 default:
482 lose++;
483 break;
487 if(lose)
489 fprintf(stderr, usage);
490 return 1;
493 if (win32)
495 wpp_add_cmdline_define("_WIN32=1");
496 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
499 /* If we do need to search standard includes, add them to the path */
500 if (stdinc)
502 wpp_add_include_path(INCLUDEDIR"/msvcrt");
503 wpp_add_include_path(INCLUDEDIR"/windows");
506 /* Kill io buffering when some kind of debuglevel is enabled */
507 if(debuglevel)
509 setbuf(stdout, NULL);
510 setbuf(stderr, NULL);
513 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
514 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
516 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
517 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
518 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
520 /* Check if the user set a language, else set default */
521 if(!defaultlanguage)
522 defaultlanguage = new_language(0, 0);
524 atexit(cleanup_files);
526 while (optind < argc) files[nb_files++] = argv[optind++];
528 for (i = 0; i < nb_files; i++)
530 input_name = files[i];
531 if (load_file( input_name, output_name )) exit(1);
533 /* stdin special case. NULL means "stdin" for wpp. */
534 if (nb_files == 0 && load_file( NULL, output_name )) exit(1);
536 if(debuglevel & DEBUGLEVEL_DUMP)
537 dump_resources(resource_top);
539 if(verify_translations_mode)
541 verify_translations(resource_top);
542 exit(0);
544 if (po_mode)
546 if (po_mode == 2) /* pot file */
548 if (!output_name)
550 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
551 strcat( output_name, ".pot" );
553 write_pot_file( output_name );
555 else write_po_files( output_name );
556 output_name = NULL;
557 exit(0);
559 add_translations( po_dir );
561 /* Convert the internal lists to binary data */
562 resources2res(resource_top);
564 chat("Writing .res-file\n");
565 if (!output_name)
567 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
568 strcat(output_name, ".res");
570 write_resfile(output_name, resource_top);
571 output_name = NULL;
573 return 0;
577 static void cleanup_files(void)
579 if (output_name) unlink(output_name);
580 if (temp_name) unlink(temp_name);
583 static void segvhandler(int sig)
585 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
586 fflush(stdout);
587 fflush(stderr);
588 abort();