PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_locale.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_locale_struct
 

Macros

#define LOCALE_NAME_BUFLEN   128
 

Typedefs

typedef struct pg_locale_structpg_locale_t
 

Functions

bool check_locale (int category, const char *locale, char **canonname)
 
char * pg_perm_setlocale (int category, const char *locale)
 
struct lconv * PGLC_localeconv (void)
 
void cache_locale_time (void)
 
void init_database_collation (void)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
int pg_strcoll (const char *arg1, const char *arg2, pg_locale_t locale)
 
int pg_strncoll (const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
 
bool pg_strxfrm_enabled (pg_locale_t locale)
 
size_t pg_strxfrm (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
bool pg_strxfrm_prefix_enabled (pg_locale_t locale)
 
size_t pg_strxfrm_prefix (char *dest, const char *src, size_t destsize, pg_locale_t locale)
 
size_t pg_strnxfrm_prefix (char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
 
int builtin_locale_encoding (const char *locale)
 
const char * builtin_validate_locale (int encoding, const char *locale)
 
void icu_validate_locale (const char *loc_str)
 
char * icu_language_tag (const char *loc_str, int elevel)
 
size_t wchar2char (char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
 
size_t char2wchar (wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale)
 

Variables

PGDLLIMPORT char * locale_messages
 
PGDLLIMPORT char * locale_monetary
 
PGDLLIMPORT char * locale_numeric
 
PGDLLIMPORT char * locale_time
 
PGDLLIMPORT int icu_validation_level
 
PGDLLIMPORT char * localized_abbrev_days []
 
PGDLLIMPORT char * localized_full_days []
 
PGDLLIMPORT char * localized_abbrev_months []
 
PGDLLIMPORT char * localized_full_months []
 
PGDLLIMPORT bool database_ctype_is_c
 

Macro Definition Documentation

◆ LOCALE_NAME_BUFLEN

#define LOCALE_NAME_BUFLEN   128

Definition at line 33 of file pg_locale.h.

Typedef Documentation

◆ pg_locale_t

typedef struct pg_locale_struct* pg_locale_t

Definition at line 102 of file pg_locale.h.

Function Documentation

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1820 of file pg_locale.c.

1821 {
1822  if (strcmp(locale, "C") == 0)
1823  return -1;
1824  if (strcmp(locale, "C.UTF-8") == 0)
1825  return PG_UTF8;
1826 
1827  ereport(ERROR,
1828  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1829  errmsg("invalid locale name \"%s\" for builtin provider",
1830  locale)));
1831 
1832  return 0; /* keep compiler quiet */
1833 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static char * locale
Definition: initdb.c:140
@ PG_UTF8
Definition: pg_wchar.h:232

References ereport, errcode(), errmsg(), ERROR, locale, and PG_UTF8.

Referenced by builtin_validate_locale(), and DefineCollation().

◆ builtin_validate_locale()

const char* builtin_validate_locale ( int  encoding,
const char *  locale 
)

Definition at line 1841 of file pg_locale.c.

1842 {
1843  const char *canonical_name = NULL;
1844  int required_encoding;
1845 
1846  if (strcmp(locale, "C") == 0)
1847  canonical_name = "C";
1848  else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1849  canonical_name = "C.UTF-8";
1850 
1851  if (!canonical_name)
1852  ereport(ERROR,
1853  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1854  errmsg("invalid locale name \"%s\" for builtin provider",
1855  locale)));
1856 
1857  required_encoding = builtin_locale_encoding(canonical_name);
1858  if (required_encoding >= 0 && encoding != required_encoding)
1859  ereport(ERROR,
1860  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1861  errmsg("encoding \"%s\" does not match locale \"%s\"",
1863 
1864  return canonical_name;
1865 }
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1820
#define pg_encoding_to_char
Definition: pg_wchar.h:630

References builtin_locale_encoding(), encoding, ereport, errcode(), errmsg(), ERROR, locale, and pg_encoding_to_char.

Referenced by create_pg_locale(), createdb(), DefineCollation(), and init_database_collation().

◆ cache_locale_time()

void cache_locale_time ( void  )

Definition at line 817 of file pg_locale.c.

818 {
819  char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
820  char *bufptr;
821  time_t timenow;
822  struct tm *timeinfo;
823  struct tm timeinfobuf;
824  bool strftimefail = false;
825  int encoding;
826  int i;
827  char *save_lc_time;
828 #ifdef WIN32
829  char *save_lc_ctype;
830 #endif
831 
832  /* did we do this already? */
833  if (CurrentLCTimeValid)
834  return;
835 
836  elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
837 
838  /*
839  * As in PGLC_localeconv(), it's critical that we not throw error while
840  * libc's locale settings have nondefault values. Hence, we just call
841  * strftime() within the critical section, and then convert and save its
842  * results afterwards.
843  */
844 
845  /* Save prevailing value of time locale */
846  save_lc_time = setlocale(LC_TIME, NULL);
847  if (!save_lc_time)
848  elog(ERROR, "setlocale(NULL) failed");
849  save_lc_time = pstrdup(save_lc_time);
850 
851 #ifdef WIN32
852 
853  /*
854  * On Windows, it appears that wcsftime() internally uses LC_CTYPE, so we
855  * must set it here. This code looks the same as what PGLC_localeconv()
856  * does, but the underlying reason is different: this does NOT determine
857  * the encoding we'll get back from strftime_win32().
858  */
859 
860  /* Save prevailing value of ctype locale */
861  save_lc_ctype = setlocale(LC_CTYPE, NULL);
862  if (!save_lc_ctype)
863  elog(ERROR, "setlocale(NULL) failed");
864  save_lc_ctype = pstrdup(save_lc_ctype);
865 
866  /* use lc_time to set the ctype */
867  setlocale(LC_CTYPE, locale_time);
868 #endif
869 
870  setlocale(LC_TIME, locale_time);
871 
872  /* We use times close to current time as data for strftime(). */
873  timenow = time(NULL);
874  timeinfo = gmtime_r(&timenow, &timeinfobuf);
875 
876  /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
877  bufptr = buf;
878 
879  /*
880  * MAX_L10N_DATA is sufficient buffer space for every known locale, and
881  * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
882  * error.) An implementation might report errors (e.g. ENOMEM) by
883  * returning 0 (or, less plausibly, a negative value) and setting errno.
884  * Report errno just in case the implementation did that, but clear it in
885  * advance of the calls so we don't emit a stale, unrelated errno.
886  */
887  errno = 0;
888 
889  /* localized days */
890  for (i = 0; i < 7; i++)
891  {
892  timeinfo->tm_wday = i;
893  if (strftime(bufptr, MAX_L10N_DATA, "%a", timeinfo) <= 0)
894  strftimefail = true;
895  bufptr += MAX_L10N_DATA;
896  if (strftime(bufptr, MAX_L10N_DATA, "%A", timeinfo) <= 0)
897  strftimefail = true;
898  bufptr += MAX_L10N_DATA;
899  }
900 
901  /* localized months */
902  for (i = 0; i < 12; i++)
903  {
904  timeinfo->tm_mon = i;
905  timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
906  if (strftime(bufptr, MAX_L10N_DATA, "%b", timeinfo) <= 0)
907  strftimefail = true;
908  bufptr += MAX_L10N_DATA;
909  if (strftime(bufptr, MAX_L10N_DATA, "%B", timeinfo) <= 0)
910  strftimefail = true;
911  bufptr += MAX_L10N_DATA;
912  }
913 
914  /*
915  * Restore the prevailing locale settings; as in PGLC_localeconv(),
916  * failure to do so is fatal.
917  */
918 #ifdef WIN32
919  if (!setlocale(LC_CTYPE, save_lc_ctype))
920  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
921 #endif
922  if (!setlocale(LC_TIME, save_lc_time))
923  elog(FATAL, "failed to restore LC_TIME to \"%s\"", save_lc_time);
924 
925  /*
926  * At this point we've done our best to clean up, and can throw errors, or
927  * call functions that might throw errors, with a clean conscience.
928  */
929  if (strftimefail)
930  elog(ERROR, "strftime() failed: %m");
931 
932  /* Release the pstrdup'd locale names */
933  pfree(save_lc_time);
934 #ifdef WIN32
935  pfree(save_lc_ctype);
936 #endif
937 
938 #ifndef WIN32
939 
940  /*
941  * As in PGLC_localeconv(), we must convert strftime()'s output from the
942  * encoding implied by LC_TIME to the database encoding. If we can't
943  * identify the LC_TIME encoding, just perform encoding validation.
944  */
946  if (encoding < 0)
948 
949 #else
950 
951  /*
952  * On Windows, strftime_win32() always returns UTF8 data, so convert from
953  * that if necessary.
954  */
955  encoding = PG_UTF8;
956 
957 #endif /* WIN32 */
958 
959  bufptr = buf;
960 
961  /* localized days */
962  for (i = 0; i < 7; i++)
963  {
965  bufptr += MAX_L10N_DATA;
967  bufptr += MAX_L10N_DATA;
968  }
969  localized_abbrev_days[7] = NULL;
970  localized_full_days[7] = NULL;
971 
972  /* localized months */
973  for (i = 0; i < 12; i++)
974  {
976  bufptr += MAX_L10N_DATA;
978  bufptr += MAX_L10N_DATA;
979  }
980  localized_abbrev_months[12] = NULL;
981  localized_full_months[12] = NULL;
982 
983  CurrentLCTimeValid = true;
984 }
#define DEBUG3
Definition: elog.h:28
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
int i
Definition: isn.c:72
static struct pg_tm tm
Definition: localtime.c:104
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
char * localized_full_months[12+1]
Definition: pg_locale.c:136
static bool CurrentLCTimeValid
Definition: pg_locale.c:145
char * locale_time
Definition: pg_locale.c:122
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:794
#define MAX_L10N_DATA
Definition: pg_locale.c:90
char * localized_abbrev_months[12+1]
Definition: pg_locale.c:135
char * localized_full_days[7+1]
Definition: pg_locale.c:134
char * localized_abbrev_days[7+1]
Definition: pg_locale.c:133
static char * buf
Definition: pg_test_fsync.c:72
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301
#define setlocale(a, b)
Definition: win32_port.h:485

References buf, cache_single_string(), CurrentLCTimeValid, DEBUG3, elog, encoding, ERROR, FATAL, i, locale_time, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, MAX_L10N_DATA, pfree(), pg_get_encoding_from_locale(), PG_SQL_ASCII, PG_UTF8, pstrdup(), setlocale, and tm.

Referenced by DCH_from_char(), and DCH_to_char().

◆ char2wchar()

size_t char2wchar ( wchar_t *  to,
size_t  tolen,
const char *  from,
size_t  fromlen,
pg_locale_t  locale 
)

Definition at line 433 of file pg_locale_libc.c.

435 {
436  size_t result;
437 
438  if (tolen == 0)
439  return 0;
440 
441 #ifdef WIN32
442  /* See WIN32 "Unicode" comment above */
443  if (GetDatabaseEncoding() == PG_UTF8)
444  {
445  /* Win32 API does not work for zero-length input */
446  if (fromlen == 0)
447  result = 0;
448  else
449  {
450  result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
451  /* A zero return is failure */
452  if (result == 0)
453  result = -1;
454  }
455 
456  if (result != -1)
457  {
458  Assert(result < tolen);
459  /* Append trailing null wchar (MultiByteToWideChar() does not) */
460  to[result] = 0;
461  }
462  }
463  else
464 #endif /* WIN32 */
465  {
466  /* mbstowcs requires ending '\0' */
467  char *str = pnstrdup(from, fromlen);
468 
469  if (locale == (pg_locale_t) 0)
470  {
471  /* Use mbstowcs directly for the default locale */
472  result = mbstowcs(to, str, tolen);
473  }
474  else
475  {
476  /* Use mbstowcs_l for nondefault locales */
477  result = mbstowcs_l(to, str, tolen, locale->info.lt);
478  }
479 
480  pfree(str);
481  }
482 
483  if (result == -1)
484  {
485  /*
486  * Invalid multibyte character encountered. We try to give a useful
487  * error message by letting pg_verifymbstr check the string. But it's
488  * possible that the string is OK to us, and not OK to mbstowcs ---
489  * this suggests that the LC_CTYPE locale is different from the
490  * database encoding. Give a generic error message if pg_verifymbstr
491  * can't find anything wrong.
492  */
493  pg_verifymbstr(from, fromlen, false); /* might not return */
494  /* but if it does ... */
495  ereport(ERROR,
496  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
497  errmsg("invalid multibyte character for locale"),
498  errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
499  }
500 
501  return result;
502 }
#define Assert(condition)
Definition: c.h:861
int errhint(const char *fmt,...)
Definition: elog.c:1317
const char * str
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1556
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
static size_t mbstowcs_l(wchar_t *dest, const char *src, size_t n, locale_t loc)

References Assert, ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), locale, mbstowcs_l(), pfree(), PG_UTF8, pg_verifymbstr(), pnstrdup(), and str.

Referenced by lowerstr_with_len(), str_initcap(), str_tolower(), str_toupper(), t_isalnum(), t_isalpha(), t_isdigit(), t_isprint(), t_isspace(), and TParserInit().

◆ check_locale()

bool check_locale ( int  category,
const char *  locale,
char **  canonname 
)

Definition at line 304 of file pg_locale.c.

305 {
306  char *save;
307  char *res;
308 
309  /* Don't let Windows' non-ASCII locale names in. */
310  if (!pg_is_ascii(locale))
311  {
313  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
314  errmsg("locale name \"%s\" contains non-ASCII characters",
315  locale)));
316  return false;
317  }
318 
319  if (canonname)
320  *canonname = NULL; /* in case of failure */
321 
322  save = setlocale(category, NULL);
323  if (!save)
324  return false; /* won't happen, we hope */
325 
326  /* save may be pointing at a modifiable scratch variable, see above. */
327  save = pstrdup(save);
328 
329  /* set the locale with setlocale, to see if it accepts it. */
330  res = setlocale(category, locale);
331 
332  /* save canonical name if requested. */
333  if (res && canonname)
334  *canonname = pstrdup(res);
335 
336  /* restore old value. */
337  if (!setlocale(category, save))
338  elog(WARNING, "failed to restore old locale \"%s\"", save);
339  pfree(save);
340 
341  /* Don't let Windows' non-ASCII locale names out. */
342  if (canonname && *canonname && !pg_is_ascii(*canonname))
343  {
345  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
346  errmsg("locale name \"%s\" contains non-ASCII characters",
347  *canonname)));
348  pfree(*canonname);
349  *canonname = NULL;
350  return false;
351  }
352 
353  return (res != NULL);
354 }
#define WARNING
Definition: elog.h:36
bool pg_is_ascii(const char *str)
Definition: string.c:132

References elog, ereport, errcode(), errmsg(), locale, pfree(), pg_is_ascii(), pstrdup(), res, setlocale, and WARNING.

Referenced by check_locale_messages(), check_locale_monetary(), check_locale_numeric(), check_locale_time(), and createdb().

◆ get_collation_actual_version()

char* get_collation_actual_version ( char  collprovider,
const char *  collcollate 
)

Definition at line 1500 of file pg_locale.c.

1501 {
1502  char *collversion = NULL;
1503 
1504  /*
1505  * The only two supported locales (C and C.UTF-8) are both based on memcmp
1506  * and are not expected to change, but track the version anyway.
1507  *
1508  * Note that the character semantics may change for some locales, but the
1509  * collation version only tracks changes to sort order.
1510  */
1511  if (collprovider == COLLPROVIDER_BUILTIN)
1512  {
1513  if (strcmp(collcollate, "C") == 0)
1514  return "1";
1515  else if (strcmp(collcollate, "C.UTF-8") == 0)
1516  return "1";
1517  else
1518  ereport(ERROR,
1519  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1520  errmsg("invalid locale name \"%s\" for builtin provider",
1521  collcollate)));
1522  }
1523 
1524 #ifdef USE_ICU
1525  if (collprovider == COLLPROVIDER_ICU)
1526  {
1527  UCollator *collator;
1528  UVersionInfo versioninfo;
1529  char buf[U_MAX_VERSION_STRING_LENGTH];
1530 
1531  collator = pg_ucol_open(collcollate);
1532 
1533  ucol_getVersion(collator, versioninfo);
1534  ucol_close(collator);
1535 
1536  u_versionToString(versioninfo, buf);
1537  collversion = pstrdup(buf);
1538  }
1539  else
1540 #endif
1541  if (collprovider == COLLPROVIDER_LIBC &&
1542  pg_strcasecmp("C", collcollate) != 0 &&
1543  pg_strncasecmp("C.", collcollate, 2) != 0 &&
1544  pg_strcasecmp("POSIX", collcollate) != 0)
1545  {
1546 #if defined(__GLIBC__)
1547  /* Use the glibc version because we don't have anything better. */
1548  collversion = pstrdup(gnu_get_libc_version());
1549 #elif defined(LC_VERSION_MASK)
1550  locale_t loc;
1551 
1552  /* Look up FreeBSD collation version. */
1553  loc = newlocale(LC_COLLATE_MASK, collcollate, NULL);
1554  if (loc)
1555  {
1556  collversion =
1557  pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
1558  freelocale(loc);
1559  }
1560  else
1561  ereport(ERROR,
1562  (errmsg("could not load locale \"%s\"", collcollate)));
1563 #elif defined(WIN32)
1564  /*
1565  * If we are targeting Windows Vista and above, we can ask for a name
1566  * given a collation name (earlier versions required a location code
1567  * that we don't have).
1568  */
1569  NLSVERSIONINFOEX version = {sizeof(NLSVERSIONINFOEX)};
1570  WCHAR wide_collcollate[LOCALE_NAME_MAX_LENGTH];
1571 
1572  MultiByteToWideChar(CP_ACP, 0, collcollate, -1, wide_collcollate,
1573  LOCALE_NAME_MAX_LENGTH);
1574  if (!GetNLSVersionEx(COMPARE_STRING, wide_collcollate, &version))
1575  {
1576  /*
1577  * GetNLSVersionEx() wants a language tag such as "en-US", not a
1578  * locale name like "English_United States.1252". Until those
1579  * values can be prevented from entering the system, or 100%
1580  * reliably converted to the more useful tag format, tolerate the
1581  * resulting error and report that we have no version data.
1582  */
1583  if (GetLastError() == ERROR_INVALID_PARAMETER)
1584  return NULL;
1585 
1586  ereport(ERROR,
1587  (errmsg("could not get collation version for locale \"%s\": error code %lu",
1588  collcollate,
1589  GetLastError())));
1590  }
1591  collversion = psprintf("%lu.%lu,%lu.%lu",
1592  (version.dwNLSVersion >> 8) & 0xFFFF,
1593  version.dwNLSVersion & 0xFF,
1594  (version.dwDefinedVersion >> 8) & 0xFFFF,
1595  version.dwDefinedVersion & 0xFF);
1596 #endif
1597  }
1598 
1599  return collversion;
1600 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define locale_t
Definition: win32_port.h:442

References buf, ereport, errcode(), errmsg(), ERROR, locale_t, pg_strcasecmp(), pg_strncasecmp(), psprintf(), and pstrdup().

Referenced by AlterCollation(), AlterDatabaseRefreshColl(), CheckMyDatabase(), create_pg_locale(), createdb(), DefineCollation(), pg_collation_actual_version(), pg_database_collation_actual_version(), and pg_import_system_collations().

◆ icu_language_tag()

char* icu_language_tag ( const char *  loc_str,
int  elevel 
)

Definition at line 1879 of file pg_locale.c.

1880 {
1881 #ifdef USE_ICU
1882  UErrorCode status;
1883  char *langtag;
1884  size_t buflen = 32; /* arbitrary starting buffer size */
1885  const bool strict = true;
1886 
1887  /*
1888  * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1889  * RFC5646 section 4.4). Additionally, in older ICU versions,
1890  * uloc_toLanguageTag() doesn't always return the ultimate length on the
1891  * first call, necessitating a loop.
1892  */
1893  langtag = palloc(buflen);
1894  while (true)
1895  {
1896  status = U_ZERO_ERROR;
1897  uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1898 
1899  /* try again if the buffer is not large enough */
1900  if ((status == U_BUFFER_OVERFLOW_ERROR ||
1901  status == U_STRING_NOT_TERMINATED_WARNING) &&
1902  buflen < MaxAllocSize)
1903  {
1904  buflen = Min(buflen * 2, MaxAllocSize);
1905  langtag = repalloc(langtag, buflen);
1906  continue;
1907  }
1908 
1909  break;
1910  }
1911 
1912  if (U_FAILURE(status))
1913  {
1914  pfree(langtag);
1915 
1916  if (elevel > 0)
1917  ereport(elevel,
1918  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1919  errmsg("could not convert locale name \"%s\" to language tag: %s",
1920  loc_str, u_errorName(status))));
1921  return NULL;
1922  }
1923 
1924  return langtag;
1925 #else /* not USE_ICU */
1926  ereport(ERROR,
1927  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1928  errmsg("ICU is not supported in this build")));
1929  return NULL; /* keep compiler quiet */
1930 #endif /* not USE_ICU */
1931 }
#define Min(x, y)
Definition: c.h:1007
#define MaxAllocSize
Definition: fe_memutils.h:22
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
void * palloc(Size size)
Definition: mcxt.c:1317

