Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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