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/guc_hooks.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
 

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)
 
void make_icu_collator (const char *iculocstr, struct pg_locale_struct *resultp)
 
pg_locale_t pg_newlocale_from_collation (Oid collid)
 
char * get_collation_actual_version (char collprovider, const char *collcollate)
 
void check_icu_locale (const char *icu_locale)
 
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+1]
 
char * localized_full_days [7+1]
 
char * localized_abbrev_months [12+1]
 
char * localized_full_months [12+1]
 
static bool CurrentLocaleConvValid = false
 
static bool CurrentLCTimeValid = false
 
static HTABcollation_cache = NULL
 
struct pg_locale_struct default_locale
 

Macro Definition Documentation

◆ MAX_L10N_DATA

#define MAX_L10N_DATA   80

Definition at line 82 of file pg_locale.c.

Function Documentation

◆ assign_locale_messages()

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

Definition at line 359 of file pg_locale.c.

360 {
361  /*
362  * LC_MESSAGES category does not exist everywhere, but accept it anyway.
363  * We ignore failure, as per comment above.
364  */
365 #ifdef LC_MESSAGES
366  (void) pg_perm_setlocale(LC_MESSAGES, newval);
367 #endif
368 }
#define newval
char * pg_perm_setlocale(int category, const char *locale)
Definition: pg_locale.c:144

References newval, and pg_perm_setlocale().

◆ assign_locale_monetary()

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

Definition at line 296 of file pg_locale.c.

297 {
298  CurrentLocaleConvValid = false;
299 }
static bool CurrentLocaleConvValid
Definition: pg_locale.c:104

References CurrentLocaleConvValid.

◆ assign_locale_numeric()

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

Definition at line 308 of file pg_locale.c.

309 {
310  CurrentLocaleConvValid = false;
311 }

References CurrentLocaleConvValid.

◆ assign_locale_time()

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

Definition at line 320 of file pg_locale.c.

321 {
322  CurrentLCTimeValid = false;
323 }
static bool CurrentLCTimeValid
Definition: pg_locale.c:105

References CurrentLCTimeValid.

◆ cache_locale_time()

void cache_locale_time ( void  )

Definition at line 738 of file pg_locale.c.

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

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

◆ cache_single_string()

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

Definition at line 715 of file pg_locale.c.

716 {
717  char *ptr;
718  char *olddst;
719 
720  /* Convert the string to the database encoding, or validate it's OK */
721  ptr = pg_any_to_server(src, strlen(src), encoding);
722 
723  /* Store the string in long-lived storage, replacing any previous value */
724  olddst = *dst;
726  if (olddst)
727  pfree(olddst);
728 
729  /* Might as well clean up any palloc'd conversion result, too */
730  if (ptr != src)
731  pfree(ptr);
732 }
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
MemoryContext TopMemoryContext
Definition: mcxt.c:130
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1470

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

Referenced by cache_locale_time().

◆ char2wchar()

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

Definition at line 2061 of file pg_locale.c.

2063 {
2064  size_t result;
2065 
2066  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
2067 
2068  if (tolen == 0)
2069  return 0;
2070 
2071 #ifdef WIN32
2072  /* See WIN32 "Unicode" comment above */
2073  if (GetDatabaseEncoding() == PG_UTF8)
2074  {
2075  /* Win32 API does not work for zero-length input */
2076  if (fromlen == 0)
2077  result = 0;
2078  else
2079  {
2080  result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
2081  /* A zero return is failure */
2082  if (result == 0)
2083  result = -1;
2084  }
2085 
2086  if (result != -1)
2087  {
2088  Assert(result < tolen);
2089  /* Append trailing null wchar (MultiByteToWideChar() does not) */
2090  to[result] = 0;
2091  }
2092  }
2093  else
2094 #endif /* WIN32 */
2095  {
2096  /* mbstowcs requires ending '\0' */
2097  char *str = pnstrdup(from, fromlen);
2098 
2099  if (locale == (pg_locale_t) 0)
2100  {
2101  /* Use mbstowcs directly for the default locale */
2102  result = mbstowcs(to, str, tolen);
2103  }
2104  else
2105  {
2106 #ifdef HAVE_LOCALE_T
2107 #ifdef HAVE_MBSTOWCS_L
2108  /* Use mbstowcs_l for nondefault locales */
2109  result = mbstowcs_l(to, str, tolen, locale->info.lt);
2110 #else /* !HAVE_MBSTOWCS_L */
2111  /* We have to temporarily set the locale as current ... ugh */
2112  locale_t save_locale = uselocale(locale->info.lt);
2113 
2114  result = mbstowcs(to, str, tolen);
2115 
2116  uselocale(save_locale);
2117 #endif /* HAVE_MBSTOWCS_L */
2118 #else /* !HAVE_LOCALE_T */
2119  /* Can't have locale != 0 without HAVE_LOCALE_T */
2120  elog(ERROR, "mbstowcs_l is not available");
2121  result = 0; /* keep compiler quiet */
2122 #endif /* HAVE_LOCALE_T */
2123  }
2124 
2125  pfree(str);
2126  }
2127 
2128  if (result == -1)
2129  {
2130  /*
2131  * Invalid multibyte character encountered. We try to give a useful
2132  * error message by letting pg_verifymbstr check the string. But it's
2133  * possible that the string is OK to us, and not OK to mbstowcs ---
2134  * this suggests that the LC_CTYPE locale is different from the
2135  * database encoding. Give a generic error message if pg_verifymbstr
2136  * can't find anything wrong.
2137  */
2138  pg_verifymbstr(from, fromlen, false); /* might not return */
2139  /* but if it does ... */
2140  ereport(ERROR,
2141  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
2142  errmsg("invalid multibyte character for locale"),
2143  errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
2144  }
2145 
2146  return result;
2147 }
int errhint(const char *fmt,...)
Definition: elog.c:1153
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ereport(elevel,...)
Definition: elog.h:145
static char * locale
Definition: initdb.c:129
Assert(fmt[strlen(fmt) - 1] !='\n')
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1505
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1494
#define locale_t
Definition: win32_port.h:419
#define mbstowcs_l
Definition: win32_port.h:446

References Assert(), elog(), ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), locale, locale_t, mbstowcs_l, pfree(), PG_UTF8, pg_verifymbstr(), pnstrdup(), and generate_unaccent_rules::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_icu_locale()

