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