wrc: Use ARRAY_SIZE instead of open coding it.
[wine.git] / tools / wrc / wrc.c
blob2448fff11eb123a8b9f74d47b601e1c0b8ac2202
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 <signal.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SYSCTL_H
33 # include <sys/sysctl.h>
34 #endif
36 #include "../tools.h"
37 #include "wrc.h"
38 #include "utils.h"
39 #include "newstruc.h"
40 #include "parser.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).
90 int win32 = 1;
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.
107 int extensions = 1;
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)
118 int pedantic = 0;
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;
130 int utf8_input = 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;
140 static int po_mode;
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,
158 LONG_OPT_TMPFILE,
159 LONG_OPT_NOTMPFILE,
160 LONG_OPT_NLS_DIR,
161 LONG_OPT_PO_DIR,
162 LONG_OPT_PREPROCESSOR,
163 LONG_OPT_SYSROOT,
164 LONG_OPT_VERSION,
165 LONG_OPT_DEBUG,
166 LONG_OPT_PEDANTIC,
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' },
174 { "help", 0, 'h' },
175 { "include-dir", 1, 'I' },
176 { "input", 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' },
189 { "utf8", 0, 'u' },
190 { "undefine", 1, 'U' },
191 { "use-temp-file", 0, LONG_OPT_TMPFILE },
192 { "verbose", 0, 'v' },
193 { "version", 0, LONG_OPT_VERSION },
194 { NULL }
197 static void set_version_defines(void)
199 char *version = xstrdup( PACKAGE_VERSION );
200 char *major, *minor, *patchlevel;
201 char buffer[100];
203 if ((minor = strchr( version, '.' )))
205 major = version;
206 *minor++ = 0;
207 if ((patchlevel = strchr( minor, '.' ))) *patchlevel++ = 0;
209 else /* pre 0.9 version */
211 major = NULL;
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);
220 free( version );
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 )
232 int ret;
234 /* Run the preprocessor on the input */
235 if(!no_preprocess)
237 FILE *output;
238 int ret, fd;
239 char *name;
242 * Preprocess the input to a temp-file, or stdout if
243 * no output was given.
246 if (preprocess_only)
248 if (output_name)
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 );
253 fclose( output );
255 else ret = wpp_parse( input_name, stdout );
257 if (ret) return ret;
258 output_name = NULL;
259 exit(0);
262 fd = make_temp_file( output_name, "", &name );
263 temp_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 );
268 fclose( output );
269 if (ret) return ret;
270 input_name = name;
273 /* Reset the language */
274 currentlanguage = defaultlanguage;
275 check_utf8 = 1;
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();
284 fclose(parser_in);
285 parser_lex_destroy();
286 if (temp_name)
288 unlink( temp_name );
289 temp_name = NULL;
291 return ret;
294 static void init_argv0_dir( const char *argv0 )
296 #ifndef _WIN32
297 char *dir;
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 );
307 free( path );
308 #else
309 dir = realpath( argv0, NULL );
310 #endif
311 if (!dir) return;
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 );
316 #endif
319 static void option_callback( int optc, char *optarg )
321 switch(optc)
323 case LONG_OPT_NOSTDINC:
324 stdinc = 0;
325 break;
326 case LONG_OPT_TMPFILE:
327 if (debuglevel) warning("--use-temp-file option not yet supported, ignored.\n");
328 break;
329 case LONG_OPT_NOTMPFILE:
330 if (debuglevel) warning("--no-use-temp-file option not yet supported, ignored.\n");
331 break;
332 case LONG_OPT_NLS_DIR:
333 nlsdirs[0] = xstrdup( optarg );
334 break;
335 case LONG_OPT_PO_DIR:
336 po_dir = xstrdup( optarg );
337 break;
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");
341 break;
342 case LONG_OPT_SYSROOT:
343 sysroot = xstrdup( optarg );
344 break;
345 case LONG_OPT_VERSION:
346 printf(version_string);
347 exit(0);
348 break;
349 case LONG_OPT_DEBUG:
350 debuglevel = strtol(optarg, NULL, 0);
351 break;
352 case LONG_OPT_PEDANTIC:
353 pedantic = 1;
354 break;
355 case 'D':
356 wpp_add_cmdline_define(optarg);
357 break;
358 case 'E':
359 preprocess_only = 1;
360 break;
361 case 'b':
362 case 'F':
363 break;
364 case 'h':
365 printf(usage);
366 exit(0);
367 case 'i':
368 strarray_add( &input_files, optarg );
369 break;
370 case 'I':
371 wpp_add_include_path(optarg);
372 break;
373 case 'J':
374 if (strcmp(optarg, "rc16") == 0) extensions = 0;
375 else if (strcmp(optarg, "rc")) error("Output format %s not supported.\n", optarg);
376 break;
377 case 'l':
378 defaultlanguage = strtol(optarg, NULL, 0);
379 if (get_language_codepage( defaultlanguage ) == -1)
380 error("Language %04x is not supported\n", defaultlanguage);
381 break;
382 case 'm':
383 if (!strcmp( optarg, "16" )) win32 = 0;
384 else win32 = 1;
385 break;
386 case 'f':
387 if (*optarg != 'o') error("Unknown option: -f%s\n", optarg);
388 optarg++;
389 /* fall through */
390 case 'o':
391 if (!output_name) output_name = xstrdup(optarg);
392 else error("Too many output files.\n");
393 break;
394 case 'O':
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);
399 break;
400 case 'r':
401 /* ignored for compatibility with rc */
402 break;
403 case 'u':
404 utf8_input = 1;
405 break;
406 case 'U':
407 wpp_del_define(optarg);
408 break;
409 case 'v':
410 debuglevel = DEBUGLEVEL_CHAT;
411 break;
412 case '?':
413 fprintf(stderr, "wrc: %s\n\n%s", optarg, usage);
414 exit(1);
418 int main(int argc,char *argv[])
420 int i;
422 signal( SIGTERM, exit_on_signal );
423 signal( SIGINT, exit_on_signal );
424 #ifdef SIGHUP
425 signal( SIGHUP, exit_on_signal );
426 #endif
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 */
441 if (stdinc)
443 static const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
445 if (includedir)
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 */
459 if(debuglevel)
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;
487 if (po_mode)
489 if (!win32) error( "PO files are not supported in 16-bit mode\n" );
490 if (po_mode == 2) /* pot file */
492 if (!output_name)
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 );
500 output_name = NULL;
501 exit(0);
503 if (win32) add_translations( po_dir );
505 chat("Writing .res-file\n");
506 if (!output_name)
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);
512 output_name = NULL;
514 return 0;
518 static void cleanup_files(void)
520 if (output_name) unlink(output_name);
521 if (temp_name) unlink(temp_name);