PostgreSQL Source Code  git master
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 103 of file pg_locale.h.

Function Documentation

◆ builtin_locale_encoding()

int builtin_locale_encoding ( const char *  locale)

Definition at line 1803 of file pg_locale.c.

1804 {
1805  if (strcmp(locale, "C") == 0)
1806  return -1;
1807  if (strcmp(locale, "C.UTF-8") == 0)
1808  return PG_UTF8;
1809 
1810  ereport(ERROR,
1811  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1812  errmsg("invalid locale name \"%s\" for builtin provider",
1813  locale)));
1814 
1815  return 0; /* keep compiler quiet */
1816 }
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 1824 of file pg_locale.c.

1825 {
1826  const char *canonical_name = NULL;
1827  int required_encoding;
1828 
1829  if (strcmp(locale, "C") == 0)
1830  canonical_name = "C";
1831  else if (strcmp(locale, "C.UTF-8") == 0 || strcmp(locale, "C.UTF8") == 0)
1832  canonical_name = "C.UTF-8";
1833 
1834  if (!canonical_name)
1835  ereport(ERROR,
1836  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1837  errmsg("invalid locale name \"%s\" for builtin provider",
1838  locale)));
1839 
1840  required_encoding = builtin_locale_encoding(canonical_name);
1841  if (required_encoding >= 0 && encoding != required_encoding)
1842  ereport(ERROR,
1843  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1844  errmsg("encoding \"%s\" does not match locale \"%s\"",
1846 
1847  return canonical_name;
1848 }
int32 encoding
Definition: pg_database.h:41
int builtin_locale_encoding(const char *locale)
Definition: pg_locale.c:1803
#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:837
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 1483 of file pg_locale.c.

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

1863 {
1864 #ifdef USE_ICU
1865  UErrorCode status;
1866  char *langtag;
1867  size_t buflen = 32; /* arbitrary starting buffer size */
1868  const bool strict = true;
1869 
1870  /*
1871  * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
1872  * RFC5646 section 4.4). Additionally, in older ICU versions,
1873  * uloc_toLanguageTag() doesn't always return the ultimate length on the
1874  * first call, necessitating a loop.
1875  */
1876  langtag = palloc(buflen);
1877  while (true)
1878  {
1879  status = U_ZERO_ERROR;
1880  uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
1881 
1882  /* try again if the buffer is not large enough */
1883  if ((status == U_BUFFER_OVERFLOW_ERROR ||
1884  status == U_STRING_NOT_TERMINATED_WARNING) &&
1885  buflen < MaxAllocSize)
1886  {
1887  buflen = Min(buflen * 2, MaxAllocSize);
1888  langtag = repalloc(langtag, buflen);
1889  continue;
1890  }
1891 
1892  break;
1893  }
1894 
1895  if (U_FAILURE(status))
1896  {
1897  pfree(langtag);
1898 
1899  if (elevel > 0)
1900  ereport(elevel,
1901  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1902  errmsg("could not convert locale name \"%s\" to language tag: %s",
1903  loc_str, u_errorName(status))));
1904  return NULL;
1905  }
1906 
1907  return langtag;
1908 #else /* not USE_ICU */
1909  ereport(ERROR,
1910  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1911  errmsg("ICU is not supported in this build")));
1912  return NULL; /* keep compiler quiet */
1913 #endif /* not USE_ICU */
1914 }
#define Min(x, y)
Definition: c.h:983
#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 1920 of file pg_locale.c.

1921 {
1922 #ifdef USE_ICU
1923  UCollator *collator;
1924  UErrorCode status;
1925  char lang[ULOC_LANG_CAPACITY];
1926  bool found = false;
1927  int elevel = icu_validation_level;
1928 
1929  /* no validation */
1930  if (elevel < 0)
1931  return;
1932 
1933  /* downgrade to WARNING during pg_upgrade */
1934  if (IsBinaryUpgrade && elevel > WARNING)
1935  elevel = WARNING;
1936 
1937  /* validate that we can extract the language */
1938  status = U_ZERO_ERROR;
1939  uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
1940  if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1941  {
1942  ereport(elevel,
1943  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1944  errmsg("could not get language from ICU locale \"%s\": %s",
1945  loc_str, u_errorName(status)),
1946  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1947  "icu_validation_level", "disabled")));
1948  return;
1949  }
1950 
1951  /* check for special language name */
1952  if (strcmp(lang, "") == 0 ||
1953  strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1954  found = true;
1955 
1956  /* search for matching language within ICU */
1957  for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
1958  {
1959  const char *otherloc = uloc_getAvailable(i);
1960  char otherlang[ULOC_LANG_CAPACITY];
1961 
1962  status = U_ZERO_ERROR;
1963  uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
1964  if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1965  continue;
1966 
1967  if (strcmp(lang, otherlang) == 0)
1968  found = true;
1969  }
1970 
1971  if (!found)
1972  ereport(elevel,
1973  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1974  errmsg("ICU locale \"%s\" has unknown language \"%s\"",
1975  loc_str, lang),
1976  errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
1977  "icu_validation_level", "disabled")));
1978 
1979  /* check that it can be opened */
1980  collator = pg_ucol_open(loc_str);
1981  ucol_close(collator);
1982 #else /* not USE_ICU */
1983  /* could get here if a collation was created by a build with ICU */
1984  ereport(ERROR,
1985  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1986  errmsg("ICU is not supported in this build")));
1987 #endif /* not USE_ICU */
1988 }
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 1335 of file pg_locale.c.

