File: skkdic-expr.c

package info (click to toggle)
skktools 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,380 kB
  • ctags: 349
  • sloc: sh: 11,551; ansic: 1,300; ruby: 1,049; perl: 798; lisp: 431; python: 241; makefile: 126; awk: 94; cpp: 73; sed: 1
file content (880 lines) | stat: -rw-r--r-- 20,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* SKK JISYO TOOLS (SKK dictionary handling tools)
Copyright (C) 1994, 1996, 1999, 2000
      Hironobu Takahashi, Masahiko Sato, Kiyotaka Sakai, Kenji Yabuuchi

Author: Hironobu Takahashi, Masahiko Sato, Kiyotaka Sakai, Kenji Yabuuchi
Maintainer: Mikio Nakajima <minakaji@osaka.email.ne.jp>
Version: $Id: skkdic-expr.c,v 1.22 2013/01/08 12:24:00 skk-cvs Exp $
Keywords: japanese
Last Modified: $Date: 2013/01/08 12:24:00 $

This file is part of Daredevil SKK.

SKK JISYO TOOLS are free software; you can redistribute them and/or modify
them under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

SKK JISYO TOOLS are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SKK; see the file COPYING.  If not, write to
the Free Software Foundation,  Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */

/* skkdic-expr.c
   ���Υץ���� SKK �μ���Υޡ���������Ԥ��ޤ���
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_MKDTEMP
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#endif /* HAVE_MKDTEMP */

#ifdef HAVE_LIBDB
#define DB_DBM_HSEARCH 1
#include <db.h>
#else /* not HAVE_LIBDB */
#ifdef HAVE_GDBM_NDBM_H
#include <gdbm/ndbm.h>
#else /* not HAVE_GDBM_NDBM_H */
#ifdef HAVE_NDBM_H
#include <ndbm.h>
#else /* not HAVE_NDBM_H */
#ifdef HAVE_DB1_NDBM_H
#include <db1/ndbm.h>
#endif /* HAVE_DB1_NDBM_H */
#endif /* HAVE_NDBM_H */
#endif /* HAVE_GDBM_NDBM_H */
#endif /* HAVE_LIBDB */

#include <errno.h>
#include <signal.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifndef errno
extern int errno;
#endif

/* ���ΰ���ե�����ξ��˽�ʬ�ʶ������ʤ���С��ѹ��������������Ǥ��礦 */
/* �� #define TMPDIR "." */

#ifndef TMPDIR
#define TMPDIR "/tmp"
#endif

/* 1994 ǯ�Ǥ� SKK ����ǤϺ�Ĺ�ιԤǤ� 656 ʸ���Ǥ��������Τ����
   �ʲ����ͤȤ��Ƥ��ޤ���*/

#ifdef MAXLINE
#define BLEN MAXLINE
#else
#define BLEN 65536
#endif

/* ����ѥե�����̾ */
char file_name[256];
char okuri_tail_name[256];
char okuri_head_name[256];
char tmpsubdir[256];

/* ����ѥǡ����١��� */
DBM *db;
DBM *okuriheaddb;
DBM *okuritaildb;
FILE *dbcontent;

/* snprintf() ���ʤ��Ķ��Τ������� */
#ifndef HAVE_SNPRINTF
/* #include <stdio.h> */
#include <stdarg.h>
int snprintf(char *s, size_t maxlen, const char *format, ...)
{
    va_list ap;
    int n;

    va_start(ap, format);
    n = vsprintf(s, format, ap);
    va_end(ap);
    return n;
}
#endif

/* ���꤬�ʤ��Ĥ�������ȥ��ޤ�ƽ�����Ԥ碌�뤫�ɤ��� */
int	okurigana_flag;

static int add_content_line();
static void subtract_content_line();

/* Part1: ����ǡ����١����������ץ��� */

/* �ե�������
   ���Υե����뤬�ʤ��Ƥ⤫�ޤ�ʤ�������ʳ������꤬����Хץ������� */
