1 /*
2 * This is a quick-and-dirty emulator of the nl_langinfo(CODESET)
3 * function defined in the Single Unix Specification for those systems
4 * (FreeBSD, etc.) that don't have one yet. It behaves as if it had
5 * been called after setlocale(LC_CTYPE, ""), that is it looks at
6 * the locale environment variables.
7 *
8 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
9 *
10 * Please extend it as needed and suggest improvements to the author.
11 * This emulator will hopefully become redundant soon as
12 * nl_langinfo(CODESET) becomes more widely implemented.
13 *
14 * Since the proposed Li18nux encoding name registry is still not mature,
15 * the output follows the MIME registry where possible:
16 *
17 * http://www.iana.org/assignments/character-sets
18 *
19 * A possible autoconf test for the availability of nl_langinfo(CODESET)
20 * can be found in
21 *
22 * http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate
23 *
24 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
25 * Permission to use, copy, modify, and distribute this software
26 * for any purpose and without fee is hereby granted. The author
27 * disclaims all warranties with regard to this software.
28 *
29 * Latest version:
30 *
31 * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c
32 */
34 #include <stdlib.h>
35 #include <string.h>
36 #include "support.h"
38 #define C_CODESET "US-ASCII" /* Return this as the encoding of the
39 * C/POSIX locale. Could as well one day
40 * become "UTF-8". */
42 #define digit(x) ((x) >= '0' && (x) <= '9')
44 static char buf[16];
46 char *nl_langinfo(nl_item item)
47 {
48 char *l, *p;
50 if (item != CODESET)
51 return NULL;
53 if (((l = getenv("LC_ALL")) && *l) ||
54 ((l = getenv("LC_CTYPE")) && *l) ||
55 ((l = getenv("LANG")) && *l)) {
56 /* check standardized locales */
57 if (!strcmp(l, "C") || !strcmp(l, "POSIX"))
58 return C_CODESET;
59 /* check for encoding name fragment */
60 if (strstr(l, "UTF") || strstr(l, "utf"))
61 return "UTF-8";
62 if ((p = strstr(l, "8859-"))) {
63 memcpy(buf, "ISO-8859-\0\0", 12);
64 p += 5;
65 if (digit(*p)) {
66 buf[9] = *p++;
67 if (digit(*p)) buf[10] = *p++;
68 return buf;
69 }
70 }
71 if (strstr(l, "KOI8-R")) return "KOI8-R";
72 if (strstr(l, "KOI8-U")) return "KOI8-U";
73 if (strstr(l, "620")) return "TIS-620";
74 if (strstr(l, "2312")) return "GB2312";
75 if (strstr(l, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */
76 if (strstr(l, "Big5") || strstr(l, "BIG5")) return "Big5";
77 if (strstr(l, "GBK")) return "GBK"; /* no MIME charset */
78 if (strstr(l, "18030")) return "GB18030"; /* no MIME charset */
79 if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return "Shift_JIS";
80 /* check for conclusive modifier */
81 if (strstr(l, "euro")) return "ISO-8859-15";
82 /* check for language (and perhaps country) codes */
83 if (strstr(l, "zh_TW")) return "Big5";
84 if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
85 if (strstr(l, "zh")) return "GB2312";
86 if (strstr(l, "ja")) return "EUC-JP";
87 if (strstr(l, "ko")) return "EUC-KR";
88 if (strstr(l, "ru")) return "KOI8-R";
89 if (strstr(l, "uk")) return "KOI8-U";
90 if (strstr(l, "pl") || strstr(l, "hr") ||
91 strstr(l, "hu") || strstr(l, "cs") ||
92 strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
93 if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
94 if (strstr(l, "el")) return "ISO-8859-7";
95 if (strstr(l, "he")) return "ISO-8859-8";
96 if (strstr(l, "tr")) return "ISO-8859-9";
97 if (strstr(l, "th")) return "TIS-620"; /* or ISO-8859-11 */
98 if (strstr(l, "lt")) return "ISO-8859-13";
99 if (strstr(l, "cy")) return "ISO-8859-14";
100 if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
101 if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
102 /* Send me further rules if you like, but don't forget that we are
103 * *only* interested in locale naming conventions on platforms
104 * that do not already provide an nl_langinfo(CODESET) implementation. */
105 return "ISO-8859-1"; /* should perhaps be "UTF-8" instead */
106 }
107 return C_CODESET;
108 }
110 /* For a demo, compile with "gcc -W -Wall -o langinfo -D TEST langinfo.c" */
112 #ifdef TEST
113 #include <stdio.h>
114 int main()
115 {
116 printf("%s\n", nl_langinfo(CODESET));
117 return 0;
118 }
119 #endif