References ereport, errcode(), errmsg(), ERROR, MaxAllocSize, Min, palloc(), pfree(), and repalloc().

Referenced by createdb(), DefineCollation(), and pg_import_system_collations().

◆ icu_validate_locale()

void icu_validate_locale ( const char *  loc_str)

Definition at line 1937 of file pg_locale.c.

1938 {
1939 #ifdef USE_ICU
1940  UCollator *collator;
1941  UErrorCode status;
1942  char lang[ULOC_LANG_CAPACITY];
1943  bool found = false;
1944  int elevel = icu_validation_level;
1945 
1946  /* no validation */
1947  if (elevel < 0)
1948  return;
1949 
1950  /* downgrade to WARNING during pg_upgrade */
1951  if (IsBinaryUpgrade && elevel > WARNING)
1952  elevel = WARNING;
1953 
1954  /* validate that we can extract the language */
1955  status = U_ZERO_ERROR;
1956  uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1957  if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1958  {
1959  ereport(elevel,
1960  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1961  errmsg("could not get language from ICU locale \"%s\": %s",
1962  loc_str, u_errorName(status)),
1963  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1964  "icu_validation_level", "disabled")));
1965  return;
1966  }
1967 
1968  /* check for special language name */
1969  if (strcmp(lang, "") == 0 ||
1970  strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1971  found = true;
1972 
1973  /* search for matching language within ICU */
1974  for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1975  {
1976  const char *otherloc = uloc_getAvailable(i);
1977  char otherlang[ULOC_LANG_CAPACITY];
1978 
1979  status = U_ZERO_ERROR;
1980  uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1981  if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1982  continue;
1983 
1984  if (strcmp(lang, otherlang) == 0)
1985  found = true;
1986  }
1987 
1988  if (!found)
1989  ereport(elevel,
1990  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1991  errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1992  loc_str, lang),
1993  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1994  "icu_validation_level", "disabled")));
1995 
1996  /* check that it can be opened */
1997  collator = pg_ucol_open(loc_str);
1998  ucol_close(collator);
1999 #else /* not USE_ICU */
2000  /* could get here if a collation was created by a build with ICU */
2001  ereport(ERROR,
2002  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2003  errmsg("ICU is not supported in this build")));
2004 #endif /* not USE_ICU */
2005 }
bool IsBinaryUpgrade
Definition: globals.c:120
int icu_validation_level
Definition: pg_locale.c:124

