ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/grkelot.C
Revision: 1.1
Committed: Mon Nov 24 17:28:08 2003 UTC (20 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 /*---------------------------------*C*--------------------------------------*
2 * File: grkelot.c
3 *--------------------------------------------------------------------------*
4 * $Id: grkelot.C,v 1.1 2003/11/18 14:22:17 root 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 /* INTPROTO */
168 void
169 kstate_setcurr(int stateno)
170 {
171 u_char prev_state;
172
173 if ((u_int) stateno > (u_int) MAX_STATES)
174 return;
175 if (pStateNow->life == 1)
176 prev_state = pStateNow->prev_state;
177 else
178 prev_state = nStateNow;
179 pStateNow = &State[nStateNow = stateno];
180 pStateNow->prev_state = prev_state;
181 }
182
183 /* INTPROTO */
184 void
185 kstate_init(void)
186 {
187 pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0;
188 pStateNow->xlat = NULL;
189 }
190
191 /* INTPROTO */
192 void
193 kstate_end(void)
194 {
195 int i;
196
197 for (i = 0; i < pStateNow->num_xlat; i++)
198 free(pStateNow->xlat[i].pval);
199 if (pStateNow->num_xlat > 0)
200 free(pStateNow->xlat);
201 }
202
203 /*
204 * Hard coded ELOT-928 translations. Could read these from an rc-type file
205 * to support other remappers.
206 */
207 /* INTPROTO */
208 void
209 kstate_init_all(int greek_mode)
210 {
211 /* the translation tables for the 4 FSM states for ELOT-928 mappings */
212 int i;
213
214 for (i = 0; i < MAX_STATES; i++) {
215 kstate_setcurr(i);
216 kstate_init();
217 }
218 if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES) /* avoid death */
219 greek_mode = GREEK_ELOT928;
220 xlat_now = &xlat_type[greek_mode];
221 kstate_setcurr(0);
222 kstate_add_xlat(xlat_now->plain);
223 kstate_add_switcher("A;:1");
224 kstate_add_switcher("A::2");
225 kstate_set_life("L0");
226
227 kstate_setcurr(1);
228 kstate_add_xlat(xlat_now->accent);
229 kstate_add_xlat(xlat_now->accent_xtra);
230 kstate_add_switcher("A::3");
231 kstate_set_life("L1");
232
233 kstate_setcurr(2);
234 kstate_add_xlat(xlat_now->umlaut);
235 kstate_add_switcher("A;:3");
236 kstate_set_life("L1");
237
238 kstate_setcurr(3);
239 kstate_add_xlat(xlat_now->acc_uml);
240 kstate_set_life("L1");
241 }
242
243 /* INTPROTO */
244 void
245 kstate_end_all(void)
246 {
247 int i;
248
249 for (i = 0; i < MAX_STATES; i++) {
250 kstate_setcurr(i);
251 kstate_end();
252 }
253 kstate_setcurr(0);
254 }
255
256 /*
257 * reset FSM
258 */
259 /* INTPROTO */
260 void
261 kstate_reset(void)
262 {
263 kstate_setcurr(0);
264 }
265
266 /* INTPROTO */
267 void
268 kstate_add_xlat(char *str)
269 {
270 K_XLAT *xlat;
271 u_int *pval_tmp;
272 char *sval;
273 int i;
274
275 if (str == NULL)
276 return;
277 /* add a new xlat table in state */
278 if (pStateNow->num_xlat == 0) {
279 pStateNow->xlat = malloc(sizeof(K_XLAT));
280 } else /* prefer contiguous data, realloc */
281 pStateNow->xlat = realloc(pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT));
282 xlat = &pStateNow->xlat[pStateNow->num_xlat];
283 /* parse str and derive first, last, values */
284 xlat->first = (u_int) atoi(strtok(str, "-"));
285 xlat->last = (u_int) atoi(strtok(NULL, ":"));
286 i = 0;
287 pval_tmp = calloc(MAX_VAL, sizeof(K_XLAT));
288 while ((sval = strtok(NULL, ",")) != NULL)
289 pval_tmp[i++] = (u_int) (atoi(sval));
290 xlat->pval = calloc(i, sizeof(K_XLAT));
291 if (xlat->pval != NULL)
292 memcpy(xlat->pval, pval_tmp, i * sizeof(u_int));
293 free(pval_tmp);
294 pStateNow->num_xlat++;
295 }
296
297 /*
298 * Ascii only for this implementation
299 */
300 /* INTPROTO */
301 void
302 kstate_add_switcher(char *str)
303 {
304 K_SWITCH *switcher;
305
306 if (str == NULL)
307 return;
308 if (pStateNow->num_switcher >= MAX_SWITCHER)
309 return;
310 switcher = &pStateNow->switcher[pStateNow->num_switcher];
311 switch (switcher->type = str[0]) {
312 case 'A': /* ascii eg: A;:2 */
313 switcher->code = str[1];
314 switcher->nextstate = atoi(&str[3]);
315 break;
316 }
317 switcher->on = 0;
318 pStateNow->num_switcher++;
319 }
320
321 /* L1 or L0 */
322 /* INTPROTO */
323 void
324 kstate_set_life(char *str)
325 {
326 pStateNow->life = atoi(&str[1]);
327 }
328
329 /* INTPROTO */
330 unsigned int
331 kstate_cxlat(unsigned int c)
332 {
333 int i;
334
335 /* check for ascii switcher */
336 for (i = 0; i < pStateNow->num_switcher; i++)
337 if (pStateNow->switcher[i].type == 'A' && /* only ascii here */
338 c == pStateNow->switcher[i].code) {
339 kstate_setcurr(pStateNow->switcher[i].nextstate);
340 pStateNow->switcher[i].on = 1;
341 return ((unsigned int)-1);
342 }
343 /* do translation */
344 for (i = 0; i < pStateNow->num_xlat; i++)
345 if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) {
346 c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first];
347 break;
348 }
349 /* switch back to previous state if life of current is 1 */
350 if (pStateNow->life == 1)
351 kstate_setcurr(pStateNow->prev_state);
352 return (c);
353 }
354
355 #ifdef RXVT
356 /* EXTPROTO */
357 void
358 greek_init(void)
359 {
360 kstate_init_all(GreekMode);
361 }
362
363 /* EXTPROTO */
364 void
365 greek_end(void)
366 {
367 kstate_end_all();
368 }
369
370 /* EXTPROTO */
371 void
372 greek_reset(void)
373 {
374 kstate_reset();
375 }
376
377 /* EXTPROTO */
378 void
379 greek_setmode(int greek_mode)
380 {
381 GreekMode = greek_mode;
382 }
383
384 /* EXTPROTO */
385 int
386 greek_getmode(void)
387 {
388 return (GreekMode);
389 }
390
391 /*
392 * xlate a given string in-place - return new string length
393 */
394 /* EXTPROTO */
395 int
396 greek_xlat(char *s, int num_chars)
397 {
398 int i, count;
399 unsigned int c;
400
401 for (i = 0, count = 0; i < num_chars; i++) {
402 c = kstate_cxlat((unsigned int)s[i]);
403 if (c != -1)
404 s[count++] = (char)c;
405 }
406 s[count] = '\0';
407 return (count);
408
409 }
410
411 #ifdef TEST
412 int
413 main(void)
414 {
415 /*char text[] = "abcdef;aGDZXC"; */
416 char text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv";
417
418 kstate_init_all(GREEK_ELOT928);
419 printf("text: %s\n", text);
420 greek_xlat(text, strlen(text));
421 printf("xlat'ed text: %s\n", text);
422 kstate_end_all();
423 return 0;
424 }
425 #endif
426 #endif /* RXVT */
427
428 #endif /* GREEK_SUPPORT */