void check_icu_locale ( const char *  icu_locale)

Definition at line 1954 of file pg_locale.c.

1955 {
1956 #ifdef USE_ICU
1957  UCollator *collator;
1958  UErrorCode status;
1959 
1960  status = U_ZERO_ERROR;
1961  collator = ucol_open(icu_locale, &status);
1962  if (U_FAILURE(status))
1963  ereport(ERROR,
1964  (errmsg("could not open collator for locale \"%s\": %s",
1965  icu_locale, u_errorName(status))));
1966 
1967  if (U_ICU_VERSION_MAJOR_NUM < 54)
1968  icu_set_collation_attributes(collator, icu_locale);
1969  ucol_close(collator);
1970 #else
1971  ereport(ERROR,
1972  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1973  errmsg("ICU is not supported in this build")));
1974 #endif
1975 }
static char * icu_locale
Definition: initdb.c:137
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

References ereport, errcode(), errmsg(), ERROR, icu_locale, and status().

Referenced by createdb().

◆ check_locale()

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

Definition at line 247 of file pg_locale.c.

248 {
249  char *save;
250  char *res;
251 
252  if (canonname)
253  *canonname = NULL; /* in case of failure */
254 
255  save = setlocale(category, NULL);
256  if (!save)
257  return false; /* won't happen, we hope */
258 
259  /* save may be pointing at a modifiable scratch variable, see above. */
260  save = pstrdup(save);
261 
262  /* set the locale with setlocale, to see if it accepts it. */
263  res = setlocale(category, locale);
264 
265  /* save canonical name if requested. */
266  if (res && canonname)
267  *canonname = pstrdup(res);
268 
269  /* restore old value. */
270  if (!setlocale(category, save))
271  elog(WARNING, "failed to restore old locale \"%s\"", save);
272  pfree(save);
273 
274  return (res != NULL);
275 }
#define WARNING
Definition: elog.h:32

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

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

◆ check_locale_messages()

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

Definition at line 336 of file pg_locale.c.

337 {
338  if (**newval == '\0')
339  {
340  if (source == PGC_S_DEFAULT)
341  return true;
342  else
343  return false;
344  }
345 
346  /*
347  * LC_MESSAGES category does not exist everywhere, but accept it anyway
348  *
349  * On Windows, we can't even check the value, so accept blindly
350  */
351 #if defined(LC_MESSAGES) && !defined(WIN32)
352  return check_locale(LC_MESSAGES, *newval, NULL);
353 #else
354  return true;
355 #endif
356 }
@ PGC_S_DEFAULT
Definition: guc.h:109
bool check_locale(int category, const char *locale, char **canonname)
Definition: pg_locale.c:247
static rewind_source * source
Definition: pg_rewind.c:81

References check_locale(), newval, PGC_S_DEFAULT, and source.

◆ check_locale_monetary()

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

Definition at line 290 of file pg_locale.c.

291 {
292  return check_locale(LC_MONETARY, *newval, NULL);
293 }

References check_locale(), and newval.

◆ check_locale_numeric()

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

Definition at line 302 of file pg_locale.c.

303 {
304  return check_locale(LC_NUMERIC, *newval, NULL);
305 }

References check_locale(), and newval.

◆ check_locale_time()

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

Definition at line 314 of file pg_locale.c.

315 {
316  return check_locale(LC_TIME, *newval, NULL);
317 }

References check_locale(), and newval.

◆ check_strxfrm_bug()

void check_strxfrm_bug ( void  )

Definition at line 1150 of file pg_locale.c.

