1 /*
2  * File:      grkelot.c
3  *
4  * Synopsis:    string -> greek ELOT928 or IBM437 string;
5  *              4-state FSM implementation.
6  *
7  * System:      Any (ANSI C)
8  *
9  * This is code derived from a more generic key remapper written by the same
10  * author and used in other environments. It was not written only
11  * for greek kbd bindings. An extension to other languages is easy
12  * (well don't know how the FSM lends itself to Far East languages).
13  *
14  * The FSM can have MAX_STATES states (change it for more).
15  * Each state contains:
16  * 1.  many tranlsation tables (registered via kstate_add_xlat())
17  * 2.  many switch codes for transition to other states (registered via
18  *      kstate_add_switcher()) : limit is static now: MAX_SWITCHER
19  * 3.   life: the number of xlations allowed in a state (0 = unlimited)
20  *
21  * Format of tranlation strings:
22  *      <first>-<last>:n1,n2,n3,...
23  * Format of switcher string:
24  *      A<char>:<state_no>
25  * (other switchers apart from A=ascii can be supported; not in this context)
26  * Format of life string:
27  *      L<N>            (N=0,1,...)
28  *
29  * Copyright (c) 1994,1995 Angelo Haritsis. All rights reserved.
30  *
31  * Redistribution and use in source and binary forms are permitted provided
32  * that the above copyright notice and this paragraph are duplicated in all
33  * such forms and that any documentation, advertising materials, and other
34  * materials related to such distribution and use acknowledge that the
35  * software was developed by Angelo Haritsis.
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
37  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
38  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39  *
40  * NB: DO NOT ALTER! THIS CODE IS USED IN MANY PLATFORMS!!!
41  *
42  * TODO: make it more dynamic (linked lists is an idea but slower)
43  */
44 
45 static const char cvs_ident[] = "$Id: grkelot.c 51650 2010-08-26 01:34:13Z lucas $";
46 
47 #include "config.h"
48 #include "feature.h"
49 
50 #ifdef GREEK_SUPPORT
51 # include "grkelot.h"
52 # include <stdlib.h>
53 # include <string.h>
54 
55 /* --- Macros, Types --------- */
56 # define MAX_STATES	4       /* max # states for the FSM */
57 # define MAX_SWITCHER	2U      /* per state */
58 # define MAX_VAL 	256     /* for temp allocation */
59 
60 typedef unsigned char u_char;
61 typedef unsigned int u_int;
62 typedef unsigned long u_long;
63 
64 typedef struct s_xlat {
65     u_int first, last;
66     u_int *pval;                /* array of translated values */
67 } K_XLAT;
68 
69 typedef struct s_switch {
70     u_char type;                /* Ascii, Virtual, Scan */
71     u_int code;
72     u_char nextstate;
73     u_char on;                  /* current state of key: 0 = off */
74 } K_SWITCH;
75 
76 typedef struct s_state {
77     u_int num_xlat;             /* number of translations */
78     K_XLAT *xlat;               /* State translations ((dynamic - realloc'ed) */
79     u_int num_switcher;         /* number of switcher keys */
80     K_SWITCH switcher[MAX_SWITCHER];    /* switcher keys to other states */
81     u_char life;                /* 0 = until switched by key */
82     u_char prev_state;          /* filled when jumped to a new state */
83 } K_STATE;
84 
85 /* type for each one of the different greek standards (xlat types) */
86 typedef struct s_xlat_type {
87     char *plain;
88     char *accent;
89     char *accent_xtra;
90     char *umlaut;
91     char *acc_uml;
92 } XLAT_TYPE;
93 
94 /* --- Local Data ------------ */
95 static K_STATE State[MAX_STATES];
96 
97 /* Current State */
98 static u_char nStateNow = 0;
99 static K_STATE *pStateNow = &State[0];
100 static int GreekMode = GREEK_ELOT928;
101 
102 /*
103  * The following are hard-coded for now. The idea is that such strings would
104  * be read from a config file making it possible to change language/encodings
105  * more flexibly.
106  */
107 /* elot 928 xlations */
108 static char elot_xlat_plain[] =
109     "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";
110 
111 /* c and s give copyright and section sign */
112 static char elot_xlat_acc[] =
113     "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,"
114     /*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";
115 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 */
116 static char elot_xlat_uml[] =
117     "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";
118 static char elot_xlat_umacc[] =
119     "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";
120 
121 /* ibm 437 xlations */
122 static char i437_xlat_plain[] =
123     "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";
124 static char i437_xlat_acc[] =
125     "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";
126 static char i437_xlat_acc_xtra[] = "46-46:250"; /* anw teleia */
127 static char i437_xlat_uml[] =
128     "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";
129 static char i437_xlat_umacc[] =
130     "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";
131 
132 
133 /*
134  * currently ELOT928 and IBM437 are supported; easy to include others
135  * (not recommended: stick to just these 2 if not only the ELOT one)
136  */
137 static XLAT_TYPE xlat_type[] = {
138     {elot_xlat_plain, elot_xlat_acc, elot_xlat_acc_xtra, elot_xlat_uml, elot_xlat_umacc},
139     {i437_xlat_plain, i437_xlat_acc, i437_xlat_acc_xtra, i437_xlat_uml, i437_xlat_umacc},
140 };
141 
142 /* the current trasnaltion type */
143 static XLAT_TYPE *xlat_now = &xlat_type[GREEK_ELOT928];
144 
145 # define NUM_XLAT_TYPES	(sizeof(xlat_type) / sizeof(xlat_type[0]))
146 
147 static void kstate_add_xlat(char *str);
148 static void kstate_add_switcher(char *str);
149 static void kstate_set_life(char *str);
150 
151 /* --- Functions ------------- */
152 static void
kstate_setcurr(int stateno)153 kstate_setcurr(int stateno)
154 {
155     u_char prev_state;
156 
157     if ((u_int) stateno > (u_int) MAX_STATES)
158         return;
159     if (pStateNow->life == 1)
160         prev_state = pStateNow->prev_state;
161     else
162         prev_state = nStateNow;
163     pStateNow = &State[nStateNow = stateno];
164     pStateNow->prev_state = prev_state;
165 }
166 
167 static void
kstate_init(void)168 kstate_init(void)
169 {
170     pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0;
171     pStateNow->xlat = NULL;
172 }
173 
174 static void
kstate_end(void)175 kstate_end(void)
176 {
177     int i;
178 
179     for (i = 0; i < pStateNow->num_xlat; i++)
180         FREE(pStateNow->xlat[i].pval);
181     if (pStateNow->num_xlat > 0)
182         FREE(pStateNow->xlat);
183 }
184 
185 /*
186  * Hard coded ELOT-928 translations. Could read these from an rc-type file
187  * to support other remappers.
188  */
189 static void
kstate_init_all(int greek_mode)190 kstate_init_all(int greek_mode)
191 {
192     /* the translation tables for the 4 FSM states for ELOT-928 mappings */
193     int i;
194 
195     for (i = 0; i < MAX_STATES; i++) {
196         kstate_setcurr(i);
197         kstate_init();
198     }
199     if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES) /* avoid death */
200         greek_mode = GREEK_ELOT928;
201     xlat_now = &xlat_type[greek_mode];
202     kstate_setcurr(0);
203     kstate_add_xlat(xlat_now->plain);
204     kstate_add_switcher("A;:1");
205     kstate_add_switcher("A::2");
206     kstate_set_life("L0");
207 
208     kstate_setcurr(1);
209     kstate_add_xlat(xlat_now->accent);
210     kstate_add_xlat(xlat_now->accent_xtra);
211     kstate_add_switcher("A::3");
212     kstate_set_life("L1");
213 
214     kstate_setcurr(2);
215     kstate_add_xlat(xlat_now->umlaut);
216     kstate_add_switcher("A;:3");
217     kstate_set_life("L1");
218 
219     kstate_setcurr(3);
220     kstate_add_xlat(xlat_now->acc_uml);
221     kstate_set_life("L1");
222 }
223 
224 static void
kstate_end_all(void)225 kstate_end_all(void)
226 {
227     int i;
228 
229     for (i = 0; i < MAX_STATES; i++) {
230         kstate_setcurr(i);
231         kstate_end();
232     }
233     kstate_setcurr(0);
234 }
235 
236 /*
237  * reset FSM
238  */
239 static void
kstate_reset(void)240 kstate_reset(void)
241 {
242     kstate_setcurr(0);
243 }
244 
245 static void
kstate_add_xlat(char * str)246 kstate_add_xlat(char *str)
247 {
248     K_XLAT *xlat;
249     u_int *pval_tmp;
250     char *sval;
251     int i;
252 
253     if (!str)
254         return;
255     /* add a new xlat table in state */
256     if (pStateNow->num_xlat == 0) {
257         pStateNow->xlat = MALLOC(sizeof(K_XLAT));
258     } else                      /* prefer contiguous data, realloc */
259         pStateNow->xlat = REALLOC(pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT));
260     xlat = &pStateNow->xlat[pStateNow->num_xlat];
261     /* parse str and derive first, last, values */
262     xlat->first = (u_int) atoi(strtok(str, "-"));
263     xlat->last = (u_int) atoi(strtok(NULL, ":"));
264     i = 0;
265     pval_tmp = CALLOC(MAX_VAL, sizeof(K_XLAT));
266     while ((sval = strtok(NULL, ","))) {
267         pval_tmp[i++] = (u_int) (atoi(sval));
268     }
269     xlat->pval = CALLOC(i, sizeof(K_XLAT));
270     if (xlat->pval)
271         memcpy(xlat->pval, pval_tmp, i * sizeof(u_int));
272     FREE(pval_tmp);
273     pStateNow->num_xlat++;
274 }
275 
276 /*
277  * Ascii only for this implementation
278  */
279 static void
kstate_add_switcher(char * str)280 kstate_add_switcher(char *str)
281 {
282     K_SWITCH *switcher;
283 
284     if (!str)
285         return;
286     if (pStateNow->num_switcher >= MAX_SWITCHER)
287         return;
288     switcher = &pStateNow->switcher[pStateNow->num_switcher];
289     switch (switcher->type = str[0]) {
290         case 'A':              /* ascii eg: A;:2 */
291             switcher->code = str[1];
292             switcher->nextstate = atoi(&str[3]);
293             break;
294     }
295     switcher->on = 0;
296     pStateNow->num_switcher++;
297 }
298 
299 /* L1 or L0 */
300 static void
kstate_set_life(char * str)301 kstate_set_life(char *str)
302 {
303     pStateNow->life = atoi(&str[1]);
304 }
305 
306 static unsigned int
kstate_cxlat(unsigned int c)307 kstate_cxlat(unsigned int c)
308 {
309     int i;
310 
311     /* check for ascii switcher */
312     for (i = 0; i < pStateNow->num_switcher; i++)
313         if (pStateNow->switcher[i].type == 'A' &&       /* only ascii here */
314             c == pStateNow->switcher[i].code) {
315             kstate_setcurr(pStateNow->switcher[i].nextstate);
316             pStateNow->switcher[i].on = 1;
317             return ((unsigned int) -1);
318         }
319     /* do translation */
320     for (i = 0; i < pStateNow->num_xlat; i++)
321         if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) {
322             c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first];
323             break;
324         }
325     /* switch back to previous state if life of current is 1 */
326     if (pStateNow->life == 1)
327         kstate_setcurr(pStateNow->prev_state);
328     return (c);
329 }
330 
331 void
greek_init(void)332 greek_init(void)
333 {
334     kstate_init_all(GreekMode);
335 }
336 
337 void
greek_end(void)338 greek_end(void)
339 {
340     kstate_end_all();
341 }
342 
343 void
greek_reset(void)344 greek_reset(void)
345 {
346     kstate_reset();
347 }
348 
349 void
greek_setmode(int greek_mode)350 greek_setmode(int greek_mode)
351 {
352     GreekMode = greek_mode;
353 }
354 
355 int
greek_getmode(void)356 greek_getmode(void)
357 {
358     return (GreekMode);
359 }
360 
361 /*
362  * xlate a given string in-place - return new string length
363  */
364 int
greek_xlat(char * s,int num_chars)365 greek_xlat(char *s, int num_chars)
366 {
367     int i, count;
368     unsigned int c;
369 
370     for (i = 0, count = 0; i < num_chars; i++) {
371         c = kstate_cxlat((unsigned int) s[i]);
372         if (c != -1)
373             s[count++] = (char) c;
374     }
375     s[count] = '\0';
376     return (count);
377 }
378 
379 #  ifdef TEST
380 int
main(void)381 main(void)
382 {
383     /*char text[] = "abcdef;aGDZXC"; */
384     char text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv";
385 
386     kstate_init_all(GREEK_ELOT928);
387     printf("text: %s\n", text);
388     greek_xlat(text, strlen(text));
389     printf("xlat'ed text: %s\n", text);
390     kstate_end_all();
391     return 0;
392 }
393 #  endif
394 
395 #endif /* GREEK_SUPPORT */
396