1336 {
1337  HeapTuple tup;
1338  Form_pg_database dbform;
1339  Datum datum;
1340 
1341  /* Fetch our pg_database row normally, via syscache */
1342  tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1343  if (!HeapTupleIsValid(tup))
1344  elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1345  dbform = (Form_pg_database) GETSTRUCT(tup);
1346 
1347  if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1348  {
1349  char *datlocale;
1350 
1351  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1352  datlocale = TextDatumGetCString(datum);
1353 
1354  builtin_validate_locale(dbform->encoding, datlocale);
1355 
1357  default_locale.ctype_is_c = (strcmp(datlocale, "C") == 0);
1358 
1360  datlocale);
1361  }
1362  else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1363  {
1364 #ifdef USE_ICU
1365  char *datlocale;
1366  char *icurules;
1367  bool isnull;
1368 
1369  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1370  datlocale = TextDatumGetCString(datum);
1371 
1372  default_locale.collate_is_c = false;
1373  default_locale.ctype_is_c = false;
1374 
1375  datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull);
1376  if (!isnull)
1377  icurules = TextDatumGetCString(datum);
1378  else
1379  icurules = NULL;
1380 
1382  default_locale.info.icu.ucol = make_icu_collator(datlocale, icurules);
1383 #else
1384  /* could get here if a collation was created by a build with ICU */
1385  ereport(ERROR,
1386  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1387  errmsg("ICU is not supported in this build")));
1388 #endif
1389  }
1390  else if (dbform->datlocprovider == COLLPROVIDER_LIBC)
1391  {
1392  const char *datcollate;
1393  const char *datctype;
1394 
1395  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datcollate);
1396  datcollate = TextDatumGetCString(datum);
1397  datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datctype);
1398  datctype = TextDatumGetCString(datum);
1399 
1400  default_locale.collate_is_c = (strcmp(datcollate, "C") == 0) ||
1401  (strcmp(datcollate, "POSIX") == 0);
1402  default_locale.ctype_is_c = (strcmp(datctype, "C") == 0) ||
1403  (strcmp(datctype, "POSIX") == 0);
1404 
1405  default_locale.info.lt = make_libc_collator(datcollate, datctype);
1406  }
1407  else
1408  /* shouldn't happen */
1409  PGLOCALE_SUPPORT_ERROR(dbform->datlocprovider);
1410 
1411 
1412  default_locale.provider = dbform->datlocprovider;
1413  default_locale.is_default = true;
1414 
1415  /*
1416  * Default locale is currently always deterministic. Nondeterministic
1417  * locales currently don't support pattern matching, which would break a
1418  * lot of things if applied globally.
1419  */
1421 
1422  ReleaseSysCache(tup);
1423 }
#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:1824
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
locale_t lt
Definition: pg_locale.h:92
union pg_locale_struct::@156 info
const char * locale
Definition: pg_locale.h:90
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:600
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631

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::is_default, 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 1434 of file pg_locale.c.

1435 {
1436  collation_cache_entry *cache_entry;
1437  bool found;
1438 
1439  if (collid == DEFAULT_COLLATION_OID)
1440  return &default_locale;
1441 
1442  if (!OidIsValid(collid))
1443  elog(ERROR, "cache lookup failed for collation %u", collid);
1444 
1447 
1448  if (CollationCache == NULL)
1449  {
1451  "collation cache",
1453  CollationCache = collation_cache_create(CollationCacheContext,
1454  16, NULL);
1455  }
1456 
1457  cache_entry = collation_cache_insert(CollationCache, collid, &found);
1458  if (!found)
1459  {
1460  /*
1461  * Make sure cache entry is marked invalid, in case we fail before
1462  * setting things.
1463  */
1464  cache_entry->locale = 0;
1465  }
1466 
1467  if (cache_entry->locale == 0)
1468  {
1470  }
1471 
1473  last_collation_cache_locale = cache_entry->locale;
1474 
1475  return cache_entry->locale;
1476 }
#define OidIsValid(objectId)
Definition: c.h:754
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:1202
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 1591 of file pg_locale.c.

