PostgreSQL Source Code  git master
pg_locale.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/htup_details.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_control.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/syscache.h"
Include dependency graph for pg_locale.c:

Go to the source code of this file.

Data Structures

struct  collation_cache_entry
 

Macros

#define MAX_L10N_DATA   80
 
#define LC_ENV_BUFSIZE   (NAMEDATALEN + 20)
 

Functions

char * pg_perm_setlocale (int category, const char *locale)
 
bool check_locale (int category, const char *locale, char **canonname)
 
bool check_locale_monetary (char **newval, void **extra, GucSource source)
 
void assign_locale_monetary (const char *newval, void *extra)
 
bool check_locale_numeric (char **newval, void **extra, GucSource source)
 
void assign_locale_numeric (const char *newval, void *extra)
 
bool check_locale_time (char **newval, void **extra, GucSource source)
 
void assign_locale_time (const char *newval, void *extra)
 
bool check_locale_messages (char **newval, void **extra, GucSource source)
 
void assign_locale_messages (const char *newval, void *extra)
 
static void free_struct_lconv (struct lconv *s)
 
static bool struct_lconv_is_valid (struct lconv *s)
 
static void db_encoding_convert (int encoding, char **str)
 
struct lconv * PGLC_localeconv (void)
 
static void cache_single_string (char **dst, const char *src, int encoding)
 
void cache_locale_time (void)
 
void check_strxfrm_bug (void)
 
static collation_cache_entrylookup_collation_cache (Oid collation, bool set_flags)
 
bool lc_collate_is_c (Oid collation)
 
bool lc_ctype_is_c (Oid collation)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
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

char * locale_messages
 
char * locale_monetary
 
char * locale_numeric
 
char * locale_time
 
char * localized_abbrev_days [7]
 
char * localized_full_days [7]
 
char * localized_abbrev_months [12]
 
char * localized_full_months [12]
 
static bool CurrentLocaleConvValid = false
 
static bool CurrentLCTimeValid = false
 
static char lc_collate_envbuf [LC_ENV_BUFSIZE]
 
static char lc_ctype_envbuf [LC_ENV_BUFSIZE]
 
static char lc_monetary_envbuf [LC_ENV_BUFSIZE]
 
static char lc_numeric_envbuf [LC_ENV_BUFSIZE]
 
static char lc_time_envbuf [LC_ENV_BUFSIZE]
 
static HTABcollation_cache = NULL
 

Macro Definition Documentation

◆ LC_ENV_BUFSIZE

#define LC_ENV_BUFSIZE   (NAMEDATALEN + 20)

Definition at line 107 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ MAX_L10N_DATA

#define MAX_L10N_DATA   80

Definition at line 86 of file pg_locale.c.

Referenced by cache_locale_time(), and PGLC_localeconv().

Function Documentation

◆ assign_locale_messages()

void assign_locale_messages ( const char *  newval,
void *  extra 
)

Definition at line 381 of file pg_locale.c.

References pg_perm_setlocale().

382 {
383  /*
384  * LC_MESSAGES category does not exist everywhere, but accept it anyway.
385  * We ignore failure, as per comment above.
386  */
387 #ifdef LC_MESSAGES
388  (void) pg_perm_setlocale(LC_MESSAGES, newval);
389 #endif
390 }
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:156
#define newval

◆ assign_locale_monetary()

void assign_locale_monetary ( const char *  newval,
void *  extra 
)

Definition at line 318 of file pg_locale.c.

References CurrentLocaleConvValid.

319 {
320  CurrentLocaleConvValid = false;
321 }
static bool CurrentLocaleConvValid
Definition: pg_locale.c:102

◆ assign_locale_numeric()

void assign_locale_numeric ( const char *  newval,
void *  extra 
)

Definition at line 330 of file pg_locale.c.

References CurrentLocaleConvValid.

331 {
332  CurrentLocaleConvValid = false;
333 }
static bool CurrentLocaleConvValid
Definition: pg_locale.c:102

◆ assign_locale_time()

void assign_locale_time ( const char *  newval,
void *  extra 
)

Definition at line 342 of file pg_locale.c.

References CurrentLCTimeValid.

343 {
344  CurrentLCTimeValid = false;
345 }
static bool CurrentLCTimeValid
Definition: pg_locale.c:103

◆ cache_locale_time()

void cache_locale_time ( void  )

Definition at line 770 of file pg_locale.c.

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_strcasecmp(), PG_UTF8, pstrdup(), setlocale, snprintf, tm, and wchar2char().

Referenced by DCH_to_char().

