]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/pg_locale.c
Update copyrights in source tree to 2008.
[postgresql] / src / backend / utils / adt / pg_locale.c
1 /*-----------------------------------------------------------------------
2  *
3  * PostgreSQL locale utilities
4  *
5  * Portions Copyright (c) 2002-2008, PostgreSQL Global Development Group
6  *
7  * $PostgreSQL: pgsql/src/backend/utils/adt/pg_locale.c,v 1.40 2008/01/01 19:45:52 momjian Exp $
8  *
9  *-----------------------------------------------------------------------
10  */
11
12 /*----------
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.
17  *
18  * LC_MESSAGES is settable at run time and will take effect
19  * immediately.
20  *
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.
30  *
31  * !!! NOW HEAR THIS !!!
32  *
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  *----------
45  */
46
47
48 #include "postgres.h"
49
50 #include <locale.h>
51
52 #include "catalog/pg_control.h"
53 #include "utils/pg_locale.h"
54
55
56 /* GUC storage area */
57
58 char       *locale_messages;
59 char       *locale_monetary;
60 char       *locale_numeric;
61 char       *locale_time;
62
63 /* indicates whether locale information cache is valid */
64 static bool CurrentLocaleConvValid = false;
65
66 /* Environment variable storage area */
67
68 #define LC_ENV_BUFSIZE (LOCALE_NAME_BUFLEN + 20)
69
70 static char lc_collate_envbuf[LC_ENV_BUFSIZE];
71 static char lc_ctype_envbuf[LC_ENV_BUFSIZE];
72
73 #ifdef LC_MESSAGES
74 static char lc_messages_envbuf[LC_ENV_BUFSIZE];
75 #endif
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];
79
80
81 /*
82  * pg_perm_setlocale
83  *
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
91  * kluge necessary.)
92  */
93 char *
94 pg_perm_setlocale(int category, const char *locale)
95 {
96         char       *result;
97         const char *envvar;
98         char       *envbuf;
99
100 #ifndef WIN32
101         result = setlocale(category, locale);
102 #else
103
104         /*
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".
109          */
110 #ifdef LC_MESSAGES
111         if (category == LC_MESSAGES)
112         {
113                 result = (char *) locale;
114                 if (locale == NULL || locale[0] == '\0')
115                         return result;
116         }
117         else
118 #endif
119                 result = setlocale(category, locale);
120 #endif   /* WIN32 */
121
122         if (result == NULL)
123                 return result;                  /* fall out immediately on failure */
124
125         switch (category)
126         {
127                 case LC_COLLATE:
128                         envvar = "LC_COLLATE";
129                         envbuf = lc_collate_envbuf;
130                         break;
131                 case LC_CTYPE:
132                         envvar = "LC_CTYPE";
133                         envbuf = lc_ctype_envbuf;
134                         break;
135 #ifdef LC_MESSAGES
136                 case LC_MESSAGES:
137                         envvar = "LC_MESSAGES";
138                         envbuf = lc_messages_envbuf;
139                         break;
140 #endif
141                 case LC_MONETARY:
142                         envvar = "LC_MONETARY";
143                         envbuf = lc_monetary_envbuf;
144                         break;
145                 case LC_NUMERIC:
146                         envvar = "LC_NUMERIC";
147                         envbuf = lc_numeric_envbuf;
148                         break;
149                 case LC_TIME:
150                         envvar = "LC_TIME";
151                         envbuf = lc_time_envbuf;
152                         break;
153                 default:
154                         elog(FATAL, "unrecognized LC category: %d", category);
155                         envvar = NULL;          /* keep compiler quiet */
156                         envbuf = NULL;
157                         break;
158         }
159
160         snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
161
162 #ifndef WIN32
163         if (putenv(envbuf))
164                 return NULL;
165 #else
166
167         /*
168          * On Windows, we need to modify both the process environment and the
169          * cached version in msvcrt
170          */
171         if (!SetEnvironmentVariable(envvar, result))
172                 return NULL;
173         if (_putenv(envbuf))
174                 return NULL;
175 #endif
176
177         return result;
178 }
179
180
181 /* GUC assign hooks */
182
183 /*
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.)
187  *
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.
191  */
192 static const char *
193 locale_xxx_assign(int category, const char *value, bool doit, GucSource source)
194 {
195         char       *save;
196
197         save = setlocale(category, NULL);
198         if (!save)
199                 return NULL;                    /* won't happen, we hope */
200
201         /* save may be pointing at a modifiable scratch variable, see above */
202         save = pstrdup(save);
203
204         if (!setlocale(category, value))
205                 value = NULL;                   /* set failure return marker */
206
207         setlocale(category, save);      /* assume this won't fail */
208         pfree(save);
209
210         /* need to reload cache next time? */
211         if (doit && value != NULL)
212                 CurrentLocaleConvValid = false;
213
214         return value;
215 }
216
217
218 const char *
219 locale_monetary_assign(const char *value, bool doit, GucSource source)
220 {
221         return locale_xxx_assign(LC_MONETARY, value, doit, source);
222 }
223
224 const char *
225 locale_numeric_assign(const char *value, bool doit, GucSource source)
226 {
227         return locale_xxx_assign(LC_NUMERIC, value, doit, source);
228 }
229
230 const char *
231 locale_time_assign(const char *value, bool doit, GucSource source)
232 {
233         return locale_xxx_assign(LC_TIME, value, doit, source);
234 }
235
236
237 /*
238  * We allow LC_MESSAGES to actually be set globally.
239  *
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.
246  */
247 const char *
248 locale_messages_assign(const char *value, bool doit, GucSource source)
249 {
250         if (*value == '\0' && source != PGC_S_DEFAULT)
251                 return NULL;
252
253         /*
254          * LC_MESSAGES category does not exist everywhere, but accept it anyway
255          *
256          * On Windows, we can't even check the value, so the non-doit case is a
257          * no-op
258          */
259 #ifdef LC_MESSAGES
260         if (doit)
261         {
262                 if (!pg_perm_setlocale(LC_MESSAGES, value))
263                         if (source != PGC_S_DEFAULT)
264                                 return NULL;
265         }
266 #ifndef WIN32
267         else
268                 value = locale_xxx_assign(LC_MESSAGES, value, false, source);
269 #endif   /* WIN32 */
270 #endif   /* LC_MESSAGES */
271         return value;
272 }
273
274
275 /*
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.
278  */
279 bool
280 lc_collate_is_c(void)
281 {
282         /* Cache result so we only have to compute it once */
283         static int      result = -1;
284         char       *localeptr;
285
286         if (result >= 0)
287                 return (bool) result;
288         localeptr = setlocale(LC_COLLATE, NULL);
289         if (!localeptr)
290                 elog(ERROR, "invalid LC_COLLATE setting");
291
292         if (strcmp(localeptr, "C") == 0)
293                 result = true;
294         else if (strcmp(localeptr, "POSIX") == 0)
295                 result = true;
296         else
297                 result = false;
298         return (bool) result;
299 }
300
301
302 /*
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.
305  */
306 bool
307 lc_ctype_is_c(void)
308 {
309         /* Cache result so we only have to compute it once */
310         static int      result = -1;
311         char       *localeptr;
312
313         if (result >= 0)
314                 return (bool) result;
315         localeptr = setlocale(LC_CTYPE, NULL);
316         if (!localeptr)
317                 elog(ERROR, "invalid LC_CTYPE setting");
318
319         if (strcmp(localeptr, "C") == 0)
320                 result = true;
321         else if (strcmp(localeptr, "POSIX") == 0)
322                 result = true;
323         else
324                 result = false;
325         return (bool) result;
326 }
327
328
329 /*
330  * Frees the malloced content of a struct lconv.  (But not the struct
331  * itself.)
332  */
333 static void
334 free_struct_lconv(struct lconv * s)
335 {
336         if (s == NULL)
337                 return;
338
339         if (s->currency_symbol)
340                 free(s->currency_symbol);
341         if (s->decimal_point)
342                 free(s->decimal_point);
343         if (s->grouping)
344                 free(s->grouping);
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);
351         if (s->mon_grouping)
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);
359 }
360
361
362 /*
363  * Return the POSIX lconv struct (contains number/money formatting
364  * information) with locale information for all categories.
365  */
366 struct lconv *
367 PGLC_localeconv(void)
368 {
369         static struct lconv CurrentLocaleConv;
370         struct lconv *extlconv;
371         char       *save_lc_monetary;
372         char       *save_lc_numeric;
373
374         /* Did we do it already? */
375         if (CurrentLocaleConvValid)
376                 return &CurrentLocaleConv;
377
378         free_struct_lconv(&CurrentLocaleConv);
379
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);
385         if (save_lc_numeric)
386                 save_lc_numeric = pstrdup(save_lc_numeric);
387
388         setlocale(LC_MONETARY, locale_monetary);
389         setlocale(LC_NUMERIC, locale_numeric);
390
391         /* Get formatting information */
392         extlconv = localeconv();
393
394         /*
395          * Must copy all values since restoring internal settings may overwrite
396          * localeconv()'s results.
397          */
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;
410
411         /* Try to restore internal settings */
412         if (save_lc_monetary)
413         {
414                 setlocale(LC_MONETARY, save_lc_monetary);
415                 pfree(save_lc_monetary);
416         }
417
418         if (save_lc_numeric)
419         {
420                 setlocale(LC_NUMERIC, save_lc_numeric);
421                 pfree(save_lc_numeric);
422         }
423
424         CurrentLocaleConvValid = true;
425         return &CurrentLocaleConv;
426 }