1151 {
1152  char buf[32];
1153  const int canary = 0x7F;
1154  bool ok = true;
1155 
1156  /*
1157  * Given a two-byte ASCII string and length limit 7, 8 or 9, Solaris 10
1158  * 05/08 returns 18 and modifies 10 bytes. It respects limits above or
1159  * below that range.
1160  *
1161  * The bug is present in Solaris 8 as well; it is absent in Solaris 10
1162  * 01/13 and Solaris 11.2. Affected locales include is_IS.ISO8859-1,
1163  * en_US.UTF-8, en_US.ISO8859-1, and ru_RU.KOI8-R. Unaffected locales
1164  * include de_DE.UTF-8, de_DE.ISO8859-1, zh_TW.UTF-8, and C.
1165  */
1166  buf[7] = canary;
1167  (void) strxfrm(buf, "ab", 7);
1168  if (buf[7] != canary)
1169  ok = false;
1170 
1171  /*
1172  * illumos bug #1594 was present in the source tree from 2010-10-11 to
1173  * 2012-02-01. Given an ASCII string of any length and length limit 1,
1174  * affected systems ignore the length limit and modify a number of bytes
1175  * one less than the return value. The problem inputs for this bug do not
1176  * overlap those for the Solaris bug, hence a distinct test.
1177  *
1178  * Affected systems include smartos-20110926T021612Z. Affected locales
1179  * include en_US.ISO8859-1 and en_US.UTF-8. Unaffected locales include C.
1180  */
1181  buf[1] = canary;
1182  (void) strxfrm(buf, "a", 1);
1183  if (buf[1] != canary)
1184  ok = false;
1185 
1186  if (!ok)
1187  ereport(ERROR,
1188  (errcode(ERRCODE_SYSTEM_ERROR),
1189  errmsg_internal("strxfrm(), in locale \"%s\", writes past the specified array length",
1190  setlocale(LC_COLLATE, NULL)),
1191  errhint("Apply system library package updates.")));
1192 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:993

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

Referenced by CheckMyDatabase(), and main().

◆ db_encoding_convert()

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

Definition at line 426 of file pg_locale.c.

427 {
428  char *pstr;
429  char *mstr;
430 
431  /* convert the string to the database encoding */
432  pstr = pg_any_to_server(*str, strlen(*str), encoding);
433  if (pstr == *str)
434  return; /* no conversion happened */
435 
436  /* need it malloc'd not palloc'd */
437  mstr = strdup(pstr);
438  if (mstr == NULL)
439  ereport(ERROR,
440  (errcode(ERRCODE_OUT_OF_MEMORY),
441  errmsg("out of memory")));
442 
443  /* replace old string */
444  free(*str);
445  *str = mstr;
446 
447  pfree(pstr);
448 }
#define free(a)
Definition: header.h:65

References encoding, ereport, errcode(), errmsg(), ERROR, free, pfree(), pg_any_to_server(), and generate_unaccent_rules::str.

Referenced by PGLC_localeconv().

◆ free_struct_lconv()

static void free_struct_lconv ( struct lconv *  s)
static

Definition at line 376 of file pg_locale.c.

377 {
378  free(s->decimal_point);
379  free(s->thousands_sep);
380  free(s->grouping);
381  free(s->int_curr_symbol);
382  free(s->currency_symbol);
383  free(s->mon_decimal_point);
384  free(s->mon_thousands_sep);
385  free(s->mon_grouping);
386  free(s->positive_sign);
387  free(s->negative_sign);
388 }

References free.

Referenced by PGLC_localeconv().

◆ get_collation_actual_version()

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

Definition at line 1647 of file pg_locale.c.

1648 {
1649  char *collversion = NULL;
1650 
1651 #ifdef USE_ICU
1652  if (collprovider == COLLPROVIDER_ICU)
1653  {
1654  UCollator *collator;
1655  UErrorCode status;
1656  UVersionInfo versioninfo;
1657  char buf[U_MAX_VERSION_STRING_LENGTH];
1658 
1659  status = U_ZERO_ERROR;
1660  collator = ucol_open(collcollate, &status);
1661  if (U_FAILURE(status))
1662  ereport(ERROR,
1663  (errmsg("could not open collator for locale \"%s\": %s",
1664  collcollate, u_errorName(status))));
1665  ucol_getVersion(collator, versioninfo);
1666  ucol_close(collator);
1667 
1668  u_versionToString(versioninfo, buf);
1669  collversion = pstrdup(buf);
1670  }
1671  else
1672 #endif
1673  if (collprovider == COLLPROVIDER_LIBC &&
1674  pg_strcasecmp("C", collcollate) != 0 &&
1675  pg_strncasecmp("C.", collcollate, 2) != 0 &&
1676  pg_strcasecmp("POSIX", collcollate) != 0)
1677  {
1678 #if defined(__GLIBC__)
1679  /* Use the glibc version because we don't have anything better. */
1680  collversion = pstrdup(gnu_get_libc_version());
1681 #elif defined(LC_VERSION_MASK)
1682  locale_t loc;
1683 
1684  /* Look up FreeBSD collation version. */
1685  loc = newlocale(LC_COLLATE, collcollate, NULL);
1686  if (loc)
1687  {
1688  collversion =
1689  pstrdup(querylocale(LC_COLLATE_MASK | LC_VERSION_MASK, loc));
1690  freelocale(loc);
1691  }
1692  else
1693  ereport(ERROR,
1694  (errmsg("could not load locale \"%s\"", collcollate)));
1695 #elif defined(WIN32)
1696  /*
1697  * If we are targeting Windows Vista and above, we can ask for a name
1698  * given a collation name (earlier versions required a location code
1699  * that we don't have).
1700  */
1701  NLSVERSIONINFOEX version = {sizeof(NLSVERSIONINFOEX)};
1702  WCHAR wide_collcollate[LOCALE_NAME_MAX_LENGTH];
1703 
1704  MultiByteToWideChar(CP_ACP, 0, collcollate, -1, wide_collcollate,
1705  LOCALE_NAME_MAX_LENGTH);
1706  if (!GetNLSVersionEx(COMPARE_STRING, wide_collcollate, &version))
1707  {
1708  /*
1709  * GetNLSVersionEx() wants a language tag such as "en-US", not a
1710  * locale name like "English_United States.1252". Until those
1711  * values can be prevented from entering the system, or 100%
1712  * reliably converted to the more useful tag format, tolerate the
1713  * resulting error and report that we have no version data.
1714  */
1715  if (GetLastError() == ERROR_INVALID_PARAMETER)
1716  return NULL;
1717 
1718  ereport(ERROR,
1719  (errmsg("could not get collation version for locale \"%s\": error code %lu",
1720  collcollate,
1721  GetLastError())));
1722  }
1723  collversion = psprintf("%ld.%ld,%ld.%ld",
1724  (version.dwNLSVersion >> 8) & 0xFFFF,
1725  version.dwNLSVersion & 0xFF,
1726  (version.dwDefinedVersion >> 8) & 0xFFFF,
1727  version.dwDefinedVersion & 0xFF);
1728 #endif
1729  }
1730 
1731  return collversion;
1732 }
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:46

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

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

◆ lc_collate_is_c()

bool lc_collate_is_c ( Oid  collation)

Definition at line 1299 of file pg_locale.c.

1300 {
1301  /*
1302  * If we're asked about "collation 0", return false, so that the code will
1303  * go into the non-C path and report that the collation is bogus.
1304  */
1305  if (!OidIsValid(collation))
1306  return false;
1307 
1308  /*
1309  * If we're asked about the default collation, we have to inquire of the C
1310  * library. Cache the result so we only have to compute it once.
1311  */
1312  if (collation == DEFAULT_COLLATION_OID)
1313  {
1314  static int result = -1;
1315  char *localeptr;
1316 
1317  if (default_locale.provider == COLLPROVIDER_ICU)
1318  return false;
1319 
1320  if (result >= 0)
1321  return (bool) result;
1322  localeptr = setlocale(LC_COLLATE, NULL);
1323  if (!localeptr)
1324  elog(ERROR, "invalid LC_COLLATE setting");
1325 
1326  if (strcmp(localeptr, "C") == 0)
1327  result = true;
1328  else if (strcmp(localeptr, "POSIX") == 0)
1329  result = true;
1330  else
1331  result = false;
1332  return (bool) result;
1333  }
1334 
1335  /*
1336  * If we're asked about the built-in C/POSIX collations, we know that.
1337  */
1338  if (collation == C_COLLATION_OID ||
1339  collation == POSIX_COLLATION_OID)
1340  return true;
1341 
1342  /*
1343  * Otherwise, we have to consult pg_collation, but we cache that.
1344  */
1345  return (lookup_collation_cache(collation, true))->collate_is_c;
1346 }
#define OidIsValid(objectId)
Definition: c.h:711
struct pg_locale_struct default_locale
Definition: pg_locale.c:1401
static collation_cache_entry * lookup_collation_cache(Oid collation, bool set_flags)
Definition: pg_locale.c:1220

References default_locale, elog(), ERROR, lookup_collation_cache(), OidIsValid, pg_locale_struct::provider, and setlocale.

Referenced by bpchareq(), bpcharne(), btvarstrequalimage(), 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().

◆ lc_ctype_is_c()

bool lc_ctype_is_c ( Oid  collation)

Definition at line 1352 of file pg_locale.c.

1353 {
1354  /*
1355  * If we're asked about "collation 0", return false, so that the code will
1356  * go into the non-C path and report that the collation is bogus.
1357  */
1358  if (!OidIsValid(collation))
1359  return false;
1360 
1361  /*
1362  * If we're asked about the default collation, we have to inquire of the C
1363  * library. Cache the result so we only have to compute it once.
1364  */
1365  if (collation == DEFAULT_COLLATION_OID)
1366  {
1367  static int result = -1;
1368  char *localeptr;
1369 
1370  if (default_locale.provider == COLLPROVIDER_ICU)
1371  return false;
1372 
1373  if (result >= 0)
1374  return (bool) result;
1375  localeptr = setlocale(LC_CTYPE, NULL);
1376  if (!localeptr)
1377  elog(ERROR, "invalid LC_CTYPE setting");
1378 
1379  if (strcmp(localeptr, "C") == 0)
1380  result = true;
1381  else if (strcmp(localeptr, "POSIX") == 0)
1382  result = true;
1383  else
1384  result = false;
1385  return (bool) result;
1386  }
1387 
1388  /*
1389  * If we're asked about the built-in C/POSIX collations, we know that.
1390  */
1391  if (collation == C_COLLATION_OID ||
1392  collation == POSIX_COLLATION_OID)
1393  return true;
1394 
1395  /*
1396  * Otherwise, we have to consult pg_collation, but we cache that.
1397  */
1398  return (lookup_collation_cache(collation, true))->ctype_is_c;
1399 }

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_isalnum(), t_isalpha(), t_isdigit(), t_isprint(), t_isspace(), and TParserInit().

◆ lookup_collation_cache()

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

Definition at line 1220 of file pg_locale.c.

1221 {
1222  collation_cache_entry *cache_entry;
1223  bool found;
1224 
1225  Assert(OidIsValid(collation));
1226  Assert(collation != DEFAULT_COLLATION_OID);
1227 
1228  if (collation_cache == NULL)
1229  {
1230  /* First time through, initialize the hash table */
1231  HASHCTL ctl;
1232 
1233  ctl.keysize = sizeof(Oid);
1234  ctl.entrysize = sizeof(collation_cache_entry);
1235  collation_cache = hash_create("Collation cache", 100, &ctl,
1236  HASH_ELEM | HASH_BLOBS);
1237  }
1238 
1239  cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
1240  if (!found)
1241  {
1242  /*
1243  * Make sure cache entry is marked invalid, in case we fail before
1244  * setting things.
1245  */
1246  cache_entry->flags_valid = false;
1247  cache_entry->locale = 0;
1248  }
1249 
1250  if (set_flags && !cache_entry->flags_valid)
1251  {
1252  /* Attempt to set the flags */
1253  HeapTuple tp;
1254  Form_pg_collation collform;
1255 
1256  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
1257  if (!HeapTupleIsValid(tp))
1258  elog(ERROR, "cache lookup failed for collation %u", collation);
1259  collform = (Form_pg_collation) GETSTRUCT(tp);
1260 
1261  if (collform->collprovider == COLLPROVIDER_LIBC)
1262  {
1263  Datum datum;
1264  bool isnull;
1265  const char *collcollate;
1266  const char *collctype;
1267 
1268  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collcollate, &isnull);
1269  Assert(!isnull);
1270  collcollate = TextDatumGetCString(datum);
1271  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collctype, &isnull);
1272  Assert(!isnull);
1273  collctype = TextDatumGetCString(datum);
1274 
1275  cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) ||
1276  (strcmp(collcollate, "POSIX") == 0));
1277  cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) ||
1278  (strcmp(collctype, "POSIX") == 0));
1279  }
1280  else
1281  {
1282  cache_entry->collate_is_c = false;
1283  cache_entry->ctype_is_c = false;
1284  }
1285 
1286  cache_entry->flags_valid = true;
1287 
1288  ReleaseSysCache(tp);
1289  }
1290 
1291  return cache_entry;
1292 }
#define TextDatumGetCString(d)
Definition: builtins.h:86
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:953
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:350
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
FormData_pg_collation * Form_pg_collation
Definition: pg_collation.h:57
static HTAB * collation_cache
Definition: pg_locale.c:118
uintptr_t Datum
Definition: postgres.h:412
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
unsigned int Oid
Definition: postgres_ext.h:31
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
Definition: pg_locale.c:110
bool collate_is_c
Definition: pg_locale.c:112
pg_locale_t locale
Definition: pg_locale.c:115
bool flags_valid
Definition: pg_locale.c:114
bool ctype_is_c
Definition: pg_locale.c:113
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ COLLOID
Definition: syscache.h:50

