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>
41 #include "wpp_private.h"
43 static const char usage
[] =
44 "Usage: wrc [options...] [infile[.rc|.res]]\n"
45 " -D, --define id[=val] Define preprocessor identifier id=val\n"
46 " --debug=nn Set debug level to 'nn'\n"
47 " -E Preprocess only\n"
48 " -F TARGET Ignored, for compatibility with windres\n"
49 " -fo FILE Synonym for -o for compatibility with windres\n"
50 " -h, --help Prints this summary\n"
51 " -i, --input=FILE The name of the input file\n"
52 " -I, --include-dir=DIR Add dir to the include search path (multiple -I allowed)\n"
53 " -J, --input-format=FORMAT The input format (either `rc' or `rc16')\n"
54 " -l, --language=LANG Set default language to LANG (default is neutral {0, 0})\n"
55 " -m16 Build a 16-bit resource file\n"
56 " --nls-dir=DIR Directory containing the NLS codepage mappings\n"
57 " --no-use-temp-file Ignored for compatibility with windres\n"
58 " --nostdinc Disables searching the standard include path\n"
59 " -o, --output=FILE Output to file (default is infile.res)\n"
60 " -O, --output-format=FORMAT The output format (`po', `pot', `res', or `res16`)\n"
61 " --pedantic Enable pedantic warnings\n"
62 " --po-dir=DIR Directory containing po files for translations\n"
63 " --preprocessor Specifies the preprocessor to use, including arguments\n"
64 " -r Ignored for compatibility with rc\n"
65 " --sysroot=DIR Prefix include paths with DIR\n"
66 " -U, --undefine id Undefine preprocessor identifier id\n"
67 " --use-temp-file Ignored for compatibility with windres\n"
68 " -v, --verbose Enable verbose mode\n"
69 " --verify-translations Check the status of the various translations\n"
70 " --version Print version and exit\n"
71 "Input is taken from stdin if no sourcefile specified.\n"
72 "Debug level 'n' is a bitmask with following meaning:\n"
73 " * 0x01 Tell which resource is parsed (verbose mode)\n"
74 " * 0x02 Dump internal structures\n"
75 " * 0x04 Create a parser trace (yydebug=1)\n"
76 " * 0x08 Preprocessor messages\n"
77 " * 0x10 Preprocessor lex messages\n"
78 " * 0x20 Preprocessor yacc trace\n"
79 "If no input filename is given and the output name is not overridden\n"
80 "with -o, then the output is written to \"wrc.tab.res\"\n"
83 static const char version_string
[] = "Wine Resource Compiler version " PACKAGE_VERSION
"\n"
84 "Copyright 1998-2000 Bertho A. Stultiens\n"
85 " 1994 Martin von Loewis\n";
88 * Set if compiling in 32bit mode (default).
93 * debuglevel == DEBUGLEVEL_NONE Don't bother
94 * debuglevel & DEBUGLEVEL_CHAT Say what's done
95 * debuglevel & DEBUGLEVEL_DUMP Dump internal structures
96 * debuglevel & DEBUGLEVEL_TRACE Create parser trace
97 * debuglevel & DEBUGLEVEL_PPMSG Preprocessor messages
98 * debuglevel & DEBUGLEVEL_PPLEX Preprocessor lex trace
99 * debuglevel & DEBUGLEVEL_PPTRACE Preprocessor yacc trace
101 int debuglevel
= DEBUGLEVEL_NONE
;
104 * Recognize win32 keywords if set (-w 32 enforces this),
105 * otherwise set with -e option.
110 * Language setting for resources (-l option)
112 static language_t defaultlanguage
;
113 language_t currentlanguage
= 0;
116 * Set when extra warnings should be generated (-W option)
121 * Set when _only_ to run the preprocessor (-E option)
123 int preprocess_only
= 0;
126 * Set when _not_ to run the preprocessor (-P cat option)
128 int no_preprocess
= 0;
132 int check_utf8
= 1; /* whether to check for valid utf8 */
134 static char *output_name
; /* The name given by the -o option */
135 const char *input_name
= NULL
; /* The name given on the command-line */
136 static char *temp_name
= NULL
; /* Temporary file for preprocess pipe */
137 static struct strarray input_files
;
139 static int stdinc
= 1;
141 static const char *po_dir
;
142 static const char *sysroot
= "";
143 static const char *includedir
;
144 const char *nlsdirs
[3] = { NULL
, NLSDIR
, NULL
};
146 int line_number
= 1; /* The current line */
147 int char_number
= 1; /* The current char pos within the line */
149 int parser_debug
, yy_flex_debug
;
151 resource_t
*resource_top
; /* The top of the parsed resources */
153 static void cleanup_files(void);
155 enum long_options_values
157 LONG_OPT_NOSTDINC
= 1,
162 LONG_OPT_PREPROCESSOR
,
169 static const char short_options
[] =
170 "b:D:Ef:F:hi:I:J:l:m:o:O:ruU:v";
171 static const struct long_option long_options
[] = {
172 { "debug", 1, LONG_OPT_DEBUG
},
173 { "define", 1, 'D' },
175 { "include-dir", 1, 'I' },
177 { "input-format", 1, 'J' },
178 { "language", 1, 'l' },
179 { "nls-dir", 1, LONG_OPT_NLS_DIR
},
180 { "no-use-temp-file", 0, LONG_OPT_NOTMPFILE
},
181 { "nostdinc", 0, LONG_OPT_NOSTDINC
},
182 { "output", 1, 'o' },
183 { "output-format", 1, 'O' },
184 { "pedantic", 0, LONG_OPT_PEDANTIC
},
185 { "po-dir", 1, LONG_OPT_PO_DIR
},
186 { "preprocessor", 1, LONG_OPT_PREPROCESSOR
},
187 { "sysroot", 1, LONG_OPT_SYSROOT
},
188 { "target", 1, 'F' },
190 { "undefine", 1, 'U' },
191 { "use-temp-file", 0, LONG_OPT_TMPFILE
},
192 { "verbose", 0, 'v' },
193 { "version", 0, LONG_OPT_VERSION
},
197 static void set_version_defines(void)
199 char *version
= xstrdup( PACKAGE_VERSION
);
200 char *major
, *minor
, *patchlevel
;
203 if ((minor
= strchr( version
, '.' )))
207 if ((patchlevel
= strchr( minor
, '.' ))) *patchlevel
++ = 0;
209 else /* pre 0.9 version */
212 patchlevel
= version
;
214 sprintf( buffer
, "__WRC__=%s", major
? major
: "0" );
215 wpp_add_cmdline_define(buffer
);
216 sprintf( buffer
, "__WRC_MINOR__=%s", minor
? minor
: "0" );
217 wpp_add_cmdline_define(buffer
);
218 sprintf( buffer
, "__WRC_PATCHLEVEL__=%s", patchlevel
? patchlevel
: "0" );
219 wpp_add_cmdline_define(buffer
);
223 /* clean things up when aborting on a signal */
224 static void exit_on_signal( int sig
)
226 exit(1); /* this will call the atexit functions */
229 /* load a single input file */
230 static int load_file( const char *input_name
, const char *output_name
)
234 /* Run the preprocessor on the input */
242 * Preprocess the input to a temp-file, or stdout if
243 * no output was given.
250 if (!(output
= fopen( output_name
, "w" )))
251 fatal_perror( "Could not open %s for writing", output_name
);
252 ret
= wpp_parse( input_name
, output
);
255 else ret
= wpp_parse( input_name
, stdout
);
262 fd
= make_temp_file( output_name
, "", &name
);
264 if (!(output
= fdopen(fd
, "wt")))
265 error("Could not open fd %s for writing\n", name
);
267 ret
= wpp_parse( input_name
, output
);
273 /* Reset the language */
274 currentlanguage
= defaultlanguage
;
277 /* Go from .rc to .res */
278 chat("Starting parse\n");
280 if(!(parser_in
= fopen(input_name
, "rb")))
281 fatal_perror("Could not open %s for input", input_name
);
283 ret
= parser_parse();
285 parser_lex_destroy();
294 static void init_argv0_dir( const char *argv0
)
299 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
300 dir
= realpath( "/proc/self/exe", NULL
);
301 #elif defined (__FreeBSD__) || defined(__DragonFly__)
302 static int pathname
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
303 size_t path_size
= PATH_MAX
;
304 char *path
= xmalloc( path_size
);
305 if (!sysctl( pathname
, ARRAY_SIZE(pathname
), path
, &path_size
, NULL
, 0 ))
306 dir
= realpath( path
, NULL
);
309 dir
= realpath( argv0
, NULL
);
312 dir
= get_dirname( dir
);
313 includedir
= strmake( "%s/%s", dir
, BIN_TO_INCLUDEDIR
);
314 if (strendswith( dir
, "/tools/wrc" )) nlsdirs
[0] = strmake( "%s/../../nls", dir
);
315 else nlsdirs
[0] = strmake( "%s/%s", dir
, BIN_TO_NLSDIR
);
319 static void option_callback( int optc
, char *optarg
)
323 case LONG_OPT_NOSTDINC
:
326 case LONG_OPT_TMPFILE
:
327 if (debuglevel
) warning("--use-temp-file option not yet supported, ignored.\n");
329 case LONG_OPT_NOTMPFILE
:
330 if (debuglevel
) warning("--no-use-temp-file option not yet supported, ignored.\n");
332 case LONG_OPT_NLS_DIR
:
333 nlsdirs
[0] = xstrdup( optarg
);
335 case LONG_OPT_PO_DIR
:
336 po_dir
= xstrdup( optarg
);
338 case LONG_OPT_PREPROCESSOR
:
339 if (strcmp(optarg
, "cat") == 0) no_preprocess
= 1;
340 else fprintf(stderr
, "-P option not yet supported, ignored.\n");
342 case LONG_OPT_SYSROOT
:
343 sysroot
= xstrdup( optarg
);
345 case LONG_OPT_VERSION
:
346 printf(version_string
);
350 debuglevel
= strtol(optarg
, NULL
, 0);
352 case LONG_OPT_PEDANTIC
:
356 wpp_add_cmdline_define(optarg
);
368 strarray_add( &input_files
, optarg
);
371 wpp_add_include_path(optarg
);
374 if (strcmp(optarg
, "rc16") == 0) extensions
= 0;
375 else if (strcmp(optarg
, "rc")) error("Output format %s not supported.\n", optarg
);
378 defaultlanguage
= strtol(optarg
, NULL
, 0);
379 if (get_language_codepage( defaultlanguage
) == -1)
380 error("Language %04x is not supported\n", defaultlanguage
);
383 if (!strcmp( optarg
, "16" )) win32
= 0;
387 if (*optarg
!= 'o') error("Unknown option: -f%s\n", optarg
);
391 if (!output_name
) output_name
= xstrdup(optarg
);
392 else error("Too many output files.\n");
395 if (strcmp(optarg
, "po") == 0) po_mode
= 1;
396 else if (strcmp(optarg
, "pot") == 0) po_mode
= 2;
397 else if (strcmp(optarg
, "res16") == 0) win32
= 0;
398 else if (strcmp(optarg
, "res")) warning("Output format %s not supported.\n", optarg
);
401 /* ignored for compatibility with rc */
407 wpp_del_define(optarg
);
410 debuglevel
= DEBUGLEVEL_CHAT
;
413 fprintf(stderr
, "wrc: %s\n\n%s", optarg
, usage
);
418 int main(int argc
,char *argv
[])
422 signal( SIGTERM
, exit_on_signal
);
423 signal( SIGINT
, exit_on_signal
);
425 signal( SIGHUP
, exit_on_signal
);
427 init_argv0_dir( argv
[0] );
429 /* Set the default defined stuff */
430 set_version_defines();
431 wpp_add_cmdline_define("RC_INVOKED=1");
432 /* Microsoft RC always searches current directory */
433 wpp_add_include_path(".");
435 strarray_addall( &input_files
,
436 parse_options( argc
, argv
, short_options
, long_options
, 0, option_callback
));
438 if (win32
) wpp_add_cmdline_define("_WIN32=1");
440 /* If we do need to search standard includes, add them to the path */
443 static const char *incl_dirs
[] = { INCLUDEDIR
, "/usr/include", "/usr/local/include" };
447 wpp_add_include_path( strmake( "%s/wine/msvcrt", includedir
));
448 wpp_add_include_path( strmake( "%s/wine/windows", includedir
));
450 for (i
= 0; i
< ARRAY_SIZE(incl_dirs
); i
++)
452 if (i
&& !strcmp( incl_dirs
[i
], incl_dirs
[0] )) continue;
453 wpp_add_include_path( strmake( "%s%s/wine/msvcrt", sysroot
, incl_dirs
[i
] ));
454 wpp_add_include_path( strmake( "%s%s/wine/windows", sysroot
, incl_dirs
[i
] ));
458 /* Kill io buffering when some kind of debuglevel is enabled */
461 setbuf(stdout
, NULL
);
462 setbuf(stderr
, NULL
);
465 parser_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
466 yy_flex_debug
= debuglevel
& DEBUGLEVEL_TRACE
? 1 : 0;
468 wpp_set_debug( (debuglevel
& DEBUGLEVEL_PPLEX
) != 0,
469 (debuglevel
& DEBUGLEVEL_PPTRACE
) != 0,
470 (debuglevel
& DEBUGLEVEL_PPMSG
) != 0 );
472 atexit(cleanup_files
);
474 for (i
= 0; i
< input_files
.count
; i
++)
476 input_name
= input_files
.str
[i
];
477 if (load_file( input_name
, output_name
)) exit(1);
479 /* stdin special case. NULL means "stdin" for wpp. */
480 if (input_files
.count
== 0 && load_file( NULL
, output_name
)) exit(1);
482 if (!po_mode
&& output_name
)
484 if (strendswith( output_name
, ".po" )) po_mode
= 1;
485 else if (strendswith( output_name
, ".pot" )) po_mode
= 2;
489 if (!win32
) error( "PO files are not supported in 16-bit mode\n" );
490 if (po_mode
== 2) /* pot file */
494 const char *name
= input_files
.count
? get_basename(input_files
.str
[0]) : "wrc.tab";
495 output_name
= replace_extension( name
, ".rc", ".pot" );
497 write_pot_file( output_name
);
499 else write_po_files( output_name
);
503 if (win32
) add_translations( po_dir
);
505 chat("Writing .res-file\n");
508 const char *name
= input_files
.count
? get_basename(input_files
.str
[0]) : "wrc.tab";
509 output_name
= replace_extension( name
, ".rc", ".res" );
511 write_resfile(output_name
, resource_top
);
518 static void cleanup_files(void)
520 if (output_name
) unlink(output_name
);
521 if (temp_name
) unlink(temp_name
);