771 {
772  char buf[(2 * 7 + 2 * 12) * MAX_L10N_DATA];
773  char *bufptr;
774  time_t timenow;
775  struct tm *timeinfo;
776  bool strftimefail = false;
777  int encoding;
778  int i;
779  char *save_lc_time;
780 #ifdef WIN32
781  char *save_lc_ctype;
782 #endif
783 
784  /* did we do this already? */
785  if (CurrentLCTimeValid)
786  return;
787 
788  elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
789 
790  /*
791  * As in PGLC_localeconv(), it's critical that we not throw error while
792  * libc's locale settings have nondefault values. Hence, we just call
793  * strftime() within the critical section, and then convert and save its
794  * results afterwards.
795  */
796 
797  /* Save prevailing value of time locale */
798  save_lc_time = setlocale(LC_TIME, NULL);
799  if (!save_lc_time)
800  elog(ERROR, "setlocale(NULL) failed");
801  save_lc_time = pstrdup(save_lc_time);
802 
803 #ifdef WIN32
804 
805  /*
806  * On Windows, it appears that wcsftime() internally uses LC_CTYPE, so we
807  * must set it here. This code looks the same as what PGLC_localeconv()
808  * does, but the underlying reason is different: this does NOT determine
809  * the encoding we'll get back from strftime_win32().
810  */
811 
812  /* Save prevailing value of ctype locale */
813  save_lc_ctype = setlocale(LC_CTYPE, NULL);
814  if (!save_lc_ctype)
815  elog(ERROR, "setlocale(NULL) failed");
816  save_lc_ctype = pstrdup(save_lc_ctype);
817 
818  /* use lc_time to set the ctype */
819  setlocale(LC_CTYPE, locale_time);
820 #endif
821 
822  setlocale(LC_TIME, locale_time);
823 
824  /* We use times close to current time as data for strftime(). */
825  timenow = time(NULL);
826  timeinfo = localtime(&timenow);
827 
828  /* Store the strftime results in MAX_L10N_DATA-sized portions of buf[] */
829  bufptr = buf;
830 
831  /*
832  * MAX_L10N_DATA is sufficient buffer space for every known locale, and
833  * POSIX defines no strftime() errors. (Buffer space exhaustion is not an
834  * error.) An implementation might report errors (e.g. ENOMEM) by
835  * returning 0 (or, less plausibly, a negative value) and setting errno.
836  * Report errno just in case the implementation did that, but clear it in
837  * advance of the calls so we don't emit a stale, unrelated errno.
838  */
839  errno = 0;
840 
841  /* localized days */
842  for (i = 0; i < 7; i++)
843  {
844  timeinfo->tm_wday = i;
845  if (strftime(bufptr, MAX_L10N_DATA, "%a", timeinfo) <= 0)
846  strftimefail = true;
847  bufptr += MAX_L10N_DATA;
848  if (strftime(bufptr, MAX_L10N_DATA, "%A", timeinfo) <= 0)
849  strftimefail = true;
850  bufptr += MAX_L10N_DATA;
851  }
852 
853  /* localized months */
854  for (i = 0; i < 12; i++)
855  {
856  timeinfo->tm_mon = i;
857  timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
858  if (strftime(bufptr, MAX_L10N_DATA, "%b", timeinfo) <= 0)
859  strftimefail = true;
860  bufptr += MAX_L10N_DATA;
861  if (strftime(bufptr, MAX_L10N_DATA, "%B", timeinfo) <= 0)
862  strftimefail = true;
863  bufptr += MAX_L10N_DATA;
864  }
865 
866  /*
867  * Restore the prevailing locale settings; as in PGLC_localeconv(),
868  * failure to do so is fatal.
869  */
870 #ifdef WIN32
871  if (!setlocale(LC_CTYPE, save_lc_ctype))
872  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
873 #endif
874  if (!setlocale(LC_TIME, save_lc_time))
875  elog(FATAL, "failed to restore LC_TIME to \"%s\"", save_lc_time);
876 
877  /*
878  * At this point we've done our best to clean up, and can throw errors, or
879  * call functions that might throw errors, with a clean conscience.
880  */
881  if (strftimefail)
882  elog(ERROR, "strftime() failed: %m");
883 
884  /* Release the pstrdup'd locale names */
885  pfree(save_lc_time);
886 #ifdef WIN32
887  pfree(save_lc_ctype);
888 #endif
889 
890 #ifndef WIN32
891 
892  /*
893  * As in PGLC_localeconv(), we must convert strftime()'s output from the
894  * encoding implied by LC_TIME to the database encoding. If we can't
895  * identify the LC_TIME encoding, just perform encoding validation.
896  */
897  encoding = pg_get_encoding_from_locale(locale_time, true);
898  if (encoding < 0)
899  encoding = PG_SQL_ASCII;
900 
901 #else
902 
903  /*
904  * On Windows, strftime_win32() always returns UTF8 data, so convert from
905  * that if necessary.
906  */
907  encoding = PG_UTF8;
908 
909 #endif /* WIN32 */
910 
911  bufptr = buf;
912 
913  /* localized days */
914  for (i = 0; i < 7; i++)
915  {
916  cache_single_string(&localized_abbrev_days[i], bufptr, encoding);
917  bufptr += MAX_L10N_DATA;
918  cache_single_string(&localized_full_days[i], bufptr, encoding);
919  bufptr += MAX_L10N_DATA;
920  }
921 
922  /* localized months */
923  for (i = 0; i < 12; i++)
924  {
925  cache_single_string(&localized_abbrev_months[i], bufptr, encoding);
926  bufptr += MAX_L10N_DATA;
927  cache_single_string(&localized_full_months[i], bufptr, encoding);
928  bufptr += MAX_L10N_DATA;
929  }
930 
931  CurrentLCTimeValid = true;
932 }
#define setlocale(a, b)
Definition: win32_port.h:417
#define DEBUG3
Definition: elog.h:23
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * localized_abbrev_months[12]
Definition: pg_locale.c:98
static struct pg_tm tm
Definition: localtime.c:108
void pfree(void *pointer)
Definition: mcxt.c:1031
#define MAX_L10N_DATA
Definition: pg_locale.c:86
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static char * buf
Definition: pg_test_fsync.c:68
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
char * localized_full_days[7]
Definition: pg_locale.c:97
static void cache_single_string(char **dst, const char *src, int encoding)
Definition: pg_locale.c:747
int32 encoding
Definition: pg_database.h:41
#define elog(elevel,...)
Definition: elog.h:226
int i
char * localized_full_months[12]
Definition: pg_locale.c:99
char * localized_abbrev_days[7]
Definition: pg_locale.c:96
char * locale_time
Definition: pg_locale.c:93
static bool CurrentLCTimeValid
Definition: pg_locale.c:103

◆ cache_single_string()

static void cache_single_string ( char **  dst,
const char *  src,
int  encoding 
)
static

Definition at line 747 of file pg_locale.c.

References MemoryContextStrdup(), pfree(), pg_any_to_server(), and TopMemoryContext.

Referenced by cache_locale_time().

748 {
749  char *ptr;
750  char *olddst;
751 
752  /* Convert the string to the database encoding, or validate it's OK */
753  ptr = pg_any_to_server(src, strlen(src), encoding);
754 
755  /* Store the string in long-lived storage, replacing any previous value */
756  olddst = *dst;
758  if (olddst)
759  pfree(olddst);
760 
761  /* Might as well clean up any palloc'd conversion result, too */
762  if (ptr != src)
763  pfree(ptr);
764 }
void pfree(void *pointer)
Definition: mcxt.c:1031
MemoryContext TopMemoryContext
Definition: mcxt.c:44
int32 encoding
Definition: pg_database.h:41
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1148
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:553

◆ char2wchar()

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

Definition at line 1845 of file pg_locale.c.

References Assert, elog, ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), pg_locale_struct::info, locale_t, mbstowcs_l, pfree(), PG_UTF8, pg_verifymbstr(), pnstrdup(), pg_locale_struct::provider, and generate_unaccent_rules::str.

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

