1/* Copyright 1992 NEC Corporation, Tokyo, Japan.
2 *
3 * Permission to use, copy, modify, distribute and sell this software
4 * and its documentation for any purpose is hereby granted without
5 * fee, provided that the above copyright notice appear in all copies
6 * and that both that copyright notice and this permission notice
7 * appear in supporting documentation, and that the name of NEC
8 * Corporation not be used in advertising or publicity pertaining to
9 * distribution of the software without specific, written prior
10 * permission.  NEC Corporation makes no representations about the
11 * suitability of this software for any purpose.  It is provided "as
12 * is" without express or implied warranty.
13 *
14 * NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
16 * NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19 * OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23/************************************************************************/
24/* THIS SOURCE CODE IS MODIFIED FOR TKO BY T.MURAI 1997
25/************************************************************************/
26
27
28#if !defined(lint) && !defined(__CODECENTER__)
29static char rcs_id[] = "@(#) 102.1 $Id: ulmount.c 14875 2005-11-12 21:25:31Z bonefish $";
30#endif
31
32#ifndef NO_EXTEND_MENU
33#include	<errno.h>
34#include 	"canna.h"
35#include "RK.h"
36#include "RKintern.h"
37
38static mountContext newMountContext(void);
39static void freeMountContext(mountContext mc);
40static struct dicname *findDic(char *s);
41static int uuMountExitCatch(uiContext d, int retval, mode_context env);
42static int uuMountQuitCatch(uiContext d, int retval, mode_context env);
43static int getDicList(uiContext d);
44
45/* cfunc mountContext
46 *
47 * mountContext
48 *
49 */
50static mountContext
51newMountContext(void)
52{
53  mountContext mcxt;
54
55  if ((mcxt = (mountContext)calloc(1, sizeof(mountContextRec)))
56                                           == (mountContext)NULL) {
57#ifndef WIN
58    jrKanjiError = "malloc (newMountContext) ��������������������������������";
59#else
60    jrKanjiError = "malloc (newMountContext) \244\307\244\255\244\336\244\273"
61	"\244\363\244\307\244\267\244\277";
62#endif
63    return (mountContext)NULL;
64  }
65  mcxt->id = MOUNT_CONTEXT;
66
67  return mcxt;
68}
69
70static void
71freeMountContext(mountContext mc)
72{
73  if (mc) {
74    if (mc->mountList) {
75      if (*(mc->mountList)) {
76	free(*(mc->mountList));
77      }
78      free(mc->mountList);
79    }
80    if (mc->mountOldStatus) {
81      free(mc->mountOldStatus);
82    }
83    if (mc->mountNewStatus) {
84      free(mc->mountNewStatus);
85    }
86    free(mc);
87  }
88}
89
90/*
91 * ��������������������������������
92 */
93int getMountContext(uiContext d)
94{
95  mountContext mc;
96  int retval = 0;
97
98  if (pushCallback(d, d->modec,
99                   NO_CALLBACK, NO_CALLBACK,
100                   NO_CALLBACK, NO_CALLBACK) == 0) {
101#ifndef WIN
102    jrKanjiError = "malloc (pushCallback) ��������������������������������";
103#else
104    jrKanjiError = "malloc (pushCallback) \244\307\244\255\244\336\244\273"
105	"\244\363\244\307\244\267\244\277";
106#endif
107    return(NG);
108  }
109
110  if((mc = newMountContext()) == (mountContext)NULL) {
111    popCallback(d);
112    return(NG);
113  }
114  mc->majorMode = d->majorMode;
115  mc->next = d->modec;
116  d->modec = (mode_context)mc;
117
118  mc->prevMode = d->current_mode;
119
120  return(retval);
121}
122
123void
124popMountMode(uiContext d)
125{
126  mountContext mc = (mountContext)d->modec;
127
128  d->modec = mc->next;
129  d->current_mode = mc->prevMode;
130  freeMountContext(mc);
131}
132
133static struct dicname *
134findDic(char *s)
135{
136  extern struct dicname *kanjidicnames;
137  struct dicname *dp;
138
139  for (dp = kanjidicnames ; dp ; dp = dp->next) {
140    if (!strcmp(s, dp->name)) {
141      return dp;
142    }
143  }
144  return (struct dicname *)0;
145}
146
147/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
148 * ��������������������������������������������������������                                              *
149 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
150
151static
152int uuMountExitCatch(uiContext d, int retval, mode_context env)
153/* ARGSUSED */
154{
155  mountContext mc;
156  int i, nmount = 0;
157  extern int defaultContext;
158  struct dicname *dp;
159
160  killmenu(d);
161  popCallback(d); /* OnOff ���������������� */
162
163  if(defaultContext == -1) {
164    if((KanjiInit() != 0) || (defaultContext == -1)) {
165#ifdef STANDALONE
166#ifndef WIN
167      jrKanjiError = "��������������������������������������������";
168#else
169      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271\244\307\244\255\244\336\244\273\244\363";
170#endif
171#else
172#ifndef WIN
173      jrKanjiError = "��������������������������������������������������������������������";
174#else
175      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271\245\265"
176	"\241\274\245\320\244\310\304\314\277\256\244\307\244\255\244\336"
177	"\244\273\244\363";
178#endif
179#endif
180      popMountMode(d);
181      popCallback(d);
182      return(GLineNGReturn(d));
183    }
184  }
185
186  mc = (mountContext)d->modec;
187  for(i=0; mc->mountList[i]; i++) {
188    if(mc->mountOldStatus[i] != mc->mountNewStatus[i]) {
189      if(mc->mountNewStatus[i]) {
190	/* ������������������������ */
191	nmount++;
192	if((retval = RkwMountDic(defaultContext, (char *)mc->mountList[i],
193			    cannaconf.kojin ? PL_ALLOW : PL_INHIBIT)) == NG) {
194	  if (errno == EPIPE) {
195	    jrKanjiPipeError();
196	  }
197	  MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\336\245\246"
198		"\245\363\245\310\244\313\274\272\307\324\244\267\244\336"
199		"\244\267\244\277", 512);
200                       /* �������������������������������������������������������� */
201	}
202	else if(d->contextCache != -1 &&
203	  (retval = RkwMountDic(d->contextCache, (char *)mc->mountList[i],
204			    cannaconf.kojin ? PL_ALLOW : PL_INHIBIT)) == NG) {
205	  if (errno == EPIPE) {
206	    jrKanjiPipeError();
207	  }
208	  MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\336\245\246"
209		"\245\363\245\310\244\313\274\272\307\324\244\267\244\336"
210		"\244\267\244\277", 512);
211                              /* �������������������������������������������������������� */
212	}
213	else { /* �������� */
214	  dp = findDic((char *)mc->mountList[i]);
215	  if (!dp) {
216	    dp = (struct dicname *)malloc(sizeof(struct dicname));
217	    if (dp) {
218	      dp->name = (char *)malloc(strlen((char *)mc->mountList[i]) + 1);
219	      if (dp->name) {
220		/* ���������������������������������������������������������������� */
221		strcpy(dp->name, (char *)mc->mountList[i]);
222		dp->dictype = DIC_PLAIN;
223		/* dp->dicflag = DIC_NOT_MOUNTED; will be rewritten below */
224		dp->next = kanjidicnames;
225		kanjidicnames = dp;
226	      }
227	      else { /* (char *)malloc failed */
228		free(dp);
229		dp = (struct dicname *)0;
230	      }
231	    }
232	  }
233	  if (dp) {
234	    dp->dicflag = DIC_MOUNTED;
235	  }
236	}
237      } else {
238	/* �������������������������������� */
239	nmount++;
240	if((retval = RkwUnmountDic(defaultContext, (char *)mc->mountList[i]))
241	   == NG) {
242	  if (errno == EPIPE) {
243	    jrKanjiPipeError();
244	  }
245	  MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\242\245\363"
246		"\245\336\245\246\245\363\245\310\244\313\274\272\307\324"
247		"\244\267\244\336\244\267\244\277", 512);
248                             /* ���������������������������������������������������������������� */
249	}
250	else if(d->contextCache != -1 &&
251	  (retval = RkwUnmountDic(d->contextCache, (char *)mc->mountList[i]))
252		== NG) {
253	  if (errno == EPIPE) {
254	    jrKanjiPipeError();
255	  }
256	  MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\242\245\363"
257		"\245\336\245\246\245\363\245\310\244\313\274\272\307\324"
258		"\244\267\244\336\244\267\244\277", 512);
259                             /* ���������������������������������������������������������������� */
260	}
261	else {
262	  dp = findDic((char *)mc->mountList[i]);
263	  if (dp) { /* �������������������������������������������� */
264	    dp->dicflag = DIC_NOT_MOUNTED;
265	  }
266	}
267      }
268    }
269  }
270
271  if(nmount)
272    makeAllContextToBeClosed(1);
273
274  if(retval != NG)
275    MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\336\245\246\245\363"
276	"\245\310\241\277\245\242\245\363\245\336\245\246\245\363\245\310"
277	"\244\362\271\324\244\244\244\336\244\267\244\277", 512);
278           /* �������������������������������������������������������������������������������� */
279  else
280    MBstowcs(d->genbuf, "\274\255\275\361\244\316\245\336\245\246\245\363"
281	"\245\310\241\277\245\242\245\363\245\336\245\246\245\363\245\310"
282	"\244\313\274\272\307\324\244\267\244\336\244\267\244\277", 512);
283           /* ������������������������������������������������������������������������������������ */
284  makeGLineMessage(d, d->genbuf, WStrlen(d->genbuf));
285
286  popMountMode(d);
287  popCallback(d);
288  currentModeInfo(d);
289
290  return(0);
291}
292
293static
294int uuMountQuitCatch(uiContext d, int retval, mode_context env)
295/* ARGSUSED */
296{
297  popCallback(d); /* OnOff ���������������� */
298
299  popMountMode(d);
300  popCallback(d);
301  currentModeInfo(d);
302
303  return prevMenuIfExist(d);
304}
305
306/*
307 * dicLbuf                dicLp       soldp   snewp
308 * �������������������������������������������� ��������������������  ������������  ������������
309 * ����iroha\@fuzokugo\@k���� ����*iroha����  ����1 ����  ����1 ����
310 * ����atakana\@satoko\@s���� ����*fuzo ����  ����1 ����  ����1 ����
311 * ����oft\@\@...        ���� ����*kata ����  ����0 ����  ����0 ����
312 * ����                  ���� ����  :   ����  ����: ����  ����: ����
313 * �������������������������������������������� ��������������������  ������������  ������������
314 * dicMbuf                dicMp
315 * �������������������������������������������� ��������������������
316 * ����iroha\@fuzokugo\@s���� ����*iroha����
317 * ����atoko\@\@...      ���� ����*fuzo ����
318 * ����                  ���� ����*sato ����
319 * ����                  ���� ����  :   ����
320 * �������������������������������������������� ��������������������
321 */
322static
323int getDicList(uiContext d)
324{
325  mountContext mc = (mountContext)d->modec;
326  char *dicLbuf, dicMbuf[ROMEBUFSIZE];
327  char **dicLp, *dicMp[ROMEBUFSIZE/2];
328  char *wptr, **Lp, **Mp;
329  BYTE *sop, *snp, *soldp, *snewp;
330  int dicLc, dicMc, i;
331  extern int defaultContext;
332
333  if((dicLbuf = (char *)malloc(ROMEBUFSIZE)) == (char *)NULL) {
334#ifndef WIN
335    jrKanjiError = "malloc (getDicList) ��������������������������������";
336#else
337    jrKanjiError = "malloc (getDicList) \244\307\244\255\244\336\244\273";
338#endif
339    return(NG);
340  }
341  if(defaultContext == -1) {
342    if((KanjiInit() != 0) || (defaultContext == -1)) {
343#ifdef STANDALONE
344#ifndef WIN
345      jrKanjiError = "��������������������������������������������";
346#else
347      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271\244\307\244\255\244\336\244\273\244\363";
348#endif
349#else
350#ifndef WIN
351      jrKanjiError = "��������������������������������������������������������������������";
352#else
353      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271"
354	"\245\265\241\274\245\320\244\310\304\314\277\256\244\307\244\255"
355	"\244\336\244\273\244\363";
356#endif
357#endif
358      free(dicLbuf);
359      return(NG);
360    }
361  }
362  if((dicLc = RkwGetDicList(defaultContext, (char *)dicLbuf, ROMEBUFSIZE))
363     < 0) {
364    if(errno == EPIPE)
365      jrKanjiPipeError();
366    jrKanjiError = "\245\336\245\246\245\363\245\310\262\304\307\275\244\312"
367	"\274\255\275\361\244\316\274\350\244\352\275\320\244\267\244\313"
368	"\274\272\307\324\244\267\244\336\244\267\244\277";
369                   /* ������������������������������������������������������������������������������������ */
370    free(dicLbuf);
371    return(NG);
372  }
373  if (dicLc == 0) {
374    jrKanjiError = "\245\336\245\246\245\363\245\310\262\304\307\275\244\312"
375	"\274\255\275\361\244\254\302\270\272\337\244\267\244\336\244\273"
376	"\244\363";
377                   /* ���������������������������������������������������������������� */
378    free(dicLbuf);
379    return NG;
380  }
381  if((dicLp = (char **)calloc(dicLc + 1, sizeof(char *))) == (char **)NULL) {
382#ifndef WIN
383    jrKanjiError = "malloc (getDicList) ��������������������������������";
384#else
385    jrKanjiError = "malloc (getDicList) \244\307\244\255\244\336\244\273"
386	"\244\363\244\307\244\267\244\277";
387#endif
388    free(dicLbuf);
389    return(NG);
390  }
391  if((soldp = (BYTE *)malloc(dicLc + 1)) == (BYTE *)NULL) {
392#ifndef WIN
393    jrKanjiError = "malloc (getDicList) ��������������������������������";
394#else
395    jrKanjiError = "malloc (getDicList) \244\307\244\255\244\336\244\273"
396	"\244\363\244\307\244\267\244\277";
397#endif
398    free(dicLbuf);
399    free(dicLp);
400    return(NG);
401  }
402  if((snewp = (BYTE *)malloc(dicLc + 1)) == (BYTE *)NULL) {
403#ifndef WIN
404    jrKanjiError = "malloc (getDicList) ��������������������������������";
405#else
406    jrKanjiError = "malloc (getDicList) \244\307\244\255\244\336\244\273"
407	"\244\363\244\307\244\267\244\277";
408#endif
409    free(dicLbuf);
410    free(dicLp);
411    free(soldp);
412    return(NG);
413  }
414  for(i = 0, wptr = dicLbuf; i < dicLc; i++) { /* buf ������������ */
415    dicLp[i] = wptr;
416    while(*wptr++)
417      /* EMPTY */
418      ; /* NULL ��������������������������������NULL ������������������������������������������������ */
419  }
420  dicLp[i] = (char *)NULL;
421
422  if(defaultContext == -1) {
423    if((KanjiInit() != 0) || (defaultContext == -1)) {
424#ifdef STANDALONE
425#ifndef WIN
426      jrKanjiError = "��������������������������������������������";
427#else
428      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271\244\307\244\255\244\336\244\273\244\363";
429#endif
430#else
431#ifndef WIN
432      jrKanjiError = "��������������������������������������������������������������������";
433#else
434      jrKanjiError = "\244\253\244\312\264\301\273\372\312\321\264\271"
435	"\245\265\241\274\245\320\244\310\304\314\277\256\244\307\244\255"
436	"\244\336\244\273\244\363";
437#endif
438#endif
439      free(dicLbuf);
440      free(dicLp);
441      free(soldp);
442      return(NG);
443    }
444  }
445  if((dicMc = RkwGetMountList(defaultContext, (char *)dicMbuf, ROMEBUFSIZE)) <
446     0) {
447    if(errno == EPIPE)
448      jrKanjiPipeError();
449    jrKanjiError = "\245\336\245\246\245\363\245\310\244\267\244\306\244\244"
450	"\244\353\274\255\275\361\244\316\274\350\244\352\275\320\244\267"
451	"\244\313\274\272\307\324\244\267\244\336\244\267\244\277";
452                   /* ���������������������������������������������������������������������������������������� */
453    free(dicLbuf);
454    free(dicLp);
455    free(soldp);
456    free(snewp);
457    return(NG);
458  }
459
460  for(i = 0, wptr = dicMbuf ; i < dicMc ; i++) { /* buf ������������ */
461    dicMp[i] = wptr;
462    while (*wptr++)
463      /* EMPTY */
464      ; /* NULL ��������������������������������NULL ������������������������������������������������ */
465  }
466  dicMp[i] = (char *)NULL;
467
468  for(i=0, sop=soldp, snp=snewp; i<dicLc; i++, sop++, snp++) {
469    *sop = 0;
470    *snp = 0;
471  }
472  for(Lp=dicLp, sop=soldp, snp=snewp; *Lp; Lp++, sop++, snp++) {
473    for(Mp=dicMp; *Mp; Mp++) {
474      if(!strcmp((char *)*Lp, (char *)*Mp)) {
475	*sop = *snp = 1;
476	break;
477      }
478    }
479  }
480  mc->mountList = dicLp;
481  mc->mountOldStatus = soldp;
482  mc->mountNewStatus = snewp;
483
484  return(dicLc);
485}
486
487int dicMount(uiContext d)
488{
489  ichiranContext oc;
490  mountContext mc;
491  int retval = 0, currentkouho = 0, nelem;
492  WCHAR_T *xxxx[100];
493  yomiContext yc = (yomiContext)d->modec;
494
495  if (yc->generalFlags & CANNA_YOMI_CHGMODE_INHIBITTED) {
496    return NothingChangedWithBeep(d);
497  }
498  d->status = 0;
499
500  if((retval = getMountContext(d)) == NG) {
501    killmenu(d);
502    return(GLineNGReturn(d));
503  }
504
505  /* ������������������������������������������������������������������������������������ montContext ������������������������ */
506  if((nelem = getDicList(d)) == NG) {
507    popMountMode(d);
508    popCallback(d);
509    killmenu(d);
510    return(GLineNGReturn(d));
511  }
512
513  mc = (mountContext)d->modec;
514#if defined(DEBUG) && !defined(WIN)
515  if(iroha_debug) {
516    int i;
517
518    printf("<����mount>\n");
519    for(i= 0; mc->mountList[i]; i++)
520      printf("[%s][%x][%x]\n", mc->mountList[i],
521	     mc->mountOldStatus[i], mc->mountNewStatus[i]);
522    printf("\n");
523  }
524#endif
525
526  /* selectOnOff �������������������������������� */
527  mc->curIkouho = currentkouho = 0;
528
529  retval = setWStrings(xxxx, mc->mountList, 0);
530  if (retval == NG) {
531    popMountMode(d);
532    popCallback(d);
533    killmenu(d);
534    return GLineNGReturn(d);
535  }
536  if((retval = selectOnOff(d, xxxx, &mc->curIkouho, nelem,
537		 BANGOMAX, currentkouho, mc->mountOldStatus,
538		 (int(*)(...))NO_CALLBACK, (int(*)(...))uuMountExitCatch,
539		 (int(*)(...))uuMountQuitCatch, (int(*)(...))uiUtilIchiranTooSmall)) == NG) {
540    popMountMode(d);
541    popCallback(d);
542    killmenu(d);
543    return GLineNGReturn(d);
544  }
545
546  oc = (ichiranContext)d->modec;
547  oc->majorMode = CANNA_MODE_ExtendMode;
548  oc->minorMode = CANNA_MODE_MountDicMode;
549  currentModeInfo(d);
550
551  /* ������������������������������������������������������������������������ */
552  if(oc->tooSmall) {
553#ifndef WIN
554    WCHAR_T p[512];
555#else
556    WCHAR_T p[64];
557#endif
558
559    ichiranFin(d);
560    popCallback(d); /* OnOff ���������������� */
561    popMountMode(d);
562    popCallback(d);
563    currentModeInfo(d);
564    MBstowcs(p ,"\274\255\275\361\260\354\315\367\315\321\244\316\311\375"
565		"\244\254\266\271\244\244\244\316\244\307\274\255\275\361"
566		"\245\336\245\246\245\363\245\310\241\277\245\242\245\363"
567		"\245\336\245\246\245\363\245\310\244\307\244\255\244\336"
568		"\244\273\244\363",64);
569         /* ������������������������������������������������������������������������������������������������������������������������ */
570    makeGLineMessage(d, p, WStrlen(p));
571    killmenu(d);
572    return(0);
573  }
574
575  makeGlineStatus(d);
576  /* d->status = ICHIRAN_EVERYTIME; */
577
578  return(retval);
579}
580#endif /* NO_EXTEND_MENU */
581