References ereport, errcode(), errhint(), errmsg(), ERROR, i, icu_validation_level, IsBinaryUpgrade, and WARNING.

Referenced by createdb(), and DefineCollation().

◆ init_database_collation()

void init_database_collation ( void  )

Definition at line 1353 of file pg_locale.c.

1354 {
1355  HeapTuple tup;
1356  Form_pg_database dbform;
1357  Datum datum;
1358 
1359  /* Fetch our pg_database row normally, via syscache */
1360  tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1361  if (!HeapTupleIsValid(tup))
1362  elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1363  dbform = (Form_pg_database) GETSTRUCT(tup);
1364 
1365  if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1366  {
1367  char *datlocale;
1368 
1369  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1370  datlocale = TextDatumGetCString(datum);
1371 
1372  builtin_validate_locale(dbform->encoding, datlocale);
1373 
1375  default_locale.ctype_is_c = (strcmp(datlocale, "C") == 0);
1376 
1379  }
1380  else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1381  {
1382 #ifdef USE_ICU
1383  char *datlocale;
1384  char *icurules;
1385  bool isnull;
1386 
1387  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1388  datlocale = TextDatumGetCString(datum);
1389 
1390  default_locale.collate_is_c = false;
1391  default_locale.ctype_is_c = false;
1392 
1393  datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull);
1394  if (!isnull)
1395  icurules = TextDatumGetCString(datum);
1396  else
1397  icurules = NULL;
1398 
1400  default_locale.info.icu.ucol = make_icu_collator(datlocale, icurules);
1401 #else
1402  /* could get here if a collation was created by a build with ICU */
1403  ereport(ERROR,
1404  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1405  errmsg("ICU is not supported in this build")));
1406 #endif
1407  }
1408  else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1409  {
1410  const char *datcollate;
1411  const char *datctype;
1412 
1413  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate);
1414  datcollate = TextDatumGetCString(datum);
1415  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype);
1416  datctype = TextDatumGetCString(datum);
1417 
1418  default_locale.collate_is_c = (strcmp(datcollate, "C") == 0) ||
1419  (strcmp(datcollate, "POSIX") == 0);
1420  default_locale.ctype_is_c = (strcmp(datctype, "C") == 0) ||
1421  (strcmp(datctype, "POSIX") == 0);
1422 
1423  default_locale.info.lt = make_libc_collator(datcollate, datctype);
1424  }
1425  else
1426  /* shouldn't happen */
1427  PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1428 
1429 
1430  default_locale.provider = dbform->datlocprovider;
1431 
1432  /*
1433  * Default locale is currently always deterministic. Nondeterministic
1434  * locales currently don't support pattern matching, which would break a
1435  * lot of things if applied globally.
1436  */
1438 
1439  ReleaseSysCache(tup);
1440 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
Oid MyDatabaseId
Definition: globals.c:93
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
static char * datlocale
Definition: initdb.c:149
MemoryContext TopMemoryContext
Definition: mcxt.c:149
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
static struct pg_locale_struct default_locale
Definition: pg_locale.c:141
locale_t make_libc_collator(const char *collate, const char *ctype)
#define PGLOCALE_SUPPORT_ERROR(provider)
Definition: pg_locale.c:81
const char * builtin_validate_locale(int encoding, const char *locale)
Definition: pg_locale.c:1841
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
locale_t lt
Definition: pg_locale.h:91
union pg_locale_struct::@156 info
const char * locale
Definition: pg_locale.h:89
struct pg_locale_struct::@156::@157 builtin
bool deterministic
Definition: pg_locale.h:82
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:595
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:626

