d2d1/tests: Add tests for rounded rectangle drawing.
[wine.git] / tools / wrc / wrc.c
blobd171bfa2ef1acf18cbe423b71fe9287d8876a17a
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 " --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 check_utf8 = 1; /* whether to check for valid utf8 */
149 static int pointer_size = sizeof(void *);
151 static int verify_translations_mode;
153 static char *output_name; /* The name given by the -o option */
154 char *input_name = NULL; /* The name given on the command-line */
155 static char *temp_name = NULL; /* Temporary file for preprocess pipe */
157 static const char *includedir;
159 int line_number = 1; /* The current line */
160 int char_number = 1; /* The current char pos within the line */
162 char *cmdline; /* The entire commandline */
164 int parser_debug, yy_flex_debug;
166 resource_t *resource_top; /* The top of the parsed resources */
168 static void cleanup_files(void);
169 static void segvhandler(int sig);
171 enum long_options_values
173 LONG_OPT_NOSTDINC = 1,
174 LONG_OPT_TMPFILE,
175 LONG_OPT_NOTMPFILE,
176 LONG_OPT_PO_DIR,
177 LONG_OPT_PREPROCESSOR,
178 LONG_OPT_SYSROOT,
179 LONG_OPT_VERSION,
180 LONG_OPT_DEBUG,
181 LONG_OPT_ENDIANNESS,
182 LONG_OPT_PEDANTIC,
183 LONG_OPT_VERIFY_TRANSL
186 static const char short_options[] =
187 "b:D:Ef:F:hi:I:J:l:m:o:O:rU:v";
188 static const struct option long_options[] = {
189 { "debug", 1, NULL, LONG_OPT_DEBUG },
190 { "define", 1, NULL, 'D' },
191 { "endianness", 1, NULL, LONG_OPT_ENDIANNESS },
192 { "help", 0, NULL, 'h' },
193 { "include-dir", 1, NULL, 'I' },
194 { "input", 1, NULL, 'i' },
195 { "input-format", 1, NULL, 'J' },
196 { "language", 1, NULL, 'l' },
197 { "no-use-temp-file", 0, NULL, LONG_OPT_NOTMPFILE },
198 { "nostdinc", 0, NULL, LONG_OPT_NOSTDINC },
199 { "output", 1, NULL, 'o' },
200 { "output-format", 1, NULL, 'O' },
201 { "pedantic", 0, NULL, LONG_OPT_PEDANTIC },
202 { "po-dir", 1, NULL, LONG_OPT_PO_DIR },
203 { "preprocessor", 1, NULL, LONG_OPT_PREPROCESSOR },
204 { "sysroot", 1, NULL, LONG_OPT_SYSROOT },
205 { "target", 1, NULL, 'F' },
206 { "undefine", 1, NULL, 'U' },
207 { "use-temp-file", 0, NULL, LONG_OPT_TMPFILE },
208 { "verbose", 0, NULL, 'v' },
209 { "verify-translations", 0, NULL, LONG_OPT_VERIFY_TRANSL },
210 { "version", 0, NULL, LONG_OPT_VERSION },
211 { NULL, 0, NULL, 0 }
214 static void set_version_defines(void)
216 char *version = xstrdup( PACKAGE_VERSION );
217 char *major, *minor, *patchlevel;
218 char buffer[100];
220 if ((minor = strchr( version, '.' )))
222 major = version;
223 *minor++ = 0;
224 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
226 else /* pre 0.9 version */
228 major = NULL;
229 patchlevel = version;
231 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
232 wpp_add_cmdline_define(buffer);
233 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
234 wpp_add_cmdline_define(buffer);
235 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
236 wpp_add_cmdline_define(buffer);
237 free( version );
240 /* clean things up when aborting on a signal */
241 static void exit_on_signal( int sig )
243 exit(1); /* this will call the atexit functions */
246 /* load a single input file */
247 static int load_file( const char *input_name, const char *output_name )
249 int ret;
251 /* Run the preprocessor on the input */
252 if(!no_preprocess)
254 FILE *output;
255 int ret, fd;
256 char *name;
259 * Preprocess the input to a temp-file, or stdout if
260 * no output was given.
263 if (preprocess_only)
265 if (output_name)
267 if (!(output = fopen( output_name, "w" )))
268 fatal_perror( "Could not open %s for writing", output_name );
269 ret = wpp_parse( input_name, output );
270 fclose( output );
272 else ret = wpp_parse( input_name, stdout );
274 if (ret) return ret;
275 output_name = NULL;
276 exit(0);
279 if (output_name && output_name[0]) name = strmake( "%s.XXXXXX", output_name );
280 else name = xstrdup( "wrc.XXXXXX" );
282 if ((fd = mkstemps( name, 0 )) == -1)
283 error("Could not generate a temp name from %s\n", name);
285 temp_name = name;
286 if (!(output = fdopen(fd, "wt")))
287 error("Could not open fd %s for writing\n", name);
289 ret = wpp_parse( input_name, output );
290 fclose( output );
291 if (ret) return ret;
292 input_name = name;
295 /* Reset the language */
296 currentlanguage = dup_language( defaultlanguage );
297 check_utf8 = 1;
299 /* Go from .rc to .res */
300 chat("Starting parse\n");
302 if(!(parser_in = fopen(input_name, "rb")))
303 fatal_perror("Could not open %s for input", input_name);
305 ret = parser_parse();
306 fclose(parser_in);
307 parser_lex_destroy();
308 if (temp_name)
310 unlink( temp_name );
311 temp_name = NULL;
313 free( currentlanguage );
314 return ret;
317 static void set_target( const char *target )
319 char *p, *cpu = xstrdup( target );
321 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
322 if (!(p = strchr( cpu, '-' ))) error( "Invalid target specification '%s'\n", target );
323 *p = 0;
324 if (!strcmp( cpu, "amd64" ) || !strcmp( cpu, "x86_64" ) ||
325 !strcmp( cpu, "ia64" ) || !strcmp( cpu, "aarch64" ))
326 pointer_size = 8;
327 else
328 pointer_size = 4;
329 free( cpu );
332 static void init_argv0_dir( const char *argv0 )
334 #ifndef _WIN32
335 char *p, *dir;
337 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
338 dir = realpath( "/proc/self/exe", NULL );
339 #elif defined (__FreeBSD__) || defined(__DragonFly__)
340 dir = realpath( "/proc/curproc/file", NULL );
341 #else
342 dir = realpath( argv0, NULL );
343 #endif
344 if (!dir) return;
345 if (!(p = strrchr( dir, '/' ))) return;
346 if (p == dir) p++;
347 *p = 0;
348 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
349 free( dir );
350 #endif
353 int main(int argc,char *argv[])
355 int optc;
356 int opti = 0;
357 int stdinc = 1;
358 int lose = 0;
359 int nb_files = 0;
360 int i;
361 int cmdlen;
362 int po_mode = 0;
363 char *po_dir = NULL;
364 const char *sysroot = "";
365 char **files = xmalloc( argc * sizeof(*files) );
367 signal(SIGSEGV, segvhandler);
368 signal( SIGTERM, exit_on_signal );
369 signal( SIGINT, exit_on_signal );
370 #ifdef SIGHUP
371 signal( SIGHUP, exit_on_signal );
372 #endif
373 init_argv0_dir( argv[0] );
375 /* Set the default defined stuff */
376 set_version_defines();
377 wpp_add_cmdline_define("RC_INVOKED=1");
378 /* Microsoft RC always searches current directory */
379 wpp_add_include_path(".");
381 /* First rebuild the commandline to put in destination */
382 /* Could be done through env[], but not all OS-es support it */
383 cmdlen = 4; /* for "wrc " */
384 for(i = 1; i < argc; i++)
385 cmdlen += strlen(argv[i]) + 1;
386 cmdline = xmalloc(cmdlen);
387 strcpy(cmdline, "wrc ");
388 for(i = 1; i < argc; i++)
390 strcat(cmdline, argv[i]);
391 if(i < argc-1)
392 strcat(cmdline, " ");
395 while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
397 switch(optc)
399 case LONG_OPT_NOSTDINC:
400 stdinc = 0;
401 break;
402 case LONG_OPT_TMPFILE:
403 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
404 break;
405 case LONG_OPT_NOTMPFILE:
406 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
407 break;
408 case LONG_OPT_PO_DIR:
409 po_dir = xstrdup( optarg );
410 break;
411 case LONG_OPT_PREPROCESSOR:
412 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
413 else fprintf(stderr, "-P option not yet supported, ignored.\n");
414 break;
415 case LONG_OPT_SYSROOT:
416 sysroot = xstrdup( optarg );
417 break;
418 case LONG_OPT_VERSION:
419 printf(version_string);
420 exit(0);
421 break;
422 case LONG_OPT_DEBUG:
423 debuglevel = strtol(optarg, NULL, 0);
424 break;
425 case LONG_OPT_ENDIANNESS:
426 switch(optarg[0])
428 case 'n':
429 case 'N':
430 byteorder = WRC_BO_NATIVE;
431 break;
432 case 'l':
433 case 'L':
434 byteorder = WRC_BO_LITTLE;
435 break;
436 case 'b':
437 case 'B':
438 byteorder = WRC_BO_BIG;
439 break;
440 default:
441 fprintf(stderr, "Byte ordering must be n[ative], l[ittle] or b[ig]\n");
442 lose++;
444 break;
445 case LONG_OPT_PEDANTIC:
446 pedantic = 1;
447 wpp_set_pedantic(1);
448 break;
449 case LONG_OPT_VERIFY_TRANSL:
450 verify_translations_mode = 1;
451 break;
452 case 'D':
453 wpp_add_cmdline_define(optarg);
454 break;
455 case 'E':
456 preprocess_only = 1;
457 break;
458 case 'b':
459 case 'F':
460 set_target( optarg );
461 break;
462 case 'h':
463 printf(usage);
464 exit(0);
465 case 'i':
466 files[nb_files++] = optarg;
467 break;
468 case 'I':
469 wpp_add_include_path(optarg);
470 break;
471 case 'J':
472 if (strcmp(optarg, "rc16") == 0) extensions = 0;
473 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
474 break;
475 case 'l':
477 int lan;
478 lan = strtol(optarg, NULL, 0);
479 if (get_language_codepage(PRIMARYLANGID(lan), SUBLANGID(lan)) == -1)
480 error("Language %04x is not supported\n", lan);
481 defaultlanguage = new_language(PRIMARYLANGID(lan), SUBLANGID(lan));
483 break;
484 case 'm':
485 if (!strcmp( optarg, "16" )) win32 = 0;
486 else if (!strcmp( optarg, "32" )) { win32 = 1; pointer_size = 4; }
487 else if (!strcmp( optarg, "64" )) { win32 = 1; pointer_size = 8; }
488 break;
489 case 'f':
490 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
491 optarg++;
492 /* fall through */
493 case 'o':
494 if (!output_name) output_name = strdup(optarg);
495 else error("Too many output files.\n");
496 break;
497 case 'O':
498 if (strcmp(optarg, "po") == 0) po_mode = 1;
499 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
500 else if (strcmp(optarg, "res16") == 0) win32 = 0;
501 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
502 break;
503 case 'r':
504 /* ignored for compatibility with rc */
505 break;
506 case 'U':
507 wpp_del_define(optarg);
508 break;
509 case 'v':
510 debuglevel = DEBUGLEVEL_CHAT;
511 break;
512 default:
513 lose++;
514 break;
518 if(lose)
520 fprintf(stderr, usage);
521 return 1;
524 if (win32)
526 wpp_add_cmdline_define("_WIN32=1");
527 if (pointer_size == 8) wpp_add_cmdline_define("_WIN64=1");
530 /* If we do need to search standard includes, add them to the path */
531 if (stdinc)
533 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
535 if (includedir)
537 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
538 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
540 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
542 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
543 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
544 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
548 /* Kill io buffering when some kind of debuglevel is enabled */
549 if(debuglevel)
551 setbuf(stdout, NULL);
552 setbuf(stderr, NULL);
555 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
556 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
558 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
559 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
560 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
562 /* Check if the user set a language, else set default */
563 if(!defaultlanguage)
564 defaultlanguage = new_language(0, 0);
566 atexit(cleanup_files);
568 while (optind < argc) files[nb_files++] = argv[optind++];
570 for (i = 0; i < nb_files; i++)
572 input_name = files[i];
573 if (load_file( input_name, output_name )) exit(1);
575 /* stdin special case. NULL means "stdin" for wpp. */
576 if (nb_files == 0 && load_file( NULL, output_name )) exit(1);
578 if(debuglevel & DEBUGLEVEL_DUMP)
579 dump_resources(resource_top);
581 if(verify_translations_mode)
583 verify_translations(resource_top);
584 exit(0);
586 if (po_mode)
588 if (po_mode == 2) /* pot file */
590 if (!output_name)
592 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
593 strcat( output_name, ".pot" );
595 write_pot_file( output_name );
597 else write_po_files( output_name );
598 output_name = NULL;
599 exit(0);
601 add_translations( po_dir );
603 /* Convert the internal lists to binary data */
604 resources2res(resource_top);
606 chat("Writing .res-file\n");
607 if (!output_name)
609 output_name = dup_basename( nb_files ? files[0] : NULL, ".rc" );
610 strcat(output_name, ".res");
612 write_resfile(output_name, resource_top);
613 output_name = NULL;
615 return 0;
619 static void cleanup_files(void)
621 if (output_name) unlink(output_name);
622 if (temp_name) unlink(temp_name);
625 static void segvhandler(int sig)
627 fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
628 fflush(stdout);
629 fflush(stderr);
630 abort();