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

File Contents

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