References pg_locale_struct::builtin, builtin_validate_locale(), pg_locale_struct::collate_is_c, pg_locale_struct::ctype_is_c, datlocale, default_locale, pg_locale_struct::deterministic, elog, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, pg_locale_struct::info, pg_locale_struct::locale, pg_locale_struct::lt, make_libc_collator(), MemoryContextStrdup(), MyDatabaseId, ObjectIdGetDatum(), PGLOCALE_SUPPORT_ERROR, pg_locale_struct::provider, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, and TopMemoryContext.

Referenced by CheckMyDatabase().

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1451 of file pg_locale.c.

1452 {
1453  collation_cache_entry *cache_entry;
1454  bool found;
1455 
1456  if (collid == DEFAULT_COLLATION_OID)
1457  return &default_locale;
1458 
1459  if (!OidIsValid(collid))
1460  elog(ERROR, "cache lookup failed for collation %u", collid);
1461 
1464 
1465  if (CollationCache == NULL)
1466  {
1468  "collation cache",
1470  CollationCache = collation_cache_create(CollationCacheContext,
1471  16, NULL);
1472  }
1473 
1474  cache_entry = collation_cache_insert(CollationCache, collid, &found);
1475  if (!found)
1476  {
1477  /*
1478  * Make sure cache entry is marked invalid, in case we fail before
1479  * setting things.
1480  */
1481  cache_entry->locale = 0;
1482  }
1483 
1484  if (cache_entry->locale == 0)
1485  {
1487  }
1488 
1490  last_collation_cache_locale = cache_entry->locale;
1491 
1492  return cache_entry->locale;
1493 }
#define OidIsValid(objectId)
Definition: c.h:778
Oid collid
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static pg_locale_t last_collation_cache_locale
Definition: pg_locale.c:180
static pg_locale_t create_pg_locale(Oid collid, MemoryContext context)
Definition: pg_locale.c:1221
static MemoryContext CollationCacheContext
Definition: pg_locale.c:172
static collation_cache_hash * CollationCache
Definition: pg_locale.c:173
static Oid last_collation_cache_oid
Definition: pg_locale.c:179
Definition: pg_locale.c:150
pg_locale_t locale
Definition: pg_locale.c:152

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CollationCache, CollationCacheContext, collid, create_pg_locale(), default_locale, elog, ERROR, last_collation_cache_locale, last_collation_cache_oid, collation_cache_entry::locale, OidIsValid, and TopMemoryContext.

