1 /*-----------------------------------------------------------------------
3 * PostgreSQL locale utilities
5 * Portions Copyright (c) 2002-2008, PostgreSQL Global Development Group
7 * $PostgreSQL: pgsql/src/backend/utils/adt/pg_locale.c,v 1.40 2008/01/01 19:45:52 momjian Exp $
9 *-----------------------------------------------------------------------
13 * Here is how the locale stuff is handled: LC_COLLATE and LC_CTYPE
14 * are fixed by initdb, stored in pg_control, and cannot be changed.
15 * Thus, the effects of strcoll(), strxfrm(), isupper(), toupper(),
16 * etc. are always in the same fixed locale.
18 * LC_MESSAGES is settable at run time and will take effect
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
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))
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.
52 #include "catalog/pg_control.h"
53 #include "utils/pg_locale.h"
56 /* GUC storage area */
58 char *locale_messages;
59 char *locale_monetary;
63 /* indicates whether locale information cache is valid */
64 static bool CurrentLocaleConvValid = false;
66 /* Environment variable storage area */
68 #define LC_ENV_BUFSIZE (LOCALE_NAME_BUFLEN + 20)
70 static char lc_collate_envbuf[LC_ENV_BUFSIZE];
71 static char lc_ctype_envbuf[LC_ENV_BUFSIZE];
74 static char lc_messages_envbuf[LC_ENV_BUFSIZE];
76 static char lc_monetary_envbuf[LC_ENV_BUFSIZE];
77 static char lc_numeric_envbuf[LC_ENV_BUFSIZE];
78 static char lc_time_envbuf[LC_ENV_BUFSIZE];
84 * This is identical to the libc function setlocale(), with the addition
85 * that if the operation is successful, the corresponding LC_XXX environment
86 * variable is set to match. By setting the environment variable, we ensure
87 * that any subsequent use of setlocale(..., "") will preserve the settings
88 * made through this routine. Of course, LC_ALL must also be unset to fully
89 * ensure that, but that has to be done elsewhere after all the individual
90 * LC_XXX variables have been set correctly. (Thank you Perl for making this
94 pg_perm_setlocale(int category, const char *locale)
101 result = setlocale(category, locale);
105 * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
106 * the given value is good and set it in the environment variables. We
107 * must ignore attempts to set to "", which means "keep using the old
108 * environment value".
111 if (category == LC_MESSAGES)
113 result = (char *) locale;
114 if (locale == NULL || locale[0] == '\0')
119 result = setlocale(category, locale);
123 return result; /* fall out immediately on failure */
128 envvar = "LC_COLLATE";
129 envbuf = lc_collate_envbuf;
133 envbuf = lc_ctype_envbuf;
137 envvar = "LC_MESSAGES";
138 envbuf = lc_messages_envbuf;
142 envvar = "LC_MONETARY";
143 envbuf = lc_monetary_envbuf;
146 envvar = "LC_NUMERIC";
147 envbuf = lc_numeric_envbuf;
151 envbuf = lc_time_envbuf;
154 elog(FATAL, "unrecognized LC category: %d", category);
155 envvar = NULL; /* keep compiler quiet */
160 snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
168 * On Windows, we need to modify both the process environment and the
169 * cached version in msvcrt
171 if (!SetEnvironmentVariable(envvar, result))
181 /* GUC assign hooks */
184 * This is common code for several locale categories. This doesn't
185 * actually set the locale permanently, it only tests if the locale is
186 * valid. (See explanation at the top of this file.)
188 * Note: we accept value = "" as selecting the postmaster's environment
189 * value, whatever it was (so long as the environment setting is legal).
190 * This will have been locked down by an earlier call to pg_perm_setlocale.
193 locale_xxx_assign(int category, const char *value, bool doit, GucSource source)
197 save = setlocale(category, NULL);
199 return NULL; /* won't happen, we hope */
201 /* save may be pointing at a modifiable scratch variable, see above */
202 save = pstrdup(save);
204 if (!setlocale(category, value))
205 value = NULL; /* set failure return marker */
207 setlocale(category, save); /* assume this won't fail */
210 /* need to reload cache next time? */
211 if (doit && value != NULL)
212 CurrentLocaleConvValid = false;
219 locale_monetary_assign(const char *value, bool doit, GucSource source)
221 return locale_xxx_assign(LC_MONETARY, value, doit, source);
225 locale_numeric_assign(const char *value, bool doit, GucSource source)
227 return locale_xxx_assign(LC_NUMERIC, value, doit, source);
231 locale_time_assign(const char *value, bool doit, GucSource source)
233 return locale_xxx_assign(LC_TIME, value, doit, source);
238 * We allow LC_MESSAGES to actually be set globally.
240 * Note: we normally disallow value = "" because it wouldn't have consistent
241 * semantics (it'd effectively just use the previous value). However, this
242 * is the value passed for PGC_S_DEFAULT, so don't complain in that case,
243 * not even if the attempted setting fails due to invalid environment value.
244 * The idea there is just to accept the environment setting *if possible*
245 * during startup, until we can read the proper value from postgresql.conf.
248 locale_messages_assign(const char *value, bool doit, GucSource source)
250 if (*value == '\0' && source != PGC_S_DEFAULT)
254 * LC_MESSAGES category does not exist everywhere, but accept it anyway
256 * On Windows, we can't even check the value, so the non-doit case is a
262 if (!pg_perm_setlocale(LC_MESSAGES, value))
263 if (source != PGC_S_DEFAULT)
268 value = locale_xxx_assign(LC_MESSAGES, value, false, source);
270 #endif /* LC_MESSAGES */
276 * We'd like to cache whether LC_COLLATE is C (or POSIX), so we can
277 * optimize a few code paths in various places.
280 lc_collate_is_c(void)
282 /* Cache result so we only have to compute it once */
283 static int result = -1;
287 return (bool) result;
288 localeptr = setlocale(LC_COLLATE, NULL);
290 elog(ERROR, "invalid LC_COLLATE setting");
292 if (strcmp(localeptr, "C") == 0)
294 else if (strcmp(localeptr, "POSIX") == 0)
298 return (bool) result;
303 * We'd like to cache whether LC_CTYPE is C (or POSIX), so we can
304 * optimize a few code paths in various places.
309 /* Cache result so we only have to compute it once */
310 static int result = -1;
314 return (bool) result;
315 localeptr = setlocale(LC_CTYPE, NULL);
317 elog(ERROR, "invalid LC_CTYPE setting");
319 if (strcmp(localeptr, "C") == 0)
321 else if (strcmp(localeptr, "POSIX") == 0)
325 return (bool) result;
330 * Frees the malloced content of a struct lconv. (But not the struct
334 free_struct_lconv(struct lconv * s)
339 if (s->currency_symbol)
340 free(s->currency_symbol);
341 if (s->decimal_point)
342 free(s->decimal_point);
345 if (s->thousands_sep)
346 free(s->thousands_sep);
347 if (s->int_curr_symbol)
348 free(s->int_curr_symbol);
349 if (s->mon_decimal_point)
350 free(s->mon_decimal_point);
352 free(s->mon_grouping);
353 if (s->mon_thousands_sep)
354 free(s->mon_thousands_sep);
355 if (s->negative_sign)
356 free(s->negative_sign);
357 if (s->positive_sign)
358 free(s->positive_sign);
363 * Return the POSIX lconv struct (contains number/money formatting
364 * information) with locale information for all categories.
367 PGLC_localeconv(void)
369 static struct lconv CurrentLocaleConv;
370 struct lconv *extlconv;
371 char *save_lc_monetary;
372 char *save_lc_numeric;
374 /* Did we do it already? */
375 if (CurrentLocaleConvValid)
376 return &CurrentLocaleConv;
378 free_struct_lconv(&CurrentLocaleConv);
380 /* Set user's values of monetary and numeric locales */
381 save_lc_monetary = setlocale(LC_MONETARY, NULL);
382 if (save_lc_monetary)
383 save_lc_monetary = pstrdup(save_lc_monetary);
384 save_lc_numeric = setlocale(LC_NUMERIC, NULL);
386 save_lc_numeric = pstrdup(save_lc_numeric);
388 setlocale(LC_MONETARY, locale_monetary);
389 setlocale(LC_NUMERIC, locale_numeric);
391 /* Get formatting information */
392 extlconv = localeconv();
395 * Must copy all values since restoring internal settings may overwrite
396 * localeconv()'s results.
398 CurrentLocaleConv = *extlconv;
399 CurrentLocaleConv.currency_symbol = strdup(extlconv->currency_symbol);
400 CurrentLocaleConv.decimal_point = strdup(extlconv->decimal_point);
401 CurrentLocaleConv.grouping = strdup(extlconv->grouping);
402 CurrentLocaleConv.thousands_sep = strdup(extlconv->thousands_sep);
403 CurrentLocaleConv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
404 CurrentLocaleConv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
405 CurrentLocaleConv.mon_grouping = strdup(extlconv->mon_grouping);
406 CurrentLocaleConv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
407 CurrentLocaleConv.negative_sign = strdup(extlconv->negative_sign);
408 CurrentLocaleConv.positive_sign = strdup(extlconv->positive_sign);
409 CurrentLocaleConv.n_sign_posn = extlconv->n_sign_posn;
411 /* Try to restore internal settings */
412 if (save_lc_monetary)
414 setlocale(LC_MONETARY, save_lc_monetary);
415 pfree(save_lc_monetary);
420 setlocale(LC_NUMERIC, save_lc_numeric);
421 pfree(save_lc_numeric);
424 CurrentLocaleConvValid = true;
425 return &CurrentLocaleConv;