1 /*  esecannaserver --- pseudo canna server that wraps another IME.
2  *  Copyright (C) 1999-2000 Yasuhiro Take
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  ree Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <time.h>
24 #include <pwd.h>
25 #include <sys/types.h>
26 
27 #include "def.h"
28 #include "misc.h"
29 #include "vje.h"
30 #include "vjesocket.h"
31 
32 typedef struct _vje25_t {
33   char *maindic; /* ���Ѥ���ᥤ����Υѥ� */
34   char *userdic; /* ���Ѥ���桼��������Υѥ� */
35   char *optdic[8]; /* ���Ѥ��륪�ץ������Υѥ� */
36   char *restart; /* vjed ��Ƶ�ư���륳�ޥ�ɤؤΥե�ѥ� */
37   char userdic_copy_to_home:1; /* �桼��������� HOME �˥��ԡ����롩 */
38   char katakana_gakushu:1; /* �������ʤ��Ѵ������Τ�ؽ����롩 */
39   char restart_vjed:1; /* vjed ��������Ȥ����Ƶ�ư���롩 ��λ���롩 */
40 } vje25_t;
41 
42 typedef struct {
43   char romaji[5];
44   char yomi[5];
45 } roma_t;
46 
47 typedef struct _context_t {
48   struct _context_t *prev, *next;
49   short context_num;
50 
51   short max_bun;
52   struct {
53     uchar yomi_pnt;
54     uchar yomi_len;
55     ushort koho_num;
56     ushort *koho;
57     char *koho_inf;
58     size_t koho_len;
59   } bun[MAX_BUNSETU_NUM];
60 
61   int client_id; /* ���Υ���ƥ����ȤΥ����ʡ��Υ��饤����Ȥ� id */
62 
63   int canna_mode; /* ����ʤ��Ѵ��⡼�� */
64 
65   uchar yomi_buffer[HIRA_BUF_SIZE]; /* �ɤߤΥХåե�. vje ���� */
66   char subst_yomi_zero_flag; /* subst_yomi �� yomi �� null ���ä����� */
67 
68   struct {
69     short id; /* vje library �� id. */
70     char sdic_opened_flag[10];
71   } vje;
72 } context_t;
73 
74 static context_t *cx_top;
75 
76 static client_t *client;
77 
78 #define VW_ERROR16(_buf) { \
79   cannaheader_t *he; \
80   he = (cannaheader_t *)(_buf); \
81   he->datalen = LSBMSB16(2); \
82   he->err.e16 = LSBMSB16(-1); \
83 }
84 
85 #define VW_ERROR8(_buf) { \
86   cannaheader_t *he; \
87   he = (cannaheader_t *)(_buf); \
88   he->datalen = LSBMSB16(1); \
89   he->err.e8 = -1; \
90 }
91 
92 /*
93  * wrapper functions ����Ȥ��� �桼�ƥ���ƥ��ؿ�s
94  */
95 
96 /* ����ƥ����ȴ�Ϣ */
97 
vw_new_context(int id)98 static short vw_new_context(int id)
99 {
100   context_t *cx, *new;
101   short cx_num;
102 
103   if ((new = (context_t *)calloc(1, sizeof(context_t))) == NULL)
104     return -1;
105 
106   cx = cx_top;
107   if (cx) {
108     while (cx->next) cx = cx->next;
109     cx->next = new;
110     new->prev = cx;
111   } else {
112     cx_top = new;
113   }
114 
115   cx_num = 1;
116   for (;;) {
117     cx = cx_top;
118 
119     for (;;) {
120       if (cx == NULL) {
121 	new->context_num = cx_num;
122 	new->client_id = id;
123 	new->vje.id = 0;
124 	return cx_num;
125       }
126 
127       if (cx->context_num == cx_num) {
128 	cx_num++;
129 	break;
130       }
131 
132       cx = cx->next;
133     }
134   }
135 }
136 
vw_get_context(short cx_num)137 static context_t *vw_get_context(short cx_num)
138 {
139   context_t *cx;
140 
141   if (cx_num == -1)
142     return NULL;
143 
144   cx = cx_top;
145 
146   while (cx) {
147     if (cx->context_num == cx_num)
148       return cx;
149     cx = cx->next;
150   }
151 
152   return NULL;
153 }
154 
vw_clear_context(short cx_num)155 static int vw_clear_context(short cx_num)
156 {
157   context_t *cx;
158   int i;
159 
160   cx = vw_get_context(cx_num);
161 
162   for (i = 0; i < cx->max_bun; i++) {
163     if (cx->bun[i].koho)
164       free(cx->bun[i].koho);
165     cx->bun[i].koho = NULL;
166     if (cx->bun[i].koho_inf)
167       free(cx->bun[i].koho_inf);
168     cx->bun[i].koho_inf = NULL;
169     cx->bun[i].yomi_pnt = cx->bun[i].yomi_len = 0;
170     cx->bun[i].koho_num = cx->bun[i].koho_len = 0;
171   }
172   cx->max_bun = 0;
173 
174   memset(cx->yomi_buffer, 0, HIRA_BUF_SIZE);
175 
176   return 0;
177 }
178 
vw_free_context(short cx_num)179 static int vw_free_context(short cx_num)
180 {
181   context_t *cx;
182 
183   cx = vw_get_context(cx_num);
184   vw_clear_context(cx_num);
185 
186   if (cx->prev)
187     cx->prev->next = cx->next;
188   else
189     cx_top = cx->next;
190   if (cx->next)
191     cx->next->prev = cx->prev;
192 
193   free(cx);
194 
195   return 0;
196 }
197 
198 /* VJELIB �ط� */
199 
vw_open_vje_library(short cx_num)200 static int vw_open_vje_library(short cx_num)
201 {
202   short vje_id;
203   context_t *cx;
204   int id, i, j, mode;
205   char *dic;
206 
207   cx = vw_get_context(cx_num);
208   id = cx->client_id;
209 
210   if ((vje_id = vje_proto_vjelibopen()) != -1) {
211 
212     cx->vje.id = vje_id;
213 
214     vje_proto_loadcoderange();
215 
216     for (i = 0; i < 10; i++) {
217       switch(i) {
218 	case 0: dic = client[id].data.vje25->maindic; j = 38; break;
219 	case 9: dic = client[id].data.vje25->userdic; j = 39; break;
220 	default: dic = client[id].data.vje25->optdic[i - 1]; j = 38; break;
221       }
222 
223       if (dic) {
224 	vje_proto_clear(vje_id);
225 	vje_proto_open_sdic(vje_id, i, dic, j);
226 	cx->vje.sdic_opened_flag[i] = 1;
227       }
228     }
229 
230     mode = 0x5700;
231     mode |= client[id].data.vje25->katakana_gakushu ? 0x40 : 0;
232 
233     vje_proto_setmode(vje_id, mode, 0x300, 10, 3);
234 
235     /* OpenKanjiTable, VRoomajiOpen */
236 
237     vje_proto_setmode(vje_id, mode, 0x500, 10, 3);
238 
239     return 0;
240   }
241 
242   return -1;
243 }
244 
vw_close_vje_library(short cx_num)245 static int vw_close_vje_library(short cx_num)
246 {
247   int i;
248   context_t *cx;
249 
250   cx = vw_get_context(cx_num);
251 
252   if (cx->vje.id) {
253     for (i = 0; i < 10; i++)
254       if (cx->vje.sdic_opened_flag[i])
255 	vje_proto_close_sdic(cx->vje.id, i);
256 
257     vje_proto_vjelibclose(cx->vje.id);
258     cx->vje.id = 0;
259   }
260 
261   return 0;
262 }
263 
264 /* ���޻��ơ��֥�ط� */
265 
266 roma_t romatbl[] = {
267   {"a", "��"},
268   {"i", "��"},
269   {"ye", "����"},
270   {"u", "��"},
271   {"e", "��"},
272   {"o", "��"},
273   {"ka", "��"},
274   {"ki", "��"},
275   {"ku", "��"},
276   {"ke", "��"},
277   {"ko", "��"},
278   {"ga", "��"},
279   {"gi", "��"},
280   {"gu", "��"},
281   {"ge", "��"},
282   {"go", "��"},
283   {"kya", "����"},
284   {"kyi", "����"},
285   {"kyu", "����"},
286   {"kye", "����"},
287   {"kyo", "����"},
288   {"gya", "����"},
289   {"gyi", "����"},
290   {"gyu", "����"},
291   {"gye", "����"},
292   {"gyo", "����"},
293   {"gwa", "����"},
294   {"gwi", "����"},
295   {"gwu", "����"},
296   {"gwe", "����"},
297   {"gwo", "����"},
298   {"sa", "��"},
299   {"si", "��"},
300   {"su", "��"},
301   {"se", "��"},
302   {"so", "��"},
303   {"za", "��"},
304   {"zi", "��"},
305   {"zu", "��"},
306   {"ze", "��"},
307   {"zo", "��"},
308   {"sha", "����"},
309   {"syi", "����"},
310   {"shu", "����"},
311   {"she", "����"},
312   {"sho", "����"},
313   {"ja", "����"},
314   {"jyi", "����"},
315   {"ju", "����"},
316   {"je", "����"},
317   {"jo", "����"},
318   {"ta", "��"},
319   {"ti", "��"},
320   {"tu", "��"},
321   {"te", "��"},
322   {"to", "��"},
323   {"da", "��"},
324   {"di", "��"},
325   {"du", "��"},
326   {"de", "��"},
327   {"do", "��"},
328   {"cha", "����"},
329   {"cyi", "����"},
330   {"chu", "����"},
331   {"che", "����"},
332   {"cho", "����"},
333   {"tsa", "�Ĥ�"},
334   {"tsi", "�Ĥ�"},
335   {"tse", "�Ĥ�"},
336   {"tso", "�Ĥ�"},
337   {"tha", "�Ƥ�"},
338   {"thi", "�Ƥ�"},
339   {"thu", "�Ƥ�"},
340   {"the", "�Ƥ�"},
341   {"tho", "�Ƥ�"},
342   {"dya", "�¤�"},
343   {"dyi", "�¤�"},
344   {"dyu", "�¤�"},
345   {"dye", "�¤�"},
346   {"dyo", "�¤�"},
347   {"dha", "�Ǥ�"},
348   {"dhi", "�Ǥ�"},
349   {"dhu", "�Ǥ�"},
350   {"dhe", "�Ǥ�"},
351   {"dho", "�Ǥ�"},
352   {"na", "��"},
353   {"ni", "��"},
354   {"nu", "��"},
355   {"ne", "��"},
356   {"no", "��"},
357   {"nn", "��"},
358   {"nya", "�ˤ�"},
359   {"nyi", "�ˤ�"},
360   {"nyu", "�ˤ�"},
361   {"nye", "�ˤ�"},
362   {"nyo", "�ˤ�"},
363   {"ha", "��"},
364   {"hi", "��"},
365   {"fu", "��"},
366   {"he", "��"},
367   {"ho", "��"},
368   {"ba", "��"},
369   {"bi", "��"},
370   {"bu", "��"},
371   {"be", "��"},
372   {"bo", "��"},
373   {"pa", "��"},
374   {"pi", "��"},
375   {"pu", "��"},
376   {"pe", "��"},
377   {"po", "��"},
378   {"hya", "�Ҥ�"},
379   {"hyi", "�Ҥ�"},
380   {"hyu", "�Ҥ�"},
381   {"hye", "�Ҥ�"},
382   {"hyo", "�Ҥ�"},
383   {"fa", "�դ�"},
384   {"fi", "�դ�"},
385   {"fe", "�դ�"},
386   {"fo", "�դ�"},
387   {"bya", "�Ӥ�"},
388   {"byi", "�Ӥ�"},
389   {"byu", "�Ӥ�"},
390   {"bye", "�Ӥ�"},
391   {"byo", "�Ӥ�"},
392   {"pya", "�Ԥ�"},
393   {"pyi", "�Ԥ�"},
394   {"pyu", "�Ԥ�"},
395   {"pye", "�Ԥ�"},
396   {"pyo", "�Ԥ�"},
397   {"ma", "��"},
398   {"mi", "��"},
399   {"mu", "��"},
400   {"me", "��"},
401   {"mo", "��"},
402   {"mya", "�ߤ�"},
403   {"myi", "�ߤ�"},
404   {"myu", "�ߤ�"},
405   {"mye", "�ߤ�"},
406   {"myo", "�ߤ�"},
407   {"ya", "��"},
408   {"yu", "��"},
409   {"yo", "��"},
410   {"ra", "��"},
411   {"ri", "��"},
412   {"ru", "��"},
413   {"re", "��"},
414   {"ro", "��"},
415   {"rya", "���"},
416   {"ryi", "�ꤣ"},
417   {"ryu", "���"},
418   {"rye", "�ꤧ"},
419   {"ryo", "���"},
420   {"wa", "��"},
421   {"wi", "��"},
422   {"we", "��"},
423   {"wo", "��"},
424   {"vu", "��"},
425   {"va", "����"},
426   {"vi", "����"},
427   {"ve", "����"},
428   {"vo", "����"},
429   {"xa", "��"},
430   {"xi", "��"},
431   {"xu", "��"},
432   {"xe", "��"},
433   {"xo", "��"},
434   {"xwa", "��"},
435   {"xtu", "��"},
436   {"xya", "��"},
437   {"xyu", "��"},
438   {"xyo", "��"}
439 };
440 
441 #define ROMATBL_SIZE (sizeof(romatbl) / sizeof(roma_t))
442 
vw_create_romaji_table()443 static int vw_create_romaji_table()
444 {
445   /* romatbl[] �γ��ɤߥǡ����� SJIS ���Ѵ����� */
446   int i;
447   char buf[20];
448 
449   for (i = 0; i < ROMATBL_SIZE; i++) {
450     euc2sjis(romatbl[i].yomi, strlen(romatbl[i].yomi), buf, 20);
451 
452     strcpy(romatbl[i].yomi, buf);
453   }
454 
455   return 0;
456 }
457 
vw_lookup_romaji_table(uchar * sjisyomi,int slen)458 static int vw_lookup_romaji_table(uchar *sjisyomi, int slen)
459 {
460   int i;
461   char buf[5];
462 
463   strncpy(buf, sjisyomi, slen);
464   buf[slen] = '\0';
465 
466   for (i = 0; i < ROMATBL_SIZE; i++)
467     if (strcmp(romatbl[i].yomi, buf) == 0)
468       return i;
469 
470   return -1;
471 }
472 
473 /* ����ե�����졼�����ե�����ط� */
474 
vw_read_conf_file(int id,char * conffilepath)475 static int vw_read_conf_file(int id, char *conffilepath)
476 {
477   FILE *fp;
478   char buf[1024], optdic[11];
479   char *ope, *val;
480   int i, ret = 0;
481   vje25_t *vje25;
482 
483   vje25 = client[id].data.vje25;
484 
485   if ((fp = fopen(conffilepath, "r")) == NULL) {
486     m_msg("Cannot open Conffile %s.\n", conffilepath);
487     return -1;
488   }
489 
490   while (fgets(buf, 1024, fp)) {
491     if (buf[0] != '#' && m_conf1_parse(buf, &ope, &val) == 0) {
492       m_conf_string(ope, "VJE25.MainDic", val, &(vje25->maindic));
493       m_conf_string(ope, "VJE25.UserDic", val, &(vje25->userdic));
494 
495       i = M_CONF_ONOFF(ope, "VJE25.UserDicCopy", val);
496       if (i) vje25->userdic_copy_to_home = i - 1;
497 
498       i = M_CONF_ONOFF(ope, "VJE25.KatakanaGaku", val);
499       if (i) client[id].data.vje25->katakana_gakushu = i - 1;
500 
501       for (i = 0; i < 8; i++) {
502 	sprintf(optdic, "VJE25.OptDic%d", i + 1);
503 	m_conf_string(ope, optdic, val, &(vje25->optdic[i]));
504       }
505 
506       if (id == VJE_ROOT_CLIENT) {
507 	m_conf_string(ope, "VJE25.RestartComm", val, &(vje25->restart));
508 
509 	i = M_CONF_ONOFF(ope, "VJE25.RestartVjed", val);
510 	if (i) vje25->restart_vjed = i - 1;
511       }
512     }
513   }
514 
515   fclose(fp);
516 
517   return ret;
518 }
519 
vw_config_client(int id,char * conffile)520 static int vw_config_client(int id, char *conffile)
521 {
522   int user_ret;
523   char *userdic, *userdicx, *userdic_dir, *userdic_file, *p;
524   vje25_t *vje25;
525 
526   if ((vje25 = client[id].data.vje25 = calloc(1, sizeof(vje25_t))) == NULL) {
527     m_msg("Out of Memory.\n");
528     return -1;
529   }
530 
531   vje25->userdic_copy_to_home = TRUE;
532 
533   user_ret = vw_read_conf_file(id, conffile);
534 
535   if (user_ret == -1)
536     return -1;
537 
538   if (access(vje25->maindic, F_OK))
539     m_msg("Maindic %s not found. WHY?\n", vje25->maindic);
540 
541   userdicx = NULL;
542 
543   if (vje25->userdic_copy_to_home && id != VJE_ROOT_CLIENT) {
544     userdic = vje25->userdic;
545 
546     if (userdic && m_splitpath(userdic, &userdic_dir, &userdic_file) == 0) {
547       if (userdic_dir && strcmp(userdic_dir, client[id].homedir) != 0) {
548 	p = m_makepath(client[id].homedir, userdic_file);
549 
550 	if (p) {
551 	  if (access(p, R_OK)) {
552 	    if (m_copy_file(userdic, p) == 0)
553 	      userdicx = p;
554 	  } else
555 	    userdicx = p;
556 	}
557 
558 	free(userdic_dir);
559 	free(userdic_file);
560       } else {
561 	userdicx = userdic;
562       }
563     }
564   } else {
565     userdicx = vje25->userdic;
566   }
567 
568   if (userdicx != vje25->userdic && vje25->userdic != NULL)
569     free(vje25->userdic);
570 
571   vje25->userdic = userdicx;
572 
573   return 0;
574 }
575 
576 /* vjed ��������Ȥ��ν����˴ؤ���ؿ�s */
577 
vw_check8(buffer_t * cbuf)578 static int vw_check8(buffer_t *cbuf)
579 {
580   extern int vjeerror;
581 
582   if (vjeerror) {
583     /* vjeproto.c �ǡ�vjed �ȤΥǡ����Τ����˼���(vjed ����������)
584        ����ȡ��Ȥꤢ������vjeerror �� TRUE �ˤ��롣
585        vjewrapper.c ¦�Ǥϡ�vje_proto_*() ��Ƥ�����ȡ�vw_check*() ��
586        �Ƥ�ǡ����顼�����ä����ɤ��������å����롣
587        cannaproto.c �Ǥϡ�vjewrapper_*() ���֤��ͤˤ�ꡢ���顼�����ä���
588        �ɤ���ʬ���ꡢ���顼�����ä��Τʤ��vjed �Ƶ�ư�����פ�Ԥ���
589        �Ƶ�ư�����������Τʤ�(���ʤ������³�����������Τʤ�)��
590        vjewrapper_restarted() ��Ƥ֡�
591        �⤷���Ԥ����Τʤ顢���Ƥ� vje client �� close ���롣rootclient ��
592        close ���롣imewrapper.c �ǡ�vjed ������Ƥ��פȤ����ե饰���ơ�
593        �ʸ� imewrapper_initialize() �ǡ���³���� IME �� VJE �Ǥ��ä�����
594        �Ƥ� vje �Ȥ���³����������褦�ˤ��롣
595 
596        vjeerror �� TRUE �ʤ�С�vje_proto_*() �� No Operation �Ǥ��롣
597     */
598 
599     VW_ERROR8(cbuf->buf);
600 
601     return 1;
602   }
603 
604   return 0;
605 }
606 
607 
vw_check16(buffer_t * cbuf)608 static int vw_check16(buffer_t *cbuf)
609 {
610   extern int vjeerror;
611 
612   if (vjeerror) {
613     VW_ERROR16(cbuf->buf);
614 
615     return 1;
616   }
617 
618   return 0;
619 }
620 
vjewrapper_restarted()621 int vjewrapper_restarted()
622 {
623   extern int vjeerror;
624 
625   vjeerror = FALSE;
626 
627   return 0;
628 }
629 
vjewrapper_clear_vjeid()630 int vjewrapper_clear_vjeid()
631 {
632   context_t *cx;
633 
634   cx = cx_top;
635 
636   while (cx) {
637     if (cx->vje.id)
638       cx->vje.id = 0;
639     cx = cx->next;
640   }
641 
642   return 0;
643 }
644 
645 
646 /* ����¾��vje �˿����ط�����ؿ�s */
647 
vw_expr_yomi_start(int start,uchar * yomibuf)648 static int vw_expr_yomi_start(int start, uchar *yomibuf)
649 {
650   int start2 = 0;
651   int i = 0;
652 
653   while (yomibuf[i]) {
654     if (start == start2)
655       return i;
656 
657     if (ISSJISKANJI1(yomibuf[i]))
658       i += 2;
659     else
660       i++;
661 
662     start2++;
663   }
664 
665   if (start == start2)
666     return i;
667 
668   return -1;
669 }
670 
vw_make_vjebuffers(uchar * in_buf,ushort * atrbuf,uchar * keybuf)671 static int vw_make_vjebuffers(uchar *in_buf, ushort *atrbuf, uchar *keybuf)
672 {
673   int atr_pnt, key_pnt, in_pnt, d_in_pnt;
674   int i, romaslen;
675   int zenkana_flag;
676   uchar roma[5];
677   uchar hira[5];
678 
679   in_pnt = atr_pnt = key_pnt = 1;
680   d_in_pnt = 0;
681 
682   while (in_pnt <= LEN_OF_OLDBUF(in_buf)) {
683     zenkana_flag = 0;
684     if (in_pnt + 1 <= LEN_OF_OLDBUF(in_buf) &&
685 	(ISSJIS_ZENKAKU_HIRAGANA(in_buf[in_pnt], in_buf[in_pnt + 1]) ||
686 	 ISSJIS_ZENKAKU_VU(in_buf[in_pnt], in_buf[in_pnt + 1]))) {
687       /* �Ҥ餬��,�ޤ��ϡ֥��פξ�� */
688       if (in_pnt + 3 <= LEN_OF_OLDBUF(in_buf) &&
689 	  (i = vw_lookup_romaji_table(&(in_buf[in_pnt]), 4)) != -1) {
690 	/* "�ˤ�" �Τ褦���ɤߤξ�� */
691 	strcpy(roma, romatbl[i].romaji);
692 	d_in_pnt = 4;
693       } else if ((i = vw_lookup_romaji_table(&(in_buf[in_pnt]), 2)) != -1) {
694 	/* "��" �Τ褦���ɤߤξ�� */
695 	strcpy(roma, romatbl[i].romaji);
696 	d_in_pnt = 2;
697       }
698     } else if (in_pnt + 1 <= LEN_OF_OLDBUF(in_buf) &&
699 	       ISSJIS_ZENKAKU_KATAKANA(in_buf[in_pnt], in_buf[in_pnt + 1])) {
700       zenkana_flag = 1;
701       /* �������ʤξ�� */
702       d_in_pnt = -1;
703       if (in_pnt + 3 <= LEN_OF_OLDBUF(in_buf) &&
704 	  ISSJIS_ZENKAKU_KATAKANA(in_buf[in_pnt + 2], in_buf[in_pnt + 3])) {
705 	/* �����ɤߤ⥫�����ʤξ�� */
706 	m_convert_zenkana2zenhira(&(in_buf[in_pnt]), hira, 4);
707 
708 	if ((i = vw_lookup_romaji_table(hira, 4)) != -1) {
709 	  /* "����" �Τ褦���ɤߤξ�� */
710 	  strcpy(roma, romatbl[i].romaji);
711 	  d_in_pnt = 4;
712 	}
713       }
714 
715       if (d_in_pnt == -1) {
716 	/* �����ɤߤ⥫�����ʤ��� "����" �Τ褦���ɤߤǤϤʤ����,�ޤ���
717 	   �����ɤߤ��������ʤǤϤʤ���� */
718 	m_convert_zenkana2zenhira(&(in_buf[in_pnt]), hira, 2);
719 
720 	if ((i = vw_lookup_romaji_table(hira, 2)) != -1) {
721 	  /* "��" �Τ褦���ɤߤξ�� */
722 	  strcpy(roma, romatbl[i].romaji);
723 	  d_in_pnt = 2;
724 	}
725       }
726 
727       if (d_in_pnt == -1) {
728 	m_msg("vw_make_vjebuffer(): unexpected error01 %d\n", in_pnt);
729 	return -1;
730       }
731     } else if (ISSJISKANJI1(in_buf[in_pnt])) {
732       /* "��" "��" "��" �ʤɤ�������Ҥ餬�� */
733       if ((roma[0] = m_convert_zen2han(&(in_buf[in_pnt]))) == 0) {
734 	m_msg("make_vjebuffer(): unexpected error02 %d\n", in_pnt);
735 	return -1;
736       }
737       roma[1] = '\0';
738       d_in_pnt = 2;
739     } else {
740       /* Ⱦ�ѤΥ���ե��٥å�,���� */
741       roma[0] = in_buf[in_pnt];
742       roma[1] = '\0';
743       d_in_pnt = 1;
744     }
745 
746     romaslen = strlen(roma);
747 
748     switch (romaslen) {
749       case 3:
750 	if (zenkana_flag == 0) {
751 	  atrbuf[atr_pnt++] = 0x83; atrbuf[atr_pnt++] = 0x40;
752 	  if (d_in_pnt == 4) {
753 	    atrbuf[atr_pnt++] = 0x80; atrbuf[atr_pnt++] = 0x40;
754 	  }
755 	} else {
756 	  atrbuf[atr_pnt++] = 0xab; atrbuf[atr_pnt++] = 0x68;
757 	  atrbuf[atr_pnt++] = 0xa8; atrbuf[atr_pnt++] = 0x68;
758 	}
759 	break;
760       case 2:
761 	if (zenkana_flag == 0) {
762 	  atrbuf[atr_pnt++] = 0x82; atrbuf[atr_pnt++] = 0x40;
763 	  if (d_in_pnt == 4) {
764 	    atrbuf[atr_pnt++] = 0x80; atrbuf[atr_pnt++] = 0x40;
765 	  }
766 	} else {
767 	  atrbuf[atr_pnt++] = 0xaa; atrbuf[atr_pnt++] = 0x68;
768 	  if (d_in_pnt == 4) {
769 	    atrbuf[atr_pnt++] = 0xa8; atrbuf[atr_pnt++] = 0x68;
770 	  }
771 	}
772 	break;
773       case 1:
774 	if (d_in_pnt == 2) {
775 	  if (zenkana_flag) {
776 	    atrbuf[atr_pnt++] = 0xa9; atrbuf[atr_pnt++] = 0x68;
777 	  } else if (roma[0] >= 0xa1 && roma[0] <= 0xdf) {
778 	    atrbuf[atr_pnt++] = 0x8081; atrbuf[atr_pnt++] = 0x8040;
779 	  } else {
780 	    atrbuf[atr_pnt++] = 0x81; atrbuf[atr_pnt++] = 0x40;
781 	  }
782 	} else
783 	  atrbuf[atr_pnt++] = 0x01;
784 	break;
785     }
786 
787     memcpy(&(keybuf[key_pnt]), roma, romaslen);
788     key_pnt += romaslen;
789     in_pnt += d_in_pnt;
790   }
791 
792   LEN_OF_OLDBUF(atrbuf) = atr_pnt - 1;
793   LEN_OF_OLDBUF(keybuf) = key_pnt - 1;
794 
795   return 0;
796 }
797 
vw_move_bunsetu(context_t * cx,int n)798 static int vw_move_bunsetu(context_t *cx, int n)
799 {
800   /*
801   while (cx->vje.cur_bun_no < n) {
802     vje_proto_ji_bunsetu(vjesockfd, cx->vje.id);
803     cx->vje.cur_bun_no++;
804   }
805 
806   while (cx->vje.cur_bun_no > n) {
807     vje_proto_zen_bunsetu(vjesockfd, cx->vje.id);
808     cx->vje.cur_bun_no--;
809   }
810   */
811 
812   vje_proto_set_koho_bno(cx->vje.id, n);
813 
814   return 0;
815 }
816 
vw_convert_yomi_canna2vje(uchar * euc,int slen)817 static int vw_convert_yomi_canna2vje(uchar *euc, int slen)
818 {
819   euc[slen] = 0;
820 
821   m_replace_string(euc, "��", "��");
822   slen = m_replace_string(euc, "����", "��");
823 
824   return slen;
825 }
826 
vw_convert_yomi_vje2canna(uchar * euc,int slen)827 static int vw_convert_yomi_vje2canna(uchar *euc, int slen)
828 {
829   euc[slen] = 0;
830 
831   m_replace_string(euc, "��", "��");
832   slen = m_replace_string(euc, "��", "����");
833 
834   return slen;
835 }
836 
vw_get_yomi(context_t * cx,int bun_no,uchar * euc)837 static int vw_get_yomi(context_t *cx, int bun_no, uchar *euc)
838 {
839   uchar tmpbuf[IN_BUF_SIZE];
840   int slen;
841 
842   slen = cx->bun[bun_no].yomi_len;
843 
844   memcpy(tmpbuf, &(cx->yomi_buffer[cx->bun[bun_no].yomi_pnt]), slen);
845   tmpbuf[slen] = 0;
846 
847   slen = sjis2euc(tmpbuf, slen, euc, IN_BUF_SIZE);
848 
849   slen = vw_convert_yomi_vje2canna(euc, slen);
850 
851   euc[slen] = 0;
852 
853   return slen;
854 }
855 
vw_get_koho_list(context_t * cx,int bun_no,int * koho_num_r,int * size_r,char ** koho_inf_r)856 static ushort *vw_get_koho_list(context_t *cx, int bun_no, int *koho_num_r,
857 				int *size_r, char **koho_inf_r)
858 {
859   int block, i, j, k, pnt, buf_pnt, slen, slen2, koho_num, jiblock_ret;
860   uchar sjis[IN_BUF_SIZE], euc[IN_BUF_SIZE], tmpbuf[IN_BUF_SIZE];
861   uchar dic_buf[DIC_BUF_SIZE * 4];
862   ushort wcs[IN_BUF_SIZE];
863   short size;
864   char *koho_inf;
865   uchar yomi_len;
866   int hira_block, hira_koho, kata_block, kata_koho;
867 
868   extern int vjeerror;
869 
870   buffer_t zbuf, zbuf2;
871   doubufx_t douon;
872   buninf25_t bi;
873 
874   zbuf.size = zbuf2.size = 0;
875   zbuf.buf = zbuf2.buf = NULL;
876   hira_block = hira_koho = kata_block = kata_koho = -1;
877 
878   vw_move_bunsetu(cx, bun_no);
879 
880   vje_proto25_get_koho_all(cx->vje.id, &bi, tmpbuf);
881   yomi_len = bi.kouho[bun_no].yomi_len;
882 
883   memset(dic_buf, 0, DIC_BUF_SIZE * 4);
884 
885   block = koho_num = size = buf_pnt = 0;
886   jiblock_ret = -1;
887   for (;;) {
888     vje_proto_get_douon(cx->vje.id, &douon);
889 
890     if (vjeerror) {
891       if (zbuf.buf)
892 	free(zbuf.buf);
893       if (zbuf2.buf)
894 	free(zbuf2.buf);
895 
896       *koho_num_r = 0;
897       *size_r = 0;
898       *koho_inf_r = NULL;
899 
900       return NULL;
901     }
902 
903     if (jiblock_ret == 0 && memcmp(douon.buf, dic_buf, douon.len) == 0) {
904       block--;
905       break;
906     }
907 
908     pnt = 0;
909     for (i = 0; i < douon.cnt; i++) {
910       slen = douon.buf[pnt++];
911       memcpy(sjis, &(douon.buf[pnt]), slen);
912       sjis[slen] = 0;
913       pnt += slen;
914 
915       slen = sjis2euc(sjis, slen, euc, IN_BUF_SIZE);
916 
917       if (m_exist_hankata(euc) == 0) {
918 	if (m_is_zenkata_string(euc)) {
919 	  kata_block = block;
920 	  kata_koho = i;
921 	} else if (m_is_hiragana_string(euc)) {
922 	  hira_block = block;
923 	  hira_koho = i;
924 	}
925 
926 	slen = euc2cannawc(euc, slen, wcs, IN_BUF_SIZE);
927 	slen *= 2;
928 
929 	size += slen + 2;
930 
931 	buffer_check(&zbuf, size);
932 
933 	memcpy(&(zbuf.buf[buf_pnt]), wcs, slen);
934 	buf_pnt += slen;
935 	zbuf.buf[buf_pnt++] = 0; zbuf.buf[buf_pnt++] = 0;
936 
937 	koho_num++;
938 
939 	buffer_check(&zbuf2, koho_num);
940 	koho_inf = (char *)zbuf2.buf;
941 	koho_inf[koho_num - 1] = block;
942       }
943     }
944 
945     if ((jiblock_ret = vje_proto_ji_block(cx->vje.id)) == 0) {
946       /* ji_block ���֤��ͤ� 0 �ΤȤ�������ʾ弡�֥�å����ʤ���ǽ�������롣
947 	 ���ξ�硢douon.buf �����Ƥ��Ѥ��ʤ��Τǡ����Υ����å��Τ��ᡢ
948 	 douon.buf �ΥХå����åפ�Ȥ롣*/
949       memcpy(dic_buf, douon.buf, douon.len);
950     }
951     block++;
952 
953     vje_proto25_get_koho_all(cx->vje.id, &bi, tmpbuf);
954     if (yomi_len != bi.kouho[bun_no].yomi_len) {
955       /* ji_block �ˤ�ꡢʸ��ζ��ڤ꤬û���ʤä���硢�����ޤ� */
956       break;
957     }
958   }
959 
960   for (i = 0; i < block; i++)
961     vje_proto_zen_block(cx->vje.id);
962 
963   vje_proto25_get_koho_all(cx->vje.id, &bi, tmpbuf);
964   if (yomi_len != bi.kouho[bun_no].yomi_len) {
965     /* �֥�å����ᤷ��������� */
966     vje_proto_ji_block(cx->vje.id);
967   }
968 
969   slen = vw_get_yomi(cx, bun_no, euc);
970 
971   /* cx->canna_mode �˽�����������դ�­�� */
972 
973   j = m_count_canna_mode(cx->canna_mode);
974 
975   for (i = 0; i < j; i++) {
976     k = m_get_canna_mode(cx->canna_mode, i);
977 
978     if (k == 3) {
979       /* �������ʸ��� */
980       koho_num++;
981 
982       slen2 = m_convert_zenhira2zenkata(euc, slen, tmpbuf);
983 
984       slen2 = euc2cannawc(tmpbuf, slen2, wcs, IN_BUF_SIZE);
985       slen2 *= 2;
986 
987       size += slen2 + 2;
988 
989       buffer_check(&zbuf, size);
990 
991       memcpy(&(zbuf.buf[buf_pnt]), wcs, slen2);
992       buf_pnt += slen2;
993       zbuf.buf[buf_pnt++] = 0; zbuf.buf[buf_pnt++] = 0;
994 
995       buffer_check(&zbuf2, koho_num);
996       koho_inf = (char *)zbuf2.buf;
997       koho_inf[koho_num - 1] = kata_block;
998     } else if (k == 1) {
999       /* �Ҥ餬�ʸ��� */
1000       koho_num++;
1001 
1002       slen2 = euc2cannawc(euc, slen, wcs, IN_BUF_SIZE);
1003       slen2 *= 2;
1004 
1005       size += slen2 + 2;
1006 
1007       buffer_check(&zbuf, size);
1008 
1009       memcpy(&(zbuf.buf[buf_pnt]), wcs, slen2);
1010       buf_pnt += slen2;
1011       zbuf.buf[buf_pnt++] = 0; zbuf.buf[buf_pnt++] = 0;
1012 
1013       buffer_check(&zbuf2, koho_num);
1014       koho_inf = (char *)zbuf2.buf;
1015       koho_inf[koho_num - 1] = hira_block;
1016     }
1017   }
1018 
1019   size += 2;
1020   buffer_check(&zbuf, size);
1021 
1022   zbuf.buf[buf_pnt++] = 0; zbuf.buf[buf_pnt++] = 0;
1023 
1024   koho_inf = (char *)zbuf2.buf;
1025 
1026   *koho_num_r = koho_num;
1027   *size_r = size;
1028   *koho_inf_r = koho_inf;
1029 
1030   return (ushort *)zbuf.buf;
1031 }
1032 
vw_retrieve_koho(short cx_num,int bun_no,int koho_no)1033 static ushort *vw_retrieve_koho(short cx_num, int bun_no, int koho_no)
1034 {
1035   context_t *cx;
1036   ushort *p, *ret;
1037   uchar sjis[IN_BUF_SIZE], euc[IN_BUF_SIZE];
1038   static ushort wcs[IN_BUF_SIZE];
1039   int i, j, slen, koho_num, koholen;
1040   char *koho_inf;
1041   buninf25_t bi;
1042 
1043   cx = vw_get_context(cx_num);
1044   p = cx->bun[bun_no].koho;
1045 
1046   if (p == NULL && koho_no > 0) {
1047     /* �ޤ������������Ƥʤ��ʤ�������� */
1048     cx->bun[bun_no].koho = vw_get_koho_list(cx, bun_no, &koho_num, &koholen,
1049 					    &koho_inf);
1050     cx->bun[bun_no].koho_inf = koho_inf;
1051     cx->bun[bun_no].koho_num = koho_num;
1052     cx->bun[bun_no].koho_len = koholen;
1053 
1054     p = cx->bun[bun_no].koho;
1055   }
1056 
1057   if (p) {
1058     i = 0; j = 0;
1059     while (i < koho_no) {
1060       while (p[j]) j++;
1061       j++;
1062       i++;
1063     }
1064 
1065     ret = &p[j];
1066   } else {
1067     vje_proto25_get_koho_all(cx->vje.id, &bi, sjis);
1068     slen = sjis2euc(&(sjis[bi.kouho[bun_no].hyk_pnt]),
1069 		    bi.kouho[bun_no].hyk_len, euc, IN_BUF_SIZE);
1070     euc2cannawc(euc, slen, wcs, IN_BUF_SIZE);
1071 
1072     ret = wcs;
1073   }
1074 
1075   return ret;
1076 }
1077 
vw_set_koho(short cx_num,int bun_no,int koho_no)1078 static int vw_set_koho(short cx_num, int bun_no, int koho_no)
1079 {
1080   int block, i, len, pnt;
1081   context_t *cx;
1082   uchar hira[IN_BUF_SIZE], kata[IN_BUF_SIZE], sjis[IN_BUF_SIZE];
1083   uchar in_buf[IN_BUF_SIZE], keybuf[IN_BUF_SIZE], tmpbuf[IN_BUF_SIZE];
1084   ushort atrbuf[IN_BUF_SIZE * 2], *sp;
1085   doubufx_t douon;
1086 
1087   cx = vw_get_context(cx_num);
1088 
1089   if (cx->bun[bun_no].koho_inf) {
1090     block = cx->bun[bun_no].koho_inf[koho_no];
1091 
1092     if (block != -1) {
1093       sp = vw_retrieve_koho(cx_num, bun_no, koho_no);
1094       len = cannawc2euc(sp, cannawcstrlen(sp), tmpbuf, IN_BUF_SIZE);
1095       euc2sjis(tmpbuf, strlen(tmpbuf), sjis, IN_BUF_SIZE);
1096 
1097       for (i = 0; i < block; i++)
1098 	vje_proto_ji_block(cx->vje.id);
1099 
1100       vje_proto_get_douon(cx->vje.id, &douon);
1101 
1102       pnt = 0;
1103       for (i = 0; i < douon.cnt; i++) {
1104 	len = douon.buf[pnt++];
1105 	memcpy(tmpbuf, &(douon.buf[pnt]), len);
1106 	tmpbuf[len] = 0;
1107 	pnt += len;
1108 
1109 	if (strcmp(tmpbuf, sjis) == 0) {
1110 	  sjis2euc(sjis, strlen(sjis), tmpbuf, IN_BUF_SIZE);
1111 
1112 	  vje_proto_set_koho(cx->vje.id, 1 + i);
1113 
1114 	  return 0;
1115 	}
1116       }
1117     } else {
1118       /* block == -1 �λ��ϡ��������ʸ��� */
1119       len = vw_get_yomi(cx, bun_no, hira);
1120 
1121       m_convert_zenhira2zenkata(hira, len, kata);
1122 
1123       len = euc2sjis(kata, len, sjis, IN_BUF_SIZE);
1124 
1125       memcpy(in_buf, cx->yomi_buffer, LEN_OF_OLDBUF(cx->yomi_buffer) + 1);
1126       memcpy(&(in_buf[cx->bun[bun_no].yomi_pnt]), sjis, len);
1127 
1128       vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1129 
1130       vje_proto25_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1131       vje_proto25_saihenkan(cx->vje.id, in_buf, 1, len, len, 8);
1132 
1133       memcpy(cx->yomi_buffer, in_buf, LEN_OF_OLDBUF(in_buf) + 1);
1134     }
1135   } else {
1136     /* koho_inf == NULL �Ȥ������Ȥϡ�GetCandidacyList ��Ƥ�Ǥ��ʤ���
1137        ���ξ�硢���� No. �� 0 ,���ʤ����ͥ�����˷�ޤäƤ�Ϥ���
1138     */
1139 
1140     vje_proto_set_koho(cx->vje.id, 1);
1141   }
1142 
1143   return 0;
1144 }
1145 
1146 /*
1147  * vw_after_henkan() : �Ѵ���Ƥִؿ�
1148  *                   - in_buf : �ɤ�
1149  *                   - bun_no : ��ʸ��κ�ͥ�����Τ�����bun_no �ǻ��ꤵ�줿
1150  *                            : ʸ�ᤫ��θ���s �� wcs_r �ˤ���롣
1151  *                   - ������ : wcs_r ��Ĺ��
1152  */
1153 
vw_after_henkan(short cx_num,uchar * in_buf,ushort * wcs_r,int bun_no)1154 static int vw_after_henkan(short cx_num, uchar *in_buf, ushort *wcs_r,
1155 			   int bun_no)
1156 {
1157   int i, pnt, len;
1158   uchar bufsjis[IN_BUF_SIZE], bufeuc[IN_BUF_SIZE];
1159   buninf25_t bi;
1160   context_t *cx;
1161 
1162   cx = vw_get_context(cx_num);
1163 
1164   vje_proto25_get_koho_all(cx->vje.id, &bi, bufsjis);
1165 
1166   sjis2euc(bufsjis + 1, bufsjis[0], bufeuc, IN_BUF_SIZE);
1167 
1168   m_msg_dbg("kouho = ");
1169 
1170   pnt = 0;
1171   for (i = bun_no; i < bi.max_bun; i++) {
1172     len = sjis2euc(&(bufsjis[bi.kouho[i].hyk_pnt]),bi.kouho[i].hyk_len,
1173 		   bufeuc, IN_BUF_SIZE);
1174     m_msg_dbg("%d:[%s] ", i, bufeuc);
1175     len = m_convert_hankana2zenkana(bufeuc, len);
1176     len = euc2cannawc(bufeuc, len, &(wcs_r[pnt]), IN_BUF_SIZE - pnt);
1177     pnt += len;
1178     wcs_r[pnt++] = 0;
1179   }
1180 
1181   m_msg_dbg("\n");
1182 
1183   wcs_r[pnt++] = 0;
1184 
1185   vw_clear_context(cx_num);
1186 
1187   memcpy(cx->yomi_buffer, in_buf, IN_BUF_SIZE);
1188 
1189   cx->max_bun = bi.max_bun;
1190 
1191   for (i = cx->max_bun - 1; i >= 0; i--) { /* ???: ��̣������
1192 					      i = 0 -> cx->max_bun - 1 �Ǥ�
1193 					      (ʸ��̤��)������ʸ��θ����
1194 					      �����Ǥ��ʤ�
1195 					   */
1196     cx->bun[i].yomi_pnt = bi.kouho[i].yomi_pnt;
1197     cx->bun[i].yomi_len = bi.kouho[i].yomi_len;
1198   }
1199 
1200   return pnt * 2;
1201 }
1202 
1203 /*
1204  * ���˸�������ؿ�s �������λ�ط�
1205  */
1206 
1207 /* dlopen() ���줿����, client[] �� vjewrapper.c ���Ϥ�����˸ƤФ�� */
1208 
vjewrapper_dl_started(client_t * cl)1209 int vjewrapper_dl_started(client_t *cl)
1210 {
1211   client = cl;
1212 
1213   m_msg("Module: %s\n", ESECANNA_MODULE_VERSION);
1214 
1215   return 0;
1216 }
1217 
vjewrapper_init_rootclient()1218 int vjewrapper_init_rootclient()
1219 {
1220   char buf[128];
1221   short cx_num;
1222 
1223   m_msg("Initializing root client for VJE25.\n");
1224 
1225   if (vw_config_client(VJE_ROOT_CLIENT, ESECANNA_RC_PATH) == -1) {
1226     m_msg("No conffile found. Aborting.\n");
1227     return -1;
1228   }
1229 
1230   vw_create_romaji_table();
1231 
1232   if (gethostname(buf, 128))
1233     strcpy(buf, "localhost");
1234 
1235   if (vje_proto_set_clienthostname(buf) == -1) {
1236     m_msg("set_clienthostname failed. Aborting.\n");
1237     return -1;
1238   }
1239 
1240   if ((cx_num = vw_new_context(VJE_ROOT_CLIENT)) == -1) {
1241     m_msg("Out of memory. Cannot allocate context. Aborting.\n");
1242     return -1;
1243   }
1244 
1245   if (vw_open_vje_library(cx_num) == -1) {
1246     m_msg("vjelibopen failed. Aborting.\n");
1247     return -1;
1248   }
1249 
1250   /* OpenKanjiTable, VRoomajiOpen */
1251 
1252   m_msg("Initialize succeeded.\n");
1253 
1254   return 0;
1255 }
1256 
vjewrapper_end_client(int id)1257 int vjewrapper_end_client(int id)
1258 {
1259   context_t *cx, *cx2;
1260 
1261   cx = cx_top;
1262 
1263   while (cx) {
1264     if (cx->client_id == id) {
1265       cx2 = cx->next;
1266 
1267       vw_close_vje_library(cx->context_num);
1268       vw_free_context(cx->context_num);
1269 
1270       cx = cx2;
1271     } else
1272       cx = cx->next;
1273   }
1274 
1275   return 0;
1276 }
1277 
vjewrapper_end_rootclient()1278 int vjewrapper_end_rootclient()
1279 {
1280   vjewrapper_end_client(VJE_ROOT_CLIENT);
1281   vje_proto_finish_session();
1282 
1283   vje_socket_close_connection();
1284 
1285   return 0;
1286 }
1287 
vjewrapper_clear_client_data(int id)1288 int vjewrapper_clear_client_data(int id)
1289 {
1290   int i;
1291   vje25_t *vje25 = client[id].data.vje25;
1292 
1293   MYFREE(vje25->maindic);
1294   MYFREE(vje25->userdic);
1295 
1296   for (i = 0; i < 8; i++)
1297     MYFREE(vje25->optdic[i]);
1298 
1299   MYFREE(vje25->restart);
1300 
1301   MYFREE(client[id].data.vje25);
1302 
1303   return 0;
1304 }
1305 
1306 /*
1307  * �֤���ʡפ� vje �� wrapping ����ؿ�s
1308  */
1309 
vjewrapper_initialize(int id,char * conffile)1310 int vjewrapper_initialize(int id, char *conffile)
1311 {
1312   if (vw_config_client(id, conffile) == 0)
1313     return vw_new_context(id);
1314 
1315   return -1;
1316 }
1317 
vjewrapper_finalize(int id,buffer_t * cbuf)1318 int vjewrapper_finalize(int id, buffer_t *cbuf)
1319 {
1320   cannaheader_t *header;
1321 
1322   client[id].need_terminate = TRUE; /* main.c �ǽ�λ�������Ƥ�餦 */
1323 
1324   header = (cannaheader_t *)cbuf->buf;
1325   header->type = 0x02;
1326   header->extra = 0;
1327   header->datalen = LSBMSB16(1);
1328   header->err.e8 = 0;
1329 
1330   return 1;
1331 }
1332 
vjewrapper_create_context(int id,buffer_t * cbuf)1333 int vjewrapper_create_context(int id, buffer_t *cbuf)
1334 {
1335   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1336   short *sp = (short *)cbuf->buf;
1337   short cx_num;
1338 
1339   cx_num = vw_new_context(id);
1340 
1341   header->type = 0x03;
1342   header->extra = 0;
1343   header->datalen = LSBMSB16(2);
1344 
1345   sp[2] = LSBMSB16(cx_num);
1346 
1347   return 1;
1348 }
1349 
vjewrapper_duplicate_context(int id,buffer_t * cbuf)1350 int vjewrapper_duplicate_context(int id, buffer_t *cbuf)
1351 {
1352   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1353   short *sp = (short *)cbuf->buf;
1354   short cx_n_new, cx_n_orig;
1355   context_t *cx_orig, *cx_new;
1356 
1357   cx_n_orig = LSBMSB16(sp[2]);
1358   cx_n_new = vw_new_context(id);
1359 
1360   cx_orig = vw_get_context(cx_n_orig);
1361   cx_new = vw_get_context(cx_n_new);
1362 
1363   memcpy(cx_new->yomi_buffer, cx_orig->yomi_buffer, HIRA_BUF_SIZE);
1364 
1365   header->type = 0x04;
1366   header->extra = 0;
1367   header->datalen = LSBMSB16(2);
1368 
1369   sp[2] = LSBMSB16(cx_n_new);
1370 
1371   return 1;
1372 }
1373 
vjewrapper_close_context(int id,buffer_t * cbuf)1374 int vjewrapper_close_context(int id, buffer_t *cbuf)
1375 {
1376   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1377   short *sp = (short *)cbuf->buf;
1378   short cx_num;
1379   uchar err;
1380 
1381   cx_num = LSBMSB16(sp[2]);
1382 
1383   vw_close_vje_library(cx_num);
1384   err = vw_free_context(cx_num);
1385 
1386   header->type = 0x05;
1387   header->extra = 0;
1388   header->datalen = LSBMSB16(1);
1389   header->err.e8 = err;
1390 
1391   return 1;
1392 }
1393 
1394 
1395 
vjewrapper_define_word(int id,buffer_t * cbuf)1396 int vjewrapper_define_word(int id, buffer_t *cbuf)
1397 {
1398   VW_ERROR8(cbuf->buf);
1399 
1400   return 0;
1401 }
1402 
vjewrapper_delete_word(int id,buffer_t * cbuf)1403 int vjewrapper_delete_word(int id, buffer_t *cbuf)
1404 {
1405   VW_ERROR8(cbuf->buf);
1406 
1407   return 0;
1408 }
1409 
vjewrapper_begin_convert(int id,buffer_t * cbuf)1410 int vjewrapper_begin_convert(int id, buffer_t *cbuf)
1411 {
1412   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1413   ushort *sp = (ushort *)cbuf->buf;
1414   int *ip = (int *)cbuf->buf;
1415   int len, datalen, koho_len, cmode;
1416   short cx_num;
1417   context_t *cx;
1418 
1419   short vje_id, mode = 3;
1420 
1421   uchar bufeuc[IN_BUF_SIZE];
1422   uchar in_buf[IN_BUF_SIZE], keybuf[IN_BUF_SIZE];
1423   ushort atrbuf[IN_BUF_SIZE * 2], wcs[IN_BUF_SIZE * 2];
1424 
1425   cx_num = LSBMSB16(sp[4]);
1426   cmode = LSBMSB32(ip[1]);
1427 
1428   cx = vw_get_context(cx_num);
1429 
1430   if (cx->vje.id == 0)
1431     vw_open_vje_library(cx_num); /* ���Ԥ����� cx->vje.id �� 0 �Τޤ� */
1432 
1433   if (vw_check16(cbuf)) return -1;
1434 
1435   len = cannawcstrlen((ushort *)&(cbuf->buf[10]));
1436   len = cannawc2euc((ushort *)&(cbuf->buf[10]), len, bufeuc, IN_BUF_SIZE);
1437 
1438   len = vw_convert_yomi_canna2vje(bufeuc, len);
1439 
1440   len = euc2sjis(bufeuc, len, in_buf + 1, IN_BUF_SIZE - 1);
1441   LEN_OF_OLDBUF(in_buf) = len;
1442 
1443   if (cx->vje.id != 0 && vw_make_vjebuffers(in_buf, atrbuf, keybuf) == 0) {
1444     vje_id = cx->vje.id;
1445 
1446     vje_proto_clear(vje_id);
1447     vje_proto_chg_sdic(vje_id, -1);
1448     vje_proto_set_koho_bno(vje_id, 0);
1449     vje_proto25_set_kihonbuff(vje_id, in_buf, atrbuf, keybuf);
1450     vje_proto25_henkanb(vje_id, in_buf, atrbuf, keybuf, &mode, 0);
1451 
1452     if (vw_check16(cbuf)) return -1;
1453 
1454     koho_len = vw_after_henkan(cx_num, in_buf, wcs, 0);
1455 
1456     if (vw_check16(cbuf)) return -1;
1457 
1458     cx->canna_mode = cmode;
1459 
1460     datalen = koho_len + 2;
1461 
1462     buffer_check(cbuf, 4 + datalen);
1463     header = (cannaheader_t *)cbuf->buf;
1464     sp = (ushort *)cbuf->buf;
1465 
1466     header->type = 0x0f;
1467     header->extra = 0;
1468     header->datalen = LSBMSB16(datalen);
1469     sp[2] = LSBMSB16(cx->max_bun);
1470     memcpy(&(sp[3]), wcs, koho_len);
1471   } else {
1472     header->type = 0x0f;
1473     header->extra = 0;
1474     header->datalen = LSBMSB16(2);
1475     header->err.e16 = LSBMSB16(-1);
1476   }
1477 
1478   return 1;
1479 }
1480 
vjewrapper_end_convert(int id,buffer_t * cbuf)1481 int vjewrapper_end_convert(int id, buffer_t *cbuf)
1482 {
1483   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1484   short *sp = (short *)cbuf->buf;
1485   int *ip = (int *)cbuf->buf;
1486   int i, koho_no;
1487   short cx_num;
1488   char err;
1489   context_t *cx;
1490 
1491   char tmpbuf[IN_BUF_SIZE];
1492 
1493   cx_num = LSBMSB16(sp[2]);
1494   cx = vw_get_context(cx_num);
1495 
1496   err = (cx->vje.id) ? 0 : -1;
1497 
1498   if (ip[2] != 0 && err == 0) {
1499     for (i = 0; i < cx->max_bun; i++) {
1500       vw_move_bunsetu(cx, i);
1501 
1502       koho_no = LSBMSB16(sp[6 + i]);
1503 
1504       vw_set_koho(cx_num, i, koho_no);
1505     }
1506 
1507     if (vw_check8(cbuf)) return -1;
1508 
1509     i = vje_proto25_kakutei1(cx->vje.id, cx->yomi_buffer, tmpbuf);
1510 
1511     if (vw_check8(cbuf)) return -1;
1512   }
1513 
1514   vw_clear_context(cx_num);
1515 
1516   header->type = 0x10;
1517   header->extra = 0;
1518   header->datalen = LSBMSB16(1);
1519   header->err.e8 = err;
1520 
1521   return 1;
1522 }
1523 
vjewrapper_get_candidacy_list(int id,buffer_t * cbuf)1524 int vjewrapper_get_candidacy_list(int id, buffer_t *cbuf)
1525 {
1526   context_t *cx;
1527   cannaheader_t *header;
1528   short *sp = (short *)cbuf->buf;
1529   int bun_no, koho_num, koholen;
1530   char *koho_inf;
1531   short size, cx_num;
1532 
1533   cx_num = LSBMSB16(sp[2]);
1534   bun_no = LSBMSB16(sp[3]);
1535 
1536   cx = vw_get_context(cx_num);
1537 
1538   if (cx->vje.id == 0) {
1539     VW_ERROR16(cbuf->buf);
1540     return 1;
1541   }
1542 
1543   /* KOHO */
1544 
1545   if (cx->bun[bun_no].koho == NULL) {
1546     cx->bun[bun_no].koho = vw_get_koho_list(cx, bun_no, &koho_num, &koholen,
1547 					    &koho_inf);
1548     cx->bun[bun_no].koho_inf = koho_inf;
1549     cx->bun[bun_no].koho_num = koho_num;
1550     cx->bun[bun_no].koho_len = koholen;
1551   } else {
1552     koho_num = cx->bun[bun_no].koho_num;
1553     koholen = cx->bun[bun_no].koho_len;
1554   }
1555 
1556   size = koholen;
1557 
1558   if (vw_check16(cbuf)) return -1;
1559 
1560   buffer_check(cbuf, size + 6);
1561   sp = (short *)cbuf->buf;
1562   header = (cannaheader_t *)cbuf->buf;
1563 
1564   memcpy(&(cbuf->buf[6]), cx->bun[bun_no].koho, size);
1565 
1566   sp[2] = LSBMSB16(koho_num);
1567 
1568   size += 2;
1569 
1570   header->type = 0x11;
1571   header->extra = 0;
1572   header->datalen = LSBMSB16(size);
1573 
1574   return 1;
1575 }
1576 
vjewrapper_get_yomi(int id,buffer_t * cbuf)1577 int vjewrapper_get_yomi(int id, buffer_t *cbuf)
1578 {
1579   cannaheader_t *header;
1580   short *sp = (short *)cbuf->buf;
1581   int size, bun_no, slen;
1582   uchar euc[YOMI_BUF_SIZE];
1583   ushort wcs[YOMI_BUF_SIZE];
1584   short cx_num, si;
1585   context_t *cx;
1586 
1587   cx_num = LSBMSB16(sp[2]);
1588   bun_no = LSBMSB16(sp[3]);
1589 
1590   cx = vw_get_context(cx_num);
1591 
1592   slen = vw_get_yomi(cx, bun_no, euc);
1593   slen = euc2cannawc(euc, slen, wcs, YOMI_BUF_SIZE);
1594   slen *= 2;
1595 
1596   size = 2 + slen + 2;
1597 
1598   buffer_check(cbuf, size + 4);
1599   sp = (short *)cbuf->buf;
1600   header = (cannaheader_t *)cbuf->buf;
1601 
1602   memcpy(&(cbuf->buf[6]), wcs, slen);
1603   cbuf->buf[6 + slen] = 0; cbuf->buf[6 + slen + 1] = 0;
1604 
1605   header->type = 0x12;
1606   header->extra = 0;
1607   header->datalen = LSBMSB16(size);
1608 
1609   si = slen;
1610   sp[2] = LSBMSB16(si);
1611 
1612   return 1;
1613 }
1614 
vjewrapper_subst_yomi(int id,buffer_t * cbuf)1615 int vjewrapper_subst_yomi(int id, buffer_t *cbuf)
1616 {
1617   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1618   ushort *sp = (ushort *)cbuf->buf;
1619   short cx_num, start, end, yomilen, mode = 0;
1620   int newyomipnt, size, errflag = 0, koho_len, datalen, cvt, i;
1621   ushort *yomi;
1622   uchar euc[IN_BUF_SIZE], sjis[IN_BUF_SIZE], keybuf[IN_BUF_SIZE];
1623   uchar in_buf[IN_BUF_SIZE];
1624   ushort atrbuf[IN_BUF_SIZE * 2], wcs[IN_BUF_SIZE * 2];
1625   context_t *cx;
1626 
1627   cx_num = LSBMSB16(sp[2]);
1628   start = LSBMSB16(sp[3]);
1629   end = LSBMSB16(sp[4]);
1630   yomilen = LSBMSB16(sp[5]);
1631   yomi = (ushort *)&(sp[6]);
1632 
1633   cx = vw_get_context(cx_num);
1634 
1635   memset(in_buf, 0, IN_BUF_SIZE);
1636   memcpy(in_buf, cx->yomi_buffer, LEN_OF_OLDBUF(cx->yomi_buffer) + 1);
1637 
1638   /* �Ѵ�����Ƥ����ɤߤ�Ĺ������� */
1639   for (i = cvt = 0; i < cx->max_bun; i++)
1640     cvt += cx->bun[i].yomi_len;
1641 
1642   if ((newyomipnt = vw_expr_yomi_start(start, &in_buf[1 + cvt])) < 0)
1643     errflag = 1;
1644 
1645   if (errflag == 0) {
1646     if (yomilen > 0) {
1647       cx->subst_yomi_zero_flag = 0;
1648 
1649       if (in_buf[1 + cvt + newyomipnt]) {
1650 	in_buf[1 + cvt + newyomipnt] = 0;
1651 	LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1652 
1653 	vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1654 	vje_proto25_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1655 
1656 	/* �Хå����ڡ������줿�Ȥ��� mode = 2 �ˤʤ�ߤ��� */
1657 	mode = 2;
1658       }
1659 
1660       cannawc2euc(yomi, yomilen, euc, IN_BUF_SIZE);
1661       m_msg_dbg("newyomi = %s/%d\n", euc, yomilen);
1662       euc2sjis(euc, strlen(euc), sjis, IN_BUF_SIZE);
1663 
1664       size = IN_BUF_SIZE - newyomipnt - cvt - 1;
1665 
1666       strncpy(&(in_buf[newyomipnt + cvt + 1]), sjis, size);
1667 
1668       in_buf[IN_BUF_SIZE - 1] = 0;
1669       LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1670     } else {
1671       cx->subst_yomi_zero_flag = 1;
1672 
1673       in_buf[newyomipnt + cvt + 1] = 0;
1674       LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1675     }
1676 
1677     sjis2euc(in_buf + 1, in_buf[0], euc, IN_BUF_SIZE);
1678     m_msg_dbg("yomi = [%s]/%d\n", euc, in_buf[0]);
1679 
1680     vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1681 
1682     vje_proto25_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1683 
1684     if (cx->subst_yomi_zero_flag == 0) {
1685       /* ����ñ�˥Хå����ڡ������줿����, henkanb ��ƤФʤ��äݤ���*/
1686       vje_proto25_henkanb(cx->vje.id, in_buf, atrbuf, keybuf, &mode, cvt);
1687     }
1688 
1689     if (vw_check16(cbuf)) return -1;
1690 
1691     koho_len = vw_after_henkan(cx_num, in_buf, wcs, 0);
1692 
1693     if (vw_check16(cbuf)) return -1;
1694 
1695     datalen = koho_len + 2;
1696 
1697     buffer_check(cbuf, 4 + datalen);
1698     header = (cannaheader_t *)cbuf->buf;
1699     sp =(ushort *)cbuf->buf;
1700 
1701     header->type = 0x13;
1702     header->extra = 0;
1703     header->datalen = LSBMSB16(datalen);
1704     sp[2] = LSBMSB16(cx->max_bun);
1705     memcpy(&sp[3], wcs, koho_len);
1706   } else {
1707     header->type = 0x13;
1708     header->extra = 0;
1709     header->datalen = LSBMSB16(2);
1710     header->err.e16 = LSBMSB16(-1);
1711   }
1712 
1713   return 1;
1714 }
1715 
vjewrapper_store_yomi(int id,buffer_t * cbuf)1716 int vjewrapper_store_yomi(int id, buffer_t *cbuf)
1717 {
1718   cannaheader_t *header;
1719   ushort *sp = (ushort *)cbuf->buf;
1720   short cx_num, bun_no, datalen;
1721   ushort *yomi;
1722   int len, koho_len, pnt, i;
1723   context_t *cx;
1724 
1725 #if 0
1726   buninf_t bi;
1727 #endif
1728 
1729   uchar euc[IN_BUF_SIZE], sjis[IN_BUF_SIZE];
1730   uchar in_buf[IN_BUF_SIZE], keybuf[IN_BUF_SIZE];
1731   ushort atrbuf[IN_BUF_SIZE * 2], wcs[IN_BUF_SIZE * 2];
1732 
1733   datalen = LSBMSB16(sp[1]);
1734   cx_num = LSBMSB16(sp[2]);
1735   bun_no = LSBMSB16(sp[3]);
1736 
1737   /* ����ʥ饤�֥��ΥХ��� �ѹ�����ɤߤ�ѥ��åȤ˴ޤ�ʤ�����Τˡ�
1738      �ѹ�����ɤߤ��̥�ʤ�ʤˤ�ޤޤ�ʤ���
1739      (Terminate Char �Ǥ��� 0x0000 �����Ϥ��ʤ���) �����б��� */
1740   if (datalen > 4)
1741     yomi = &sp[4];
1742   else
1743     yomi = NULL;
1744 
1745   cx = vw_get_context(cx_num);
1746 
1747   if (yomi) {
1748     len = cannawc2euc(yomi, cannawcstrlen(yomi), euc, IN_BUF_SIZE);
1749     len = euc2sjis(euc, len, sjis, IN_BUF_SIZE);
1750   } else {
1751     len = 0;
1752   }
1753 
1754 #if 0
1755 
1756   /* ��̩�ˤϤ��Υ����ɤ�ɬ�פ����Τ�ʤ������ʤ��Ƥ�ޤȤ��ư���� */
1757 
1758   memset(in_buf, 0, IN_BUF_SIZE);
1759 
1760   pnt = 1;
1761   for (i = 0; i < cx->max_bun; i++) {
1762     memcpy(&in_buf[pnt], &cx->yomi_buffer[cx->bun[i].yomi_pnt],
1763 	   cx->bun[i].yomi_len);
1764     pnt += cx->bun[i].yomi_len;
1765   }
1766 
1767   in_buf[pnt] = 0;
1768   LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1769 
1770   vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1771 
1772   vje_proto_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1773 
1774   vje_proto_get_koho_all(cx->vje.id, &bi, sjis);
1775 
1776   vje_proto_set_koho_bno(cx->vje.id, bi.max_bun - 1);
1777   vje_proto_chg_sdic(cx->vje.id, -1);
1778 
1779   vje_proto_get_koho_all(cx->vje.id, &bi, sjis);
1780 
1781 #endif
1782 
1783   vw_move_bunsetu(cx, bun_no);
1784 
1785   memset(in_buf, 0, IN_BUF_SIZE);
1786 
1787   pnt = 1;
1788   for (i = 0; i < cx->max_bun; i++) {
1789     if (i == bun_no) {
1790       if (len > 0) {
1791 	memcpy(&in_buf[pnt], sjis, len);
1792 	pnt += len;
1793       }
1794     } else {
1795       memcpy(&in_buf[pnt], &cx->yomi_buffer[cx->bun[i].yomi_pnt],
1796 	     cx->bun[i].yomi_len);
1797       pnt += cx->bun[i].yomi_len;
1798     }
1799   }
1800 
1801   in_buf[pnt] = 0;
1802   LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1803 
1804   vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1805 
1806   vje_proto25_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1807 
1808   if (len > 0)
1809     vje_proto25_saihenkan(cx->vje.id, in_buf, 1, len, len, 5);
1810   else
1811     vje_proto25_saihenkan(cx->vje.id, in_buf, 1, 0, 0, 0);
1812 
1813   if (vw_check16(cbuf)) return -1;
1814 
1815   koho_len = vw_after_henkan(cx_num, in_buf, wcs, 0);
1816 
1817   if (vw_check16(cbuf)) return -1;
1818 
1819   datalen = koho_len + 2;
1820 
1821   buffer_check(cbuf, 4 + datalen);
1822   header = (cannaheader_t *)cbuf->buf;
1823   sp = (ushort *)cbuf->buf;
1824 
1825   header->type = 0x14;
1826   header->extra = 0;
1827   header->datalen = LSBMSB16(datalen);
1828   sp[2] = LSBMSB16(cx->max_bun);
1829   memcpy(&sp[3], wcs, koho_len);
1830 
1831   return 1;
1832 }
1833 
vjewrapper_store_range(int id,buffer_t * cbuf)1834 int vjewrapper_store_range(int id, buffer_t *cbuf)
1835 {
1836   VW_ERROR8(cbuf->buf);
1837 
1838   return 0;
1839 }
1840 
vjewrapper_get_lastyomi(int id,buffer_t * cbuf)1841 int vjewrapper_get_lastyomi(int id, buffer_t *cbuf)
1842 {
1843   cannaheader_t *header;
1844   ushort *sp = (ushort *)cbuf->buf;
1845   short cx_num, maxbun, datalen;
1846   int len, cvt, i;
1847   uchar *yomi, euc[IN_BUF_SIZE];
1848   ushort wcs[IN_BUF_SIZE];
1849   context_t *cx;
1850 
1851   cx_num = LSBMSB16(sp[2]);
1852   cx = vw_get_context(cx_num);
1853 
1854   maxbun = cx->max_bun - 1;
1855 
1856   for (i = cvt = 0; i < cx->max_bun; i++)
1857     cvt += cx->bun[i].yomi_len;
1858 
1859   i = cx->bun[maxbun].yomi_pnt + cx->bun[maxbun].yomi_len;
1860 
1861   yomi = &cx->yomi_buffer[i];
1862 
1863   len = sjis2euc(yomi, strlen(yomi), euc, IN_BUF_SIZE);
1864 
1865   m_msg_dbg("lastyomi = [%s]\n", euc);
1866 
1867   len = euc2cannawc(euc, len, wcs, IN_BUF_SIZE);
1868 
1869   datalen = 2 + len * 2 + 2;
1870 
1871   buffer_check(cbuf, datalen + 4);
1872   header = (cannaheader_t *)cbuf->buf;
1873   sp = (ushort *)cbuf->buf;
1874 
1875   header->type = 0x16;
1876   header->extra = 0;
1877   header->datalen = LSBMSB16(datalen);
1878   sp[2] = LSBMSB16(len);
1879   memcpy(&sp[3], wcs, len * 2 + 2);
1880 
1881   return 1;
1882 }
1883 
vjewrapper_flush_yomi(int id,buffer_t * cbuf)1884 int vjewrapper_flush_yomi(int id, buffer_t *cbuf)
1885 {
1886   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1887   ushort *sp = (ushort *)cbuf->buf;
1888   short cx_num, mode = 1, datalen;
1889   int i, cvt, koho_len;
1890   context_t *cx;
1891 
1892   uchar in_buf[IN_BUF_SIZE], keybuf[IN_BUF_SIZE];
1893   ushort atrbuf[IN_BUF_SIZE * 2], wcs[IN_BUF_SIZE * 2];
1894 
1895   cx_num = LSBMSB16(sp[2]);
1896   cx = vw_get_context(cx_num);
1897 
1898   if (cx->subst_yomi_zero_flag)
1899     mode = 3;
1900 
1901   memcpy(in_buf, cx->yomi_buffer, IN_BUF_SIZE);
1902 
1903   vw_make_vjebuffers(in_buf, atrbuf, keybuf);
1904 
1905   /* �ɤ���� bno �ˤ��Ѵ�����Ƥ���ʸ������Ϥ��餷��(��ư�Ѵ��ΤȤ��Τ�) */
1906   vje_proto_set_koho_bno(cx->vje.id, cx->max_bun);
1907 
1908   /* �Ѵ�����Ƥ����ɤߤ�Ĺ������� */
1909   for (i = cvt = 0; i < cx->max_bun; i++)
1910     cvt += cx->bun[i].yomi_len;
1911 
1912   vje_proto_chg_sdic(cx->vje.id, -1);
1913   vje_proto25_set_kihonbuff(cx->vje.id, in_buf, atrbuf, keybuf);
1914   vje_proto25_henkanb(cx->vje.id, in_buf, atrbuf, keybuf, &mode, cvt);
1915 
1916   if (vw_check16(cbuf)) return -1;
1917 
1918   koho_len = vw_after_henkan(cx_num, in_buf, wcs, 0);
1919 
1920   if (vw_check16(cbuf)) return -1;
1921 
1922   cannawc2euc(wcs, cannawcstrlen(wcs), in_buf, IN_BUF_SIZE);
1923 
1924   datalen = koho_len + 2;
1925 
1926   buffer_check(cbuf, 4 + datalen);
1927   header = (cannaheader_t *)cbuf->buf;
1928   sp = (ushort *)cbuf->buf;
1929 
1930   header->type = 0x17;
1931   header->extra = 0;
1932   header->datalen = LSBMSB16(datalen);
1933   sp[2] = LSBMSB16(cx->max_bun);
1934   memcpy(&sp[3], wcs, koho_len);
1935 
1936   return 1;
1937 }
1938 
vjewrapper_remove_yomi(int id,buffer_t * cbuf)1939 int vjewrapper_remove_yomi(int id, buffer_t *cbuf)
1940 {
1941   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
1942   short *sp = (short *)cbuf->buf;
1943   int *ip = (int *)cbuf->buf;
1944   int i, koho_no, bun_no, len, pnt;
1945   short cx_num, datalen;
1946   char err;
1947   context_t *cx;
1948 
1949   char tmpbuf[IN_BUF_SIZE], in_buf[HIRA_BUF_SIZE], euc[IN_BUF_SIZE];
1950   ushort wcs[IN_BUF_SIZE * 2];
1951 
1952   datalen = LSBMSB16(sp[1]);
1953   cx_num = LSBMSB16(sp[2]);
1954   bun_no = LSBMSB16(sp[3]);
1955 
1956   cx = vw_get_context(cx_num);
1957 
1958   err = (cx->vje.id) ? 0 : -1;
1959 
1960   if (err == 0) {
1961     vje_proto_set_koho_bno(cx->vje.id, cx->max_bun - 1);
1962 
1963     if (ip[2] != 0) {
1964       for (i = 0; i <= bun_no; i++) {
1965 	if (datalen >= 8 + (i + 1) * 2) {
1966 	  vw_move_bunsetu(cx, i);
1967 
1968 	  koho_no = LSBMSB16(sp[6 + i]);
1969 
1970 	  vw_set_koho(cx_num, i, koho_no);
1971 	}
1972       }
1973 
1974       if (vw_check8(cbuf)) return -1;
1975     }
1976 
1977     vw_move_bunsetu(cx, bun_no);
1978 
1979     memcpy(in_buf, cx->yomi_buffer, HIRA_BUF_SIZE);
1980 
1981     i = vje_proto25_kakutei2(cx->vje.id, in_buf, tmpbuf);
1982 
1983     if (vw_check8(cbuf)) return -1;
1984 
1985     pnt = cx->bun[bun_no].yomi_pnt + cx->bun[bun_no].yomi_len;
1986     len = LEN_OF_OLDBUF(in_buf) - pnt + 1 + 1;
1987 
1988     memmove(&in_buf[1], &in_buf[pnt], len);
1989     LEN_OF_OLDBUF(in_buf) = strlen(&in_buf[1]);
1990 
1991     sjis2euc(in_buf + 1, in_buf[0], euc, IN_BUF_SIZE);
1992 
1993     vw_after_henkan(cx_num, in_buf, wcs, 0);
1994   }
1995 
1996   header->type = 0x18;
1997   header->extra = 0;
1998   header->datalen = LSBMSB16(1);
1999   header->err.e8 = cx->max_bun;
2000 
2001   return 1;
2002 }
2003 
vjewrapper_get_simplekanji(int id,buffer_t * cbuf)2004 int vjewrapper_get_simplekanji(int id, buffer_t *cbuf)
2005 {
2006   VW_ERROR16(cbuf->buf);
2007 
2008   return 0;
2009 }
2010 
vjewrapper_resize_pause(int id,buffer_t * cbuf)2011 int vjewrapper_resize_pause(int id, buffer_t *cbuf)
2012 {
2013   int i, j, vjeyomilen = 0, bun_no, cx_num, koho_len, datalen;
2014   short cannayomilen;
2015   int nbun, len, len2;
2016   short *sp = (short *)cbuf->buf;
2017   cannaheader_t *header;
2018   context_t *cx;
2019   uchar in_buf[IN_BUF_SIZE];
2020   ushort wcs[IN_BUF_SIZE * 2];
2021 
2022   buninf25_t bi;
2023 
2024   cx_num = LSBMSB16(sp[2]);
2025   bun_no = LSBMSB16(sp[3]);
2026   cannayomilen = LSBMSB16(sp[4]);
2027 
2028   cx = vw_get_context(cx_num);
2029 
2030   if (cx->vje.id == 0) {
2031     VW_ERROR16(cbuf->buf);
2032     return 1;
2033   }
2034 
2035   vje_proto25_get_koho_all(cx->vje.id, &bi, in_buf);
2036 
2037   if (cannayomilen == -1) { /* ʸ�῭�Ф� */
2038     if (bun_no + 1 >= cx->max_bun)
2039       vjeyomilen = cx->bun[bun_no].yomi_len;
2040     else {
2041       vjeyomilen = cx->bun[bun_no].yomi_len;
2042       if (ISSJISKANJI1(cx->yomi_buffer[cx->bun[bun_no + 1].yomi_pnt]))
2043 	vjeyomilen += 2;
2044       else
2045 	vjeyomilen++;
2046     }
2047   } else if (cannayomilen == -2) { /* ʸ��̤� */
2048     for (i = j = 0; i < cx->bun[bun_no].yomi_len;) {
2049       if (ISSJISKANJI1(cx->yomi_buffer[cx->bun[bun_no].yomi_pnt + i]))
2050 	j = 2;
2051       else
2052 	j = 1;
2053       i += j;
2054     }
2055 
2056     vjeyomilen = cx->bun[bun_no].yomi_len - j;
2057     if (vjeyomilen <= 0)
2058       vjeyomilen = cx->bun[bun_no].yomi_len;
2059   } else if (cannayomilen > 0) {
2060     j = vjeyomilen = 0;
2061     while (j != cannayomilen) {
2062       if (ISSJISKANJI1(cx->yomi_buffer[cx->bun[bun_no].yomi_pnt + vjeyomilen]))
2063 	vjeyomilen += 2;
2064       else
2065 	vjeyomilen++;
2066       j++;
2067     }
2068   }
2069 
2070   if (vjeyomilen > 0) {
2071     i = bun_no; len2 = 0;
2072     for (;;) {
2073       len2 += cx->bun[i].yomi_len;
2074 
2075       if (vjeyomilen <= len2)
2076 	break;
2077 
2078       i++;
2079     }
2080 
2081     nbun = 1 + i - bun_no;
2082     len = vjeyomilen;
2083     memcpy(in_buf, cx->yomi_buffer, IN_BUF_SIZE);
2084 
2085     vw_move_bunsetu(cx, bun_no);
2086 
2087     vje_proto25_saihenkan(cx->vje.id, in_buf, nbun, len2, len, 5);
2088 
2089     if (vw_check16(cbuf)) return -1;
2090 
2091     koho_len = vw_after_henkan(cx_num, in_buf, wcs, bun_no);
2092 
2093     if (vw_check16(cbuf)) return -1;
2094 
2095     datalen = koho_len + 2;
2096 
2097     buffer_check(cbuf, datalen + 4);
2098     header = (cannaheader_t *)cbuf->buf;
2099     sp = (short *)cbuf->buf;
2100 
2101     header->type = 0x1a;
2102     header->extra = 0;
2103     header->datalen = LSBMSB16(datalen);
2104     sp[2] = LSBMSB16(cx->max_bun);
2105     memcpy(&(sp[3]), wcs, koho_len);
2106   } else {
2107     VW_ERROR16(cbuf->buf);
2108   }
2109 
2110   return 1;
2111 }
2112 
vjewrapper_get_hinshi(int id,buffer_t * cbuf)2113 int vjewrapper_get_hinshi(int id, buffer_t *cbuf)
2114 {
2115   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
2116 
2117   header->type = 0x1b;
2118   header->extra = 0;
2119   header->datalen = LSBMSB16(1);
2120   header->err.e8 = 0;
2121 
2122   return 1;
2123 }
2124 
vjewrapper_get_lex(int id,buffer_t * cbuf)2125 int vjewrapper_get_lex(int id, buffer_t *cbuf)
2126 {
2127   VW_ERROR16(cbuf->buf);
2128 
2129   return 0;
2130 }
2131 
vjewrapper_get_status(int id,buffer_t * cbuf)2132 int vjewrapper_get_status(int id, buffer_t *cbuf)
2133 {
2134   struct {
2135     int bunnum;
2136     int candnum;
2137     int maxcand;
2138     int diccand;
2139     int ylen;
2140     int klen;
2141     int tlen;
2142   } stat;
2143 
2144   short bun_no, koho_no, cx_num;
2145   short *sp = (short *)cbuf->buf;
2146   int i, koho_num, koholen;
2147   cannaheader_t *header;
2148   context_t *cx;
2149   uchar euc[IN_BUF_SIZE];
2150   ushort wcs[IN_BUF_SIZE];
2151   char *koho_inf;
2152 
2153   cx_num = LSBMSB16(sp[2]);
2154   bun_no = LSBMSB16(sp[3]);
2155   koho_no = LSBMSB16(sp[4]);
2156 
2157   cx = vw_get_context(cx_num);
2158 
2159   if (cx->vje.id == 0) {
2160     VW_ERROR8(cbuf->buf);
2161     return 1;
2162   }
2163 
2164   i = vw_get_yomi(cx, bun_no, euc);
2165   i = euc2cannawc(euc, i, wcs, IN_BUF_SIZE);
2166   stat.ylen = LSBMSB32(i); /* �����ȸ�����ɤߤ��ʤΥХ��ȿ� */
2167 
2168   if (cx->bun[bun_no].koho == NULL) {
2169     /* �ޤ������������Ƥʤ��ʤ�������� */
2170     cx->bun[bun_no].koho = vw_get_koho_list(cx, bun_no, &koho_num, &koholen,
2171 					    &koho_inf);
2172     cx->bun[bun_no].koho_inf = koho_inf;
2173     cx->bun[bun_no].koho_num = koho_num;
2174     cx->bun[bun_no].koho_len = koholen;
2175   }
2176 
2177   i = cannawcstrlen(vw_retrieve_koho(cx_num, bun_no, koho_no));
2178 
2179   stat.klen = LSBMSB32(i); /* �����ȸ���δ�������ΥХ��ȿ� */
2180 
2181   stat.tlen = LSBMSB32(1); /* �����ȸ���ι���ñ��� */
2182 
2183   i = cx->bun[bun_no].koho_num;
2184 
2185   stat.maxcand = LSBMSB32(i); /* ������ʸ��θ���� */
2186 
2187   i -= m_count_canna_mode(cx->canna_mode);
2188 
2189   stat.diccand = LSBMSB32(i); /* maxcand - �⡼�ɻ���ʬ */
2190 
2191   stat.bunnum = LSBMSB32(bun_no); /* ������ʸ���ֹ� */
2192   stat.candnum = LSBMSB32(koho_no); /* �����ȸ����ֹ� */
2193 
2194   if (vw_check8(cbuf)) return -1;
2195 
2196   buffer_check(cbuf, 33);
2197   header = (cannaheader_t *)cbuf->buf;
2198 
2199   header->type = 0x1d;
2200   header->extra = 0;
2201   header->datalen = LSBMSB16(29);
2202 
2203   cbuf->buf[4] = 0;
2204 
2205   memcpy(&(cbuf->buf[5]), (char *)&stat, 28);
2206 
2207   return 1;
2208 }
2209 
vjewrapper_set_locale(int id,buffer_t * cbuf)2210 int vjewrapper_set_locale(int id, buffer_t *cbuf)
2211 {
2212   VW_ERROR8(cbuf->buf);
2213 
2214   return 0;
2215 }
2216 
vjewrapper_auto_convert(int id,buffer_t * cbuf)2217 int vjewrapper_auto_convert(int id, buffer_t *cbuf)
2218 {
2219   cannaheader_t *header = (cannaheader_t *)cbuf->buf;
2220   short *sp = (short *)cbuf->buf;
2221   int *ip = (int *)cbuf->buf;
2222   int cmode;
2223   short cx_num;
2224   context_t *cx;
2225 
2226   cx_num = LSBMSB16(sp[2]);
2227   cmode = LSBMSB32(ip[2]);
2228 
2229   cx = vw_get_context(cx_num);
2230 
2231   if (vw_check8(cbuf)) return -1;
2232 
2233   if (cx->vje.id == 0)
2234     vw_open_vje_library(cx_num); /* ���Ԥ����� cx->vje.id �� 0 �Τޤ� */
2235 
2236   if (vw_check16(cbuf)) return -1;
2237 
2238   if (cx->vje.id) {
2239     vje_proto_clear(cx->vje.id);
2240 
2241     vw_clear_context(cx_num);
2242 
2243     cx->subst_yomi_zero_flag = 0;
2244     cx->canna_mode = cmode;
2245 
2246     /* ɬ�פʤ餳���ǥ���ƥ����Ȥ˥ե饰���Ƥ� */
2247 
2248     header->datalen = LSBMSB16(1);
2249     header->err.e8 = 0;
2250   } else {
2251     header->datalen = LSBMSB16(1);
2252     header->err.e8 = -1;
2253   }
2254 
2255   return 1;
2256 }
2257 
2258 
2259