1 /*--------------------------------*-C-*---------------------------------*
2 * File: grkelot.c
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1994,1995 Angelo Haritsis
7 * Copyright (c) 1997,1998 Oezger Kesim <kesim@math.fu-berlin.de>
8 * Copyright (c) 2004 Jingmin Zhou <jimmyzhou@users.sourceforge.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *----------------------------------------------------------------------*/
24
25 #include "../config.h"
26 #include "rxvt.h"
27
28
29 #ifdef GREEK_SUPPORT
30
31 /* --- Macros, Types --------- */
32 #define MAX_STATES 4 /* max # states for the FSM */
33 #define MAX_SWITCHER 2U /* per state */
34 #define MAX_VAL 256 /* for temp allocation */
35
36 #ifndef HAVE_SYS_TYPES_H
37 typedef unsigned char u_char;
38 typedef unsigned int u_int;
39 typedef unsigned long u_long;
40 #endif
41
42 typedef struct s_xlat {
43 u_int first, last;
44 u_int *pval; /* array of translated values */
45 } K_XLAT;
46
47 typedef struct s_switch {
48 u_char type; /* Ascii, Virtual, Scan */
49 u_int code;
50 u_char nextstate;
51 u_char on; /* current state of key: 0 = off */
52 } K_SWITCH;
53
54 typedef struct s_state {
55 u_int num_xlat; /* number of translations */
56 K_XLAT *xlat; /* State translations ((dynamic - realloc'ed) */
57 u_int num_switcher; /* number of switcher keys */
58 K_SWITCH switcher[MAX_SWITCHER]; /* switcher keys to other states */
59 u_char life; /* 0 = until switched by key */
60 u_char prev_state; /* filled when jumped to a new state */
61 } K_STATE;
62
63 /* type for each one of the different greek standards (xlat types) */
64 typedef struct s_xlat_type {
65 char *plain;
66 char *accent;
67 char *accent_xtra;
68 char *umlaut;
69 char *acc_uml;
70 } XLAT_TYPE;
71
72 /* --- Local Data ------------ */
73 static K_STATE State[MAX_STATES];
74
75 /* Current State */
76 static u_char nStateNow = 0;
77 static K_STATE *pStateNow = &State[0];
78 static int GreekMode = GREEK_ELOT928;
79
80 /*
81 * The following are hard-coded for now. The idea is that such strings would
82 * be read from a config file making it possible to change language/encodings
83 * more flexibly.
84 */
85 /* elot 928 xlations */
86 static char elot_xlat_plain[] = "65-122:193,194,216,196,197,214,195,199,201,206,202,203,204,205,207,208,81,209,211,212,200,217,87,215,213,198,91,92,93,94,95,96,225,226,248,228,229,246,227,231,233,238,234,235,236,237,239,240,113,241,243,244,232,249,242,247,245,230";
87
88 /* c and s give copyright and section sign */
89 static char elot_xlat_acc[] = "65-122:182,194,216,196,184,214,195,185,186,206,202,203,204,205,188,208,81,209,211,212,200,191,87,215,190,198,91,92,93,94,95,96,220,226," /*248 */ "169,228,221,246,227,222,223,238,234,235,236,237,252,240,113,241," /*243 */ "167,244,232,254,242,247,253,230";
90 static char elot_xlat_acc_xtra[] = "46-62:183,47,48,49,50,51,52,53,54,55,56,57,58,59,171,61,187"; /* anw teleia, quotes */
91 static char elot_xlat_uml[] = "65-122:193,194,216,196,197,214,195,199,218,206,202,203,204,205,207,208,81,209,211,212,200,217,87,215,219,198,91,92,93,94,95,96,225,226,248,228,229,246,227,231,250,238,234,235,236,237,239,240,113,241,243,244,232,249,242,247,251,230";
92 static char elot_xlat_umacc[] = "65-122:193,194,216,196,197,214,195,199,201,206,202,203,204,205,207,208,81,209,211,212,200,217,87,215,213,198,91,92,93,94,95,96,225,226,248,228,229,246,227,231,192,238,234,235,236,237,239,240,113,241,243,244,232,249,242,247,224,230";
93
94 /* ibm 437 xlations */
95 static char i437_xlat_plain[] = "65-122:128,129,150,131,132,148,130,134,136,141,137,138,139,140,142,143,81,144,145,146,135,151,87,149,147,133,91,92,93,94,95,96,152,153,175,155,156,173,154,158,160,165,161,162,163,164,166,167,113,168,169,171,159,224,170,174,172,157";
96 static char i437_xlat_acc[] = "65-122:234,129,150,131,235,148,130,236,237,141,137,138,139,140,238,143,81,144,145,146,135,240,87,149,239,133,91,92,93,94,95,96,225,153,175,155,226,173,154,227,229,165,161,162,163,164,230,167,113,168,169,171,159,233,170,174,231,157";
97 static char i437_xlat_acc_xtra[] = "46-46:250"; /* anw teleia */
98 static char i437_xlat_uml[] = "65-122:128,129,150,131,132,148,130,134,136,141,137,138,139,140,142,143,81,144,145,146,135,151,87,149,147,133,91,92,93,94,95,96,152,153,175,155,156,173,154,158,228,165,161,162,163,164,166,167,113,168,169,171,159,224,170,174,232,157";
99 static char i437_xlat_umacc[] = "65-122:128,129,150,131,132,148,130,134,136,141,137,138,139,140,142,143,81,144,145,146,135,151,87,149,147,133,91,92,93,94,95,96,152,153,175,155,156,173,154,158,42,165,161,162,163,164,166,167,113,168,169,171,159,224,170,174,42,157";
100
101 /*
102 * currently ELOT928 and IBM437 are supported; easy to include others
103 * (not recommended: stick to just these 2 if not only the ELOT one)
104 */
105 static XLAT_TYPE xlat_type[] =
106 {
107 {elot_xlat_plain, elot_xlat_acc, elot_xlat_acc_xtra, elot_xlat_uml, elot_xlat_umacc},
108 {i437_xlat_plain, i437_xlat_acc, i437_xlat_acc_xtra, i437_xlat_uml, i437_xlat_umacc},
109 };
110
111 /* the current trasnaltion type */
112 static XLAT_TYPE *xlat_now = &xlat_type[GREEK_ELOT928];
113
114 #define NUM_XLAT_TYPES (sizeof(xlat_type) / sizeof(xlat_type[0]))
115
116 static void kstate_add_xlat(char *str);
117 static void kstate_add_switcher(char *str);
118 static void kstate_set_life(char *str);
119
120 /* --- Functions ------------- */
121 /* INTPROTO */
122 void
kstate_setcurr(int stateno)123 kstate_setcurr(int stateno)
124 {
125 u_char prev_state;
126
127 if ((u_int) stateno > (u_int) MAX_STATES)
128 return;
129 if (pStateNow->life == 1)
130 prev_state = pStateNow->prev_state;
131 else
132 prev_state = nStateNow;
133 pStateNow = &State[nStateNow = stateno];
134 pStateNow->prev_state = prev_state;
135 }
136
137 /* INTPROTO */
138 void
kstate_init(void)139 kstate_init(void)
140 {
141 pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0;
142 pStateNow->xlat = NULL;
143 }
144
145 /* INTPROTO */
146 void
kstate_end(void)147 kstate_end(void)
148 {
149 int i;
150
151 for (i = 0; i < pStateNow->num_xlat; i++)
152 rxvt_free(pStateNow->xlat[i].pval);
153 if (pStateNow->num_xlat > 0)
154 rxvt_free(pStateNow->xlat);
155 }
156
157 /*
158 * Hard coded ELOT-928 translations. Could read these from an rc-type file
159 * to support other remappers.
160 */
161 /* INTPROTO */
162 void
kstate_init_all(int greek_mode)163 kstate_init_all(int greek_mode)
164 {
165 /* the translation tables for the 4 FSM states for ELOT-928 mappings */
166 int i;
167
168 for (i = 0; i < MAX_STATES; i++) {
169 kstate_setcurr(i);
170 kstate_init();
171 }
172 if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES) /* avoid death */
173 greek_mode = GREEK_ELOT928;
174 xlat_now = &xlat_type[greek_mode];
175 kstate_setcurr(0);
176 kstate_add_xlat(xlat_now->plain);
177 kstate_add_switcher("A;:1");
178 kstate_add_switcher("A::2");
179 kstate_set_life("L0");
180
181 kstate_setcurr(1);
182 kstate_add_xlat(xlat_now->accent);
183 kstate_add_xlat(xlat_now->accent_xtra);
184 kstate_add_switcher("A::3");
185 kstate_set_life("L1");
186
187 kstate_setcurr(2);
188 kstate_add_xlat(xlat_now->umlaut);
189 kstate_add_switcher("A;:3");
190 kstate_set_life("L1");
191
192 kstate_setcurr(3);
193 kstate_add_xlat(xlat_now->acc_uml);
194 kstate_set_life("L1");
195 }
196
197 /* INTPROTO */
198 void
kstate_end_all(void)199 kstate_end_all(void)
200 {
201 int i;
202
203 for (i = 0; i < MAX_STATES; i++) {
204 kstate_setcurr(i);
205 kstate_end();
206 }
207 kstate_setcurr(0);
208 }
209
210 /*
211 * reset FSM
212 */
213 /* INTPROTO */
214 void
kstate_reset(void)215 kstate_reset(void)
216 {
217 kstate_setcurr(0);
218 }
219
220 /* INTPROTO */
221 void
kstate_add_xlat(char * str)222 kstate_add_xlat(char *str)
223 {
224 K_XLAT *xlat;
225 u_int *pval_tmp;
226 char *sval;
227 int i;
228
229 if (IS_NULL(str))
230 return;
231
232 /* add a new xlat table in state */
233 if (pStateNow->num_xlat == 0) {
234 pStateNow->xlat = rxvt_malloc(sizeof(K_XLAT));
235 }
236 else /* prefer contiguous data, realloc */
237 pStateNow->xlat = rxvt_realloc (pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT));
238
239 xlat = &pStateNow->xlat[pStateNow->num_xlat];
240 /* parse str and derive first, last, values */
241 xlat->first = (u_int) atoi(strtok(str, "-"));
242 xlat->last = (u_int) atoi(strtok(NULL, ":"));
243 i = 0;
244 pval_tmp = calloc(MAX_VAL, sizeof(K_XLAT));
245 while (NOT_NULL(sval = strtok(NULL, ",")))
246 pval_tmp[i++] = (u_int) (atoi(sval));
247 xlat->pval = calloc(i, sizeof(K_XLAT));
248 if (NOT_NULL(xlat->pval))
249 MEMCPY(xlat->pval, pval_tmp, i * sizeof(u_int));
250 rxvt_free(pval_tmp);
251 pStateNow->num_xlat++;
252 }
253
254 /*
255 * Ascii only for this implementation
256 */
257 /* INTPROTO */
258 void
kstate_add_switcher(char * str)259 kstate_add_switcher(char *str)
260 {
261 K_SWITCH *switcher;
262
263 if (IS_NULL(str))
264 return;
265 if (pStateNow->num_switcher >= MAX_SWITCHER)
266 return;
267 switcher = &pStateNow->switcher[pStateNow->num_switcher];
268 switch (switcher->type = str[0]) {
269 case 'A': /* ascii eg: A;:2 */
270 switcher->code = str[1];
271 switcher->nextstate = atoi(&str[3]);
272 break;
273 }
274 switcher->on = 0;
275 pStateNow->num_switcher++;
276 }
277
278 /* L1 or L0 */
279 /* INTPROTO */
280 void
kstate_set_life(char * str)281 kstate_set_life(char *str)
282 {
283 pStateNow->life = atoi(&str[1]);
284 }
285
286 /* INTPROTO */
287 unsigned int
kstate_cxlat(unsigned int c)288 kstate_cxlat(unsigned int c)
289 {
290 int i;
291
292 /* check for ascii switcher */
293 for (i = 0; i < pStateNow->num_switcher; i++)
294 if (pStateNow->switcher[i].type == 'A' && /* only ascii here */
295 c == pStateNow->switcher[i].code) {
296 kstate_setcurr(pStateNow->switcher[i].nextstate);
297 pStateNow->switcher[i].on = 1;
298 return ((unsigned int)-1);
299 }
300 /* do translation */
301 for (i = 0; i < pStateNow->num_xlat; i++)
302 if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) {
303 c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first];
304 break;
305 }
306 /* switch back to previous state if life of current is 1 */
307 if (pStateNow->life == 1)
308 kstate_setcurr(pStateNow->prev_state);
309 return (c);
310 }
311
312 /* EXTPROTO */
313 void
greek_init(void)314 greek_init(void)
315 {
316 kstate_init_all(GreekMode);
317 }
318
319 /* EXTPROTO */
320 void
greek_end(void)321 greek_end(void)
322 {
323 kstate_end_all();
324 }
325
326 /* EXTPROTO */
327 void
greek_reset(void)328 greek_reset(void)
329 {
330 kstate_reset();
331 }
332
333 /* EXTPROTO */
334 void
greek_setmode(int greek_mode)335 greek_setmode(int greek_mode)
336 {
337 GreekMode = greek_mode;
338 }
339
340 /* EXTPROTO */
341 int
greek_getmode(void)342 greek_getmode(void)
343 {
344 return (GreekMode);
345 }
346
347 /*
348 * xlate a given string in-place - return new string length
349 */
350 /* EXTPROTO */
351 int
greek_xlat(char * s,int num_chars)352 greek_xlat(char *s, int num_chars)
353 {
354 int i, count;
355 unsigned int c;
356
357 for (i = 0, count = 0; i < num_chars; i++) {
358 c = kstate_cxlat((unsigned int)s[i]);
359 if (c != -1)
360 s[count++] = (char)c;
361 }
362 s[count] = '\0';
363 return (count);
364
365 }
366
367 #ifdef TEST
368 int
main(void)369 main(void)
370 {
371 /*char text[] = "abcdef;aGDZXC"; */
372 char text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv";
373
374 kstate_init_all(GREEK_ELOT928);
375 printf("text: %s\n", text);
376 greek_xlat(text, STRLEN(text));
377 printf("xlat'ed text: %s\n", text);
378 kstate_end_all();
379 return 0;
380 }
381 #endif
382
383 #endif /* GREEK_SUPPORT */
384 /*----------------------- end-of-file (C source) -----------------------*/
385