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
7  * Copyright (c) 1997,1998	 Oezger Kesim <kesim@math.fu-berlin.de>
8  * Copyright (c) 2004		  Jingmin Zhou <jimmyzhou@users.sourceforge.net>
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 /*
25 ** $Id: grkelot.c,v 1.12 2004/11/11 00:20:28 cvs Exp $
26 */
27 
28 #include "../config.h"
29 #include "rxvt.h"
30 
31 
32 #ifdef DEBUG_VERBOSE
33 #define DEBUG_LEVEL 1
34 #else
35 #define DEBUG_LEVEL 0
36 #endif
37 
38 #if DEBUG_LEVEL
39 #define DBG_MSG(d,x) if(d <= DEBUG_LEVEL) fprintf x
40 #else
41 #define DBG_MSG(d,x)
42 #endif
43 
44 
45 #ifdef GREEK_SUPPORT
46 
47 /* --- Macros, Types --------- */
48 #define MAX_STATES	4	/* max # states for the FSM */
49 #define MAX_SWITCHER	2U	/* per state */
50 #define MAX_VAL 	256	/* for temp allocation */
51 
52 #ifndef HAVE_SYS_TYPES_H
53 typedef unsigned char u_char;
54 typedef unsigned int u_int;
55 typedef unsigned long u_long;
56 #endif
57 
58 typedef struct s_xlat {
59 	u_int		   first, last;
60 	u_int		  *pval;	/* array of translated values */
61 } K_XLAT;
62 
63 typedef struct s_switch {
64 	u_char		  type;	/* Ascii, Virtual, Scan */
65 	u_int		   code;
66 	u_char		  nextstate;
67 	u_char		  on;		/* current state of key: 0 = off */
68 } K_SWITCH;
69 
70 typedef struct s_state {
71 	u_int		  num_xlat;	/* number of translations */
72 	K_XLAT		 *xlat;	/* State translations ((dynamic - realloc'ed) */
73 	u_int		   num_switcher;	/* number of switcher keys */
74 	K_SWITCH		switcher[MAX_SWITCHER];	/* switcher keys to other states */
75 	u_char		  life;	/* 0 = until switched by key */
76 	u_char		  prev_state;	/* filled when jumped to a new state */
77 } K_STATE;
78 
79 /* type for each one of the different greek standards (xlat types) */
80 typedef struct s_xlat_type {
81 	char		   *plain;
82 	char		   *accent;
83 	char		   *accent_xtra;
84 	char		   *umlaut;
85 	char		   *acc_uml;
86 } XLAT_TYPE;
87 
88 /* --- Local Data ------------ */
89 static K_STATE  State[MAX_STATES];
90 
91 /* Current State */
92 static u_char   nStateNow = 0;
93 static K_STATE *pStateNow = &State[0];
94 static int	  GreekMode = GREEK_ELOT928;
95 
96 /*
97  * The following are hard-coded for now. The idea is that such strings would
98  * be read from a config file making it possible to change language/encodings
99  * more flexibly.
100  */
101 /* elot 928 xlations */
102 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";
103 
104 /* c and s give copyright and section sign */
105 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";
106 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 */
107 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";
108 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";
109 
110 /* ibm 437 xlations */
111 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";
112 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";
113 static char	 i437_xlat_acc_xtra[] = "46-46:250";	/* anw teleia */
114 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";
115 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";
116 
117 /*
118  * currently ELOT928 and IBM437 are supported; easy to include others
119  * (not recommended: stick to just these 2 if not only the ELOT one)
120  */
121 static XLAT_TYPE xlat_type[] =
122 {
123 	{elot_xlat_plain, elot_xlat_acc, elot_xlat_acc_xtra, elot_xlat_uml, elot_xlat_umacc},
124 	{i437_xlat_plain, i437_xlat_acc, i437_xlat_acc_xtra, i437_xlat_uml, i437_xlat_umacc},
125 };
126 
127 /* the current trasnaltion type */
128 static XLAT_TYPE *xlat_now = &xlat_type[GREEK_ELOT928];
129 
130 #define NUM_XLAT_TYPES	(sizeof(xlat_type) / sizeof(xlat_type[0]))
131 
132 static void	 kstate_add_xlat(char *str);
133 static void	 kstate_add_switcher(char *str);
134 static void	 kstate_set_life(char *str);
135 
136 /* --- Functions ------------- */
137 /* INTPROTO */
138 void
kstate_setcurr(int stateno)139 kstate_setcurr(int stateno)
140 {
141 	u_char		  prev_state;
142 
143 	if ((u_int) stateno > (u_int) MAX_STATES)
144 	return;
145 	if (pStateNow->life == 1)
146 	prev_state = pStateNow->prev_state;
147 	else
148 	prev_state = nStateNow;
149 	pStateNow = &State[nStateNow = stateno];
150 	pStateNow->prev_state = prev_state;
151 }
152 
153 /* INTPROTO */
154 void
kstate_init(void)155 kstate_init(void)
156 {
157 	pStateNow->num_xlat = pStateNow->num_switcher = pStateNow->life = pStateNow->prev_state = 0;
158 	pStateNow->xlat = NULL;
159 }
160 
161 /* INTPROTO */
162 void
kstate_end(void)163 kstate_end(void)
164 {
165 	int			 i;
166 
167 	for (i = 0; i < pStateNow->num_xlat; i++)
168 	free(pStateNow->xlat[i].pval);
169 	if (pStateNow->num_xlat > 0)
170 	free(pStateNow->xlat);
171 }
172 
173 /*
174  * Hard coded ELOT-928 translations. Could read these from an rc-type file
175  * to support other remappers.
176  */
177 /* INTPROTO */
178 void
kstate_init_all(int greek_mode)179 kstate_init_all(int greek_mode)
180 {
181 /* the translation tables for the 4 FSM states for ELOT-928 mappings */
182 	int			 i;
183 
184 	for (i = 0; i < MAX_STATES; i++) {
185 	kstate_setcurr(i);
186 	kstate_init();
187 	}
188 	if (greek_mode < 0 || greek_mode >= NUM_XLAT_TYPES)		/* avoid death */
189 	greek_mode = GREEK_ELOT928;
190 	xlat_now = &xlat_type[greek_mode];
191 	kstate_setcurr(0);
192 	kstate_add_xlat(xlat_now->plain);
193 	kstate_add_switcher("A;:1");
194 	kstate_add_switcher("A::2");
195 	kstate_set_life("L0");
196 
197 	kstate_setcurr(1);
198 	kstate_add_xlat(xlat_now->accent);
199 	kstate_add_xlat(xlat_now->accent_xtra);
200 	kstate_add_switcher("A::3");
201 	kstate_set_life("L1");
202 
203 	kstate_setcurr(2);
204 	kstate_add_xlat(xlat_now->umlaut);
205 	kstate_add_switcher("A;:3");
206 	kstate_set_life("L1");
207 
208 	kstate_setcurr(3);
209 	kstate_add_xlat(xlat_now->acc_uml);
210 	kstate_set_life("L1");
211 }
212 
213 /* INTPROTO */
214 void
kstate_end_all(void)215 kstate_end_all(void)
216 {
217 	int			 i;
218 
219 	for (i = 0; i < MAX_STATES; i++) {
220 	kstate_setcurr(i);
221 	kstate_end();
222 	}
223 	kstate_setcurr(0);
224 }
225 
226 /*
227  * reset FSM
228  */
229 /* INTPROTO */
230 void
kstate_reset(void)231 kstate_reset(void)
232 {
233 	kstate_setcurr(0);
234 }
235 
236 /* INTPROTO */
237 void
kstate_add_xlat(char * str)238 kstate_add_xlat(char *str)
239 {
240 	K_XLAT		 *xlat;
241 	u_int		  *pval_tmp;
242 	char		   *sval;
243 	int			 i;
244 
245 	if (str == NULL)
246 		return;
247 
248 	/* add a new xlat table in state */
249 	if (pStateNow->num_xlat == 0) {
250 		pStateNow->xlat = rxvt_malloc(sizeof(K_XLAT));
251 	}
252 	else			/* prefer contiguous data, realloc */
253 		pStateNow->xlat = rxvt_realloc (pStateNow->xlat, (pStateNow->num_xlat + 1) * sizeof(K_XLAT));
254 
255 	xlat = &pStateNow->xlat[pStateNow->num_xlat];
256 	/* parse str and derive first, last, values */
257 	xlat->first = (u_int) atoi(strtok(str, "-"));
258 	xlat->last = (u_int) atoi(strtok(NULL, ":"));
259 	i = 0;
260 	pval_tmp = calloc(MAX_VAL, sizeof(K_XLAT));
261 	while ((sval = strtok(NULL, ",")) != NULL)
262 		pval_tmp[i++] = (u_int) (atoi(sval));
263 	xlat->pval = calloc(i, sizeof(K_XLAT));
264 	if (xlat->pval != NULL)
265 		MEMCPY(xlat->pval, pval_tmp, i * sizeof(u_int));
266 	free(pval_tmp);
267 	pStateNow->num_xlat++;
268 }
269 
270 /*
271  * Ascii only for this implementation
272  */
273 /* INTPROTO */
274 void
kstate_add_switcher(char * str)275 kstate_add_switcher(char *str)
276 {
277 	K_SWITCH	   *switcher;
278 
279 	if (str == NULL)
280 		return;
281 	if (pStateNow->num_switcher >= MAX_SWITCHER)
282 		return;
283 	switcher = &pStateNow->switcher[pStateNow->num_switcher];
284 	switch (switcher->type = str[0]) {
285 	case 'A':			/* ascii eg: A;:2 */
286 		switcher->code = str[1];
287 		switcher->nextstate = atoi(&str[3]);
288 		break;
289 	}
290 	switcher->on = 0;
291 	pStateNow->num_switcher++;
292 }
293 
294 /* L1 or L0 */
295 /* INTPROTO */
296 void
kstate_set_life(char * str)297 kstate_set_life(char *str)
298 {
299 	pStateNow->life = atoi(&str[1]);
300 }
301 
302 /* INTPROTO */
303 unsigned int
kstate_cxlat(unsigned int c)304 kstate_cxlat(unsigned int c)
305 {
306 	int			 i;
307 
308 /* check for ascii switcher */
309 	for (i = 0; i < pStateNow->num_switcher; i++)
310 	if (pStateNow->switcher[i].type == 'A' &&	/* only ascii here */
311 		c == pStateNow->switcher[i].code) {
312 		kstate_setcurr(pStateNow->switcher[i].nextstate);
313 		pStateNow->switcher[i].on = 1;
314 		return ((unsigned int)-1);
315 	}
316 /* do translation */
317 	for (i = 0; i < pStateNow->num_xlat; i++)
318 	if (c >= pStateNow->xlat[i].first && c <= pStateNow->xlat[i].last) {
319 		c = pStateNow->xlat[i].pval[c - pStateNow->xlat[i].first];
320 		break;
321 	}
322 /* switch back to previous state if life of current is 1 */
323 	if (pStateNow->life == 1)
324 	kstate_setcurr(pStateNow->prev_state);
325 	return (c);
326 }
327 
328 /* EXTPROTO */
329 void
greek_init(void)330 greek_init(void)
331 {
332 	kstate_init_all(GreekMode);
333 }
334 
335 /* EXTPROTO */
336 void
greek_end(void)337 greek_end(void)
338 {
339 	kstate_end_all();
340 }
341 
342 /* EXTPROTO */
343 void
greek_reset(void)344 greek_reset(void)
345 {
346 	kstate_reset();
347 }
348 
349 /* EXTPROTO */
350 void
greek_setmode(int greek_mode)351 greek_setmode(int greek_mode)
352 {
353 	GreekMode = greek_mode;
354 }
355 
356 /* EXTPROTO */
357 int
greek_getmode(void)358 greek_getmode(void)
359 {
360 	return (GreekMode);
361 }
362 
363 /*
364  * xlate a given string in-place - return new string length
365  */
366 /* EXTPROTO */
367 int
greek_xlat(char * s,int num_chars)368 greek_xlat(char *s, int num_chars)
369 {
370 	int			 i, count;
371 	unsigned int	c;
372 
373 	for (i = 0, count = 0; i < num_chars; i++) {
374 	c = kstate_cxlat((unsigned int)s[i]);
375 	if (c != -1)
376 		s[count++] = (char)c;
377 	}
378 	s[count] = '\0';
379 	return (count);
380 
381 }
382 
383 #ifdef TEST
384 int
main(void)385 main(void)
386 {
387 /*char text[] = "abcdef;aGDZXC"; */
388 	char			text[] = "abcdef;a:ibgdezhuiklmnjoprstyfxcv";
389 
390 	kstate_init_all(GREEK_ELOT928);
391 	printf("text: %s\n", text);
392 	greek_xlat(text, STRLEN(text));
393 	printf("xlat'ed text: %s\n", text);
394 	kstate_end_all();
395 	return 0;
396 }
397 #endif
398 
399 #endif				/* GREEK_SUPPORT */
400 /*----------------------- end-of-file (C source) -----------------------*/
401