References Assert(), collation_cache_entry::collate_is_c, collation_cache, 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, ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by lc_collate_is_c(), and pg_newlocale_from_collation().

◆ make_icu_collator()

void make_icu_collator ( const char *  iculocstr,
struct pg_locale_struct resultp 
)

Definition at line 1404 of file pg_locale.c.

1406 {
1407 #ifdef USE_ICU
1408  UCollator *collator;
1409  UErrorCode status;
1410 
1411  status = U_ZERO_ERROR;
1412  collator = ucol_open(iculocstr, &status);
1413  if (U_FAILURE(status))
1414  ereport(ERROR,
1415  (errmsg("could not open collator for locale \"%s\": %s",
1416  iculocstr, u_errorName(status))));
1417 
1418  if (U_ICU_VERSION_MAJOR_NUM < 54)
1419  icu_set_collation_attributes(collator, iculocstr);
1420 
1421  /* We will leak this string if the caller errors later :-( */
1422  resultp->info.icu.locale = MemoryContextStrdup(TopMemoryContext, iculocstr);
1423  resultp->info.icu.ucol = collator;
1424 #else /* not USE_ICU */
1425  /* could get here if a collation was created by a build with ICU */
1426  ereport(ERROR,
1427  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1428  errmsg("ICU is not supported in this build")));
1429 #endif /* not USE_ICU */
1430 }
union pg_locale_struct::@142 info

