tin  2.6.1
About: TIN is a threaded NNTP and spool based UseNet newsreader.
  Fossies Dox: tin-2.6.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

langinfo.c
Go to the documentation of this file.
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 */
33
34#if 0
35# include <stdlib.h>
36# include <string.h>
37# include "langinfo.h"
38#else
39# ifndef TIN_H
40# include "tin.h"
41# endif /* !TIN_H */
42#endif /* 0 */
43
44#ifndef NO_LOCALE
45# ifndef HAVE_LANGINFO_CODESET
46# define C_CODESET "US-ASCII" /* Return this as the encoding of the
47 * C/POSIX locale. Could as well one day
48 * become "UTF-8". */
49
50# define digit(x) ((x) >= '0' && (x) <= '9')
51
52static char buf[16];
54const char *
56 nl_item item)
57{
58 char *l, *p;
59
60 if (item != CODESET)
61 return NULL;
62
63 if (((l = getenv("LC_ALL")) && *l) ||
64 ((l = getenv("LC_CTYPE")) && *l) ||
65 ((l = getenv("LANG")) && *l)) {
66 /* check standardized locales */
67 if (!strcmp(l, "C") || !strcmp(l, "POSIX"))
68 return C_CODESET;
69 /* check for encoding name fragment */
70 if (strstr(l, "UTF") || strstr(l, "utf"))
71 return "UTF-8";
72 if ((p = strstr(l, "8859-"))) {
73 memcpy(buf, "ISO-8859-\0\0", 12);
74 p += 5;
75 if (digit(*p)) {
76 buf[9] = *p++;
77 if (digit(*p)) buf[10] = *p++;
78 return buf;
79 }
80 }
81 if (strstr(l, "KOI8-R")) return "KOI8-R";
82 if (strstr(l, "KOI8-U")) return "KOI8-U";
83 if (strstr(l, "620")) return "TIS-620";
84 if (strstr(l, "2312")) return "GB2312";
85 if (strstr(l, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */
86 if (strstr(l, "Big5") || strstr(l, "BIG5")) return "Big5";
87 if (strstr(l, "GBK")) return "GBK"; /* no MIME charset */
88 if (strstr(l, "18030")) return "GB18030"; /* no MIME charset */
89 if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return "Shift_JIS";
90 /* check for conclusive modifier */
91 if (strstr(l, "euro")) return "ISO-8859-15";
92 /* check for language (and perhaps country) codes */
93 if (strstr(l, "zh_TW")) return "Big5";
94 if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
95 if (strstr(l, "zh")) return "GB2312";
96 if (strstr(l, "ja")) return "EUC-JP";
97 if (strstr(l, "ko")) return "EUC-KR";
98 if (strstr(l, "ru")) return "KOI8-R";
99 if (strstr(l, "uk")) return "KOI8-U";
100 if (strstr(l, "pl") || strstr(l, "hr") ||
101 strstr(l, "hu") || strstr(l, "cs") ||
102 strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
103 if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
104 if (strstr(l, "el")) return "ISO-8859-7";
105 if (strstr(l, "he")) return "ISO-8859-8";
106 if (strstr(l, "tr")) return "ISO-8859-9";
107 if (strstr(l, "th")) return "TIS-620"; /* or ISO-8859-11 */
108 if (strstr(l, "lt")) return "ISO-8859-13";
109 if (strstr(l, "cy")) return "ISO-8859-14";
110 if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
111 if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
112 /* Send me further rules if you like, but don't forget that we are
113 * *only* interested in locale naming conventions on platforms
114 * that do not already provide an nl_langinfo(CODESET) implementation. */
115 return "ISO-8859-1"; /* should perhaps be "UTF-8" instead */
116 }
117 return C_CODESET;
118}
119# else
120const char *
122 nl_item item)
123{
124 return nl_langinfo(item);
125}
126# endif /* !HAVE_LANGINFO_CODESET */
127#else
128static void no_langinfo(void);
129static void
130no_langinfo( /* ANSI C requires non-empty source file */
131 void)
132{
133}
134#endif /* !NO_LOCALE */
const char * tin_nl_langinfo(nl_item item)
Definition: langinfo.c:53
#define digit(x)
Definition: langinfo.c:48
static char buf[16]
Definition: langinfo.c:50
#define C_CODESET
Definition: langinfo.c:46
char * strstr(const char *text, const char *pattern)
Definition: string.c:334
int nl_item
Definition: tin.h:773
#define CODESET
Definition: tin.h:776