1592 {
1593  int result;
1594 
1595  if (locale->provider == COLLPROVIDER_LIBC)
1596  result = strncoll_libc(arg1, -1, arg2, -1, locale);
1597 #ifdef USE_ICU
1598  else if (locale->provider == COLLPROVIDER_ICU)
1599  result = strncoll_icu(arg1, -1, arg2, -1, locale);
1600 #endif
1601  else
1602  /* shouldn't happen */
1603  PGLOCALE_SUPPORT_ERROR(locale->provider);
1604 
1605  return result;
1606 }
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 1623 of file pg_locale.c.

1625 {
1626  int result;
1627 
1628  if (locale->provider == COLLPROVIDER_LIBC)
1629  result = strncoll_libc(arg1, len1, arg2, len2, locale);
1630 #ifdef USE_ICU
1631  else if (locale->provider == COLLPROVIDER_ICU)
1632  result = strncoll_icu(arg1, len1, arg2, len2, locale);
1633 #endif
1634  else
1635  /* shouldn't happen */
1636  PGLOCALE_SUPPORT_ERROR(locale->provider);
1637 
1638  return result;
1639 }

References locale, PGLOCALE_SUPPORT_ERROR, and strncoll_libc().

Referenced by MatchText(), and 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 1716 of file pg_locale.c.

1718 {
1719  size_t result = 0; /* keep compiler quiet */
1720 
1721  if (locale->provider == COLLPROVIDER_LIBC)
1722  result = strnxfrm_libc(dest, destsize, src, srclen, locale);
1723 #ifdef USE_ICU
1724  else if (locale->provider == COLLPROVIDER_ICU)
1725  result = strnxfrm_icu(dest, destsize, src, srclen, locale);
1726 #endif
1727  else
1728  /* shouldn't happen */
1729  PGLOCALE_SUPPORT_ERROR(locale->provider);
1730 
1731  return result;
1732 }
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 1783 of file pg_locale.c.

1785 {
1786  size_t result = 0; /* keep compiler quiet */
1787 
1788 #ifdef USE_ICU
1789  if (locale->provider == COLLPROVIDER_ICU)
1790  result = strnxfrm_prefix_icu(dest, destsize, src, -1, locale);
1791  else
1792 #endif
1793  PGLOCALE_SUPPORT_ERROR(locale->provider);
1794 
1795  return result;
1796 }

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 1679 of file pg_locale.c.

1680 {
1681  size_t result = 0; /* keep compiler quiet */
1682 
1683  if (locale->provider == COLLPROVIDER_LIBC)
1684  result = strnxfrm_libc(dest, destsize, src, -1, locale);
1685 #ifdef USE_ICU
1686  else if (locale->provider == COLLPROVIDER_ICU)
1687  result = strnxfrm_icu(dest, destsize, src, -1, locale);
1688 #endif
1689  else
1690  /* shouldn't happen */
1691  PGLOCALE_SUPPORT_ERROR(locale->provider);
1692 
1693  return result;
1694 }

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 1656 of file pg_locale.c.

1657 {
1658  if (locale->provider == COLLPROVIDER_LIBC)
1659 #ifdef TRUST_STRXFRM
1660  return true;
1661 #else
1662  return false;
1663 #endif
1664  else if (locale->provider == COLLPROVIDER_ICU)
1665  return true;
1666  else
1667  /* shouldn't happen */
1668  PGLOCALE_SUPPORT_ERROR(locale->provider);
1669 
1670  return false; /* keep compiler quiet */
1671 }

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 1758 of file pg_locale.c.

1760 {
1761  return pg_strnxfrm_prefix(dest, destsize, src, -1, locale);
1762 }
size_t pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1783

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 1739 of file pg_locale.c.

1740 {
1741  if (locale->provider == COLLPROVIDER_LIBC)
1742  return false;
1743  else if (locale->provider == COLLPROVIDER_ICU)
1744  return true;
1745  else
1746  /* shouldn't happen */
1747  PGLOCALE_SUPPORT_ERROR(locale->provider);
1748 
1749  return false; /* keep compiler quiet */
1750 }

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().