1847 {
1848  size_t result;
1849 
1850  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1851 
1852  if (tolen == 0)
1853  return 0;
1854 
1855 #ifdef WIN32
1856  /* See WIN32 "Unicode" comment above */
1857  if (GetDatabaseEncoding() == PG_UTF8)
1858  {
1859  /* Win32 API does not work for zero-length input */
1860  if (fromlen == 0)
1861  result = 0;
1862  else
1863  {
1864  result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
1865  /* A zero return is failure */
1866  if (result == 0)
1867  result = -1;
1868  }
1869 
1870  if (result != -1)
1871  {
1872  Assert(result < tolen);
1873  /* Append trailing null wchar (MultiByteToWideChar() does not) */
1874  to[result] = 0;
1875  }
1876  }
1877  else
1878 #endif /* WIN32 */
1879  {
1880  /* mbstowcs requires ending '\0' */
1881  char *str = pnstrdup(from, fromlen);
1882 
1883  if (locale == (pg_locale_t) 0)
1884  {
1885  /* Use mbstowcs directly for the default locale */
1886  result = mbstowcs(to, str, tolen);
1887  }
1888  else
1889  {
1890 #ifdef HAVE_LOCALE_T
1891 #ifdef HAVE_MBSTOWCS_L
1892  /* Use mbstowcs_l for nondefault locales */
1893  result = mbstowcs_l(to, str, tolen, locale->info.lt);
1894 #else /* !HAVE_MBSTOWCS_L */
1895  /* We have to temporarily set the locale as current ... ugh */
1896  locale_t save_locale = uselocale(locale->info.lt);
1897 
1898  result = mbstowcs(to, str, tolen);
1899 
1900  uselocale(save_locale);
1901 #endif /* HAVE_MBSTOWCS_L */
1902 #else /* !HAVE_LOCALE_T */
1903  /* Can't have locale != 0 without HAVE_LOCALE_T */
1904  elog(ERROR, "mbstowcs_l is not available");
1905  result = 0; /* keep compiler quiet */
1906 #endif /* HAVE_LOCALE_T */
1907  }
1908 
1909  pfree(str);
1910  }
1911 
1912  if (result == -1)
1913  {
1914  /*
1915  * Invalid multibyte character encountered. We try to give a useful
1916  * error message by letting pg_verifymbstr check the string. But it's
1917  * possible that the string is OK to us, and not OK to mbstowcs ---
1918  * this suggests that the LC_CTYPE locale is different from the
1919  * database encoding. Give a generic error message if pg_verifymbstr
1920  * can't find anything wrong.
1921  */
1922  pg_verifymbstr(from, fromlen, false); /* might not return */
1923  /* but if it does ... */
1924  ereport(ERROR,
1925  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1926  errmsg("invalid multibyte character for locale"),
1927  errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
1928  }
1929 
1930  return result;
1931 }
int errhint(const char *fmt,...)
Definition: elog.c:974
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1172
int errcode(int sqlerrcode)
Definition: elog.c:570
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define locale_t
Definition: win32_port.h:372
#define ereport(elevel, rest)
Definition: elog.h:141
int GetDatabaseEncoding(void)
Definition: mbutils.c:996
#define Assert(condition)
Definition: c.h:732
#define mbstowcs_l
Definition: win32_port.h:399
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1914
union pg_locale_struct::@143 info

◆ check_locale()

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

Definition at line 269 of file pg_locale.c.

References elog, pfree(), pstrdup(), setlocale, and WARNING.

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

270 {
271  char *save;
272  char *res;
273 
274  if (canonname)
275  *canonname = NULL; /* in case of failure */
276 
277  save = setlocale(category, NULL);
278  if (!save)
279  return false; /* won't happen, we hope */
280 
281  /* save may be pointing at a modifiable scratch variable, see above. */
282  save = pstrdup(save);
283 
284  /* set the locale with setlocale, to see if it accepts it. */
285  res = setlocale(category, locale);
286 
287  /* save canonical name if requested. */
288  if (res && canonname)
289  *canonname = pstrdup(res);
290 
291  /* restore old value. */
292  if (!setlocale(category, save))
293  elog(WARNING, "failed to restore old locale \"%s\"", save);
294  pfree(save);
295 
296  return (res != NULL);
297 }
#define setlocale(a, b)
Definition: win32_port.h:417
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void pfree(void *pointer)
Definition: mcxt.c:1031
#define WARNING
Definition: elog.h:40
#define elog(elevel,...)
Definition: elog.h:226
static char * locale
Definition: initdb.c:125

◆ check_locale_messages()

bool check_locale_messages ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 358 of file pg_locale.c.

References check_locale(), and PGC_S_DEFAULT.

359 {
360  if (**newval == '\0')
361  {
362  if (source == PGC_S_DEFAULT)
363  return true;
364  else
365  return false;
366  }
367 
368  /*
369  * LC_MESSAGES category does not exist everywhere, but accept it anyway
370  *
371  * On Windows, we can't even check the value, so accept blindly
372  */
373 #if defined(LC_MESSAGES) && !defined(WIN32)
374  return check_locale(LC_MESSAGES, *newval, NULL);
375 #else
376  return true;
377 #endif
378 }
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:269
#define newval

◆ check_locale_monetary()

bool check_locale_monetary ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 312 of file pg_locale.c.

References check_locale().

313 {
314  return check_locale(LC_MONETARY, *newval, NULL);
315 }
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:269
#define newval

◆ check_locale_numeric()

bool check_locale_numeric ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 324 of file pg_locale.c.

References check_locale().

325 {
326  return check_locale(LC_NUMERIC, *newval, NULL);
327 }
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:269
#define newval

◆ check_locale_time()

bool check_locale_time ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 336 of file pg_locale.c.

References check_locale().

337 {
338  return check_locale(LC_TIME, *newval, NULL);
339 }
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:269
#define newval

◆ check_strxfrm_bug()

void check_strxfrm_bug ( void  )

Definition at line 1056 of file pg_locale.c.

References buf, ereport, errcode(), errhint(), errmsg_internal(), ERROR, and setlocale.

Referenced by CheckMyDatabase(), and main().

1057 {
1058  char buf[32];
1059  const int canary = 0x7F;
1060  bool ok = true;
1061 
1062  /*
1063  * Given a two-byte ASCII string and length limit 7, 8 or 9, Solaris 10
1064  * 05/08 returns 18 and modifies 10 bytes. It respects limits above or
1065  * below that range.
1066  *
1067  * The bug is present in Solaris 8 as well; it is absent in Solaris 10
1068  * 01/13 and Solaris 11.2. Affected locales include is_IS.ISO8859-1,
1069  * en_US.UTF-8, en_US.ISO8859-1, and ru_RU.KOI8-R. Unaffected locales
1070  * include de_DE.UTF-8, de_DE.ISO8859-1, zh_TW.UTF-8, and C.
1071  */
1072  buf[7] = canary;
1073  (void) strxfrm(buf, "ab", 7);
1074  if (buf[7] != canary)
1075  ok = false;
1076 
1077  /*
1078  * illumos bug #1594 was present in the source tree from 2010-10-11 to
1079  * 2012-02-01. Given an ASCII string of any length and length limit 1,
1080  * affected systems ignore the length limit and modify a number of bytes
1081  * one less than the return value. The problem inputs for this bug do not
1082  * overlap those for the Solaris bug, hence a distinct test.
1083  *
1084  * Affected systems include smartos-20110926T021612Z. Affected locales
1085  * include en_US.ISO8859-1 and en_US.UTF-8. Unaffected locales include C.
1086  */
1087  buf[1] = canary;
1088  (void) strxfrm(buf, "a", 1);
1089  if (buf[1] != canary)
1090  ok = false;
1091 
1092  if (!ok)
1093  ereport(ERROR,
1094  (errcode(ERRCODE_SYSTEM_ERROR),
1095  errmsg_internal("strxfrm(), in locale \"%s\", writes past the specified array length",
1096  setlocale(LC_COLLATE, NULL)),
1097  errhint("Apply system library package updates.")));
1098 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define setlocale(a, b)
Definition: win32_port.h:417
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg_internal(const char *fmt,...)
Definition: elog.c:814

◆ db_encoding_convert()

static void db_encoding_convert ( int  encoding,
char **  str 
)
static

Definition at line 458 of file pg_locale.c.

References ereport, errcode(), errmsg(), ERROR, free, pfree(), and pg_any_to_server().

Referenced by PGLC_localeconv().

459 {
460  char *pstr;
461  char *mstr;
462 
463  /* convert the string to the database encoding */
464  pstr = pg_any_to_server(*str, strlen(*str), encoding);
465  if (pstr == *str)
466  return; /* no conversion happened */
467 
468  /* need it malloc'd not palloc'd */
469  mstr = strdup(pstr);
470  if (mstr == NULL)
471  ereport(ERROR,
472  (errcode(ERRCODE_OUT_OF_MEMORY),
473  errmsg("out of memory")));
474 
475  /* replace old string */
476  free(*str);
477  *str = mstr;
478 
479  pfree(pstr);
480 }
int errcode(int sqlerrcode)
Definition: elog.c:570
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define free(a)
Definition: header.h:65
int32 encoding
Definition: pg_database.h:41
int errmsg(const char *fmt,...)
Definition: elog.c:784
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:553

◆ free_struct_lconv()

static void free_struct_lconv ( struct lconv *  s)
static

Definition at line 398 of file pg_locale.c.

References free.

Referenced by PGLC_localeconv().

399 {
400  if (s->decimal_point)
401  free(s->decimal_point);
402  if (s->thousands_sep)
403  free(s->thousands_sep);
404  if (s->grouping)
405  free(s->grouping);
406  if (s->int_curr_symbol)
407  free(s->int_curr_symbol);
408  if (s->currency_symbol)
409  free(s->currency_symbol);
410  if (s->mon_decimal_point)
411  free(s->mon_decimal_point);
412  if (s->mon_thousands_sep)
413  free(s->mon_thousands_sep);
414  if (s->mon_grouping)
415  free(s->mon_grouping);
416  if (s->positive_sign)
417  free(s->positive_sign);
418  if (s->negative_sign)
419  free(s->negative_sign);
420 }
#define free(a)
Definition: header.h:65

◆ get_collation_actual_version()

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

Definition at line 1519 of file pg_locale.c.

References asc_tolower(), buf, ereport, errmsg(), ERROR, get_encoding_name_for_icu(), GetDatabaseEncoding(), name, palloc(), pg_attribute_unused(), pstrdup(), status(), generate_unaccent_rules::str, and value.

Referenced by AlterCollation(), DefineCollation(), pg_collation_actual_version(), pg_import_system_collations(), and pg_newlocale_from_collation().

1520 {
1521  char *collversion;
1522 
1523 #ifdef USE_ICU
1524  if (collprovider == COLLPROVIDER_ICU)
1525  {
1526  UCollator *collator;
1527  UErrorCode status;
1528  UVersionInfo versioninfo;
1529  char buf[U_MAX_VERSION_STRING_LENGTH];
1530 
1531  status = U_ZERO_ERROR;
1532  collator = ucol_open(collcollate, &status);
1533  if (U_FAILURE(status))
1534  ereport(ERROR,
1535  (errmsg("could not open collator for locale \"%s\": %s",
1536  collcollate, u_errorName(status))));
1537  ucol_getVersion(collator, versioninfo);
1538  ucol_close(collator);
1539 
1540  u_versionToString(versioninfo, buf);
1541  collversion = pstrdup(buf);
1542  }
1543  else
1544 #endif
1545  collversion = NULL;
1546 
1547  return collversion;
1548 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ lc_collate_is_c()

bool lc_collate_is_c ( Oid  collation)

Definition at line 1191 of file pg_locale.c.

References elog, ERROR, lookup_collation_cache(), OidIsValid, and setlocale.

Referenced by bpchareq(), bpcharne(), convert_string_datum(), DefineCollation(), hashbpchar(), hashbpcharextended(), hashtext(), hashtextextended(), make_greater_string(), match_pattern_prefix(), spg_text_inner_consistent(), text_position_setup(), text_starts_with(), texteq(), textne(), varstr_cmp(), and varstr_sortsupport().

1192 {
1193  /*
1194  * If we're asked about "collation 0", return false, so that the code will
1195  * go into the non-C path and report that the collation is bogus.
1196  */
1197  if (!OidIsValid(collation))
1198  return false;
1199 
1200  /*
1201  * If we're asked about the default collation, we have to inquire of the C
1202  * library. Cache the result so we only have to compute it once.
1203  */
1204  if (collation == DEFAULT_COLLATION_OID)
1205  {
1206  static int result = -1;
1207  char *localeptr;
1208 
1209  if (result >= 0)
1210  return (bool) result;
1211  localeptr = setlocale(LC_COLLATE, NULL);
1212  if (!localeptr)
1213  elog(ERROR, "invalid LC_COLLATE setting");
1214 
1215  if (strcmp(localeptr, "C") == 0)
1216  result = true;
1217  else if (strcmp(localeptr, "POSIX") == 0)
1218  result = true;
1219  else
1220  result = false;
1221  return (bool) result;
1222  }
1223 
1224  /*
1225  * If we're asked about the built-in C/POSIX collations, we know that.
1226  */
1227  if (collation == C_COLLATION_OID ||
1228  collation == POSIX_COLLATION_OID)
1229  return true;
1230 
1231  /*
1232  * Otherwise, we have to consult pg_collation, but we cache that.
1233  */
1234  return (lookup_collation_cache(collation, true))->collate_is_c;
1235 }
#define setlocale(a, b)
Definition: win32_port.h:417
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:226
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1126

◆ lc_ctype_is_c()

bool lc_ctype_is_c ( Oid  collation)

Definition at line 1241 of file pg_locale.c.

References elog, ereport, errcode(), errdetail(), errmsg(), ERROR, lookup_collation_cache(), OidIsValid, and setlocale.

Referenced by DefineCollation(), Generic_Text_IC_like(), GenericMatchText(), like_fixed_prefix(), lowerstr_with_len(), pg_set_regex_collation(), str_initcap(), str_tolower(), str_toupper(), t_isalpha(), t_isdigit(), t_isprint(), t_isspace(), and TParserInit().

1242 {
1243  /*
1244  * If we're asked about "collation 0", return false, so that the code will
1245  * go into the non-C path and report that the collation is bogus.
1246  */
1247  if (!OidIsValid(collation))
1248  return false;
1249 
1250  /*
1251  * If we're asked about the default collation, we have to inquire of the C
1252  * library. Cache the result so we only have to compute it once.
1253  */
1254  if (collation == DEFAULT_COLLATION_OID)
1255  {
1256  static int result = -1;
1257  char *localeptr;
1258 
1259  if (result >= 0)
1260  return (bool) result;
1261  localeptr = setlocale(LC_CTYPE, NULL);
1262  if (!localeptr)
1263  elog(ERROR, "invalid LC_CTYPE setting");
1264 
1265  if (strcmp(localeptr, "C") == 0)
1266  result = true;
1267  else if (strcmp(localeptr, "POSIX") == 0)
1268  result = true;
1269  else
1270  result = false;
1271  return (bool) result;
1272  }
1273 
1274  /*
1275  * If we're asked about the built-in C/POSIX collations, we know that.
1276  */
1277  if (collation == C_COLLATION_OID ||
1278  collation == POSIX_COLLATION_OID)
1279  return true;
1280 
1281  /*
1282  * Otherwise, we have to consult pg_collation, but we cache that.
1283  */
1284  return (lookup_collation_cache(collation, true))->ctype_is_c;
1285 }
#define setlocale(a, b)
Definition: win32_port.h:417
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:226
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1126

◆ lookup_collation_cache()

static collation_cache_entry* lookup_collation_cache ( Oid  collation,
bool  set_flags 
)
static

Definition at line 1126 of file pg_locale.c.

References Assert, collation_cache_entry::collate_is_c, COLLOID, collation_cache_entry::ctype_is_c, elog, HASHCTL::entrysize, ERROR, collation_cache_entry::flags_valid, GETSTRUCT, HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HeapTupleIsValid, HASHCTL::keysize, collation_cache_entry::locale, NameStr, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), and SearchSysCache1().

Referenced by lc_collate_is_c(), lc_ctype_is_c(), and pg_newlocale_from_collation().

1127 {
1128  collation_cache_entry *cache_entry;
1129  bool found;
1130 
1131  Assert(OidIsValid(collation));
1132  Assert(collation != DEFAULT_COLLATION_OID);
1133 
1134  if (collation_cache == NULL)
1135  {
1136  /* First time through, initialize the hash table */
1137  HASHCTL ctl;
1138 
1139  memset(&ctl, 0, sizeof(ctl));
1140  ctl.keysize = sizeof(Oid);
1141  ctl.entrysize = sizeof(collation_cache_entry);
1142  collation_cache = hash_create("Collation cache", 100, &ctl,
1143  HASH_ELEM | HASH_BLOBS);
1144  }
1145 
1146  cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
1147  if (!found)
1148  {
1149  /*
1150  * Make sure cache entry is marked invalid, in case we fail before
1151  * setting things.
1152  */
1153  cache_entry->flags_valid = false;
1154  cache_entry->locale = 0;
1155  }
1156 
1157  if (set_flags && !cache_entry->flags_valid)
1158  {
1159  /* Attempt to set the flags */
1160  HeapTuple tp;
1161  Form_pg_collation collform;
1162  const char *collcollate;
1163  const char *collctype;
1164 
1165  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
1166  if (!HeapTupleIsValid(tp))
1167  elog(ERROR, "cache lookup failed for collation %u", collation);
1168  collform = (Form_pg_collation) GETSTRUCT(tp);
1169 
1170  collcollate = NameStr(collform->collcollate);
1171  collctype = NameStr(collform->collctype);
1172 
1173  cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) ||
1174  (strcmp(collcollate, "POSIX") == 0));
1175  cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) ||
1176  (strcmp(collctype, "POSIX") == 0));
1177 
1178  cache_entry->flags_valid = true;
1179 
1180  ReleaseSysCache(tp);
1181  }
1182 
1183  return cache_entry;
1184 }
bool flags_valid
Definition: pg_locale.c:126
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define HASH_ELEM
Definition: hsearch.h:87
bool ctype_is_c
Definition: pg_locale.c:125
Size entrysize
Definition: hsearch.h:73
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
bool collate_is_c
Definition: pg_locale.c:124
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
pg_locale_t locale
Definition: pg_locale.c:127
#define HASH_BLOBS
Definition: hsearch.h:88
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Size keysize
Definition: hsearch.h:72
static HTAB * collation_cache
Definition: pg_locale.c:130
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
Definition: pg_locale.c:121

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1338 of file pg_locale.c.