References ereport, errcode(), errmsg(), ERROR, pg_locale_struct::info, MemoryContextStrdup(), status(), and TopMemoryContext.

Referenced by CheckMyDatabase(), and pg_newlocale_from_collation().

◆ pg_newlocale_from_collation()

pg_locale_t pg_newlocale_from_collation ( Oid  collid)

Definition at line 1481 of file pg_locale.c.

1482 {
1483  collation_cache_entry *cache_entry;
1484 
1485  /* Callers must pass a valid OID */
1486  Assert(OidIsValid(collid));
1487 
1488  if (collid == DEFAULT_COLLATION_OID)
1489  {
1490  if (default_locale.provider == COLLPROVIDER_ICU)
1491  return &default_locale;
1492  else
1493  return (pg_locale_t) 0;
1494  }
1495 
1496  cache_entry = lookup_collation_cache(collid, false);
1497 
1498  if (cache_entry->locale == 0)
1499  {
1500  /* We haven't computed this yet in this session, so do it */
1501  HeapTuple tp;
1502  Form_pg_collation collform;
1503  struct pg_locale_struct result;
1504  pg_locale_t resultp;
1505  Datum datum;
1506  bool isnull;
1507 
1508  tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
1509  if (!HeapTupleIsValid(tp))
1510  elog(ERROR, "cache lookup failed for collation %u", collid);
1511  collform = (Form_pg_collation) GETSTRUCT(tp);
1512 
1513  /* We'll fill in the result struct locally before allocating memory */
1514  memset(&result, 0, sizeof(result));
1515  result.provider = collform->collprovider;
1516  result.deterministic = collform->collisdeterministic;
1517 
1518  if (collform->collprovider == COLLPROVIDER_LIBC)
1519  {
1520 #ifdef HAVE_LOCALE_T
1521  const char *collcollate;
1522  const char *collctype pg_attribute_unused();
1523  locale_t loc;
1524 
1525  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collcollate, &isnull);
1526  Assert(!isnull);
1527  collcollate = TextDatumGetCString(datum);
1528  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collctype, &isnull);
1529  Assert(!isnull);
1530  collctype = TextDatumGetCString(datum);
1531 
1532  if (strcmp(collcollate, collctype) == 0)
1533  {
1534  /* Normal case where they're the same */
1535  errno = 0;
1536 #ifndef WIN32
1537  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
1538  NULL);
1539 #else
1540  loc = _create_locale(LC_ALL, collcollate);
1541 #endif
1542  if (!loc)
1543  report_newlocale_failure(collcollate);
1544  }
1545  else
1546  {
1547 #ifndef WIN32
1548  /* We need two newlocale() steps */
1549  locale_t loc1;
1550 
1551  errno = 0;
1552  loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
1553  if (!loc1)
1554  report_newlocale_failure(collcollate);
1555  errno = 0;
1556  loc = newlocale(LC_CTYPE_MASK, collctype, loc1);
1557  if (!loc)
1558  report_newlocale_failure(collctype);
1559 #else
1560 
1561  /*
1562  * XXX The _create_locale() API doesn't appear to support
1563  * this. Could perhaps be worked around by changing
1564  * pg_locale_t to contain two separate fields.
1565  */
1566  ereport(ERROR,
1567  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1568  errmsg("collations with different collate and ctype values are not supported on this platform")));
1569 #endif
1570  }
1571 
1572  result.info.lt = loc;
1573 #else /* not HAVE_LOCALE_T */
1574  /* platform that doesn't support locale_t */
1575  ereport(ERROR,
1576  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1577  errmsg("collation provider LIBC is not supported on this platform")));
1578 #endif /* not HAVE_LOCALE_T */
1579  }
1580  else if (collform->collprovider == COLLPROVIDER_ICU)
1581  {
1582  const char *iculocstr;
1583 
1584  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_colliculocale, &isnull);
1585  Assert(!isnull);
1586  iculocstr = TextDatumGetCString(datum);
1587  make_icu_collator(iculocstr, &result);
1588  }
1589 
1590  datum = SysCacheGetAttr(COLLOID, tp, Anum_pg_collation_collversion,
1591  &isnull);
1592  if (!isnull)
1593  {
1594  char *actual_versionstr;
1595  char *collversionstr;
1596 
1597  collversionstr = TextDatumGetCString(datum);
1598 
1599  datum = SysCacheGetAttr(COLLOID, tp, collform->collprovider == COLLPROVIDER_ICU ? Anum_pg_collation_colliculocale : Anum_pg_collation_collcollate, &isnull);
1600  Assert(!isnull);
1601 
1602  actual_versionstr = get_collation_actual_version(collform->collprovider,
1603  TextDatumGetCString(datum));
1604  if (!actual_versionstr)
1605  {
1606  /*
1607  * This could happen when specifying a version in CREATE
1608  * COLLATION but the provider does not support versioning, or
1609  * manually creating a mess in the catalogs.
1610  */
1611  ereport(ERROR,
1612  (errmsg("collation \"%s\" has no actual version, but a version was recorded",
1613  NameStr(collform->collname))));
1614  }
1615 
1616  if (strcmp(actual_versionstr, collversionstr) != 0)
1617  ereport(WARNING,
1618  (errmsg("collation \"%s\" has version mismatch",
1619  NameStr(collform->collname)),
1620  errdetail("The collation in the database was created using version %s, "
1621  "but the operating system provides version %s.",
1622  collversionstr, actual_versionstr),
1623  errhint("Rebuild all objects affected by this collation and run "
1624  "ALTER COLLATION %s REFRESH VERSION, "
1625  "or build PostgreSQL with the right library version.",
1626  quote_qualified_identifier(get_namespace_name(collform->collnamespace),
1627  NameStr(collform->collname)))));
1628  }
1629 
1630  ReleaseSysCache(tp);
1631 
1632  /* We'll keep the pg_locale_t structures in TopMemoryContext */
1633  resultp = MemoryContextAlloc(TopMemoryContext, sizeof(*resultp));
1634  *resultp = result;
1635 
1636  cache_entry->locale = resultp;
1637  }
1638 
1639  return cache_entry->locale;
1640 }
#define NameStr(name)
Definition: c.h:682
int errdetail(const char *fmt,...)
Definition: elog.c:1039
pg_attribute_unused() static bool check_table(const char *(*TABLE)[2]
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3331
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
void make_icu_collator(const char *iculocstr, struct pg_locale_struct *resultp)
Definition: pg_locale.c:1404
char * get_collation_actual_version(char collprovider, const char *collcollate)
Definition: pg_locale.c:1647
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:11613

References Assert(), COLLOID, default_locale, 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(), make_icu_collator(), MemoryContextAlloc(), NameStr, ObjectIdGetDatum(), OidIsValid, pg_attribute_unused(), pg_locale_struct::provider, quote_qualified_identifier(), ReleaseSysCache(), SearchSysCache1(), 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().

◆ pg_perm_setlocale()

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

Definition at line 144 of file pg_locale.c.

145 {
146  char *result;
147  const char *envvar;
148 
149 #ifndef WIN32
150  result = setlocale(category, locale);
151 #else
152 
153  /*
154  * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
155  * the given value is good and set it in the environment variables. We
156  * must ignore attempts to set to "", which means "keep using the old
157  * environment value".
158  */
159 #ifdef LC_MESSAGES
160  if (category == LC_MESSAGES)
161  {
162  result = (char *) locale;
163  if (locale == NULL || locale[0] == '\0')
164  return result;
165  }
166  else
167 #endif
168  result = setlocale(category, locale);
169 #endif /* WIN32 */
170 
171  if (result == NULL)
172  return result; /* fall out immediately on failure */
173 
174  /*
175  * Use the right encoding in translated messages. Under ENABLE_NLS, let
176  * pg_bind_textdomain_codeset() figure it out. Under !ENABLE_NLS, message
177  * format strings are ASCII, but database-encoding strings may enter the
178  * message via %s. This makes the overall message encoding equal to the
179  * database encoding.
180  */
181  if (category == LC_CTYPE)
182  {
183  static char save_lc_ctype[LOCALE_NAME_BUFLEN];
184 
185  /* copy setlocale() return value before callee invokes it again */
186  strlcpy(save_lc_ctype, result, sizeof(save_lc_ctype));
187  result = save_lc_ctype;
188 
189 #ifdef ENABLE_NLS
190  SetMessageEncoding(pg_bind_textdomain_codeset(textdomain(NULL)));
191 #else
193 #endif
194  }
195 
196  switch (category)
197  {
198  case LC_COLLATE:
199  envvar = "LC_COLLATE";
200  break;
201  case LC_CTYPE:
202  envvar = "LC_CTYPE";
203  break;
204 #ifdef LC_MESSAGES
205  case LC_MESSAGES:
206  envvar = "LC_MESSAGES";
207 #ifdef WIN32
208  result = IsoLocaleName(locale);
209  if (result == NULL)
210  result = (char *) locale;
211  elog(DEBUG3, "IsoLocaleName() executed; locale: \"%s\"", result);
212 #endif /* WIN32 */
213  break;
214 #endif /* LC_MESSAGES */
215  case LC_MONETARY:
216  envvar = "LC_MONETARY";
217  break;
218  case LC_NUMERIC:
219  envvar = "LC_NUMERIC";
220  break;
221  case LC_TIME:
222  envvar = "LC_TIME";
223  break;
224  default:
225  elog(FATAL, "unrecognized LC category: %d", category);
226  return NULL; /* keep compiler quiet */
227  }
228 
229  if (setenv(envvar, result, 1) != 0)
230  return NULL;
231 
232  return result;
233 }
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1114
#define LOCALE_NAME_BUFLEN
Definition: pg_locale.h:36
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define setenv(x, y, z)
Definition: win32_port.h:534

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

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

◆ PGLC_localeconv()

struct lconv* PGLC_localeconv ( void  )

Definition at line 456 of file pg_locale.c.

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

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

◆ struct_lconv_is_valid()

static bool struct_lconv_is_valid ( struct lconv *  s)
static

Definition at line 395 of file pg_locale.c.

396 {
397  if (s->decimal_point == NULL)
398  return false;
399  if (s->thousands_sep == NULL)
400  return false;
401  if (s->grouping == NULL)
402  return false;
403  if (s->int_curr_symbol == NULL)
404  return false;
405  if (s->currency_symbol == NULL)
406  return false;
407  if (s->mon_decimal_point == NULL)
408  return false;
409  if (s->mon_thousands_sep == NULL)
410  return false;
411  if (s->mon_grouping == NULL)
412  return false;
413  if (s->positive_sign == NULL)
414  return false;
415  if (s->negative_sign == NULL)
416  return false;
417  return true;
418 }

Referenced by PGLC_localeconv().

◆ wchar2char()

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

Definition at line 1990 of file pg_locale.c.

1991 {
1992  size_t result;
1993 
1994  Assert(!locale || locale->provider == COLLPROVIDER_LIBC);
1995 
1996  if (tolen == 0)
1997  return 0;
1998 
1999 #ifdef WIN32
2000 
2001  /*
2002  * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
2003  * for some reason mbstowcs and wcstombs won't do this for us, so we use
2004  * MultiByteToWideChar().
2005  */
2006  if (GetDatabaseEncoding() == PG_UTF8)
2007  {
2008  result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
2009  NULL, NULL);
2010  /* A zero return is failure */
2011  if (result <= 0)
2012  result = -1;
2013  else
2014  {
2015  Assert(result <= tolen);
2016  /* Microsoft counts the zero terminator in the result */
2017  result--;
2018  }
2019  }
2020  else
2021 #endif /* WIN32 */
2022  if (locale == (pg_locale_t) 0)
2023  {
2024  /* Use wcstombs directly for the default locale */
2025  result = wcstombs(to, from, tolen);
2026  }
2027  else
2028  {
2029 #ifdef HAVE_LOCALE_T
2030 #ifdef HAVE_WCSTOMBS_L
2031  /* Use wcstombs_l for nondefault locales */
2032  result = wcstombs_l(to, from, tolen, locale->info.lt);
2033 #else /* !HAVE_WCSTOMBS_L */
2034  /* We have to temporarily set the locale as current ... ugh */
2035  locale_t save_locale = uselocale(locale->info.lt);
2036 
2037  result = wcstombs(to, from, tolen);
2038 
2039  uselocale(save_locale);
2040 #endif /* HAVE_WCSTOMBS_L */
2041 #else /* !HAVE_LOCALE_T */
2042  /* Can't have locale != 0 without HAVE_LOCALE_T */
2043  elog(ERROR, "wcstombs_l is not available");
2044  result = 0; /* keep compiler quiet */
2045 #endif /* HAVE_LOCALE_T */
2046  }
2047 
2048  return result;
2049 }
#define wcstombs_l
Definition: win32_port.h:445

