1 /*
2  * Copyright (c) 1994  Sony Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Except as contained in this notice, the name of Sony Corporation
24  * shall not be used in advertising or otherwise to promote the sale, use
25  * or other dealings in this Software without prior written authorization
26  * from Sony Corporation.
27  *
28  */
29 
30 /*
31  * $SonyRCSfile: conv.c,v $
32  * $SonyRevision: 1.1 $
33  * $SonyDate: 1994/06/03 08:03:11 $
34  */
35 
36 
37 
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include "wchar16.h"
42 #include "select.h"
43 #include "key.h"
44 #include "inkey.h"
45 #include "common.h"
46 
47 #include "sj3.h"
48 
49 #define MSEC	300
50 
51 
52 
53 int	keyvalue;
54 
55 char	*funckey[] = {
56 		"\033OH",	"\033OP",	"\033OQ",
57 		"\033OR",	"\033OS",	"\033OT",
58 		"\033OU",	"\033OV",	"\033OW",
59 		"\033OX",	"\033OY",	"\033OZ",
60 		"\033[C",	"\033OC",	"\033[D",
61 		"\033OD",	"\033[A",	"\033OA",
62 		"\033[B",	"\033OB",	"\033[H",
63 		"\033[4~",	"\033[11~",	"\033[8~",
64 		"\033[12~",	"\033[13~",	"\033[9~",
65 		"\033[14~",	"\033[15~",	"\033[17~",
66 		"\033[18~",	"\033[19~",	"\033[20~",
67 		"\033[21~",	"\033[7~",
68 	};
69 
70 int	funcvalue[] = {
71 		KEY_CONV,	KEY_HENKAN,	KEY_MUHENKAN,
72 		KEY_ETC,	KEY_KETTEI,	KEY_CODE,
73 		KEY_HALPHA,	KEY_ZALPHA,	KEY_HKATA,
74 		KEY_ZKATA,	KEY_ZHIRA,	KEY_OTHER,
75 		KEY_RIGHT,	KEY_RIGHT,	KEY_LEFT,
76 		KEY_LEFT,	KEY_UP,		KEY_UP,
77 		KEY_DOWN,	KEY_DOWN,	KEY_CONV,
78 		KEY_HENKAN,	KEY_HENKAN,	KEY_MUHENKAN,
79 		KEY_MUHENKAN,	KEY_ETC,	KEY_KETTEI,
80 		KEY_KETTEI,	KEY_CODE,	KEY_HALPHA,
81 		KEY_ZALPHA,	KEY_HKATA,	KEY_ZKATA,
82 		KEY_ZHIRA,	KEY_CONV,
83 	};
84 
85 int	keynumber = sizeof(funckey)/sizeof(char*);
86 
87 
88 void
convert_stat()89 convert_stat()
90 {
91 	stat_init ();
92 	stat_conv ();
93 }
94 
95 void
inputprocess()96 inputprocess()
97 {
98 	int	c;
99 	wchar16_t		obuf[2];
100 
101 	if (Direct) {
102 		set_guide_line (KEY_CONV);
103 		convert_stat ();
104 	}
105 	set_guide_line (KEY_NORMAL);
106 	print_guide_line ();
107 	for (;;) {
108 		if ((c = inkey ()) == EOF)
109 			continue;
110 		switch (keyvalue) {
111 		case KEY_NORMAL:
112 			obuf[0] = c;
113 			obuf[1] = 0;
114 			SJ_write (obuf, 1);
115 			break;
116 		case KEY_CONV:
117 			set_guide_line (KEY_CONV);
118 			convert_stat ();
119 			set_guide_line (KEY_NORMAL);
120 			print_guide_line ();
121 			break;
122 		default:
123 			write_pseq (0);
124 			break;
125 		}
126 	}
127 }
128 
129 
130 wchar16_t		ibuf[KEYBUFSIZ];
131 wchar16_t		pars_seq[KEYBUFSIZ];
132 int		buf_count = 0;
133 int		pars_n;
134 static int	escape = 0;
135 
136 int
inkey()137 inkey()
138 {
139 	Conversion	*cv;
140 	int	i, j;
141 	wchar16_t c;
142 
143 	cv = current_conversion;
144 	keyvalue = KEY_NORMAL;
145 	pars_n = 0;
146 	if (buf_count) {
147 		if (escape && cv->Cflag)
148 			master_flush ();
149 		c = *ibuf;
150 	}
151 	else {
152 		if ((c = SJ_getchar ()) == (wchar16_t) EOF)
153 
154 			return ((short) c);
155 		*ibuf = c;
156 		buf_count = 1;
157 	}
158 
159 
160 	if (*ibuf == ESC) {
161 		keyvalue = sequence (ibuf, &buf_count);
162 		escape = 1;
163 	}
164 	else {
165 		escape = 0;
166 	}
167 
168 	if (keyvalue == KEY_NORMAL)
169 		keyvalue = parse_command (ibuf, &buf_count);
170 
171 	if (keyvalue == KEY_NORMAL) {
172 		c = *ibuf;
173 		for (i=1 ; i < buf_count ; i++)
174 			ibuf[i - 1] = ibuf[i];
175 		buf_count--;
176 	}
177 
178 	if (buf_count > KEYBUFLIM) {
179 		for (i = buf_count - 1; i >= 0 ; i --) {
180 			if (ibuf[i] == ESC) {
181 
182 				for (j = i ; j < buf_count ; j ++)
183 					ibuf[j-i] = ibuf[j];
184 				buf_count -= i;
185 				return(c);
186 			}
187 		}
188 
189 		buf_count = 0;
190 	}
191 	return (c);
192 }
193 
194 int
sequence(wchar16_t * buf,int * count)195 sequence(wchar16_t *buf, int *count)
196 {
197 
198 
199 
200 	int	n;
201 	SELECT_FD	ifds;
202 	struct timeval	t;
203 	wchar16_t *s;
204 	int		value;
205 
206 
207 	if (*count > 1 && (value = parse_escape (buf, count, 1)) != KEY_MORE)
208 		return (value);
209 
210 	s = buf + *count;
211 	for (;;) {
212 		t.tv_sec = 0;
213 		t.tv_usec = UsecWeight (0);
214 		FD_ZERO (&ifds);
215 		FD_SET (STDIN, &ifds);
216 		if (select (STDIN + 1, &ifds, 0, 0, &t) <= 0) {
217 			if (errno == EINTR) {
218 				errno = 0;
219 				continue;
220 			} else {
221 				return (parse_escape (buf, count, 0));
222 			}
223 		}
224 		if ((n = SJ_read (s, KEYBUFLIM)) <= 0) {
225 			return (parse_escape (buf, count, 0));
226 		}
227 		s += n;
228 		*count += n;
229 		if ((value = parse_escape (buf, count, 1)) == KEY_MORE)
230 			continue;
231 		else {
232 			return (value);
233 		}
234 	}
235 }
236 
237 
238 
239 int
parse_escape(wchar16_t * s,int * count,int more)240 parse_escape(wchar16_t *s, int *count, int more)
241 {
242 	int	i, j;
243 	wchar16_t		temp[KEYBUFSIZ];
244 	int		possible;
245 
246 	possible = 0;
247 
248 
249 
250 	for (i = 0 ; i < Unumber ; i ++) {
251 
252 
253 		for (j=0 ; j < *count ; j++) {
254 			if (s[j] != Ukey[i][j])
255 				break;
256 		}
257 
258 
259 		if (j == wslen (&Ukey[i][0])) {
260 
261 
262 			Strncpy (pars_seq, s, j);
263 			pars_seq[j] = (wchar16_t) '\0';
264 			pars_n = j;
265 
266 
267 			*count -= j;
268 
269 
270 			if (*count > 0) {
271 				Strncpy (temp, &s[j], *count);
272 				temp[*count] = (wchar16_t) '\0';
273                                 wscpy(s, temp);
274 			}
275 
276 			return (Uvalue[i]);
277 		}
278 		else if (j == *count)
279 			possible = 1;
280 	}
281 
282 
283 
284 	for (i = 0 ; i < keynumber ; i ++) {
285 
286 
287 		for (j = 0 ; j < *count ; j ++) {
288 			if (s[j] != (u_char)funckey[i][j])
289 				break;
290 		}
291 
292 
293 		if (j == strlen (funckey[i])) {
294 
295 
296 			Strncpy (pars_seq, s, j);
297 			pars_seq[j] = (wchar16_t) '\0';
298 			pars_n = j;
299 
300 
301 			*count -= j;
302 
303 
304 			if (*count > 0) {
305 				Strncpy (temp, &s[j], *count);
306 				temp[*count] = (wchar16_t) '\0';
307 				wscpy (s, temp);
308 			}
309 
310 			return (funcvalue[i]);
311 		}
312 		else if (j == *count)
313 			possible = 1;
314 	}
315 
316 	if (*count == 1)
317 		return (KEY_NORMAL);
318 	else if (more && possible)
319 		return (KEY_MORE);
320 	else {
321 		Strncpy (pars_seq, s, *count);
322 		pars_seq[*count] = (wchar16_t) '\0';
323 		pars_n = *count;
324 		*count = 0;
325 		*s = '\0';
326 		return (KEY_OTHER);
327 	}
328 }
329 
330 int
write_pseq(int mod)331 write_pseq (int mod)
332 {
333 
334 
335 	if (mod == 3) {
336 		if (pars_n > 0) {
337 			pars_n = 0;
338 			return (*pars_seq);
339 		}
340 	}
341 	if (mod == 2) {
342 		if (pars_n == 1) {
343 			pars_n = 0;
344 			return (*pars_seq);
345 		}
346 		pars_n = 0;
347 		return (0);
348 	}
349 	if (mod == 1) {
350 		if (pars_n == 1) {
351 			pars_n = 0;
352 			return (*pars_seq);
353 		}
354 		else if (IsGoto (*pars_seq)) {
355 			Strncpy (&ibuf[buf_count], &pars_seq[1], pars_n - 1);
356 			buf_count += pars_n - 1;
357 			pars_n = 0;
358 			return (*pars_seq);
359 		}
360 	}
361 	if (pars_n > 0)
362 		SJ_through (pars_seq, pars_n);
363 	pars_n = 0;
364 	return (0);
365 }
366 
367