References Assert, COLLOID, pg_locale_struct::deterministic, elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, get_collation_actual_version(), get_namespace_name(), GETSTRUCT, HeapTupleIsValid, pg_locale_struct::info, collation_cache_entry::locale, locale_t, lookup_collation_cache(), MemoryContextAlloc(), MemoryContextStrdup(), NameStr, ObjectIdGetDatum, OidIsValid, pg_attribute_unused(), pg_locale_struct::provider, quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), status(), SysCacheGetAttr(), TextDatumGetCString, TopMemoryContext, and WARNING.

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

1339 {
1340  collation_cache_entry *cache_entry;
1341 
1342  /* Callers must pass a valid OID */
1343  Assert(OidIsValid(collid));
1344 
1345  /* Return 0 for "default" collation, just in case caller forgets */
1346  if (collid == DEFAULT_COLLATION_OID)
1347  return (pg_locale_t) 0;
1348 
1349  cache_entry = lookup_collation_cache(collid, false);
1350 
1351  if (cache_entry->locale == 0)
1352  {
1353  /* We haven't computed this yet in this session, so do it */
1354  HeapTuple tp;
1355  Form_pg_collation collform;
1356  const char *collcollate;
1357  const char *collctype pg_attribute_unused();
1358  struct pg_locale_struct result;
1359  pg_locale_t resultp;
1360  Datum collversion;
1361  bool isnull;
1362 
1363  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1364  if (!HeapTupleIsValid(tp))
1365  elog(ERROR, "cache lookup failed for collation %u", collid);
1366  collform = (Form_pg_collation) GETSTRUCT(tp);
1367 
1368  collcollate = NameStr(collform->collcollate);
1369  collctype = NameStr(collform->collctype);
1370 
1371  /* We'll fill in the result struct locally before allocating memory */
1372  memset(&result, 0, sizeof(result));
1373  result.provider = collform->collprovider;
1374  result.deterministic = collform->collisdeterministic;
1375 
1376  if (collform->collprovider == COLLPROVIDER_LIBC)
1377  {
1378 #ifdef HAVE_LOCALE_T
1379  locale_t loc;
1380 
1381  if (strcmp(collcollate, collctype) == 0)
1382  {
1383  /* Normal case where they're the same */
1384  errno = 0;
1385 #ifndef WIN32
1386  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
1387  NULL);
1388 #else
1389  loc = _create_locale(LC_ALL, collcollate);
1390 #endif
1391  if (!loc)
1392  report_newlocale_failure(collcollate);
1393  }
1394  else
1395  {
1396 #ifndef WIN32
1397  /* We need two newlocale() steps */
1398  locale_t loc1;
1399 
1400  errno = 0;
1401  loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
1402  if (!loc1)
1403  report_newlocale_failure(collcollate);
1404  errno = 0;
1405  loc = newlocale(LC_CTYPE_MASK, collctype, loc1);
1406  if (!loc)
1407  report_newlocale_failure(collctype);
1408 #else
1409 
1410  /*
1411  * XXX The _create_locale() API doesn't appear to support
1412  * this. Could perhaps be worked around by changing
1413  * pg_locale_t to contain two separate fields.
1414  */
1415  ereport(ERROR,
1416  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1417  errmsg("collations with different collate and ctype values are not supported on this platform")));
1418 #endif
1419  }
1420 
1421  result.info.lt = loc;
1422 #else /* not HAVE_LOCALE_T */
1423  /* platform that doesn't support locale_t */
1424  ereport(ERROR,
1425  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1426  errmsg("collation provider LIBC is not supported on this platform")));
1427 #endif /* not HAVE_LOCALE_T */
1428  }
1429  else if (collform->collprovider == COLLPROVIDER_ICU)
1430  {
1431 #ifdef USE_ICU
1432  UCollator *collator;
1433  UErrorCode status;
1434 
1435  if (strcmp(collcollate, collctype) != 0)
1436  ereport(ERROR,
1437  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1438  errmsg("collations with different collate and ctype values are not supported by ICU")));
1439 
1440  status = U_ZERO_ERROR;
1441  collator = ucol_open(collcollate, &status);
1442  if (U_FAILURE(status))
1443  ereport(ERROR,
1444  (errmsg("could not open collator for locale \"%s\": %s",
1445  collcollate, u_errorName(status))));
1446 
1447  if (U_ICU_VERSION_MAJOR_NUM < 54)
1448  icu_set_collation_attributes(collator, collcollate);
1449 
1450  /* We will leak this string if we get an error below :-( */
1451  result.info.icu.locale = MemoryContextStrdup(TopMemoryContext,
1452  collcollate);
1453  result.info.icu.ucol = collator;
1454 #else /* not USE_ICU */
1455  /* could get here if a collation was created by a build with ICU */
1456  ereport(ERROR,
1457  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1458  errmsg("ICU is not supported in this build"), \
1459  errhint("You need to rebuild PostgreSQL using --with-icu.")));
1460 #endif /* not USE_ICU */
1461  }
1462 
1463  collversion = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1464  &isnull);
1465  if (!isnull)
1466  {
1467  char *actual_versionstr;
1468  char *collversionstr;
1469 
1470  actual_versionstr = get_collation_actual_version(collform->collprovider, collcollate);
1471  if (!actual_versionstr)
1472  {
1473  /*
1474  * This could happen when specifying a version in CREATE
1475  * COLLATION for a libc locale, or manually creating a mess in
1476  * the catalogs.
1477  */
1478  ereport(ERROR,
1479  (errmsg("collation \"%s\" has no actual version, but a version was specified",
1480  NameStr(collform->collname))));
1481  }
1482  collversionstr = TextDatumGetCString(collversion);
1483 
1484  if (strcmp(actual_versionstr, collversionstr) != 0)
1485  ereport(WARNING,
1486  (errmsg("collation \"%s\" has version mismatch",
1487  NameStr(collform->collname)),
1488  errdetail("The collation in the database was created using version %s, "
1489  "but the operating system provides version %s.",
1490  collversionstr, actual_versionstr),
1491  errhint("Rebuild all objects affected by this collation and run "
1492  "ALTER COLLATION %s REFRESH VERSION, "
1493  "or build PostgreSQL with the right library version.",
1494  quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1495  NameStr(collform->collname)))));
1496  }
1497 
1498  ReleaseSysCache(tp);
1499 
1500  /* We'll keep the pg_locale_t structures in TopMemoryContext */
1501  resultp = MemoryContextAlloc(TopMemoryContext, sizeof(*resultp));
1502  *resultp = result;
1503 
1504  cache_entry->locale = resultp;
1505  }
1506 
1507  return cache_entry->locale;
1508 }
int errhint(const char *fmt,...)
Definition: elog.c:974
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:570
#define OidIsValid(objectId)
Definition: c.h:638
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define locale_t
Definition: win32_port.h:372
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10712
#define WARNING
Definition: elog.h:40
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
pg_locale_t locale
Definition: pg_locale.c:127
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
pg_attribute_unused()
Definition: isn.c:64
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:784
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1148
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1126
Definition: pg_locale.c:121
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1519

