winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / tools / wrc / wrc.c
blobfec05148adc0091e07b9001220302432cad8a4f1
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 "dumpres.h"
41 #include "genres.h"
42 #include "newstruc.h"
43 #include "parser.h"
44 #include "wine/wpp.h"
46 #ifdef WORDS_BIGENDIAN
47 #define ENDIAN "big"
48 #else
49 #define ENDIAN "little"
50 #endif
52 static const char usage[] =
53 "Usage: wrc [options...] [infile[.rc|.res]]\n"
54 " -b, --target=TARGET Specify target CPU and platform when cross-compiling\n"
55 " -D, --define id[=val] Define preprocessor identifier id=val\n"
56 " --debug=nn Set debug level to 'nn'\n"
57 " -E Preprocess only\n"
58 " --endianness=e Set output byte-order e={n[ative], l[ittle], b[ig]}\n"
59 " (win32 only; default is " ENDIAN "-endian)\n"
60 " -F TARGET Synonym for -b for compatibility with windres\n"
61 " -fo FILE Synonym for -o for compatibility with windres\n"
62 " -h, --help Prints this summary\n"
63 " -i, --input=FILE The name of the input file\n"
64 " -I, --include-dir=PATH Set include search dir to path (multiple -I allowed)\n"
65 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
66 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
67 " -m16, -m32, -m64 Build for 16-bit, 32-bit resp. 64-bit platforms\n"
68 " --nls-dir=DIR Directory containing the NLS codepage mappings\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 " --sysroot=DIR Prefix include paths with DIR\n"
78 " -U, --undefine id Undefine preprocessor identifier id\n"
79 " --use-temp-file Ignored for compatibility with windres\n"
80 " -v, --verbose Enable verbose mode\n"
81 " --verify-translations Check the status of the various translations\n"
82 " --version Print version and exit\n"
83 "Input is taken from stdin if no sourcefile specified.\n"
84 "Debug level 'n' is a bitmask with following meaning:\n"
85 " * 0x01 Tell which resource is parsed (verbose mode)\n"
86 " * 0x02 Dump internal structures\n"
87 " * 0x04 Create a parser trace (yydebug=1)\n"
88 " * 0x08 Preprocessor messages\n"
89 " * 0x10 Preprocessor lex messages\n"
90 " * 0x20 Preprocessor yacc trace\n"
91 "If no input filename is given and the output name is not overridden\n"
92 "with -o, then the output is written to \"wrc.tab.res\"\n"
95 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
96 "Copyright 1998-2000 Bertho A. Stultiens\n"
97 " 1994 Martin von Loewis\n";
100 * Set if compiling in 32bit mode (default).
102 int win32 = 1;
105 * debuglevel == DEBUGLEVEL_NONE Don't bother
106 * debuglevel & DEBUGLEVEL_CHAT Say what's done
107 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
108 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
109 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
110 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
111 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
113 int debuglevel = DEBUGLEVEL_NONE;
116 * Recognize win32 keywords if set (-w 32 enforces this),
117 * otherwise set with -e option.
119 int extensions = 1;
122 * Language setting for resources (-l option)
124 static language_t *defaultlanguage;
125 language_t *currentlanguage = NULL;
128 * Set when extra warnings should be generated (-W option)
130 int pedantic = 0;
133 * The output byte-order of resources (set with -B)
135 int byteorder = WRC_BO_NATIVE;
138 * Set when _only_ to run the preprocessor (-E option)
140 int preprocess_only = 0;
143 * Set when _not_ to run the preprocessor (-P cat option)
145 int no_preprocess = 0;
147 int utf8_input = 0;
149 int check_utf8 = 1; /* whether to check for valid utf8 */
151 static int pointer_size = sizeof(void *);
153 static int verify_translations_mode;
155 static char *output_name; /* The name given by the -o option */
156 char *input_name = NULL; /* The name given on the command-line */
157 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
159 static const char *includedir;
160 const char *nlsdirs[3] = { NULL, NLSDIR, NULL };
162 int line_number = 1; /* The current line */
163 int char_number = 1; /* The current char pos within the line */
165 char *cmdline; /* The entire commandline */
167 int parser_debug, yy_flex_debug;
169 resource_t *resource_top; /* The top of the parsed resources */
171 static void cleanup_files(void);
172 static void segvhandler(int sig);
174 enum long_options_values
176 LONG_OPT_NOSTDINC = 1,
177 LONG_OPT_TMPFILE,
178 LONG_OPT_NOTMPFILE,
179 LONG_OPT_NLS_DIR,
180 LONG_OPT_PO_DIR,
181 LONG_OPT_PREPROCESSOR,
182 LONG_OPT_SYSROOT,
183 LONG_OPT_VERSION,
184 LONG_OPT_DEBUG,
185 LONG_OPT_ENDIANNESS,
186 LONG_OPT_PEDANTIC,
187 LONG_OPT_VERIFY_TRANSL
190 static const char short_options[] =
191 "b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
192 static const struct option long_options[] = {
193 { "debug", 1, NULL, LONG_OPT_DEBUG },
194 { "define", 1, NULL, 'D' },
195 { "endianness", 1, NULL, LONG_OPT_ENDIANNESS },
196 { "help", 0, NULL, 'h' },
197 { "include-dir", 1, NULL, 'I' },
198 { "input", 1, NULL, 'i' },
199 { "input-format", 1, NULL, 'J' },
200 { "language", 1, NULL, 'l' },
201 { "nls-dir", 1, NULL, LONG_OPT_NLS_DIR },
202 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
203 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
204 { "output", 1, NULL, 'o' },
205 { "output-format", 1, NULL, 'O' },
206 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
207 { "po-dir", 1, NULL, LONG_OPT_PO_DIR },
208 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
209 { "sysroot", 1, NULL, LONG_OPT_SYSROOT },
210 { "target", 1, NULL, 'F' },
211 { "utf8", 0, NULL, 'u' },
212 { "undefine", 1, NULL, 'U' },
213 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
214 { "verbose", 0, NULL, 'v' },
215 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
216 { "version", 0, NULL, LONG_OPT_VERSION },
217 { NULL, 0, NULL, 0 }
220 static void set_version_defines(void)
222 char *version = xstrdup( PACKAGE_VERSION );
223 char *major, *minor, *patchlevel;
224 char buffer[100];
226 if ((minor = strchr( version, '.' )))
228 major = version;
229 *minor++ = 0;
230 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
232 else /* pre 0.9 version */
234 major = NULL;
235 patchlevel = version;
237 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
238 wpp_add_cmdline_define(buffer);
239 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
240 wpp_add_cmdline_define(buffer);
241 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
242 wpp_add_cmdline_define(buffer);
243 free( version );
246 /* clean things up when aborting on a signal */
247 static void exit_on_signal( int sig )
249 exit(1); /* this will call the atexit functions */
252 /* load a single input file */
253 static int load_file( const char *input_name, const char *output_name )
255 int ret;
257 /* Run the preprocessor on the input */
258 if(!no_preprocess)
260 FILE *output;
261 int ret, fd;
262 char *name;
265 * Preprocess the input to a temp-file, or stdout if
266 * no output was given.
269 if (preprocess_only)
271 if (output_name)
273 if (!(output = fopen( output_name, "w" )))
274 fatal_perror( "Could not open %s for writing", output_name );
275 ret = wpp_parse( input_name, output );
276 fclose( output );
278 else ret = wpp_parse( input_name, stdout );
280 if (ret) return ret;
281 output_name = NULL;
282 exit(0);
285 if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
286 else name = xstrdup( "wrc.XXXXXX" );
288 if ((fd = mkstemps( name, 0 )) == -1)
289 error("Could not generate a temp name from %s\n", name);
291 temp_name = name;
292 if (!(output = fdopen(fd, "wt")))
293 error("Could not open fd %s for writing\n", name);
295 ret = wpp_parse( input_name, output );
296 fclose( output );
297 if (ret) return ret;
298 input_name = name;
301 /* Reset the language */
302 currentlanguage = dup_language( defaultlanguage );
303 check_utf8 = 1;
305 /* Go from .rc to .res */
306 chat("Starting parse\n");
308 if(!(parser_in = fopen(input_name, "rb")))
309 fatal_perror("Could not open %s for input", input_name);
311 ret = parser_parse();
312 fclose(parser_in);
313 parser_lex_destroy();
314 if (temp_name)
316 unlink( temp_name );
317 temp_name = NULL;
319 free( currentlanguage );
320 return ret;
323 static void set_target( const char *target )
325 char *p, *cpu = xstrdup( target );
327 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
328 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
329 *p = 0;
330 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) ||
331 !strcmp( cpu, "ia64" ) || !strcmp( cpu, "aarch64" ) ||
332 !strcmp( cpu, "powerpc64" ) || !strcmp( cpu, "powerpc64le" ))
333 pointer_size = 8;
334 else
335 pointer_size = 4;
336 free( cpu );
339 static void init_argv0_dir( const char *argv0 )
341 #ifndef _WIN32
342 char *p, *dir;
344 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
345 dir = realpath( "/proc/self/exe", NULL );
346 #elif defined (__FreeBSD__) || defined(__DragonFly__)
347 dir = realpath( "/proc/curproc/file", NULL );
348 #else
349 dir = realpath( argv0, NULL );
350 #endif
351 if (!dir) return;
352 if (!(p = strrchr( dir, '/' ))) return;
353 if (p == dir) p++;
354 *p = 0;
355 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
356 if (strendswith( dir, "/tools/wrc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
357 else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
358 free( dir );
359 #endif
362 int main(int argc,char *argv[])
364 int optc;
365 int opti = 0;
366 int stdinc = 1;
367 int lose = 0;
368 int nb_files = 0;
369 int i;
370 int cmdlen;
371 int po_mode = 0;
372 char *po_dir = NULL;
373 const char *sysroot = "";
374 char **files = xmalloc( argc * sizeof(*files) );
376 signal(SIGSEGV, segvhandler);
377 signal( SIGTERM, exit_on_signal );
378 signal( SIGINT, exit_on_signal );
379 #ifdef SIGHUP
380 signal( SIGHUP, exit_on_signal );
381 #endif
382 init_argv0_dir( argv[0] );
384 /* Set the default defined stuff */
385 set_version_defines();
386 wpp_add_cmdline_define("RC_INVOKED=1");
387 /* Microsoft RC always searches current directory */
388 wpp_add_include_path(".");
390 /* First rebuild the commandline to put in destination */
391 /* Could be done through env[], but not all OS-es support it */
392 cmdlen = 4; /* for "wrc " */
393 for(i = 1; i < argc; i++)
394 cmdlen += strlen(argv[i]) + 1;
395 cmdline = xmalloc(cmdlen);
396 strcpy(cmdline, "wrc ");
397 for(i = 1; i < argc; i++)
399 strcat(cmdline, argv[i]);
400 if(i < argc-1)
401 strcat(cmdline, " ");
404 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
406 switch(optc)
408 case LONG_OPT_NOSTDINC:
409 stdinc = 0;
410 break;
411 case LONG_OPT_TMPFILE:
412 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
413 break;
414 case LONG_OPT_NOTMPFILE:
415 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
416 break;
417 case LONG_OPT_NLS_DIR:
418 nlsdirs[0] = xstrdup( optarg );
419 break;
420 case LONG_OPT_PO_DIR:
421 po_dir = xstrdup( optarg );
422 break;
423 case LONG_OPT_PREPROCESSOR:
424 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
425 else fprintf(stderr, "-P option not yet supported, ignored.\n");
426 break;
427 case LONG_OPT_SYSROOT:
428 sysroot = xstrdup( optarg );
429 break;
430 case LONG_OPT_VERSION:
431 printf(version_string);
432 exit(0);
433 break;
434 case LONG_OPT_DEBUG:
435 debuglevel = strtol(optarg, NULL, 0);
436 break;
437 case LONG_OPT_ENDIANNESS:
438 switch(optarg[0])
440 case 'n':
441 case 'N':
442 byteorder = WRC_BO_NATIVE;
443 break;
444 case 'l':
445 case 'L':
446 byteorder = WRC_BO_LITTLE;
447 break;
448 case 'b':
449 case 'B':
450 byteorder = WRC_BO_BIG;
451 break;
452 default:
453 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
454 lose++;
456 break;
457 case LONG_OPT_PEDANTIC:
458 pedantic = 1;
459 wpp_set_pedantic(1);
460 break;
461 case LONG_OPT_VERIFY_TRANSL:
462 verify_translations_mode = 1;
463 break;
464 case 'D':
465 wpp_add_cmdline_define(optarg);
466 break;
467 case 'E':
468 preprocess_only = 1;
469 break;
470 case 'b':
471 case 'F':
472 set_target( optarg );
473 break;
474 case 'h':
475 printf(usage);
476 exit(0);
477 case 'i':
478 files[nb_files++] = optarg;
479 break;
480 case 'I':
481 wpp_add_include_path(optarg);
482 break;
483 case 'J':
484 if (strcmp(optarg, "rc16") == 0) extensions = 0;
485 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
486 break;
487 case 'l':
489 int lan;
490 lan = strtol(optarg, NULL, 0);
491 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
492 error("Language %04x is not supported\n", lan);
493 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
495 break;
496 case 'm':
497 if (!strcmp( optarg, "16" )) win32 = 0;
498 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
499 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
500 break;
501 case 'f':
502 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
503 optarg++;
504 /* fall through */
505 case 'o':
506 if (!output_name) output_name = strdup(optarg);
507 else error("Too many output files.\n");
508 break;
509 case 'O':
510 if (strcmp(optarg, "po") == 0) po_mode = 1;
511 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
512 else if (strcmp(optarg, "res16") == 0) win32 = 0;
513 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
514 break;
515 case 'r':
516 /* ignored for compatibility with rc */
517 break;
518 case 'u':
519 utf8_input = 1;
520 break;
521 case 'U':
522 wpp_del_define(optarg);
523 break;
524 case 'v':
525 debuglevel = DEBUGLEVEL_CHAT;
526 break;
527 default:
528 lose++;
529 break;
533 if(lose)
535 fprintf(stderr, usage);
536 return 1;
539 if (win32)
541 wpp_add_cmdline_define("_WIN32=1");
542 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
545 /* If we do need to search standard includes, add them to the path */
546 if (stdinc)
548 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
550 if (includedir)
552 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
553 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
555 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
557 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
558 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
559 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
563 /* Kill io buffering when some kind of debuglevel is enabled */
564 if(debuglevel)
566 setbuf(stdout, NULL);
567 setbuf(stderr, NULL);
570 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
571 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
573 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
574 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
575 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
577 /* Check if the user set a language, else set default */
578 if(!defaultlanguage)
579 defaultlanguage = new_language(0, 0);
581 atexit(cleanup_files);
583 while (optind < argc) files[nb_files++] = argv[optind++];
585 for (i = 0; i < nb_files; i++)
587 input_name = files[i];
588 if (load_file( input_name, output_name )) exit(1);
590 /* stdin special case. NULL means "stdin" for wpp. */
591 if (nb_files == 0 && load_file( NULL, output_name )) exit(1);
593 if(debuglevel & DEBUGLEVEL_DUMP)
594 dump_resources(resource_top);
596 if(verify_translations_mode)
598 verify_translations(resource_top);
599 exit(0);
601 if (!po_mode && output_name)
603 if (strendswith( output_name, ".po" )) po_mode = 1;
604 else if (strendswith( output_name, ".pot" )) po_mode = 2;
606 if (po_mode)
608 if (!win32) error( "PO files are not supported in 16-bit mode\n" );
609 if (po_mode == 2) /* pot file */
611 if (!output_name)
613 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
614 strcat( output_name, ".pot" );
616 write_pot_file( output_name );
618 else write_po_files( output_name );
619 output_name = NULL;
620 exit(0);
622 if (win32) add_translations( po_dir );
624 /* Convert the internal lists to binary data */
625 resources2res(resource_top);
627 chat("Writing .res-file\n");
628 if (!output_name)
630 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
631 strcat(output_name, ".res");
633 write_resfile(output_name, resource_top);
634 output_name = NULL;
636 return 0;
640 static void cleanup_files(void)
642 if (output_name) unlink(output_name);
643 if (temp_name) unlink(temp_name);
646 static void segvhandler(int sig)
648 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
649 fflush(stdout);
650 fflush(stderr);
651 abort();