wmc.c 8.6 KB

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