◆ pg_perm_setlocale()

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

Definition at line 156 of file pg_locale.c.

References elog, FATAL, GetDatabaseEncoding(), lc_collate_envbuf, lc_ctype_envbuf, LC_ENV_BUFSIZE, lc_monetary_envbuf, lc_numeric_envbuf, lc_time_envbuf, putenv, setlocale, SetMessageEncoding(), snprintf, and strlcpy().

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

157 {
158  char *result;
159  const char *envvar;
160  char *envbuf;
161 
162 #ifndef WIN32
163  result = setlocale(category, locale);
164 #else
165 
166  /*
167  * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
168  * the given value is good and set it in the environment variables. We
169  * must ignore attempts to set to "", which means "keep using the old
170  * environment value".
171  */
172 #ifdef LC_MESSAGES
173  if (category == LC_MESSAGES)
174  {
175  result = (char *) locale;
176  if (locale == NULL || locale[0] == '\0')
177  return result;
178  }
179  else
180 #endif
181  result = setlocale(category, locale);
182 #endif /* WIN32 */
183 
184  if (result == NULL)
185  return result; /* fall out immediately on failure */
186 
187  /*
188  * Use the right encoding in translated messages. Under ENABLE_NLS, let
189  * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
190  * format strings are ASCII, but database-encoding strings may enter the
191  * message via %s. This makes the overall message encoding equal to the
192  * database encoding.
193  */
194  if (category == LC_CTYPE)
195  {
196  static char save_lc_ctype[LC_ENV_BUFSIZE];
197 
198  /* copy setlocale() return value before callee invokes it again */
199  strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
200  result = save_lc_ctype;
201 
202 #ifdef ENABLE_NLS
203  SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
204 #else
206 #endif
207  }
208 
209  switch (category)
210  {
211  case LC_COLLATE:
212  envvar = "LC_COLLATE";
213  envbuf = lc_collate_envbuf;
214  break;
215  case LC_CTYPE:
216  envvar = "LC_CTYPE";
217  envbuf = lc_ctype_envbuf;
218  break;
219 #ifdef LC_MESSAGES
220  case LC_MESSAGES:
221  envvar = "LC_MESSAGES";
222  envbuf = lc_messages_envbuf;
223 #ifdef WIN32
224  result = IsoLocaleName(locale);
225  if (result == NULL)
226  result = (char *) locale;
227 #endif /* WIN32 */
228  break;
229 #endif /* LC_MESSAGES */
230  case LC_MONETARY:
231  envvar = "LC_MONETARY";
232  envbuf = lc_monetary_envbuf;
233  break;
234  case LC_NUMERIC:
235  envvar = "LC_NUMERIC";
236  envbuf = lc_numeric_envbuf;
237  break;
238  case LC_TIME:
239  envvar = "LC_TIME";
240  envbuf = lc_time_envbuf;
241  break;
242  default:
243  elog(FATAL, "unrecognized LC category: %d", category);
244  envvar = NULL; /* keep compiler quiet */
245  envbuf = NULL;
246  return NULL;
247  }
248 
249  snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
250 
251  if (putenv(envbuf))
252  return NULL;
253 
254  return result;
255 }
static char lc_numeric_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:116
void SetMessageEncoding(int encoding)
Definition: mbutils.c:900
static char lc_monetary_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:115
#define setlocale(a, b)
Definition: win32_port.h:417
#define LC_ENV_BUFSIZE
Definition: pg_locale.c:107
static char lc_time_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:117
#define FATAL
Definition: elog.h:52
static char lc_ctype_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:110
int GetDatabaseEncoding(void)
Definition: mbutils.c:996
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define putenv(x)
Definition: win32_port.h:483
#define elog(elevel,...)
Definition: elog.h:226
static char * locale
Definition: initdb.c:125
static char lc_collate_envbuf[LC_ENV_BUFSIZE]
Definition: pg_locale.c:109
#define snprintf
Definition: port.h:192

