LCOV - code coverage report
Current view: top level - glib/glib - gcharset.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 203 228 89.0 %
Date: 2024-04-23 05:16:05 Functions: 18 18 100.0 %
Branches: 116 150 77.3 %

           Branch data     Line data    Source code
       1                 :            : /* gcharset.c - Charset information
       2                 :            :  *
       3                 :            :  * Copyright (C) 2011 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General Public
      18                 :            :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include "config.h"
      22                 :            : 
      23                 :            : #include "gcharset.h"
      24                 :            : #include "gcharsetprivate.h"
      25                 :            : 
      26                 :            : #include "garray.h"
      27                 :            : #include "genviron.h"
      28                 :            : #include "ghash.h"
      29                 :            : #include "gmessages.h"
      30                 :            : #include "gstrfuncs.h"
      31                 :            : #include "gthread.h"
      32                 :            : #include "gthreadprivate.h"
      33                 :            : #ifdef G_OS_WIN32
      34                 :            : #include "gwin32.h"
      35                 :            : #endif
      36                 :            : 
      37                 :            : #include "libcharset/libcharset.h"
      38                 :            : 
      39                 :            : #include <string.h>
      40                 :            : #include <stdio.h>
      41                 :            : 
      42                 :            : #if (HAVE_LANGINFO_TIME_CODESET || HAVE_LANGINFO_CODESET)
      43                 :            : #include <langinfo.h>
      44                 :            : #endif
      45                 :            : 
      46                 :            : #include <locale.h>
      47                 :            : #ifdef G_OS_WIN32
      48                 :            : #define WIN32_LEAN_AND_MEAN
      49                 :            : #include <windows.h>
      50                 :            : #endif
      51                 :            : 
      52                 :            : G_LOCK_DEFINE_STATIC (aliases);
      53                 :            : 
      54                 :            : static GHashTable *
      55                 :          2 : get_alias_hash (void)
      56                 :            : {
      57                 :            :   static GHashTable *alias_hash = NULL;
      58                 :            :   const char *aliases;
      59                 :            : 
      60                 :          2 :   G_LOCK (aliases);
      61                 :            : 
      62         [ +  + ]:          2 :   if (!alias_hash)
      63                 :            :     {
      64                 :          1 :       alias_hash = g_hash_table_new (g_str_hash, g_str_equal);
      65                 :            : 
      66                 :          1 :       aliases = _g_locale_get_charset_aliases ();
      67         [ -  + ]:          1 :       while (*aliases != '\0')
      68                 :            :         {
      69                 :            :           const char *canonical;
      70                 :            :           const char *alias;
      71                 :            :           const char **alias_array;
      72                 :          0 :           int count = 0;
      73                 :            : 
      74                 :          0 :           alias = aliases;
      75                 :          0 :           aliases += strlen (aliases) + 1;
      76                 :          0 :           canonical = aliases;
      77                 :          0 :           aliases += strlen (aliases) + 1;
      78                 :            : 
      79                 :          0 :           alias_array = g_hash_table_lookup (alias_hash, canonical);
      80         [ #  # ]:          0 :           if (alias_array)
      81                 :            :             {
      82         [ #  # ]:          0 :               while (alias_array[count])
      83                 :          0 :                 count++;
      84                 :            :             }
      85                 :            : 
      86                 :          0 :           alias_array = g_renew (const char *, alias_array, count + 2);
      87                 :          0 :           alias_array[count] = alias;
      88                 :          0 :           alias_array[count + 1] = NULL;
      89                 :            : 
      90                 :          0 :           g_hash_table_insert (alias_hash, (char *)canonical, alias_array);
      91                 :            :         }
      92                 :            :     }
      93                 :            : 
      94                 :          2 :   G_UNLOCK (aliases);
      95                 :            : 
      96                 :          2 :   return alias_hash;
      97                 :            : }
      98                 :            : 
      99                 :            : /* As an abuse of the alias table, the following routines gets
     100                 :            :  * the charsets that are aliases for the canonical name.
     101                 :            :  */
     102                 :            : const char **
     103                 :          2 : _g_charset_get_aliases (const char *canonical_name)
     104                 :            : {
     105                 :          2 :   GHashTable *alias_hash = get_alias_hash ();
     106                 :            : 
     107                 :          2 :   return g_hash_table_lookup (alias_hash, canonical_name);
     108                 :            : }
     109                 :            : 
     110                 :            : static gboolean
     111                 :       1447 : g_utf8_get_charset_internal (const char  *raw_data,
     112                 :            :                              const char **a)
     113                 :            : {
     114                 :            :   /* Allow CHARSET to override the charset of any locale category. Users should
     115                 :            :    * probably never be setting this — instead, just add the charset after a `.`
     116                 :            :    * in `LANGUAGE`/`LC_ALL`/`LC_*`/`LANG`. I can’t find any reference (in
     117                 :            :    * `git log`, code comments, or man pages) to this environment variable being
     118                 :            :    * standardised or documented or even used anywhere outside GLib. Perhaps it
     119                 :            :    * should eventually be removed. */
     120                 :       1447 :   const char *charset = g_getenv ("CHARSET");
     121                 :            : 
     122   [ +  +  +  - ]:       1447 :   if (charset && *charset)
     123                 :            :     {
     124                 :          9 :       *a = charset;
     125                 :            : 
     126   [ +  -  +  + ]:          9 :       if (charset && strstr (charset, "UTF-8"))
     127                 :          7 :         return TRUE;
     128                 :            :       else
     129                 :          2 :         return FALSE;
     130                 :            :     }
     131                 :            : 
     132                 :            :   /* The libcharset code tries to be thread-safe without
     133                 :            :    * a lock, but has a memory leak and a missing memory
     134                 :            :    * barrier, so we lock for it
     135                 :            :    */
     136                 :       1438 :   G_LOCK (aliases);
     137                 :       1438 :   charset = _g_locale_charset_unalias (raw_data);
     138                 :       1438 :   G_UNLOCK (aliases);
     139                 :            : 
     140   [ +  -  +  - ]:       1438 :   if (charset && *charset)
     141                 :            :     {
     142                 :       1438 :       *a = charset;
     143                 :            : 
     144   [ +  -  +  + ]:       1438 :       if (charset && strstr (charset, "UTF-8"))
     145                 :        112 :         return TRUE;
     146                 :            :       else
     147                 :       1326 :         return FALSE;
     148                 :            :     }
     149                 :            : 
     150                 :            :   /* Assume this for compatibility at present.  */
     151                 :          0 :   *a = "US-ASCII";
     152                 :            : 
     153                 :          0 :   return FALSE;
     154                 :            : }
     155                 :            : 
     156                 :            : typedef struct _GCharsetCache GCharsetCache;
     157                 :            : 
     158                 :            : struct _GCharsetCache {
     159                 :            :   gboolean is_utf8;
     160                 :            :   gchar *raw;
     161                 :            :   gchar *charset;
     162                 :            : };
     163                 :            : 
     164                 :            : static void
     165                 :        703 : charset_cache_free (gpointer data)
     166                 :            : {
     167                 :        703 :   GCharsetCache *cache = data;
     168                 :        703 :   g_free (cache->raw);
     169                 :        703 :   g_free (cache->charset);
     170                 :        703 :   g_free (cache);
     171                 :        703 : }
     172                 :            : 
     173                 :            : /**
     174                 :            :  * g_get_charset:
     175                 :            :  * @charset: (out) (optional) (transfer none): return location for character set
     176                 :            :  *   name, or %NULL.
     177                 :            :  *
     178                 :            :  * Obtains the character set for the [current locale][setlocale]; you
     179                 :            :  * might use this character set as an argument to g_convert(), to convert
     180                 :            :  * from the current locale's encoding to some other encoding. (Frequently
     181                 :            :  * g_locale_to_utf8() and g_locale_from_utf8() are nice shortcuts, though.)
     182                 :            :  *
     183                 :            :  * On Windows the character set returned by this function is the
     184                 :            :  * so-called system default ANSI code-page. That is the character set
     185                 :            :  * used by the "narrow" versions of C library and Win32 functions that
     186                 :            :  * handle file names. It might be different from the character set
     187                 :            :  * used by the C library's current locale.
     188                 :            :  *
     189                 :            :  * On Linux, the character set is found by consulting nl_langinfo() if
     190                 :            :  * available. If not, the environment variables `LC_ALL`, `LC_CTYPE`, `LANG`
     191                 :            :  * and `CHARSET` are queried in order. nl_langinfo() returns the C locale if
     192                 :            :  * no locale has been loaded by setlocale().
     193                 :            :  *
     194                 :            :  * The return value is %TRUE if the locale's encoding is UTF-8, in that
     195                 :            :  * case you can perhaps avoid calling g_convert().
     196                 :            :  *
     197                 :            :  * The string returned in @charset is not allocated, and should not be
     198                 :            :  * freed.
     199                 :            :  *
     200                 :            :  * Returns: %TRUE if the returned charset is UTF-8
     201                 :            :  */
     202                 :            : gboolean
     203                 :      27890 : g_get_charset (const char **charset)
     204                 :            : {
     205                 :            :   static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
     206                 :      27890 :   GCharsetCache *cache = g_private_get (&cache_private);
     207                 :            :   const gchar *raw;
     208                 :            : 
     209         [ +  + ]:      27890 :   if (!cache)
     210                 :       1416 :     cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache));
     211                 :            : 
     212                 :      27890 :   G_LOCK (aliases);
     213                 :      27890 :   raw = _g_locale_charset_raw ();
     214                 :      27890 :   G_UNLOCK (aliases);
     215                 :            : 
     216   [ +  +  +  + ]:      27890 :   if (cache->raw == NULL || strcmp (cache->raw, raw) != 0)
     217                 :            :     {
     218                 :            :       const gchar *new_charset;
     219                 :            : 
     220                 :       1438 :       g_free (cache->raw);
     221                 :       1438 :       g_free (cache->charset);
     222                 :       1438 :       cache->raw = g_strdup (raw);
     223                 :       1438 :       cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
     224                 :       1438 :       cache->charset = g_strdup (new_charset);
     225                 :            :     }
     226                 :            : 
     227         [ +  + ]:      27890 :   if (charset)
     228                 :      27313 :     *charset = cache->charset;
     229                 :            : 
     230                 :      27890 :   return cache->is_utf8;
     231                 :            : }
     232                 :            : 
     233                 :            : /*
     234                 :            :  * Do the same as g_get_charset() but it temporarily set locale (LC_ALL to
     235                 :            :  * LC_TIME) to correctly check for charset about time conversion relatives.
     236                 :            :  *
     237                 :            :  * Returns: %TRUE if the returned charset is UTF-8
     238                 :            :  */
     239                 :            : gboolean
     240                 :      10662 : _g_get_time_charset (const char **charset)
     241                 :            : {
     242                 :            :   static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
     243                 :      10662 :   GCharsetCache *cache = g_private_get (&cache_private);
     244                 :            :   const gchar *raw;
     245                 :            : 
     246         [ +  + ]:      10662 :   if (!cache)
     247                 :          3 :     cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache));
     248                 :            : 
     249                 :            : #ifdef HAVE_LANGINFO_TIME_CODESET
     250                 :      10662 :   raw = nl_langinfo (_NL_TIME_CODESET);
     251                 :            : #else
     252                 :            :   G_LOCK (aliases);
     253                 :            :   raw = _g_locale_charset_raw ();
     254                 :            :   G_UNLOCK (aliases);
     255                 :            : #endif
     256                 :            : 
     257   [ +  +  +  + ]:      10662 :   if (cache->raw == NULL || strcmp (cache->raw, raw) != 0)
     258                 :            :     {
     259                 :            :       const gchar *new_charset;
     260                 :            : 
     261                 :          8 :       g_free (cache->raw);
     262                 :          8 :       g_free (cache->charset);
     263                 :          8 :       cache->raw = g_strdup (raw);
     264                 :          8 :       cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
     265                 :          8 :       cache->charset = g_strdup (new_charset);
     266                 :            :     }
     267                 :            : 
     268         [ +  - ]:      10662 :   if (charset)
     269                 :      10662 :     *charset = cache->charset;
     270                 :            : 
     271                 :      10662 :   return cache->is_utf8;
     272                 :            : }
     273                 :            : /*
     274                 :            :  * Do the same as g_get_charset() but it temporarily set locale (LC_ALL to
     275                 :            :  * LC_CTYPE) to correctly check for charset about CTYPE conversion relatives.
     276                 :            :  *
     277                 :            :  * Returns: %TRUE if the returned charset is UTF-8
     278                 :            :  */
     279                 :            : gboolean
     280                 :         20 : _g_get_ctype_charset (const char **charset)
     281                 :            : {
     282                 :            :   static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
     283                 :         20 :   GCharsetCache *cache = g_private_get (&cache_private);
     284                 :            :   const gchar *raw;
     285                 :            : 
     286         [ +  + ]:         20 :   if (!cache)
     287                 :          1 :     cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache));
     288                 :            : 
     289                 :            : #ifdef HAVE_LANGINFO_CODESET
     290                 :         20 :   raw = nl_langinfo (CODESET);
     291                 :            : #else
     292                 :            :   G_LOCK (aliases);
     293                 :            :   raw = _g_locale_charset_raw ();
     294                 :            :   G_UNLOCK (aliases);
     295                 :            : #endif
     296                 :            : 
     297   [ +  +  -  + ]:         20 :   if (cache->raw == NULL || strcmp (cache->raw, raw) != 0)
     298                 :            :     {
     299                 :            :       const gchar *new_charset;
     300                 :            : 
     301                 :          1 :       g_free (cache->raw);
     302                 :          1 :       g_free (cache->charset);
     303                 :          1 :       cache->raw = g_strdup (raw);
     304                 :          1 :       cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
     305                 :          1 :       cache->charset = g_strdup (new_charset);
     306                 :            :     }
     307                 :            : 
     308         [ +  - ]:         20 :   if (charset)
     309                 :         20 :     *charset = cache->charset;
     310                 :            : 
     311                 :         20 :   return cache->is_utf8;
     312                 :            : }
     313                 :            : 
     314                 :            : /**
     315                 :            :  * g_get_codeset:
     316                 :            :  *
     317                 :            :  * Gets the character set for the current locale.
     318                 :            :  *
     319                 :            :  * Returns: a newly allocated string containing the name
     320                 :            :  *     of the character set. This string must be freed with g_free().
     321                 :            :  */
     322                 :            : gchar *
     323                 :          5 : g_get_codeset (void)
     324                 :            : {
     325                 :            :   const gchar *charset;
     326                 :            : 
     327                 :          5 :   g_get_charset (&charset);
     328                 :            : 
     329                 :         10 :   return g_strdup (charset);
     330                 :            : }
     331                 :            : 
     332                 :            : /**
     333                 :            :  * g_get_console_charset:
     334                 :            :  * @charset: (out) (optional) (transfer none): return location for character set
     335                 :            :  *   name, or %NULL.
     336                 :            :  *
     337                 :            :  * Obtains the character set used by the console attached to the process,
     338                 :            :  * which is suitable for printing output to the terminal.
     339                 :            :  *
     340                 :            :  * Usually this matches the result returned by g_get_charset(), but in
     341                 :            :  * environments where the locale's character set does not match the encoding
     342                 :            :  * of the console this function tries to guess a more suitable value instead.
     343                 :            :  *
     344                 :            :  * On Windows the character set returned by this function is the
     345                 :            :  * output code page used by the console associated with the calling process.
     346                 :            :  * If the codepage can't be determined (for example because there is no
     347                 :            :  * console attached) UTF-8 is assumed.
     348                 :            :  *
     349                 :            :  * The return value is %TRUE if the locale's encoding is UTF-8, in that
     350                 :            :  * case you can perhaps avoid calling g_convert().
     351                 :            :  *
     352                 :            :  * The string returned in @charset is not allocated, and should not be
     353                 :            :  * freed.
     354                 :            :  *
     355                 :            :  * Returns: %TRUE if the returned charset is UTF-8
     356                 :            :  *
     357                 :            :  * Since: 2.62
     358                 :            :  */
     359                 :            : gboolean
     360                 :      25002 : g_get_console_charset (const char **charset)
     361                 :            : {
     362                 :            : #ifdef G_OS_WIN32
     363                 :            :   static GPrivate cache_private = G_PRIVATE_INIT (charset_cache_free);
     364                 :            :   GCharsetCache *cache = g_private_get (&cache_private);
     365                 :            :   const gchar *locale;
     366                 :            :   unsigned int cp;
     367                 :            :   char buf[2 + 20 + 1]; /* "CP" + G_MAXUINT64 (to be safe) in decimal form (20 bytes) + "\0" */
     368                 :            :   const gchar *raw = NULL;
     369                 :            : 
     370                 :            :   if (!cache)
     371                 :            :     cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache));
     372                 :            : 
     373                 :            :   /* first try to query $LANG (works for Cygwin/MSYS/MSYS2 and others using mintty) */
     374                 :            :   locale = g_getenv ("LANG");
     375                 :            :   if (locale != NULL && locale[0] != '\0')
     376                 :            :     {
     377                 :            :       /* If the locale name contains an encoding after the dot, return it.  */
     378                 :            :       const char *dot = strchr (locale, '.');
     379                 :            : 
     380                 :            :       if (dot != NULL)
     381                 :            :         {
     382                 :            :           const char *modifier;
     383                 :            : 
     384                 :            :           dot++;
     385                 :            :           /* Look for the possible @... trailer and remove it, if any.  */
     386                 :            :           modifier = strchr (dot, '@');
     387                 :            :           if (modifier == NULL)
     388                 :            :             raw = dot;
     389                 :            :           else if ((gsize) (modifier - dot) < sizeof (buf))
     390                 :            :             {
     391                 :            :               memcpy (buf, dot, modifier - dot);
     392                 :            :               buf[modifier - dot] = '\0';
     393                 :            :               raw = buf;
     394                 :            :             }
     395                 :            :         }
     396                 :            :     }
     397                 :            :   /* next try querying console codepage using native win32 API */
     398                 :            :   if (raw == NULL)
     399                 :            :     {
     400                 :            :       cp = GetConsoleOutputCP ();
     401                 :            :       if (cp)
     402                 :            :         {
     403                 :            :           sprintf (buf, "CP%u", cp);
     404                 :            :           raw = buf;
     405                 :            :         }
     406                 :            :       else if (GetLastError () != ERROR_INVALID_HANDLE)
     407                 :            :         {
     408                 :            :           gchar *emsg = g_win32_error_message (GetLastError ());
     409                 :            :           g_warning ("Failed to determine console output code page: %s. "
     410                 :            :                      "Falling back to UTF-8", emsg);
     411                 :            :           g_free (emsg);
     412                 :            :         }
     413                 :            :     }
     414                 :            :   /* fall-back to UTF-8 if the rest failed (it's a universal default) */
     415                 :            :   if (raw == NULL)
     416                 :            :     raw = "UTF-8";
     417                 :            : 
     418                 :            :   if (cache->raw == NULL || strcmp (cache->raw, raw) != 0)
     419                 :            :     {
     420                 :            :       const gchar *new_charset;
     421                 :            : 
     422                 :            :       g_free (cache->raw);
     423                 :            :       g_free (cache->charset);
     424                 :            :       cache->raw = g_strdup (raw);
     425                 :            :       cache->is_utf8 = g_utf8_get_charset_internal (raw, &new_charset);
     426                 :            :       cache->charset = g_strdup (new_charset);
     427                 :            :     }
     428                 :            : 
     429                 :            :   if (charset)
     430                 :            :     *charset = cache->charset;
     431                 :            : 
     432                 :            :   return cache->is_utf8;
     433                 :            : #else
     434                 :            :   /* assume the locale settings match the console encoding on non-Windows OSs */
     435                 :      25002 :   return g_get_charset (charset);
     436                 :            : #endif
     437                 :            : }
     438                 :            : 
     439                 :            : #ifndef G_OS_WIN32
     440                 :            : 
     441                 :            : /* read an alias file for the locales */
     442                 :            : static void
     443                 :         58 : read_aliases (const gchar *file,
     444                 :            :               GHashTable  *alias_table)
     445                 :            : {
     446                 :            :   FILE *fp;
     447                 :            :   char buf[256];
     448                 :            : 
     449                 :         58 :   fp = fopen (file, "re");
     450         [ -  + ]:         58 :   if (!fp)
     451                 :          0 :     return;
     452         [ +  + ]:       4814 :   while (fgets (buf, 256, fp))
     453                 :            :     {
     454                 :            :       char *p, *q;
     455                 :            : 
     456                 :       4756 :       g_strstrip (buf);
     457                 :            : 
     458                 :            :       /* Line is a comment */
     459   [ +  +  +  + ]:       4756 :       if ((buf[0] == '#') || (buf[0] == '\0'))
     460                 :       2146 :         continue;
     461                 :            : 
     462                 :            :       /* Reads first column */
     463         [ +  - ]:      21924 :       for (p = buf, q = NULL; *p; p++) {
     464   [ +  +  +  +  :      21924 :         if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
                   -  + ]
     465                 :       2610 :           *p = '\0';
     466                 :       2610 :           q = p+1;
     467   [ +  +  +  + ]:      11890 :           while ((*q == '\t') || (*q == ' ')) {
     468                 :       9280 :             q++;
     469                 :            :           }
     470                 :       2610 :           break;
     471                 :            :         }
     472                 :            :       }
     473                 :            :       /* The line only had one column */
     474   [ +  -  -  + ]:       2610 :       if (!q || *q == '\0')
     475                 :          0 :         continue;
     476                 :            : 
     477                 :            :       /* Read second column */
     478         [ +  + ]:      41876 :       for (p = q; *p; p++) {
     479   [ +  -  -  + ]:      39266 :         if ((*p == '\t') || (*p == ' ')) {
     480                 :          0 :           *p = '\0';
     481                 :          0 :           break;
     482                 :            :         }
     483                 :            :       }
     484                 :            : 
     485                 :            :       /* Add to alias table if necessary */
     486         [ +  - ]:       2610 :       if (!g_hash_table_lookup (alias_table, buf)) {
     487                 :       2610 :         g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
     488                 :            :       }
     489                 :            :     }
     490                 :         58 :   fclose (fp);
     491                 :            : }
     492                 :            : 
     493                 :            : #endif
     494                 :            : 
     495                 :            : static char *
     496                 :         81 : unalias_lang (char *lang)
     497                 :            : {
     498                 :            : #ifndef G_OS_WIN32
     499                 :            :   static GHashTable *alias_table = NULL;
     500                 :            :   char *p;
     501                 :            :   int i;
     502                 :            : 
     503   [ +  +  +  -  :         81 :   if (g_once_init_enter_pointer (&alias_table))
                   +  + ]
     504                 :            :     {
     505                 :         58 :       GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
     506                 :         58 :       read_aliases ("/usr/share/locale/locale.alias", table);
     507                 :         58 :       g_once_init_leave_pointer (&alias_table, table);
     508                 :            :     }
     509                 :            : 
     510                 :         81 :   i = 0;
     511   [ -  +  -  - ]:         81 :   while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
     512                 :            :     {
     513                 :          0 :       lang = p;
     514         [ #  # ]:          0 :       if (i++ == 30)
     515                 :            :         {
     516                 :            :           static gboolean said_before = FALSE;
     517         [ #  # ]:          0 :           if (!said_before)
     518                 :          0 :             g_warning ("Too many alias levels for a locale, "
     519                 :            :                        "may indicate a loop");
     520                 :          0 :           said_before = TRUE;
     521                 :          0 :           return lang;
     522                 :            :         }
     523                 :            :     }
     524                 :            : #endif
     525                 :         81 :   return lang;
     526                 :            : }
     527                 :            : 
     528                 :            : /* Mask for components of locale spec. The ordering here is from
     529                 :            :  * least significant to most significant
     530                 :            :  */
     531                 :            : enum
     532                 :            : {
     533                 :            :   COMPONENT_CODESET =   1 << 0,
     534                 :            :   COMPONENT_TERRITORY = 1 << 1,
     535                 :            :   COMPONENT_MODIFIER =  1 << 2
     536                 :            : };
     537                 :            : 
     538                 :            : /* Break an X/Open style locale specification into components
     539                 :            :  */
     540                 :            : static guint
     541                 :        123 : explode_locale (const gchar *locale,
     542                 :            :                 gchar      **language,
     543                 :            :                 gchar      **territory,
     544                 :            :                 gchar      **codeset,
     545                 :            :                 gchar      **modifier)
     546                 :            : {
     547                 :            :   const gchar *uscore_pos;
     548                 :            :   const gchar *at_pos;
     549                 :            :   const gchar *dot_pos;
     550                 :            : 
     551                 :        123 :   guint mask = 0;
     552                 :            : 
     553                 :        123 :   uscore_pos = strchr (locale, '_');
     554         [ +  + ]:        123 :   dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
     555   [ +  +  +  + ]:        123 :   at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
     556                 :            : 
     557         [ +  + ]:        123 :   if (at_pos)
     558                 :            :     {
     559                 :         12 :       mask |= COMPONENT_MODIFIER;
     560                 :         12 :       *modifier = g_strdup (at_pos);
     561                 :            :     }
     562                 :            :   else
     563                 :        111 :     at_pos = locale + strlen (locale);
     564                 :            : 
     565         [ +  + ]:        123 :   if (dot_pos)
     566                 :            :     {
     567                 :         22 :       mask |= COMPONENT_CODESET;
     568                 :         22 :       *codeset = g_strndup (dot_pos, at_pos - dot_pos);
     569                 :            :     }
     570                 :            :   else
     571                 :        101 :     dot_pos = at_pos;
     572                 :            : 
     573         [ +  + ]:        123 :   if (uscore_pos)
     574                 :            :     {
     575                 :         39 :       mask |= COMPONENT_TERRITORY;
     576                 :         39 :       *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
     577                 :            :     }
     578                 :            :   else
     579                 :         84 :     uscore_pos = dot_pos;
     580                 :            : 
     581                 :        123 :   *language = g_strndup (locale, uscore_pos - locale);
     582                 :            : 
     583                 :        123 :   return mask;
     584                 :            : }
     585                 :            : 
     586                 :            : /*
     587                 :            :  * Compute all interesting variants for a given locale name -
     588                 :            :  * by stripping off different components of the value.
     589                 :            :  *
     590                 :            :  * For simplicity, we assume that the locale is in
     591                 :            :  * X/Open format: language[_territory][.codeset][@modifier]
     592                 :            :  *
     593                 :            :  * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
     594                 :            :  *       as well. We could just copy the code from glibc wholesale
     595                 :            :  *       but it is big, ugly, and complicated, so I'm reluctant
     596                 :            :  *       to do so when this should handle 99% of the time...
     597                 :            :  */
     598                 :            : static void
     599                 :        123 : append_locale_variants (GPtrArray *array,
     600                 :            :                         const gchar *locale)
     601                 :            : {
     602                 :        123 :   gchar *language = NULL;
     603                 :        123 :   gchar *territory = NULL;
     604                 :        123 :   gchar *codeset = NULL;
     605                 :        123 :   gchar *modifier = NULL;
     606                 :            : 
     607                 :            :   guint mask;
     608                 :            :   guint i, j;
     609                 :            : 
     610                 :        123 :   g_return_if_fail (locale != NULL);
     611                 :            : 
     612                 :        123 :   mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
     613                 :            : 
     614                 :            :   /* Iterate through all possible combinations, from least attractive
     615                 :            :    * to most attractive.
     616                 :            :    */
     617         [ +  + ]:        394 :   for (j = 0; j <= mask; ++j)
     618                 :            :     {
     619                 :        271 :       i = mask - j;
     620                 :            : 
     621         [ +  + ]:        271 :       if ((i & ~mask) == 0)
     622                 :            :         {
     623                 :        693 :           gchar *val = g_strconcat (language,
     624         [ +  + ]:        231 :                                     (i & COMPONENT_TERRITORY) ? territory : "",
     625         [ +  + ]:        231 :                                     (i & COMPONENT_CODESET) ? codeset : "",
     626         [ +  + ]:        231 :                                     (i & COMPONENT_MODIFIER) ? modifier : "",
     627                 :            :                                     NULL);
     628                 :        231 :           g_ptr_array_add (array, val);
     629                 :            :         }
     630                 :            :     }
     631                 :            : 
     632                 :        123 :   g_free (language);
     633         [ +  + ]:        123 :   if (mask & COMPONENT_CODESET)
     634                 :         22 :     g_free (codeset);
     635         [ +  + ]:        123 :   if (mask & COMPONENT_TERRITORY)
     636                 :         39 :     g_free (territory);
     637         [ +  + ]:        123 :   if (mask & COMPONENT_MODIFIER)
     638                 :         12 :     g_free (modifier);
     639                 :            : }
     640                 :            : 
     641                 :            : /**
     642                 :            :  * g_get_locale_variants:
     643                 :            :  * @locale: a locale identifier
     644                 :            :  *
     645                 :            :  * Returns a list of derived variants of @locale, which can be used to
     646                 :            :  * e.g. construct locale-dependent filenames or search paths. The returned
     647                 :            :  * list is sorted from most desirable to least desirable.
     648                 :            :  * This function handles territory, charset and extra locale modifiers. See
     649                 :            :  * [`setlocale(3)`](man:setlocale) for information about locales and their format.
     650                 :            :  *
     651                 :            :  * @locale itself is guaranteed to be returned in the output.
     652                 :            :  *
     653                 :            :  * For example, if @locale is `fr_BE`, then the returned list
     654                 :            :  * is `fr_BE`, `fr`. If @locale is `en_GB.UTF-8@euro`, then the returned list
     655                 :            :  * is `en_GB.UTF-8@euro`, `en_GB.UTF-8`, `en_GB@euro`, `en_GB`, `en.UTF-8@euro`,
     656                 :            :  * `en.UTF-8`, `en@euro`, `en`.
     657                 :            :  *
     658                 :            :  * If you need the list of variants for the current locale,
     659                 :            :  * use g_get_language_names().
     660                 :            :  *
     661                 :            :  * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly
     662                 :            :  *   allocated array of newly allocated strings with the locale variants. Free with
     663                 :            :  *   g_strfreev().
     664                 :            :  *
     665                 :            :  * Since: 2.28
     666                 :            :  */
     667                 :            : gchar **
     668                 :         42 : g_get_locale_variants (const gchar *locale)
     669                 :            : {
     670                 :            :   GPtrArray *array;
     671                 :            : 
     672                 :         42 :   g_return_val_if_fail (locale != NULL, NULL);
     673                 :            : 
     674                 :         42 :   array = g_ptr_array_sized_new (8);
     675                 :         42 :   append_locale_variants (array, locale);
     676                 :         42 :   g_ptr_array_add (array, NULL);
     677                 :            : 
     678                 :         42 :   return (gchar **) g_ptr_array_free (array, FALSE);
     679                 :            : }
     680                 :            : 
     681                 :            : /* The following is (partly) taken from the gettext package.
     682                 :            :    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.  */
     683                 :            : 
     684                 :            : static const gchar *
     685                 :       5373 : guess_category_value (const gchar *category_name)
     686                 :            : {
     687                 :            :   const gchar *retval;
     688                 :            : 
     689                 :            :   /* The highest priority value is the 'LANGUAGE' environment
     690                 :            :      variable.  This is a GNU extension.  */
     691                 :       5373 :   retval = g_getenv ("LANGUAGE");
     692   [ +  +  +  - ]:       5373 :   if ((retval != NULL) && (retval[0] != '\0'))
     693                 :        609 :     return retval;
     694                 :            : 
     695                 :            :   /* 'LANGUAGE' is not set.  So we have to proceed with the POSIX
     696                 :            :      methods of looking to 'LC_ALL', 'LC_xxx', and 'LANG'.  On some
     697                 :            :      systems this can be done by the 'setlocale' function itself.  */
     698                 :            : 
     699                 :            :   /* Setting of LC_ALL overwrites all other.  */
     700                 :       4764 :   retval = g_getenv ("LC_ALL");
     701   [ +  +  +  - ]:       4764 :   if ((retval != NULL) && (retval[0] != '\0'))
     702                 :       4199 :     return retval;
     703                 :            : 
     704                 :            :   /* Next comes the name of the desired category.  */
     705                 :        565 :   retval = g_getenv (category_name);
     706   [ +  +  +  - ]:        565 :   if ((retval != NULL) && (retval[0] != '\0'))
     707                 :          1 :     return retval;
     708                 :            : 
     709                 :            :   /* Last possibility is the LANG environment variable.  */
     710                 :        564 :   retval = g_getenv ("LANG");
     711   [ +  +  +  - ]:        564 :   if ((retval != NULL) && (retval[0] != '\0'))
     712                 :        563 :     return retval;
     713                 :            : 
     714                 :            : #ifdef G_PLATFORM_WIN32
     715                 :            :   /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
     716                 :            :    * LANG, which we already did above. Oh well. The main point of
     717                 :            :    * calling g_win32_getlocale() is to get the thread's locale as used
     718                 :            :    * by Windows and the Microsoft C runtime (in the "English_United
     719                 :            :    * States" format) translated into the Unixish format.
     720                 :            :    */
     721                 :            :   {
     722                 :            :     char *locale = g_win32_getlocale ();
     723                 :            :     retval = g_intern_string (locale);
     724                 :            :     g_free (locale);
     725                 :            :     return retval;
     726                 :            :   }
     727                 :            : #endif
     728                 :            : 
     729                 :          1 :   return NULL;
     730                 :            : }
     731                 :            : 
     732                 :            : typedef struct _GLanguageNamesCache GLanguageNamesCache;
     733                 :            : 
     734                 :            : struct _GLanguageNamesCache {
     735                 :            :   gchar *languages;
     736                 :            :   gchar **language_names;
     737                 :            : };
     738                 :            : 
     739                 :            : static void
     740                 :         15 : language_names_cache_free (gpointer data)
     741                 :            : {
     742                 :         15 :   GLanguageNamesCache *cache = data;
     743                 :         15 :   g_free (cache->languages);
     744                 :         15 :   g_strfreev (cache->language_names);
     745                 :         15 :   g_free (cache);
     746                 :         15 : }
     747                 :            : 
     748                 :            : /**
     749                 :            :  * g_get_language_names:
     750                 :            :  *
     751                 :            :  * Computes a list of applicable locale names, which can be used to
     752                 :            :  * e.g. construct locale-dependent filenames or search paths. The returned
     753                 :            :  * list is sorted from most desirable to least desirable and always contains
     754                 :            :  * the default locale "C".
     755                 :            :  *
     756                 :            :  * For example, if LANGUAGE=de:en_US, then the returned list is
     757                 :            :  * "de", "en_US", "en", "C".
     758                 :            :  *
     759                 :            :  * This function consults the environment variables `LANGUAGE`, `LC_ALL`,
     760                 :            :  * `LC_MESSAGES` and `LANG` to find the list of locales specified by the
     761                 :            :  * user.
     762                 :            :  *
     763                 :            :  * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
     764                 :            :  *    that must not be modified or freed.
     765                 :            :  *
     766                 :            :  * Since: 2.6
     767                 :            :  */
     768                 :            : const gchar * const *
     769                 :       5368 : g_get_language_names (void)
     770                 :            : {
     771                 :       5368 :   return g_get_language_names_with_category ("LC_MESSAGES");
     772                 :            : }
     773                 :            : 
     774                 :            : /**
     775                 :            :  * g_get_language_names_with_category:
     776                 :            :  * @category_name: a locale category name
     777                 :            :  *
     778                 :            :  * Computes a list of applicable locale names with a locale category name,
     779                 :            :  * which can be used to construct the fallback locale-dependent filenames
     780                 :            :  * or search paths. The returned list is sorted from most desirable to
     781                 :            :  * least desirable and always contains the default locale "C".
     782                 :            :  *
     783                 :            :  * This function consults the environment variables `LANGUAGE`, `LC_ALL`,
     784                 :            :  * @category_name, and `LANG` to find the list of locales specified by the
     785                 :            :  * user.
     786                 :            :  *
     787                 :            :  * g_get_language_names() returns g_get_language_names_with_category("LC_MESSAGES").
     788                 :            :  *
     789                 :            :  * Returns: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by
     790                 :            :  *    the thread g_get_language_names_with_category was called from.
     791                 :            :  *    It must not be modified or freed. It must be copied if planned to be used in another thread.
     792                 :            :  *
     793                 :            :  * Since: 2.58
     794                 :            :  */
     795                 :            : const gchar * const *
     796                 :       5373 : g_get_language_names_with_category (const gchar *category_name)
     797                 :            : {
     798                 :            :   static GPrivate cache_private = G_PRIVATE_INIT ((void (*)(gpointer)) g_hash_table_unref);
     799                 :       5373 :   GHashTable *cache = g_private_get (&cache_private);
     800                 :            :   const gchar *languages;
     801                 :            :   GLanguageNamesCache *name_cache;
     802                 :            : 
     803                 :       5373 :   g_return_val_if_fail (category_name != NULL, NULL);
     804                 :            : 
     805         [ +  + ]:       5373 :   if (!cache)
     806                 :            :     {
     807                 :         62 :       cache = g_hash_table_new_full (g_str_hash, g_str_equal,
     808                 :            :                                      g_free, language_names_cache_free);
     809                 :         62 :       g_private_set (&cache_private, cache);
     810                 :            :     }
     811                 :            : 
     812                 :       5373 :   languages = guess_category_value (category_name);
     813         [ +  + ]:       5373 :   if (!languages)
     814                 :          1 :     languages = "C";
     815                 :            : 
     816                 :       5373 :   name_cache = (GLanguageNamesCache *) g_hash_table_lookup (cache, category_name);
     817   [ +  +  +  - ]:       5373 :   if (!(name_cache && name_cache->languages &&
     818         [ +  + ]:       5311 :         strcmp (name_cache->languages, languages) == 0))
     819                 :            :     {
     820                 :            :       GPtrArray *array;
     821                 :            :       gchar **alist, **a;
     822                 :            : 
     823                 :         75 :       g_hash_table_remove (cache, category_name);
     824                 :            : 
     825                 :         75 :       array = g_ptr_array_sized_new (8);
     826                 :            : 
     827                 :         75 :       alist = g_strsplit (languages, ":", 0);
     828         [ +  + ]:        156 :       for (a = alist; *a; a++)
     829                 :         81 :         append_locale_variants (array, unalias_lang (*a));
     830                 :         75 :       g_strfreev (alist);
     831                 :         75 :       g_ptr_array_add (array, g_strdup ("C"));
     832                 :         75 :       g_ptr_array_add (array, NULL);
     833                 :            : 
     834                 :         75 :       name_cache = g_new0 (GLanguageNamesCache, 1);
     835                 :         75 :       name_cache->languages = g_strdup (languages);
     836                 :         75 :       name_cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
     837                 :         75 :       g_hash_table_insert (cache, g_strdup (category_name), name_cache);
     838                 :            :     }
     839                 :            : 
     840                 :       5373 :   return (const gchar * const *) name_cache->language_names;
     841                 :            : }

Generated by: LCOV version 1.14