]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
Escape literal leading full stop in man/new.man.
[nmh] / sbr / check_charset.c
1
2 /*
3 * check_charset.c -- routines for character sets
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11
12 #include <langinfo.h>
13
14 /* Only get_charset() should use norm_charmap(), so hide its
15 declaration here. */
16 char *norm_charmap(char *);
17
18 /*
19 * Get the current character set
20 */
21 char *
22 get_charset ()
23 {
24 return norm_charmap(nl_langinfo (CODESET));
25 }
26
27
28 /*
29 * Check if we can display a given character set natively.
30 * We are passed the length of the initial part of the
31 * string to check, since we want to allow the name of the
32 * character set to be a substring of a larger string.
33 */
34
35 int
36 check_charset (char *str, int len)
37 {
38 static char *mm_charset = NULL;
39 static char *alt_charset = NULL;
40 static int mm_len;
41 static int alt_len;
42
43 /* Cache the name of our default character set */
44 if (!mm_charset) {
45 if (!(mm_charset = get_charset ()))
46 mm_charset = "US-ASCII";
47 mm_len = strlen (mm_charset);
48
49 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
50 if (!strncasecmp("ISO-8859-", mm_charset, 9) ||
51 !strcasecmp("UTF-8", mm_charset)) {
52 alt_charset = "US-ASCII";
53 alt_len = strlen (alt_charset);
54 }
55 }
56
57 /* Check if character set is OK */
58 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
59 return 1;
60 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
61 return 1;
62
63 return 0;
64 }
65
66
67 /*
68 * Return the name of the character set we are
69 * using for 8bit text.
70 */
71 char *
72 write_charset_8bit (void)
73 {
74 static char *mm_charset = NULL;
75
76 /*
77 * Cache the name of the character set to
78 * use for 8bit text.
79 */
80 if (!mm_charset && !(mm_charset = get_charset ()))
81 mm_charset = "x-unknown";
82
83 return mm_charset;
84 }