Referenced by bpchareq(), bpcharne(), btvarstrequalimage(), convert_string_datum(), DefineCollation(), Generic_Text_IC_like(), GenericMatchText(), hashbpchar(), hashbpcharextended(), hashtext(), hashtextextended(), like_fixed_prefix(), make_greater_string(), match_pattern_prefix(), pg_set_regex_collation(), spg_text_inner_consistent(), str_initcap(), str_tolower(), str_toupper(), text_position_setup(), text_starts_with(), texteq(), textne(), varstr_cmp(), and varstr_sortsupport().

◆ pg_perm_setlocale()

char* pg_perm_setlocale ( int  category,
const char *  locale 
)

Definition at line 201 of file pg_locale.c.

202 {
203  char *result;
204  const char *envvar;
205 
206 #ifndef WIN32
207  result = setlocale(category, locale);
208 #else
209 
210  /*
211  * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
212  * the given value is good and set it in the environment variables. We
213  * must ignore attempts to set to "", which means "keep using the old
214  * environment value".
215  */
216 #ifdef LC_MESSAGES
217  if (category == LC_MESSAGES)
218  {
219  result = (char *) locale;
220  if (locale == NULL || locale[0] == '\0')
221  return result;
222  }
223  else
224 #endif
225  result = setlocale(category, locale);
226 #endif /* WIN32 */
227 
228  if (result == NULL)
229  return result; /* fall out immediately on failure */
230 
231  /*
232  * Use the right encoding in translated messages. Under ENABLE_NLS, let
233  * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
234  * format strings are ASCII, but database-encoding strings may enter the
235  * message via %s. This makes the overall message encoding equal to the
236  * database encoding.
237  */
238  if (category == LC_CTYPE)
239  {
240  static char save_lc_ctype[LOCALE_NAME_BUFLEN];
241 
242  /* copy setlocale() return value before callee invokes it again */
243  strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
244  result = save_lc_ctype;
245 
246 #ifdef ENABLE_NLS
247  SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
248 #else
250 #endif
251  }
252 
253  switch (category)
254  {
255  case LC_COLLATE:
256  envvar = "LC_COLLATE";
257  break;
258  case LC_CTYPE:
259  envvar = "LC_CTYPE";
260  break;
261 #ifdef LC_MESSAGES
262  case LC_MESSAGES:
263  envvar = "LC_MESSAGES";
264 #ifdef WIN32
265  result = IsoLocaleName(locale);
266  if (result == NULL)
267  result = (char *) locale;
268  elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
269 #endif /* WIN32 */
270  break;
271 #endif /* LC_MESSAGES */
272  case LC_MONETARY:
273  envvar = "LC_MONETARY";
274  break;
275  case LC_NUMERIC:
276  envvar = "LC_NUMERIC";
277  break;
278  case LC_TIME:
279  envvar = "LC_TIME";
280  break;
281  default:
282  elog(FATAL, "unrecognized LC category: %d", category);
283  return NULL; /* keep compiler quiet */
284  }
285 
286  if (setenv(envvar, result, 1) != 0)
287  return NULL;
288 
289  return result;
290 }
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1171
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:33
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define setenv(x, y, z)
Definition: win32_port.h:555