static void db_remove_file(fname)
     char *fname;
{
    if (unlink(fname) == -1)
	if (errno != ENOENT) {
	    perror(fname);
	    exit(1);
	}
}

/* ����ѥǡ����١����ե��������
   file_name �ˤ� content ����Ǽ����� */
static void db_remove_files()
{
    char pag_name[256];
    char dir_name[256];

    db_remove_file(file_name);
    snprintf(pag_name, sizeof(pag_name), "%s.pag", file_name);
    db_remove_file(pag_name);
    snprintf(dir_name, sizeof(dir_name), "%s.dir", file_name);
    db_remove_file(dir_name);
    snprintf(dir_name, sizeof(dir_name), "%s.db", file_name);
    db_remove_file(dir_name);

    if (okurigana_flag) {
	db_remove_file(okuri_head_name);
	snprintf(pag_name, sizeof(pag_name), "%s.pag", okuri_head_name);
	db_remove_file(pag_name);
	snprintf(dir_name, sizeof(dir_name), "%s.dir", okuri_head_name);
	db_remove_file(dir_name);
	snprintf(dir_name, sizeof(dir_name), "%s.db", okuri_head_name);
	db_remove_file(dir_name);

	db_remove_file(okuri_tail_name);
	snprintf(pag_name, sizeof(pag_name), "%s.pag", okuri_tail_name);
	db_remove_file(pag_name);
	snprintf(dir_name, sizeof(dir_name), "%s.dir", okuri_tail_name);
	db_remove_file(dir_name);
	snprintf(dir_name, sizeof(dir_name), "%s.db", okuri_tail_name);
	db_remove_file(dir_name);
    }
}

/* �ǡ����١����ե��������� */
static void db_make_files()
{
    db_remove_files();
    if ((db = dbm_open(file_name, O_RDWR|O_CREAT, 0600)) == NULL){
	perror(file_name);
	exit(1);
    }
    if ((dbcontent = fopen(file_name, "w+")) == NULL){
	perror(file_name);
	exit(1);
    }
    if (okurigana_flag) {
	if ((okuriheaddb = dbm_open(okuri_head_name, O_RDWR|O_CREAT, 0600)) 
	    == NULL){
	    perror(okuri_head_name);
	    exit(1);
	}
	if ((okuritaildb = dbm_open(okuri_tail_name, O_RDWR|O_CREAT, 0600)) 
	    == NULL){
	    perror(okuri_tail_name);
	    exit(1);
	}
    }
}

/* �ǡ����١����ե�����˿������ɲä��� */
static void db_append(key, new, dbm)
     datum key;
     unsigned char *new;
     DBM *dbm;
{
    long pos;
    datum content;

    fseek(dbcontent, 0L, 2);
    pos = ftell(dbcontent);
    fwrite(new, strlen(new)+1, 1, dbcontent);
    content.dptr = (char *)&pos;
    content.dsize = sizeof pos;
    dbm_store(dbm, key, content, DBM_REPLACE);
}

/* �ǡ����١�����Υݥ�������׻����� (alignment ������) */
static long getpos(ptr)
     unsigned char *ptr;
{
    long i;
    union long_dat {
	long pos;
	unsigned char c[sizeof i];
    } dat;
    for (i = 0; i < (sizeof i); ++ i)
	dat.c[i] = ptr[i];
    return dat.pos;
}

/* �ǡ����١����ե���������Ƥ�����꾮������Τ��ִ����� */
static void db_replace(ptr, new)
     unsigned char *ptr;
     unsigned char *new;
{
    fseek(dbcontent, getpos(ptr), 0);
    fwrite(new, strlen(new)+1, 1, dbcontent);
}

/* �ǡ����١����ե����뤫��ʸ������� */
static void db_gets(ptr, len, fp)
     unsigned char *ptr;
     int len;
     FILE *fp;
{
    while((*(ptr++) = getc(fp)) != '\0');
}

/* Part2: ��������� */

RETSIGTYPE
signal_handler (signo)
     int signo;
{
    db_remove_files();
    rmdir(tmpsubdir);
    signal(signo, SIG_DFL);
    kill(getpid(), signo);
}

