simplify whatnow.c/main() function
[mmh] / sbr / charset.c
1 /*
2 ** charset.c -- routines for character sets
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #ifdef HAVE_LANGINFO_H
11 # include <langinfo.h>
12 #endif
13
14
15 /*
16 ** Get the current character set
17 */
18 char *
19 get_charset()
20 {
21         char *charset = getenv("MM_CHARSET");
22 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
23         if (!charset) {
24                 charset = norm_charmap(nl_langinfo(CODESET));
25         }
26 #endif
27         return charset;
28 }
29
30
31 /*
32 ** Check if we can display a given character set natively.
33 */
34 int
35 is_native_charset(char *str)
36 {
37         char *mm_charset = NULL;
38
39         if (!(mm_charset = get_charset())) {
40                 mm_charset = "US-ASCII";
41         }
42         if (mh_strcasecmp(str, mm_charset)==0) {
43                 return 1;
44         }
45
46         /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
47         if (strncasecmp("ISO-8859-", mm_charset, 9)==0 ||
48                         mh_strcasecmp("UTF-8", mm_charset)==0) {
49                 if (mh_strcasecmp(str, "US-ASCII")==0) {
50                         return 1;
51                 }
52         }
53         return 0;
54 }
55
56
57 /*
58 ** Return the name of the character set we are
59 ** using for 8bit text.
60 */
61 char *
62 write_charset_8bit(void)
63 {
64         char *mm_charset = NULL;
65
66         if (!(mm_charset = get_charset())) {
67                 mm_charset = "x-unknown";
68         }
69         return mm_charset;
70 }