References DEBUG3, elog, FATAL, GetDatabaseEncoding(), locale, LOCALE_NAME_BUFLEN, setenv, setlocale, SetMessageEncoding(), and strlcpy().

Referenced by assign_locale_messages(), CheckMyDatabase(), and init_locale().

◆ pg_strcoll()

int pg_strcoll ( const char *  arg1,
const char *  arg2,
pg_locale_t  locale 
)

Definition at line 1608 of file pg_locale.c.

1609 {
1610  int result;
1611 
1612  if (locale->provider == COLLPROVIDER_LIBC)
1613  result = strncoll_libc(arg1, -1, arg2, -1, locale);
1614 #ifdef USE_ICU
1615  else if (locale->provider == COLLPROVIDER_ICU)
1616  result = strncoll_icu(arg1, -1, arg2, -1, locale);
1617 #endif
1618  else
1619  /* shouldn't happen */
1620  PGLOCALE_SUPPORT_ERROR(locale->provider);
1621 
1622  return result;
1623 }
int strncoll_libc(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)

References locale, PGLOCALE_SUPPORT_ERROR, and strncoll_libc().

Referenced by varstrfastcmp_locale().

◆ pg_strncoll()

int pg_strncoll ( const char *  arg1,
ssize_t  len1,
const char *  arg2,
ssize_t  len2,
pg_locale_t  locale 
)

Definition at line 1640 of file pg_locale.c.

1642 {
1643  int result;
1644 
1645  if (locale->provider == COLLPROVIDER_LIBC)
1646  result = strncoll_libc(arg1, len1, arg2, len2, locale);
1647 #ifdef USE_ICU
1648  else if (locale->provider == COLLPROVIDER_ICU)
1649  result = strncoll_icu(arg1, len1, arg2, len2, locale);
1650 #endif
1651  else
1652  /* shouldn't happen */
1653  PGLOCALE_SUPPORT_ERROR(locale->provider);
1654 
1655  return result;
1656 }

References locale, PGLOCALE_SUPPORT_ERROR, and strncoll_libc().

Referenced by varstr_cmp().

◆ pg_strnxfrm()

size_t pg_strnxfrm ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1733 of file pg_locale.c.

1735 {
1736  size_t result = 0; /* keep compiler quiet */
1737 
1738  if (locale->provider == COLLPROVIDER_LIBC)
1739  result = strnxfrm_libc(dest, destsize, src, srclen, locale);
1740 #ifdef USE_ICU
1741  else if (locale->provider == COLLPROVIDER_ICU)
1742  result = strnxfrm_icu(dest, destsize, src, srclen, locale);
1743 #endif
1744  else
1745  /* shouldn't happen */
1746  PGLOCALE_SUPPORT_ERROR(locale->provider);
1747 
1748  return result;
1749 }
size_t strnxfrm_libc(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)

References generate_unaccent_rules::dest, locale, PGLOCALE_SUPPORT_ERROR, and strnxfrm_libc().

Referenced by hashbpchar(), hashbpcharextended(), hashtext(), and hashtextextended().

◆ pg_strnxfrm_prefix()

size_t pg_strnxfrm_prefix ( char *  dest,
size_t  destsize,
const char *  src,
ssize_t  srclen,
pg_locale_t  locale 
)

Definition at line 1800 of file pg_locale.c.

1802 {
1803  size_t result = 0; /* keep compiler quiet */
1804 
1805 #ifdef USE_ICU
1806  if (locale->provider == COLLPROVIDER_ICU)
1807  result = strnxfrm_prefix_icu(dest, destsize, src, -1, locale);
1808  else
1809 #endif
1810  PGLOCALE_SUPPORT_ERROR(locale->provider);
1811 
1812  return result;
1813 }

References generate_unaccent_rules::dest, locale, and PGLOCALE_SUPPORT_ERROR.

Referenced by pg_strxfrm_prefix().

◆ pg_strxfrm()

size_t pg_strxfrm ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1696 of file pg_locale.c.

1697 {
1698  size_t result = 0; /* keep compiler quiet */
1699 
1700  if (locale->provider == COLLPROVIDER_LIBC)
1701  result = strnxfrm_libc(dest, destsize, src, -1, locale);
1702 #ifdef USE_ICU
1703  else if (locale->provider == COLLPROVIDER_ICU)
1704  result = strnxfrm_icu(dest, destsize, src, -1, locale);
1705 #endif
1706  else
1707  /* shouldn't happen */
1708  PGLOCALE_SUPPORT_ERROR(locale->provider);
1709 
1710  return result;
1711 }

References generate_unaccent_rules::dest, locale, PGLOCALE_SUPPORT_ERROR, and strnxfrm_libc().

Referenced by convert_string_datum(), and varstr_abbrev_convert().

◆ pg_strxfrm_enabled()

bool pg_strxfrm_enabled ( pg_locale_t  locale)

Definition at line 1673 of file pg_locale.c.

1674 {
1675  if (locale->provider == COLLPROVIDER_LIBC)
1676 #ifdef TRUST_STRXFRM
1677  return true;
1678 #else
1679  return false;
1680 #endif
1681  else if (locale->provider == COLLPROVIDER_ICU)
1682  return true;
1683  else
1684  /* shouldn't happen */
1685  PGLOCALE_SUPPORT_ERROR(locale->provider);
1686 
1687  return false; /* keep compiler quiet */
1688 }

