wrc.c 17 KB

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