2 * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3 * Copyright (C) 2004-2008 Kim Woelders
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies of the Software, its documentation and marketing & publicity
14 * materials, and acknowledgment shall be given in the documentation, materials
15 * and software packages that this Software was used.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #if HAVE_LANGINFO_CODESET
38 #define bindtextdomain(pkg,locale)
39 #define textdomain(pkg)
40 #define bind_textdomain_codeset(pkg,enc)
46 #define BAD_CD ((iconv_t)-1)
48 #ifndef ICONV_CONST /* Not sure this is necessary */
52 static iconv_t iconv_cd_int2utf8 = BAD_CD;
53 static iconv_t iconv_cd_utf82int = BAD_CD;
54 static iconv_t iconv_cd_int2loc = BAD_CD;
55 static iconv_t iconv_cd_loc2int = BAD_CD;
58 Eiconv(iconv_t icd, const char *txt, size_t len)
65 pi = (ICONV_CONST char *)txt;
67 ni = (len > 0) ? len : strlen(txt);
69 return Estrndup(txt, ni);
71 err = iconv(icd, &pi, &ni, &po, &no);
73 po = Estrndup(buf, sizeof(buf) - no);
79 EiconvOpen(const char *to, const char *from)
83 icd = iconv_open(to, from);
85 Eprintf("*** WARNING - Missing conversion %s->%s\n", from, to);
93 /* Convert locale to internal format (alloc always) */
95 EstrLoc2Int(const char *str, int len)
101 if (iconv_cd_loc2int != BAD_CD)
102 return Eiconv(iconv_cd_loc2int, str, len);
107 return Estrndup(str, len);
111 /* Convert UTF-8 to internal format (alloc always) */
113 EstrUtf82Int(const char *str, int len)
119 if (iconv_cd_utf82int != BAD_CD)
120 return Eiconv(iconv_cd_utf82int, str, len);
125 return Estrndup(str, len);
128 /* Convert internal to required (alloc only if necessary) */
130 EstrInt2Enc(const char *str, int want_utf8)
133 if (Mode.locale.utf8_int == want_utf8)
140 return Eiconv(iconv_cd_int2utf8, str, strlen(str));
142 return Eiconv(iconv_cd_int2loc, str, strlen(str));
149 /* Free string returned by EstrInt2Enc() */
151 EstrInt2EncFree(const char *str, int want_utf8)
154 if (Mode.locale.utf8_int == want_utf8)
165 * Stuff to do mb/utf8 <-> wc conversions.
168 static iconv_t iconv_cd_str2wcs = BAD_CD;
169 static iconv_t iconv_cd_wcs2str = BAD_CD;
181 enc = nl_langinfo(CODESET);
183 #if SIZEOF_WCHAR_T == 4
184 iconv_cd_str2wcs = EiconvOpen("UCS-4", enc);
185 iconv_cd_wcs2str = EiconvOpen(enc, "UCS-4");
187 iconv_cd_str2wcs = EiconvOpen("WCHAR_T", enc);
188 iconv_cd_wcs2str = EiconvOpen(enc, "WCHAR_T");
191 if (iconv_cd_str2wcs != BAD_CD && iconv_cd_wcs2str != BAD_CD)
197 /* NB! This case will not work properly if needed MB encoding is utf8
198 * but locale isn't */
208 if (iconv_cd_str2wcs != BAD_CD)
209 iconv_close(iconv_cd_str2wcs);
210 iconv_cd_str2wcs = BAD_CD;
211 if (iconv_cd_wcs2str != BAD_CD)
212 iconv_close(iconv_cd_wcs2str);
213 iconv_cd_wcs2str = BAD_CD;
218 EwcStrToWcs(const char *str, int len, wchar_t * wcs, int wcl)
221 ICONV_CONST char *pi;
226 pi = (ICONV_CONST char *)str;
233 rc = iconv(iconv_cd_str2wcs, &pi, &ni, &po, &no);
234 if (rc == (size_t) (-1) || no == 0)
236 wcl = (4096 - no) / sizeof(wchar_t);
241 no = wcl * sizeof(wchar_t);
242 rc = iconv(iconv_cd_str2wcs, &pi, &ni, &po, &no);
243 if (rc == (size_t) (-1))
245 return wcl - no / sizeof(wchar_t);
248 return mbstowcs(NULL, str, 0);
250 mbstowcs(wcs, str, wcl);
251 wcs[wcl] = (wchar_t) '\0';
259 EwcWcsToStr(const wchar_t * wcs, int wcl, char *str, int len)
262 ICONV_CONST char *pi;
265 pi = (ICONV_CONST char *)wcs;
266 ni = wcl * sizeof(wchar_t);
268 rc = iconv(iconv_cd_wcs2str, &pi, &ni, &str, &no);
269 if (rc == (size_t) (-1))
276 for (i = 0; i < wcl; i++)
278 if (j + (int)MB_CUR_MAX > len)
280 n = wctomb(str + j, wcs[i]);
304 char *env_lc_messages;
309 LangEnvironmentSetup(const char *locale)
311 /* Precedence: LANGUAGE, LC_ALL, LC_MESSAGES, LANG */
315 Esetenv("LANGUAGE", locale);
316 Esetenv("LC_ALL", locale);
317 Esetenv("LANG", locale);
322 Esetenv("LANGUAGE", locale_data.env_language);
323 Esetenv("LC_ALL", locale_data.env_lc_all);
324 Esetenv("LC_MESSAGES", locale_data.env_lc_messages);
325 Esetenv("LANG", locale_data.env_lang);
330 LangEnvironmentSave(void)
332 if (locale_data.init)
334 locale_data.init = 1;
336 locale_data.env_language = Estrdup(getenv("LANGUAGE"));
337 locale_data.env_lc_all = Estrdup(getenv("LC_ALL"));
338 locale_data.env_lc_messages = Estrdup(getenv("LC_MESSAGES"));
339 locale_data.env_lang = Estrdup(getenv("LANG"));
345 if (Conf_locale.exported)
346 LangEnvironmentSetup(Conf_locale.exported);
347 else if (Conf_locale.internal)
348 LangEnvironmentSetup(NULL);
354 const char *enc_loc, *enc_int;
356 if (!locale_data.init)
357 LangEnvironmentSave();
359 LangEnvironmentSetup(Conf_locale.internal);
361 setlocale(LC_ALL, ""); /* Set up things according to env vars */
363 bindtextdomain(PACKAGE, LOCALEDIR);
366 if (!XSupportsLocale())
367 setlocale(LC_ALL, "C");
368 XSetLocaleModifiers("");
370 /* I dont want any internationalisation of my numeric input & output */
371 setlocale(LC_NUMERIC, "C");
373 /* Get the environment character encoding */
374 #if HAVE_LANGINFO_CODESET
375 enc_loc = nl_langinfo(CODESET);
377 enc_loc = "ISO-8859-1";
380 /* Debug - possibility to set desired internal representation */
381 enc_int = getenv("E_CHARSET");
383 bind_textdomain_codeset(PACKAGE, enc_int);
387 Mode.locale.lang = setlocale(LC_MESSAGES, NULL);
388 if (EDebug(EDBUG_TYPE_VERBOSE))
390 Eprintf("Locale: %s\n", setlocale(LC_ALL, NULL));
391 Eprintf("Character encoding: locale=%s internal=%s MB_CUR_MAX=%zu\n",
392 enc_loc, enc_int, MB_CUR_MAX);
395 if (!Estrcasecmp(enc_loc, "utf8") || !Estrcasecmp(enc_loc, "utf-8"))
396 Mode.locale.utf8_loc = 1;
397 if (!Estrcasecmp(enc_int, "utf8") || !Estrcasecmp(enc_int, "utf-8"))
398 Mode.locale.utf8_int = 1;
401 if (Mode.locale.utf8_int && Mode.locale.utf8_loc)
403 if (Mode.locale.utf8_int)
405 iconv_cd_loc2int = EiconvOpen("UTF-8", enc_loc);
406 iconv_cd_int2loc = EiconvOpen(enc_loc, "UTF-8");
410 iconv_cd_utf82int = EiconvOpen(enc_loc, "UTF-8");
411 iconv_cd_int2utf8 = EiconvOpen("UTF-8", enc_loc);
420 if (iconv_cd_int2utf8 != BAD_CD)
421 iconv_close(iconv_cd_int2utf8);
422 if (iconv_cd_utf82int != BAD_CD)
423 iconv_close(iconv_cd_utf82int);
424 if (iconv_cd_int2loc != BAD_CD)
425 iconv_close(iconv_cd_int2loc);
426 if (iconv_cd_loc2int != BAD_CD)
427 iconv_close(iconv_cd_loc2int);
428 iconv_cd_int2utf8 = iconv_cd_utf82int = BAD_CD;
429 iconv_cd_int2loc = iconv_cd_loc2int = BAD_CD;
432 LangEnvironmentSetup(NULL);
436 LangCfgChange(void *item __UNUSED__, const char *locale)
441 _EFDUP(Conf_locale.internal, locale);
445 static const CfgItem LocaleCfgItems[] = {
446 CFG_FUNC_STR(Conf_locale, internal, LangCfgChange),
447 CFG_ITEM_STR(Conf_locale, exported),
449 #define N_CFG_ITEMS (sizeof(LocaleCfgItems)/sizeof(CfgItem))
451 extern const EModule ModLocale;
452 const EModule ModLocale = {
456 {N_CFG_ITEMS, LocaleCfgItems}