References locale, and PGLOCALE_SUPPORT_ERROR.

Referenced by varstr_sortsupport().

◆ pg_strxfrm_prefix()

size_t pg_strxfrm_prefix ( char *  dest,
const char *  src,
size_t  destsize,
pg_locale_t  locale 
)

Definition at line 1775 of file pg_locale.c.

1777 {
1778  return pg_strnxfrm_prefix(dest, destsize, src, -1, locale);
1779 }
size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1800

References generate_unaccent_rules::dest, locale, and pg_strnxfrm_prefix().

Referenced by varstr_abbrev_convert().

◆ pg_strxfrm_prefix_enabled()

bool pg_strxfrm_prefix_enabled ( pg_locale_t  locale)

Definition at line 1756 of file pg_locale.c.

1757 {
1758  if (locale->provider == COLLPROVIDER_LIBC)
1759  return false;
1760  else if (locale->provider == COLLPROVIDER_ICU)
1761  return true;
1762  else
1763  /* shouldn't happen */
1764  PGLOCALE_SUPPORT_ERROR(locale->provider);
1765 
1766  return false; /* keep compiler quiet */
1767 }

References locale, and PGLOCALE_SUPPORT_ERROR.

Referenced by varstr_abbrev_convert().

◆ PGLC_localeconv()

struct lconv* PGLC_localeconv ( void  )

Definition at line 535 of file pg_locale.c.

536 {
537  static struct lconv CurrentLocaleConv;
538  static bool CurrentLocaleConvAllocated = false;
539  struct lconv *extlconv;
540  struct lconv worklconv;
541  char *save_lc_monetary;
542  char *save_lc_numeric;
543 #ifdef WIN32
544  char *save_lc_ctype;
545 #endif
546 
547  /* Did we do it already? */
549  return &CurrentLocaleConv;
550 
551  /* Free any already-allocated storage */
552  if (CurrentLocaleConvAllocated)
553  {
554  free_struct_lconv(&CurrentLocaleConv);
555  CurrentLocaleConvAllocated = false;
556  }
557 
558  /*
559  * This is tricky because we really don't want to risk throwing error
560  * while the locale is set to other than our usual settings. Therefore,
561  * the process is: collect the usual settings, set locale to special
562  * setting, copy relevant data into worklconv using strdup(), restore
563  * normal settings, convert data to desired encoding, and finally stash
564  * the collected data in CurrentLocaleConv. This makes it safe if we
565  * throw an error during encoding conversion or run out of memory anywhere
566  * in the process. All data pointed to by struct lconv members is
567  * allocated with strdup, to avoid premature elog(ERROR) and to allow
568  * using a single cleanup routine.
569  */
570  memset(&worklconv, 0, sizeof(worklconv));
571 
572  /* Save prevailing values of monetary and numeric locales */
573  save_lc_monetary = setlocale(LC_MONETARY, NULL);
574  if (!save_lc_monetary)
575  elog(ERROR, "setlocale(NULL) failed");
576  save_lc_monetary = pstrdup(save_lc_monetary);
577 
578  save_lc_numeric = setlocale(LC_NUMERIC, NULL);
579  if (!save_lc_numeric)
580  elog(ERROR, "setlocale(NULL) failed");
581  save_lc_numeric = pstrdup(save_lc_numeric);
582 
583 #ifdef WIN32
584 
585  /*
586  * The POSIX standard explicitly says that it is undefined what happens if
587  * LC_MONETARY or LC_NUMERIC imply an encoding (codeset) different from
588  * that implied by LC_CTYPE. In practice, all Unix-ish platforms seem to
589  * believe that localeconv() should return strings that are encoded in the
590  * codeset implied by the LC_MONETARY or LC_NUMERIC locale name. Hence,
591  * once we have successfully collected the localeconv() results, we will
592  * convert them from that codeset to the desired server encoding.
593  *
594  * Windows, of course, resolutely does things its own way; on that
595  * platform LC_CTYPE has to match LC_MONETARY/LC_NUMERIC to get sane
596  * results. Hence, we must temporarily set that category as well.
597  */
598 
599  /* Save prevailing value of ctype locale */
600  save_lc_ctype = setlocale(LC_CTYPE, NULL);
601  if (!save_lc_ctype)
602  elog(ERROR, "setlocale(NULL) failed");
603  save_lc_ctype = pstrdup(save_lc_ctype);
604 
605  /* Here begins the critical section where we must not throw error */
606 
607  /* use numeric to set the ctype */
608  setlocale(LC_CTYPE, locale_numeric);
609 #endif
610 
611  /* Get formatting information for numeric */
612  setlocale(LC_NUMERIC, locale_numeric);
613  extlconv = localeconv();
614 
615  /* Must copy data now in case setlocale() overwrites it */
616  worklconv.decimal_point = strdup(extlconv->decimal_point);
617  worklconv.thousands_sep = strdup(extlconv->thousands_sep);
618  worklconv.grouping = strdup(extlconv->grouping);
619 
620 #ifdef WIN32
621  /* use monetary to set the ctype */
622  setlocale(LC_CTYPE, locale_monetary);
623 #endif
624 
625  /* Get formatting information for monetary */
626  setlocale(LC_MONETARY, locale_monetary);
627  extlconv = localeconv();
628 
629  /* Must copy data now in case setlocale() overwrites it */
630  worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
631  worklconv.currency_symbol = strdup(extlconv->currency_symbol);
632  worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
633  worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
634  worklconv.mon_grouping = strdup(extlconv->mon_grouping);
635  worklconv.positive_sign = strdup(extlconv->positive_sign);
636  worklconv.negative_sign = strdup(extlconv->negative_sign);
637  /* Copy scalar fields as well */
638  worklconv.int_frac_digits = extlconv->int_frac_digits;
639  worklconv.frac_digits = extlconv->frac_digits;
640  worklconv.p_cs_precedes = extlconv->p_cs_precedes;
641  worklconv.p_sep_by_space = extlconv->p_sep_by_space;
642  worklconv.n_cs_precedes = extlconv->n_cs_precedes;
643  worklconv.n_sep_by_space = extlconv->n_sep_by_space;
644  worklconv.p_sign_posn = extlconv->p_sign_posn;
645  worklconv.n_sign_posn = extlconv->n_sign_posn;
646 
647  /*
648  * Restore the prevailing locale settings; failure to do so is fatal.
649  * Possibly we could limp along with nondefault LC_MONETARY or LC_NUMERIC,
650  * but proceeding with the wrong value of LC_CTYPE would certainly be bad
651  * news; and considering that the prevailing LC_MONETARY and LC_NUMERIC
652  * are almost certainly "C", there's really no reason that restoring those
653  * should fail.
654  */
655 #ifdef WIN32
656  if (!setlocale(LC_CTYPE, save_lc_ctype))
657  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
658 #endif
659  if (!setlocale(LC_MONETARY, save_lc_monetary))
660  elog(FATAL, "failed to restore LC_MONETARY to \"%s\"", save_lc_monetary);
661  if (!setlocale(LC_NUMERIC, save_lc_numeric))
662  elog(FATAL, "failed to restore LC_NUMERIC to \"%s\"", save_lc_numeric);
663 
664  /*
665  * At this point we've done our best to clean up, and can call functions
666  * that might possibly throw errors with a clean conscience. But let's
667  * make sure we don't leak any already-strdup'd fields in worklconv.
668  */
669  PG_TRY();
670  {
671  int encoding;
672 
673  /* Release the pstrdup'd locale names */
674  pfree(save_lc_monetary);
675  pfree(save_lc_numeric);
676 #ifdef WIN32
677  pfree(save_lc_ctype);
678 #endif
679 
680  /* If any of the preceding strdup calls failed, complain now. */
681  if (!struct_lconv_is_valid(&worklconv))
682  ereport(ERROR,
683  (errcode(ERRCODE_OUT_OF_MEMORY),
684  errmsg("out of memory")));
685 
686  /*
687  * Now we must perform encoding conversion from whatever's associated
688  * with the locales into the database encoding. If we can't identify
689  * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
690  * use PG_SQL_ASCII, which will result in just validating that the
691  * strings are OK in the database encoding.
692  */
694  if (encoding < 0)
696 
697  db_encoding_convert(encoding, &worklconv.decimal_point);
698  db_encoding_convert(encoding, &worklconv.thousands_sep);
699  /* grouping is not text and does not require conversion */
700 
702  if (encoding < 0)
704 
705  db_encoding_convert(encoding, &worklconv.int_curr_symbol);
706  db_encoding_convert(encoding, &worklconv.currency_symbol);
707  db_encoding_convert(encoding, &worklconv.mon_decimal_point);
708  db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
709  /* mon_grouping is not text and does not require conversion */
710  db_encoding_convert(encoding, &worklconv.positive_sign);
711  db_encoding_convert(encoding, &worklconv.negative_sign);
712  }
713  PG_CATCH();
714  {
715  free_struct_lconv(&worklconv);
716  PG_RE_THROW();
717  }
718  PG_END_TRY();
719 
720  /*
721  * Everything is good, so save the results.
722  */
723  CurrentLocaleConv = worklconv;
724  CurrentLocaleConvAllocated = true;
725  CurrentLocaleConvValid = true;
726  return &CurrentLocaleConv;
727 }
#define PG_RE_THROW()
Definition: elog.h:412
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
char * locale_numeric
Definition: pg_locale.c:121
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:505
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:455
static bool CurrentLocaleConvValid
Definition: pg_locale.c:144
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:474
char * locale_monetary
Definition: pg_locale.c:120