RETSIGTYPE
set_signal_handler()
{
#ifdef SIGQUIT
    if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
	signal(SIGQUIT, signal_handler);
#endif
    if (signal(SIGINT, SIG_IGN) != SIG_IGN)
	signal(SIGINT, signal_handler);
#ifdef SIGALRM
    if (signal(SIGALRM, SIG_IGN) != SIG_IGN)
	signal(SIGALRM, signal_handler);
#endif
#ifdef SIGHUP
    if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
	signal(SIGHUP, signal_handler);
#endif
    if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
	signal(SIGSEGV, signal_handler);
#ifdef SIGBUS
    if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
	signal(SIGBUS, signal_handler);
#endif
}

/* Part3: ����ե�����κ��� */

/* base ��ʸ����� *s �� *e �˶��ޤ줿ʸ���󤬤��뤫�ɤ���Ĵ�٤�
   0- �ʤ�  1- ���� */
static int match_item(base, s, e)
     unsigned char *base;
     unsigned char *s;
     unsigned char *e;
{
    unsigned char *p, *q1, *q2;

    for (p = base; *p != '\0'; ++ p) {
	if (*p == *s) {
	    for(q1 = p+1, q2 = s+1; q2 <= e; ++ q1, ++ q2)
		if (*q1 != *q2) goto next;
	    return 1; /* matched */
	}
      next:;
    }
    return 0;
}

/* base ��ʸ����� *s �� *e �˶��ޤ줿ʸ������ɲ� */
static void append_item(base, s, e)
     unsigned char *base;
     unsigned char *s;
     unsigned char *e;
{
    unsigned char *p, *q;

    for (p = base; *p >= 0x20; ++ p);
    for (q = s+1; q <= e; ++ q, ++ p)
	*p = *q;
    *p = '\0';
}

/* �����꤬�ʥ���ȥ�ϡ�"��ꤳm��"�Τ褦�ʥ����������
 * �����ͤϡ�"/����/���/"�Ȥʤ�
 */
void add_okuri_item(key, s)
     datum 		*key;
     unsigned char 	*s;
{
    unsigned char	*p, *headtop;
    unsigned char	keybuf[BLEN];
    unsigned char	content[BLEN];
    unsigned char	new[BLEN];

    int			len;
    datum		otails, oheads;
    datum		tkey, hkey;

    /* ���Ф��򥳥ԡ� */
    strncpy(keybuf, key->dptr, key->dsize);
    tkey.dptr = (char *)keybuf;
    headtop = keybuf + key->dsize;	/* ������ɤ��˥��ԡ����뤫 */

    /* ������tkey�˥��ԡ� */
    for (p = headtop; *s != '/'; s++, p++) {
	if (*s < 0x20) return;
	*p = *s;
    }
    tkey.dsize = p - keybuf;

    /* �촴��ʬ��content�˥��ԡ����� */
    p = content; 
    for( ; *s != ']'; s++, p++) {
	if (*s < 0x20) return;
	*p = *s;
    }
    *p = '\0';
    if (*++s != '/') 
	return ;		/* �ե����ޥåȥ��顼 */
    
    /* �Ť���Τ���٤�ɬ�פʤ�append */
    otails = dbm_fetch(okuriheaddb, tkey);
    if (otails.dptr == NULL)  {
	db_append(tkey, content, okuriheaddb);
    } else {
	fseek(dbcontent, getpos(otails.dptr), 0);
	db_gets(new, BLEN, dbcontent);
	if (add_content_line(new, content, NULL))
	    db_append(tkey, new, okuriheaddb);
    }
}

/* tails�ǻؤ��Ƥ���ʸ�����line�����Ƥ��������������ɲ�
 * ���ΤȤ�ʸ�����"/[������/[������/"�Τ褦�ʷ����ȤʤäƤ���
 */
