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
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SYSCTL_H
33 # include <sys/sysctl.h>
42 #include "wpp_private.h"
44 static const char usage
[] =
45 "Usage: wrc [options...] [infile[.rc|.res]]\n"
46 " -D, --define id[=val] Define preprocessor identifier id=val\n"
47 " --debug=nn Set debug level to 'nn'\n"
48 " -E Preprocess only\n"
49 " -F TARGET Ignored, for compatibility with windres\n"
50 " -fo FILE Synonym for -o for compatibility with windres\n"
51 " -h, --help Prints this summary\n"
52 " -i, --input=FILE The name of the input file\n"
53 " -I, --include-dir=DIR Add dir to the include search path (multiple -I allowed)\n"
54 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
55 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
56 " -m16 Build a 16-bit resource file\n"
57 " --nls-dir=DIR Directory containing the NLS codepage mappings\n"
58 " --no-use-temp-file Ignored for compatibility with windres\n"
59 " --nostdinc Disables searching the standard include path\n"
60 " -o, --output=FILE Output to file (default is infile.res)\n"
61 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
62 " --pedantic Enable pedantic warnings\n"
63 " --po-dir=DIR Directory containing po files for translations\n"
64 " --preprocessor Specifies the preprocessor to use, including arguments\n"
65 " -r Ignored for compatibility with rc\n"
66 " --sysroot=DIR Prefix include paths with DIR\n"
67 " -U, --undefine id Undefine preprocessor identifier id\n"
68 " --use-temp-file Ignored for compatibility with windres\n"
69 " -v, --verbose Enable verbose mode\n"
70 " --verify-translations Check the status of the various translations\n"
71 " --version Print version and exit\n"
72 "Input is taken from stdin if no sourcefile specified.\n"
73 "Debug level 'n' is a bitmask with following meaning:\n"
74 " * 0x01 Tell which resource is parsed (verbose mode)\n"
75 " * 0x02 Dump internal structures\n"
76 " * 0x04 Create a parser trace (yydebug=1)\n"
77 " * 0x08 Preprocessor messages\n"
78 " * 0x10 Preprocessor lex messages\n"
79 " * 0x20 Preprocessor yacc trace\n"
80 "If no input filename is given and the output name is not overridden\n"
81 "with -o, then the output is written to \"wrc.tab.res\"\n"
84 static const char version_string
[] = "Wine Resource Compiler version " PACKAGE_VERSION
"\n"
85 "Copyright 1998-2000 Bertho A. Stultiens\n"
86 " 1994 Martin von Loewis\n";
89 * Set if compiling in 32bit mode (default).
94 * debuglevel == DEBUGLEVEL_NONE Don't bother
95 * debuglevel & DEBUGLEVEL_CHAT Say what's done
96 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
97 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
98 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
99 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
100 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
102 int debuglevel
= DEBUGLEVEL_NONE
;
105 * Recognize win32 keywords if set (-w 32 enforces this),
106 * otherwise set with -e option.
111 * Language setting for resources (-l option)
113 static language_t
*defaultlanguage
;
114 language_t
*currentlanguage
= NULL
;
117 * Set when extra warnings should be generated (-W option)
122 * Set when _only_ to run the preprocessor (-E option)
124 int preprocess_only
= 0;
127 * Set when _not_ to run the preprocessor (-P cat option)
129 int no_preprocess
= 0;
133 int check_utf8
= 1; /* whether to check for valid utf8 */
135 static int verify_translations_mode
;
137 static char *output_name
; /* The name given by the -o option */
138 const char *input_name
= NULL
; /* The name given on the command-line */
139 static char *temp_name
= NULL
; /* Temporary file for preprocess pipe */
140 static struct strarray input_files
;
142 static int stdinc
= 1;
144 static const char *po_dir
;
145 static const char *sysroot
= "";
146 static const char *includedir
;
147 const char *nlsdirs
[3] = { NULL
, NLSDIR
, NULL
};
149 int line_number
= 1; /* The current line */
150 int char_number
= 1; /* The current char pos within the line */
152 int parser_debug
, yy_flex_debug
;
154 resource_t
*resource_top
; /* The top of the parsed resources */
156 static void cleanup_files(void);
158 enum long_options_values
160 LONG_OPT_NOSTDINC
= 1,
165 LONG_OPT_PREPROCESSOR
,
170 LONG_OPT_VERIFY_TRANSL
173 static const char short_options
[] =
174 "b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
175 static const struct long_option long_options
[] = {
176 { "debug", 1, LONG_OPT_DEBUG
},
177 { "define", 1, 'D' },
179 { "include-dir", 1, 'I' },
181 { "input-format", 1, 'J' },
182 { "language", 1, 'l' },
183 { "nls-dir", 1, LONG_OPT_NLS_DIR
},
184 { "no-use-temp-file", 0, LONG_OPT_NOTMPFILE
},
185 { "nostdinc", 0, LONG_OPT_NOSTDINC
},
186 { "output", 1, 'o' },
187 { "output-format", 1, 'O' },
188 { "pedantic", 0, LONG_OPT_PEDANTIC
},
189 { "po-dir", 1, LONG_OPT_PO_DIR
},
190 { "preprocessor", 1, LONG_OPT_PREPROCESSOR
},
191 { "sysroot", 1, LONG_OPT_SYSROOT
},
192 { "target", 1, 'F' },
194 { "undefine", 1, 'U' },
195 { "use-temp-file", 0, LONG_OPT_TMPFILE
},
196 { "verbose", 0, 'v' },
197 { "verify-translations", 0, LONG_OPT_VERIFY_TRANSL
},
198 { "version", 0, LONG_OPT_VERSION
},
202 static void set_version_defines(void)
204 char *version
= xstrdup( PACKAGE_VERSION
);
205 char *major
, *minor
, *patchlevel
;
208 if ((minor
= strchr( version
, '.' )))
212 if ((patchlevel
= strchr( minor
, '.' ))) *patchlevel
++ = 0;
214 else /* pre 0.9 version */
217 patchlevel
= version
;
219 sprintf( buffer
, "__WRC__=%s", major
? major
: "0" );
220 wpp_add_cmdline_define(buffer
);
221 sprintf( buffer
, "__WRC_MINOR__=%s", minor
? minor
: "0" );
222 wpp_add_cmdline_define(buffer
);
223 sprintf( buffer
, "__WRC_PATCHLEVEL__=%s", patchlevel
? patchlevel
: "0" );
224 wpp_add_cmdline_define(buffer
);
228 /* clean things up when aborting on a signal */
229 static void exit_on_signal( int sig
)
231 exit(1); /* this will call the atexit functions */
234 /* load a single input file */
235 static int load_file( const char *input_name
, const char *output_name
)
239 /* Run the preprocessor on the input */
247 * Preprocess the input to a temp-file, or stdout if
248 * no output was given.
255 if (!(output
= fopen( output_name
, "w" )))
256 fatal_perror( "Could not open %s for writing", output_name
);
257 ret
= wpp_parse( input_name
, output
);
260 else ret
= wpp_parse( input_name
, stdout
);
267 fd
= make_temp_file( output_name
, "", &name
);
269 if (!(output
= fdopen(fd
, "wt")))
270 error("Could not open fd %s for writing\n", name
);
272 ret
= wpp_parse( input_name
, output
);
278 /* Reset the language */
279 currentlanguage
= dup_language( defaultlanguage
);
282 /* Go from .rc to .res */
283 chat("Starting parse\n");
285 if(!(parser_in
= fopen(input_name
, "rb")))
286 fatal_perror("Could not open %s for input", input_name
);
288 ret
= parser_parse();
290 parser_lex_destroy();
296 free( currentlanguage
);
300 static void init_argv0_dir( const char *argv0
)
305 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
306 dir
= realpath( "/proc/self/exe", NULL
);
307 #elif defined (__FreeBSD__) || defined(__DragonFly__)
308 static int pathname
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
309 size_t path_size
= PATH_MAX
;
310 char *path
= malloc( path_size
);
311 if (path
&& !sysctl( pathname
, sizeof(pathname
)/sizeof(pathname
[0]), path
, &path_size
, NULL
, 0 ))
312 dir
= realpath( path
, NULL
);
315 dir
= realpath( argv0
, NULL
);
318 dir
= get_dirname( dir
);
319 includedir
= strmake( "%s/%s", dir
, BIN_TO_INCLUDEDIR
);
320 if (strendswith( dir
, "/tools/wrc" )) nlsdirs
[0] = strmake( "%s/../../nls", dir
);
321 else nlsdirs
[0] = strmake( "%s/%s", dir
, BIN_TO_NLSDIR
);
325 static void option_callback( int optc
, char *optarg
)
329 case LONG_OPT_NOSTDINC
:
332 case LONG_OPT_TMPFILE
:
333 if (debuglevel
) warning("--use-temp-file option not yet supported, ignored.\n");
335 case LONG_OPT_NOTMPFILE
:
336 if (debuglevel
) warning("--no-use-temp-file option not yet supported, ignored.\n");
338 case LONG_OPT_NLS_DIR
:
339 nlsdirs
[0] = xstrdup( optarg
);
341 case LONG_OPT_PO_DIR
:
342 po_dir
= xstrdup( optarg
);
344 case LONG_OPT_PREPROCESSOR
:
345 if (strcmp(optarg
, "cat") == 0) no_preprocess
= 1;
346 else fprintf(stderr
, "-P option not yet supported, ignored.\n");
348 case LONG_OPT_SYSROOT
:
349 sysroot
= xstrdup( optarg
);
351 case LONG_OPT_VERSION
:
352 printf(version_string
);
356 debuglevel
= strtol(optarg
, NULL
, 0);
358 case LONG_OPT_PEDANTIC
:
361 case LONG_OPT_VERIFY_TRANSL
:
362 verify_translations_mode
= 1;
365 wpp_add_cmdline_define(optarg
);
377 strarray_add( &input_files
, optarg
);
380 wpp_add_include_path(optarg
);
383 if (strcmp(optarg
, "rc16") == 0) extensions
= 0;
384 else if (strcmp(optarg
, "rc")) error("Output format %s not supported.\n", optarg
);
389 lan
= strtol(optarg
, NULL
, 0);
390 if (get_language_codepage(PRIMARYLANGID(lan
), SUBLANGID(lan
)) == -1)
391 error("Language %04x is not supported\n", lan
);
392 defaultlanguage
= new_language(PRIMARYLANGID(lan
), SUBLANGID(lan
));
396 if (!strcmp( optarg
, "16" )) win32
= 0;
400 if (*optarg
!= 'o') error("Unknown option: -f%s\n", optarg
);
404 if (!output_name
) output_name
= strdup(optarg
);
405 else error("Too many output files.\n");
408 if (strcmp(optarg
, "po") == 0) po_mode
= 1;
409 else if (strcmp(optarg
, "pot") == 0) po_mode
= 2;
410 else if (strcmp(optarg
, "res16") == 0) win32
= 0;
411 else if (strcmp(optarg
, "res")) warning("Output format %s not supported.\n", optarg
);
414 /* ignored for compatibility with rc */
420 wpp_del_define(optarg
);
423 debuglevel
= DEBUGLEVEL_CHAT
;
426 fprintf(stderr
, "wrc: %s\n\n%s", optarg
, usage
);
431 int main(int argc
,char *argv
[])
435 signal( SIGTERM
, exit_on_signal
);
436 signal( SIGINT
, exit_on_signal
);
438 signal( SIGHUP
, exit_on_signal
);
440 init_argv0_dir( argv
[0] );
442 /* Set the default defined stuff */
443 set_version_defines();
444 wpp_add_cmdline_define("RC_INVOKED=1");
445 /* Microsoft RC always searches current directory */
446 wpp_add_include_path(".");
448 strarray_addall( &input_files
,
449 parse_options( argc
, argv
, short_options
, long_options
, 0, option_callback
));
451 if (win32
) wpp_add_cmdline_define("_WIN32=1");
453 /* If we do need to search standard includes, add them to the path */
456 static const char *incl_dirs
[] = { INCLUDEDIR
, "/usr/include", "/usr/local/include" };
460 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir
));
461 wpp_add_include_path( strmake( "%s/wine/windows", includedir
));
463 for (i
= 0; i
< ARRAY_SIZE(incl_dirs
); i
++)
465 if (i
&& !strcmp( incl_dirs
[i
], incl_dirs
[0] )) continue;
466 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot
, incl_dirs
[i
] ));
467 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot
, incl_dirs
[i
] ));
471 /* Kill io buffering when some kind of debuglevel is enabled */
474 setbuf(stdout
, NULL
);
475 setbuf(stderr
, NULL
);
478 parser_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
479 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
481 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
482 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
483 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
485 /* Check if the user set a language, else set default */
487 defaultlanguage
= new_language(0, 0);
489 atexit(cleanup_files
);
491 for (i
= 0; i
< input_files
.count
; i
++)
493 input_name
= input_files
.str
[i
];
494 if (load_file( input_name
, output_name
)) exit(1);
496 /* stdin special case. NULL means "stdin" for wpp. */
497 if (input_files
.count
== 0 && load_file( NULL
, output_name
)) exit(1);
499 if(debuglevel
& DEBUGLEVEL_DUMP
)
500 dump_resources(resource_top
);
502 if(verify_translations_mode
)
504 verify_translations(resource_top
);
507 if (!po_mode
&& output_name
)
509 if (strendswith( output_name
, ".po" )) po_mode
= 1;
510 else if (strendswith( output_name
, ".pot" )) po_mode
= 2;
514 if (!win32
) error( "PO files are not supported in 16-bit mode\n" );
515 if (po_mode
== 2) /* pot file */
519 const char *name
= input_files
.count
? get_basename(input_files
.str
[0]) : "wrc.tab";
520 output_name
= replace_extension( name
, ".rc", ".pot" );
522 write_pot_file( output_name
);
524 else write_po_files( output_name
);
528 if (win32
) add_translations( po_dir
);
530 chat("Writing .res-file\n");
533 const char *name
= input_files
.count
? get_basename(input_files
.str
[0]) : "wrc.tab";
534 output_name
= replace_extension( name
, ".rc", ".res" );
536 write_resfile(output_name
, resource_top
);
543 static void cleanup_files(void)
545 if (output_name
) unlink(output_name
);
546 if (temp_name
) unlink(temp_name
);