References Assert(), elog(), ERROR, GetDatabaseEncoding(), locale, locale_t, PG_UTF8, and wcstombs_l.

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

Variable Documentation

◆ collation_cache

HTAB* collation_cache = NULL
static

Definition at line 118 of file pg_locale.c.

Referenced by lookup_collation_cache().

◆ CurrentLCTimeValid

bool CurrentLCTimeValid = false
static

Definition at line 105 of file pg_locale.c.

Referenced by assign_locale_time(), and cache_locale_time().

◆ CurrentLocaleConvValid

bool CurrentLocaleConvValid = false
static

Definition at line 104 of file pg_locale.c.

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

◆ default_locale

struct pg_locale_struct default_locale

Definition at line 1352 of file pg_locale.c.

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

◆ locale_messages

char* locale_messages

Definition at line 86 of file pg_locale.c.

◆ locale_monetary

char* locale_monetary

Definition at line 87 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_numeric

char* locale_numeric

Definition at line 88 of file pg_locale.c.

Referenced by PGLC_localeconv().

◆ locale_time

char* locale_time

Definition at line 89 of file pg_locale.c.

Referenced by cache_locale_time().

◆ localized_abbrev_days

char* localized_abbrev_days[7+1]

Definition at line 98 of file pg_locale.c.

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

◆ localized_abbrev_months

char* localized_abbrev_months[12+1]

Definition at line 100 of file pg_locale.c.

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

◆ localized_full_days

char* localized_full_days[7+1]

Definition at line 99 of file pg_locale.c.

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

◆ localized_full_months

char* localized_full_months[12+1]

Definition at line 101 of file pg_locale.c.

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