Debian Codegraph @

nx-X11/lib/src/xkb/XKBCvt.c

raw
1 /*
2
3 Copyright 1988, 1989, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from The Open Group.
26
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <nx-X11/X.h>
37 #include <nx-X11/Xlib.h>
38 #include "Xlibint.h"
39 #include "Xlcint.h"
40 #include "XlcPubI.h"
41 #include "Ximint.h"
42 #include <nx-X11/Xutil.h>
43 #include <nx-X11/Xmd.h>
44 #define XK_LATIN1
45 #define XK_PUBLISHING
46 #include <nx-X11/keysym.h>
47 #include <nx-X11/extensions/XKBproto.h>
48 #include "XKBlibint.h"
49 #include <nx-X11/Xlocale.h>
50 #include <ctype.h>
51 #include <nx-X11/Xos.h>
52
53 static int
54 _XkbHandleSpecialSym(KeySym keysym, char *buffer, int nbytes, int *extra_rtrn)
55 {
56
57 /* try to convert to Latin-1, handling ctrl */
58 if (!(((keysym >= XK_BackSpace) && (keysym <= XK_Clear)) ||
59 (keysym == XK_Return) || (keysym == XK_Escape) ||
60 (keysym == XK_KP_Space) || (keysym == XK_KP_Tab) ||
61 (keysym == XK_KP_Enter) ||
62 ((keysym >= XK_KP_Multiply) && (keysym <= XK_KP_9)) ||
63 (keysym == XK_KP_Equal) || (keysym == XK_Delete)))
64 return 0;
65
66 if (nbytes < 1) {
67 if (extra_rtrn)
68 *extra_rtrn = 1;
69 return 0;
70 }
71 /* if X keysym, convert to ascii by grabbing low 7 bits */
72 if (keysym == XK_KP_Space)
73 buffer[0] = XK_space & 0x7F; /* patch encoding botch */
74 else if (keysym == XK_hyphen)
75 buffer[0] = (char) (XK_minus & 0xFF); /* map to equiv character */
76 else
77 buffer[0] = (char) (keysym & 0x7F);
78 return 1;
79 }
80
81 /*ARGSUSED*/
82 static int
83 _XkbKSToKnownSet(XPointer priv,
84 KeySym keysym,
85 char *buffer,
86 int nbytes,
87 int *extra_rtrn)
88 {
89 char tbuf[8], *buf;
90
91 if (extra_rtrn)
92 *extra_rtrn = 0;
93
94 /* convert "dead" diacriticals for dumb applications */
95 if ((keysym & 0xffffff00) == 0xfe00) {
96 switch (keysym) {
97 case XK_dead_grave: keysym = XK_grave; break;
98 case XK_dead_acute: keysym = XK_acute; break;
99 case XK_dead_circumflex: keysym = XK_asciicircum; break;
100 case XK_dead_tilde: keysym = XK_asciitilde; break;
101 case XK_dead_macron: keysym = XK_macron; break;
102 case XK_dead_breve: keysym = XK_breve; break;
103 case XK_dead_abovedot: keysym = XK_abovedot; break;
104 case XK_dead_diaeresis: keysym = XK_diaeresis; break;
105 case XK_dead_abovering: keysym = XK_degree; break;
106 case XK_dead_doubleacute: keysym = XK_doubleacute; break;
107 case XK_dead_caron: keysym = XK_caron; break;
108 case XK_dead_cedilla: keysym = XK_cedilla; break;
109 case XK_dead_ogonek: keysym = XK_ogonek; break;
110 case XK_dead_iota: keysym = XK_Greek_iota; break;
111 #ifdef XK_KATAKANA
112 case XK_dead_voiced_sound: keysym = XK_voicedsound; break;
113 case XK_dead_semivoiced_sound: keysym = XK_semivoicedsound; break;
114 #endif
115 }
116 }
117
118 if (nbytes < 1)
119 buf = tbuf;
120 else
121 buf = buffer;
122
123 if ((keysym & 0xffffff00) == 0xff00) {
124 return _XkbHandleSpecialSym(keysym, buf, nbytes, extra_rtrn);
125 }
126 return _XimGetCharCode(priv, keysym, (unsigned char *) buf, nbytes);
127 }
128
129 typedef struct _XkbToKS {
130 unsigned prefix;
131 char *map;
132 } XkbToKS;
133
134 /*ARGSUSED*/
135 static KeySym
136 _XkbKnownSetToKS(XPointer priv, char *buffer, int nbytes, Status *status)
137 {
138 if (nbytes != 1)
139 return NoSymbol;
140 if (((buffer[0] & 0x80) == 0) && (buffer[0] >= 32))
141 return buffer[0];
142 else if ((buffer[0] & 0x7f) >= 32) {
143 XkbToKS *map = (XkbToKS *) priv;
144
145 if (map) {
146 if (map->map)
147 return map->prefix | map->map[buffer[0] & 0x7f];
148 else
149 return map->prefix | buffer[0];
150 }
151 return buffer[0];
152 }
153 return NoSymbol;
154 }
155
156 static KeySym
157 __XkbDefaultToUpper(KeySym sym)
158 {
159 KeySym lower, upper;
160
161 XConvertCase(sym, &lower, &upper);
162 return upper;
163 }
164
165 #ifdef XKB_EXTEND_LOOKUP_STRING
166 static int
167 Strcmp(char *str1, char *str2)
168 {
169 char str[256];
170 char c, *s;
171
172 /*
173 * unchecked strings from the environment can end up here, so check
174 * the length before copying.
175 */
176 if (strlen(str1) >= sizeof(str)) /* almost certain it's a mismatch */
177 return 1;
178
179 for (s = str; (c = *str1++);) {
180 if (isupper(c))
181 c = tolower(c);
182 *s++ = c;
183 }
184 *s = '\0';
185 return (strcmp(str, str2));
186 }
187 #endif
188
189 int
190 _XkbGetConverters(const char *encoding_name, XkbConverters * cvt_rtrn)
191 {
192 if (!cvt_rtrn)
193 return 0;
194
195 cvt_rtrn->KSToMB = _XkbKSToKnownSet;
196 cvt_rtrn->KSToMBPriv = _XimGetLocaleCode(encoding_name);
197 cvt_rtrn->MBToKS = _XkbKnownSetToKS;
198 cvt_rtrn->MBToKSPriv = NULL;
199 cvt_rtrn->KSToUpper = __XkbDefaultToUpper;
200 return 1;
201 }
202
203 /***====================================================================***/
204
205 /*
206 * The function _XkbGetCharset seems to be missnamed as what it seems to
207 * be used for is to determine the encoding-name for the locale. ???
208 */
209
210 #ifdef XKB_EXTEND_LOOKUP_STRING
211
212 /*
213 * XKB_EXTEND_LOOKUP_STRING is not used by the SI. It is used by various
214 * X Consortium/X Project Team members, so we leave it in the source as
215 * an simplify integration by these companies.
216 */
217
218 #define CHARSET_FILE "/usr/lib/X11/input/charsets"
219 static char *_XkbKnownLanguages =
220 "c=ascii:da,de,en,es,fr,is,it,nl,no,pt,sv=iso8859-1:hu,pl,cs=iso8859-2:"
221 "eo=iso8859-3:sp=iso8859-5:ar,ara=iso8859-6:el=iso8859-7:he=iso8859-8:"
222 "tr=iso8859-9:lt,lv=iso8859-13:et,fi=iso8859-15:ru=koi8-r:uk=koi8-u:"
223 "th,th_TH,th_TH.iso8859-11=iso8859-11:th_TH.TIS620=tis620:hy=armscii-8:"
224 "vi=tcvn-5712:ka=georgian-academy:be,bg=microsoft-cp1251";
225
226 char *
227 _XkbGetCharset(void)
228 {
229 /*
230 * PAGE USAGE TUNING: explicitly initialize to move these to data
231 * instead of bss
232 */
233 static char buf[100] = { 0 };
234 char lang[256];
235 char *start, *tmp, *end, *next, *set;
236 char *country, *charset;
237 char *locale;
238
239 tmp = getenv("_XKB_CHARSET");
240 if (tmp)
241 return tmp;
242 locale = setlocale(LC_CTYPE, NULL);
243
244 if (locale == NULL)
245 return NULL;
246
247 if (strlen(locale) >= sizeof(lang))
248 return NULL;
249
250 for (tmp = lang; *tmp = *locale++; tmp++) {
251 if (isupper(*tmp))
252 *tmp = tolower(*tmp);
253 }
254 country = strchr(lang, '_');
255 if (country) {
256 *country++ = '\0';
257 charset = strchr(country, '.');
258 if (charset)
259 *charset++ = '\0';
260 if (charset) {
261 strncpy(buf, charset, 99);
262 buf[99] = '\0';
263 return buf;
264 }
265 }
266 else {
267 charset = NULL;
268 }
269
270 if ((tmp = getenv("_XKB_LOCALE_CHARSETS")) != NULL) {
271 start = _XkbAlloc(strlen(tmp) + 1);
272 strcpy(start, tmp);
273 tmp = start;
274 }
275 else {
276 struct stat sbuf;
277 FILE *file;
278 char *cf = CHARSET_FILE;
279
280 #ifndef S_ISREG
281 # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
282 #endif
283
284 if ((stat(cf, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
285 (file = fopen(cf, "r"))) {
286 tmp = _XkbAlloc(sbuf.st_size + 1);
287 if (tmp != NULL) {
288 sbuf.st_size = (long) fread(tmp, 1, sbuf.st_size, file);
289 tmp[sbuf.st_size] = '\0';
290 }
291 fclose(file);
292 }
293 }
294
295 if (tmp == NULL) {
296 tmp = _XkbAlloc(strlen(_XkbKnownLanguages) + 1);
297 if (!tmp)
298 return NULL;
299 strcpy(tmp, _XkbKnownLanguages);
300 }
301 start = tmp;
302 do {
303 if ((set = strchr(tmp, '=')) == NULL)
304 break;
305 *set++ = '\0';
306 if ((next = strchr(set, ':')) != NULL)
307 *next++ = '\0';
308 while (tmp && *tmp) {
309 if ((end = strchr(tmp, ',')) != NULL)
310 *end++ = '\0';
311 if (Strcmp(tmp, lang) == 0) {
312 strncpy(buf, set, 100);
313 buf[99] = '\0';
314 Xfree(start);
315 return buf;
316 }
317 tmp = end;
318 }
319 tmp = next;
320 } while (tmp && *tmp);
321 Xfree(start);
322 return NULL;
323 }
324 #else
325 char *
326 _XkbGetCharset(void)
327 {
328 char *tmp;
329 XLCd lcd;
330
331 tmp = getenv("_XKB_CHARSET");
332 if (tmp)
333 return tmp;
334
335 lcd = _XlcCurrentLC();
336 if (lcd)
337 return XLC_PUBLIC(lcd, encoding_name);
338
339 return NULL;
340 }
341 #endif