◆ PGLC_localeconv()

struct lconv* PGLC_localeconv ( void  )

Definition at line 488 of file pg_locale.c.

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

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

489 {
490  static struct lconv CurrentLocaleConv;
491  static bool CurrentLocaleConvAllocated = false;
492  struct lconv *extlconv;
493  struct lconv worklconv;
494  char *save_lc_monetary;
495  char *save_lc_numeric;
496 #ifdef WIN32
497  char *save_lc_ctype;
498 #endif
499 
500  /* Did we do it already? */
502  return &CurrentLocaleConv;
503 
504  /* Free any already-allocated storage */
505  if (CurrentLocaleConvAllocated)
506  {
507  free_struct_lconv(&CurrentLocaleConv);
508  CurrentLocaleConvAllocated = false;
509  }
510 
511  /*
512  * This is tricky because we really don't want to risk throwing error
513  * while the locale is set to other than our usual settings. Therefore,
514  * the process is: collect the usual settings, set locale to special
515  * setting, copy relevant data into worklconv using strdup(), restore
516  * normal settings, convert data to desired encoding, and finally stash
517  * the collected data in CurrentLocaleConv. This makes it safe if we
518  * throw an error during encoding conversion or run out of memory anywhere
519  * in the process. All data pointed to by struct lconv members is
520  * allocated with strdup, to avoid premature elog(ERROR) and to allow
521  * using a single cleanup routine.
522  */
523  memset(&worklconv, 0, sizeof(worklconv));
524 
525  /* Save prevailing values of monetary and numeric locales */
526  save_lc_monetary = setlocale(LC_MONETARY, NULL);
527  if (!save_lc_monetary)
528  elog(ERROR, "setlocale(NULL) failed");
529  save_lc_monetary = pstrdup(save_lc_monetary);
530 
531  save_lc_numeric = setlocale(LC_NUMERIC, NULL);
532  if (!save_lc_numeric)
533  elog(ERROR, "setlocale(NULL) failed");
534  save_lc_numeric = pstrdup(save_lc_numeric);
535 
536 #ifdef WIN32
537 
538  /*
539  * The POSIX standard explicitly says that it is undefined what happens if
540  * LC_MONETARY or LC_NUMERIC imply an encoding (codeset) different from
541  * that implied by LC_CTYPE. In practice, all Unix-ish platforms seem to
542  * believe that localeconv() should return strings that are encoded in the
543  * codeset implied by the LC_MONETARY or LC_NUMERIC locale name. Hence,
544  * once we have successfully collected the localeconv() results, we will
545  * convert them from that codeset to the desired server encoding.
546  *
547  * Windows, of course, resolutely does things its own way; on that
548  * platform LC_CTYPE has to match LC_MONETARY/LC_NUMERIC to get sane
549  * results. Hence, we must temporarily set that category as well.
550  */
551 
552  /* Save prevailing value of ctype locale */
553  save_lc_ctype = setlocale(LC_CTYPE, NULL);
554  if (!save_lc_ctype)
555  elog(ERROR, "setlocale(NULL) failed");
556  save_lc_ctype = pstrdup(save_lc_ctype);
557 
558  /* Here begins the critical section where we must not throw error */
559 
560  /* use numeric to set the ctype */
561  setlocale(LC_CTYPE, locale_numeric);
562 #endif
563 
564  /* Get formatting information for numeric */
565  setlocale(LC_NUMERIC, locale_numeric);
566  extlconv = localeconv();
567 
568  /* Must copy data now in case setlocale() overwrites it */
569  worklconv.decimal_point = strdup(extlconv->decimal_point);
570  worklconv.thousands_sep = strdup(extlconv->thousands_sep);
571  worklconv.grouping = strdup(extlconv->grouping);
572 
573 #ifdef WIN32
574  /* use monetary to set the ctype */
575  setlocale(LC_CTYPE, locale_monetary);
576 #endif
577 
578  /* Get formatting information for monetary */
579  setlocale(LC_MONETARY, locale_monetary);
580  extlconv = localeconv();
581 
582  /* Must copy data now in case setlocale() overwrites it */
583  worklconv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
584  worklconv.currency_symbol = strdup(extlconv->currency_symbol);
585  worklconv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
586  worklconv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
587  worklconv.mon_grouping = strdup(extlconv->mon_grouping);
588  worklconv.positive_sign = strdup(extlconv->positive_sign);
589  worklconv.negative_sign = strdup(extlconv->negative_sign);
590  /* Copy scalar fields as well */
591  worklconv.int_frac_digits = extlconv->int_frac_digits;
592  worklconv.frac_digits = extlconv->frac_digits;
593  worklconv.p_cs_precedes = extlconv->p_cs_precedes;
594  worklconv.p_sep_by_space = extlconv->p_sep_by_space;
595  worklconv.n_cs_precedes = extlconv->n_cs_precedes;
596  worklconv.n_sep_by_space = extlconv->n_sep_by_space;
597  worklconv.p_sign_posn = extlconv->p_sign_posn;
598  worklconv.n_sign_posn = extlconv->n_sign_posn;
599 
600  /*
601  * Restore the prevailing locale settings; failure to do so is fatal.
602  * Possibly we could limp along with nondefault LC_MONETARY or LC_NUMERIC,
603  * but proceeding with the wrong value of LC_CTYPE would certainly be bad
604  * news; and considering that the prevailing LC_MONETARY and LC_NUMERIC
605  * are almost certainly "C", there's really no reason that restoring those
606  * should fail.
607  */
608 #ifdef WIN32
609  if (!setlocale(LC_CTYPE, save_lc_ctype))
610  elog(FATAL, "failed to restore LC_CTYPE to \"%s\"", save_lc_ctype);
611 #endif
612  if (!setlocale(LC_MONETARY, save_lc_monetary))
613  elog(FATAL, "failed to restore LC_MONETARY to \"%s\"", save_lc_monetary);
614  if (!setlocale(LC_NUMERIC, save_lc_numeric))
615  elog(FATAL, "failed to restore LC_NUMERIC to \"%s\"", save_lc_numeric);
616 
617  /*
618  * At this point we've done our best to clean up, and can call functions
619  * that might possibly throw errors with a clean conscience. But let's
620  * make sure we don't leak any already-strdup'd fields in worklconv.
621  */
622  PG_TRY();
623  {
624  int encoding;
625 
626  /* Release the pstrdup'd locale names */
627  pfree(save_lc_monetary);
628  pfree(save_lc_numeric);
629 #ifdef WIN32
630  pfree(save_lc_ctype);
631 #endif
632 
633  /* If any of the preceding strdup calls failed, complain now. */
634  if (!struct_lconv_is_valid(&worklconv))
635  ereport(ERROR,
636  (errcode(ERRCODE_OUT_OF_MEMORY),
637  errmsg("out of memory")));
638 
639  /*
640  * Now we must perform encoding conversion from whatever's associated
641  * with the locales into the database encoding. If we can't identify
642  * the encoding implied by LC_NUMERIC or LC_MONETARY (ie we get -1),
643  * use PG_SQL_ASCII, which will result in just validating that the
644  * strings are OK in the database encoding.
645  */
647  if (encoding < 0)
648  encoding = PG_SQL_ASCII;
649 
650  db_encoding_convert(encoding, &worklconv.decimal_point);
651  db_encoding_convert(encoding, &worklconv.thousands_sep);
652  /* grouping is not text and does not require conversion */
653 
655  if (encoding < 0)
656  encoding = PG_SQL_ASCII;
657 
658  db_encoding_convert(encoding, &worklconv.int_curr_symbol);
659  db_encoding_convert(encoding, &worklconv.currency_symbol);
660  db_encoding_convert(encoding, &worklconv.mon_decimal_point);
661  db_encoding_convert(encoding, &worklconv.mon_thousands_sep);
662  /* mon_grouping is not text and does not require conversion */
663  db_encoding_convert(encoding, &worklconv.positive_sign);
664  db_encoding_convert(encoding, &worklconv.negative_sign);
665  }
666  PG_CATCH();
667  {
668  free_struct_lconv(&worklconv);
669  PG_RE_THROW();
670  }
671  PG_END_TRY();
672 
673  /*
674  * Everything is good, so save the results.
675  */
676  CurrentLocaleConv = worklconv;
677  CurrentLocaleConvAllocated = true;
678  CurrentLocaleConvValid = true;
679  return &CurrentLocaleConv;
680 }
static bool CurrentLocaleConvValid
Definition: pg_locale.c:102
#define setlocale(a, b)
Definition: win32_port.h:417
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:570
char * locale_numeric
Definition: pg_locale.c:92
static void free_struct_lconv(struct lconv *s)
Definition: pg_locale.c:398
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
#define PG_CATCH()
Definition: elog.h:310
static bool struct_lconv_is_valid(struct lconv *s)
Definition: pg_locale.c:427
#define PG_RE_THROW()
Definition: elog.h:331
int32 encoding
Definition: pg_database.h:41
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define PG_TRY()
Definition: elog.h:301
char * locale_monetary
Definition: pg_locale.c:91
static void db_encoding_convert(int encoding, char **str)
Definition: pg_locale.c:458
#define PG_END_TRY()
Definition: elog.h:317

