]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
Improve fmttest output
[nmh] / sbr / check_charset.c
1 /* check_charset.c -- routines for character sets
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "check_charset.h"
10
11 #include <string.h>
12 #include <langinfo.h>
13
14 static const char *norm_charmap(char *name);
15
16 /*
17 * Get the current character set
18 */
19 char *
20 get_charset(void)
21 {
22 return (char *)norm_charmap(nl_langinfo(CODESET));
23 }
24
25
26 /*
27 * Check if we can display a given character set natively.
28 * We are passed the length of the initial part of the
29 * string to check, since we want to allow the name of the
30 * character set to be a substring of a larger string.
31 */
32
33 int
34 check_charset (char *str, int len)
35 {
36 static char *mm_charset = NULL;
37 static char *alt_charset = NULL;
38 static int mm_len;
39 static int alt_len;
40
41 /* Cache the name of our default character set */
42 if (!mm_charset) {
43 if (!(mm_charset = get_charset ()))
44 mm_charset = "US-ASCII";
45 mm_len = strlen (mm_charset);
46
47 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
48 if (!strncasecmp("ISO-8859-", mm_charset, 9) ||
49 !strcasecmp("UTF-8", mm_charset)) {
50 alt_charset = "US-ASCII";
51 alt_len = strlen (alt_charset);
52 }
53 }
54
55 /* Check if character set is OK */
56 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
57 return 1;
58 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
59 return 1;
60
61 return 0;
62 }
63
64
65 /*
66 * Return the name of the character set we are
67 * using for 8bit text.
68 */
69 char *
70 write_charset_8bit (void)
71 {
72 static char *mm_charset = NULL;
73
74 /*
75 * Cache the name of the character set to
76 * use for 8bit text.
77 */
78 if (!mm_charset && !(mm_charset = get_charset ()))
79 mm_charset = "x-unknown";
80
81 return mm_charset;
82 }
83
84 /* The Single Unix Specification function nl_langinfo(CODESET)
85 * returns the name of the encoding used by the currently selected
86 * locale:
87 *
88 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
89 *
90 * Unfortunately the encoding names are not yet standardized.
91 * This function knows about the encoding names used on many
92 * different systems and converts them where possible into
93 * the corresponding MIME charset name registered in
94 *
95 * http://www.iana.org/assignments/character-sets
96 *
97 * Please extend it as needed and suggest improvements to the author.
98 *
99 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
100 * Permission to use, copy, modify, and distribute this software
101 * for any purpose and without fee is hereby granted. The author
102 * disclaims all warranties with regard to this software.
103 *
104 * Latest version:
105 *
106 * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
107 */
108
109 static const char *
110 norm_charmap(char *name)
111 {
112 static const char *correct[] = {
113 "UTF-8",
114 "US-ASCII",
115 NULL
116 }, **cor;
117 static struct {
118 const char *alias;
119 const char *name;
120 } *ali, aliases[] = {
121 /* Names for US-ASCII. */
122 { "ANSI_X3.4-1968", "US-ASCII" }, /* LC_ALL=C. */
123 { "ASCII", "US-ASCII" },
124 { "646", "US-ASCII" },
125 { "ISO646", "US-ASCII" },
126 { "ISO_646.IRV", "US-ASCII" },
127 /* Case differs. */
128 { "BIG5", "Big5" },
129 { "BIG5HKSCS", "Big5HKSCS" },
130 /* Names for ISO-8859-11. */
131 { "TIS-620", "ISO-8859-11" },
132 { "TIS620.2533", "ISO-8859-11" },
133 { NULL, NULL }
134 };
135 static struct {
136 const char *substr;
137 const char *name;
138 } *sub, substrs[] = {
139 { "8859-1", "ISO-8859-1" },
140 { "8859-2", "ISO-8859-2" },
141 { "8859-3", "ISO-8859-3" },
142 { "8859-4", "ISO-8859-4" },
143 { "8859-5", "ISO-8859-5" },
144 { "8859-6", "ISO-8859-6" },
145 { "8859-7", "ISO-8859-7" },
146 { "8859-8", "ISO-8859-8" },
147 { "8859-9", "ISO-8859-9" },
148 { "8859-10", "ISO-8859-10" },
149 { "8859-11", "ISO-8859-11" },
150 /* 12, Latin/Devanagari, not completed. */
151 { "8859-13", "ISO-8859-13" },
152 { "8859-14", "ISO-8859-14" },
153 { "8859-15", "ISO-8859-15" },
154 { "8859-16", "ISO-8859-16" },
155 { "CP1200", "WINDOWS-1200" },
156 { "CP1201", "WINDOWS-1201" },
157 { "CP1250", "WINDOWS-1250" },
158 { "CP1251", "WINDOWS-1251" },
159 { "CP1252", "WINDOWS-1252" },
160 { "CP1253", "WINDOWS-1253" },
161 { "CP1254", "WINDOWS-1254" },
162 { "CP1255", "WINDOWS-1255" },
163 { "CP1256", "WINDOWS-1256" },
164 { "CP1257", "WINDOWS-1257" },
165 { "CP1258", "WINDOWS-1258" },
166 { NULL, NULL }
167 };
168
169 if (!name)
170 return name;
171
172 /* Avoid lots of tests for common correct names. */
173 for (cor = correct; *cor; cor++)
174 if (!strcmp(name, *cor))
175 return name;
176
177 for (ali = aliases; ali->alias; ali++)
178 if (!strcmp(name, ali->alias))
179 return ali->name;
180
181 for (sub = substrs; sub->substr; sub++)
182 if (strstr(name, sub->substr))
183 return sub->name;
184
185 return name;
186 }