2009-11-13 20:39:33 +00:00
|
|
|
/* gettext.c - gettext module */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
#include <grub/list.h>
|
2009-11-13 20:39:33 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/normal.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/kernel.h>
|
2009-11-24 21:42:14 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2009-12-23 16:41:32 +00:00
|
|
|
/*
|
|
|
|
.mo file information from:
|
2009-11-13 20:39:33 +00:00
|
|
|
http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html .
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static grub_file_t fd_mo;
|
|
|
|
|
|
|
|
static int grub_gettext_offsetoriginal;
|
|
|
|
static int grub_gettext_max;
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static const char *(*grub_gettext_original) (const char *s);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
struct grub_gettext_msg
|
|
|
|
{
|
|
|
|
struct grub_gettext_msg *next;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
const char *translated;
|
|
|
|
};
|
|
|
|
|
2011-03-23 11:05:13 +00:00
|
|
|
static struct grub_gettext_msg *grub_gettext_msg_list = NULL;
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
#define GETTEXT_MAGIC_NUMBER 0
|
|
|
|
#define GETTEXT_FILE_FORMAT 4
|
|
|
|
#define GETTEXT_NUMBER_OF_STRINGS 8
|
|
|
|
#define GETTEXT_OFFSET_ORIGINAL 12
|
|
|
|
#define GETTEXT_OFFSET_TRANSLATION 16
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
#define MO_MAGIC_NUMBER 0x950412de
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 12:50:46 +00:00
|
|
|
static grub_ssize_t
|
|
|
|
grub_gettext_pread (grub_file_t file, void *buf, grub_size_t len,
|
|
|
|
grub_off_t offset)
|
|
|
|
{
|
|
|
|
if (grub_file_seek (file, offset) == (grub_off_t) - 1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return grub_file_read (file, buf, len);
|
|
|
|
}
|
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
static grub_uint32_t
|
|
|
|
grub_gettext_get_info (int offset)
|
|
|
|
{
|
|
|
|
grub_uint32_t value;
|
|
|
|
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, (char *) &value, 4, offset);
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
value = grub_cpu_to_le32 (value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_gettext_getstring_from_offset (grub_uint32_t offset,
|
|
|
|
grub_uint32_t length, char *translation)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, translation, length, offset);
|
2009-11-13 20:39:33 +00:00
|
|
|
translation[length] = '\0';
|
|
|
|
}
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
static const char *
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext_gettranslation_from_position (int position)
|
|
|
|
{
|
|
|
|
int offsettranslation;
|
|
|
|
int internal_position;
|
|
|
|
grub_uint32_t length, offset;
|
|
|
|
char *translation;
|
|
|
|
|
|
|
|
offsettranslation = grub_gettext_get_info (GETTEXT_OFFSET_TRANSLATION);
|
|
|
|
|
|
|
|
internal_position = offsettranslation + position * 8;
|
|
|
|
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, (char *) &length, 4, internal_position);
|
2009-11-13 20:39:33 +00:00
|
|
|
length = grub_cpu_to_le32 (length);
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, (char *) &offset, 4, internal_position + 4);
|
2009-11-13 20:39:33 +00:00
|
|
|
offset = grub_cpu_to_le32 (offset);
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
translation = grub_malloc (length + 1);
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext_getstring_from_offset (offset, length, translation);
|
|
|
|
|
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static char *
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext_getstring_from_position (int position)
|
|
|
|
{
|
|
|
|
int internal_position;
|
|
|
|
int length, offset;
|
|
|
|
char *original;
|
|
|
|
|
|
|
|
/* Get position for string i. */
|
|
|
|
internal_position = grub_gettext_offsetoriginal + (position * 8);
|
|
|
|
|
|
|
|
/* Get the length of the string i. */
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, (char *) &length, 4, internal_position);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
/* Get the offset of the string i. */
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (fd_mo, (char *) &offset, 4, internal_position + 4);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
/* Get the string i. */
|
|
|
|
original = grub_malloc (length + 1);
|
|
|
|
grub_gettext_getstring_from_offset (offset, length, original);
|
|
|
|
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static const char *
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext_translate (const char *orig)
|
|
|
|
{
|
|
|
|
char *current_string;
|
2009-12-05 19:03:27 +00:00
|
|
|
const char *ret;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
int min, max, current;
|
2009-12-05 19:03:27 +00:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
struct grub_gettext_msg *cur;
|
|
|
|
|
2010-01-14 22:31:06 +00:00
|
|
|
/* Make sure we can use grub_gettext_translate for error messages. Push
|
|
|
|
active error message to error stack and reset error message. */
|
|
|
|
grub_error_push ();
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_gettext_msg_list),
|
|
|
|
orig);
|
|
|
|
|
|
|
|
if (cur)
|
2010-01-14 22:31:06 +00:00
|
|
|
{
|
|
|
|
grub_error_pop ();
|
|
|
|
return cur->translated;
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
if (fd_mo == 0)
|
2010-01-14 22:31:06 +00:00
|
|
|
{
|
|
|
|
grub_error_pop ();
|
|
|
|
return orig;
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = grub_gettext_max;
|
|
|
|
|
|
|
|
current = (max + min) / 2;
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
while (current != min && current != max && found == 0)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
current_string = grub_gettext_getstring_from_position (current);
|
|
|
|
|
|
|
|
/* Search by bisection. */
|
|
|
|
if (grub_strcmp (current_string, orig) < 0)
|
2009-11-22 11:49:54 +00:00
|
|
|
{
|
|
|
|
grub_free (current_string);
|
|
|
|
min = current;
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
else if (grub_strcmp (current_string, orig) > 0)
|
2009-11-22 11:49:54 +00:00
|
|
|
{
|
|
|
|
grub_free (current_string);
|
|
|
|
max = current;
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
else if (grub_strcmp (current_string, orig) == 0)
|
2009-11-22 11:49:54 +00:00
|
|
|
{
|
|
|
|
grub_free (current_string);
|
2009-12-05 19:03:27 +00:00
|
|
|
found = 1;
|
2009-11-22 11:49:54 +00:00
|
|
|
}
|
|
|
|
current = (max + min) / 2;
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
ret = found ? grub_gettext_gettranslation_from_position (current) : orig;
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
cur = grub_zalloc (sizeof (*cur));
|
|
|
|
|
|
|
|
if (cur)
|
|
|
|
{
|
|
|
|
cur->name = grub_strdup (orig);
|
|
|
|
if (cur->name)
|
|
|
|
{
|
|
|
|
cur->translated = ret;
|
|
|
|
grub_list_push (GRUB_AS_LIST_P (&grub_gettext_msg_list),
|
|
|
|
GRUB_AS_LIST (cur));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-01-14 22:31:06 +00:00
|
|
|
grub_error_pop ();
|
2009-11-13 20:39:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-05 11:05:36 +00:00
|
|
|
/* This is similar to grub_file_open. */
|
2009-11-13 20:39:33 +00:00
|
|
|
static grub_file_t
|
|
|
|
grub_mofile_open (const char *filename)
|
|
|
|
{
|
|
|
|
int unsigned magic;
|
|
|
|
int version;
|
|
|
|
|
|
|
|
/* Using fd_mo and not another variable because
|
|
|
|
it's needed for grub_gettext_get_info. */
|
|
|
|
|
2010-09-05 11:05:36 +00:00
|
|
|
fd_mo = grub_file_open (filename);
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
2009-11-22 16:57:17 +00:00
|
|
|
if (!fd_mo)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2009-11-22 18:58:19 +00:00
|
|
|
grub_dprintf ("gettext", "Cannot read %s\n", filename);
|
2009-11-13 20:39:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
magic = grub_gettext_get_info (GETTEXT_MAGIC_NUMBER);
|
|
|
|
|
|
|
|
if (magic != MO_MAGIC_NUMBER)
|
|
|
|
{
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE, "mo: invalid mo file: %s",
|
|
|
|
filename);
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_file_close (fd_mo);
|
|
|
|
fd_mo = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
version = grub_gettext_get_info (GETTEXT_FILE_FORMAT);
|
|
|
|
|
|
|
|
if (version != 0)
|
|
|
|
{
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILE_TYPE,
|
2009-12-24 22:53:05 +00:00
|
|
|
"mo: invalid mo version in file: %s", filename);
|
2009-11-13 20:39:33 +00:00
|
|
|
fd_mo = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd_mo;
|
|
|
|
}
|
|
|
|
|
2010-12-10 11:45:08 +00:00
|
|
|
/* Returning grub_file_t would be more natural, but grub_mofile_open assigns
|
|
|
|
to fd_mo anyway ... */
|
2009-11-13 20:39:33 +00:00
|
|
|
static void
|
2010-12-10 11:45:08 +00:00
|
|
|
grub_mofile_open_lang (const char *locale_dir, const char *locale)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
char *mo_file;
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2009-11-21 23:11:14 +00:00
|
|
|
/* mo_file e.g.: /boot/grub/locale/ca.mo */
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2010-12-10 11:45:08 +00:00
|
|
|
mo_file = grub_xasprintf ("%s/%s.mo", locale_dir, locale);
|
2009-12-29 09:04:06 +00:00
|
|
|
if (!mo_file)
|
|
|
|
return;
|
2009-11-21 21:05:15 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
fd_mo = grub_mofile_open (mo_file);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
/* Will try adding .gz as well. */
|
|
|
|
if (fd_mo == NULL)
|
|
|
|
{
|
2010-09-30 19:07:51 +00:00
|
|
|
char *mo_file_old;
|
|
|
|
mo_file_old = mo_file;
|
2010-01-20 08:12:47 +00:00
|
|
|
mo_file = grub_xasprintf ("%s.gz", mo_file);
|
2010-09-30 19:07:51 +00:00
|
|
|
grub_free (mo_file_old);
|
2009-12-29 09:04:06 +00:00
|
|
|
if (!mo_file)
|
|
|
|
return;
|
2009-11-22 11:49:54 +00:00
|
|
|
fd_mo = grub_mofile_open (mo_file);
|
|
|
|
}
|
2010-12-10 11:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
grub_gettext_init_ext (const char *locale)
|
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
const char *locale_dir;
|
2010-12-10 11:45:08 +00:00
|
|
|
|
2011-11-03 15:04:26 +00:00
|
|
|
if (!locale)
|
|
|
|
return;
|
|
|
|
|
2010-12-10 11:45:08 +00:00
|
|
|
locale_dir = grub_env_get ("locale_dir");
|
|
|
|
if (locale_dir == NULL)
|
|
|
|
{
|
|
|
|
grub_dprintf ("gettext", "locale_dir variable is not set up.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_mo = NULL;
|
|
|
|
|
|
|
|
grub_mofile_open_lang (locale_dir, locale);
|
|
|
|
|
|
|
|
/* ll_CC didn't work, so try ll. */
|
|
|
|
if (fd_mo == NULL)
|
|
|
|
{
|
|
|
|
char *lang = grub_strdup (locale);
|
|
|
|
char *underscore = grub_strchr (lang, '_');
|
|
|
|
|
|
|
|
if (underscore)
|
|
|
|
{
|
|
|
|
*underscore = '\0';
|
|
|
|
grub_mofile_open_lang (locale_dir, lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (lang);
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
if (fd_mo)
|
|
|
|
{
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_gettext_offsetoriginal =
|
|
|
|
grub_gettext_get_info (GETTEXT_OFFSET_ORIGINAL);
|
|
|
|
grub_gettext_max = grub_gettext_get_info (GETTEXT_NUMBER_OF_STRINGS);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
grub_gettext_original = grub_gettext;
|
|
|
|
grub_gettext = grub_gettext_translate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
static void
|
2009-12-29 14:04:18 +00:00
|
|
|
grub_gettext_delete_list (void)
|
2009-12-05 19:03:27 +00:00
|
|
|
{
|
2010-03-26 19:20:52 +00:00
|
|
|
while (grub_gettext_msg_list)
|
2009-12-05 19:03:27 +00:00
|
|
|
{
|
2010-03-26 19:20:52 +00:00
|
|
|
grub_free ((char *) grub_gettext_msg_list->name);
|
|
|
|
grub_gettext_msg_list = grub_gettext_msg_list->next;
|
2010-01-14 22:31:06 +00:00
|
|
|
/* Don't delete the translated message because could be in use. */
|
2009-12-05 19:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static char *
|
|
|
|
grub_gettext_env_write_lang (struct grub_env_var *var
|
|
|
|
__attribute__ ((unused)), const char *val)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
grub_gettext_init_ext (val);
|
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
grub_gettext_delete_list ();
|
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
return grub_strdup (val);
|
|
|
|
}
|
|
|
|
|
2009-11-22 18:58:19 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_translate (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char **args)
|
|
|
|
{
|
|
|
|
if (argc != 1)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "text to translate required");
|
|
|
|
|
|
|
|
const char *translation;
|
|
|
|
translation = grub_gettext_translate (args[0]);
|
|
|
|
grub_printf ("%s\n", translation);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
GRUB_MOD_INIT (gettext)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
const char *lang;
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
lang = grub_env_get ("lang");
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
grub_gettext_init_ext (lang);
|
|
|
|
|
2009-11-22 18:58:19 +00:00
|
|
|
grub_register_command_p1 ("gettext", grub_cmd_translate,
|
2010-01-09 20:20:27 +00:00
|
|
|
N_("STRING"),
|
|
|
|
N_("Translates the string with the current settings."));
|
2009-11-22 18:58:19 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
/* Reload .mo file information if lang changes. */
|
|
|
|
grub_register_variable_hook ("lang", NULL, grub_gettext_env_write_lang);
|
|
|
|
|
|
|
|
/* Preserve hooks after context changes. */
|
|
|
|
grub_env_export ("lang");
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
GRUB_MOD_FINI (gettext)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
if (fd_mo != 0)
|
2009-11-22 11:49:54 +00:00
|
|
|
grub_file_close (fd_mo);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
grub_gettext_delete_list ();
|
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext = grub_gettext_original;
|
|
|
|
}
|