◆ struct_lconv_is_valid()

static bool struct_lconv_is_valid ( struct lconv *  s)
static

Definition at line 427 of file pg_locale.c.

Referenced by PGLC_localeconv().

428 {
429  if (s->decimal_point == NULL)
430  return false;
431  if (s->thousands_sep == NULL)
432  return false;
433  if (s->grouping == NULL)
434  return false;
435  if (s->int_curr_symbol == NULL)
436  return false;
437  if (s->currency_symbol == NULL)
438  return false;
439  if (s->mon_decimal_point == NULL)
440  return false;
441  if (s->mon_thousands_sep == NULL)
442  return false;
443  if (s->mon_grouping == NULL)
444  return false;
445  if (s->positive_sign == NULL)
446  return false;
447  if (s->negative_sign == NULL)
448  return false;
449  return true;
450 }

◆ wchar2char()

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

Definition at line 1774 of file pg_locale.c.

References Assert, elog, ERROR, GetDatabaseEncoding(), pg_locale_struct::info, locale_t, PG_UTF8, pg_locale_struct::provider, and wcstombs_l.

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

1775 {
1776  size_t result;
1777 
1778  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1779 
1780  if (tolen == 0)
1781  return 0;
1782 
1783 #ifdef WIN32
1784 
1785  /*
1786  * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
1787  * for some reason mbstowcs and wcstombs won't do this for us, so we use
1788  * MultiByteToWideChar().
1789  */
1790  if (GetDatabaseEncoding() == PG_UTF8)
1791  {
1792  result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
1793  NULL, NULL);
1794  /* A zero return is failure */
1795  if (result <= 0)
1796  result = -1;
1797  else
1798  {
1799  Assert(result <= tolen);
1800  /* Microsoft counts the zero terminator in the result */
1801  result--;
1802  }
1803  }
1804  else
1805 #endif /* WIN32 */
1806  if (locale == (pg_locale_t) 0)
1807  {
1808  /* Use wcstombs directly for the default locale */
1809  result = wcstombs(to, from, tolen);
1810  }
1811  else
1812  {
1813 #ifdef HAVE_LOCALE_T
1814 #ifdef HAVE_WCSTOMBS_L
1815  /* Use wcstombs_l for nondefault locales */
1816  result = wcstombs_l(to, from, tolen, locale->info.lt);
1817 #else /* !HAVE_WCSTOMBS_L */
1818  /* We have to temporarily set the locale as current ... ugh */
1819  locale_t save_locale = uselocale(locale->info.lt);
1820 
1821  result = wcstombs(to, from, tolen);
1822 
1823  uselocale(save_locale);
1824 #endif /* HAVE_WCSTOMBS_L */
1825 #else /* !HAVE_LOCALE_T */
1826  /* Can't have locale != 0 without HAVE_LOCALE_T */
1827  elog(ERROR, "wcstombs_l is not available");
1828  result = 0; /* keep compiler quiet */
1829 #endif /* HAVE_LOCALE_T */
1830  }
1831 
1832  return result;
1833 }
#define wcstombs_l
Definition: win32_port.h:398
#define ERROR
Definition: elog.h:43
#define locale_t
Definition: win32_port.h:372
int GetDatabaseEncoding(void)
Definition: mbutils.c:996
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226
union pg_locale_struct::@143 info

