1 /*
2  */
3 
4 /*
5 WNN7 CLIENT LIBRARY--SOFTWARE LICENSE TERMS AND CONDITIONS
6 
7 
8 Wnn7 Client Library :
9 (C) Copyright OMRON Corporation.       1995,1998,2000,2001 all rights reserved.
10 (C) Copyright OMRON Software Co., Ltd. 1995,1998,2000,2001 all rights reserved.
11 
12 Wnn Software :
13 (C) Copyright Kyoto University Research Institute for Mathematical Sciences
14      1987, 1988, 1989, 1990, 1991, 1992, 1993
15 (C) Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1993
16 (C) Copyright ASCTEC, Inc.  1987, 1988, 1989, 1990, 1991, 1992, 1993
17 
18 Preamble
19 
20 These Wnn7 Client Library--Software License Terms and Conditions
21  (the "License Agreement") shall state the conditions under which you are
22  permitted to copy, distribute or modify the software which can be used
23  to create Wnn7 Client Library (the "Wnn7 Client Library").  The License
24  Agreement can be freely copied and distributed verbatim, however, you
25  shall NOT add, delete or change anything on the License Agreement.
26 
27 OMRON Corporation and OMRON Software Co., Ltd. (collectively referred to
28  as "OMRON") jointly developed the Wnn7 Software (development code name
29  is FI-Wnn), based on the Wnn Software.  Starting from November, 1st, 1998,
30  OMRON publishes the source code of the Wnn7 Client Library, and OMRON
31  permits anyone to copy, distribute or change the Wnn7 Client Library under
32  the License Agreement.
33 
34 Wnn7 Client Library is based on the original version of Wnn developed by
35  Kyoto University Research Institute for Mathematical Sciences (KURIMS),
36  OMRON Corporation and ASTEC Inc.
37 
38 Article 1.  Definition.
39 
40 "Source Code" means the embodiment of the computer code, readable and
41  understandable by a programmer of ordinary skills.  It includes related
42  source code level system documentation, comments and procedural code.
43 
44 "Object File" means a file, in substantially binary form, which is directly
45  executable by a computer after linking applicable files.
46 
47 "Library" means a file, composed of several Object Files, which is directly
48  executable by a computer after linking applicable files.
49 
50 "Software" means a set of Source Code including information on its use.
51 
52 "Wnn7 Client Library" the computer program, originally supplied by OMRON,
53  which can be used to create Wnn7 Client Library.
54 
55 "Executable Module" means a file, created after linking Object Files or
56  Library, which is directly executable by a computer.
57 
58 "User" means anyone who uses the Wnn7 Client Library under the License
59  Agreement.
60 
61 Article 2.  Copyright
62 
63 2.1  OMRON Corporation and OMRON Software Co., Ltd. jointly own the Wnn7
64  Client Library, including, without limitation, its copyright.
65 
66 2.2  Following words followed by the above copyright notices appear
67  in all supporting documentation of software based on Wnn7 Client Library:
68 
69   This software is based on the original version of Wnn7 Client Library
70   developed by OMRON Corporation and OMRON Software Co., Ltd. and also based on
71   the original version of Wnn developed by Kyoto University Research Institute
72   for Mathematical Sciences (KURIMS), OMRON Corporation and ASTEC Inc.
73 
74 Article 3.  Grant
75 
76 3.1  A User is permitted to make and distribute verbatim copies of
77  the Wnn7 Client Library, including verbatim of copies of the License
78  Agreement, under the License Agreement.
79 
80 3.2  A User is permitted to modify the Wnn7 Client Library to create
81  Software ("Modified Software") under the License Agreement.  A User
82  is also permitted to make or distribute copies of Modified Software,
83  including verbatim copies of the License Agreement with the following
84  information.  Upon modifying the Wnn7 Client Library, a User MUST insert
85  comments--stating the name of the User, the reason for the modifications,
86  the date of the modifications, additional terms and conditions on the
87  part of the modifications if there is any, and potential risks of using
88  the Modified Software if they are known--right after the end of the
89  License Agreement (or the last comment, if comments are inserted already).
90 
91 3.3  A User is permitted to create Library or Executable Modules by
92  modifying the Wnn7 Client Library in whole or in part under the License
93  Agreement.  A User is also permitted to make or distribute copies of
94  Library or Executable Modules with verbatim copies of the License
95  Agreement under the License Agreement.  Upon modifying the Wnn7 Client
96  Library for creating Library or Executable Modules, except for porting
97  a computer, a User MUST add a text file to a package of the Wnn7 Client
98  Library, providing information on the name of the User, the reason for
99  the modifications, the date of the modifications, additional terms and
100  conditions on the part of the modifications if there is any, and potential
101  risks associated with using the modified Wnn7 Client Library, Library or
102  Executable Modules if they are known.
103 
104 3.4  A User is permitted to incorporate the Wnn7 Client Library in whole
105  or in part into another Software, although its license terms and
106  conditions may be different from the License Agreement, if such
107  incorporation or use associated with the incorporation does NOT violate
108  the License Agreement.
109 
110 Article 4. Warranty
111 
112 THE WNN7 CLIENT LIBRARY IS PROVIDED BY OMRON ON AN "AS IS" BAISIS.
113   OMRON EXPRESSLY DISLCIAMS ANY AND ALL WRRANTIES, EXPRESS OR IMPLIED,
114  INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY AND FITNESS
115  FOR A PARTICULAR PURPOSE, IN CONNECTION WITH THE WNN7 CLIENT LIBRARY
116  OR THE USE OR OTHER DEALING IN THE WNN7 CLIENT LIBRARY.  IN NO EVENT
117  SHALL OMRON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, PUNITIVE
118  OR CONSEQUENTIAL DAMAGES OF ANY KIND WHATSOEVER IN CONNECTION WITH THE
119  WNN7 CLIENT LIBRARY OR THE USE OR OTHER DEALING IN THE WNN7 CLIENT
120 LIBRARY.
121 
122 ***************************************************************************
123 Wnn7 Client Library :
124 (C) Copyright OMRON Corporation.       1995,1998,2000,2001 all rights reserved.
125 (C) Copyright OMRON Software Co., Ltd. 1995,1998,2000,2001 all rights reserved.
126 
127 Wnn Software :
128 (C) Copyright Kyoto University Research Institute for Mathematical Sciences
129      1987, 1988, 1989, 1990, 1991, 1992, 1993
130 (C) Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1993
131 (C) Copyright ASCTEC, Inc.  1987, 1988, 1989, 1990, 1991, 1992, 1993
132 ***************************************************************************
133 
134 Comments on Modifications:
135 */
136 
137 /*	Version 4.0
138  */
139 /*
140   Binary (Updatable, Stable) dictionary common routine.
141 */
142 
143 #if defined(JS)
144 #ifdef WRITE_CHECK
145 #define BDIC_WRITE_CHECK
146 #endif /* WRITE_CHECK */
147 #endif /* JS */
148 
149 #ifdef UX386
150 #include <X11/Xos.h>
151 #else
152 #include <sys/types.h>
153 #endif
154 #ifndef JS
155 #include <sys/stat.h>
156 #include <stdio.h>
157 #include "commonhd.h"
158 #include "jslib.h"
159 #include "jh.h"
160 #endif
161 #include "jdata.h"
162 #include "wnn_os.h"
163 #include "wnn_string.h"
164 
165 #ifdef JS
166 #ifndef BDIC_WRITE_CHECK
167 #define vputc(X, pt) putc((X), (pt))
168 #endif /* !BDIC_WRITE_CHECK */
169 #define vgetc(pt) getc(pt)
170 #else /* JS */
171 #define vputc(X, pt) putc((X), (pt))
172 #define vgetc(pt) getc(pt)
173 #endif /* JS */
174 
175 #ifndef min
176 #define min(a, b) ((a > b)? b:a)
177 #define max(a, b) ((a < b)? b:a)
178 #endif
179 
180 /*:::DOC_START
181  *
182  *    Function Name: dic_fopen
183  *    Description  : open normal dictionary
184  *    Parameter    :
185  *         file_name:     	(In) file_name
186  *         mode_str:     	(In) open mode
187  *
188  *    Return value : file pointer for fopen-ed or popen-ed dictionary file
189  *
190  *    Revision history:
191  *         22-Sep-94: initial
192  *
193  *:::DOC_END
194  */
195 #ifdef JS
196 static
197 #endif
198 FILE *
dic_fopen(file_name,mode_str)199 dic_fopen(file_name, mode_str)
200 char *file_name;
201 char *mode_str;
202 {
203 	return(fopen(file_name , mode_str));
204 } /* End of dic_fopen */
205 
206 /*:::DOC_START
207  *
208  *    Function Name: dic_fclose
209  *    Description  : close dictionary file
210  *    Parameter    :
211  *         fp:     	(In) file pointer for dictionary file
212  *
213  *    Return value : None.
214  *
215  *    Revision history:
216  *         22-Sep-94: initial
217  *
218  *:::DOC_END
219  */
220 #ifdef JS
221 static
222 #endif
223 void
dic_fclose(fp)224 dic_fclose(fp)
225 FILE *fp;
226 {
227 	if (fp != (FILE*)-1 && fp != NULL)
228 		fclose(fp);
229 } /* End of dic_fclose */
230 
231 /*:::DOC_START
232  *
233  *    Function Name: cd_convert_yomi
234  *    Description  : convert or dis-convert one w_char in yomi
235  *    Parameter    :
236  *         w_c:     	  (In) a w_char to be converted or dis-converted.
237  *         deconvert:     (In) 0 specify convert, !0 specify dis-convert.
238  *
239  *    Return value : converted or dis-converted one w_char in yomi. or Null.
240  *
241  *    Revision history:
242  *         22-Sep-94: initial
243  *
244  *:::DOC_END
245  */
246 #ifdef JS
247 static
248 #endif
249 w_char
cd_convert_yomi(w_c,deconvert)250 cd_convert_yomi(w_c, deconvert)
251 w_char w_c;
252 int deconvert;
253 {
254 	int j;
255 	/* data for conversion */
256 	/* convert to ascii */
257 	int cvt_tbl_size = 128-33-1;
258 	int cvt_tbl_offset = 33;
259 	static w_char cvt_tbl[] = {
260 		         0xa1aa, 0xa1af, 0xa1f4, 0xa1f0, 0xa1f3, 0xa1f5, 0xa1ad, /* 041 ! - ' */
261 		 0xa1ca, 0xa1cb, 0xa1f6, 0xa1dc, 0xa1a4, 0xa1dd, 0xa1a5, 0xa1bf, /* 050 ( - / */
262 		 0xa3b0, 0xa3b1, 0xa3b2, 0xa3b3, 0xa3b4, 0xa3b5, 0xa3b6, 0xa3b7, /* 060 0 - 7 */
263 		 0xa3b8, 0xa3b9, 0xa1a7, 0xa1a8, 0xa1e3, 0xa1e1, 0xa1e4, 0xa1a9, /* 070 8 - ? */
264 		 0xa1f7, 0xa3c1, 0xa3c2, 0xa3c3, 0xa3c4, 0xa3c5, 0xa3c6, 0xa3c7, /* 100 @ - G */
265 		 0xa3c8, 0xa3c9, 0xa3ca, 0xa3cb, 0xa3cc, 0xa3cd, 0xa3ce, 0xa3cf, /* 110 H - O */
266 		 0xa3d0, 0xa3d1, 0xa3d2, 0xa3d3, 0xa3d4, 0xa3d5, 0xa3d6, 0xa3d7, /* 120 P - W */
267 		 0xa3d8, 0xa3d9, 0xa3da, 0xa1ce, 0xa1ef, 0xa1cf, 0xa1b0, 0xa1b2, /* 130 X - _ */
268 		 0xa1ae, 0xa3e1, 0xa3e2, 0xa3e3, 0xa3e4, 0xa3e5, 0xa3e6, 0xa3e7, /* 140 ` - g */
269 		 0xa3e8, 0xa3e9, 0xa3ea, 0xa3eb, 0xa3ec, 0xa3ed, 0xa3ee, 0xa3ef, /* 150 h - o */
270 		 0xa3f0, 0xa3f1, 0xa3f2, 0xa3f3, 0xa3f4, 0xa3f5, 0xa3f6, 0xa3f7, /* 160 p - w */
271 		 0xa3f8, 0xa3f9, 0xa3fa, 0xa1d0, 0xa1c3, 0xa1d1, 0xa1b1,         /* 170 x - ~ */
272 	};
273 	int cvt_tbl2_size = 3;
274 	int cvt_tbl2_offset = 0;
275 	static w_char cvt_tbl2[][2] = {
276 		  { 0xa1bc, 0xf8, }, { 0xa5f4, 0xf9, }, { 0xa1ab, 0xfa, },
277 		  /* 1/28 "��" 5/84 "��" unused f8, 1/x "��" dakuten  */
278 	};
279 	if( deconvert == 0 ) {
280 		if( w_c > 0x20 && w_c < 0x7f ) {
281 			/* already ascii, do nothing */
282 			return w_c;
283 		} else
284 			if( w_c > 0xa4a0 && w_c < 0xa4f4 ) {
285 				/* "hiragana", re-move first byte */
286 				return w_c&0xff;
287 			}
288 		else
289 				if( w_c > 0xa5a0 && w_c < 0xa5f4 ) {
290 					/* "katakana", print warning and re-move first byte */
291 					return w_c&0xff;
292 				}
293 				else {
294 					for(j=0; j<cvt_tbl_size ; j++ ) {
295 						if( w_c == cvt_tbl[j] ) {
296 							return j+cvt_tbl_offset;
297 						}
298 					}
299 					for(j=0; j<cvt_tbl2_size ; j++ ) {
300 						if( w_c == cvt_tbl2[j][0] ) {
301 							return cvt_tbl2[j][1]+cvt_tbl2_offset;
302 						}
303 					}
304 					return 0;
305 				}
306 	} else {
307 		if( w_c > 0x20 && w_c < 0x7f ) {
308 			/* convert to ascii */
309 			return cvt_tbl[w_c-cvt_tbl_offset];
310 		} else
311 			if( w_c > 0xa0 && w_c < 0xf4 ) {
312 				/* "hiragana" or "katakana", insert first byte */
313 				return w_c|0xa400;
314 			}
315 			else {
316 				for(j=0; j<cvt_tbl2_size ; j++ ) {
317 					if( (w_c-cvt_tbl2_offset) == cvt_tbl2[j][1] ) {
318 						return cvt_tbl2[j][0];
319 					}
320 				}
321 				return 0;
322 			}
323 	}
324 }
325 
326 /*:::DOC_START
327  *
328  *    Function Name: dic_unlink
329  *    Description  : unlink dictionary file
330  *    Parameter    :
331  *         file_name:     	(In) file_name
332  *
333  *    Return value : same as unlink()
334  *
335  *    Revision history:
336  *         22-Sep-94: initial
337  *
338  *:::DOC_END
339  */
340 #ifdef JS
341 static
342 #endif
343 int
dic_unlink(file_name)344 dic_unlink(file_name)
345 char *file_name;
346 {
347 	return unlink(file_name);
348 } /* End of dic_unlink */
349 
350 
351 #ifdef JS
352 static
353 #else
354 extern
355 #endif
356 int output_file_header(), input_file_uniq(), output_header_jt(),
357 #ifdef BDIC_WRITE_CHECK
358 put_short(), output_file_uniq(),
359 #endif /* BDIC_WRITE_CHECK */
360 create_file_header(), input_file_header(), output_header_hjt();
361 
362 #ifdef JS
363 static
364 #else
365 extern
366 #endif
367 #ifdef BDIC_WRITE_CHECK
368 void new_pwd();
369 #else /* BDIC_WRITE_CHECK */
370 void put_short(), output_file_uniq(), new_pwd();
371 #endif /* BDIC_WRITE_CHECK */
372 
373 #ifdef BDIC_WRITE_CHECK
374 static int
vputc(c,fp)375 vputc(c, fp)
376 char c;
377 FILE *fp;
378 {
379 	if (fwrite(&c, sizeof(char), 1, fp) <= 0) return(-1);
380 	return(0);
381 }
382 #endif /* BDIC_WRITE_CHECK */
383 
384 #ifdef JS
385 static
386 #endif
387 #ifdef BDIC_WRITE_CHECK
388 int
389 #else /* BDIC_WRITE_CHECK */
390 void
391 #endif /* BDIC_WRITE_CHECK */
putnull(ofpter,n)392 putnull(ofpter, n)
393 register FILE *ofpter;
394 register int n;
395 {
396 	while (n-- > 0) {
397 #ifdef BDIC_WRITE_CHECK
398 		if (vputc('\0', ofpter) == -1) return(-1);
399 #else /* BDIC_WRITE_CHECK */
400 		vputc('\0', ofpter);
401 #endif /* BDIC_WRITE_CHECK */
402 	}
403 #ifdef BDIC_WRITE_CHECK
404 	return(0);
405 #endif /* BDIC_WRITE_CHECK */
406 }
407 
408 #ifdef JS
409 static
410 #endif
411 #ifdef BDIC_WRITE_CHECK
412 int
413 #else /* BDIC_WRITE_CHECK */
414 void
415 #endif /* BDIC_WRITE_CHECK */
put_n_str(ofpter,c,n)416 put_n_str(ofpter, c, n)
417 register FILE *ofpter;
418 register int n;
419 register char *c;
420 {
421 	while (n-- > 0) {
422 #ifdef BDIC_WRITE_CHECK
423 		if (vputc(*c++, ofpter) == -1) return(-1);
424 #else /* BDIC_WRITE_CHECK */
425 		vputc(*c++, ofpter);
426 #endif /* BDIC_WRITE_CHECK */
427 	}
428 #ifdef BDIC_WRITE_CHECK
429 	return(0);
430 #endif /* BDIC_WRITE_CHECK */
431 }
432 
433 #ifdef JS
434 static
435 #endif
436 #ifdef BDIC_WRITE_CHECK
437 int
438 #else /* BDIC_WRITE_CHECK */
439 void
440 #endif /* BDIC_WRITE_CHECK */
put_n_EU_str(ofpter,c,n)441 put_n_EU_str(ofpter, c, n)
442 register FILE *ofpter;
443 register w_char *c;
444 register int n;
445 {
446 	while (n-- > 0) {
447 #ifdef BDIC_WRITE_CHECK
448 		if (put_short(ofpter, (int)(*c++)) == -1) return(-1);
449 #else /* BDIC_WRITE_CHECK */
450 		put_short(ofpter, (int)(*c++));
451 #endif /* BDIC_WRITE_CHECK */
452 	}
453 #ifdef BDIC_WRITE_CHECK
454 	return(0);
455 #endif /* BDIC_WRITE_CHECK */
456 }
457 
458 #ifdef JS
459 static
460 #endif
461 #ifdef BDIC_WRITE_CHECK
462 int
463 #else /* BDIC_WRITE_CHECK */
464 void
465 #endif /* BDIC_WRITE_CHECK */
put_short(ofpter,i)466 put_short(ofpter, i)
467 register FILE *ofpter;
468 register int i;
469 {
470 #ifdef BDIC_WRITE_CHECK
471 	if ((vputc(i >> 8, ofpter) == -1) ||
472 	    (vputc(i     , ofpter) == -1)) return(-1);
473 	return(0);
474 #else /* BDIC_WRITE_CHECK */
475 	vputc(i >> 8, ofpter);
476 	vputc(i     , ofpter);
477 #endif /* BDIC_WRITE_CHECK */
478 }
479 
480 #ifdef JS
481 static
482 #endif
483 #ifdef BDIC_WRITE_CHECK
484 int
485 #else /* BDIC_WRITE_CHECK */
486 void
487 #endif /* BDIC_WRITE_CHECK */
putint(ofpter,i)488 putint(ofpter, i)
489 register FILE *ofpter;
490 register int i;
491 {
492 #ifdef BDIC_WRITE_CHECK
493 	if ((vputc(i >> 24, ofpter) == -1) ||
494 	    (vputc(i >> 16, ofpter) == -1) ||
495 	    (vputc(i >> 8 , ofpter) == -1) ||
496 	    (vputc(i      , ofpter) == -1)) return(-1);
497 	return(0);
498 #else /* BDIC_WRITE_CHECK */
499 	vputc(i >> 24, ofpter);
500 	vputc(i >> 16, ofpter);
501 	vputc(i >> 8 , ofpter);
502 	vputc(i      , ofpter);
503 #endif /* BDIC_WRITE_CHECK */
504 }
505 
506 #ifdef JS
507 static
508 #endif
509 #ifdef BDIC_WRITE_CHECK
510 int
511 #else /* BDIC_WRITE_CHECK */
512 void
513 #endif /* BDIC_WRITE_CHECK */
put_n_int(ofpter,n)514 put_n_int(ofpter, n)
515 register FILE *ofpter;
516 register int n;
517 {
518 #ifdef BDIC_WRITE_CHECK
519 	return(putnull(ofpter, n));
520 #else /* BDIC_WRITE_CHECK */
521 	putnull(ofpter, n);
522 #endif /* BDIC_WRITE_CHECK */
523 }
524 
525 #ifdef JS
526 static
527 #endif
528 int
getnull(ifpter,n)529 getnull(ifpter, n)
530 register FILE *ifpter;
531 register int n;
532 {
533 	register int k;
534 
535 	while (n-- > 0) {
536 		k = vgetc(ifpter);
537 		if(k == -1)return(-1);
538 	}
539 	return(0);
540 }
541 
542 #ifdef JS
543 static
544 #endif
545 int
getnstr(ifpter,n,st)546 getnstr(ifpter, n, st)
547 register FILE *ifpter;
548 register int n;
549 register char *st;
550 {
551 	int k;
552 
553 	while (n-- > 0) {
554 		k = vgetc(ifpter);
555 		*st++ = k;
556 		if(k == -1)return(-1);
557 	}
558 	return(0);
559 }
560 
561 #if	!defined(JS) && !defined(JSERVER)
562 int
get_n_EU_str(ifpter,n,st)563 get_n_EU_str(ifpter, n, st)
564 register FILE *ifpter;
565 register int n;
566 register w_char *st;
567 {
568 	register    int k;
569 	extern int get_short(short *sp, FILE *ifpter);
570 
571 	while (n-- > 0) {
572 		k = get_short((short *)st++, ifpter);
573 		if(k == -1)return(-1);
574 	}
575 	return(0);
576 }
577 
578 int
get_short(sp,ifpter)579 get_short(sp,ifpter)
580 register short *sp;
581 FILE *ifpter;
582 {
583 	register int i = 0;
584 	register int k;
585 
586 	i |= (k = vgetc(ifpter)) << 8;
587 	if(k == -1)return(-1);
588 	i |= (k = vgetc(ifpter));
589 	if(k == -1)return(-1);
590 	*sp = i;
591 	return(0);
592 }
593 #endif
594 
595 #ifdef JS
596 static
597 #endif
598 int
getint(ip,ifpter)599 getint(ip,ifpter)
600 register FILE *ifpter;
601 int *ip;
602 {
603 	register int i = 0;
604 	register int k;
605 
606 	i |= (k = vgetc(ifpter)) << 24;
607 	if(k == -1)return(-1);
608 	i |= (k = vgetc(ifpter)) << 16;
609 	if(k == -1)return(-1);
610 	i |= (k = vgetc(ifpter)) << 8;
611 	if(k == -1)return(-1);
612 	i |= (k = vgetc(ifpter));
613 	if(k == -1)return(-1);
614 	*ip = i;
615 	return(0);
616 }
617 
618 #ifndef JS
619 void
udytoS(yomi,tnum,hostart,tary)620 udytoS(yomi,tnum,hostart,tary)
621 register w_char *yomi;
622 register int tnum;
623 char *hostart;
624 register struct uind1 *tary;
625 {
626 	struct uind2 *hop;
627 	int len;
628 
629 	hop = (struct uind2 *)((char *)hostart + tary[tnum].pter);
630 	yomi[0] = tary[tnum].yomi1 >> 16;
631 	yomi[1] = tary[tnum].yomi1 & 0xffff;
632 	if(yomi[1]){
633 		yomi[2] = tary[tnum].yomi2 >> 16;
634 		if(yomi[2]){
635 			yomi[3] = tary[tnum].yomi2 & 0xffff;
636 		}
637 	}
638 	len = hop->yomi[0];
639 	/*Strncpy(yomi + 4, (hop->yomi) + 1, len - 4);
640 	      In order not to use ../etc/strings.c*/
641 	memcpy((char *)(yomi + 4), (char *)((hop->yomi) + 1), max(0, ((len - 4)) * 2));
642 	yomi[len] = 0;
643 }
644 #endif
645 
646 #ifdef BDIC_WRITE_CHECK
647 static char *
make_tmp_name(n)648 make_tmp_name(n)
649 char *n;
650 {
651 	static char buf[MAXPATHLEN];
652 
653 	if (n == NULL || *n == '\0') return NULL;
654 	snprintf(buf, sizeof(buf), "%s~", n);
655 	return(buf);
656 }
657 
658 static char *
make_backup_name(n)659 make_backup_name(n)
660 char *n;
661 {
662 	static char buf[MAXPATHLEN];
663 
664 	if (n == NULL || *n == '\0') return NULL;
665 	snprintf(buf, sizeof(buf), "%s#", n);
666 	return(buf);
667 }
668 
669 #ifdef JS
670 static
671 #endif
672 void
check_backup(n)673 check_backup(n)
674 char *n;
675 {
676 	char *p;
677 
678 	if ((p = make_tmp_name(n)) && (access(p, F_OK) != -1)) unlink(p);
679 	if ((p = make_backup_name(n)) && (access(p, F_OK) != -1)) {
680 		unlink(n);
681 		link(p, n);
682 		unlink(p);
683 	}
684 }
685 
686 #ifdef JS
687 static
688 #endif
copy_file_to_file(from,to)689 copy_file_to_file(from, to)
690 FILE *from, *to;
691 {
692 	char buf[1024];
693 	int r_len, w_len, i;
694 
695 	for (;;) {
696 		r_len = fread(buf, 1, sizeof(buf), from);
697 		if (r_len <= 0) break;
698 		for ( i = 0; i < r_len; i += w_len) {
699 			w_len = fwrite(&buf[i], 1, r_len - i, to);
700 			if (w_len <= 0) {
701 				fseek(from, 0, 0);
702 				fseek(to, 0, 0);
703 				return(-1);
704 			}
705 			if (w_len >= r_len) break;
706 		}
707 	}
708 	fseek(from, 0, 0);
709 	fseek(to, 0, 0);
710 	return(0);
711 }
712 
713 #ifdef JS
714 static
715 #endif
716 void
delete_tmp_file(n)717 delete_tmp_file(n)
718 char *n;
719 {
720 	if (n && *n) unlink(n);
721 }
722 
723 #ifdef JS
724 static
725 #endif
726 char *
make_backup_file(n)727 make_backup_file(n)
728 char *n;
729 {
730 	FILE *fp, *fp2;
731 	char *p, *p2;
732 
733 	if ((p = make_backup_name(n)) == NULL) return(NULL);
734 	if ((p2 = make_tmp_name(n)) == NULL) return(NULL);
735 
736 	fp = fp2 = NULL;
737 	if (((fp = fopen(p2, "w+")) == NULL) || ((fp2 = fopen(n, "r")) == NULL)) {
738 		if(fp) fclose(fp);
739 		if(fp2) fclose(fp2);
740 		return NULL;
741 	}
742 	if (copy_file_to_file(fp2, fp) == -1) {
743 		fclose(fp);
744 		fclose(fp2);
745 		return(NULL);
746 	}
747 	fclose(fp);
748 	fclose(fp2);
749 	if (access(p, F_OK) != -1) unlink(p);
750 	link(p2, p);
751 	unlink(p2);
752 	return(p);
753 }
754 
755 #ifdef JS
756 static
757 #endif
758 char *
make_tmp_file(n,copy,ret_fp)759 make_tmp_file(n, copy, ret_fp)
760 char *n;
761 int copy;
762 FILE **ret_fp;
763 {
764 	FILE *fp, *fp2;
765 	struct wnn_file_head fh;
766 	char *p;
767 
768 	p = make_tmp_name(n);
769 	if ((fp = fopen(p, "w+")) == NULL) {
770 		*ret_fp = NULL;
771 		return NULL;
772 	}
773 	if (copy && (fp2 = fopen(n, "r")) != NULL) {
774 		input_file_header(fp2, &fh);
775 		fseek(fp2, 0, 0);
776 		if ((copy_file_to_file(fp2, fp) == -1) ||
777 		    (create_file_header(fp, fh.file_type, fh.file_passwd) == -1)) {
778 			fclose(fp);
779 			fclose(fp2);
780 			*ret_fp = NULL;
781 			return NULL;
782 		}
783 		fseek(fp, 0, 0);
784 		fclose(fp2);
785 	}
786 	*ret_fp = fp;
787 	return p;
788 }
789 
790 #ifdef JS
791 static
792 #endif
793 void
move_tmp_to_org(tmp_name,org_name,copy)794 move_tmp_to_org(tmp_name, org_name, copy)
795 char *tmp_name, *org_name;
796 int copy;
797 {
798 	FILE *org, *tmp;
799 
800 	org = tmp = NULL;
801 
802 	if (copy) {
803 		if (((tmp = fopen(tmp_name, "r")) == NULL) ||
804 		    ((org = fopen(org_name, "w+")) == NULL)) {
805 			if(tmp) fclose(tmp);
806 			if(org) fclose(org);
807 			return;
808 		}
809 		copy_file_to_file(tmp, org);
810 		fclose(tmp);
811 		fclose(org);
812 	} else {
813 		unlink(org_name);
814 		link(tmp_name, org_name);
815 	}
816 	unlink(tmp_name);
817 }
818 
819 #ifdef JS
820 static
821 #endif
822 void
write_file_move_tmp_to_org(tmp_name,org_name,file_type,file_passwd,file_uniq)823 write_file_move_tmp_to_org(tmp_name, org_name, file_type, file_passwd, file_uniq)
824 char *tmp_name, *org_name;
825 int file_type;
826 char *file_passwd;
827 struct wnn_file_uniq *file_uniq;
828 {
829 	FILE *org, *tmp;
830 
831 	org = tmp = NULL;
832 
833 	if (((tmp = fopen(tmp_name, "r")) == NULL) ||
834 		((org = fopen(org_name, "w+")) == NULL)) {
835 		if(tmp) fclose(tmp);
836 		if(org) fclose(org);
837 		return;
838 	}
839 	copy_file_to_file(tmp, org);
840 #ifndef WNN_CHECK_INODE
841 	rewind(org);
842 	change_file_uniq1(org, file_type, file_passwd, file_uniq);
843 #endif
844 	fclose(tmp);
845 	fclose(org);
846 	unlink(tmp_name);
847 }
848 
849 #endif /* BDIC_WRITE_CHECK */
850 
851 #ifdef JS
852 static
853 #endif
854 int
set_file_header(fh,ofpter,file_type,file_passwd)855 set_file_header(fh, ofpter, file_type, file_passwd)
856 FILE *ofpter;
857 int file_type;
858 char *file_passwd;
859 struct wnn_file_head *fh;
860 {
861 	/* WNN_HOSTLEN" is "fh->file_uniq.createhost" size */
862 	char hostname[WNN_HOSTLEN];
863 	struct stat buf;
864 
865 	if(fstat(fileno(ofpter), &buf) == -1){
866 		return(-1);
867 	}
868 	gethostname(hostname, sizeof(hostname) - 1);
869 	hostname[sizeof(hostname) - 1] = '\0';
870 
871 	fh->file_uniq.time = (int)buf.st_ctime;
872 	fh->file_uniq.dev = (int)buf.st_dev;
873 	fh->file_uniq.inode = (int)buf.st_ino;
874 	memset(fh->file_uniq.createhost, 0, WNN_HOSTLEN);
875 	strncpy(fh->file_uniq.createhost, hostname, WNN_HOSTLEN - 1);
876 
877 	/* file_uniq_org */
878 	fh->file_uniq_org.time = (int)buf.st_ctime;
879 	fh->file_uniq_org.dev = (int)buf.st_dev;
880 	fh->file_uniq_org.inode = (int)buf.st_ino;
881 	memset(fh->file_uniq_org.createhost, 0, WNN_HOSTLEN);
882 	strncpy(fh->file_uniq_org.createhost, hostname, WNN_HOSTLEN - 1);
883 
884 	fh->file_type = file_type;
885 	if(file_passwd){
886 		strncpy(fh->file_passwd, file_passwd, WNN_PASSWD_LEN);
887 	} else{
888 		memset(fh->file_passwd, 0, WNN_PASSWD_LEN);
889 	}
890 	return(0);
891 }
892 
893 #ifdef JS
894 static
895 #endif
896 int
create_file_header(ofpter,file_type,file_passwd)897 create_file_header(ofpter, file_type, file_passwd)
898 FILE *ofpter;
899 int file_type;
900 char *file_passwd;
901 {
902 	struct wnn_file_head fh;
903 	if(set_file_header(&fh, ofpter, file_type, file_passwd) == -1) return(-1);
904 	if(output_file_header(ofpter, &fh) == -1) return(-1);
905 	return(0);
906 }
907 
908 /* 128 Bytes File Header */
909 
910 #ifdef JS
911 static
912 #endif
913 int
output_file_header(ofpter,hp)914 output_file_header(ofpter, hp)
915 FILE *ofpter;
916 struct wnn_file_head *hp;
917 {
918 #ifdef BDIC_WRITE_CHECK
919 #ifdef WNN7_MAKE_DICT
920 	if ((put_n_str(ofpter, WNN7_FILE_STRING, WNN_FILE_STRING_LEN) == -1) ||
921 #else
922 	if ((put_n_str(ofpter, WNN_FILE_STRING, WNN_FILE_STRING_LEN) == -1) ||
923 #endif
924 	    (putint(ofpter, hp->file_type) == -1) ||
925 	    (output_file_uniq(&hp->file_uniq, ofpter) == -1) ||
926 	    (output_file_uniq(&hp->file_uniq_org, ofpter) == -1) ||
927 	    (put_n_str(ofpter, hp->file_passwd, WNN_PASSWD_LEN) == -1) ||
928 	    (putnull(ofpter, 36) == -1)) return(-1);		/* Future Use */
929 #else /* BDIC_WRITE_CHECK */
930 #ifdef WNN7_MAKE_DICT
931 	put_n_str(ofpter, WNN7_FILE_STRING, WNN_FILE_STRING_LEN);
932 #else
933 	put_n_str(ofpter, WNN_FILE_STRING, WNN_FILE_STRING_LEN);
934 #endif
935 	putint(ofpter, hp->file_type);
936 	output_file_uniq(&hp->file_uniq, ofpter);
937 	output_file_uniq(&hp->file_uniq_org, ofpter);
938 	put_n_str(ofpter, hp->file_passwd, WNN_PASSWD_LEN);
939 	putnull(ofpter, 36);		/* Future Use */
940 #endif /* BDIC_WRITE_CHECK */
941 	return(0);
942 }
943 
944 #ifdef JS
945 static
946 #endif
947 int
input_file_header(ifpter,hp)948 input_file_header(ifpter, hp)
949 FILE *ifpter;
950 struct wnn_file_head *hp;
951 {
952 	char wnn_file_string[WNN_FILE_STRING_LEN + 1];
953 	int err = 0;
954 
955 	getnstr(ifpter, WNN_FILE_STRING_LEN, wnn_file_string);
956 	if(strncmp(wnn_file_string, WNN_FILE_STRING, WNN_FILE_STRING_LEN)) {
957 		if(strncmp(wnn_file_string, WNN7_FILE_STRING, WNN_FILE_STRING_LEN))
958 			err = -1;
959 	}
960 	if(getint((&hp->file_type), ifpter) == -1) err = -1;
961 	if(input_file_uniq(&(hp->file_uniq), ifpter) == -1) err = -1;
962 	if(input_file_uniq(&(hp->file_uniq_org), ifpter) == -1) err = -1;
963 	getnstr(ifpter, WNN_PASSWD_LEN, hp->file_passwd);
964 	getnull(ifpter, 36);
965 	return(err);
966 }
967 
968 #ifdef JS
969 static
970 #endif
971 #ifdef BDIC_WRITE_CHECK
972 int
973 #else /* BDIC_WRITE_CHECK */
974 void
975 #endif /* BDIC_WRITE_CHECK */
output_file_uniq(funiq,ofpter)976 output_file_uniq(funiq, ofpter)
977 struct wnn_file_uniq *funiq;
978 FILE *ofpter;
979 {
980 #ifdef BDIC_WRITE_CHECK
981 	if ((putint(ofpter, funiq->time) == -1) ||
982 	    (putint(ofpter, funiq->dev) == -1) ||
983 	    (putint(ofpter, funiq->inode) == -1) ||
984 	    (put_n_str(ofpter, funiq->createhost,
985 	    strlen(funiq->createhost)) == -1) ||
986 	    (putnull(ofpter, WNN_HOSTLEN - strlen(funiq->createhost)) == -1))
987 		return(-1);
988 	return(0);
989 #else /* BDIC_WRITE_CHECK */
990 	putint(ofpter, funiq->time);
991 	putint(ofpter, funiq->dev);
992 	putint(ofpter, funiq->inode);
993 	put_n_str(ofpter, funiq->createhost, strlen(funiq->createhost));
994 	putnull(ofpter, WNN_HOSTLEN - strlen(funiq->createhost));
995 #endif /* BDIC_WRITE_CHECK */
996 }
997 
998 #ifdef JS
999 static
1000 #endif
1001 int
input_file_uniq(funiq,ifpter)1002 input_file_uniq(funiq, ifpter)
1003 struct wnn_file_uniq *funiq;
1004 FILE *ifpter;
1005 {
1006 	if(
1007        getint(&(funiq->time), ifpter) == -1 ||
1008 	    getint(&(funiq->dev), ifpter) == -1 ||
1009 	    getint(&(funiq->inode), ifpter) == -1 ||
1010 	    getnstr(ifpter, WNN_HOSTLEN, funiq->createhost) == -1)
1011 		return(-1);
1012 	return(0);
1013 }
1014 
1015 #ifdef WNN_CHECK_INODE
1016 static
1017 #else
1018 #endif
1019 int
change_file_uniq1(ofpter,file_type,file_passwd,file_uniq)1020 change_file_uniq1(ofpter, file_type, file_passwd, file_uniq)
1021 FILE *ofpter;
1022 int file_type;
1023 char *file_passwd;
1024 struct wnn_file_uniq *file_uniq;
1025 {
1026 	struct stat buf;
1027 	char hostname[WNN_HOSTLEN];
1028 	struct wnn_file_head fh;
1029 
1030 	if(fstat(fileno(ofpter), &buf) == -1){
1031 		return(-1);
1032 	}
1033 	gethostname(hostname, sizeof(hostname) - 1);
1034 	hostname[sizeof(hostname) - 1] = '\0';
1035 
1036 	fh.file_uniq.time = (int)buf.st_ctime;
1037 	fh.file_uniq.dev = (int)buf.st_dev;
1038 	fh.file_uniq.inode = (int)buf.st_ino;
1039 	memset(fh.file_uniq.createhost, 0, WNN_HOSTLEN);
1040 	strncpy(fh.file_uniq.createhost, hostname, WNN_HOSTLEN - 1);
1041 
1042 	/* file_uniq_org */
1043 	fh.file_uniq_org.time = file_uniq->time;
1044 	fh.file_uniq_org.dev = file_uniq->dev;
1045 	fh.file_uniq_org.inode = file_uniq->inode;
1046 	memset(fh.file_uniq_org.createhost, 0, WNN_HOSTLEN);
1047 	strncpy(fh.file_uniq_org.createhost, file_uniq->createhost, WNN_HOSTLEN - 1);
1048 
1049 	fh.file_type = file_type;
1050 	if(file_passwd){
1051 		strncpy(fh.file_passwd, file_passwd, WNN_PASSWD_LEN);
1052 	} else{
1053 		memset(fh.file_passwd, 0, WNN_PASSWD_LEN);
1054 	}
1055 	if(output_file_header(ofpter, &fh) == -1) return(-1);
1056 	return(0);
1057 }
1058 
1059 #ifdef WNN_CHECK_INODE
1060 
1061 static
1062 int
check_inode(f,fh)1063 check_inode(f, fh)
1064 FILE *f;
1065 struct wnn_file_head *fh;
1066 {
1067 	struct stat buf;
1068 	if(fstat(fileno(f), &buf) == -1){
1069 		return(-1);
1070 	}
1071 	if((int)buf.st_ino != fh->file_uniq.inode){
1072 		return(-1);
1073 	}
1074 	return(0);
1075 }
1076 
1077 /*:::DOC_START
1078  *
1079  *    Function Name: dic_check_inode
1080  *    Description  : check i-node of file
1081  *    Parameter    :
1082  *         f:           (In) file pointer for dictionary file
1083  *         fh:          (In) file header
1084  *
1085  *    Return value : check result, 0: same, -1: different
1086  *
1087  *    Revision history:
1088  *         22-Sep-94: initial
1089  *
1090  *:::DOC_END
1091  */
1092 #ifdef JS
1093 static
1094 #endif
1095 int
dic_check_inode(f,fh)1096 dic_check_inode(f, fh)
1097 FILE *f;
1098 struct wnn_file_head *fh;
1099 {
1100 	return(check_inode(f, fh));
1101 } /* End of dic_check_inode */
1102 
1103 #ifdef JS
1104 static
1105 #endif
1106 int
change_file_uniq(fh,n)1107 change_file_uniq(fh, n)
1108 struct wnn_file_head *fh;
1109 char *n;
1110 {
1111 	int x;
1112 	FILE *fp;
1113 #ifdef BDIC_WRITE_CHECK
1114 	char *tmp, *backup = NULL;
1115 	backup = make_backup_file(n);
1116 	if ((tmp = make_tmp_file(n, 1, &fp)) == NULL) {
1117 		delete_tmp_file(backup);
1118 #else /* BDIC_WRITE_CHECK */
1119 	if((fp = fopen(n,"r+")) == NULL){
1120 #endif /* BDIC_WRITE_CHECK */
1121 		return(-1);
1122 	}
1123 	/* file_uniq_org ���ѹ����ʤ� */
1124 #ifdef BDIC_WRITE_CHECK
1125 	if (change_file_uniq1(fp, fh->file_type, fh->file_passwd, &(fh->file_uniq_org)) == -1) {
1126 		fclose(fp);
1127 		delete_tmp_file(tmp);
1128 		delete_tmp_file(backup);
1129 		return(-1);
1130 	}
1131 #else /* BDIC_WRITE_CHECK */
1132 	change_file_uniq1(fp, fh->file_type, fh->file_passwd, &(fh->file_uniq_org));
1133 #endif /* BDIC_WRITE_CHECK */
1134 	fseek(fp, 0, 0);
1135 	x = input_file_header(fp, fh);/* It must not occur. */
1136 	fclose(fp);
1137 #ifdef BDIC_WRITE_CHECK
1138 	if(x == -1) {
1139 		delete_tmp_file(tmp);
1140 		delete_tmp_file(backup);
1141 		return(-1);
1142 	}
1143 	move_tmp_to_org(tmp, n, 0);
1144 	delete_tmp_file(backup);
1145 #else /* BDIC_WRITE_CHECK */
1146 	if(x == -1) return(-1);
1147 #endif /* BDIC_WRITE_CHECK */
1148 	return(0);
1149 }
1150 
1151 #endif /* WNN_CHECK_INODE */
1152 
1153 
1154 
1155 #if	!defined(JS) && !defined(JSERVER)
1156 void
vputs(c,fp)1157 vputs(c, fp)
1158 register char *c;
1159 register FILE *fp;
1160 {
1161 	while(*c){
1162 		vputc(*c++, fp);
1163 	}
1164 }
1165 
1166 int
vputws(w,fp)1167 vputws(w, fp)
1168 register w_char *w;
1169 register FILE *fp;
1170 {
1171 	register int n;
1172 	UCHAR tmp[LENGTHYOMI*3];
1173 
1174 	n = wnn_sStrncpy(tmp, sizeof(tmp) - 1, w, -1);
1175 	vputs(tmp, fp);
1176 	return(n);
1177 }
1178 #endif
1179 
1180 
1181 extern char *wnn_get_hinsi_name(int k);
1182 
1183 #if	!defined(JS) && !defined(JSERVER)
1184 int
put_yomi_str(yomi,ofpter)1185 put_yomi_str(yomi, ofpter)
1186 w_char *yomi;
1187 FILE *ofpter;
1188 {
1189 	register int c;
1190 	register int i = 0;
1191 	UCHAR tmp[LENGTHYOMI*3], *p;
1192 
1193 	wnn_sStrncpy(tmp, sizeof(tmp) - 1, yomi, -1);
1194 	for(p = tmp;*p;p++){
1195 		c = (int)*p;
1196 		if(c == '\\'){
1197 			vputc('\\', ofpter);
1198 			vputc('\\', ofpter);
1199 			i += 2;
1200 		} else if(c > 0x20){
1201 			vputc(c, ofpter);
1202 			i += 1;
1203 		} else{
1204 			char tmp[8];
1205 			snprintf(tmp, sizeof(tmp), "\\0%o" , c);
1206 			vputs(tmp, ofpter);
1207 			i += strlen(tmp);
1208 		}
1209 	}
1210 	return(i);
1211 }
1212 #endif
1213 
1214 #ifndef JS
1215 /*:::DOC_START
1216  *
1217  *    Function Name: Get_knj2
1218  *    Description  : get KOUHO string , YOMI string and comment string from
1219  *                   "kptr" with "kanji2" offset
1220  *    Parameter    :
1221  *         kptr:     (In) wchar pointer of those strings
1222  *         kanji2:   (In) offset of terget entry. �嵭���ݥ�������
1223  *                        ���֤���θ��䡦�ɤߡ������ȤΥ��ȥ����������������ꤹ�롣
1224  *                         0: jentptr���Τ�Ρ�
1225  *                         1: ��
1226  *                         .
1227  *                         n: n����
1228  *         kouho:    (Out) return of the HOUHO(����) string
1229  *         yomi:     (Out) return of the YOMI(�ɤ�) string
1230  *         com:      (Out) return of the comment string
1231  *
1232  *    Return value :  None.
1233  *
1234  *    Revision history:
1235  *         31-AUG-94: just wrote this comment. no changes souces
1236  *
1237  *:::DOC_END
1238  */
1239 void
Get_knj2(kptr,kanji2,kouho,yomi,comment)1240 Get_knj2(kptr,kanji2, kouho, yomi, comment)
1241 UCHAR *kptr;
1242 int kanji2;
1243 w_char *kouho, *comment, *yomi;
1244 {
1245 	extern void get_kanji_str();
1246 
1247 	int tcnt;
1248 	for (tcnt = 0; tcnt < kanji2; tcnt++){
1249 		kptr += *kptr;
1250 	}
1251 	get_kanji_str(kptr, kouho, yomi, comment);
1252 	return;
1253 }
1254 
1255 static void
copy_kanji_str_from_cd(kptr,kanji)1256 copy_kanji_str_from_cd(kptr, kanji)
1257 UCHAR *kptr;
1258 w_char *kanji;
1259 {
1260 	int n , k;
1261 	n = *kptr++ - 1;
1262 	for(k = 0 ; k < n ;) {
1263 		if(kanji) *kanji++ = ((int) *kptr << 8 | *(kptr + 1));
1264 		kptr += 2;
1265 		k+= 2;
1266 	}
1267 	if(kanji)*kanji = 0;
1268 }
1269 
1270 void
get_kanji_str(kptr,kanji,yomi,comment)1271 get_kanji_str(kptr, kanji, yomi, comment)
1272 UCHAR *kptr;
1273 w_char *kanji, *comment, *yomi;
1274 {
1275 	w_char *tmpy;
1276 	w_char *tmpk;
1277 	w_char *tmpc;
1278 	extern void Get_kanji_str_r();
1279 
1280 	/* �������ɤߡ������Ⱦ�������� */
1281 	if(kanji) kanji[0] = (w_char)0;
1282 	if(yomi) yomi[0] = (w_char)0;
1283 	if(comment) comment[0] = (w_char)0;
1284 
1285 	/* CAUTION: jisho syurui is not checked, but ... */
1286 	/* not compact dictionary case */
1287 	if( (((int) (*kptr)) % 2 ) == 0){
1288 		Get_kanji_str_r(kptr, &tmpk, &tmpy, &tmpc);
1289 
1290 		if(kanji && tmpk)wnn_Sreverse(kanji, tmpk);
1291 		if(yomi && tmpy)wnn_Sreverse(yomi, tmpy);
1292 		if(comment && tmpc)wnn_Strcpy(comment, tmpc);
1293 	} else{
1294 		w_char *c;
1295 		w_char tmpk_dt[LENGTHKANJI];
1296 		c = tmpk = &tmpk_dt[0];
1297 		copy_kanji_str_from_cd(kptr, c);
1298 		if(kanji && tmpk)wnn_Sreverse(kanji, tmpk);
1299 	}
1300 }
1301 
1302 /* ?? need doc. CAUTION: *tmpk is IO */
1303 void
Get_kanji_str_r_cd(kptr,tmpk,tmpy,tmpc)1304 Get_kanji_str_r_cd (kptr, tmpk, tmpy, tmpc)
1305 UCHAR *kptr;
1306 w_char **tmpk, **tmpy, **tmpc;
1307 {
1308 	/* CAUTION: jisho syurui is not checked, but ... */
1309 	/* not compact dictionary case */
1310 	if( (((int) (*kptr)) % 2 ) == 0){
1311 		printf("Get_kanji_str_r_cd: do not call me for non-compact dictionary.\n" );
1312 	} else{
1313 		/* compact dictionary case */
1314 	}
1315 	copy_kanji_str_from_cd(kptr, *tmpk);
1316 }
1317 
1318 void
Get_kanji_str_r(kptr,tmpk,tmpy,tmpc)1319 Get_kanji_str_r(kptr, tmpk, tmpy, tmpc)
1320 UCHAR *kptr;
1321 w_char **tmpk, **tmpy, **tmpc;
1322 {
1323 	int state = *(kptr + 1);
1324 	static w_char dmy = 0; /* Must not be allocated on Stack!! */
1325 	int adres;
1326 
1327 	/* CAUTION: jisho syurui is not checked, but ... */
1328 	/* not compact dictionary case */
1329 	if( (((int) (*kptr)) % 2 ) == 0){
1330 	} else{
1331 		/* compact dictionary case */
1332 		printf("Get_kanji_str_r: do not call me for compact dictionary.\n" );
1333 	}
1334 	if(state & FORWARDED){
1335 		/* Forward occures when comment-set is used */
1336 		adres = (*(w_char *)(kptr + 2)) << 16 | (*(w_char *)(kptr + 4));
1337 		Get_kanji_str_r(kptr + adres, tmpk, tmpy, tmpc);
1338 		return;
1339 	}
1340 	/*    get_kanji_str_r(kptr, tmpk, tmpy, comment); */
1341 	if(state & HAS_YOMI){
1342 		*tmpy = ((w_char *)(kptr + 2));
1343 		*tmpk = *tmpy + wnn_Strlen(*tmpy) + 1;
1344 	} else{
1345 		*tmpy = &dmy;
1346 		*tmpk = ((w_char *)(kptr + 2));
1347 	}
1348 	if(state & HAS_COMMENT){
1349 		*tmpc = *tmpk + wnn_Strlen(*tmpk) + 1;
1350 	} else{
1351 		*tmpc = &dmy;
1352 	}
1353 }
1354 
1355 
1356 UCHAR
kanjiaddr(d0,kanji,yomi,comment)1357 kanjiaddr(d0, kanji, yomi, comment)
1358 UCHAR *d0;
1359 w_char *kanji, *yomi, *comment;
1360 {
1361 	w_char *dest = (w_char *)(d0 + 2);
1362 	int state = 0;
1363 	w_char *pt;
1364 
1365 	if(yomi && *yomi){
1366 		pt = yomi + wnn_Strlen(yomi) - 1;
1367 		for(;pt >= yomi;){
1368 			*dest++ = *pt--;
1369 		}
1370 		state |= HAS_YOMI;
1371 		*dest++ = 0;
1372 	}
1373 
1374 	pt = kanji + wnn_Strlen(kanji) - 1;
1375 	for(;pt >= kanji;){
1376 		*dest++ = *pt--;
1377 	}
1378 	*dest++ = 0;
1379 
1380 	if(comment && *comment){
1381 		pt = comment;
1382 		for(;*pt;){
1383 			*dest++ = *pt++;
1384 		}
1385 		state |= HAS_COMMENT;
1386 		*dest++ = 0;
1387 	}
1388 	*d0 = (UCHAR)((UCHAR *)dest - d0);
1389 	*(d0 + 1) = state;
1390 	return(*d0);
1391 }
1392 
1393 #endif
1394 
1395 
1396 /*:::DOC_START
1397  *
1398  *    Function Name: create_null_dic
1399  *    Description  : ����ե�������������
1400  *    Parameter    :
1401  *         fn :      (In) ��������ե�����̾
1402  *         comm :    (In) ��������ե����륳����
1403  *         passwd    (In) ��������ե�����ѥ����
1404  *         hpasswd : (In) �������������٥ѥ����
1405  *         which :   (In) ��������ե����륿����
1406  *
1407  *    Return value : 0==SUCCESS, -1==ERROR
1408  *
1409  *    Revision history:
1410  *
1411  *:::DOC_END
1412  */
1413 #ifdef JS
1414 static
1415 #endif
1416 int
create_null_dic(fn,comm,passwd,hpasswd,which)1417 create_null_dic(fn, comm, passwd, hpasswd, which)
1418 char  *fn;
1419 w_char *comm;
1420 char *passwd, *hpasswd;  /* not encoded passwd */
1421 int which;
1422 {
1423 	FILE *fp;
1424 	struct JT jt;
1425 	char epasswd[WNN_PASSWD_LEN];
1426 	extern void new_pwd();
1427 
1428 	/* �ޡ�������ˤϥ����ƥ༭��ޥ�����Ω�Ƥ� */
1429 	if(which == WNN_MERGE_DICT) which |= WNN_SYSTEM_DICT;
1430 
1431 	jt.total = 0;
1432 	jt.gosuu = 0;
1433 	if(hpasswd){
1434 		new_pwd(hpasswd, jt.hpasswd);
1435 	} else{
1436 		memset(jt.hpasswd, 0, WNN_PASSWD_LEN);
1437 	}
1438 	jt.syurui = which;
1439 	jt.maxserial = 0;
1440 	jt.maxtable = 0;
1441 	jt.maxhontai = (which == WNN_UD_DICT)? 4: 0;
1442 	jt.maxkanji = 0;
1443 	if(comm){
1444 		jt.maxcomment = wnn_Strlen(comm);
1445 	} else{
1446 		jt.maxcomment = 0;
1447 	}
1448 	jt.maxhinsi_list = 0;
1449 	jt.maxri1[D_YOMI] = 0;
1450 	jt.maxri1[D_KANJI] = 0;
1451 	jt.maxri2 = 0;
1452 
1453 	if((fp = fopen(fn, "w+")) == NULL){
1454 		return(-1);
1455 	}
1456 	if(passwd){
1457 		new_pwd(passwd, epasswd);
1458 	} else{
1459 		memset(epasswd, 0, WNN_PASSWD_LEN);
1460 	}
1461 	if(create_file_header(fp, WNN_FT_DICT_FILE,epasswd) == -1 ||
1462 		output_header_jt(fp, &jt) == -1){
1463 		fclose(fp);
1464 		return(-1);
1465 	}
1466 #ifdef BDIC_WRITE_CHECK
1467 	if (put_n_EU_str(fp, comm, jt.maxcomment) == -1) {
1468 		fclose(fp);
1469 		return(-1);
1470 	}
1471 #else /* BDIC_WRITE_CHECK */
1472 	put_n_EU_str(fp, comm, jt.maxcomment);
1473 #endif /* BDIC_WRITE_CHECK */
1474 	if(which == WNN_UD_DICT){
1475 #ifdef BDIC_WRITE_CHECK
1476 		if (putint(fp, 0) == -1) {	/* hontai[0] */
1477 			fclose(fp);
1478 			return(-1);
1479 		}
1480 #else /* BDIC_WRITE_CHECK */
1481 		putint(fp, 0);			/* hontai[0] */
1482 #endif /* BDIC_WRITE_CHECK */
1483 	}
1484 
1485 #ifdef	BSD42
1486 	fchmod(fileno(fp), 0644);
1487 	fclose(fp);
1488 #else	/* SYSV */
1489 	fclose(fp);
1490 	chmod(fn, 0644);
1491 #endif	/* BSD42 */
1492 	return(0);
1493 } /* End of create_null_dic */
1494 
1495 
1496 #ifdef JS
1497 static
1498 #endif
1499 int
create_hindo_file(funiq,fn,comm,passwd,serial)1500 create_hindo_file(funiq, fn, comm, passwd, serial)
1501 struct wnn_file_uniq *funiq;
1502 char *fn;
1503 w_char *comm;
1504 char *passwd;			/* Not encoded */
1505 int serial;
1506 {
1507 	FILE *fp;
1508 	struct HJT hjt;
1509 	char epasswd[WNN_PASSWD_LEN];
1510 	w_char tmp[1];
1511 	int i;
1512 
1513 	tmp[0] = 0;
1514 	if(comm == NULL) comm = tmp;
1515 	memcpy(&(hjt.dic_file_uniq), funiq, WNN_F_UNIQ_LEN);
1516 	hjt.maxcomment = wnn_Strlen(comm);
1517 
1518 	hjt.maxserial = serial;
1519 	hjt.hindo_area = (int)((serial + MAX_HINDO_DATA_AREA - 1) / MAX_HINDO_DATA_AREA);
1520 	if(hjt.hindo_area != 0) {
1521 		if((hjt.curserial = (unsigned char *)
1522 			malloc(hjt.hindo_area * sizeof(unsigned char))) == NULL)
1523 			return(-1);
1524 		for(i = 0; i < hjt.hindo_area; i++) (hjt.curserial)[i] = 0;
1525 	} else
1526 		hjt.curserial = NULL;
1527 
1528 	if((fp = fopen(fn, "w+")) == NULL){
1529 		return(-1);
1530 	}
1531 	if(passwd){
1532 		new_pwd(passwd, epasswd);
1533 	} else{
1534 		memset(epasswd, 0, WNN_PASSWD_LEN);
1535 	}
1536 	if(create_file_header(fp, WNN_FT_HINDO_FILE, epasswd) == -1){
1537 		fclose(fp);
1538 		return(-1);
1539 	}
1540 #ifdef BDIC_WRITE_CHECK
1541 	if ((output_header_hjt(fp, &hjt) == -1) ||
1542 		(put_n_EU_str(fp, comm, hjt.maxcomment) == -1) ||
1543 		(put_n_int(fp, 0) == -1)) {
1544 		fclose(fp);
1545 		return(-1);
1546 	}
1547 #else /* BDIC_WRITE_CHECK */
1548 	output_header_hjt(fp, &hjt);
1549 	put_n_EU_str(fp, comm, hjt.maxcomment);
1550 	put_n_int(fp, 0);
1551 #endif /* BDIC_WRITE_CHECK */
1552 
1553 #ifdef	BSD42
1554 	fchmod(fileno(fp), 0644);
1555 	fclose(fp);
1556 #else	/* SYSV */
1557 	fclose(fp);
1558 	chmod(fn, 0644);
1559 #endif	/* BSD42 */
1560 	return(0);
1561 }
1562 
1563 /* Header Total 128 Bytes */
1564 
1565 #ifndef	JS
1566 int
input_header_jt(ifpter,jt1)1567 input_header_jt(ifpter, jt1)
1568 FILE *ifpter;
1569 struct JT *jt1;
1570 {
1571 	if(
1572    getint(&jt1->syurui , ifpter) == -1 ||
1573 		getint(&jt1->maxcomment, ifpter) == -1 ||
1574 		getint(&jt1->maxhinsi_list, ifpter) == -1 ||
1575 		getint(&jt1->maxserial , ifpter) == -1 ||
1576 		getint(&jt1->maxkanji , ifpter) == -1 ||
1577 		getint(&jt1->maxtable , ifpter) == -1 ||
1578 		getint(&jt1->maxhontai , ifpter) == -1 ||
1579 		getint(&jt1->gosuu , ifpter) == -1 ||
1580 		getnstr(ifpter, WNN_PASSWD_LEN, jt1->hpasswd) == -1 ||
1581 		getint(&jt1->total , ifpter) == -1 ||
1582 		getint(&jt1->maxri1[D_YOMI] , ifpter) == -1 ||
1583 		getint(&jt1->maxri1[D_KANJI] , ifpter) == -1 ||
1584 		getint(&jt1->maxri2, ifpter) == -1 ||
1585 		getnull(ifpter, 56) == -1)
1586 		return(-1);
1587 	return(0);
1588 }
1589 #endif
1590 
1591 #ifdef JS
1592 static
1593 #endif
1594 int
output_header_jt(ofpter,jt1)1595 output_header_jt(ofpter, jt1)
1596 FILE *ofpter;
1597 struct JT *jt1;
1598 {
1599 #ifdef BDIC_WRITE_CHECK
1600 	if ((putint(ofpter, jt1->syurui) == -1) ||
1601 		(putint(ofpter, jt1->maxcomment) == -1) ||
1602 		(putint(ofpter,  jt1->maxhinsi_list) == -1) ||
1603 		(putint(ofpter, jt1->maxserial) == -1) ||
1604 		(putint(ofpter, jt1->maxkanji) == -1) ||
1605 		(putint(ofpter, jt1->maxtable) == -1) ||
1606 		(putint(ofpter, jt1->maxhontai) == -1) ||
1607 		(putint(ofpter, jt1->gosuu) == -1) ||
1608 		(put_n_str(ofpter, jt1->hpasswd, WNN_PASSWD_LEN) == -1) ||
1609 		(putint(ofpter, jt1->total) == -1) ||
1610 		(putint(ofpter, jt1->maxri1[D_YOMI]) == -1) ||
1611 		(putint(ofpter, jt1->maxri1[D_KANJI]) == -1) ||
1612 		(putint(ofpter, jt1->maxri2) == -1) ||
1613 		(putnull(ofpter, 56) == -1)) return(-1);
1614 #else /* BDIC_WRITE_CHECK */
1615 	putint(ofpter, jt1->syurui);
1616 	putint(ofpter, jt1->maxcomment);
1617 	putint(ofpter,  jt1->maxhinsi_list);
1618 	putint(ofpter, jt1->maxserial);
1619 	putint(ofpter, jt1->maxkanji);
1620 	putint(ofpter, jt1->maxtable);
1621 	putint(ofpter, jt1->maxhontai);
1622 	putint(ofpter, jt1->gosuu);
1623 	put_n_str(ofpter, jt1->hpasswd, WNN_PASSWD_LEN);
1624 	putint(ofpter, jt1->total);
1625 	putint(ofpter, jt1->maxri1[D_YOMI]);
1626 	putint(ofpter, jt1->maxri1[D_KANJI]);
1627 	putint(ofpter, jt1->maxri2);
1628 	putnull(ofpter, 56);
1629 #endif /* BDIC_WRITE_CHECK */
1630 	return(0);
1631 }
1632 
1633 /* Header 64 Byte */
1634 #ifndef	JS
1635 int
input_header_hjt(ifpter,hjt1)1636 input_header_hjt(ifpter, hjt1)
1637 FILE *ifpter;
1638 struct HJT *hjt1;
1639 {
1640 	int i;
1641 	if(
1642    input_file_uniq(&hjt1->dic_file_uniq, ifpter) == -1 ||  /* 7 * 4 */
1643 	getint(&hjt1->maxcomment, ifpter) == -1 ||
1644 		getint(&hjt1->maxserial, ifpter) == -1) return(-1);
1645 
1646 	if(getint(&hjt1->hindo_area, ifpter) == -1)
1647 		return(-1);
1648 	if(hjt1->hindo_area != 0) {
1649 		if((hjt1->curserial = (unsigned char *)
1650 			malloc(hjt1->hindo_area * sizeof(unsigned char))) == NULL)
1651 			return(-1);
1652 		for(i = 0; i < hjt1->hindo_area; i++)
1653 			(hjt1->curserial)[i] = vgetc(ifpter);
1654 	} else
1655 		hjt1->curserial = NULL;
1656 
1657 	return(0);
1658 }
1659 #endif
1660 
1661 #ifdef JS
1662 static
1663 #endif
1664 int
output_header_hjt(ofpter,hjt1)1665 output_header_hjt(ofpter, hjt1)
1666 FILE *ofpter;
1667 struct HJT *hjt1;
1668 {
1669 	int i;
1670 #ifdef BDIC_WRITE_CHECK
1671 	if ((output_file_uniq(&hjt1->dic_file_uniq, ofpter) == -1) ||
1672 		(putint(ofpter, hjt1->maxcomment) == -1) ||
1673 		(putint(ofpter, hjt1->maxserial) == -1) ||
1674 		(putint(ofpter, hjt1->hindo_area) == -1)) return(-1);
1675 	for(i = 0; i < hjt1->hindo_area; i++) {
1676 		if(vputc((hjt1->curserial)[i], ofpter) == -1) return(-1);
1677 	}
1678 #else /* BDIC_WRITE_CHECK */
1679 	output_file_uniq(&hjt1->dic_file_uniq, ofpter);
1680 	putint(ofpter, hjt1->maxcomment);
1681 	putint(ofpter, hjt1->maxserial);
1682 	putint(ofpter, hjt1->hindo_area);
1683 	for(i = 0; i < hjt1->hindo_area; i++)
1684 		vputc((hjt1->curserial)[i], ofpter);
1685 #endif /* BDIC_WRITE_CHECK */
1686 	return(0);
1687 }
1688 
1689 
1690 #if	!defined(JS) && !defined(JSERVER)
1691 /* Only use JUTIL */
1692 void
Print_entry(yomi,kstr,cstr,hindo,ima,hinsi,serial,ofpter,esc_exp)1693 Print_entry(yomi, kstr, cstr, hindo, ima, hinsi, serial, ofpter, esc_exp)
1694 	w_char *yomi, *kstr,*cstr;
1695 int serial, hindo, ima, hinsi;
1696 FILE *ofpter;
1697 int esc_exp;
1698 {
1699 	register int len;
1700 	char *k;
1701 	char buf[32];
1702 	static w_char revy[LENGTHKANJI];
1703 	extern void kanji_esc_str();
1704 
1705 	if ((yomi == 0) || (kstr == 0))
1706 		return;
1707 
1708 	if(serial != -1){
1709 		snprintf(buf, sizeof(buf), "%d\t", serial);
1710 		vputs(buf, ofpter);
1711 	}
1712 
1713 	len = put_yomi_str(yomi, ofpter);
1714 	if(len < 8)vputc('\t' , ofpter);
1715 	if(len < 16)vputc('\t' , ofpter);
1716 	vputc('\t' , ofpter);
1717 
1718 	if(esc_exp){
1719 		wnn_Sreverse(revy, yomi);
1720 		kanji_esc_str(kstr, revy, wnn_Strlen(yomi));
1721 		len = put_yomi_str(kstr, ofpter);
1722 	} else{
1723 		len = vputws(kstr, ofpter);
1724 	}
1725 	if(len < 8)vputc('\t' , ofpter);
1726 	if(len < 16)vputc('\t' , ofpter);
1727 	vputc('\t' , ofpter);
1728 
1729 	k = wnn_get_hinsi_name(hinsi);
1730 	if(k){
1731 		vputs(k, ofpter);
1732 		if((int)strlen(k) < 8)vputc('\t' , ofpter);
1733 		vputc('\t' , ofpter);
1734 	} else{
1735 		snprintf(buf, sizeof(buf), "%d\t\t", hinsi);
1736 		vputs(buf, ofpter);
1737 	}
1738 
1739 	if(ima) vputc('*', ofpter);
1740 	if(hindo == -1){    /*  Real hindo == -1 means Not to use it */
1741 		vputs("-", ofpter);
1742 	} else{
1743 		snprintf(buf, sizeof(buf), "%d", hindo);
1744 		vputs(buf, ofpter);
1745 	}
1746 	if(cstr && cstr[0]){
1747 		vputc('\t', ofpter);
1748 		len = vputws(cstr, ofpter);
1749 	}
1750 	vputc('\n', ofpter);
1751 }
1752 #endif
1753 
1754 
1755 #ifdef BDIC_WRITE_CHECK
1756 #undef BDIC_WRITE_CHECK
1757 #endif
1758