static int add_okuri_tail_line(tails, line)
     unsigned char 	*tails, *line;
{
    unsigned char     	*s, *e;
    int n_add;

    n_add = 0;
    for(s = line; (0x20 <= *s); ++ s) {
	if (*s == '/') {
	    if (s[1] == '[') {
		for(e = s+2; (0x20 <= *e); ++e) {
		    if (*e == '/') {
			if (!match_item(tails, s, e)) {
			    append_item(tails, s, e);
			    n_add ++;
			}
			s = e-1;
			goto next;
		    }
		}
		return n_add;
	    }
	}
      next:;
    }
    return n_add;
}

/* base �������Ƥ���ʸ����� new �����Ƥǽ�ʣ���ʤ���Τ�ä���
   ����ä��ʤ���� 0 �򡢤���ʳ��ϲä����ϸ����֤� */
static int add_content_line(base, new, key)
     unsigned char *base;
     unsigned char *new;
     datum   	   *key;
{
    unsigned char *s, *e;
    int n_add;

    n_add = 0;
    for(s = new; (0x20 <= *s); ++ s) {
	if (*s == '/') {
	    if (s[1] == '[') {
		if (okurigana_flag) 
		    add_okuri_item(key, s + 2);
		for(s++ ; *s != ']'; ++s)
		    if (*s < 0x20) return n_add;
		goto next;
	    }
	    for(e = s+1; (0x20 <= *e); ++ e) {
		if (*e == '/') {
		    if (!match_item(base, s, e)) {
			append_item(base, s, e);
			n_add ++;
		    }
		    s = e-1;
		    goto next;
		}
	    }
	    return n_add;
	}
      next:;
    }
    return n_add;
}

/* �����ĤΡֽ����פ����뤫���� */
static int item_number(p)
     unsigned char *p;
{
    int n = 0;
    for (p++ ; *p >= 0x20; ++ p)
	if (*p == '/') ++ n;
    return n;
}

static int line_process(buffer, key, kanji, tails)
     unsigned char *buffer;
     datum *key;
     unsigned char *kanji;
     unsigned char *tails;
{
    int key_len;

    if ((buffer[0] == ';') || (buffer[0] == '\0')) return 0;
    for (key_len = 1;
	(buffer[key_len] != ' ') || (buffer[key_len+1] != '/');
	++ key_len)
	if (buffer[key_len] == '\0') return 0;
    key->dptr = buffer;
    key->dsize = key_len;

    kanji[0] = tails[0] = '/';
    kanji[1] = tails[1] = '\0';
    add_content_line(kanji, buffer+key_len+1, key);
    if (okurigana_flag) 
	add_okuri_tail_line(tails, buffer+key_len+1);
    return item_number(kanji);
}

/* ���ꤵ�줿̾���Υե���������Ƥ�ä��� */
static void add_file(srcname)
     char *srcname;
{
    static unsigned char buffer[BLEN], kanji[BLEN], new[BLEN];
    static unsigned char tails[BLEN], tkeybuf[BLEN];
    datum key, old, tkey;
    FILE *fp;

    if ((fp = fopen(srcname, "r")) == NULL) {
	perror(srcname);
	return;
    }
    while(fgets(buffer, BLEN, fp) != NULL) {
	if (line_process(buffer, &key, kanji, tails) > 0) {
	    if (okurigana_flag) {
		/* ��������Ͽ */
		if (tails[1] != '\0') {
		    old = dbm_fetch(okuritaildb, key);
		    if (old.dptr == NULL) {
			db_append(key, tails, okuritaildb);
		    } else {
			fseek(dbcontent, getpos(old.dptr), 0);
			db_gets(new, BLEN, dbcontent);
			if (add_okuri_tail_line(new, tails))
			    db_append(key, new, okuritaildb);
		    }
		}
	    }

	    old = dbm_fetch(db, key);
	    if (old.dptr == NULL) {
		db_append(key, kanji, db);
	    } else {
		fseek(dbcontent, getpos(old.dptr), 0);
		db_gets(new, BLEN, dbcontent);
		if (add_content_line(new, kanji, NULL))
		    db_append(key, new, db);
	    }
	}
    }
    fclose(fp);
}

