gettext.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* gettext.c - gettext module */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/dl.h>
  24. #include <grub/normal.h>
  25. #include <grub/file.h>
  26. #include <grub/kernel.h>
  27. #include <grub/i18n.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. /*
  30. .mo file information from:
  31. http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html .
  32. */
  33. static const char *(*grub_gettext_original) (const char *s);
  34. struct grub_gettext_msg
  35. {
  36. char *name;
  37. char *translated;
  38. };
  39. struct header
  40. {
  41. grub_uint32_t magic;
  42. grub_uint32_t version;
  43. grub_uint32_t number_of_strings;
  44. grub_uint32_t offset_original;
  45. grub_uint32_t offset_translation;
  46. };
  47. struct string_descriptor
  48. {
  49. grub_uint32_t length;
  50. grub_uint32_t offset;
  51. };
  52. struct grub_gettext_context
  53. {
  54. grub_file_t fd_mo;
  55. grub_off_t grub_gettext_offset_original;
  56. grub_off_t grub_gettext_offset_translation;
  57. grub_size_t grub_gettext_max;
  58. int grub_gettext_max_log;
  59. struct grub_gettext_msg *grub_gettext_msg_list;
  60. };
  61. static struct grub_gettext_context main_context, secondary_context;
  62. #define MO_MAGIC_NUMBER 0x950412de
  63. static grub_err_t
  64. grub_gettext_pread (grub_file_t file, void *buf, grub_size_t len,
  65. grub_off_t offset)
  66. {
  67. if (len == 0)
  68. return GRUB_ERR_NONE;
  69. if (grub_file_seek (file, offset) == (grub_off_t) - 1)
  70. return grub_errno;
  71. if (grub_file_read (file, buf, len) != (grub_ssize_t) len)
  72. {
  73. if (!grub_errno)
  74. grub_error (GRUB_ERR_READ_ERROR, N_("premature end of file"));
  75. return grub_errno;
  76. }
  77. return GRUB_ERR_NONE;
  78. }
  79. static char *
  80. grub_gettext_getstr_from_position (struct grub_gettext_context *ctx,
  81. grub_off_t off,
  82. grub_size_t position)
  83. {
  84. grub_off_t internal_position;
  85. grub_size_t length;
  86. grub_off_t offset;
  87. char *translation;
  88. struct string_descriptor desc;
  89. grub_err_t err;
  90. internal_position = (off + position * sizeof (desc));
  91. err = grub_gettext_pread (ctx->fd_mo, (char *) &desc,
  92. sizeof (desc), internal_position);
  93. if (err)
  94. return NULL;
  95. length = grub_cpu_to_le32 (desc.length);
  96. offset = grub_cpu_to_le32 (desc.offset);
  97. translation = grub_malloc (length + 1);
  98. if (!translation)
  99. return NULL;
  100. err = grub_gettext_pread (ctx->fd_mo, translation, length, offset);
  101. if (err)
  102. {
  103. grub_free (translation);
  104. return NULL;
  105. }
  106. translation[length] = '\0';
  107. return translation;
  108. }
  109. static const char *
  110. grub_gettext_gettranslation_from_position (struct grub_gettext_context *ctx,
  111. grub_size_t position)
  112. {
  113. if (!ctx->grub_gettext_msg_list[position].translated)
  114. ctx->grub_gettext_msg_list[position].translated
  115. = grub_gettext_getstr_from_position (ctx,
  116. ctx->grub_gettext_offset_translation,
  117. position);
  118. return ctx->grub_gettext_msg_list[position].translated;
  119. }
  120. static const char *
  121. grub_gettext_getstring_from_position (struct grub_gettext_context *ctx,
  122. grub_size_t position)
  123. {
  124. if (!ctx->grub_gettext_msg_list[position].name)
  125. ctx->grub_gettext_msg_list[position].name
  126. = grub_gettext_getstr_from_position (ctx,
  127. ctx->grub_gettext_offset_original,
  128. position);
  129. return ctx->grub_gettext_msg_list[position].name;
  130. }
  131. static const char *
  132. grub_gettext_translate_real (struct grub_gettext_context *ctx,
  133. const char *orig)
  134. {
  135. grub_size_t current = 0;
  136. int i;
  137. const char *current_string;
  138. static int depth = 0;
  139. if (!ctx->grub_gettext_msg_list || !ctx->fd_mo)
  140. return NULL;
  141. /* Shouldn't happen. Just a precaution if our own code
  142. calls gettext somehow. */
  143. if (depth > 2)
  144. return NULL;
  145. depth++;
  146. /* Make sure we can use grub_gettext_translate for error messages. Push
  147. active error message to error stack and reset error message. */
  148. grub_error_push ();
  149. for (i = ctx->grub_gettext_max_log; i >= 0; i--)
  150. {
  151. grub_size_t test;
  152. int cmp;
  153. test = current | (1 << i);
  154. if (test >= ctx->grub_gettext_max)
  155. continue;
  156. current_string = grub_gettext_getstring_from_position (ctx, test);
  157. if (!current_string)
  158. {
  159. grub_errno = GRUB_ERR_NONE;
  160. grub_error_pop ();
  161. depth--;
  162. return NULL;
  163. }
  164. /* Search by bisection. */
  165. cmp = grub_strcmp (current_string, orig);
  166. if (cmp <= 0)
  167. current = test;
  168. if (cmp == 0)
  169. {
  170. const char *ret = 0;
  171. ret = grub_gettext_gettranslation_from_position (ctx, current);
  172. if (!ret)
  173. {
  174. grub_errno = GRUB_ERR_NONE;
  175. grub_error_pop ();
  176. depth--;
  177. return NULL;
  178. }
  179. grub_error_pop ();
  180. depth--;
  181. return ret;
  182. }
  183. }
  184. if (current == 0 && ctx->grub_gettext_max != 0)
  185. {
  186. current_string = grub_gettext_getstring_from_position (ctx, 0);
  187. if (!current_string)
  188. {
  189. grub_errno = GRUB_ERR_NONE;
  190. grub_error_pop ();
  191. depth--;
  192. return NULL;
  193. }
  194. if (grub_strcmp (current_string, orig) == 0)
  195. {
  196. const char *ret = 0;
  197. ret = grub_gettext_gettranslation_from_position (ctx, current);
  198. if (!ret)
  199. {
  200. grub_errno = GRUB_ERR_NONE;
  201. grub_error_pop ();
  202. depth--;
  203. return NULL;
  204. }
  205. grub_error_pop ();
  206. depth--;
  207. return ret;
  208. }
  209. }
  210. grub_error_pop ();
  211. depth--;
  212. return NULL;
  213. }
  214. static const char *
  215. grub_gettext_translate (const char *orig)
  216. {
  217. const char *ret;
  218. if (orig[0] == 0)
  219. return orig;
  220. ret = grub_gettext_translate_real (&main_context, orig);
  221. if (ret)
  222. return ret;
  223. ret = grub_gettext_translate_real (&secondary_context, orig);
  224. if (ret)
  225. return ret;
  226. return orig;
  227. }
  228. static void
  229. grub_gettext_delete_list (struct grub_gettext_context *ctx)
  230. {
  231. struct grub_gettext_msg *l = ctx->grub_gettext_msg_list;
  232. grub_size_t i;
  233. if (!l)
  234. return;
  235. ctx->grub_gettext_msg_list = 0;
  236. for (i = 0; i < ctx->grub_gettext_max; i++)
  237. grub_free (l[i].name);
  238. /* Don't delete the translated message because could be in use. */
  239. grub_free (l);
  240. if (ctx->fd_mo)
  241. grub_file_close (ctx->fd_mo);
  242. ctx->fd_mo = 0;
  243. grub_memset (ctx, 0, sizeof (*ctx));
  244. }
  245. /* This is similar to grub_file_open. */
  246. static grub_err_t
  247. grub_mofile_open (struct grub_gettext_context *ctx,
  248. const char *filename)
  249. {
  250. struct header head;
  251. grub_err_t err;
  252. grub_file_t fd;
  253. /* Using fd_mo and not another variable because
  254. it's needed for grub_gettext_get_info. */
  255. fd = grub_file_open (filename);
  256. if (!fd)
  257. return grub_errno;
  258. err = grub_gettext_pread (fd, &head, sizeof (head), 0);
  259. if (err)
  260. {
  261. grub_file_close (fd);
  262. return err;
  263. }
  264. if (head.magic != grub_cpu_to_le32_compile_time (MO_MAGIC_NUMBER))
  265. {
  266. grub_file_close (fd);
  267. return grub_error (GRUB_ERR_BAD_FILE_TYPE,
  268. "mo: invalid mo magic in file: %s", filename);
  269. }
  270. if (head.version != 0)
  271. {
  272. grub_file_close (fd);
  273. return grub_error (GRUB_ERR_BAD_FILE_TYPE,
  274. "mo: invalid mo version in file: %s", filename);
  275. }
  276. ctx->grub_gettext_offset_original = grub_le_to_cpu32 (head.offset_original);
  277. ctx->grub_gettext_offset_translation = grub_le_to_cpu32 (head.offset_translation);
  278. ctx->grub_gettext_max = grub_le_to_cpu32 (head.number_of_strings);
  279. for (ctx->grub_gettext_max_log = 0; ctx->grub_gettext_max >> ctx->grub_gettext_max_log;
  280. ctx->grub_gettext_max_log++);
  281. ctx->grub_gettext_msg_list = grub_zalloc (ctx->grub_gettext_max
  282. * sizeof (ctx->grub_gettext_msg_list[0]));
  283. if (!ctx->grub_gettext_msg_list)
  284. {
  285. grub_file_close (fd);
  286. return grub_errno;
  287. }
  288. ctx->fd_mo = fd;
  289. if (grub_gettext != grub_gettext_translate)
  290. {
  291. grub_gettext_original = grub_gettext;
  292. grub_gettext = grub_gettext_translate;
  293. }
  294. return 0;
  295. }
  296. /* Returning grub_file_t would be more natural, but grub_mofile_open assigns
  297. to fd_mo anyway ... */
  298. static grub_err_t
  299. grub_mofile_open_lang (struct grub_gettext_context *ctx,
  300. const char *part1, const char *part2, const char *locale)
  301. {
  302. char *mo_file;
  303. grub_err_t err;
  304. /* mo_file e.g.: /boot/grub/locale/ca.mo */
  305. mo_file = grub_xasprintf ("%s%s/%s.mo", part1, part2, locale);
  306. if (!mo_file)
  307. return grub_errno;
  308. err = grub_mofile_open (ctx, mo_file);
  309. grub_free (mo_file);
  310. /* Will try adding .gz as well. */
  311. if (err)
  312. {
  313. grub_errno = GRUB_ERR_NONE;
  314. mo_file = grub_xasprintf ("%s%s/%s.mo.gz", part1, part2, locale);
  315. if (!mo_file)
  316. return grub_errno;
  317. err = grub_mofile_open (ctx, mo_file);
  318. grub_free (mo_file);
  319. }
  320. /* Will try adding .gmo as well. */
  321. if (err)
  322. {
  323. grub_errno = GRUB_ERR_NONE;
  324. mo_file = grub_xasprintf ("%s%s/%s.gmo", part1, part2, locale);
  325. if (!mo_file)
  326. return grub_errno;
  327. err = grub_mofile_open (ctx, mo_file);
  328. grub_free (mo_file);
  329. }
  330. return err;
  331. }
  332. static grub_err_t
  333. grub_gettext_init_ext (struct grub_gettext_context *ctx,
  334. const char *locale,
  335. const char *locale_dir, const char *prefix)
  336. {
  337. const char *part1, *part2;
  338. grub_err_t err;
  339. grub_gettext_delete_list (ctx);
  340. if (!locale || locale[0] == 0)
  341. return 0;
  342. part1 = locale_dir;
  343. part2 = "";
  344. if (!part1 || part1[0] == 0)
  345. {
  346. part1 = prefix;
  347. part2 = "/locale";
  348. }
  349. if (!part1 || part1[0] == 0)
  350. return 0;
  351. err = grub_mofile_open_lang (ctx, part1, part2, locale);
  352. /* ll_CC didn't work, so try ll. */
  353. if (err)
  354. {
  355. char *lang = grub_strdup (locale);
  356. char *underscore = lang ? grub_strchr (lang, '_') : 0;
  357. if (underscore)
  358. {
  359. *underscore = '\0';
  360. grub_errno = GRUB_ERR_NONE;
  361. err = grub_mofile_open_lang (ctx, part1, part2, lang);
  362. }
  363. grub_free (lang);
  364. }
  365. if (locale[0] == 'e' && locale[1] == 'n'
  366. && (locale[2] == '\0' || locale[2] == '_'))
  367. grub_errno = err = GRUB_ERR_NONE;
  368. return err;
  369. }
  370. static char *
  371. grub_gettext_env_write_lang (struct grub_env_var *var
  372. __attribute__ ((unused)), const char *val)
  373. {
  374. grub_err_t err;
  375. err = grub_gettext_init_ext (&main_context, val, grub_env_get ("locale_dir"),
  376. grub_env_get ("prefix"));
  377. if (err)
  378. grub_print_error ();
  379. err = grub_gettext_init_ext (&secondary_context, val,
  380. grub_env_get ("secondary_locale_dir"), 0);
  381. if (err)
  382. grub_print_error ();
  383. return grub_strdup (val);
  384. }
  385. void
  386. grub_gettext_reread_prefix (const char *val)
  387. {
  388. grub_err_t err;
  389. err = grub_gettext_init_ext (&main_context, grub_env_get ("lang"),
  390. grub_env_get ("locale_dir"),
  391. val);
  392. if (err)
  393. grub_print_error ();
  394. }
  395. static char *
  396. read_main (struct grub_env_var *var
  397. __attribute__ ((unused)), const char *val)
  398. {
  399. grub_err_t err;
  400. err = grub_gettext_init_ext (&main_context, grub_env_get ("lang"), val,
  401. grub_env_get ("prefix"));
  402. if (err)
  403. grub_print_error ();
  404. return grub_strdup (val);
  405. }
  406. static char *
  407. read_secondary (struct grub_env_var *var
  408. __attribute__ ((unused)), const char *val)
  409. {
  410. grub_err_t err;
  411. err = grub_gettext_init_ext (&secondary_context, grub_env_get ("lang"), val,
  412. 0);
  413. if (err)
  414. grub_print_error ();
  415. return grub_strdup (val);
  416. }
  417. static grub_err_t
  418. grub_cmd_translate (grub_command_t cmd __attribute__ ((unused)),
  419. int argc, char **args)
  420. {
  421. if (argc != 1)
  422. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  423. const char *translation;
  424. translation = grub_gettext_translate (args[0]);
  425. grub_printf ("%s\n", translation);
  426. return 0;
  427. }
  428. GRUB_MOD_INIT (gettext)
  429. {
  430. const char *lang;
  431. grub_err_t err;
  432. lang = grub_env_get ("lang");
  433. err = grub_gettext_init_ext (&main_context, lang, grub_env_get ("locale_dir"),
  434. grub_env_get ("prefix"));
  435. if (err)
  436. grub_print_error ();
  437. err = grub_gettext_init_ext (&secondary_context, lang,
  438. grub_env_get ("secondary_locale_dir"), 0);
  439. if (err)
  440. grub_print_error ();
  441. grub_register_variable_hook ("locale_dir", NULL, read_main);
  442. grub_register_variable_hook ("secondary_locale_dir", NULL, read_secondary);
  443. grub_register_command_p1 ("gettext", grub_cmd_translate,
  444. N_("STRING"),
  445. /* TRANSLATORS: It refers to passing the string through gettext.
  446. So it's "translate" in the same meaning as in what you're
  447. doing now.
  448. */
  449. N_("Translates the string with the current settings."));
  450. /* Reload .mo file information if lang changes. */
  451. grub_register_variable_hook ("lang", NULL, grub_gettext_env_write_lang);
  452. /* Preserve hooks after context changes. */
  453. grub_env_export ("lang");
  454. grub_env_export ("locale_dir");
  455. grub_env_export ("secondary_locale_dir");
  456. }
  457. GRUB_MOD_FINI (gettext)
  458. {
  459. grub_gettext_delete_list (&main_context);
  460. grub_gettext_delete_list (&secondary_context);
  461. grub_gettext = grub_gettext_original;
  462. }