ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/grkelot.C
Revision: 1.3
Committed: Sat Jan 31 00:20:21 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -17 lines
Log Message:
*** empty log message ***

File Contents

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