References CurrentLocaleConvValid, db_encoding_convert(), elog, encoding, ereport, errcode(), errmsg(), ERROR, FATAL, free_struct_lconv(), locale_monetary, locale_numeric, pfree(), PG_CATCH, PG_END_TRY, pg_get_encoding_from_locale(), PG_RE_THROW, PG_SQL_ASCII, PG_TRY, pstrdup(), setlocale, and struct_lconv_is_valid().

Referenced by cash_in(), cash_numeric(), cash_out(), int4_cash(), int8_cash(), NUM_prepare_locale(), and numeric_cash().

◆ wchar2char()

size_t wchar2char ( char *  to,
const wchar_t *  from,
size_t  tolen,
pg_locale_t  locale 
)

Definition at line 379 of file pg_locale_libc.c.

380 {
381  size_t result;
382 
383  if (tolen == 0)
384  return 0;
385 
386 #ifdef WIN32
387 
388  /*
389  * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
390  * for some reason mbstowcs and wcstombs won't do this for us, so we use
391  * MultiByteToWideChar().
392  */
393  if (GetDatabaseEncoding() == PG_UTF8)
394  {
395  result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
396  NULL, NULL);
397  /* A zero return is failure */
398  if (result <= 0)
399  result = -1;
400  else
401  {
402  Assert(result <= tolen);
403  /* Microsoft counts the zero terminator in the result */
404  result--;
405  }
406  }
407  else
408 #endif /* WIN32 */
409  if (locale == (pg_locale_t) 0)
410  {
411  /* Use wcstombs directly for the default locale */
412  result = wcstombs(to, from, tolen);
413  }
414  else
415  {
416  /* Use wcstombs_l for nondefault locales */
417  result = wcstombs_l(to, from, tolen, locale->info.lt);
418  }
419 
420  return result;
421 }
static size_t wcstombs_l(char *dest, const wchar_t *src, size_t n, locale_t loc)

References Assert, GetDatabaseEncoding(), locale, PG_UTF8, and wcstombs_l().

Referenced by lowerstr_with_len(), str_initcap(), str_tolower(), and str_toupper().

Variable Documentation

◆ database_ctype_is_c

PGDLLIMPORT bool database_ctype_is_c
extern

◆ icu_validation_level

PGDLLIMPORT int icu_validation_level
extern

Definition at line 124 of file pg_locale.c.

Referenced by createdb(), DefineCollation(), and icu_validate_locale().

◆ locale_messages

PGDLLIMPORT char* locale_messages
extern

Definition at line 119 of file pg_locale.c.

◆ locale_monetary

PGDLLIMPORT char* locale_monetary
extern

Definition at line 120 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

PGDLLIMPORT char* locale_numeric
extern

Definition at line 121 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

PGDLLIMPORT char* locale_time
extern

Definition at line 122 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

PGDLLIMPORT char* localized_abbrev_days[]
extern

Definition at line 133 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_abbrev_months

PGDLLIMPORT char* localized_abbrev_months[]
extern

Definition at line 135 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_days

PGDLLIMPORT char* localized_full_days[]
extern

Definition at line 134 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().

◆ localized_full_months

PGDLLIMPORT char* localized_full_months[]
extern

Definition at line 136 of file pg_locale.c.

Referenced by cache_locale_time(), DCH_from_char(), and DCH_to_char().