wmc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Wine Message Compiler main program
  3. *
  4. * Copyright 2000 Bertho A. Stultiens (BS)
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <signal.h>
  26. #ifdef HAVE_GETOPT_H
  27. # include <getopt.h>
  28. #endif
  29. #include "wmc.h"
  30. #include "utils.h"
  31. #include "lang.h"
  32. #include "write.h"
  33. static const char usage[] =
  34. "Usage: wmc [options...] [inputfile.mc]\n"
  35. " -B x Set output byte-order x={n[ative], l[ittle], b[ig]}\n"
  36. " (default is n[ative] which equals "
  37. #ifdef WORDS_BIGENDIAN
  38. "big"
  39. #else
  40. "little"
  41. #endif
  42. "-endian)\n"
  43. " -c Set 'custom-bit' in values\n"
  44. " -d Use decimal values in output\n"
  45. " -D Set debug flag\n"
  46. " -h, --help Print this message\n"
  47. " -H FILE Write header file to FILE (default is inputfile.h)\n"
  48. " -i Inline messagetable(s)\n"
  49. " --nls-dir=DIR Directory containing the NLS codepage mappings\n"
  50. " -o, --output=FILE Output to FILE (default is infile.rc)\n"
  51. " -O, --output-format=FORMAT The output format (`rc', `res', or `pot')\n"
  52. " -P, --po-dir=DIR Directory containing po files for translations\n"
  53. " -u Input file is in unicode\n"
  54. " -U Output unicode messagetable(s)\n"
  55. " -v Show supported codepages and languages\n"
  56. " -V, --version Print version end exit\n"
  57. " -W, --pedantic Enable pedantic warnings\n"
  58. "Input is taken from stdin if no inputfile is specified.\n"
  59. "Byteorder of unicode input is based upon the first couple of\n"
  60. "bytes read, which should be 0x0000..0x00ff.\n"
  61. ;
  62. static const char version_string[] =
  63. "Wine Message Compiler version " PACKAGE_VERSION "\n"
  64. "Copyright 2000 Bertho A. Stultiens\n"
  65. ;
  66. /*
  67. * The output byte-order of resources (set with -B)
  68. */
  69. int byteorder = WMC_BO_NATIVE;
  70. /*
  71. * Custom bit (bit 29) in output values must be set (-c option)
  72. */
  73. int custombit = 0;
  74. /*
  75. * Output decimal values (-d option)
  76. */
  77. int decimal = 0;
  78. /*
  79. * Enable pedantic warnings; check arg references (-W option)
  80. */
  81. int pedantic = 0;
  82. /*
  83. * Unicode input (-u option)
  84. */
  85. int unicodein = 0;
  86. /*
  87. * Inline the messagetables (don't write *.bin files; -i option)
  88. */
  89. int rcinline = 0;
  90. /*
  91. * Debugging flag (-D option)
  92. */
  93. static int dodebug = 0;
  94. static char *po_dir;
  95. char *output_name = NULL; /* The name given by the -o option */
  96. char *input_name = NULL; /* The name given on the command-line */
  97. char *header_name = NULL; /* The name given by the -H option */
  98. const char *nlsdirs[3] = { NULL, NLSDIR, NULL };
  99. int line_number = 1; /* The current line */
  100. int char_number = 1; /* The current char pos within the line */
  101. char *cmdline; /* The entire commandline */
  102. time_t now; /* The time of start of wmc */
  103. int mcy_debug;
  104. FILE *yyin;
  105. static enum
  106. {
  107. FORMAT_UNKNOWN,
  108. FORMAT_RC,
  109. FORMAT_RES,
  110. FORMAT_POT
  111. } output_format;
  112. enum long_options_values
  113. {
  114. LONG_OPT_NLS_DIR = 1,
  115. };
  116. static const char short_options[] = "B:cdDhH:io:O:P:uUvVW";
  117. static const struct option long_options[] =
  118. {
  119. { "help", 0, NULL, 'h' },
  120. { "nls-dir", 1, NULL, LONG_OPT_NLS_DIR },
  121. { "output", 1, NULL, 'o' },
  122. { "output-format", 1, NULL, 'O' },
  123. { "pedantic", 0, NULL, 'W' },
  124. { "po-dir", 1, NULL, 'P' },
  125. { "version", 0, NULL, 'v' }
  126. };
  127. static void segvhandler(int sig);
  128. static void cleanup_files(void)
  129. {
  130. if (output_name) unlink( output_name );
  131. if (header_name) unlink( header_name );
  132. }
  133. static void exit_on_signal( int sig )
  134. {
  135. exit(1); /* this will call the atexit functions */
  136. }
  137. static void init_argv0_dir( const char *argv0 )
  138. {
  139. #ifndef _WIN32
  140. char *p, *dir;
  141. #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
  142. dir = realpath( "/proc/self/exe", NULL );
  143. #elif defined (__FreeBSD__) || defined(__DragonFly__)
  144. dir = realpath( "/proc/curproc/file", NULL );
  145. #else
  146. dir = realpath( argv0, NULL );
  147. #endif
  148. if (!dir) return;
  149. if (!(p = strrchr( dir, '/' ))) return;
  150. if (p == dir) p++;
  151. *p = 0;
  152. if (strendswith( dir, "/tools/wmc" )) nlsdirs[0] = strmake( "%s/../../nls", dir );
  153. else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR );
  154. free( dir );
  155. #endif
  156. }
  157. int main(int argc,char *argv[])
  158. {
  159. int optc;
  160. int opti = 0;
  161. int lose = 0;
  162. int ret;
  163. int i;
  164. int cmdlen;
  165. atexit( cleanup_files );
  166. signal(SIGSEGV, segvhandler);
  167. signal( SIGTERM, exit_on_signal );
  168. signal( SIGINT, exit_on_signal );
  169. #ifdef SIGHUP
  170. signal( SIGHUP, exit_on_signal );
  171. #endif
  172. init_argv0_dir( argv[0] );
  173. /* First rebuild the commandline to put in destination */
  174. /* Could be done through env[], but not all OS-es support it */
  175. cmdlen = 5; /* for "wmc " and \0 */
  176. for(i = 1; i < argc; i++)
  177. cmdlen += strlen(argv[i]) + 1;
  178. cmdline = xmalloc(cmdlen);
  179. strcpy(cmdline, "wmc ");
  180. for(i = 1; i < argc; i++)
  181. {
  182. strcat(cmdline, argv[i]);
  183. if(i < argc-1)
  184. strcat(cmdline, " ");
  185. }
  186. while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF)
  187. {
  188. switch(optc)
  189. {
  190. case 'B':
  191. switch(optarg[0])
  192. {
  193. case 'n':
  194. case 'N':
  195. byteorder = WMC_BO_NATIVE;
  196. break;
  197. case 'l':
  198. case 'L':
  199. byteorder = WMC_BO_LITTLE;
  200. break;
  201. case 'b':
  202. case 'B':
  203. byteorder = WMC_BO_BIG;
  204. break;
  205. default:
  206. fprintf(stderr, "Byteordering must be n[ative], l[ittle] or b[ig]\n");
  207. lose++;
  208. }
  209. break;
  210. case 'c':
  211. custombit = 1;
  212. break;
  213. case 'd':
  214. decimal = 1;
  215. break;
  216. case 'D':
  217. dodebug = 1;
  218. break;
  219. case 'h':
  220. printf("%s", usage);
  221. exit(0);
  222. /* No return */
  223. case 'H':
  224. header_name = xstrdup(optarg);
  225. break;
  226. case 'i':
  227. rcinline = 1;
  228. break;
  229. case 'o':
  230. output_name = xstrdup(optarg);
  231. break;
  232. case 'O':
  233. if (!strcmp( optarg, "rc" )) output_format = FORMAT_RC;
  234. else if (!strcmp( optarg, "res" )) output_format = FORMAT_RES;
  235. else if (!strcmp( optarg, "pot" )) output_format = FORMAT_POT;
  236. else
  237. {
  238. fprintf(stderr, "Output format must be rc or res\n" );
  239. lose++;
  240. }
  241. break;
  242. case 'P':
  243. po_dir = xstrdup( optarg );
  244. break;
  245. case 'u':
  246. unicodein = 1;
  247. break;
  248. case 'U': /* ignored for backwards compatibility */
  249. break;
  250. case 'v':
  251. show_languages();
  252. exit(0);
  253. /* No return */
  254. case 'V':
  255. printf(version_string);
  256. exit(0);
  257. /* No return */
  258. case 'W':
  259. pedantic = 1;
  260. break;
  261. case LONG_OPT_NLS_DIR:
  262. nlsdirs[0] = xstrdup( optarg );
  263. break;
  264. default:
  265. lose++;
  266. break;
  267. }
  268. }
  269. if(lose)
  270. {
  271. fprintf(stderr, "%s", usage);
  272. return 1;
  273. }
  274. mcy_debug = dodebug;
  275. if(dodebug)
  276. {
  277. setbuf(stdout, NULL);
  278. setbuf(stderr, NULL);
  279. }
  280. /* Check for input file on command-line */
  281. if(optind < argc)
  282. {
  283. input_name = argv[optind];
  284. }
  285. /* Guess output format */
  286. if (output_format == FORMAT_UNKNOWN)
  287. {
  288. if (output_name && strendswith( output_name, ".res" )) output_format = FORMAT_RES;
  289. else if (output_name && strendswith( output_name, ".pot" )) output_format = FORMAT_POT;
  290. else output_format = FORMAT_RC;
  291. }
  292. /* Generate appropriate outfile names */
  293. if(!output_name)
  294. {
  295. output_name = dup_basename(input_name, ".mc");
  296. strcat(output_name, ".rc");
  297. }
  298. if(!header_name)
  299. {
  300. header_name = dup_basename(input_name, ".mc");
  301. strcat(header_name, ".h");
  302. }
  303. if(input_name)
  304. {
  305. if(!(yyin = fopen(input_name, "rb")))
  306. error("Could not open %s for input\n", input_name);
  307. }
  308. else
  309. yyin = stdin;
  310. ret = mcy_parse();
  311. if(input_name)
  312. fclose(yyin);
  313. if(ret)
  314. {
  315. /* Error during parse */
  316. exit(1);
  317. }
  318. #ifdef WORDS_BIGENDIAN
  319. byte_swapped = (byteorder == WMC_BO_LITTLE);
  320. #else
  321. byte_swapped = (byteorder == WMC_BO_BIG);
  322. #endif
  323. switch (output_format)
  324. {
  325. case FORMAT_RC:
  326. write_h_file(header_name);
  327. write_rc_file(output_name);
  328. if(!rcinline)
  329. write_bin_files();
  330. break;
  331. case FORMAT_RES:
  332. add_translations( po_dir );
  333. write_res_file( output_name );
  334. break;
  335. case FORMAT_POT:
  336. write_pot_file( output_name );
  337. break;
  338. default:
  339. break;
  340. }
  341. output_name = NULL;
  342. header_name = NULL;
  343. return 0;
  344. }
  345. static void segvhandler(int sig)
  346. {
  347. fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
  348. fflush(stdout);
  349. fflush(stderr);
  350. abort();
  351. }