Don't use 'return' where you should use 'PG_RETURN_xxx'.
[PostgreSQL.git] / src / backend / utils / adt / pg_locale.c
bloba75b5c4b48c483519b5725ab9b277d67065bb10f
1 /*-----------------------------------------------------------------------
3 * PostgreSQL locale utilities
5 * Portions Copyright (c) 2002-2009, PostgreSQL Global Development Group
7 * $PostgreSQL$
9 *-----------------------------------------------------------------------
12 /*----------
13 * Here is how the locale stuff is handled: LC_COLLATE and LC_CTYPE
14 * are fixed at CREATE DATABASE time, stored in pg_database, and cannot
15 * be changed. Thus, the effects of strcoll(), strxfrm(), isupper(),
16 * toupper(), etc. are always in the same fixed locale.
18 * LC_MESSAGES is settable at run time and will take effect
19 * immediately.
21 * The other categories, LC_MONETARY, LC_NUMERIC, and LC_TIME are also
22 * settable at run-time. However, we don't actually set those locale
23 * categories permanently. This would have bizarre effects like no
24 * longer accepting standard floating-point literals in some locales.
25 * Instead, we only set the locales briefly when needed, cache the
26 * required information obtained from localeconv(), and set them back.
27 * The cached information is only used by the formatting functions
28 * (to_char, etc.) and the money type. For the user, this should all be
29 * transparent.
31 * !!! NOW HEAR THIS !!!
33 * We've been bitten repeatedly by this bug, so let's try to keep it in
34 * mind in future: on some platforms, the locale functions return pointers
35 * to static data that will be overwritten by any later locale function.
36 * Thus, for example, the obvious-looking sequence
37 * save = setlocale(category, NULL);
38 * if (!setlocale(category, value))
39 * fail = true;
40 * setlocale(category, save);
41 * DOES NOT WORK RELIABLY: on some platforms the second setlocale() call
42 * will change the memory save is pointing at. To do this sort of thing
43 * safely, you *must* pstrdup what setlocale returns the first time.
44 *----------
48 #include "postgres.h"
50 #include <locale.h>
51 #include <time.h>
53 #include "catalog/pg_control.h"
54 #include "mb/pg_wchar.h"
55 #include "utils/memutils.h"
56 #include "utils/pg_locale.h"
58 #ifdef WIN32
59 #include <shlwapi.h>
60 #endif
62 #define MAX_L10N_DATA 80
65 /* GUC settings */
66 char *locale_messages;
67 char *locale_monetary;
68 char *locale_numeric;
69 char *locale_time;
71 /* lc_time localization cache */
72 char *localized_abbrev_days[7];
73 char *localized_full_days[7];
74 char *localized_abbrev_months[12];
75 char *localized_full_months[12];
77 /* indicates whether locale information cache is valid */
78 static bool CurrentLocaleConvValid = false;
79 static bool CurrentLCTimeValid = false;
81 /* Environment variable storage area */
83 #define LC_ENV_BUFSIZE (NAMEDATALEN + 20)
85 static char lc_collate_envbuf[LC_ENV_BUFSIZE];
86 static char lc_ctype_envbuf[LC_ENV_BUFSIZE];
88 #ifdef LC_MESSAGES
89 static char lc_messages_envbuf[LC_ENV_BUFSIZE];
90 #endif
91 static char lc_monetary_envbuf[LC_ENV_BUFSIZE];
92 static char lc_numeric_envbuf[LC_ENV_BUFSIZE];
93 static char lc_time_envbuf[LC_ENV_BUFSIZE];
95 #if defined(WIN32) && defined(LC_MESSAGES)
96 static char *IsoLocaleName(const char *); /* MSVC specific */
97 #endif
101 * pg_perm_setlocale
103 * This is identical to the libc function setlocale(), with the addition
104 * that if the operation is successful, the corresponding LC_XXX environment
105 * variable is set to match. By setting the environment variable, we ensure
106 * that any subsequent use of setlocale(..., "") will preserve the settings
107 * made through this routine. Of course, LC_ALL must also be unset to fully
108 * ensure that, but that has to be done elsewhere after all the individual
109 * LC_XXX variables have been set correctly. (Thank you Perl for making this
110 * kluge necessary.)
112 char *
113 pg_perm_setlocale(int category, const char *locale)
115 char *result;
116 const char *envvar;
117 char *envbuf;
119 #ifndef WIN32
120 result = setlocale(category, locale);
121 #else
124 * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
125 * the given value is good and set it in the environment variables. We
126 * must ignore attempts to set to "", which means "keep using the old
127 * environment value".
129 #ifdef LC_MESSAGES
130 if (category == LC_MESSAGES)
132 result = (char *) locale;
133 if (locale == NULL || locale[0] == '\0')
134 return result;
136 else
137 #endif
138 result = setlocale(category, locale);
139 #endif /* WIN32 */
141 if (result == NULL)
142 return result; /* fall out immediately on failure */
144 switch (category)
146 case LC_COLLATE:
147 envvar = "LC_COLLATE";
148 envbuf = lc_collate_envbuf;
149 break;
150 case LC_CTYPE:
151 envvar = "LC_CTYPE";
152 envbuf = lc_ctype_envbuf;
153 break;
154 #ifdef LC_MESSAGES
155 case LC_MESSAGES:
156 envvar = "LC_MESSAGES";
157 envbuf = lc_messages_envbuf;
158 #ifdef WIN32
159 result = IsoLocaleName(locale);
160 if (result == NULL)
161 result = (char *) locale;
162 #endif /* WIN32 */
163 break;
164 #endif /* LC_MESSAGES */
165 case LC_MONETARY:
166 envvar = "LC_MONETARY";
167 envbuf = lc_monetary_envbuf;
168 break;
169 case LC_NUMERIC:
170 envvar = "LC_NUMERIC";
171 envbuf = lc_numeric_envbuf;
172 break;
173 case LC_TIME:
174 envvar = "LC_TIME";
175 envbuf = lc_time_envbuf;
176 break;
177 default:
178 elog(FATAL, "unrecognized LC category: %d", category);
179 envvar = NULL; /* keep compiler quiet */
180 envbuf = NULL;
181 return NULL;
184 snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
186 if (putenv(envbuf))
187 return NULL;
189 return result;
194 * Is the locale name valid for the locale category?
196 bool
197 check_locale(int category, const char *value)
199 char *save;
200 bool ret;
202 save = setlocale(category, NULL);
203 if (!save)
204 return false; /* won't happen, we hope */
206 /* save may be pointing at a modifiable scratch variable, see above */
207 save = pstrdup(save);
209 /* set the locale with setlocale, to see if it accepts it. */
210 ret = (setlocale(category, value) != NULL);
212 setlocale(category, save); /* assume this won't fail */
213 pfree(save);
215 return ret;
218 /* GUC assign hooks */
221 * This is common code for several locale categories. This doesn't
222 * actually set the locale permanently, it only tests if the locale is
223 * valid. (See explanation at the top of this file.)
225 * Note: we accept value = "" as selecting the postmaster's environment
226 * value, whatever it was (so long as the environment setting is legal).
227 * This will have been locked down by an earlier call to pg_perm_setlocale.
229 static const char *
230 locale_xxx_assign(int category, const char *value, bool doit, GucSource source)
232 if (!check_locale(category, value))
233 value = NULL; /* set failure return marker */
235 /* need to reload cache next time? */
236 if (doit && value != NULL)
238 CurrentLocaleConvValid = false;
239 CurrentLCTimeValid = false;
242 return value;
246 const char *
247 locale_monetary_assign(const char *value, bool doit, GucSource source)
249 return locale_xxx_assign(LC_MONETARY, value, doit, source);
252 const char *
253 locale_numeric_assign(const char *value, bool doit, GucSource source)
255 return locale_xxx_assign(LC_NUMERIC, value, doit, source);
258 const char *
259 locale_time_assign(const char *value, bool doit, GucSource source)
261 return locale_xxx_assign(LC_TIME, value, doit, source);
266 * We allow LC_MESSAGES to actually be set globally.
268 * Note: we normally disallow value = "" because it wouldn't have consistent
269 * semantics (it'd effectively just use the previous value). However, this
270 * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
271 * not even if the attempted setting fails due to invalid environment value.
272 * The idea there is just to accept the environment setting *if possible*
273 * during startup, until we can read the proper value from postgresql.conf.
275 const char *
276 locale_messages_assign(const char *value, bool doit, GucSource source)
278 if (*value == '\0' && source != PGC_S_DEFAULT)
279 return NULL;
282 * LC_MESSAGES category does not exist everywhere, but accept it anyway
284 * On Windows, we can't even check the value, so the non-doit case is a
285 * no-op
287 #ifdef LC_MESSAGES
288 if (doit)
290 if (!pg_perm_setlocale(LC_MESSAGES, value))
291 if (source != PGC_S_DEFAULT)
292 return NULL;
294 #ifndef WIN32
295 else
296 value = locale_xxx_assign(LC_MESSAGES, value, false, source);
297 #endif /* WIN32 */
298 #endif /* LC_MESSAGES */
299 return value;
304 * We'd like to cache whether LC_COLLATE is C (or POSIX), so we can
305 * optimize a few code paths in various places.
307 bool
308 lc_collate_is_c(void)
310 /* Cache result so we only have to compute it once */
311 static int result = -1;
312 char *localeptr;
314 if (result >= 0)
315 return (bool) result;
316 localeptr = setlocale(LC_COLLATE, NULL);
317 if (!localeptr)
318 elog(ERROR, "invalid LC_COLLATE setting");
320 if (strcmp(localeptr, "C") == 0)
321 result = true;
322 else if (strcmp(localeptr, "POSIX") == 0)
323 result = true;
324 else
325 result = false;
326 return (bool) result;
331 * We'd like to cache whether LC_CTYPE is C (or POSIX), so we can
332 * optimize a few code paths in various places.
334 bool
335 lc_ctype_is_c(void)
337 /* Cache result so we only have to compute it once */
338 static int result = -1;
339 char *localeptr;
341 if (result >= 0)
342 return (bool) result;
343 localeptr = setlocale(LC_CTYPE, NULL);
344 if (!localeptr)
345 elog(ERROR, "invalid LC_CTYPE setting");
347 if (strcmp(localeptr, "C") == 0)
348 result = true;
349 else if (strcmp(localeptr, "POSIX") == 0)
350 result = true;
351 else
352 result = false;
353 return (bool) result;
358 * Frees the malloced content of a struct lconv. (But not the struct
359 * itself.)
361 static void
362 free_struct_lconv(struct lconv * s)
364 if (s == NULL)
365 return;
367 if (s->currency_symbol)
368 free(s->currency_symbol);
369 if (s->decimal_point)
370 free(s->decimal_point);
371 if (s->grouping)
372 free(s->grouping);
373 if (s->thousands_sep)
374 free(s->thousands_sep);
375 if (s->int_curr_symbol)
376 free(s->int_curr_symbol);
377 if (s->mon_decimal_point)
378 free(s->mon_decimal_point);
379 if (s->mon_grouping)
380 free(s->mon_grouping);
381 if (s->mon_thousands_sep)
382 free(s->mon_thousands_sep);
383 if (s->negative_sign)
384 free(s->negative_sign);
385 if (s->positive_sign)
386 free(s->positive_sign);
391 * Return the POSIX lconv struct (contains number/money formatting
392 * information) with locale information for all categories.
394 struct lconv *
395 PGLC_localeconv(void)
397 static struct lconv CurrentLocaleConv;
398 struct lconv *extlconv;
399 char *save_lc_monetary;
400 char *save_lc_numeric;
402 /* Did we do it already? */
403 if (CurrentLocaleConvValid)
404 return &CurrentLocaleConv;
406 free_struct_lconv(&CurrentLocaleConv);
408 /* Set user's values of monetary and numeric locales */
409 save_lc_monetary = setlocale(LC_MONETARY, NULL);
410 if (save_lc_monetary)
411 save_lc_monetary = pstrdup(save_lc_monetary);
412 save_lc_numeric = setlocale(LC_NUMERIC, NULL);
413 if (save_lc_numeric)
414 save_lc_numeric = pstrdup(save_lc_numeric);
416 setlocale(LC_MONETARY, locale_monetary);
417 setlocale(LC_NUMERIC, locale_numeric);
419 /* Get formatting information */
420 extlconv = localeconv();
423 * Must copy all values since restoring internal settings may overwrite
424 * localeconv()'s results.
426 CurrentLocaleConv = *extlconv;
427 CurrentLocaleConv.currency_symbol = strdup(extlconv->currency_symbol);
428 CurrentLocaleConv.decimal_point = strdup(extlconv->decimal_point);
429 CurrentLocaleConv.grouping = strdup(extlconv->grouping);
430 CurrentLocaleConv.thousands_sep = strdup(extlconv->thousands_sep);
431 CurrentLocaleConv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
432 CurrentLocaleConv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
433 CurrentLocaleConv.mon_grouping = strdup(extlconv->mon_grouping);
434 CurrentLocaleConv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
435 CurrentLocaleConv.negative_sign = strdup(extlconv->negative_sign);
436 CurrentLocaleConv.positive_sign = strdup(extlconv->positive_sign);
437 CurrentLocaleConv.n_sign_posn = extlconv->n_sign_posn;
439 /* Try to restore internal settings */
440 if (save_lc_monetary)
442 setlocale(LC_MONETARY, save_lc_monetary);
443 pfree(save_lc_monetary);
446 if (save_lc_numeric)
448 setlocale(LC_NUMERIC, save_lc_numeric);
449 pfree(save_lc_numeric);
452 CurrentLocaleConvValid = true;
453 return &CurrentLocaleConv;
456 #ifdef WIN32
458 * On win32, strftime() returns the encoding in CP_ACP, which is likely
459 * different from SERVER_ENCODING. This is especially important in Japanese
460 * versions of Windows which will use SJIS encoding, which we don't support
461 * as a server encoding.
463 * Replace strftime() with a version that gets the string in UTF16 and then
464 * converts it to the appropriate encoding as necessary.
466 * Note that this only affects the calls to strftime() in this file, which are
467 * used to get the locale-aware strings. Other parts of the backend use
468 * pg_strftime(), which isn't locale-aware and does not need to be replaced.
470 static size_t
471 strftime_win32(char *dst, size_t dstlen, const wchar_t *format, const struct tm * tm)
473 size_t len;
474 wchar_t wbuf[MAX_L10N_DATA];
475 int encoding;
477 encoding = GetDatabaseEncoding();
479 len = wcsftime(wbuf, MAX_L10N_DATA, format, tm);
480 if (len == 0)
483 * strftime call failed - return 0 with the contents of dst
484 * unspecified
486 return 0;
488 len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen, NULL, NULL);
489 if (len == 0)
490 elog(ERROR,
491 "could not convert string to UTF-8:error %lu", GetLastError());
493 dst[len] = '\0';
494 if (encoding != PG_UTF8)
496 char *convstr = pg_do_encoding_conversion(dst, len, PG_UTF8, encoding);
498 if (dst != convstr)
500 strlcpy(dst, convstr, dstlen);
501 len = strlen(dst);
505 return len;
508 #define strftime(a,b,c,d) strftime_win32(a,b,L##c,d)
509 #endif /* WIN32 */
513 * Update the lc_time localization cache variables if needed.
515 void
516 cache_locale_time(void)
518 char *save_lc_time;
519 time_t timenow;
520 struct tm *timeinfo;
521 char buf[MAX_L10N_DATA];
522 char *ptr;
523 int i;
525 #ifdef WIN32
526 char *save_lc_ctype;
527 #endif
529 /* did we do this already? */
530 if (CurrentLCTimeValid)
531 return;
533 elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
535 #ifdef WIN32
536 /* set user's value of ctype locale */
537 save_lc_ctype = setlocale(LC_CTYPE, NULL);
538 if (save_lc_ctype)
539 save_lc_ctype = pstrdup(save_lc_ctype);
541 setlocale(LC_CTYPE, locale_time);
542 #endif
544 /* set user's value of time locale */
545 save_lc_time = setlocale(LC_TIME, NULL);
546 if (save_lc_time)
547 save_lc_time = pstrdup(save_lc_time);
549 setlocale(LC_TIME, locale_time);
551 timenow = time(NULL);
552 timeinfo = localtime(&timenow);
554 /* localized days */
555 for (i = 0; i < 7; i++)
557 timeinfo->tm_wday = i;
558 strftime(buf, MAX_L10N_DATA, "%a", timeinfo);
559 ptr = MemoryContextStrdup(TopMemoryContext, buf);
560 if (localized_abbrev_days[i])
561 pfree(localized_abbrev_days[i]);
562 localized_abbrev_days[i] = ptr;
564 strftime(buf, MAX_L10N_DATA, "%A", timeinfo);
565 ptr = MemoryContextStrdup(TopMemoryContext, buf);
566 if (localized_full_days[i])
567 pfree(localized_full_days[i]);
568 localized_full_days[i] = ptr;
571 /* localized months */
572 for (i = 0; i < 12; i++)
574 timeinfo->tm_mon = i;
575 timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
576 strftime(buf, MAX_L10N_DATA, "%b", timeinfo);
577 ptr = MemoryContextStrdup(TopMemoryContext, buf);
578 if (localized_abbrev_months[i])
579 pfree(localized_abbrev_months[i]);
580 localized_abbrev_months[i] = ptr;
582 strftime(buf, MAX_L10N_DATA, "%B", timeinfo);
583 ptr = MemoryContextStrdup(TopMemoryContext, buf);
584 if (localized_full_months[i])
585 pfree(localized_full_months[i]);
586 localized_full_months[i] = ptr;
589 /* try to restore internal settings */
590 if (save_lc_time)
592 setlocale(LC_TIME, save_lc_time);
593 pfree(save_lc_time);
596 #ifdef WIN32
597 /* try to restore internal ctype settings */
598 if (save_lc_ctype)
600 setlocale(LC_CTYPE, save_lc_ctype);
601 pfree(save_lc_ctype);
603 #endif
605 CurrentLCTimeValid = true;
609 #if defined(WIN32) && defined(LC_MESSAGES)
611 * Convert Windows locale name to the ISO formatted one
612 * if possible.
614 * This function returns NULL if conversion is impossible,
615 * otherwise returns the pointer to a static area which
616 * contains the iso formatted locale name.
618 static
619 char *
620 IsoLocaleName(const char *winlocname)
622 #if (_MSC_VER >= 1400) /* VC8.0 or later */
623 static char iso_lc_messages[32];
624 _locale_t loct = NULL;
626 if (pg_strcasecmp("c", winlocname) == 0 ||
627 pg_strcasecmp("posix", winlocname) == 0)
629 strcpy(iso_lc_messages, "C");
630 return iso_lc_messages;
633 loct = _create_locale(LC_CTYPE, winlocname);
634 if (loct != NULL)
636 char isolang[32],
637 isocrty[32];
638 LCID lcid;
640 lcid = loct->locinfo->lc_handle[LC_CTYPE];
641 if (lcid == 0)
642 lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
643 _free_locale(loct);
645 if (!GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, isolang, sizeof(isolang)))
646 return NULL;
647 if (!GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, isocrty, sizeof(isocrty)))
648 return NULL;
649 snprintf(iso_lc_messages, sizeof(iso_lc_messages) - 1, "%s_%s", isolang, isocrty);
650 return iso_lc_messages;
652 return NULL;
653 #else
654 return NULL; /* Not supported on this version of msvc/mingw */
655 #endif /* _MSC_VER >= 1400 */
658 #endif /* WIN32 && LC_MESSAGES */