static void
delete_item(base, s, e)
     unsigned char *base;
     unsigned char *s;
     unsigned char *e;
/* base ��ʸ������� *s �� *e �˶��ޤ줿ʸ���󤬤���к�� */
{
    unsigned char *p, *q1, *q2;

    for (p = base; *p != '\0'; ++ p) {
	if (*p == *s) {
	    for(q1 = p+1, q2 = s+1; q2 <= e; ++ q1, ++ q2)
		if (*q1 != *q2) goto next;
	    /* matched */
	    for(q2 = p+1; *q1 != '\0'; ++ q1, ++ q2)
		*q2 = *q1;
	    *q2 = '\0';
	    return;
	}
      next:;
    }
}

/* �����꤬�ʥ���ȥ�ϡ�"��ꤳm��"�Τ褦�ʥ�������Ĥ��Ȥˤʤ�
 * �����ͤϡ�"/����/���/"�Ȥʤ�
 */
void subtract_okuri_item(key, s)
     datum 		*key;
     unsigned char 	*s;
{
    unsigned char	*p, *headtop;
    unsigned char	keybuf[BLEN];
    unsigned char	content[BLEN];
    unsigned char	new[BLEN];

    int			len;
    datum		otails, oheads;
    datum		tkey, hkey;

    /* ���Ф��򥳥ԡ� */
    strncpy(keybuf, key->dptr, key->dsize);
    tkey.dptr = (char *)keybuf;
    headtop = keybuf + key->dsize;	/* ������ɤ��˥��ԡ����뤫 */

    /* ������tkey�˥��ԡ� */ 
    for (p = headtop; *s != '/'; s++, p++) {
	if (*s < 0x20) return;
	*p = *s;
    }
    tkey.dsize = p - keybuf;

    /* �촴��ʬ��content�˥��ԡ����� */
    p = content; 
    for( ; *s != ']'; s++, p++) {
	if (*s < 0x20) return;
	*p = *s;
    }
    *p = '\0';
    if (*++s != '/') 
	return ;		/* �ե����ޥåȥ��顼 */
    
    /* �Ť���Τ���٤�ɬ�פʤ�replace/delete */
    otails = dbm_fetch(okuriheaddb, tkey);
    if (otails.dptr != NULL)  {
	fseek(dbcontent, getpos(otails.dptr), 0);
	db_gets(new, BLEN, dbcontent);
	subtract_content_line(new, content, NULL);
	if (strlen(new) >= 3)
	    db_replace(otails.dptr, new);
	else
	    dbm_delete(okuriheaddb, tkey);
    }
}

/* tail�ǻؤ��Ƥ���ʸ���󤫤�line�˴ޤޤ���������
 */
void subtract_okuri_tail_line(tails, line)
     unsigned char 	*tails, *line;
{
    unsigned char     	*s, *e;

    for(s = line; (0x20 <= *s); ++ s) {
	if (*s == '/') {
	    if (s[1] == '[') {
		for(e = s+2; (0x20 <= *e); ++e) {
		    if (*e == '/') {
			delete_item(tails, s, e);
			s = e-1;
			goto next;
		    }
		}
		return;
	    }
	}
      next:;
    }
    return;
}

static void
subtract_content_line(base, new, key)
     unsigned char *base;
     unsigned char *new;
     datum	   *key;
/* base ��ʸ�����椫�� new ���ʸ������� */
{
    unsigned char *s, *e;

    for(s = new; (0x20 <= *s); ++ s) {
	if (*s == '/') {
	    if (s[1] == '[') {
		if (okurigana_flag)
		    subtract_okuri_item(key, s + 2);
		for(s++; *s != ']'; ++s)
		    if (*s < 0x20) return;
		goto next;
	    }
	    for(e = s+1; (0x20 <= *e); ++ e) {
		if (*e == '/') {
		    delete_item(base, s, e);
		    s = e-1;
		    goto next;
		}
	    }
	    return;
	}
      next:;
    }
}

