コード例 #1
0
ファイル: findlocale.c プロジェクト: cmjonze/eglibc_svn
    internal_function
_nl_find_locale (const char *locale_path, size_t locale_path_len,
                 int category, const char **name)
{
    int mask;
    /* Name of the locale for this category.  */
    char *loc_name;
    const char *language;
    const char *modifier;
    const char *territory;
    const char *codeset;
    const char *normalized_codeset;
    struct loaded_l10nfile *locale_file;

    if ((*name)[0] == '\0')
    {
        /* The user decides which locale to use by setting environment
        variables.  */
        *name = getenv ("LC_ALL");
        if (*name == NULL || (*name)[0] == '\0')
            *name = getenv (_nl_category_names.str
                            + _nl_category_name_idxs[category]);
        if (*name == NULL || (*name)[0] == '\0')
            *name = getenv ("LANG");
    }

    if (*name == NULL || (*name)[0] == '\0'
            || (__builtin_expect (__libc_enable_secure, 0)
                && strchr (*name, '/') != NULL))
        *name = (char *) _nl_C_name;

    if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
            || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
    {
        /* We need not load anything.  The needed data is contained in
        the library itself.  */
        *name = (char *) _nl_C_name;
        return _nl_C[category];
    }

    /* We really have to load some data.  First we try the archive,
       but only if there was no LOCPATH environment variable specified.  */
    if (__builtin_expect (locale_path == NULL, 1))
    {
        struct __locale_data *data
            = _nl_load_locale_from_archive (category, name);
        if (__builtin_expect (data != NULL, 1))
            return data;

        /* Nothing in the archive.  Set the default path to search below.  */
        locale_path = _nl_default_locale_path;
        locale_path_len = sizeof _nl_default_locale_path;
    }

    /* We really have to load some data.  First see whether the name is
       an alias.  Please note that this makes it impossible to have "C"
       or "POSIX" as aliases.  */
    loc_name = (char *) _nl_expand_alias (*name);
    if (loc_name == NULL)
        /* It is no alias.  */
        loc_name = (char *) *name;

    /* Make a writable copy of the locale name.  */
    loc_name = strdupa (loc_name);

    /* LOCALE can consist of up to four recognized parts for the XPG syntax:

    	language[_territory[.codeset]][@modifier]

       Beside the first all of them are allowed to be missing.  If the
       full specified locale is not found, the less specific one are
       looked for.  The various part will be stripped off according to
       the following order:
    	(1) codeset
    	(2) normalized codeset
    	(3) territory
    	(4) modifier
     */
    mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
                             &codeset, &normalized_codeset);
    if (mask == -1)
        /* Memory allocate problem.  */
        return NULL;

    /* If exactly this locale was already asked for we have an entry with
       the complete name.  */
    locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
                                      locale_path, locale_path_len, mask,
                                      language, territory, codeset,
                                      normalized_codeset, modifier,
                                      _nl_category_names.str
                                      + _nl_category_name_idxs[category], 0);

    if (locale_file == NULL)
    {
        /* Find status record for addressed locale file.  We have to search
        through all directories in the locale path.  */
        locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
                                          locale_path, locale_path_len, mask,
                                          language, territory, codeset,
                                          normalized_codeset, modifier,
                                          _nl_category_names.str
                                          + _nl_category_name_idxs[category], 1);
        if (locale_file == NULL)
            /* This means we are out of core.  */
            return NULL;
    }

    /* The space for normalized_codeset is dynamically allocated.  Free it.  */
    if (mask & XPG_NORM_CODESET)
        free ((void *) normalized_codeset);

    if (locale_file->decided == 0)
        _nl_load_locale (locale_file, category);

    if (locale_file->data == NULL)
    {
        int cnt;
        for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
        {
            if (locale_file->successor[cnt]->decided == 0)
                _nl_load_locale (locale_file->successor[cnt], category);
            if (locale_file->successor[cnt]->data != NULL)
                break;
        }
        /* Move the entry we found (or NULL) to the first place of
        successors.  */
        locale_file->successor[0] = locale_file->successor[cnt];
        locale_file = locale_file->successor[cnt];

        if (locale_file == NULL)
            return NULL;
    }

    /* The LC_CTYPE category allows to check whether a locale is really
       usable.  If the locale name contains a charset name and the
       charset name used in the locale (present in the LC_CTYPE data) is
       not the same (after resolving aliases etc) we reject the locale
       since using it would irritate users expecting the charset named
       in the locale name.  */
    if (codeset != NULL)
    {
        /* Get the codeset information from the locale file.  */
        static const int codeset_idx[] =
        {
            [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
            [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
            [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
            [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
            [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
            [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
            [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
            [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
            [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
            [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
            [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
            [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
        };
        const struct __locale_data *data;
        const char *locale_codeset;
        char *clocale_codeset;
        char *ccodeset;

        data = (const struct __locale_data *) locale_file->data;
        locale_codeset =
            (const char *) data->values[codeset_idx[category]].string;
        assert (locale_codeset != NULL);
        /* Note the length of the allocated memory: +3 for up to two slashes
        and the NUL byte.  */
        clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
        strip (clocale_codeset, locale_codeset);

        ccodeset = (char *) alloca (strlen (codeset) + 3);
        strip (ccodeset, codeset);

        if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
                                   upstr (clocale_codeset,
                                          clocale_codeset)) != 0)
            /* The codesets are not identical, don't use the locale.  */
            return NULL;
    }
コード例 #2
0
ファイル: finddomain.c プロジェクト: KrisLee/War-Of-Kingdom
internal_function
_nl_find_domain (const char *dirname, char *locale,
		 const char *domainname, struct binding *domainbinding)
{
  struct loaded_l10nfile *retval;
  const char *language;
  const char *modifier;
  const char *territory;
  const char *codeset;
  const char *normalized_codeset;
  const char *alias_value;
  int mask;

  /* LOCALE can consist of up to four recognized parts for the XPG syntax:

		language[_territory][.codeset][@modifier]

     Beside the first part all of them are allowed to be missing.  If
     the full specified locale is not found, the less specific one are
     looked for.  The various parts will be stripped off according to
     the following order:
		(1) codeset
		(2) normalized codeset
		(3) territory
		(4) modifier
   */

  /* We need to protect modifying the _NL_LOADED_DOMAINS data.  */
  gl_rwlock_define_initialized (static, lock);
  gl_rwlock_rdlock (lock);

  /* If we have already tested for this locale entry there has to
     be one data set in the list of loaded domains.  */
  retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
			       strlen (dirname) + 1, 0, locale, NULL, NULL,
			       NULL, NULL, domainname, 0);

  gl_rwlock_unlock (lock);

  if (retval != NULL)
    {
      /* We know something about this locale.  */
      int cnt;

      if (retval->decided <= 0)
	_nl_load_domain (retval, domainbinding);

      if (retval->data != NULL)
	return retval;

      for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
	{
	  if (retval->successor[cnt]->decided <= 0)
	    _nl_load_domain (retval->successor[cnt], domainbinding);

	  if (retval->successor[cnt]->data != NULL)
	    break;
	}

      return retval;
      /* NOTREACHED */
    }

  /* See whether the locale value is an alias.  If yes its value
     *overwrites* the alias name.  No test for the original value is
     done.  */
  alias_value = _nl_expand_alias (locale);
  if (alias_value != NULL)
    {
#if defined _LIBC || defined HAVE_STRDUP
      locale = strdup (alias_value);
      if (locale == NULL)
	return NULL;
#else
      size_t len = strlen (alias_value) + 1;
      locale = (char *) malloc (len);
      if (locale == NULL)
	return NULL;

      memcpy (locale, alias_value, len);
#endif
    }

  /* Now we determine the single parts of the locale name.  First
     look for the language.  Termination symbols are `_', '.', and `@'.  */
  mask = _nl_explode_name (locale, &language, &modifier, &territory,
			   &codeset, &normalized_codeset);

  /* We need to protect modifying the _NL_LOADED_DOMAINS data.  */
  gl_rwlock_wrlock (lock);

  /* Create all possible locale entries which might be interested in
     generalization.  */
  retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
			       strlen (dirname) + 1, mask, language, territory,
			       codeset, normalized_codeset, modifier,
			       domainname, 1);

  gl_rwlock_unlock (lock);

  if (retval == NULL)
    /* This means we are out of core.  */
    return NULL;

  if (retval->decided <= 0)
    _nl_load_domain (retval, domainbinding);

  if (retval->data == NULL)
    {
      int cnt;
      for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
	{
	  if (retval->successor[cnt]->decided <= 0)
	    _nl_load_domain (retval->successor[cnt], domainbinding);
	  if (retval->successor[cnt]->data != NULL)
	    break;
	}
    }

  /* The room for an alias was dynamically allocated.  Free it now.  */
  if (alias_value != NULL)
    free (locale);

  /* The space for normalized_codeset is dynamically allocated.  Free it.  */
  if (mask & XPG_NORM_CODESET)
    free ((void *) normalized_codeset);

  return retval;
}
コード例 #3
0
ファイル: finddomain.c プロジェクト: Efreak/elinks
/* Return a data structure describing the message catalog described by
   the DOMAINNAME and CATEGORY parameters with respect to the currently
   established bindings.  */
struct loaded_l10nfile *
_nl_find_domain(const unsigned char *dirname, unsigned char *locale, const unsigned char *domainname,
		struct binding *domainbinding)
{
	struct loaded_l10nfile *retval;
	const unsigned char *language;
	const unsigned char *modifier;
	const unsigned char *territory;
	const unsigned char *codeset;
	const unsigned char *normalized_codeset;
	const unsigned char *special;
	const unsigned char *sponsor;
	const unsigned char *revision;
	const unsigned char *alias_value;
	int mask;

	/* LOCALE can consist of up to four recognized parts for the XPG syntax:

	   language[_territory[.codeset]][@modifier]

	   and six parts for the CEN syntax:

	   language[_territory][+audience][+special][,[sponsor][_revision]]

	   Beside the first part all of them are allowed to be missing.  If
	   the full specified locale is not found, the less specific one are
	   looked for.  The various parts will be stripped off according to
	   the following order:
	   (1) revision
	   (2) sponsor
	   (3) special
	   (4) codeset
	   (5) normalized codeset
	   (6) territory
	   (7) audience/modifier
	 */

	/* If we have already tested for this locale entry there has to
	   be one data set in the list of loaded domains.  */
	retval = _nl_make_l10nflist(&_nl_loaded_domains, dirname,
				    strlen(dirname) + 1, 0, locale, NULL, NULL,
				    NULL, NULL, NULL, NULL, NULL, domainname,
				    0);
	if (retval != NULL) {
		/* We know something about this locale.  */
		int cnt;

		if (retval->decided == 0)
			_nl_load_domain(retval, domainbinding);

		if (retval->data != NULL)
			return retval;

		for (cnt = 0; retval->successor[cnt] != NULL; ++cnt) {
			if (retval->successor[cnt]->decided == 0)
				_nl_load_domain(retval->successor[cnt],
						domainbinding);

			if (retval->successor[cnt]->data != NULL)
				break;
		}
		return cnt >= 0 ? retval : NULL;
		/* NOTREACHED */
	}

	/* See whether the locale value is an alias.  If yes its value
	 *overwrites* the alias name.  No test for the original value is
	 done.  */
	alias_value = _nl_expand_alias(locale);
	if (alias_value != NULL) {
		locale = strdup(alias_value);
		if (locale == NULL)
			return NULL;
	}

	/* Now we determine the single parts of the locale name.  First
	   look for the language.  Termination symbols are `_' and `@' if
	   we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
	mask = _nl_explode_name(locale, &language, &modifier, &territory,
				&codeset, &normalized_codeset, &special,
				&sponsor, &revision);

	/* Create all possible locale entries which might be interested in
	   generalization.  */
	retval = _nl_make_l10nflist(&_nl_loaded_domains, dirname,
				    strlen(dirname) + 1, mask, language,
				    territory, codeset, normalized_codeset,
				    modifier, special, sponsor, revision,
				    domainname, 1);
	if (retval == NULL)
		/* This means we are out of core.  */
		return NULL;

	if (retval->decided == 0)
		_nl_load_domain(retval, domainbinding);
	if (retval->data == NULL) {
		int cnt;

		for (cnt = 0; retval->successor[cnt] != NULL; ++cnt) {
			if (retval->successor[cnt]->decided == 0)
				_nl_load_domain(retval->successor[cnt],
						domainbinding);
			if (retval->successor[cnt]->data != NULL)
				break;
		}
	}

	/* The room for an alias was dynamically allocated.  Free it now.  */
	if (alias_value != NULL)
		free(locale);

	/* The space for normalized_codeset is dynamically allocated.  Free it.  */
	if (mask & XPG_NORM_CODESET)
		free((void *) normalized_codeset);

	return retval;
}