winepulse: Move session_wrapper_create into mmdevapi.
[wine.git] / tools / wrc / wrc.c
blob48d5d2033d8244544a78755b5fc654c96c110523
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"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <limits.h>
30 #include <sys/types.h>
32 #include "../tools.h"
33 #include "wrc.h"
34 #include "utils.h"
35 #include "newstruc.h"
36 #include "parser.h"
37 #include "wpp_private.h"
39 static const char usage[] =
40 "Usage: wrc [options...] [infile[.rc|.res]]\n"
41 " -D, --define id[=val] Define preprocessor identifier id=val\n"
42 " --debug=nn Set debug level to 'nn'\n"
43 " -E Preprocess only\n"
44 " -F TARGET Ignored, for compatibility with windres\n"
45 " -fo FILE Synonym for -o for compatibility with windres\n"
46 " -h, --help Prints this summary\n"
47 " -i, --input=FILE The name of the input file\n"
48 " -I, --include-dir=DIR Add dir to the include search path (multiple -I allowed)\n"
49 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
50 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
51 " -m16 Build a 16-bit resource file\n"
52 " --nls-dir=DIR Directory containing the NLS codepage mappings\n"
53 " --no-use-temp-file Ignored for compatibility with windres\n"
54 " --nostdinc Disables searching the standard include path\n"
55 " -o, --output=FILE Output to file (default is infile.res)\n"
56 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
57 " --pedantic Enable pedantic warnings\n"
58 " --po-dir=DIR Directory containing po files for translations\n"
59 " --preprocessor Specifies the preprocessor to use, including arguments\n"
60 " -r Ignored for compatibility with rc\n"
61 " --sysroot=DIR Prefix include paths with DIR\n"
62 " -U, --undefine id Undefine preprocessor identifier id\n"
63 " --use-temp-file Ignored for compatibility with windres\n"
64 " -v, --verbose Enable verbose mode\n"
65 " --verify-translations Check the status of the various translations\n"
66 " --version Print version and exit\n"
67 "Input is taken from stdin if no sourcefile specified.\n"
68 "Debug level 'n' is a bitmask with following meaning:\n"
69 " * 0x01 Tell which resource is parsed (verbose mode)\n"
70 " * 0x02 Dump internal structures\n"
71 " * 0x04 Create a parser trace (yydebug=1)\n"
72 " * 0x08 Preprocessor messages\n"
73 " * 0x10 Preprocessor lex messages\n"
74 " * 0x20 Preprocessor yacc trace\n"
75 "If no input filename is given and the output name is not overridden\n"
76 "with -o, then the output is written to \"wrc.tab.res\"\n"
79 static const char version_string[] = "Wine Resource Compiler version " PACKAGE_VERSION "\n"
80 "Copyright 1998-2000 Bertho A. Stultiens\n"
81 " 1994 Martin von Loewis\n";
84 * Set if compiling in 32bit mode (default).
86 int win32 = 1;
89 * debuglevel == DEBUGLEVEL_NONE Don't bother
90 * debuglevel & DEBUGLEVEL_CHAT Say what's done
91 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
92 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
93 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
94 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
95 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
97 int debuglevel = DEBUGLEVEL_NONE;
100 * Recognize win32 keywords if set (-w 32 enforces this),
101 * otherwise set with -e option.
103 int extensions = 1;
106 * Language setting for resources (-l option)
108 static language_t defaultlanguage;
109 language_t currentlanguage = 0;
112 * Set when extra warnings should be generated (-W option)
114 int pedantic = 0;
117 * Set when _only_ to run the preprocessor (-E option)
119 int preprocess_only = 0;
122 * Set when _not_ to run the preprocessor (-P cat option)
124 int no_preprocess = 0;
126 int utf8_input = 0;
128 int check_utf8 = 1; /* whether to check for valid utf8 */
130 static char *output_name; /* The name given by the -o option */
131 const char *input_name = NULL; /* The name given on the command-line */
132 static struct strarray input_files;
133 const char *temp_dir = NULL;
134 struct strarray temp_files = { 0 };
136 static int stdinc = 1;
137 static int po_mode;
138 static const char *po_dir;
139 static const char *sysroot = "";
140 static const char *includedir;
141 const char *nlsdirs[3] = { NULL, NLSDIR, NULL };
143 int line_number = 1; /* The current line */
144 int char_number = 1; /* The current char pos within the line */
146 int parser_debug, yy_flex_debug;
148 resource_t *resource_top; /* The top of the parsed resources */
150 static void cleanup_files(void);
152 enum long_options_values
154 LONG_OPT_NOSTDINC = 1,
155 LONG_OPT_TMPFILE,
156 LONG_OPT_NOTMPFILE,
157 LONG_OPT_NLS_DIR,
158 LONG_OPT_PO_DIR,
159 LONG_OPT_PREPROCESSOR,
160 LONG_OPT_SYSROOT,
161 LONG_OPT_VERSION,
162 LONG_OPT_DEBUG,
163 LONG_OPT_PEDANTIC,
166 static const char short_options[] =
167 "b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
168 static const struct long_option long_options[] = {
169 { "debug", 1, LONG_OPT_DEBUG },
170 { "define", 1, 'D' },
171 { "help", 0, 'h' },
172 { "include-dir", 1, 'I' },
173 { "input", 1, 'i' },
174 { "input-format", 1, 'J' },
175 { "language", 1, 'l' },
176 { "nls-dir", 1, LONG_OPT_NLS_DIR },
177 { "no-use-temp-file", 0, LONG_OPT_NOTMPFILE },
178 { "nostdinc", 0, LONG_OPT_NOSTDINC },
179 { "output", 1, 'o' },
180 { "output-format", 1, 'O' },
181 { "pedantic", 0, LONG_OPT_PEDANTIC },
182 { "po-dir", 1, LONG_OPT_PO_DIR },
183 { "preprocessor", 1, LONG_OPT_PREPROCESSOR },
184 { "sysroot", 1, LONG_OPT_SYSROOT },
185 { "target", 1, 'F' },
186 { "utf8", 0, 'u' },
187 { "undefine", 1, 'U' },
188 { "use-temp-file", 0, LONG_OPT_TMPFILE },
189 { "verbose", 0, 'v' },
190 { "version", 0, LONG_OPT_VERSION },
191 { NULL }
194 static void set_version_defines(void)
196 char *version = xstrdup( PACKAGE_VERSION );
197 char *major, *minor, *patchlevel;
198 char buffer[100];
200 if ((minor = strchr( version, '.' )))
202 major = version;
203 *minor++ = 0;
204 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
206 else /* pre 0.9 version */
208 major = NULL;
209 patchlevel = version;
211 sprintf( buffer, "__WRC__=%s", major ? major : "0" );
212 wpp_add_cmdline_define(buffer);
213 sprintf( buffer, "__WRC_MINOR__=%s", minor ? minor : "0" );
214 wpp_add_cmdline_define(buffer);
215 sprintf( buffer, "__WRC_PATCHLEVEL__=%s", patchlevel ? patchlevel : "0" );
216 wpp_add_cmdline_define(buffer);
217 free( version );
220 /* clean things up when aborting on a signal */
221 static void exit_on_signal( int sig )
223 exit(1); /* this will call the atexit functions */
226 /* load a single input file */
227 static int load_file( const char *input_name, const char *output_name )
229 int ret;
231 /* Run the preprocessor on the input */
232 if(!no_preprocess)
234 FILE *output;
235 int ret;
236 char *name;
239 * Preprocess the input to a temp-file, or stdout if
240 * no output was given.
243 if (preprocess_only)
245 if (output_name)
247 if (!(output = fopen( output_name, "w" )))
248 fatal_perror( "Could not open %s for writing", output_name );
249 ret = wpp_parse( input_name, output );
250 fclose( output );
252 else ret = wpp_parse( input_name, stdout );
254 if (ret) return ret;
255 output_name = NULL;
256 exit(0);
259 name = make_temp_file( output_name, "" );
260 if (!(output = fopen(name, "wt")))
261 error("Could not open fd %s for writing\n", name);
263 ret = wpp_parse( input_name, output );
264 fclose( output );
265 if (ret) return ret;
266 input_name = name;
269 /* Reset the language */
270 currentlanguage = defaultlanguage;
271 check_utf8 = 1;
273 /* Go from .rc to .res */
274 chat("Starting parse\n");
276 if(!(parser_in = fopen(input_name, "rb")))
277 fatal_perror("Could not open %s for input", input_name);
279 ret = parser_parse();
280 fclose(parser_in);
281 parser_lex_destroy();
282 return ret;
285 static void init_argv0_dir( const char *argv0 )
287 char *dir = get_argv0_dir( argv0 );
289 if (!dir) return;
290 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
291 if (strendswith( dir, "/tools/wrc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
292 else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
295 static void option_callback( int optc, char *optarg )
297 switch(optc)
299 case LONG_OPT_NOSTDINC:
300 stdinc = 0;
301 break;
302 case LONG_OPT_TMPFILE:
303 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
304 break;
305 case LONG_OPT_NOTMPFILE:
306 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
307 break;
308 case LONG_OPT_NLS_DIR:
309 nlsdirs[0] = xstrdup( optarg );
310 break;
311 case LONG_OPT_PO_DIR:
312 po_dir = xstrdup( optarg );
313 break;
314 case LONG_OPT_PREPROCESSOR:
315 if (strcmp(optarg, "cat") == 0) no_preprocess = 1;
316 else fprintf(stderr, "-P option not yet supported, ignored.\n");
317 break;
318 case LONG_OPT_SYSROOT:
319 sysroot = xstrdup( optarg );
320 break;
321 case LONG_OPT_VERSION:
322 printf(version_string);
323 exit(0);
324 break;
325 case LONG_OPT_DEBUG:
326 debuglevel = strtol(optarg, NULL, 0);
327 break;
328 case LONG_OPT_PEDANTIC:
329 pedantic = 1;
330 break;
331 case 'D':
332 wpp_add_cmdline_define(optarg);
333 break;
334 case 'E':
335 preprocess_only = 1;
336 break;
337 case 'b':
338 case 'F':
339 break;
340 case 'h':
341 printf(usage);
342 exit(0);
343 case 'i':
344 strarray_add( &input_files, optarg );
345 break;
346 case 'I':
347 wpp_add_include_path(optarg);
348 break;
349 case 'J':
350 if (strcmp(optarg, "rc16") == 0) extensions = 0;
351 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
352 break;
353 case 'l':
354 defaultlanguage = strtol(optarg, NULL, 0);
355 if (get_language_codepage( defaultlanguage ) == -1)
356 error("Language %04x is not supported\n", defaultlanguage);
357 break;
358 case 'm':
359 if (!strcmp( optarg, "16" )) win32 = 0;
360 else win32 = 1;
361 break;
362 case 'f':
363 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
364 optarg++;
365 /* fall through */
366 case 'o':
367 if (!output_name) output_name = xstrdup(optarg);
368 else error("Too many output files.\n");
369 break;
370 case 'O':
371 if (strcmp(optarg, "po") == 0) po_mode = 1;
372 else if (strcmp(optarg, "pot") == 0) po_mode = 2;
373 else if (strcmp(optarg, "res16") == 0) win32 = 0;
374 else if (strcmp(optarg, "res")) warning("Output format %s not supported.\n", optarg);
375 break;
376 case 'r':
377 /* ignored for compatibility with rc */
378 break;
379 case 'u':
380 utf8_input = 1;
381 break;
382 case 'U':
383 wpp_del_define(optarg);
384 break;
385 case 'v':
386 debuglevel = DEBUGLEVEL_CHAT;
387 break;
388 case '?':
389 fprintf(stderr, "wrc: %s\n\n%s", optarg, usage);
390 exit(1);
394 int main(int argc,char *argv[])
396 int i;
398 init_signals( exit_on_signal );
399 init_argv0_dir( argv[0] );
401 /* Set the default defined stuff */
402 set_version_defines();
403 wpp_add_cmdline_define("RC_INVOKED=1");
404 /* Microsoft RC always searches current directory */
405 wpp_add_include_path(".");
407 strarray_addall( &input_files,
408 parse_options( argc, argv, short_options, long_options, 0, option_callback ));
410 if (win32) wpp_add_cmdline_define("_WIN32=1");
412 /* If we do need to search standard includes, add them to the path */
413 if (stdinc)
415 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
417 if (includedir)
419 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir ));
420 wpp_add_include_path( strmake( "%s/wine/windows", includedir ));
422 for (i = 0; i < ARRAY_SIZE(incl_dirs); i++)
424 if (i && !strcmp( incl_dirs[i], incl_dirs[0] )) continue;
425 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot, incl_dirs[i] ));
426 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot, incl_dirs[i] ));
430 /* Kill io buffering when some kind of debuglevel is enabled */
431 if(debuglevel)
433 setbuf(stdout, NULL);
434 setbuf(stderr, NULL);
437 parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
438 yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
440 wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
441 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
442 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );
444 atexit(cleanup_files);
446 for (i = 0; i < input_files.count; i++)
448 input_name = input_files.str[i];
449 if (load_file( input_name, output_name )) exit(1);
451 /* stdin special case. NULL means "stdin" for wpp. */
452 if (input_files.count == 0 && load_file( NULL, output_name )) exit(1);
454 if (!po_mode && output_name)
456 if (strendswith( output_name, ".po" )) po_mode = 1;
457 else if (strendswith( output_name, ".pot" )) po_mode = 2;
459 if (po_mode)
461 if (!win32) error( "PO files are not supported in 16-bit mode\n" );
462 if (po_mode == 2) /* pot file */
464 if (!output_name)
466 const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
467 output_name = replace_extension( name, ".rc", ".pot" );
469 write_pot_file( output_name );
471 else write_po_files( output_name );
472 output_name = NULL;
473 exit(0);
475 if (win32) add_translations( po_dir );
477 chat("Writing .res-file\n");
478 if (!output_name)
480 const char *name = input_files.count ? get_basename(input_files.str[0]) : "wrc.tab";
481 output_name = replace_extension( name, ".rc", ".res" );
483 write_resfile(output_name, resource_top);
484 output_name = NULL;
486 return 0;
490 static void cleanup_files(void)
492 if (output_name) unlink(output_name);
493 remove_temp_files();