/* Ϳ����̾���μ�������Ƥ򸽺ߤμ��񤫤������� */
static void subtract_file(srcname)
     char *srcname;
{
    static unsigned char buffer[BLEN], kanji[BLEN], new[BLEN];
    static unsigned char tails[BLEN], tkeybuf[BLEN];
    datum key, old, tkey;
    FILE *fp;

    if ((fp = fopen(srcname, "r")) == NULL) {
	perror(srcname);
	return;
    }
    while(fgets(buffer, BLEN, fp) != NULL) {
	/* �Ԥ�����ɤ�(key)�פȡֽ���(content)�פ���Ф���
	   �⤷�⥳���ȹ����Ǥ������Ф� */
	if (line_process(buffer, &key, kanji, tails) > 0) {
	    /* ����ˤ��Ǥˤ���Ф��줫�������롣�ʤ���в��⤷�ʤ� */

	    if (okurigana_flag) {
		if (tails[1] != '\0') {
		    old = dbm_fetch(okuritaildb, key);
		    if (old.dptr != NULL) {
			fseek(dbcontent, getpos(old.dptr), 0);
			db_gets(new, BLEN, dbcontent);
			subtract_okuri_tail_line(new, tails);
			if (strlen(new) >= 3)
			    db_replace(old.dptr, new);
			else
			    dbm_delete(okuritaildb, key);
		    }
		}
	    }

	    old = dbm_fetch(db, key);
	    if (old.dptr != NULL) {
		fseek(dbcontent, getpos(old.dptr), 0L);
		db_gets(new, BLEN, dbcontent);
		subtract_content_line(new, kanji, &key);
		if (strlen(new) >= 3)
		    db_replace(old.dptr, new);
		else 
		    dbm_delete(db, key);
	    }
	}
    }
    fclose(fp);
}

void okuri_type_out(key, output)
     datum	*key;
     FILE	*output;
{
    unsigned char	*s, *e, *headtop;
    unsigned char	keybuf[BLEN];
    unsigned char	tail_content[BLEN];
    unsigned char	head_content[BLEN];

    datum	tails, tkey;
    datum	one;

    /* ���Ф��򥳥ԡ� */
    strncpy(keybuf, key->dptr, key->dsize);
    tkey.dptr = (char *)keybuf;
    tkey.dsize = key->dsize;
    headtop = keybuf + tkey.dsize;	/* ������ɤ��˥��ԡ����뤫 */

    tails = dbm_fetch(okuritaildb, tkey);
    if (tails.dptr == NULL)  {
	return;
    } else {
	fseek(dbcontent, getpos(tails.dptr), 0);
	db_gets(tail_content, BLEN, dbcontent);

	s = tail_content + 2; 		/* '/'��'['��ȤФ� */
	for(e = s; e[1] != '\0'; s = e + 2) {
	    for (e = s; *e != '/'; e++)
		if (*e < 0x20) 
		    return;

	    strncpy(headtop, s, e - s);
	    tkey.dsize = (headtop - keybuf) + (e - s);
	    one = dbm_fetch(okuriheaddb, tkey);
	    if (one.dptr == NULL) {
		continue;
	    } else {
		fseek(dbcontent, getpos(one.dptr), 0);
		db_gets(head_content, BLEN, dbcontent);

		putc('[', output);
		while (s != e)
		    putc(*s++, output);		    
		fputs(head_content, output);
		fputs("]/", output);
	    }
	}
    }
}

/* ��̤����
   ����Ϥޤä����ΤǤ����ˤʤ�Τǡ��ǽ�Ū�ʷ�̤�����ˤ�
   skkdic-sort ���Ѥ���
   */