Variable Documentation

◆ collation_cache

HTAB* collation_cache = NULL
static

Definition at line 130 of file pg_locale.c.

◆ CurrentLCTimeValid

bool CurrentLCTimeValid = false
static

Definition at line 103 of file pg_locale.c.

Referenced by assign_locale_time(), and cache_locale_time().

◆ CurrentLocaleConvValid

bool CurrentLocaleConvValid = false
static

Definition at line 102 of file pg_locale.c.

Referenced by assign_locale_monetary(), assign_locale_numeric(), and PGLC_localeconv().

◆ lc_collate_envbuf

char lc_collate_envbuf[LC_ENV_BUFSIZE]
static

Definition at line 109 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ lc_ctype_envbuf

char lc_ctype_envbuf[LC_ENV_BUFSIZE]
static

Definition at line 110 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ lc_monetary_envbuf

char lc_monetary_envbuf[LC_ENV_BUFSIZE]
static

Definition at line 115 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ lc_numeric_envbuf

char lc_numeric_envbuf[LC_ENV_BUFSIZE]
static

Definition at line 116 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ lc_time_envbuf

char lc_time_envbuf[LC_ENV_BUFSIZE]
static

Definition at line 117 of file pg_locale.c.

Referenced by pg_perm_setlocale().

◆ locale_messages

char* locale_messages

Definition at line 90 of file pg_locale.c.

◆ locale_monetary

char* locale_monetary

Definition at line 91 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

char* locale_numeric

Definition at line 92 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

char* locale_time

Definition at line 93 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

char* localized_abbrev_days[7]

Definition at line 96 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

◆ localized_abbrev_months

char* localized_abbrev_months[12]

Definition at line 98 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

◆ localized_full_days

char* localized_full_days[7]

Definition at line 97 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

◆ localized_full_months

char* localized_full_months[12]

Definition at line 99 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().