]> granicus.if.org Git - postgresql/blob - src/port/chklocale.c
On OS X, assume that an empty-string result for nl_langinfo(CODESET)
[postgresql] / src / port / chklocale.c
1 /*-------------------------------------------------------------------------
2  *
3  * chklocale.c
4  *              Functions for handling locale-related info
5  *
6  *
7  * Copyright (c) 1996-2007, PostgreSQL Global Development Group
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/port/chklocale.c,v 1.2 2007/09/28 23:36:06 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
21
22 #include <locale.h>
23 #ifdef HAVE_LANGINFO_H
24 #include <langinfo.h>
25 #endif
26
27 #include "mb/pg_wchar.h"
28
29
30 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
31
32 /*
33  * This table needs to recognize all the CODESET spellings for supported
34  * backend encodings.  We don't need to handle frontend-only encodings.
35  * Note that we search the table with pg_strcasecmp(), so variant
36  * capitalizations don't need their own entries.
37  */
38 struct encoding_match
39 {
40         enum pg_enc pg_enc_code;
41         const char *system_enc_name;
42 };
43
44 static const struct encoding_match encoding_match_list[] = {
45         {PG_EUC_JP, "EUC-JP"},
46         {PG_EUC_JP, "eucJP"},
47         {PG_EUC_JP, "IBM-eucJP"},
48         {PG_EUC_JP, "sdeckanji"},
49
50         {PG_EUC_CN, "EUC-CN"},
51         {PG_EUC_CN, "eucCN"},
52         {PG_EUC_CN, "IBM-eucCN"},
53         {PG_EUC_CN, "GB2312"},
54         {PG_EUC_CN, "dechanzi"},
55
56         {PG_EUC_KR, "EUC-KR"},
57         {PG_EUC_KR, "eucKR"},
58         {PG_EUC_KR, "IBM-eucKR"},
59         {PG_EUC_KR, "deckorean"},
60         {PG_EUC_KR, "5601"},
61
62         {PG_EUC_TW, "EUC-TW"},
63         {PG_EUC_TW, "eucTW"},
64         {PG_EUC_TW, "IBM-eucTW"},
65         {PG_EUC_TW, "cns11643"},
66
67         {PG_UTF8, "UTF-8"},
68         {PG_UTF8, "utf8"},
69
70         {PG_LATIN1, "ISO-8859-1"},
71         {PG_LATIN1, "ISO8859-1"},
72         {PG_LATIN1, "iso88591"},
73
74         {PG_LATIN2, "ISO-8859-2"},
75         {PG_LATIN2, "ISO8859-2"},
76         {PG_LATIN2, "iso88592"},
77
78         {PG_LATIN3, "ISO-8859-3"},
79         {PG_LATIN3, "ISO8859-3"},
80         {PG_LATIN3, "iso88593"},
81
82         {PG_LATIN4, "ISO-8859-4"},
83         {PG_LATIN4, "ISO8859-4"},
84         {PG_LATIN4, "iso88594"},
85
86         {PG_LATIN5, "ISO-8859-9"},
87         {PG_LATIN5, "ISO8859-9"},
88         {PG_LATIN5, "iso88599"},
89
90         {PG_LATIN6, "ISO-8859-10"},
91         {PG_LATIN6, "ISO8859-10"},
92         {PG_LATIN6, "iso885910"},
93
94         {PG_LATIN7, "ISO-8859-13"},
95         {PG_LATIN7, "ISO8859-13"},
96         {PG_LATIN7, "iso885913"},
97
98         {PG_LATIN8, "ISO-8859-14"},
99         {PG_LATIN8, "ISO8859-14"},
100         {PG_LATIN8, "iso885914"},
101
102         {PG_LATIN9, "ISO-8859-15"},
103         {PG_LATIN9, "ISO8859-15"},
104         {PG_LATIN9, "iso885915"},
105
106         {PG_LATIN10, "ISO-8859-16"},
107         {PG_LATIN10, "ISO8859-16"},
108         {PG_LATIN10, "iso885916"},
109
110         {PG_KOI8R, "KOI8-R"},
111
112         {PG_WIN1252, "CP1252"},
113         {PG_WIN1253, "CP1253"},
114         {PG_WIN1254, "CP1254"},
115         {PG_WIN1255, "CP1255"},
116         {PG_WIN1256, "CP1256"},
117         {PG_WIN1257, "CP1257"},
118         {PG_WIN1258, "CP1258"},
119 #ifdef NOT_VERIFIED
120         {PG_WIN874, "???"},
121 #endif
122         {PG_WIN1251, "CP1251"},
123         {PG_WIN866, "CP866"},
124
125         {PG_ISO_8859_5, "ISO-8859-5"},
126         {PG_ISO_8859_5, "ISO8859-5"},
127         {PG_ISO_8859_5, "iso88595"},
128
129         {PG_ISO_8859_6, "ISO-8859-6"},
130         {PG_ISO_8859_6, "ISO8859-6"},
131         {PG_ISO_8859_6, "iso88596"},
132
133         {PG_ISO_8859_7, "ISO-8859-7"},
134         {PG_ISO_8859_7, "ISO8859-7"},
135         {PG_ISO_8859_7, "iso88597"},
136
137         {PG_ISO_8859_8, "ISO-8859-8"},
138         {PG_ISO_8859_8, "ISO8859-8"},
139         {PG_ISO_8859_8, "iso88598"},
140
141         {PG_SQL_ASCII, NULL}            /* end marker */
142 };
143
144
145 /*
146  * Given a setting for LC_CTYPE, return the Postgres ID of the associated
147  * encoding, if we can determine it.
148  *
149  * Pass in NULL to get the encoding for the current locale setting.
150  *
151  * If the result is PG_SQL_ASCII, callers should treat it as being compatible
152  * with any desired encoding.  We return this if the locale is C/POSIX or we
153  * can't determine the encoding.
154  */
155 int
156 pg_get_encoding_from_locale(const char *ctype)
157 {
158         char       *sys;
159         int                     i;
160
161         /* Get the CODESET property, and also LC_CTYPE if not passed in */
162         if (ctype)
163         {
164                 char       *save;
165
166                 save = setlocale(LC_CTYPE, NULL);
167                 if (!save)
168                         return PG_SQL_ASCII;            /* setlocale() broken? */
169                 /* must copy result, or it might change after setlocale */
170                 save = strdup(save);
171                 if (!save)
172                         return PG_SQL_ASCII;            /* out of memory; unlikely */
173
174                 if (!setlocale(LC_CTYPE, ctype))
175                 {
176                         free(save);
177                         return PG_SQL_ASCII;            /* bogus ctype passed in? */
178                 }
179
180                 sys = nl_langinfo(CODESET);
181                 if (sys)
182                         sys = strdup(sys);
183
184                 setlocale(LC_CTYPE, save);
185                 free(save);
186         }
187         else
188         {
189                 /* much easier... */
190                 ctype = setlocale(LC_CTYPE, NULL);
191                 if (!ctype)
192                         return PG_SQL_ASCII;            /* setlocale() broken? */
193                 sys = nl_langinfo(CODESET);
194                 if (sys)
195                         sys = strdup(sys);
196         }
197
198         if (!sys)
199                 return PG_SQL_ASCII;            /* out of memory; unlikely */
200
201         /* If locale is C or POSIX, we can allow all encodings */
202         if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
203         {
204                 free(sys);
205                 return PG_SQL_ASCII;
206         }
207
208         /* Check the table */
209         for (i = 0; encoding_match_list[i].system_enc_name; i++)
210         {
211                 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
212                 {
213                         free(sys);
214                         return encoding_match_list[i].pg_enc_code;
215                 }
216         }
217
218         /* Special-case kluges for particular platforms go here */
219
220 #ifdef __darwin__
221         /*
222          * Current OS X has many locales that report an empty string for CODESET,
223          * but they all seem to actually use UTF-8.
224          */
225         if (strlen(sys) == 0)
226         {
227                 free(sys);
228                 return PG_UTF8;
229         }
230 #endif
231
232         /*
233          * We print a warning if we got a CODESET string but couldn't recognize
234          * it.  This means we need another entry in the table.
235          */
236 #ifdef FRONTEND
237         fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
238                         ctype, sys);
239         /* keep newline separate so there's only one translatable string */
240         fputc('\n', stderr);
241 #else
242         ereport(WARNING,
243                         (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
244                                         ctype, sys),
245                          errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
246 #endif
247
248         free(sys);
249         return PG_SQL_ASCII;
250 }
251
252 #else /* !(HAVE_LANGINFO_H && CODESET) */
253
254 /*
255  * stub if no platform support
256  */
257 int
258 pg_get_encoding_from_locale(const char *ctype)
259 {
260         return PG_SQL_ASCII;
261 }
262
263 #endif /* HAVE_LANGINFO_H && CODESET */