static void type_out(output)
     FILE *output;
{
    datum key, content;
    int i;
    unsigned char kanji[BLEN];

    for (key = dbm_firstkey(db); key.dptr !=  NULL;  key = dbm_nextkey(db)) {
	content = dbm_fetch(db, key);
	for(i = 0; i < key.dsize; ++ i)
	    putc(((char *)key.dptr)[i], output);
	putc(' ', output);
	fseek(dbcontent, getpos(content.dptr), 0);
	db_gets(kanji, BLEN, dbcontent);
	fputs(kanji, output);
	if (okurigana_flag)
	    okuri_type_out(&key, output);
	putc('\n', output);
    }

    if (okurigana_flag) {
	datum 	entry;
	for (key = dbm_firstkey(okuritaildb); 
	     key.dptr !=  NULL;  key = dbm_nextkey(okuritaildb)) {
	    entry = dbm_fetch(db, key);
	    if (entry.dptr != NULL) continue;

	    for(i = 0; i < key.dsize; ++ i)
		putc(((char *)key.dptr)[i], output);
	    putc(' ', output);
	    putc('/', output);
	    okuri_type_out(&key, output);
	    putc('\n', output);
	}
    }
}

/* ����ˡ��ɽ�� */
static void print_usage(argc, argv)
     int argc;
     char **argv;
{
    fprintf(stderr,
	    "Usage: %s [-d workdir] [-o output] [-O] jisyo1 [[+-] jisyo2]...\n",
	    argv[0]);
}

/* �ᥤ��ץ���  ������������ */
int main(argc, argv)
     int argc;
     char **argv;
{
    int negate, i;
    FILE *output;
    char *tmpdir;

    output = stdout;

    tmpdir = getenv("TMPDIR");
    if (tmpdir == NULL) {
      tmpdir = TMPDIR;
    }

    /* ������ */
    for (i = 1; i < argc; ++ i) {
	if (argv[i][0] == '-') {
	    if (argv[i][1] == 'd') { /* -d ��ȥǥ��쥯�ȥ� */
		tmpdir=argv[i+1];
		i ++;
	    } else if (argv[i][1] == 'o') { /* -o ���ϥե����� */
		if ((output = fopen(argv[i+1], "w")) == NULL) {
		    perror(argv[i+1]);
		    exit(1);
		}
		i ++;
	    } else if (argv[i][1] == 'O') { /* ���꤬�ʤν����λ���  */
		okurigana_flag++;
	    } else {
		print_usage(argc, argv);
		exit(1);
	    }
	} else {
	    break;
	}
    }

    if (i >= argc) { /* ���񤬻��ꤵ��Ƥ��ʤ� */
	print_usage(argc, argv);
	exit(1);
    }

#ifdef HAVE_MKDTEMP
    snprintf(tmpsubdir, sizeof(tmpsubdir), "%s/skkdicXXXXXX", tmpdir);
    if (mkdtemp(tmpsubdir) == NULL) {
#else /* not HAVE_MKDTEMP */
    snprintf(tmpsubdir, sizeof(tmpsubdir), "%s/skkdic%d", tmpdir, getpid());
    if (mkdir(tmpsubdir, 0700)) {
#endif /* not HAVE_MKDTEMP */
	perror(tmpsubdir);
	exit(1);
    }
    tmpdir = tmpsubdir;
    snprintf(file_name, sizeof(file_name), "%s/skkdic%d", tmpdir, getpid());
    if (okurigana_flag) {
	snprintf(okuri_head_name, sizeof(okuri_head_name), "%s/skkhead%d", tmpdir, getpid());
	snprintf(okuri_tail_name, sizeof(okuri_tail_name), "%s/skktail%d", tmpdir, getpid());
    }
    set_signal_handler();
    db_make_files();

    negate = 0;
    for (; i < argc; ++ i) {
	if (argv[i][0] == '+') {
	    negate = 0;
	    if (strlen(argv[i]) > 1)
		add_file(argv[i]+1);
	} else if (argv[i][0] == '-') {
	    negate = 1;
	    if (strlen(argv[i]) > 1) {
		subtract_file(argv[i]+1);
		negate = 0;
	    }
	} else {
	    if (negate == 0)
		add_file(argv[i]);
	    else
		subtract_file(argv[i]);
	}
    }
    type_out(output);
    db_remove_files();
    rmdir(tmpsubdir);
    return 0;
}

/* end of skkdic-expr.c */