Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 282904 | Differences between
and this patch

Collapse All | Expand All

(-)xjdic.orig/exjdxgen.c (-1 / +1 lines)
Lines 22-28 Link Here
22
#include <sys/stat.h>
22
#include <sys/stat.h>
23
23
24
#include <stdio.h>
24
#include <stdio.h>
25
/*#include <stdlib.h>*/
25
#include <stdlib.h>
26
#include <ctype.h>
26
#include <ctype.h>
27
#include <string.h>
27
#include <string.h>
28
#include "xjdic.h"
28
#include "xjdic.h"
(-)xjdic.orig/exjdxgen.c~ (+305 lines)
Line 0 Link Here
1
/**************************************************************************
2
*                   E X J D X G E N
3
*                                                   Author: Jim Breen
4
*
5
*   This is the Unix version of EJDXGEN, ported from MS-DOS
6
***************************************************************************/
7
/*  This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 1, or (at your option)
10
    any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
20
21
#include <sys/types.h>
22
#include <sys/stat.h>
23
24
#include <stdio.h>
25
/*#include <stdlib.h>*/
26
#include <ctype.h>
27
#include <string.h>
28
#include "xjdic.h"
29
30
#define TRUE 1
31
#define FALSE 0
32
#define EXLIM 100
33
34
unsigned char *db;
35
unsigned char ENVname[50];
36
unsigned char *dicenv;
37
struct stat *buf;
38
unsigned long dbyte;
39
unsigned long  *jindex;
40
unsigned long indptr,llone,charno,recpos,charnost;
41
int State;
42
unsigned char Dname[80] = {"edictext"};
43
unsigned char JDXname[80] = {"edictext.xjdx"};
44
int jiver = 14;		/*The last time the index structure changed was Version1.4*/
45
46
/*====== prototypes=================================================*/
47
int stringcomp(unsigned char *s1, unsigned char *s2);
48
void jqsort(long i, long j);
49
int Kstrcmp(unsigned long lhs, unsigned long rhs);
50
/*====== end prototypes=================================================*/
51
52
int stringcomp(unsigned char *s1, unsigned char *s2)
53
{
54
	int i;
55
	unsigned char c1,c2;
56
57
	for(i = 0; i < strlen(s1);i++) 
58
	{
59
		c1 = s1[i];
60
		if (c1 < 0x60) c1 = (c1|0x20);
61
		c2 = s2[i];
62
		if (c2 < 0x60) c2 = (c2|0x20);
63
		if (c1 != c2) return(1);
64
	}
65
	return (0);
66
}
67
68
/*====function to Load Dictionary and load/create index table=======*/
69
main(argc,argv)
70
int argc;
71
unsigned char **argv;
72
{
73
  FILE *fp,*fopen();
74
  unsigned long schi,diclen,indlen;
75
  int i,inwd,saving,nodread;
76
  unsigned char c;
77
  unsigned char **ap;
78
79
  printf("\nEXJDXGEN V2.0 Extension Index Table Generator for XJDIC. \n      Copyright J.W. Breen, 1995\n");
80
  if (argc > 1)
81
  {
82
	ap = argv;
83
	ap++;
84
	if(strcmp(*ap,"-h") == 0)
85
	{
86
		printf("\nThere are two command-line options:\n");
87
		printf("  -h  this display\n");
88
		printf("  filename - file to be indexed\n\n");
89
		exit(0);
90
	}
91
	strcpy(Dname,*ap);
92
	strcpy(JDXname,*ap);
93
	strcat(JDXname,".xjdx");
94
    printf("Commandline request to use files %s and %s \n",Dname,JDXname);
95
  }
96
  inwd = FALSE;
97
  indptr = 1;
98
  llone = 1;
99
  buf = (void *)malloc(1000);
100
  if(stat(Dname, buf) != 0)
101
  {
102
	 perror(NULL);
103
	 printf("Cannot stat: %s \n",Dname);
104
	 exit(1);
105
  }
106
  diclen = buf->st_size;
107
  printf("\nWARNING!!  This program may take a long time to run .....\n");
108
109
  puts ("\nLoading Dictionary file.  Please wait.....\n");
110
  fp=fopen(Dname,"rb");
111
  if (fp==NULL )
112
  {
113
	printf("\nCannot open dictionary file\n");
114
	exit(1);
115
  }
116
  db = (unsigned char *)malloc((diclen+100) * sizeof(unsigned char));
117
  if(db == NULL)
118
  {
119
      fprintf(stderr,"malloc() for dictionary failed.\n");
120
      fclose(fp);
121
      exit(1);
122
  }
123
  nodread = diclen/1024;
124
  dbyte = fread((unsigned char *)db+1, 1024, nodread, fp);
125
  nodread = diclen % 1024;
126
  dbyte = fread((unsigned char *)(db+(diclen/1024)*1024)+1, nodread,1, fp);
127
  fclose(fp);
128
  diclen++;
129
  dbyte = diclen;
130
  db[diclen] = 10;
131
  db[0] = 10;
132
  printf("Dictionary size: %ld bytes.\n",dbyte);
133
  indlen = diclen / 2;
134
  jindex = (unsigned long *)malloc(indlen);
135
  if(jindex == NULL)
136
  {
137
	  fprintf(stderr,"malloc() for index table failed.\n");
138
	  fclose(fp);
139
	  exit(1);
140
  }
141
  printf("Parsing.... \n");
142
  indptr = 1;
143
  saving = FALSE;
144
  charno = -1;
145
  State = 0;
146
  for (schi =0; schi < dbyte; schi++) /* scan whole dictionary  */
147
  {
148
	  c = db[schi];
149
	  charno++;
150
	  if (c == 0x0a)
151
	  {
152
		recpos = charno+1;
153
		continue;
154
	  }
155
	  switch (State)
156
	  {
157
	  case 0 :	/* Looking for < 	*/
158
		if (c == '<') State = 1;
159
		break;
160
	  case 1 :	/* Inside <..>, but nothing started yet	*/
161
		if (c >= 127)
162
		{
163
			saving = TRUE;
164
			charnost = charno-1;
165
			State = 2;
166
			break;
167
		}
168
		else
169
		{
170
			State = 3;
171
	  		schi--;
172
	  		charno--;
173
			break;
174
		}
175
	case 2 :	/* storing keywords  */
176
		if (c >= 127)
177
		{
178
			break;
179
		}
180
		else
181
		{
182
			jindex[indptr] = charnost;
183
			jindex[indptr+1] = recpos;
184
			indptr+=2;
185
			if (indptr > indlen/sizeof(long))
186
			{
187
			  	printf("Index table overflow. Dictionary too large?\n");
188
			  	exit(1);
189
			}
190
			saving = FALSE;
191
	  		schi--;
192
	  		charno--;
193
			State = 3;
194
			break;
195
		}
196
	case 3 : 	/* encountered non-JIS	*/
197
		if (c == '>')
198
		{
199
			State = 0;
200
			break;
201
		}
202
		if (c == '[')
203
		{
204
			State = 4;
205
			break;
206
		}
207
		if (c >= 127)
208
		{
209
			schi--;
210
  			charno--;
211
			State = 1;
212
		}
213
		break;
214
	case 4 :	/* skip all until ]		*/
215
		if (c == ']') State = 1;
216
		break;
217
	}
218
    }
219
    indptr-=2;
220
    printf("Index entries: %ld  \nSorting (this is slow)......\n",indptr);
221
    jqsort(llone,(indptr/2)+1);
222
    printf("Sorted\nWriting index file ....\n");
223
    fp = fopen(JDXname,"wb");
224
    if (fp==NULL )
225
    {
226
    	printf("\nCannot open %s output file\n",JDXname);
227
    	exit(1);
228
    }
229
  jindex[0] = diclen+jiver;
230
  fwrite(jindex,sizeof(long),indptr+2,fp);
231
  fclose(fp);
232
}
233
/*======function to sort jindex table====================*/
234
235
void jqsort(long lhsr, long rhsr)
236
{
237
	long i,last,midp,lhs,rhs;
238
	unsigned long temp,temp2;
239
240
	lhs = ((lhsr-1)*2)+1;
241
	rhs = ((rhsr-1)*2)+1;
242
	if (lhs >= rhs) return;
243
	/* Swap ( lhs , (lhs+rhs)/2);*/
244
	midp = (lhs+rhs)/2;
245
	if (!(midp & 1)) midp--;
246
	temp = jindex[lhs];
247
	temp2 = jindex[lhs+1];
248
	jindex[lhs] = jindex[midp];
249
	jindex[lhs+1] = jindex[midp+1];
250
	jindex[midp] = temp;
251
	jindex[midp+1] = temp2;
252
	last = lhs;
253
	for (i = lhs+2;i <= rhs; i+=2)
254
		{
255
			if (Kstrcmp(jindex[i],jindex[lhs]) < 0)
256
			{
257
				/* Swap(++last,i);*/
258
				last+=2;
259
				temp = jindex[i];
260
				jindex[i] = jindex[last];
261
				jindex[last] = temp;
262
				temp = jindex[i+1];
263
				jindex[i+1] = jindex[last+1];
264
				jindex[last+1] = temp;
265
			}
266
		}
267
/*	Swap (lhs,last);*/
268
	temp = jindex[lhs];
269
	jindex[lhs] = jindex[last];
270
	jindex[last] = temp;
271
	temp = jindex[lhs+1];
272
	jindex[lhs+1] = jindex[last+1];
273
	jindex[last+1] = temp;
274
	jqsort((lhs/2)+1,last/2);
275
	jqsort((last/2)+2,(rhs/2)+1);
276
}
277
/*=====string comparison used by jqsort==========================*/
278
int Kstrcmp(unsigned long lhs, unsigned long rhs)
279
{
280
	int i,c1,c2;
281
/* effectively does a strnicmp on two "strings" within the dictionary,
282
   except it will make katakana and hirgana match (EUC A4 & A5) */
283
284
	for (i = 0; i<20 ; i++)
285
	{
286
		c1 = db[lhs+i+1];
287
		c2 = db[rhs+i+1];
288
		if ((i % 2) == 0)
289
		{
290
			if (c1 == 0xA5)
291
			{
292
				c1 = 0xA4;
293
			}
294
			if (c2 == 0xA5)
295
			{
296
				c2 = 0xA4;
297
			}
298
		}
299
		if ((c1 >= 'A') && (c1 <= 'Z')) c1 |= 0x20;
300
		if ((c2 >= 'A') && (c2 <= 'Z')) c2 |= 0x20;
301
		if (c1 != c2 ) break;
302
	}
303
	return(c1-c2);
304
}
305
(-)xjdic.orig/Makefile (-1 / +2 lines)
Lines 54-60 Link Here
54
#
54
#
55
#LIBS= -lsocket -lnsl
55
#LIBS= -lsocket -lnsl
56
#
56
#
57
CC=gcc -g
57
#CC=gcc -g
58
CC=gcc
58
all: xjdic_sa xjdic_cl xjdserver xjdxgen exjdxgen
59
all: xjdic_sa xjdic_cl xjdserver xjdxgen exjdxgen
59
client: xjdic_cl
60
client: xjdic_cl
60
server: xjdserver
61
server: xjdserver
(-)xjdic.orig/Makefile~ (+144 lines)
Line 0 Link Here
1
########################################################################
2
#
3
#       X J D I C
4
#
5
#######################################################################
6
# This is the makefile for xjdic/xjdserver/xjdxgen V2.4
7
# 
8
# to use this file, you have several options:
9
#	make all - does all the binaries
10
#	make client - just makes the xjdic_cl binary
11
#	make server - just makes the xjdserver server
12
#	make stand - just makes the xjdic_sa stand-alone version
13
#	make xjdxgen - just makes the xjdxgen index generator
14
#	make exjdxgen - just makes the exjdxgen extension file index generator
15
#	make clean - just tidies up, removing the .o files.
16
#	make realclean - deletes everything compiled, not just .o files.
17
#
18
#	This Makefile is set to use the gcc compiler. If you want cc instead
19
#	you will need to alter the "CC=gcc" below, or use CC=cc on the command 
20
#	line.
21
#
22
#	If you want strict BSD ioctls used (which seems to be the case
23
#	for Suns) add -D__STRICT_BSD___ to the SACFLAGS and CLCFLAGS macros.
24
#
25
#	You need to select one of three dictionary and index file
26
#	access options by uncommenting the appropriate SACFLAGS and
27
#	SVCFLAGS macros:
28
#
29
#		memory-mapped I/O	use -DMMAP
30
#		demand-paging		use -DDEMAND_PAGING
31
#		load into RAM		use -DRAM_LOAD
32
#
33
#	Memory-mapped I/O does not appear to function correctly
34
#	under Ultrix. It works with Solaris and Linux.
35
#
36
#	Note that it is assumed that window-size ioctls are available.
37
#	If you want to use curses instead, delete the -DWINSIZE....,
38
#	and compile the client and/or the stand-alone programs with
39
#	the "-lcurses -ltermcap".
40
#
41
#	Expect some compilation warnings about "qsort". No-one seems
42
#	able to remove them.
43
#
44
#	For DEC Ultrix, you will probably need to alter the comments
45
#	on the SACFLAGS and SVCFLAGS macros to remove MMAP, as this
46
#	appears not to work on that OS.
47
#
48
#	For Sun and Linux, you may need to include the "unistd.h" file.
49
#
50
#	For Solaris 2.x, you will probably need to uncomment the following
51
#	if you want to use the server/client operation. Also be prepared
52
#	for some complaining about pointer mismatches in routines like
53
#	`recv'. They can be ignored.
54
#
55
#LIBS= -lsocket -lnsl
56
#
57
CC=gcc -g
58
all: xjdic_sa xjdic_cl xjdserver xjdxgen exjdxgen
59
client: xjdic_cl
60
server: xjdserver
61
stand: xjdic_sa
62
#
63
#	X J D X G E N
64
#
65
JDXOBJECTS = xjdxgen.o
66
xjdxgen: $(JDXOBJECTS)
67
	$(CC) $(JDXOBJECTS) -o xjdxgen
68
xjdxgen.o:	xjdxgen.c
69
	$(CC) -c xjdxgen.c
70
#
71
#	E X J D X G E N
72
#
73
EJDXOBJECTS = exjdxgen.o
74
exjdxgen: $(EJDXOBJECTS)
75
	$(CC) $(EJDXOBJECTS) -o exjdxgen
76
exjdxgen.o:	exjdxgen.c
77
	$(CC) -c exjdxgen.c
78
#
79
#	S T A N D _ A L O N E 
80
#
81
SAOBJECTS = xjdsa.o xjdcomm_sa.o xjdservcomm_sa.o xjdfrontend_sa.o
82
#SACFLAGS = -DXJDFRONTEND -DXJDDIC -DDEMAND_PAGING
83
SACFLAGS = -DXJDFRONTEND -DXJDDIC -DMMAP
84
#SACFLAGS = -DXJDFRONTEND -DXJDDIC -DRAM_LOAD
85
xjdic_sa: $(SAOBJECTS)
86
	$(CC) $(SAOBJECTS) -o xjdic_sa
87
xjdsa.o: xjdsa.c
88
	$(CC) -c $(SACFLAGS) xjdsa.c
89
xjdcomm_sa.o: xjdcomm.c
90
	$(CC) -c $(SACFLAGS) xjdcomm.c -o xjdcomm_sa.o
91
xjdservcomm_sa.o: xjdservcomm.c
92
	$(CC) -c $(SACFLAGS) xjdservcomm.c -o xjdservcomm_sa.o
93
xjdfrontend_sa.o: xjdfrontend.c
94
	$(CC) -c $(SACFLAGS) xjdfrontend.c -o xjdfrontend_sa.o
95
$(SAOBJECTS): xjdic.h
96
#
97
#	C L I E N T
98
#
99
CLOBJECTS = xjdclient.o xjdcomm_cl.o xjdfrontend_cl.o
100
CLCFLAGS = -DXJDFRONTEND -DXJDCLSERV
101
xjdic_cl: $(CLOBJECTS)
102
	$(CC) $(CLOBJECTS) $(LIBS) -o xjdic_cl
103
xjdclient.o: xjdclient.c
104
	$(CC) -c $(CLCFLAGS) xjdclient.c
105
xjdcomm_cl.o: xjdcomm.c
106
	$(CC) -c $(CLCFLAGS) xjdcomm.c -o xjdcomm_cl.o
107
xjdfrontend_cl.o: xjdfrontend.c
108
	$(CC) -c $(CLCFLAGS) xjdfrontend.c -o xjdfrontend_cl.o
109
$(CLOBJECTS): xjdic.h
110
#
111
#	S E R V E R
112
#
113
SVOBJECTS = xjdcomm_sv.o xjdservcomm_sv.o xjdserver.o
114
#SVCFLAGS = -DXJDDIC -DXJDCLSERV -DDEMAND_PAGING
115
SVCFLAGS = -DXJDDIC -DXJDCLSERV -DMMAP
116
#SVCFLAGS = -DXJDDIC -DXJDCLSERV -DRAM_LOAD
117
xjdserver: $(SVOBJECTS)
118
	$(CC) $(SVOBJECTS) $(LIBS) -o xjdserver
119
xjdcomm_sv.o: xjdcomm.c
120
	$(CC) -c $(SVCFLAGS) xjdcomm.c -o xjdcomm_sv.o
121
xjdservcomm_sv.o: xjdservcomm.c
122
	$(CC) -c $(SVCFLAGS) xjdservcomm.c -o xjdservcomm_sv.o
123
xjdserver.o: xjdserver.c
124
	$(CC) -c $(SVCFLAGS) xjdserver.c 
125
$(SVOBJECTS): xjdic.h
126
#
127
clean:
128
	rm -f *.o
129
realclean: clean
130
	rm -f xjdic_sa xjdic_cl xjdserver xjdxgen exjdxgen
131
#
132
#    This program is free software; you can redistribute it and/or modify
133
#    it under the terms of the GNU General Public License as published by
134
#    the Free Software Foundation; either version 1, or (at your option)
135
#    any later version.
136
#
137
#    This program is distributed in the hope that it will be useful,
138
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
139
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
140
#    GNU General Public License for more details.
141
#
142
#    You should have received a copy of the GNU General Public License
143
#    along with this program; if not, write to the Free Software
144
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
(-)xjdic.orig/xjdclient.c (-11 / +11 lines)
Lines 36-48 Link Here
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <unistd.h>
37
#include <unistd.h>
38
#include <signal.h>
38
#include <signal.h>
39
#include <errno.h>
39
#include "xjdic.h"
40
#include "xjdic.h"
40
41
41
#define CVERBOSE 0
42
#define CVERBOSE 0
42
int chk_cnt=0;
43
int chk_cnt=0;
43
extern int errno;
44
unsigned char host[51] = {"localhost"};
44
unsigned char host[51] = {"localhost"};
45
unsigned char yn[2];
45
unsigned char yesno[2];
46
unsigned int portno = XJ_PORTNO;
46
unsigned int portno = XJ_PORTNO;
47
char protocol[4] = {"udp"};
47
char protocol[4] = {"udp"};
48
int s, elapse;
48
int s, elapse;
Lines 154-160 Link Here
154
{
154
{
155
	long temp;
155
	long temp;
156
	int i;
156
	int i;
157
	char yn[5];
157
	char yesno[5];
158
158
159
	if (CVERBOSE) printf("PDU: %d %d %d %d %d %d %s %d\n",
159
	if (CVERBOSE) printf("PDU: %d %d %d %d %d %d %s %d\n",
160
	       ntohs(pdu_in.xjdrsp_type),
160
	       ntohs(pdu_in.xjdrsp_type),
Lines 178-190 Link Here
178
		chk_cnt = 0;
178
		chk_cnt = 0;
179
		return (TRUE);
179
		return (TRUE);
180
	}
180
	}
181
	if (CVERBOSE) printf("Cal checksum: %d PDU checksum: %d\n",temp,ntohl(pdu_in.xjdrsp_checksum));
181
	if (CVERBOSE) printf("Cal checksum: %ld PDU checksum: %d\n",temp,ntohl(pdu_in.xjdrsp_checksum));
182
	if (chk_cnt++ > 20)
182
	if (chk_cnt++ > 20)
183
	{
183
	{
184
		printf("20 consecutive checksum failures: Try again? ");
184
		printf("20 consecutive checksum failures: Try again? ");
185
		scanf("%s",&yn);
185
		scanf("%s",&yesno[0]);
186
		printf("\n");
186
		printf("\n");
187
		if((yn[0] | 0x20) == 'y') 
187
		if((yesno[0] | 0x20) == 'y') 
188
		{
188
		{
189
			chk_cnt = 0;
189
			chk_cnt = 0;
190
			return(FALSE);
190
			return(FALSE);
Lines 260-268 Link Here
260
				{
260
				{
261
					rep_count = 0;
261
					rep_count = 0;
262
					printf("Cannot contact Server: %s Try again? ",host);
262
					printf("Cannot contact Server: %s Try again? ",host);
263
					scanf("%s",&yn);
263
					scanf("%s",&yesno[0]);
264
					printf("\n");
264
					printf("\n");
265
					if((yn[0] | 0x20) == 'y') 
265
					if((yesno[0] | 0x20) == 'y') 
266
					{
266
					{
267
						curr_timer = TINITVAL;
267
						curr_timer = TINITVAL;
268
						continue; 
268
						continue; 
Lines 298-305 Link Here
298
			if (errno == 111)
298
			if (errno == 111)
299
			{
299
			{
300
				printf("Cannot contact Server: %s Try again?\n",host);
300
				printf("Cannot contact Server: %s Try again?\n",host);
301
				scanf("%s",&yn);
301
				scanf("%s",&yesno[0]);
302
				if((yn[0] | 0x20) == 'y') 
302
				if((yesno[0] | 0x20) == 'y') 
303
				{
303
				{
304
					continue; 
304
					continue; 
305
				}
305
				}
Lines 322-328 Link Here
322
			sendit = FALSE;
322
			sendit = FALSE;
323
			continue;
323
			continue;
324
		}
324
		}
325
		if (CVERBOSE) printf("Valid PDU: %d %ld %d %d\n",ntohs(pdu_in.xjdrsp_type),
325
		if (CVERBOSE) printf("Valid PDU: %d %d %d %d\n",ntohs(pdu_in.xjdrsp_type),
326
							ntohl(pdu_in.xjdrsp_resindex),
326
							ntohl(pdu_in.xjdrsp_resindex),
327
							ntohs(pdu_in.xjdrsp_hitposn),
327
							ntohs(pdu_in.xjdrsp_hitposn),
328
							ntohs(pdu_in.xjdrsp_reslen));
328
							ntohs(pdu_in.xjdrsp_reslen));
(-)xjdic.orig/xjdclient.c~ (+344 lines)
Line 0 Link Here
1
/**************************************************************************
2
*                 X  J  D  C  L  I  E  N  T                               *     *
3
*              Code  for Interfacing the Client with the server           *
4
*         Japanese-English Dictionary program (X11 version)               *
5
*                                                   Author: Jim Breen     *
6
***************************************************************************/
7
/*  This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 1, or (at your option)
10
    any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
20
21
/* for Solaris 2.5, which doesn't have INADDR_NONE in its <netinet/in.h> */
22
#ifdef SOLARIS
23
#define INADDR_NONE -1
24
#endif
25
26
27
#include <stdio.h>
28
#include <ctype.h>
29
#include <string.h>
30
#include <sys/types.h>
31
#include <sys/socket.h>
32
#include <netinet/in.h>
33
#include <netdb.h>
34
#include <sys/stat.h>
35
#include <sys/time.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <signal.h>
39
#include <errno.h>
40
#include "xjdic.h"
41
42
#define CVERBOSE 0
43
int chk_cnt=0;
44
unsigned char host[51] = {"localhost"};
45
unsigned char yn[2];
46
unsigned int portno = XJ_PORTNO;
47
char protocol[4] = {"udp"};
48
int s, elapse;
49
struct timeval timeout;
50
int txno = 0;
51
52
REQ_PDU pdu_out;
53
RSP_PDU pdu_in;
54
int pduseq = 0;
55
int curr_timer = TINITVAL;
56
int rep_count;
57
struct timezone tz;
58
long timeofday_st,timeofday_en;
59
fd_set fildesc;
60
int nfds;
61
unsigned char *sptr;
62
int NoDics,CurrDic;
63
char Dnamet[10][100];
64
65
void xjdserver (int type, int dic_no, long index_posn, int sch_str_len,
66
                unsigned char *sch_str, int *sch_resp, long *res_index, 
67
                int *hit_posn, int *res_len, unsigned char *res_str,
68
                long *dic_loc );
69
void DicSet();
70
int connectsock(int portno, char *host);
71
void reqchecksum();
72
int respchecksum();
73
74
void DicSet()
75
{
76
/*	In the Client version, DicSet() establishes the connection with 
77
	the server. */
78
79
	int i,schresp, dumint, trying,hullodic;
80
	long dumlong,dumlong0 = 0;
81
	char dummy[2],dnamelist[512];
82
83
	connectsock(portno, host);
84
	nfds = getdtablesize();
85
	FD_SET(s, &fildesc);
86
	trying = TRUE;
87
	while (trying)
88
	{
89
		xjdserver(XJ_HULLO, 0, dumlong0, 0, dummy, &schresp, &dumlong, 
90
			&hullodic, &dumint, dnamelist, &dumlong);
91
		if (schresp == XJ_OK) 
92
		{
93
			printf("Server: %s detected\n", host);
94
			NoDics = hullodic;
95
			sptr = strtok(dnamelist,"#");
96
			while (sptr != NULL)
97
			{
98
				i = sptr[0] - '0';
99
				if (i <= NoDics) 
100
				{
101
					strcpy(Dnamet[i],sptr+1);
102
					printf ("Dictionary: %d [%s]\n",i,Dnamet[i]);
103
				}
104
				sptr = strtok(NULL,"#");
105
			}
106
			CurrDic = 1;
107
			return;
108
		}
109
		printf("Server not responding.(%d) Try again? (y/n)",schresp);
110
		scanf("%c",dummy);
111
		printf("\n");
112
		if ((dummy[0] | 0x20) == 'n') exit(1);
113
	}
114
}
115
116
int connectsock(int portno, char *host)
117
{
118
119
	struct protoent *ppe;
120
	struct hostent *phe;
121
	struct sockaddr_in sin;
122
123
	bzero((char *)&sin, sizeof(sin));
124
	sin.sin_family = AF_INET;
125
	sin.sin_port = htons(portno);
126
	if (phe = gethostbyname(host))
127
		bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length);
128
	else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
129
	{
130
		printf ("Cannot Get Host Entry!!\n");
131
		exit(1);
132
	}
133
	if ((ppe = getprotobyname("udp")) == 0)
134
	{
135
		printf ("Cannot set up UDP!!\n");
136
		exit(1);
137
	}
138
	s = socket(PF_INET, SOCK_DGRAM, ppe->p_proto);
139
	if (s < 0)
140
	{
141
		printf ("Cannot create socket!!: %s\n",strerror(errno));
142
		exit(1);
143
	}
144
	if (connect (s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
145
	{
146
		printf ("Cannot connect to host: %s, port: %d\n",host,portno);
147
		exit(1);
148
	}
149
150
	return (s);
151
}
152
153
int respchecksum()
154
{
155
	long temp;
156
	int i;
157
	char yn[5];
158
159
	if (CVERBOSE) printf("PDU: %d %d %d %d %d %d %s %d\n",
160
	       ntohs(pdu_in.xjdrsp_type),
161
	       ntohs(pdu_in.xjdrsp_seq),
162
	       ntohl(pdu_in.xjdrsp_resindex),
163
	       ntohl(pdu_in.xjdrsp_dicloc),
164
	       ntohs(pdu_in.xjdrsp_hitposn),
165
	       ntohs(pdu_in.xjdrsp_reslen),
166
	       pdu_in.xjdrsp_resstr,
167
	       ntohl(pdu_in.xjdrsp_checksum));
168
	temp = ntohs(pdu_in.xjdrsp_type);
169
	temp+= ntohs(pdu_in.xjdrsp_seq);
170
	temp+= ntohl(pdu_in.xjdrsp_resindex);
171
	temp+= ntohl(pdu_in.xjdrsp_dicloc);
172
	temp+= ntohs(pdu_in.xjdrsp_hitposn);
173
	temp+= ntohs(pdu_in.xjdrsp_reslen);
174
	for (i = 0; i < strlen(pdu_in.xjdrsp_resstr); i++)
175
		temp+= pdu_in.xjdrsp_resstr[i];
176
	if (ntohl(pdu_in.xjdrsp_checksum) == temp) 
177
	{
178
		chk_cnt = 0;
179
		return (TRUE);
180
	}
181
	if (CVERBOSE) printf("Cal checksum: %d PDU checksum: %d\n",temp,ntohl(pdu_in.xjdrsp_checksum));
182
	if (chk_cnt++ > 20)
183
	{
184
		printf("20 consecutive checksum failures: Try again? ");
185
		scanf("%s",&yn);
186
		printf("\n");
187
		if((yn[0] | 0x20) == 'y') 
188
		{
189
			chk_cnt = 0;
190
			return(FALSE);
191
		}
192
		else 
193
		{
194
			exit(1);
195
		}
196
	}
197
	return (FALSE);
198
}
199
200
void reqchecksum()
201
{
202
	long temp;
203
	int i;
204
205
	temp = ntohs(pdu_out.xjdreq_type);
206
	temp+= ntohs(pdu_out.xjdreq_seq);
207
	temp+= ntohs(pdu_out.xjdreq_dicno);
208
	temp+= ntohl(pdu_out.xjdreq_indexpos);
209
	temp+= ntohs(pdu_out.xjdreq_schlen);
210
	for (i = 0; i < strlen(pdu_out.xjdreq_schstr); i++) temp+= pdu_out.xjdreq_schstr[i];
211
212
	pdu_out.xjdreq_checksum = htonl(temp);
213
}
214
215
void xjdserver (int type, int dic_no, long index_posn, int sch_str_len,
216
                unsigned char *sch_str, int *sch_resp, long *res_index, 
217
                int *hit_posn, int *res_len, unsigned char *res_str,
218
                long *dic_loc )
219
{
220
	int i, ares, sendit;
221
222
	gettimeofday((struct timeval *)&timeout,(struct timezone *)&tz);
223
	timeofday_st = timeout.tv_sec;
224
	pdu_out.xjdreq_type = htons(type);
225
	pdu_out.xjdreq_seq = htons(++pduseq);
226
	pdu_out.xjdreq_dicno = htons(dic_no);
227
	pdu_out.xjdreq_indexpos = htonl(index_posn);
228
	pdu_out.xjdreq_schlen = htons(sch_str_len);
229
	strcpy(pdu_out.xjdreq_schstr,sch_str);
230
	reqchecksum();
231
	sendit = TRUE;
232
	while (TRUE)
233
	{
234
		timeout.tv_sec = curr_timer;
235
		timeout.tv_usec = 0;
236
		if (sendit) 
237
		{
238
			txno++;
239
			(void) write (s, &pdu_out,sizeof(REQ_PDU));
240
			if (CVERBOSE) printf("txno %d (seq: %d)\n",txno,pduseq);
241
		}
242
		if ((ares = select(nfds, &fildesc, (fd_set *)0, (fd_set *)0, (struct timeval *)&timeout) )< 0)
243
		{
244
			printf("Select error: %s\n", strerror(errno));
245
			exit(1);
246
		}
247
		sendit = TRUE; 
248
		if (ares == 0)
249
		{
250
			if (CVERBOSE) printf("Timeout: %d secs (seq: %d)\n",curr_timer,pduseq);
251
			FD_SET(s, &fildesc);
252
			if (rep_count < TMAXREP) 
253
			{
254
				rep_count++;		
255
				continue;
256
			}
257
			else
258
			{
259
				if (curr_timer >= TMAXVAL)
260
				{
261
					rep_count = 0;
262
					printf("Cannot contact Server: %s Try again? ",host);
263
					scanf("%s",&yn);
264
					printf("\n");
265
					if((yn[0] | 0x20) == 'y') 
266
					{
267
						curr_timer = TINITVAL;
268
						continue; 
269
					}
270
					else 
271
					{
272
						exit(1);
273
					}
274
				}
275
				else
276
				{
277
					curr_timer*=2;
278
					rep_count = 0;
279
					continue;
280
				}
281
			}
282
		}
283
		if (CVERBOSE) printf("Something in!\n");
284
		gettimeofday((struct timeval *)&timeout,(struct timezone *)&tz);
285
		timeofday_en = timeout.tv_sec;
286
		elapse = timeofday_en - timeofday_st;
287
		if (elapse < 1) elapse = 1;
288
		if (elapse > curr_timer) 
289
		{
290
			curr_timer = elapse << 1;
291
		}
292
		else
293
		{
294
			if (curr_timer - elapse > 2) curr_timer = elapse+2;
295
		}
296
		if (recv(s, &pdu_in, sizeof(RSP_PDU), 0) < 0)
297
		{
298
			if (errno == 111)
299
			{
300
				printf("Cannot contact Server: %s Try again?\n",host);
301
				scanf("%s",&yn);
302
				if((yn[0] | 0x20) == 'y') 
303
				{
304
					continue; 
305
				}
306
				else 
307
				{
308
					exit(1);
309
				}
310
			}
311
			printf("UDP recv error: %d %s\n",errno,strerror(errno));
312
			exit(1);
313
		}
314
		if (!respchecksum()) 
315
		{
316
			if (CVERBOSE) printf("PDU checksum error (seq: %d)\n",pduseq);
317
			continue;
318
		}
319
		if (ntohs(pdu_in.xjdrsp_seq) != pduseq) 
320
		{
321
			if (CVERBOSE) printf("PDU sequence error. Rec: %d Exp: %d\n",ntohs(pdu_in.xjdrsp_seq),pduseq);
322
			sendit = FALSE;
323
			continue;
324
		}
325
		if (CVERBOSE) printf("Valid PDU: %d %ld %d %d\n",ntohs(pdu_in.xjdrsp_type),
326
							ntohl(pdu_in.xjdrsp_resindex),
327
							ntohs(pdu_in.xjdrsp_hitposn),
328
							ntohs(pdu_in.xjdrsp_reslen));
329
		*sch_resp = ntohs(pdu_in.xjdrsp_type);
330
		*res_index = ntohl(pdu_in.xjdrsp_resindex);
331
		*hit_posn = ntohs(pdu_in.xjdrsp_hitposn);
332
		*res_len = ntohs(pdu_in.xjdrsp_reslen);
333
		*dic_loc = ntohl(pdu_in.xjdrsp_dicloc);
334
		for (i = 0; i < *res_len; i++) 
335
		{
336
		
337
			res_str[i] = pdu_in.xjdrsp_resstr[i];
338
			res_str[i+1] = 0;
339
		}
340
		return;
341
	}
342
343
344
}
(-)xjdic.orig/xjdfrontend.c (-2 / +3 lines)
Lines 26-31 Link Here
26
#include <stdlib.h>
26
#include <stdlib.h>
27
#include <signal.h>
27
#include <signal.h>
28
#include <errno.h>
28
#include <errno.h>
29
#include <unistd.h>
29
#include "xjdic.h"
30
#include "xjdic.h"
30
31
31
/*    Paul Burchard supplied a patch to provide BSD compatibility for xjdic
32
/*    Paul Burchard supplied a patch to provide BSD compatibility for xjdic
Lines 894-900 Link Here
894
		fread(ejdxtest,sizeof(long),1,fex);
895
		fread(ejdxtest,sizeof(long),1,fex);
895
		if (ejdxtest[0] != (extlen+jiver))
896
		if (ejdxtest[0] != (extlen+jiver))
896
		{
897
		{
897
			printf("\nEDICT Extension file & Index Mismatch! %ld %ld\n",ejdxtest[0],extlen+jiver);
898
			printf("\nEDICT Extension file & Index Mismatch! %d %d\n",ejdxtest[0],extlen+jiver);
898
			exit(1);
899
			exit(1);
899
		}
900
		}
900
	}
901
	}
Lines 2115-2121 Link Here
2115
			}
2116
			}
2116
		}
2117
		}
2117
		NoSets++;
2118
		NoSets++;
2118
		printf(" (%d) ",strlen(RKSet[NoSets-1])/2);
2119
		printf(" (%ld) ",strlen(RKSet[NoSets-1])/2);
2119
	}
2120
	}
2120
	printf("\n");
2121
	printf("\n");
2121
}
2122
}
(-)xjdic.orig/xjdfrontend.c~ (+3220 lines)
Line 0 Link Here
1
/**************************************************************************
2
*                     X  J  D  F R O N T E N D                            *
3
*                Common Frontend Routines for client & stand-alone vers.  *
4
*         Japanese-English Dictionary program (X11 version)               *
5
*                                                   Author: Jim Breen     *
6
***************************************************************************/
7
/*  This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 1, or (at your option)
10
    any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
20
21
#include <stdio.h>
22
#include <ctype.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <sys/stat.h>
26
#include <stdlib.h>
27
#include <signal.h>
28
#include <errno.h>
29
#include "xjdic.h"
30
31
/*    Paul Burchard supplied a patch to provide BSD compatibility for xjdic
32
(it uses the appropriate BSD ioctls in place of the SVR4 stuff).  In
33
order to enable this, people should compile with the option
34
-D__STRICT_BSD___ .         */
35
36
#ifdef __STRICT_BSD__
37
#include <sgtty.h>
38
#else
39
#ifdef __POSIX__
40
#include <sys/termios.h>
41
#else
42
#include <termio.h>
43
#endif
44
#endif
45
46
#define WINSIZE_IOCTL_AVAILABLE
47
48
49
50
static enum
51
{
52
IOCTL_ORIG,
53
IOCTL_RAW
54
} myi_status = IOCTL_ORIG;
55
56
#ifdef __STRICT_BSD__
57
static struct sgttyb    orig,new;
58
#else
59
static struct termio    orig,new;
60
#endif
61
62
extern unsigned char CBname[100];
63
extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
64
extern unsigned char *dicbufft[10];
65
extern unsigned long diclent[10], indkent[10],indptrt[10];
66
extern int NoDics,CurrDic;
67
int thisdic = 0;
68
int gdicnos[10],gdicmax=0,GDmode=FALSE,gdiclen;
69
int GDALLmode=FALSE;
70
71
FILE *fe, *fex, *fclip, *fopen();
72
73
#ifdef XJDCLSERV
74
extern int portno;
75
extern unsigned char host[];
76
#endif
77
78
#ifdef XJDDIC
79
extern unsigned long dichits[10],dicmiss[10];
80
extern unsigned long indhits[10],indmiss[10];
81
extern unsigned long vbkills;
82
#endif
83
84
char DicDir[100];
85
int xfilelen;
86
87
pid_t pid;
88
int sig=SIGTSTP;
89
int ShiftJIS = FALSE,NoSJIS=FALSE;
90
unsigned char instr[256],radkanj[250][2];
91
int radnos[250];
92
unsigned char kanatab[NRKANA*2][7];
93
int Omode = 0,Smode = 0,Dmode = 0,AKanaMode;
94
int DRow,DCol,MaxY=MAXLINES,MaxX=MAXCOLS-1,KFlushRes,nok;
95
unsigned long hittab[NOHITS];
96
int verblen,DispHit,ksp,hitind,FirstKanj = 0,prieng = FALSE,Extopen=FALSE,NoSkip;
97
int extlen,extjdxlen;
98
unsigned char kmodes[2][10] = {"ON","OFF"};
99
unsigned char kmodes_r[2][10] = {"OFF","ON"};
100
unsigned long chline,chpos,it;
101
unsigned char strfilt[10],tempout[80];
102
unsigned char KSname[50] = {"kanjstroke"};
103
unsigned char RKname[50] = {"radkfile"};
104
unsigned char Rname[50] = {"radicals.tm"};
105
unsigned char ROMname[60] = {"romkana.cnv"};
106
unsigned char EXTJDXname[80] = {"edictext.xjdx"};
107
unsigned char EXTname[80] = {"edictext"};
108
unsigned char Vname[60] = {"vconj"};
109
unsigned char ENVname[100];
110
unsigned char cl_rcfile[100];
111
unsigned char Clip_File[100] = {"clipboard"};
112
unsigned char GPL_File[100] = {"gnu_licence"};
113
unsigned char KDNSlist[50];
114
int jiver = 14;		/*The last time the index structure changed was Version1.4*/
115
unsigned char sver[] = {SVER};
116
unsigned char fbuff[512],KLine[KFBUFFSIZE],karray[KANJARRAYSIZE][5];
117
unsigned char LogLine[200];
118
unsigned char ksch[50],ktarg[50];
119
/* The following Help table has "~" to force spaces   */
120
unsigned char Help[40][81] = {
121
"\n~~~~~~~~~~~~~~~~~~XJDIC USAGE SUMMARY ",
122
"At the SEARCH KEY prompt respond with a string of ascii, kana and/or ",
123
"kanji to look up in the current dictionary (prefix with @ or # to invoke ",
124
"conversion of romaji to hiragana or katakana)",
125
"~~~~~~~~~~~~~~~~~~SINGLE CHARACTER COMMANDS",
126
"~~\\ enter Kanji Dictionary mode~~~? ~~get this Help display",
127
"~~_ select dictionary files ~~~~~~=/^ cycle up/down dictionary files",
128
"~~\' set/clear one-off filter~~~~~~; activate/deactivate general filters",
129
"~~/ toggle kanji_within_compound~~- toggle long kanji display on/off",
130
"~~$ set global dictionaries ~~~~~~% toggle global search mode on/off",
131
"~~] display Dictionary Extension ~: toggle verb deinflection on/off",
132
"~~+ toggle priority English keys~~| toggle unedited display mode on/off",
133
"~~[ toggle exact_match on/off~~~~~& toggle kana input mode on/off",
134
"~~{ switch to clipboard input ~~~~} toggle reverse video of matches",
135
"~~` toggle multiple global disp.~~Ctrl-D  to exit",
136
"~~! display gnu licence~~~~~~~~~~~Ctrl-Z to suspend",
137
"~~~~~~Kanji Dictionary mode - prompt is KANJI LOOKUP TYPE:.  Responses:",
138
"~~a single kanji or a kana reading (default)",
139
"~~j followed by the 4-digit hexadecimal JIS code for a kanji",
140
"~~j followed by k and the 4-digit KUTEN code for a kanji",
141
"~~~~~~~(precede code with `h' for JIS X 0212 kanji.)",
142
"~~j followed by s and the 4-digit hexadecimal Shift-JIS code for a kanji",
143
"~~m followed by an (English) kanji meaning",
144
"~~c followed by an index code such as Nnnn (Nelson), Bnn (Bushu), etc",
145
"~~r initiates a display of all radicals and their numbers",
146
"~~l switches the program into the Radical Lookup mode",
147
"$$$"
148
};
149
unsigned char RVon[] = {0x1b,'[','7','m',0};
150
unsigned char RVoff[] = {0x1b,'[','m',0};
151
int nofilts=FALSE,filton[NOFILT],filtact[NOFILT],filttype[NOFILT],filtcoden[NOFILT];
152
unsigned char filtnames[NOFILT][50],filtcodes[NOFILT][10][10];
153
unsigned char testline[1025],SingleFilter[50];
154
unsigned char vdicf[VMAX][7],vinfl[VMAX][21],vcomms[41][50];
155
int strf,Jverb = TRUE,SFFlag=FALSE,vcommno[VMAX];
156
int ROmode = 1,EMmode = 1,KLmode = 1,KImode = 1,KLRmode,KLcount;
157
unsigned char vline[250],vstr[13];
158
unsigned char RadK1[300],RadK2[300],*RKanj1,*RKanj2,*RKSet[10];
159
unsigned char RKTarg[21];
160
int rmax,NoSets=0,NoRads,RKStart[300],RKCnt[300];
161
unsigned char ops[3],kstrokes[7000];
162
int kstrokelim,kstroketype;
163
int clipmode=FALSE;
164
unsigned char clipstring1[51];
165
unsigned char clipstring2[51]={"XXXX"};
166
int RVACTIVE = TRUE;
167
168
int DicNum;
169
long DicLoc;
170
171
/*====== Prototypes========================================================*/
172
173
FILE  *xfopen(char *file_name, char *file_mode, int *xfilelen);
174
int getcharxx();
175
void RadDisp();
176
void RadBuild();
177
int FindRad(unsigned char char1, unsigned char char2);
178
void DoLOOKUP();
179
void RadLoad();
180
void KSLoad();
181
void xjdserver (int type, int dic_no, long index_posn, int sch_str_len, 
182
		unsigned char *sch_str, int *sch_resp, long *res_index, 
183
		int *hit_posn, int *res_len, unsigned char *res_str,
184
		long *dic_loc );
185
void OneShot();
186
void RadSet();
187
void Verbtoggle();
188
void FiltSet();
189
void xjdicrc();
190
void DoRADICALS();
191
int Vlookup();
192
void AppKanji(unsigned char c1,unsigned char c2);
193
void Verbinit();
194
void KOut(unsigned char *sout);
195
void LoadKana();
196
void DoRomaji(unsigned char kc);
197
int kanaconv(int rpos, int rlen);
198
void togglemode();
199
void engpritoggle();
200
void altdic(int dicincr);
201
void DoKANJI();
202
void DoJIS();
203
int addhit(long spot);
204
void GetEUC(unsigned char *eucline);
205
void cbreakon();
206
void cbreakoff();
207
void ioctlorig();
208
void ioctlraw();
209
void Lookup();
210
void Lookup2();
211
void DicSet();
212
void sjis2jis(int *p1,int *p2);/* Ken Lunde's routine  */
213
void jis2sjis(unsigned char *p1,unsigned char *p2);/* Ken Lunde's routine  */
214
int stringcomp(unsigned char *s1, unsigned char *s2);
215
void FixSJIS(unsigned char *jline);
216
void GetKBStr(unsigned char *prompt);
217
void ExtFileDisp();
218
void SeekErr(int iores);
219
void KOutc(int c);
220
void KLookup();
221
int KFlush(unsigned char *msg);
222
int KEOS (unsigned char *msg);
223
void NewWinSize();
224
int kcmp (unsigned char *t1, unsigned char *t2);
225
unsigned char *DicName(int dn);
226
/*====== end of prototypes==================================================*/
227
228
229
/* ==== ioctl routines supplied by Cameron Blackwood=======================*/
230
/* ==== BSD compatibility hacked in by Paul Burchard ==============*/
231
/*	These routines are to disable/re-enable "cbreak", when I want to
232
	switch in & out of canonical input processing. Mostly xjdic does
233
	not use canonical input, as I want to react immediately to most	
234
	key-presses		*/
235
236
void    ioctlraw()
237
{
238
  if (myi_status == IOCTL_ORIG)
239
  {
240
#ifdef __STRICT_BSD__
241
    ioctl(0, TIOCGETP, &orig); ioctl(0, TIOCGETP, &new);
242
    new.sg_flags |= CBREAK; new.sg_flags &= ~ECHO;
243
    ioctl(0, TIOCSETP, &new);
244
#else
245
    ioctl(0, TCGETA, &orig); ioctl(0, TCGETA, &new);
246
    new.c_lflag &= ~ICANON; new.c_lflag &= ~ISIG; new.c_lflag &= ~ECHO;
247
    new.c_lflag &= ~IXON;
248
    new.c_cc[4] = 1; new.c_cc[5] = 0;   ioctl(0, TCSETA, &new);
249
#endif
250
    myi_status=IOCTL_RAW;
251
  }
252
  else return;
253
}
254
255
void    ioctlorig()
256
{
257
#ifdef __STRICT_BSD__
258
   ioctl(0, TIOCSETP, &orig);
259
#else
260
   ioctl(0, TCSETA, &orig);
261
#endif
262
    myi_status = IOCTL_ORIG;
263
}
264
265
266
void cbreakoff()
267
{
268
	ioctlorig();
269
	/*system("stty -cbreak echo");*/
270
}
271
272
void cbreakon()
273
{
274
	ioctlraw();
275
	/*system("stty cbreak -echo");*/
276
}
277
278
int getcharxx()
279
{
280
	if (clipmode)
281
	{
282
		return('n');
283
	}
284
	else
285
	{
286
		return(getchar());
287
	}
288
}
289
/*====GetWinSize==== Hank Cohen's routine for getting the (new) window size==*/
290
/*
291
 * (The following was supplied by Hank Cohen)
292
 * We would like to use the Berkeley winsize structure to determine
293
 * the number of lines and columns in the window.  If that's not
294
 * available we can use termcap.
295
 * If using termcap, you will need to compile with "-lcurses -ltermcap".
296
 */
297
char termcap_buf[1024];
298
void GetWinSize()
299
{
300
	char *term_name;
301
302
#ifdef WINSIZE_IOCTL_AVAILABLE
303
	struct winsize window;
304
305
	signal(SIGWINCH, NewWinSize);
306
	ioctl(0,TIOCGWINSZ,&window);
307
	MaxX = window.ws_col;
308
	MaxX--;
309
	MaxY = window.ws_row;
310
#else /* Use termcap */
311
	if ((term_name = getenv("TERM")) == 0){
312
		fprintf(stderr,"No TERM in environment!\n");
313
	} else if (tgetent(termcap_buf, term_name) != 1) {
314
		fprintf(stderr,"No termcap entry for %s!\n", term_name);
315
	} else {
316
		MaxX = tgetnum("co");
317
		MaxY = tgetnum("li");
318
	}
319
#endif  /* termcap */
320
}
321
void NewWinSize()
322
{
323
	struct winsize window;
324
	ioctl(0,TIOCGWINSZ,&window);
325
	MaxX = window.ws_col;
326
	MaxX--;
327
	MaxY = window.ws_row;
328
}
329
330
/*=====KSLoad===Load file of kanji/stroke data==========================*/
331
void KSLoad()
332
{
333
	int i,j,k,lno;
334
	FILE *fk,*fopen();
335
336
	fk = xfopen(KSname,"r",&xfilelen);
337
	lno=0;
338
	while(!feof(fk))
339
	{
340
		fgets(testline,9,fk);
341
		if(feof(fk)) break;
342
		testline[3] = 0;
343
		lno++;
344
		i = ((testline[0] & 0x7f)-0x30)*94 + ((testline[1] & 0x7f) - 0x21);
345
		if ((i < 0) || (i > 6999))
346
		{
347
			printf("Bad kanji in %s at line %d (%s)\n",KSname,lno,testline);
348
			i = 6999;
349
		}
350
		kstrokes[i] = testline[2]-'0';
351
	}
352
	fclose(fk);
353
}
354
355
/*=====RadLoad===Load file of Radical Data==============================*/
356
void RadLoad()
357
{
358
	int i,j,k;
359
	FILE *fk,*fopen();
360
361
	fk = xfopen(RKname,"r", &xfilelen);
362
	k = xfilelen/2;
363
	RKanj1 = malloc(k*sizeof(char));
364
	if (RKanj1==NULL)
365
	{
366
		printf("malloc failed for radical tables!\n");
367
		exit(1);
368
	}
369
	RKanj2 = malloc(k*sizeof(char));
370
	if (RKanj2==NULL)
371
	{
372
		printf("malloc failed for radical tables!\n");
373
		exit(1);
374
	}
375
	i = -1;
376
	j = 0;
377
	rmax = 0;
378
	while(!feof(fk))
379
	{
380
		fgets(testline,199,fk);
381
		if(feof(fk)) break;
382
		if (testline[0] == '$')
383
		{
384
			i++;
385
			RadK1[i] = testline[2];
386
			RadK2[i] = testline[3];
387
			RKStart[i] = j;
388
			RKCnt[i] = 0;
389
			if (i != 0)
390
			{
391
				if (RKCnt[i-1] > rmax) rmax = RKCnt[i-1];
392
			}
393
		}
394
		else
395
		{
396
			for (k = 0; k < strlen(testline); k++)
397
			{
398
				if (testline[k] < 127) continue;
399
				RKanj1[j] = testline[k];
400
				RKanj2[j] = testline[k+1];
401
				RKCnt[i]++;
402
				j++;
403
				k++;
404
			}
405
		}
406
	}
407
	fclose(fk);
408
	NoRads = i;
409
	for (i=0; i<10;i++)
410
	{
411
		RKSet[i] = malloc(2*rmax+1);
412
		if (RKSet[i] == NULL)
413
		{
414
			printf("malloc() failed for Radical set(s)!\n");
415
		}
416
	}
417
}
418
/*=====DoRomaji==collect search key from keyboard. Convert to kana.======*/
419
/*	Code originally from JDIC			*/
420
void DoRomaji(unsigned char kc)
421
{
422
	int is;
423
	unsigned char c;
424
425
/* collect the search string   */
426
  Smode = 1;
427
  if(kc == '#') Smode = 2;
428
  is = 0;
429
  ksp = 0;
430
  c = 0;
431
  ktarg[0] = 0;
432
  printf("\r                                         \r%sROMAJI ENTRY:%s ",RVon,RVoff);
433
  while (c != 0x0a)	  /* loop until Enter is pressed   */
434
  {
435
	c = getcharxx();
436
437
	if ((c == 0x8)||(c == 0x7f))  /* backspace  */
438
	{
439
			if (is != ksp)  /* kana mode - back over a romaji  */
440
			{
441
				ksch[is-1] = '\0';
442
				is--;
443
  				sprintf(tempout,"\r                                     \r%sROMAJI ENTRY:%s %s%s",RVon,RVoff,ktarg,ksch+ksp);
444
				KOut(tempout);
445
				continue;
446
			}
447
			else
448
			{
449
				if (strlen(ktarg) != 0)  /* kana mode - back over a kana  */
450
				{
451
					ktarg[strlen(ktarg)-2] = '\0';
452
  					sprintf(tempout,"\r                                     \r%sROMAJI ENTRY:%s %s%s",RVon,RVoff,ktarg,ksch+ksp);
453
					KOut(tempout);
454
					continue;
455
				}
456
			}
457
	}
458
	if (c > 32)   /* ordinary character - store and display   */
459
	{
460
		ksch[is] = c | 0x20;
461
		ksch[is+1] = '\0';
462
		is++;
463
  		sprintf(tempout,"\r                                     \r%sROMAJI ENTRY:%s %s%s",RVon,RVoff,ktarg,ksch+ksp);
464
		KOut(tempout);
465
  	}
466
	if (ksp != is)
467
	{
468
469
/* the input string so far is now parsed for romaji -> kana conversions.
470
   function "kanaconv" is used for the various sub-strings    */
471
472
/* if there is an "nn", "nm" or "mm", convert to first to "q" to force it to be
473
   converted to a kana "n"   */
474
		if ((ksch[ksp] == 'n')&&(ksch[ksp+1] == 'n')) ksch[ksp] = 'q';
475
		if ((ksch[ksp] == 'm')&&(ksch[ksp+1] == 'm')) ksch[ksp] = 'q';
476
		if ((ksch[ksp] == 'n')&&(ksch[ksp+1] == 'm')) ksch[ksp] = 'q';
477
/* if there is "nx", where "x" is not a vowel or a "y", force the "n".  */
478
		if (((ksch[ksp] == 'n')||(ksch[ksp] == 'm'))&&(ksch[ksp+1] != 0))
479
		{
480
			c = ksch[ksp+1];
481
			if((c!='y')&&(c!='a')&&(c!='e')&&(c!='i')&&(c!='o')&&(c!='u')&&(c!='\''))
482
			{
483
				ksch[ksp] = 'q';
484
			}
485
		}
486
		if(kanaconv(ksp,1))  /*  match on a,e,i,o,u,q or -  ?  */
487
		{
488
			continue;
489
		}
490
		if(ksch[ksp] == '\'')
491
		{
492
			ksp++;
493
			continue;
494
		}
495
		if (ksch[ksp] == ksch[ksp+1])  /* double consonant - force small
496
						 "tsu" */
497
		{
498
			ksch[ksp] = '*';
499
			kanaconv(ksp,1);
500
			continue;
501
		}
502
		if(kanaconv(ksp,2))  /* match on two letter syllable   */
503
		{
504
			continue;
505
		}
506
		if(kanaconv(ksp,3))  /*match on 3 letter syllable    */
507
		{
508
			continue;
509
		}
510
		if(kanaconv(ksp,4))  /*match on 4 letter syllable    */
511
		{
512
			continue;
513
		}
514
	}		/* end of rom->kana */
515
  }			/* end of while loop */
516
  if (ksch[ksp] =='n')  /*flush out a trailing "n"*/
517
  {
518
	ksch[ksp] = 'q';
519
	kanaconv(ksp,1);
520
  }
521
522
  strcpy(instr,ktarg);
523
}
524
525
/*========kanaconv==convert romaji to kana==============================*/
526
/*	More code from JDIC			*/
527
int kanaconv(int rpos, int rlen)
528
{
529
/* rpos and rlen are the start and length of the romaji characters in
530
   array ksch.  Smode specifies hira or kata. If found, the kana is
531
   appended to ktarg and TRUE returned.                          */
532
533
	int koff,ki;
534
	unsigned char targ[50];
535
536
	koff = (Smode-1)*NRKANA;
537
	for(ki = 0; ki < rlen; ki++)
538
	{
539
		targ[ki] = ksch[rpos+ki];
540
	}
541
	targ[rlen] = 0;
542
	for (ki = koff; ki < koff+NRKANA; ki+=2)
543
	{
544
		if (strlen(targ) != strlen(kanatab[ki])) continue;
545
		if (stringcomp(targ,kanatab[ki]) != 0) continue;
546
		strcat(ktarg,kanatab[ki+1]);
547
		ksp = ksp+rlen;
548
  		sprintf(tempout,"\r                                     \r%sROMAJI ENTRY:%s %s%s",RVon,RVoff,ktarg,ksch+ksp);
549
		KOut(tempout);
550
		return (TRUE);
551
	}
552
	return(FALSE);
553
}
554
555
/* ==== LoadKana=== Load the romaji to kana conversion file============*/
556
void LoadKana()
557
{
558
	int i,ih,mode;
559
	FILE *fp,*fopen();
560
	unsigned char LKin[80],*ptr;
561
562
	for (i = 0; i < NRKANA*2; i++) strcpy(kanatab[i]," ");
563
	fp = xfopen(ROMname,"r", &xfilelen);
564
	mode = 0;
565
	while (!feof(fp))
566
	{
567
		fgets(LKin,79,fp);
568
		if (feof(fp))break;
569
		/*LKin[strlen(LKin)-1] = '\0';*/
570
		if(LKin[0] == '#')continue;
571
		if((LKin[0] == '$')&&((LKin[1]|0x20) == 'h'))
572
		{
573
			mode = 0;
574
			ih = 0;
575
			continue;
576
		}
577
		if((LKin[0] == '$')&&((LKin[1]|0x20) == 'k'))
578
		{
579
			mode = 1;
580
			ih = 0;
581
			continue;
582
		}
583
		ptr = (unsigned char *)strtok(LKin," \t");
584
		if ( ptr != NULL ) strcpy(kanatab[mode*NRKANA+ih*2+1],ptr);
585
		ptr = (unsigned char *)strtok(NULL," \t\r\n");
586
		if ( ptr != NULL ) strcpy(kanatab[mode*NRKANA+ih*2],ptr);
587
		ih++;
588
		if(ih == NRKANA) 
589
		{
590
			printf("Too many romaji table entries!");
591
			exit(1);
592
		}
593
	}
594
	fclose(fp);
595
}
596
/*======jis2sjis  (from Ken Lunde) =====================================*/
597
void jis2sjis(unsigned char *p1,unsigned char *p2) /* courtesy of Ken Lunde */
598
{
599
    register unsigned char c1 = *p1;
600
    register unsigned char c2 = *p2;
601
    register int rowOffset = c1 < 95 ? 112 : 176;
602
    register int cellOffset = c1 % 2 ? 31 + (c2 > 95) : 126;
603
604
    *p1 = ((c1 + 1) >> 1) + rowOffset;
605
    *p2 = c2 + cellOffset;
606
}
607
608
/*====KEOS===End of screen processing for KFlush==================*/
609
int KEOS (unsigned char *msg)
610
{
611
	unsigned char ck;
612
613
	if (DRow < MaxY-2) return(TRUE);
614
	DRow = 0;
615
	if (strlen(msg) == 0) return (TRUE);
616
	printf("%s\n",msg);
617
	ck = getcharxx();
618
	if ((ck == 'n')||(ck == 'N')) return (FALSE);
619
	if ((ck == 'y')||(ck == 'Y')) return (TRUE);
620
	if ((ck != 0xa) && (ck != 0xd)) ungetc(ck,stdin);
621
	return (FALSE);
622
}
623
/*====KFlush==== Output a line, folding if necessary===============*/
624
/*	This is now the main screen output routine. An array "KLine"
625
	is built up of text to be output. KFlush() sends it to the screen,
626
	folding the line at white-space if it is about to hit the RHS.
627
	It also tests for end of screen, and prompts for continuation,
628
	using the KEOS(). Returns TRUE for continue, and FALSE for 
629
	stopping.  
630
	Called with the prompt measseage.			*/
631
632
int KFlush(unsigned char *msg)
633
{
634
	unsigned char *kptr,ktemp[512];
635
	int retf,it,j;
636
	int Test;
637
638
	if (strlen(KLine) == 0) return (TRUE);
639
	kptr = (unsigned char *)strtok(KLine," ");
640
	while (kptr != NULL)
641
	{
642
		Test = MaxX;
643
		strcpy(ktemp,kptr);
644
		if (ktemp[0] == '\t')
645
		{
646
			it = DCol % 10;
647
			if (it != 0) 
648
			{
649
				DCol = DCol+10-it;
650
				if (DCol <= Test-1)
651
				{
652
					for (j = 0;j < 10-it;j++) KOut(" ");
653
				}
654
			}
655
			strcpy(ktemp,ktemp+1);
656
		}
657
		it = strlen(ktemp);
658
		if (DCol+it < Test)
659
		{
660
			DCol = DCol+it+1;
661
		}
662
		else
663
		{
664
			KOut("\n");
665
			DRow++;
666
			DCol=it+1;
667
			retf = KEOS(msg);
668
			if (!retf) return (FALSE);
669
		}
670
		KOut(ktemp);
671
		if (DCol <= MAXCOLS) KOut(" ");
672
		kptr = (unsigned char *)strtok(NULL," ");
673
	}
674
	KOut("\n");
675
	DRow++;
676
	retf = KEOS(msg);
677
	DCol = 0;
678
	if (!retf) return (FALSE);
679
	return (TRUE);
680
}
681
/*====KOutc===add a character to KLine=============================*/
682
void KOutc(int c)
683
{
684
	unsigned char tmp[2];
685
	tmp[0] = c;
686
	tmp[1] = 0;
687
	strcat(KLine,tmp);
688
	return;
689
}
690
691
/*=====KOut===output kana/kanji according to selected mode===========*/
692
/*	Mostly called from KFlush. Outputs the parameter string, in the
693
	specified JIS mode.					*/
694
void KOut(unsigned char *sout)
695
{
696
	int i;
697
	unsigned char c1,c2;
698
699
	for (i = 0; i < strlen(sout); i++)
700
	{
701
		c1 = sout[i]; c2 = sout[i+1];
702
		if (c1 < 127)
703
		{
704
			if (c1 == '~') c1 = ' ';
705
			printf("%c",c1);
706
			continue;
707
		}
708
		switch(Omode)
709
		{
710
		case 0 :  /* JIS (default)  */
711
			if (c1 == 0x8f)
712
			{
713
				printf("%c$(D%c%c%c(B",0x1b,c2&0x7f,sout[i+2]&0x7f,0x1b);
714
				i+=2;
715
				break;
716
			}
717
			printf("%c$B%c%c%c(B",0x1b,c1&0x7f,c2&0x7f,0x1b);
718
			i++;
719
			break;
720
721
		case 1 : /* EUC (internal format) */
722
			if (c1 == 0x8f)
723
			{
724
				printf("%c%c%c",c1,c2,sout[i+2]);
725
				i+=2;
726
				break;
727
			}
728
			printf("%c%c",c1,c2);
729
			i++;
730
			break;
731
732
		case 2 : /* Shift-JIS */
733
			c1 -= 128; c2 -= 128;
734
			jis2sjis(&c1,&c2);
735
			printf("%c%c",c1,c2);
736
			i++;
737
			break;
738
		}
739
	}
740
}
741
742
/*======function to test if this entry has already been displayed=====*/
743
int addhit(long spot)
744
{
745
	int i;
746
747
	if (spot == 0) return(TRUE);
748
749
	for (i=0;i<=hitind;i++)
750
	{
751
		if(hittab[i] == spot) return(FALSE);
752
	}
753
	if(hitind < NOHITS) hitind++;
754
	hittab[hitind] = spot;
755
	return(TRUE);
756
}
757
758
/*====GetEUC - ensures that any JIS in the string is EUC ============*/
759
/*      Based on the GetEUC in JREADER, this routine examines
760
	the string "instr" and converts any JIS or SJIS to EUC.
761
	The result is placed in the specified string.        */
762
763
void GetEUC(unsigned char *eucline)
764
{
765
	int i,j,SI,J212,J212sw;
766
767
	J212 = FALSE;
768
	eucline[0] = '\0';
769
	chline = 0;
770
	chpos = 0;
771
	SI = FALSE;
772
	ShiftJIS = FALSE;
773
	j = 0;
774
	for (i = 0; i < strlen(instr); i++)
775
	{
776
		if((instr[i]==0x1b)&&(instr[i+1]=='$')&&(instr[i+2]=='(')&&(instr[i+3]=='D'))
777
		{
778
			SI = TRUE;
779
			J212 = TRUE;
780
			J212sw = 0;
781
			NoSJIS = TRUE;
782
			i+=3;
783
			continue;
784
		}
785
		if((instr[i]==0x1b)&&(instr[i+1]=='$')&&(instr[i+2]=='B'))
786
		{
787
			SI = TRUE;
788
			J212 = FALSE;
789
			i+=2;
790
			continue;
791
		}
792
		if((instr[i]==0x1b)&&(instr[i+1]=='$')&&(instr[i+2]=='@'))
793
		{
794
			SI = TRUE;
795
			J212 = FALSE;
796
			i+=2;
797
			continue;
798
		}
799
		if((instr[i]==0x1b)&&(instr[i+1]=='(')&&(instr[i+2]=='J'))
800
		{
801
			SI = FALSE;
802
			J212 = FALSE;
803
			i+=2;
804
			continue;
805
		}
806
		if((instr[i]==0x1b)&&(instr[i+1]=='(')&&(instr[i+2]=='B'))
807
		{
808
			SI = FALSE;
809
			J212 = FALSE;
810
			i+=2;
811
			continue;
812
		}
813
		if (instr[i] == '\0')break;
814
		if (SI)
815
		{
816
			if (J212 && (J212sw == 0)) eucline[j++] = 0x8f;
817
			eucline[j] = instr[i] | 0x80;
818
			J212sw = (J212sw+1) % 2;
819
		}
820
		else
821
		{
822
			eucline[j] = instr[i];
823
		}
824
		j++;
825
		eucline[j] = '\0';
826
	}
827
/*  fix up SHIFT-JIS, if present  */
828
	if (!NoSJIS) FixSJIS(eucline);
829
}
830
831
/*====FixSJIS=== convert any SJIS to EUC in a string==================*/
832
void FixSJIS(unsigned char *jline)
833
{
834
	int i,p1,p2,ShiftJIS;
835
836
	ShiftJIS = FALSE;
837
	for (i = 0; i < strlen(jline); i++)
838
	{
839
		p1 = jline[i];
840
		if (p1 < 127)continue;
841
		p2 = jline[i+1];
842
        	if ((p1 >= 129) && (p1 <= 159)) ShiftJIS = TRUE;
843
		if (((p1 >= 224) && (p1 <= 239))&& ((p2 >= 64) && (p2 <= 158))) ShiftJIS = TRUE;
844
		if (ShiftJIS)
845
		{
846
            		sjis2jis   (&p1,&p2);
847
            		p1 += 128;
848
            		p2 += 128;
849
			jline[i] = p1;
850
			jline[i+1] = p2;
851
		
852
		}
853
		i++;
854
	}
855
}
856
857
/*===sjis2jis    - convert ShiftJIS to JIS ===========================*/
858
859
void sjis2jis   (int *p1,int *p2)   /* Ken Lunde's routine  */
860
{
861
    register unsigned char c1 = *p1;
862
    register unsigned char c2 = *p2;
863
    register int adjust = c2 < 159;
864
    register int rowOffset = c1 < 160 ? 112 : 176;
865
    register int cellOffset = adjust ? (31 + (c2 > 127)) : 126;
866
867
    *p1 = ((c1 - rowOffset) << 1) - adjust;
868
    *p2 -= cellOffset;
869
}
870
871
/*====ExtFileDisp===Display from EDICTEXT file=======================*/
872
873
void ExtFileDisp()
874
{
875
876
	unsigned extrec[2],seekoff,iores,hi,lo,mid,ejdxtest[2];
877
	unsigned char Ssch[20];
878
	int bsres,i;
879
880
    printf("%sExtension Key:%s ",RVon,RVoff);
881
882
	GetKBStr("Extension Key:");
883
	if(fbuff[0] < 128) 
884
	{
885
		printf("\nThe extension file has Kanji & Kana keys\n");
886
		return;
887
	}
888
	if(!Extopen)
889
	{
890
		fe = xfopen(EXTname,"rb", &extlen);
891
        	fex  = xfopen(EXTJDXname,"rb", &extjdxlen);
892
		Extopen = TRUE;
893
		extlen++;
894
		fread(ejdxtest,sizeof(long),1,fex);
895
		if (ejdxtest[0] != (extlen+jiver))
896
		{
897
			printf("\nEDICT Extension file & Index Mismatch! %ld %ld\n",ejdxtest[0],extlen+jiver);
898
			exit(1);
899
		}
900
	}
901
902
	lo = 0;
903
	hi = (extjdxlen/(2*sizeof(long)))-1;
904
	while(TRUE)
905
	{
906
		mid = (lo+hi)/2;
907
		seekoff = mid;
908
		seekoff *= (2*sizeof(long));
909
		seekoff+=sizeof(long);
910
		if((iores = fseek(fex,seekoff,SEEK_SET)) != 0)SeekErr(iores);
911
		iores = fread(&extrec,sizeof(long),2,fex);
912
		seekoff = extrec[0];
913
		if((iores = fseek(fe,seekoff,SEEK_SET)) != 0)SeekErr(iores);
914
		iores = fread(&Ssch,sizeof(char),19,fe);
915
		Ssch[19] = 0;
916
		i = 0;
917
		bsres = 0;
918
		for (i = 0; Ssch[i] != 0 ;i++)
919
		{
920
			if (Ssch[i] < 128) break;
921
			if (fbuff[i] < 128) break;
922
			if (fbuff[i] < Ssch[i])
923
			{
924
				bsres = -1;
925
				break;
926
			}
927
			if (fbuff[i] > Ssch[i])
928
			{
929
				bsres = 1;
930
				break;
931
			}
932
		}
933
		if ((bsres != 0) && ((lo + hi) == 0)) break;
934
		if(bsres < 0)
935
		{
936
			if (mid == 0) break;
937
			hi = mid-1;
938
		}
939
		else
940
		{
941
			lo = mid+1;
942
		}
943
		if (bsres  == 0) break;
944
		if (lo > hi) break;
945
		if (hi < 0) break;
946
	}
947
	if (bsres == 0)
948
	{
949
		printf("\n%sDictionary Extension Display%s\n",RVon,RVoff);
950
		seekoff = extrec[1];
951
		if((iores = fseek(fe,seekoff,SEEK_SET)) != 0)SeekErr(iores);
952
		strcpy (KLine," <");
953
		DRow = 0;
954
		DCol = 0;
955
		while(!feof(fe))
956
		{
957
			fgets(LogLine,199,fe);
958
			if (feof(fe)) break;
959
			if ((LogLine[0] == '<') && (LogLine[1] > 128)) break;
960
			for (i = strlen(LogLine); i >= 0; i--)
961
			{
962
				if (LogLine[i] < 0x20) LogLine[i] = 0;
963
			}
964
			strcat(KLine,LogLine);
965
			if (!KFlush("Continue displaying extension information? (y/n)")) break;
966
			DCol = 0;
967
			strcpy (KLine," ");
968
		}
969
		return;
970
	}
971
	if (bsres != 0)
972
	{
973
		printf("\nNot found in Extension file!\n");
974
		return;
975
	}
976
}
977
/*====kcmp === comparison routine for qsort for kanji table==========*/
978
int kcmp (unsigned char *t1, unsigned char *t2)
979
{
980
	int i,cmp;
981
	for (i=0;i<4;i++)
982
	{
983
		cmp = t1[i] - t2[i];
984
		if (cmp == 0) continue;
985
		return(cmp);
986
	}
987
	return(0);
988
}
989
990
/*==== Verbinit===Load & initialize verb inflection details==========*/
991
void Verbinit()
992
{
993
	unsigned char tempstr[512],*vptr;
994
	int vmode,i;
995
	FILE *fi,*fopen();
996
	
997
	for (i = 0;i< VMAX;i++)
998
	{
999
		vdicf[i][0] = 0;
1000
		vinfl[i][0] = 0;
1001
		vcommno[i] = 40;
1002
	}
1003
	for (i = 0;i<40;i++)
1004
	{
1005
		vcomms[i][0] = 0;
1006
	}
1007
	vmode = 1;
1008
	strcpy(vcomms[40],"Unknown type");
1009
	verblen = 0;
1010
	fi = xfopen(Vname,"r", &xfilelen);
1011
	while(TRUE)
1012
	{
1013
		fgets(tempstr,511,fi);
1014
		if(feof(fi))break;
1015
		if (tempstr[0] == '#')continue;
1016
		if (tempstr[0] == '$')
1017
		{
1018
			vmode = 2;
1019
			continue;
1020
		}
1021
		
1022
		switch (vmode) {
1023
		case 1 :
1024
			vptr = (unsigned char *)strtok(tempstr," \t\n\r");
1025
			i = atoi(vptr);
1026
			if ((i < 0) || (i > 39)) break;
1027
			vptr = (unsigned char *)strtok(NULL,"\t\n\r");
1028
			strcpy(vcomms[i],vptr);
1029
			break;	
1030
		case 2 :	
1031
			vptr = (unsigned char *)strtok(tempstr," \t\n\r");
1032
			strcpy(vinfl[verblen],vptr);
1033
			vptr = (unsigned char *)strtok(NULL," \t\n\r");
1034
			strcpy(vdicf[verblen],vptr);
1035
			vptr = (unsigned char *)strtok(NULL," \t\n\r");
1036
			i = atoi(vptr);
1037
			if ((i >= 0)&&(i <= 40)) vcommno[verblen] = i;
1038
			verblen++;
1039
			if (verblen==VMAX)
1040
			{
1041
				printf("Verb data overflow. Ignoring following entries\n");
1042
				verblen--;
1043
				fclose(fi);
1044
				return;
1045
			}
1046
			break;
1047
		}
1048
	}
1049
	verblen--;
1050
	fclose(fi);
1051
}
1052
1053
/*=== append a kana/kanji to the verb display line================*/
1054
void AppKanji(unsigned char c1,unsigned char c2)
1055
{
1056
	unsigned char ops[3];
1057
1058
	ops[0] = c1;
1059
	ops[1] = c2;
1060
	ops[2] = 0;
1061
	strcat(vline,ops);
1062
}
1063
1064
/*=======Vlookup== look up plain form verb in dictionary=============*/
1065
int Vlookup()
1066
{
1067
1068
	int xjresp,roff,rlen;
1069
	unsigned char repstr[512];
1070
	long respos;
1071
	int hit,schix;
1072
	unsigned char khi,klo,cc,ops[4];
1073
	long it;
1074
1075
	DicNum = CurrDic;
1076
	khi = 0;
1077
	klo = 0;
1078
	vline[0] = 0;
1079
	xjdserver (XJ_FIND, DicNum,it, strlen(vstr), vstr, 
1080
		&xjresp, &respos, &roff, &rlen, repstr, &DicLoc);
1081
	if (xjresp != XJ_OK) return (FALSE);
1082
	it = respos;
1083
	while (xjresp == XJ_OK)
1084
	{
1085
		if (roff != 0)
1086
		{
1087
			it++;
1088
			xjdserver (XJ_ENTRY, DicNum, it, strlen(vstr), vstr, 
1089
				&xjresp, &respos, &roff, &rlen, repstr, &DicLoc);
1090
			continue;
1091
		}
1092
		schix = 0;
1093
		/* now work forwards, displaying the line  */
1094
		hit = FALSE;
1095
		/* If the first word has already been displayed, skip it.
1096
	   	Otherwise, put braces around the first word (kanji)  */
1097
		while (TRUE)
1098
		{
1099
			cc = repstr[schix];
1100
			if (cc < 32) break;
1101
			/* put a () around the yomikata in an ascii or Kanji search */
1102
			if (cc=='[')
1103
			{
1104
				AppKanji(0xa1,0xcA);  /* EUC (  */
1105
				schix++;
1106
				continue;
1107
			}
1108
			if (cc==']')
1109
			{
1110
				AppKanji(0xA1,0xCB);  /* EUC )  */
1111
				schix++;
1112
				continue;
1113
			}
1114
			if (cc < 128) /* non-Japanese, display it
1115
					(fix up the "/") */
1116
			{
1117
				ops[0]= cc;
1118
				ops[1] = '\0';
1119
				if (ops[0] == '/')
1120
				{
1121
					if (hit)
1122
					{
1123
						ops[0] = ',';
1124
						ops[1] = ' ';
1125
						ops[2] = '\0';
1126
					}
1127
					else
1128
					{
1129
						hit = TRUE;
1130
						ops[0] = '\0';
1131
					}
1132
				}
1133
				strcat(vline,ops);
1134
			}
1135
			else
1136
			{
1137
				if (cc == 0x8f)
1138
				{
1139
					AppKanji(cc,0);
1140
					schix++;
1141
					continue;
1142
				}
1143
				if (khi == 0)
1144
				{
1145
					khi = cc;
1146
				}
1147
				else
1148
				{
1149
					klo = cc;
1150
					AppKanji(khi,klo);
1151
					khi = 0;
1152
					klo = 0;
1153
				}
1154
			}
1155
			schix++;
1156
		}
1157
        	if(strlen(vline) > 0) break;
1158
     	}
1159
     	if(strlen(vline) <=1) return(FALSE);
1160
     	return(TRUE);
1161
}
1162
/*=========== slencal - calculate the actual search string length ===*/
1163
/*   This routine exists to sort out the actual length of the search
1164
string when there is a mix of 2 and 3-byte EUC characters   */
1165
1166
int slencal (int noch, unsigned char *targ)
1167
{
1168
	int i,j;
1169
1170
	if (targ[0] < 127) return(noch+1);
1171
	i = 0;
1172
	j = 0;
1173
	while(i <= noch)
1174
	{
1175
		if (targ[j] == 0x8f) j++;
1176
		i++;
1177
		j+=2;
1178
	}
1179
	return(j);
1180
}
1181
1182
/*=========== Lookup - global frontend to dictionary search ========*/
1183
void Lookup()
1184
{
1185
	int gi,dicsav,gdiclenbest;
1186
	unsigned char retsave[KFBUFFSIZE],rethdr[5];
1187
1188
	retsave[0] = 0;
1189
	gdiclen = 0;
1190
	gdiclenbest = 0;
1191
	if ((!GDmode) || (Dmode == 1))
1192
	{
1193
		Lookup2();
1194
		return;
1195
	}
1196
	if (gdicmax == 0)
1197
	{
1198
		printf("\nNo global dictionaries specified - disabling!\n");
1199
		GDmode = FALSE;
1200
		Lookup2();
1201
		return;
1202
	}
1203
	dicsav = CurrDic;
1204
	for (gi=0;gi<=gdicmax;gi++)
1205
	{
1206
		CurrDic = gdicnos[gi];
1207
		Lookup2();
1208
		if (GDALLmode)
1209
		{
1210
			strcpy(retsave,KLine);
1211
			strcpy(rethdr,"[X] ");
1212
			rethdr[1] = '0'+gdicnos[gi];
1213
			strcpy(KLine,rethdr);
1214
			strcat(KLine,retsave);
1215
			KFlush("");
1216
			continue;
1217
		}
1218
		if ((gdiclen > gdiclenbest) || ((strlen(KLine) > strlen(retsave)) && (gdiclen == gdiclenbest)))
1219
		{
1220
			strcpy(retsave,KLine);
1221
			strcpy(rethdr,"[X] ");
1222
			rethdr[1] = '0'+gdicnos[gi];
1223
			gdiclenbest = gdiclen;
1224
		}
1225
	}
1226
	if (GDALLmode)
1227
	{
1228
		CurrDic = dicsav;
1229
		return;
1230
	}
1231
	strcpy(KLine,rethdr);
1232
	strcat(KLine,retsave);
1233
	KFlush("");
1234
	CurrDic = dicsav;
1235
}
1236
1237
1238
1239
/*=========== Lookup2 - carry out dictionary search ================*/
1240
void Lookup2()
1241
{
1242
/*
1243
1244
Carries out a search for matches with the string "fbuff" in the specified
1245
dictionary. It matches the full string, then progressively shortens it.
1246
1247
*/
1248
  	int schix,schiy,schiz,j,dind,hit,skip,brace,engrv;
1249
  	int EngFirst;
1250
	int slk,slen,slenx,i,srchlen,srchlenok;
1251
  	unsigned int khi,klo,cc;
1252
  	unsigned long zz,bshit[20];
1253
  	unsigned char *kptr,*kptr2, k1,k2,kanj1,kanj2,kanj3;
1254
	int FiltOK;
1255
	unsigned char vlast[11],temp[11],ops[80];
1256
	int vi,vok,prevch,KDNSflag,KDskip,KTest;
1257
	int xjresp,roff,rlen;
1258
	unsigned char repstr[512];
1259
	unsigned long respos;
1260
1261
	vlast[0] = 0;
1262
	KLcount = 0;
1263
	DRow = 3;
1264
1265
	if (!GDmode && (Dmode == 0)&&Jverb&&(fbuff[0] > 0xa8) && (fbuff[2] < 0xa5) && (fbuff[4] < 0xa5))
1266
	{
1267
	/* possible inflected verb or adjective, so look up the verb table
1268
	   for a matching plain form, and serch the dictionary for it      */
1269
1270
		vstr[0] = fbuff[0];
1271
		vstr[1] = fbuff[1];
1272
		vstr[2] = 0;
1273
		for(i=0;i<11;i++) temp[i] = fbuff[i];
1274
1275
		for (vi = 0;vi <= verblen;vi++)
1276
		{
1277
			vok = TRUE;
1278
			vstr[2] = 0;
1279
			for (i = 0;i < strlen(vinfl[vi]);i++)
1280
			{
1281
				if (fbuff[2+i] != vinfl[vi][i])
1282
				{
1283
					vok = FALSE;
1284
					break;
1285
				}
1286
			}
1287
			if (!vok) continue;
1288
			strcat(vstr,vdicf[vi]);
1289
			
1290
			if(strcmp(vstr,temp) == 0) continue;
1291
			if (strcmp(vstr,vlast) == 0)continue;
1292
			if (Vlookup())
1293
			{
1294
				strcpy(vlast,vstr);
1295
				printf(" Possible inflected verb or adjective: (%s)\n",vcomms[vcommno[vi]]);				
1296
				strcpy(KLine,vline);
1297
				DCol = 0;
1298
				KFlush("");
1299
1300
				printf("Continue with this search (y/n)\n");
1301
				cc = getcharxx();
1302
				if ((cc == 'n')||(cc == 'N')) return;
1303
				if ((cc != 'y')&&(cc != 'Y'))
1304
				{
1305
					ungetc(cc,stdin);
1306
					return;
1307
				}
1308
				DRow = 1;
1309
			}		
1310
		}
1311
  	}
1312
	/*  do a binary search through the index table looking for a match  */
1313
	KLine[0] = 0;
1314
	DCol = 0;
1315
	nok = 0;
1316
	DispHit = FALSE;
1317
  	khi = 0;
1318
  	klo = 0;
1319
	DicNum = CurrDic;
1320
	if(Dmode !=0) DicNum = 0;
1321
	if (fbuff[strlen(fbuff)-1] < 32) fbuff[strlen(fbuff)-1] = 0;
1322
	for (slenx = 1; slenx < 20; slenx++)
1323
	{
1324
		if (slencal(slenx-1,fbuff) >= strlen(fbuff)) break;
1325
	}
1326
	Smode = 0;
1327
	if ((fbuff[0] == 0xa4)||(fbuff[0] == 0xa5)) 
1328
	{
1329
		Smode = 1;
1330
		for (i = 0; i < strlen(fbuff); i+=2)
1331
		{
1332
			if (fbuff[i] > 0xa5)
1333
			{
1334
				Smode = 0;
1335
				break;
1336
			}
1337
		}
1338
	}
1339
	srchlenok = 0;
1340
  	for ( slen = 0; slen <slenx; slen++)
1341
  	{
1342
		srchlen = slencal(slen,fbuff);
1343
  		xjdserver (XJ_FIND, DicNum, zz, srchlen, fbuff, 
1344
			&xjresp, &respos, &roff, &rlen, repstr, &DicLoc);
1345
/* printf("F: Returning: %d %ld %d %d %s\n",xjresp,respos,roff,rlen,repstr); */
1346
  		if (xjresp == XJ_OK) 
1347
  		{
1348
  			bshit[slen] = respos;
1349
  			srchlenok = srchlen;
1350
  			continue;
1351
  		}
1352
  		else
1353
  		{
1354
  			break;
1355
  		}
1356
	}
1357
  	if (slen == 0)
1358
  	{
1359
		if (!GDmode) printf("No Match Found\n");
1360
		return;
1361
  	}
1362
	if (EMmode == 0) 
1363
	{
1364
		if (srchlenok != strlen(fbuff))
1365
		{
1366
			printf("No Match Found (Exact Test)\n");
1367
			return;
1368
		}
1369
		if (((fbuff[0] <127) && isalnum(repstr[roff+srchlenok])) 
1370
			||((fbuff[0] >=0xa4) && (repstr[roff+srchlenok] > 127)))
1371
		{
1372
			printf("No Match Found (Exact Test)\n");
1373
			return;
1374
		}
1375
	}
1376
  	hitind = 0;
1377
  	hittab[0] = -1;
1378
	gdiclen = slen*2;
1379
	/*  loop for each possible string length  */
1380
	for (dind = slen-1; dind >= 0 ; dind--)
1381
	{
1382
		/* this is the display loop - usually one line per entry  */
1383
		it = bshit[dind];
1384
  		while (TRUE) /* display as long as there are matches*/
1385
  		{
1386
			srchlen = slencal(dind,fbuff);
1387
  			xjdserver (XJ_ENTRY, DicNum, it, srchlen, 
1388
			fbuff, &xjresp, &respos, &roff, &rlen, repstr, &DicLoc);
1389
/* printf("E: Returning: %d %ld %d %d %ld %s\n",xjresp,respos,roff,rlen,DicLoc,repstr);  */
1390
  			if (xjresp != XJ_OK) break;
1391
			schix =  0;
1392
			schiy = roff;
1393
		/* make copy of line for filter testing   */
1394
			slk=0;
1395
			for(schiz=0;repstr[schiz]>=0x20;schiz++)
1396
			{
1397
				testline[schiz] = repstr[schiz];
1398
				if(testline[schiz]=='/')slk++;
1399
			}
1400
			testline[schiz] = '\0';
1401
			kanj1 = testline[0];
1402
			kanj2 = testline[1];
1403
			FiltOK = TRUE;
1404
		/* now if filters are active, check the line for a match   */
1405
			if((Dmode == 0)&& nofilts && (!GDmode))
1406
			{
1407
				if(nofilts>0)
1408
				{
1409
					for (i=0;i<NOFILT;i++)
1410
					{
1411
						if(!filtact[i]||!filton[i])continue;
1412
						if(filttype[i] == 0)
1413
						{
1414
							FiltOK = FALSE;
1415
							for(j=0;j<filtcoden[i];j++)
1416
							{
1417
								if(strstr(testline,filtcodes[i][j]) != NULL)
1418
								{
1419
									FiltOK = TRUE;
1420
									break;
1421
								}
1422
							}
1423
							if(FiltOK)break;
1424
						}
1425
						if((filttype[i] == 1)||((filttype[i] == 2)&&(slk <= 2)))
1426
						{
1427
							FiltOK = TRUE;
1428
							for(j=0;j<filtcoden[i];j++)
1429
							{
1430
								if(strstr(testline,filtcodes[i][j]) != NULL)
1431
								{
1432
									FiltOK = FALSE;
1433
									break;
1434
								}
1435
							}
1436
								if(!FiltOK) break;
1437
						}
1438
					}
1439
				}
1440
			}
1441
			if(strf && (Dmode ==1))
1442
			{
1443
				FiltOK = FALSE;
1444
				if (strstr(testline,strfilt) != NULL) FiltOK = TRUE;
1445
			}
1446
			if ((Dmode == 0) && SFFlag && FiltOK)
1447
			{
1448
				FiltOK = FALSE;
1449
				kptr = strstr(testline,SingleFilter);
1450
				if ((kptr != NULL) && (SingleFilter[0] < 128))
1451
				{
1452
					FiltOK = TRUE;
1453
				}
1454
				else
1455
				{
1456
					while (TRUE)
1457
					{
1458
						if (kptr == NULL) break;
1459
						if ((strlen(testline) - strlen(kptr)) % 2 == 0)
1460
						{
1461
							FiltOK = TRUE;
1462
							break;
1463
						}
1464
						else
1465
						{
1466
							kptr2 = kptr;
1467
							kptr = strstr(kptr2+1,SingleFilter);
1468
						}
1469
					}
1470
				}
1471
			}
1472
			if ((EMmode == 0) && (((fbuff[0] <127) && isalnum(repstr[roff+srchlenok])) 
1473
				||((fbuff[0] >=0xa4) && (repstr[roff+srchlenok] > 127))))
1474
			{
1475
				FiltOK = FALSE;
1476
			}
1477
			KFlushRes = TRUE;
1478
		/* now work forwards, displaying the line  */
1479
			KTest = TRUE;
1480
			if (((fbuff[0] > 0xa5) || (fbuff[0] == 0x8f)) && (roff != 0)) KTest = FALSE;
1481
			if ((Dmode == 0) && GDmode) KTest = TRUE;
1482
			if (roff != 0) gdiclen--;
1483
			if (FiltOK && addhit(DicLoc) && (!FirstKanj || (FirstKanj && KTest)))
1484
			{
1485
				DispHit = TRUE;
1486
				if ((Dmode == 0) &&(ROmode == 0))
1487
				{
1488
			/* We are in "raw output mode, so just splat out the EDICT line.
1489
			   Note that this block does its own "end-of display" processing */
1490
					for(schiz=0;repstr[schiz]>=0x20;schiz++)
1491
					{
1492
						KLine[schiz] = repstr[schiz];
1493
					}
1494
					KLine[schiz] = '\0';
1495
					KFlushRes = KFlush("Continue displaying matches? (y/n)");
1496
					it++;
1497
					if (!KFlushRes) return;
1498
					continue;
1499
				}
1500
				engrv = FALSE;
1501
				EngFirst = TRUE;
1502
				if((Dmode == 0) || ((Dmode ==1)&&(KLRmode == 0))) KLine[0] = 0;
1503
				if (Dmode == 0)
1504
				{
1505
					if (fbuff[0] == SPTAG)
1506
					{
1507
						sprintf(ops,"%d: ",dind);
1508
						strcpy(KLine,ops);
1509
					}
1510
					else
1511
					{
1512
						sprintf(ops,"%d: ",dind+1);
1513
						strcpy(KLine,ops);
1514
					}
1515
					DCol = 0;
1516
				}
1517
				strcat(KLine," ");
1518
				if (Dmode == 1)
1519
				{
1520
					KDNSflag = TRUE;
1521
					prevch = 0;
1522
					ops[0] = 0xa1; ops[1] = 0xda;
1523
					kanj1 =  repstr[0];
1524
					kanj2 =  repstr[1];
1525
					kanj3 =  repstr[2];
1526
					ops[2] =  repstr[0];
1527
					ops[3] =  repstr[1];
1528
					ops[4] =  repstr[2];
1529
					ops[5] = 0;
1530
					if (ops[2] != 0x8f) ops[4] = 0;
1531
					instr[0] = 0xa1; instr[1] = 0xdb; instr[2] = 0;
1532
					strcat (ops,instr);
1533
					if (KLRmode == 0) 
1534
					{
1535
						if (ops[2] == 0x8f)
1536
						{
1537
							sprintf (instr,"%s 1-%x%x [1-%d]",ops,kanj2&0x7f,kanj3&0x7f,((kanj2&0x7f)-0x20)*100+(kanj3&0x7f)-0x20);
1538
							schix+=8;
1539
1540
						}
1541
						else
1542
						{
1543
							k1 = kanj1 & 0x7f;
1544
							k2 = kanj2 & 0x7f;
1545
							jis2sjis(&k1,&k2);
1546
							sprintf (instr,"%s %x%x [%d:%x%x]",ops,kanj1&0x7f,kanj2&0x7f,((kanj1&0x7f)-0x20)*100+(kanj2&0x7f)-0x20,k1,k2);
1547
							schix+=7;
1548
						}	
1549
						strcat(KLine,instr);
1550
					}
1551
					else
1552
					{
1553
						KLine[0] = 0;
1554
						karray[nok][2] = kanj1;
1555
						karray[nok][3] = kanj2;
1556
						karray[nok][4] = kanj3;
1557
						kptr = strstr(testline,"S");
1558
						if (kptr == NULL)
1559
						{
1560
							karray[nok][0] = 0;
1561
						}
1562
						else
1563
						{
1564
							karray[nok][0] = atoi(kptr+1);
1565
						}
1566
						kptr = strstr(testline,"B");
1567
						if (kptr == NULL)
1568
						{
1569
							karray[nok][1] = 0;
1570
						}
1571
						else
1572
						{
1573
							karray[nok][1] = atoi(kptr+1);
1574
						}
1575
						KLcount++;
1576
						if (nok < KANJARRAYSIZE) nok++;
1577
						continue;
1578
					}
1579
				}
1580
				KDskip = FALSE;
1581
				while ((cc = repstr[schix]) != 0x0a)
1582
				{
1583
					if (cc == 0x0d) break;
1584
					if (RVACTIVE && (schix == roff)) strcat (KLine,RVon);
1585
					if (RVACTIVE && (schix == roff+srchlen)) strcat (KLine,RVoff);
1586
					if ((Dmode == 0) && (cc == '['))
1587
					{
1588
						ops[0] = 0xa1; ops[1] = 0xca; ops[2] = 0; /* JIS (  */
1589
						strcat(KLine,ops);
1590
						schix++;
1591
						continue;
1592
					}
1593
					if ((Dmode == 0) && (cc == ']'))
1594
					{
1595
						ops[0] = 0xa1; ops[1] = 0xcb; ops[2] = 0; /* JIS (  */
1596
						strcat(KLine,ops);
1597
						schix++;
1598
						continue;
1599
					}
1600
					if (KDskip)
1601
					{
1602
						if (cc == ' ')KDskip = FALSE;
1603
						schix++;
1604
						continue;
1605
					}
1606
					if (cc < 128) /* non-Japanese, display it (fix up the EDICT "/") */
1607
					{
1608
						ops[0]= cc;
1609
						ops[1] = '\0';
1610
						if (cc == SPTAG)
1611
						{
1612
							if (prieng)
1613
							{
1614
								strcat (KLine,RVon);
1615
								engrv = TRUE;
1616
							}
1617
							schix++;
1618
							continue;
1619
						}
1620
						if (!isalnum(cc) && engrv)
1621
						{
1622
							engrv = FALSE;
1623
							strcat (KLine,RVoff);
1624
						}
1625
						if (KDNSflag && (prevch == ' ') && (Dmode != 0))
1626
						{
1627
							if (strstr(KDNSlist,ops) != NULL)
1628
							{
1629
								KDskip = TRUE;
1630
								schix++;
1631
								continue;
1632
							}
1633
						}
1634
						prevch = cc;
1635
						if ((Dmode == 1) && (cc == '}'))  /* { */
1636
						{
1637
							ops[0] = ';';
1638
							if ((repstr[schix+2] == 0x0a) && (ops[0] == ';')) ops[0] = '\0';
1639
						}
1640
						if ((Dmode == 1) && (cc == '{')) 
1641
						{
1642
							KDNSflag = FALSE;
1643
							schix++;
1644
							continue;
1645
          				}
1646
						if ((Dmode == 0) && (ops[0] == '/'))
1647
						{
1648
							if (!EngFirst)
1649
							{
1650
								ops[0] = ';';
1651
								ops[1] = ' ';
1652
								ops[2] = 0;
1653
							}
1654
							else
1655
							{
1656
								EngFirst = FALSE;
1657
								ops[0] = 0;
1658
							}
1659
						}
1660
						if ((repstr[schix+1] == 0x0a) && (ops[0] == ';')) ops[0] = '\0';
1661
						if ((repstr[schix+1] == 0x0d) && (ops[0] == ';')) ops[0] = '\0';
1662
						strcat(KLine,ops);
1663
					}
1664
					else   /* kana or kanji  */
1665
					{
1666
						if (cc == 0x8f)
1667
						{
1668
							KOutc(0x8f);
1669
							schix++;
1670
							continue;
1671
						}
1672
						if (khi == 0)
1673
						{
1674
							khi = cc;
1675
						}
1676
						else
1677
						{
1678
							klo = cc;
1679
							KOutc(khi);
1680
							KOutc(klo);
1681
							khi = 0;
1682
						}
1683
					}
1684
					schix++;
1685
				}  /* end of line display loop  */
1686
				if (GDmode && (Dmode == 0)) return;  /* early return from global*/
1687
				if (Dmode == 0) KFlushRes = KFlush("Continue displaying matches? (y/n)");
1688
				if ((Dmode == 1)&&(KLRmode == 0)) KFlushRes = KFlush("Continue displaying matches? (y/n)");
1689
			}  /* of "once-per-line" test */
1690
			it++;
1691
			if ((Dmode == 0)||((Dmode ==1)&&(KLRmode == 0)))
1692
			{
1693
				if (!KFlushRes) return;
1694
			}
1695
		}  /*  end of the "while it matches loop  */
1696
		if (!DispHit) printf("No display for this key (filtered or non-initial kanji)\n");
1697
		if ((Dmode == 1)&&(KLRmode == 1)) 
1698
		{
1699
			if (nok >= KANJARRAYSIZE) printf("\nWarning! Kanji table overflow!\n");
1700
			if (nok == 0) return;
1701
1702
/* Thanks to Bruce Raup for the casting which fixed the compilation
1703
 * warning in the following qsort() call
1704
 */
1705
			qsort(&karray,nok,sizeof(karray[0]),(int (*) (const void *, const void *)) kcmp);
1706
			for (i = 0; i < nok; i++)
1707
			{
1708
				if ((i != 0) && (karray[i][2] == karray[i-1][2]) && (karray[i][3] == karray[i-1][3]) && (karray[i][4] == karray[i-1][4])) continue;
1709
				KOutc(karray[i][2]);
1710
				KOutc(karray[i][3]);
1711
				if (karray[i][2] == 0x8f) KOutc(karray[i][4]);
1712
				KOutc(' ');
1713
			}
1714
			KFlushRes = KFlush("Continue displaying kanji (y/n)");
1715
		}
1716
		if ((Dmode != 0) || (dind == 0)) return;
1717
		if (EMmode == 0) return;
1718
		printf("End of %d character matches. Continue for shorter matches? (y/n)\n\r",dind+1);
1719
		ops[0] = getcharxx();
1720
		DRow = 1;
1721
		fflush(stdin);
1722
		if ((ops[0] == 'y')||(ops[0] == 'Y')) continue;
1723
		if ((ops[0] != 'n')&&(ops[0] != 'N')) 
1724
		{
1725
			if ((ops[0] != 0x0a) && (ops[0] != 0x0d)) ungetc(ops[0],stdin);
1726
		}
1727
		break;
1728
	}  /* end of the dind loop  */
1729
}  /* end of lookup */
1730
1731
/*======RadSet=== set up Radicals for bushu search=================*/
1732
void RadSet()
1733
{
1734
	int i,errf;
1735
	unsigned char rstr[20];
1736
	FILE *fpr, *fopen();
1737
1738
	fpr = xfopen(Rname,"r", &xfilelen);
1739
	i = 0;
1740
	while(TRUE)
1741
	{
1742
		errf = (fgets(rstr,19,fpr) == NULL);
1743
		if (feof(fpr)||errf) break;
1744
		while(rstr[strlen(rstr)-1] < 0x20) rstr[strlen(rstr)-1] = 0;
1745
		if (rstr[3]  == '0') continue;
1746
		radkanj[i][0] = rstr[0];
1747
		radkanj[i][1] = rstr[1];
1748
		if (rstr[3] == '(')
1749
		{
1750
			radnos[i] = atoi(rstr+4);
1751
		}
1752
		else
1753
		{
1754
			radnos[i] = atoi(rstr+3);
1755
		}
1756
		i++;
1757
	}
1758
	fclose(fpr);
1759
	return;
1760
}
1761
/*=====DispLic======display GPL ==============================*/
1762
void DispLic()
1763
{
1764
	FILE *flic,*fopen();
1765
1766
	flic = xfopen(GPL_File,"r", &xfilelen);
1767
	DRow = 1;
1768
	DCol = 0;
1769
	while (!feof(flic))
1770
	{
1771
		fgets(KLine,81,flic);
1772
		if (feof(flic))
1773
		{
1774
			KFlush("");
1775
			return;
1776
		}
1777
		KLine[strlen(KLine)-1] = 0;
1778
		if(!KFlush("Continue Licence Display? (y/n)")) return;
1779
	}
1780
}
1781
/*=====DoRADICALS===display Theresa's Radical File================*/
1782
1783
void DoRADICALS()
1784
{
1785
1786
	int errf,j;
1787
	unsigned char rstr[20];
1788
	FILE *fpr, *fopen();
1789
1790
	fpr = xfopen(Rname,"r", &xfilelen);
1791
	printf("\n RADICAL DISPLAY \n");
1792
	DRow = 3;
1793
	DCol = 0;
1794
	KLine[0] = 0;
1795
	fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
1796
	j = 0;
1797
	while(TRUE)
1798
	{
1799
		errf = (fgets(rstr,19,fpr) == NULL);
1800
		if (feof(fpr)||errf)
1801
		{
1802
			KFlush("");
1803
			fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
1804
			return;
1805
		}
1806
		while(rstr[strlen(rstr)-1] < 0x20) rstr[strlen(rstr)-1] = 0;
1807
		if ((rstr[strlen(rstr)-2]  == ' ')&&(rstr[strlen(rstr)-1]  == '0'))
1808
		{
1809
			KFlushRes = KFlush("Continue displaying radicals? (y/n)");
1810
			if (!KFlushRes) return;
1811
			strcpy(KLine,"  ");
1812
			KFlushRes = KFlush("Continue displaying radicals? (y/n)");
1813
			if (!KFlushRes) return;
1814
			rstr[2] = 0;
1815
			sprintf(tempout,"%s Stroke Radicals ",rstr);
1816
			strcpy(KLine,tempout);
1817
			KFlushRes = KFlush("Continue displaying radicals? (y/n)");
1818
			if (!KFlushRes) return;
1819
			strcpy(KLine,"  ");
1820
			KFlushRes = KFlush("Continue displaying radicals? (y/n)");
1821
			if (!KFlushRes) return;
1822
			continue;
1823
		}
1824
		strcat(KLine,"\t");
1825
		KOutc(rstr[0]);
1826
		KOutc(rstr[1]);
1827
		KOutc(0xa1);
1828
		KOutc(0xa1);
1829
		sprintf(tempout,"%s ",rstr+3);
1830
		strcat(KLine,tempout);
1831
	}
1832
	KFlush("");
1833
}
1834
1835
/*=========KLookup=== Special front-end to Lookup for Kanji searches==========*/
1836
void KLookup()
1837
{
1838
	if (KLmode == 0)	/* Long display mode, just pass on call  */
1839
	{
1840
		KLRmode = 0;
1841
		Lookup();
1842
		return;
1843
	}			/* Short display mode - see how many there */
1844
	KLRmode = 1;
1845
	Lookup();
1846
	if (KLcount == 1)	/* only one - force long display	*/
1847
	{
1848
		KLRmode = 0;
1849
		Lookup();
1850
	}
1851
	return;
1852
}
1853
1854
/*=========DoJIS === JIS kanji lookup==========================================*/
1855
1856
void DoJIS()
1857
{
1858
	int i,ktf,sjf,hojof,i1,i2;
1859
	unsigned char cj;
1860
1861
	ktf = FALSE;
1862
	sjf = FALSE;
1863
	hojof = FALSE;
1864
	cbreakoff();
1865
	scanf("%s",instr);
1866
	cbreakon();
1867
	fflush(stdin);
1868
	cj = instr[0];
1869
	if ((cj == '-') || (cj == 'k') || (cj == 'K'))
1870
	{
1871
		ktf = TRUE;
1872
		strcpy(instr,instr+1);
1873
	}
1874
	else if ((cj == 's')||(cj == 'S'))
1875
	{
1876
		sjf = TRUE;
1877
		strcpy(instr,instr+1);
1878
	}
1879
	cj = instr[0];
1880
	if ((cj == 'h') || (cj == 'H'))
1881
	{
1882
		hojof = TRUE;
1883
		strcpy(instr,instr+1);
1884
	}
1885
	if (ktf)
1886
	{
1887
		for (i = 0;i <4; i++)
1888
		{
1889
			if ((instr[i] >= '0') && (instr[i] <= '9'))instr[i] = instr[i] - '0';
1890
		}
1891
		instr[0] = (instr[0]*10+instr[1]+0x20) | 0x80;
1892
		instr[1] = (instr[2]*10+instr[3]+0x20) | 0x80;
1893
		instr[2] = 0;
1894
	}
1895
	else
1896
	{
1897
		for (i = 0;i <4; i++)
1898
		{
1899
			if ((instr[i] >= '0') && (instr[i] <= '9'))instr[i] = instr[i] - '0';
1900
			if ((instr[i] >= 'a') && (instr[i] <= 'f'))instr[i] = instr[i]-'a'+0x0a;
1901
			if ((instr[i] >= 'A') && (instr[i] <= 'F'))instr[i] = instr[i]-'A'+0x0a;
1902
		}
1903
		if(sjf)
1904
		{
1905
			i1 = (instr[0] << 4) + instr[1];
1906
			i2 = (instr[2] << 4) + instr[3];
1907
			sjis2jis(&i1,&i2);
1908
			instr[0] = i1 | 0x80;
1909
			instr[1] = i2 | 0x80;
1910
			instr[2] = 0;
1911
		}
1912
		else
1913
		{
1914
			instr[0] = ((instr[0] << 4) + instr[1]) | 0x80;
1915
			instr[1] = ((instr[2] << 4) + instr[3]) | 0x80;
1916
			instr[2] = 0;
1917
		}
1918
	}
1919
	Dmode =1;
1920
	fbuff[0] = 0; fbuff[1] = 0;
1921
	if (hojof) fbuff[0] = 0x8f;
1922
	strcat(fbuff,instr);
1923
	fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
1924
	KLookup();
1925
	instr[0] = 0;
1926
}
1927
1928
/*=====  GetKBStr=== Collect ASCII or JIS string from keyboard=========*/
1929
1930
void GetKBStr(unsigned char *prompt)
1931
{
1932
	int ShowIt,escf,bit8,i;
1933
	unsigned char c;
1934
1935
	escf = FALSE;
1936
	bit8 = FALSE;
1937
	c='x'; /*keep lint happy*/
1938
	ShowIt = FALSE;
1939
1940
	for (i = 0; (c != 0xd) && (c != 0xa); i++)
1941
	{
1942
		instr[i+1] = 0;
1943
		c = getcharxx();
1944
		if (!bit8 && !escf && ((c == '@') || (c == '#')))
1945
		{
1946
			DoRomaji(c);
1947
			break;
1948
		}
1949
		if (c == 0x1b) escf = TRUE;
1950
		if (c > 0x7f) bit8 = TRUE;
1951
		if ((c == 0x7f) || (c == 8))
1952
		{
1953
			if(bit8) i--;
1954
			if( i > 0) instr[--i] = 0;
1955
			i--;
1956
			strcpy(fbuff,instr);
1957
			if (!NoSJIS) FixSJIS(fbuff);
1958
			printf("\r                                       \r");
1959
			printf("%s%s%s ",RVon,prompt,RVoff);
1960
			KOut(fbuff);
1961
			continue;
1962
		}
1963
		instr[i] = c;
1964
		if (!escf)
1965
		{
1966
			if (!bit8)
1967
			{
1968
				printf("\r                                       \r");
1969
				printf("%s%s%s %s",RVon,prompt,RVoff,instr);
1970
			}
1971
			else
1972
			{
1973
				strcpy(fbuff,instr);
1974
				if ((strlen(fbuff) % 2) > 0) fbuff[strlen(fbuff)-1] = 0;
1975
				printf("\r                                       \r");
1976
				printf("%s%s%s ",RVon,prompt,RVoff);
1977
				if (!NoSJIS) FixSJIS(fbuff);
1978
				KOut(fbuff);
1979
			}
1980
		}
1981
		if ((instr[i] == 'B')&&(instr[i-1] == '(')&&(instr[i-2] == 0x1b)) 
1982
		{
1983
			ShowIt = TRUE;
1984
			break;
1985
		}
1986
	}
1987
	fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
1988
	fflush(stdin);
1989
	GetEUC(fbuff);
1990
	if ( fbuff[strlen(fbuff)-1] < 0x20) fbuff[strlen(fbuff)-1] = 0;
1991
	if (ShowIt)
1992
	{
1993
		printf("\r                                       \r");
1994
		printf("%s%s%s ",RVon,prompt,RVoff);
1995
		KOut(fbuff);
1996
	}
1997
	printf("\n\r");
1998
}
1999
2000
/*=====  OneShot === Collect and set single filter=============*/
2001
2002
void OneShot()
2003
{
2004
	printf("\nFilter inactivated. Enter new filter, or press return\n\n");
2005
	printf("%sFILTER:%s ",RVon,RVoff);
2006
        SFFlag = FALSE;
2007
2008
	GetKBStr("FILTER:");
2009
	strcpy(SingleFilter,fbuff);
2010
	if (strlen(fbuff) >= 2) 
2011
	{
2012
		SFFlag = TRUE;
2013
		printf("Filter set to: ");
2014
		KOut(fbuff);
2015
		printf("\n");
2016
	}
2017
	else
2018
	{
2019
		printf("No filter set\n");
2020
	}
2021
}
2022
2023
/*====FindRad=== finds the spot in the table for a radical==========*/
2024
int FindRad(unsigned char char1, unsigned char char2)
2025
{
2026
	int i,j,k,Found;
2027
2028
	Found = FALSE;
2029
	for (i = 0; i <= NoRads; i++)
2030
	{
2031
		if ((char1 == RadK1[i]) && (char2 == RadK2[i]))
2032
		{
2033
			Found = TRUE;
2034
			break;
2035
		}
2036
	}
2037
	if (!Found) return(-1);
2038
	return(i);
2039
}
2040
/*===RadBuild==Extracts the kanji that meet the multi-radical selection===*/
2041
void RadBuild()
2042
{
2043
/*	RKTarg will contain the string of target "radicals"
2044
	The RKSet tables are progressively loaded with the kanji that 
2045
	meet the radical criteria		*/
2046
2047
	int stest,jtest,i,j,k,l,m,n,lind,hitcount;
2048
2049
/*	the first radical has all its kanji loaded (which meet any stroke count test)	*/
2050
	i = FindRad((unsigned char)RKTarg[0],(unsigned char)RKTarg[1]);
2051
	if (i < 0)
2052
	{
2053
		printf("Invalid Radical!\n");
2054
		RKTarg[0] = 0;
2055
		return;
2056
	}
2057
	k = 0;
2058
	hitcount = 0;
2059
	for (j = RKStart[i]; j < RKStart[i]+RKCnt[i]; j++)
2060
	{
2061
		if (kstrokelim > 0)
2062
		{
2063
			n = ((RKanj1[j] & 0x7f)-0x30)*94 + ((RKanj2[j] & 0x7f) - 0x21);
2064
			if ((kstroketype == 0) && (kstrokelim != kstrokes[n])) continue;
2065
			if ((kstroketype == 1) && (kstrokelim > kstrokes[n])) continue;
2066
			if ((kstroketype == 2) && (kstrokelim < kstrokes[n])) continue;
2067
		}
2068
		RKSet[0][k] = RKanj1[j];
2069
		RKSet[0][k+1] = RKanj2[j];
2070
		RKSet[0][k+2] = 0;
2071
		hitcount++;
2072
		k+=2;
2073
	}
2074
	ops[0] = RKTarg[0]; ops[1] = RKTarg[1]; ops[2] = 0;
2075
	printf("Target Radicals: 0 ");
2076
	KOut(ops);
2077
	printf(" (%d) ",hitcount);
2078
	NoSets = 1;
2079
	if (strlen(RKTarg) <= 2)
2080
	{
2081
		printf("\n");
2082
		return;
2083
	}
2084
/* The second and subsequent radicals only have their matching kanji loaded
2085
	if they are a member of the previous set		*/
2086
2087
	for (l=2; l< strlen(RKTarg); l+=2)
2088
	{
2089
		lind = (l/2)-1;
2090
		i = FindRad((unsigned char)RKTarg[l],(unsigned char)RKTarg[l+1]);
2091
		ops[0] = RKTarg[l]; ops[1] = RKTarg[l+1]; ops[2] = 0;
2092
		printf(" %d ",NoSets);
2093
		KOut(ops);
2094
		if (i < 0)
2095
		{
2096
			printf("\nInvalid Radical!\n");
2097
			RKTarg[l] = 0;
2098
			return;
2099
		}
2100
		k = 0;
2101
		RKSet[lind+1][k] = 0;
2102
		jtest = RKStart[i]+RKCnt[i];
2103
		for (j = RKStart[i]; j < jtest; j++)
2104
		{
2105
			for (n = 0; RKSet[lind][n] != 0; n+=2)
2106
			{
2107
				if ((RKSet[lind][n] == RKanj1[j]) && (RKSet[lind][n+1] == RKanj2[j]))
2108
				{
2109
					RKSet[lind+1][k] = RKanj1[j];
2110
					RKSet[lind+1][k+1] = RKanj2[j];
2111
					RKSet[lind+1][k+2] = 0;
2112
					k+=2;
2113
					break;
2114
				}
2115
			}
2116
		}
2117
		NoSets++;
2118
		printf(" (%d) ",strlen(RKSet[NoSets-1])/2);
2119
	}
2120
	printf("\n");
2121
}
2122
2123
/*=====RadDisp===Display Radical Data==============================*/
2124
void RadDisp()
2125
{
2126
	FILE *fk,*fopen();
2127
	int j,k,l,n;
2128
	unsigned char *ptr;
2129
2130
	fk = xfopen(RKname,"r", &xfilelen);
2131
	j = 0;
2132
	printf("RADICAL TABLE FOR USE WITH THE XJDIC RADICAL LOOKUP FUNCTION\n");
2133
	DRow = 0;
2134
	DCol = 0;
2135
	k = 99;
2136
	KLine[0] = 0;
2137
	while(!feof(fk))
2138
	{
2139
		fgets(testline,199,fk);
2140
		if(feof(fk)) break;
2141
		if (testline[0] != '$') continue;
2142
		ptr = strtok(testline+4," ");
2143
		l = atoi(ptr);
2144
		if (l != k)
2145
		{
2146
			k = l;
2147
			for (n = 0; n < strlen(ptr); n++)
2148
			{
2149
				if (ptr[n] < 32) break;
2150
				KOutc(0xa3); KOutc(ptr[n] | 0x80);
2151
			}
2152
			KOutc(' ');
2153
		}
2154
		KOutc(testline[2]); KOutc(testline[3]); KOutc(' ');
2155
	}
2156
	fclose(fk);
2157
	KFlush("");
2158
}
2159
/*===KanjRad=== Display which "radicals" are used to imdex this kanji=====*/
2160
void KanjRad()
2161
{
2162
	int i,j;
2163
2164
	printf("%sWhich Kanji:%s",RVon,RVoff);
2165
	GetKBStr("Which Kanji:");
2166
	if (fbuff[0] < 127) return;
2167
	strcpy(KLine,"Kanji: ");
2168
	KOutc(fbuff[0]);
2169
	KOutc(fbuff[1]);
2170
	strcat(KLine," Elements: ");
2171
	DRow = 0;
2172
	DCol = 0;
2173
	for (i = 0; i <= NoRads; i++)
2174
	{
2175
		for (j = RKStart[i]; j < RKStart[i]+RKCnt[i]; j++)
2176
		{
2177
			if ((fbuff[0] == RKanj1[j]) && (fbuff[1] == RKanj2[j]))
2178
			{
2179
				KOutc(RadK1[i]);
2180
				KOutc(RadK2[i]);
2181
				KOutc(' ');
2182
			}
2183
		}
2184
	}
2185
	KFlush("");
2186
}
2187
/*====RadKDisp=====display selected kanji============================*/
2188
void RadKDisp()
2189
{
2190
	int i;
2191
2192
	if (RKTarg[0] == 0) return;
2193
	if (NoSets == 0) return;
2194
	printf("Selected Kanji: ");
2195
	for (i = 0; i < strlen(RKSet[NoSets-1]); i+=2)
2196
	{
2197
		ops[0] = RKSet[NoSets-1][i]; ops[1] = RKSet[NoSets-1][i+1]; ops[2] = 0;
2198
		KOut(ops);
2199
		printf(" ");
2200
	}
2201
	printf("\n");
2202
}
2203
/*====DoLOOKUP=====driver routine for the multi-radical lookup=======*/
2204
void DoLOOKUP()
2205
{
2206
	int i;
2207
2208
	printf("\nRadical Lookup Mode\n");
2209
	RKTarg[0] = 0;
2210
	while(TRUE)
2211
	{
2212
		printf("%sLookup Code:%s",RVon,RVoff);
2213
		GetKBStr("Lookup Code:");
2214
		if (fbuff[0] > 127)
2215
		{
2216
			ops[0] = fbuff[0]; ops[1] = fbuff[1]; ops[2] = 0;
2217
			if (strlen(RKTarg) == 20)
2218
			{
2219
				printf("\nToo many radicals!\n");
2220
				continue;
2221
			}
2222
			strcat(RKTarg,ops);
2223
			RadBuild();
2224
			if(NoSets == 0) continue;
2225
			if (strlen(RKSet[NoSets-1]) == 0) continue;
2226
			if (strlen(RKSet[NoSets-1]) <=RADLOOKLIM) RadKDisp();
2227
			continue;
2228
		}
2229
		if ((fbuff[0] | 0x20) == 'r')
2230
		{
2231
			RadDisp();
2232
			continue;
2233
		}
2234
		if ((fbuff[0] | 0x20) == 'v')
2235
		{
2236
			KanjRad();
2237
			continue;
2238
		}
2239
		if ((fbuff[0] | 0x20) == 'x')
2240
		{
2241
			instr[0] = 0;
2242
			fflush(stdin);
2243
			return;
2244
		}
2245
		if ((fbuff[0] | 0x20) == 'c')
2246
		{
2247
			RKTarg[0] = 0;
2248
			kstrokelim = 0;
2249
			printf("Cleared\n");
2250
			continue;
2251
		}
2252
		if ((fbuff[0] | 0x20) == 's')
2253
		{
2254
			kstroketype = 0;
2255
			i = 1;
2256
			testline[0] = 0;
2257
			if (fbuff[1] == '+') 
2258
			{
2259
				kstroketype = 1;
2260
				strcat(testline," >= ");
2261
				i = 2;
2262
			}
2263
			if (fbuff[1] == '-') 
2264
			{
2265
				kstroketype = 2;
2266
				strcat(testline," <= ");
2267
				i = 2;
2268
			}
2269
			kstrokelim = atoi(fbuff+i);
2270
			if (kstrokelim == 0)
2271
			{
2272
				printf("Stroke-count cleared\n");
2273
			}
2274
			else
2275
			{
2276
				printf("Stroke-count set to %s%d\n",testline,kstrokelim);
2277
			}
2278
			if (strlen(RKTarg) > 0) RadBuild();
2279
			if (NoSets <= 0) continue;
2280
			if (strlen(RKSet[NoSets-1]) <=RADLOOKLIM) RadKDisp();
2281
			continue;
2282
		}
2283
		if ((fbuff[0] | 0x20) == 'd')
2284
		{
2285
			i = fbuff[1]-'0';
2286
			if (i >= strlen(RKTarg)/2)
2287
			{
2288
				printf("Out of Range!\n");
2289
				continue;
2290
			}
2291
			strcpy(RKTarg+i*2,RKTarg+i*2+2);
2292
			if(strlen(RKTarg) > 0) RadBuild();
2293
			if (strlen(RKSet[NoSets-1]) <=RADLOOKLIM) RadKDisp();
2294
			continue;
2295
		}
2296
2297
		if ((fbuff[0] | 0x20) == 'l') RadKDisp();
2298
	}
2299
}
2300
2301
/*=====  DoKANJI === Kanji single lookup ======================*/
2302
void DoKANJI()
2303
{
2304
2305
	GetKBStr("KANJI/KANA:");
2306
	Dmode =1;
2307
	KLookup();
2308
	instr[0] = 0;
2309
}
2310
/*===EMtoggle==alternate between match display  modes===============*/
2311
void EMtoggle()
2312
{
2313
	EMmode = (EMmode+1) % 2;
2314
	printf("Exact Match Display Mode: %s\n",kmodes[EMmode]);
2315
}
2316
/*===togglekana==alternate between kana input modes===============*/
2317
void togglekana()
2318
{
2319
	KImode = (KImode+1) % 2;
2320
	printf("Kana Default Input Mode: %s\n",kmodes[KImode]);
2321
}
2322
/*===togglekanji==alternate between kanji display  modes===============*/
2323
void togglekanji()
2324
{
2325
	KLmode = (KLmode+1) % 2;
2326
	printf("Long Kanji Display Mode: %s\n",kmodes[KLmode]);
2327
}
2328
/*===toggleraw===alternate between raw/edited output modes===============*/
2329
void toggleraw()
2330
{
2331
	ROmode = (ROmode+1) % 2;
2332
	printf("Unedited Output Mode: %s\n",kmodes[ROmode]);
2333
}
2334
/*===RVtoggle===alternate between reverse video modes===============*/
2335
void RVtoggle()
2336
{
2337
	RVACTIVE  = (RVACTIVE +1) % 2;
2338
	printf("Reverse Video Match Display Mode: %s\n",kmodes_r[RVACTIVE ]);
2339
}
2340
/*===togglemode===alternate between kanji compound modes===============*/
2341
void togglemode()
2342
{
2343
	FirstKanj = (FirstKanj+1) % 2;
2344
	printf("Display All Kanji Mode: %s\n",kmodes[FirstKanj]);
2345
}
2346
2347
/*=====engpritoggle=====alternate between English priorities=======*/
2348
void engpritoggle()
2349
{
2350
	if(prieng == FALSE)
2351
	{
2352
		prieng = TRUE;
2353
		printf("English search will now only select priority keys\n");
2354
		return;
2355
	}
2356
	prieng = FALSE;
2357
	printf("English search will now select all keys\n");
2358
}
2359
2360
/*=====seldic=====select dictionary file =====================*/
2361
void seldic()
2362
{
2363
	int i;
2364
	char c;
2365
2366
	if (NoDics == 1)
2367
	{
2368
		printf("No alternative dictionary active!\n");
2369
		return;
2370
	}
2371
	for (i = 1; i <= NoDics; i++)
2372
	{
2373
		printf("Dictionary: %d  [%s]\n",i,DicName(i));
2374
	}
2375
	printf ("Select a dictionary file (1-%d)\n",NoDics);
2376
	c = getcharxx();
2377
	if ((c ==  0x1b)||(c == 0xa)||(c == 0xd)) return;
2378
	i = c-'0';
2379
	if ((i <0)||(i >NoDics))
2380
	{
2381
		printf("INVALID!\n");
2382
		return;
2383
	}
2384
	CurrDic = i;
2385
	printf("Active Dictionary is now #%d (%s)\n",CurrDic,DicName(CurrDic));
2386
}
2387
2388
/*=====BuffStats=====report on page buffer stats=====================*/
2389
void BuffStats()
2390
{
2391
	int i;
2392
2393
#ifdef XJDDIC
2394
	printf("DEMAND-PAGING STATISTICS:\n\n");
2395
	printf("Dic Hits: ");
2396
	for (i=0;i<=NoDics;i++) printf("\t%ld ",dichits[i]);
2397
	printf("\n");
2398
	printf("Dic Miss: ");
2399
	for (i=0;i<=NoDics;i++) printf("\t%ld ",dicmiss[i]);
2400
	printf("\n");
2401
	printf("Ind Hits: ");
2402
	for (i=0;i<=NoDics;i++) printf("\t%ld ",indhits[i]);
2403
	printf("\n");
2404
	printf("Ind Miss: ");
2405
	for (i=0;i<=NoDics;i++) printf("\t%ld ",indmiss[i]);
2406
	printf("\n");
2407
	printf("Buffer overwrites: \t%ld\n",vbkills);
2408
#else
2409
	printf("CLIENT! NO BUFFER STATISTICS\n");
2410
#endif
2411
}
2412
/*=====altdic=====rotate around dictionaries=======*/
2413
void altdic(int dicincr)
2414
{
2415
	if (NoDics == 1)
2416
	{
2417
		printf("No alternative dictionary active!\n");
2418
		return;
2419
	}
2420
	CurrDic = CurrDic + dicincr;
2421
	if (CurrDic == NoDics+1) CurrDic = 1;
2422
	if (CurrDic == 0) CurrDic = NoDics;
2423
	printf("Switching to dictionary: %d [%s]\n",CurrDic,DicName(CurrDic));
2424
}
2425
/*====GDicSet====== initialize the global dictionary list===============*/
2426
void  GDicSet()
2427
{
2428
	char gdtemp[100],*gdp;
2429
2430
	if (NoDics == 1)
2431
	{
2432
		printf("No alternative dictionary active!\n");
2433
		return;
2434
	}
2435
2436
	gdicmax = 0;
2437
	printf("\nEnter the list of dictionary numbers for the global search\n");
2438
	printf("%sDictionary Numbers:%s",RVon,RVoff);
2439
	GetKBStr("Dictionary Numbers:");
2440
	strcpy(gdtemp,fbuff);
2441
	gdp = strtok(gdtemp," ,\t");
2442
	while(gdp!=NULL)
2443
	{
2444
		gdicnos[gdicmax] = atoi(gdp);
2445
		gdp = strtok(NULL," ,\t");
2446
		gdicmax++;
2447
		if (gdicmax > 9)
2448
		{
2449
			printf("Too many dictionary numbers!\n");
2450
			gdicmax--;
2451
			break;
2452
		}
2453
		if (gdicnos[gdicmax-1] > NoDics)
2454
		{
2455
			printf("Illegal Dictionary number!\n");
2456
			gdicmax--;
2457
		}
2458
	}
2459
	gdicmax--;
2460
	if (gdicmax <1)
2461
	{
2462
		printf("Warning! Insufficient dictionaries set!\n");
2463
	}
2464
}
2465
2466
/*====GDictoggle=== alternate between single & global dics==============*/
2467
void GDictoggle()
2468
{
2469
	int gi;
2470
2471
	if (GDmode)
2472
	{
2473
		GDmode = FALSE;
2474
		printf("Global dictionary searching is now OFF\n");
2475
	}
2476
	else
2477
	{
2478
		if (gdicmax <1)
2479
		{
2480
			printf("Insufficient dictionaries set!\n");
2481
			return;
2482
		}
2483
		GDmode = TRUE; 
2484
		printf("Global dictionary searching is now ON [");
2485
		for (gi=0;gi<=gdicmax;gi++)
2486
		{
2487
			printf("%d",gdicnos[gi]);
2488
			if (gi != gdicmax) printf(" ");
2489
		}
2490
		printf("]\n");
2491
	}
2492
}
2493
/*====GDicALLtoggle=== alternate between single & multiple global dic  disp====*/
2494
void GDicALLtoggle()
2495
{
2496
	if(GDALLmode)
2497
	{
2498
		GDALLmode = FALSE;
2499
		printf("Multiple Global Display is now OFF\n");
2500
	}
2501
	else
2502
	{
2503
		GDALLmode = TRUE;
2504
		printf("Multiple Global Display is now ON\n");
2505
	}
2506
}
2507
/*====Verbtoggle=== alternate between deinflection on/off===============*/
2508
void Verbtoggle()
2509
{
2510
	if (Jverb)
2511
	{
2512
		Jverb = FALSE;
2513
		printf("Verb deinflection is now OFF\n");
2514
	}
2515
	else
2516
	{
2517
		Jverb = TRUE; 
2518
		printf("Verb deinflection is now ON\n");
2519
	}
2520
}
2521
/*=== FiltSet=== turn filters on & off==============================*/
2522
void FiltSet()
2523
{
2524
	unsigned char c,fff[10];
2525
	int anyfilt,j,k;
2526
2527
	anyfilt = FALSE;
2528
	printf("%sFilter Setting%s\n",RVon,RVoff);
2529
	for(j = 0;j < NOFILT;j++)
2530
	{
2531
		if(!filtact[j])continue;
2532
		anyfilt = TRUE;
2533
		strcpy(fff,"OFF");
2534
		if(filton[j])strcpy(fff,"ON ");
2535
		printf("No: %d Type: %d Status: %s Name: %s\n",j,filttype[j],fff,filtnames[j]);
2536
	}
2537
	if (!anyfilt)
2538
	{
2539
		printf("No filters are loaded!\n");
2540
		return;
2541
	}
2542
	printf("Modify which Filter?\n");
2543
	c = getcharxx();
2544
	if ((c ==  0x1b)||(c == 0xa)||(c == 0xd)) return;
2545
	k = c-'0';
2546
	if ((k <0)||(k >NOFILT))
2547
	{
2548
		printf("INVALID!\n");
2549
		return;
2550
	}
2551
	if (!filtact[k])
2552
	{
2553
		printf("Filter %c is not active!\n",c);
2554
		return;
2555
	}
2556
	printf("Filter %c OFF (0) or ON (1)\n",c);
2557
	c = getcharxx();
2558
	if ((c != '0') && (c != '1'))return;
2559
	if(c == '0') 
2560
	{
2561
		filton[k] = FALSE;
2562
		printf("Filter %d Turned OFF\n",k);
2563
	}
2564
	if(c == '1')
2565
	{
2566
		filton[k] = TRUE;
2567
		printf("Filter %d Turned ON\n",k);
2568
	}
2569
	nofilts = FALSE;
2570
	for(j = 0;j < NOFILT;j++)if(filton[j])nofilts=TRUE;
2571
	return;
2572
}
2573
2574
/*                  M A I N                                      */
2575
2576
main(argc,argv)
2577
int argc;
2578
unsigned char **argv;
2579
2580
{
2581
	int i,j,ip,cmdmode,bit8,escf;
2582
  	unsigned char *dicenv,strtmp[50];
2583
  	unsigned char c;
2584
	unsigned char xap[50];
2585
	unsigned char kbprompt[99];
2586
	unsigned char kbprompt2[99];
2587
	
2588
  	printf("XJDIC Version %s (Japanese Dictionary) Copyright J.W.Breen 2003.\n",sver);
2589
#ifdef XJDDIC
2590
	printf("   Stand-alone mode\n");
2591
#else
2592
	printf("   Client mode\n");
2593
#endif
2594
2595
	for (i=0;i<NOFILT;i++)
2596
	{
2597
		filtact[i] = FALSE;
2598
		filton[i] = FALSE;
2599
	}
2600
	dicenv = (unsigned char *)getenv("XJDIC");
2601
	if (!dicenv) dicenv = (unsigned char *)DEFAULT_DICDIR;
2602
	if (strlen(dicenv) <= 2) 
2603
	{
2604
		dicenv = (unsigned char *)getcwd(ENVname,sizeof(ENVname));
2605
		if (dicenv == NULL) 
2606
		{
2607
			printf("Cannot extract working directory!\n");
2608
			exit(1);
2609
		}
2610
	}
2611
	else
2612
	{
2613
		strcpy (ENVname,dicenv);
2614
        }
2615
#ifdef XJDDIC
2616
        strcpy (Dnamet[1],"edict");
2617
        strcpy (Dnamet[0], "kanjidic");
2618
        strcpy (XJDXnamet[1], "edict.xjdx");
2619
        strcpy (XJDXnamet[0], "kanjidic.xjdx");
2620
	NoDics = 1;
2621
	CurrDic = 1;
2622
#endif
2623
	cl_rcfile[0] = 0;
2624
/* process command-line options*/
2625
	if (argc > 1)
2626
	{
2627
		for (i = 1; i < argc; i++)
2628
		{
2629
			strcpy(xap,argv[i]);
2630
			if ((xap[0] == '-') && (xap[1] == 'c'))
2631
			{
2632
				if(xap[2] != 0)
2633
				{
2634
					strcpy(strtmp,xap+2);
2635
				}
2636
				else
2637
				{
2638
					i++;
2639
					strcpy(xap,argv[i]);
2640
					strcpy (strtmp,xap);
2641
				}
2642
				strcpy (cl_rcfile,strtmp);
2643
				printf ("Using control-file: %s\n",cl_rcfile);
2644
			}
2645
			if ((xap[0] == '-') && (xap[1] == 'j'))
2646
			{
2647
				if(xap[2] != 0)
2648
				{
2649
					strcpy(strtmp,xap+2);
2650
				}
2651
				else
2652
				{
2653
					i++;
2654
					strcpy(xap,argv[i]);
2655
					strcpy (strtmp,xap);
2656
				}
2657
				if (strtmp[0] == 'j')
2658
				{
2659
					Omode = 0;
2660
					printf("Output mode set to JIS\n");
2661
				}
2662
				if (strtmp[0] == 's')
2663
				{
2664
					Omode = 2;
2665
					printf("Output mode set to Shift-JIS\n");
2666
				}
2667
				if (strtmp[0] == 'e')
2668
				{
2669
					Omode = 1;
2670
					printf("Output mode set to EUC\n");
2671
				}
2672
				continue;
2673
			}
2674
#ifdef XJDCLSERV
2675
			if ((xap[0] == '-') && (xap[1] == 'S'))
2676
			{
2677
				if(xap[2] != 0)
2678
				{
2679
					strcpy(host,xap+2);
2680
				}
2681
				else
2682
				{
2683
					i++;
2684
					strcpy(xap,argv[i]);
2685
					strcpy(host,xap);
2686
				}
2687
				printf("Command-line request to use server: %s\n",host);
2688
				continue;
2689
			}
2690
			if ((xap[0] == '-') && (xap[1] == 'P'))
2691
			{
2692
				if(xap[2] != 0)
2693
				{
2694
					portno = atoi(xap+2);
2695
				}
2696
				else
2697
				{
2698
					i++;
2699
					strcpy(xap,argv[i]);
2700
					portno = atoi(xap);
2701
				}
2702
				printf("Command-line request to use port: %d\n",portno);
2703
				continue;
2704
			}
2705
#endif
2706
#ifdef XJDDIC
2707
			if ((xap[0] == '-') && (xap[1] == 'C'))
2708
			{
2709
				if(xap[2] != 0)
2710
				{
2711
					strcpy(strtmp,xap+2);
2712
				}
2713
				else
2714
				{
2715
					i++;
2716
					strcpy(xap,argv[i]);
2717
					strcpy (strtmp,xap);
2718
				}
2719
				strcpy (Clip_File,strtmp);
2720
				printf ("Using clipboard-file: %s\n",Clip_File);
2721
			}
2722
			if ((xap[0] == '-') && (xap[1] == 'd'))
2723
			{
2724
				if (thisdic == 0)
2725
				{
2726
					thisdic = 1;
2727
				}
2728
				else
2729
				{
2730
					thisdic++;
2731
					NoDics++;
2732
				}
2733
				if(xap[2] != 0)
2734
				{
2735
					strcpy(strtmp,xap+2);
2736
				}
2737
				else
2738
				{
2739
					i++;
2740
					strcpy(xap,argv[i]);
2741
					strcpy (strtmp,xap);
2742
				}
2743
				strcpy (Dnamet[thisdic],strtmp);
2744
				strcpy (XJDXnamet[thisdic],strtmp);
2745
				strcat (XJDXnamet[thisdic],".xjdx");
2746
				printf("Command-line request to use dictionary files (%d): %s and %s\n",
2747
					thisdic,Dnamet[thisdic],XJDXnamet[thisdic]);
2748
				continue;
2749
			}
2750
			if ((xap[0] == '-') && (xap[1] == 'k'))
2751
			{
2752
				if(xap[2] != 0)
2753
				{
2754
					strcpy(strtmp,xap+2);
2755
				}
2756
				else
2757
				{
2758
					i++;
2759
					strcpy(xap,argv[i]);
2760
					strcpy (strtmp,xap);
2761
				}
2762
				strcpy (Dnamet[0],strtmp);
2763
				strcpy (XJDXnamet[0],strtmp);
2764
				strcat (XJDXnamet[0],".xjdx");
2765
				printf("Command-line request to use kanji dictionary files: %s and %s\n",Dnamet[0],XJDXnamet[0]);
2766
				continue;
2767
			}
2768
#endif
2769
			if ((xap[0] == '-') && (xap[1] == 'V'))
2770
			{
2771
				RVACTIVE = FALSE;
2772
				printf("Reverse-video match display disabled\n");
2773
			}
2774
			if ((xap[0] == '-') && (xap[1] == 'E'))
2775
			{
2776
				NoSJIS = TRUE;
2777
				printf("EUC (No Shift-JIS) operation enforced\n");
2778
			}
2779
			if ((xap[0] == '-') && (xap[1] == 'v'))
2780
			{
2781
				Jverb = FALSE;
2782
				printf("Verb deinflection turned OFF\n");
2783
			}
2784
			if ((xap[0] == '-') && (xap[1] == 'h'))
2785
			{
2786
printf("\nUSAGE: xjdic <options>\n\nwhere options are:\n\n");
2787
printf("   -h this information\n");
2788
printf("   -c control-file\n");
2789
#ifdef XJDCLSERV
2790
printf("   -P port-no\n");
2791
printf("   -S server\n");
2792
#else
2793
printf("   -d dictionary file_path\n");
2794
printf("   -k kanji-dictionary file_path\n");
2795
#endif
2796
printf("   -v disable verb function\n");
2797
printf("   -j x (where x is Output code: e = EUC, s = Shift-JIS, j  = JIS)\n");
2798
printf("   -V disable reverse video display\n");
2799
printf("   -C clipboard-file\n");
2800
printf("\nSee xjdic24.inf for full information\n");
2801
exit(0);
2802
			}
2803
		}
2804
	}
2805
	xjdicrc();
2806
#ifdef XJDDIC
2807
  	printf ("Loading Dictionary and Index files.  Please wait...\n");
2808
#endif
2809
	Verbinit();
2810
  	DicSet ();
2811
	RadSet();
2812
	RadLoad();
2813
	KSLoad();
2814
	LoadKana();
2815
	togglemode();
2816
	togglekanji();
2817
	cbreakon();
2818
	printf("\n(Enter ? for a summary of operating instructions)\n");
2819
2820
/*
2821
	From here on, the code loops endlessly, reading commands 
2822
	from the keyboard. This code decodes any kana/kanji/romaji
2823
	itself, rather than using the "GetKBStr" function, as it has
2824
	to sort out special commands as well.
2825
*/
2826
  	while (TRUE)
2827
	{
2828
		GetWinSize(); /* Just in case the screen has changed  */
2829
		sprintf(kbprompt,"%sXJDIC [%d:%s] SEARCH KEY:%s ",RVon,CurrDic,DicName(CurrDic),RVoff);
2830
		sprintf(kbprompt2,"XJDIC [%d:%s] SEARCH KEY: ",CurrDic,DicName(CurrDic));
2831
		if (GDmode)
2832
		{
2833
			sprintf(kbprompt,"%sXJDIC [GLOBAL] SEARCH KEY:%s ",RVon,RVoff);
2834
			sprintf(kbprompt2,"XJDIC [GLOBAL] SEARCH KEY: ");
2835
		}
2836
		printf("\n\r%s",kbprompt);
2837
		c = 0;
2838
		cmdmode = FALSE;
2839
		strf = FALSE;
2840
		escf = FALSE;
2841
		bit8 = FALSE;
2842
		AKanaMode = FALSE;
2843
		if (KImode == 0) AKanaMode = TRUE;
2844
		for (i = 0; (c != 0xd) && (c != 0xa); i++)
2845
		{
2846
			instr[i+1] = 0;
2847
			if (clipmode) break;
2848
			c = getcharxx();
2849
			if (c == 0x1b) 
2850
			{
2851
				escf = TRUE;
2852
				AKanaMode = FALSE;
2853
			}
2854
			if (c > 0x7f) 
2855
			{
2856
				bit8 = TRUE;
2857
				AKanaMode = FALSE;
2858
			}
2859
			if (!bit8 && !escf && (c == 0x04))   /* Simulate ^D */
2860
			{
2861
				cbreakoff();
2862
				exit(0);
2863
			}
2864
			if (!bit8 && !escf && (c == 0x1a))   /* Simulate ^Z */
2865
			{
2866
				cbreakoff();
2867
				printf("\nSuspending XJDIC. Type `fg' to resume.\n");
2868
				pid = getpid();
2869
				kill(pid,sig);
2870
				cbreakon();
2871
				break;
2872
			}
2873
			/*   Romaji Input	*/
2874
			if (!bit8 && !escf && ((c == '@') || (c == '#')))
2875
			{
2876
				DoRomaji(c);
2877
				break;
2878
			}
2879
			/* On-line Help Summmary		*/
2880
			if ((c == '?') && (!escf) && (!bit8))
2881
			{
2882
				DRow = 0;
2883
				for (ip = 0; strcmp(Help[ip],"$$$")!=0;ip++) 
2884
				{
2885
					strcpy(KLine,Help[ip]);
2886
					if(!KFlush("Continue Help Display? (y/n)")) break;
2887
				}
2888
				break;
2889
			}
2890
			/* Turn on cmd mode for special characters */
2891
			if ((!escf) && (!bit8) && (strchr("!{}$%*&^=/-:\'+\\;][|_`",c) != NULL))
2892
			{
2893
				cmdmode = TRUE; 
2894
				break;
2895
			}
2896
			/* backspace or rubout - shrink i/p buffer and redisplay	*/
2897
			if ((c == 0x7f) || (c == 8))
2898
			{
2899
				if(bit8) i--;
2900
				if( i > 0) instr[--i] = 0;
2901
				i--;
2902
				strcpy(fbuff,instr);
2903
				if (!NoSJIS) FixSJIS(fbuff);
2904
				printf("\r                                       \r");
2905
				printf("%s",kbprompt);
2906
				KOut(fbuff);
2907
				continue;
2908
			}
2909
			/* Actually an input character	*/
2910
			instr[i] = c;
2911
			if (AKanaMode)
2912
			{
2913
				if ((c == 'l') || (c == 'L'))
2914
				{
2915
					AKanaMode = FALSE;
2916
					GetKBStr(kbprompt2);
2917
					break;
2918
				}
2919
				if (c < 128)
2920
				{
2921
					ungetc(c,stdin);
2922
					DoRomaji('@');
2923
					break;
2924
				}
2925
			}
2926
			if (!escf)
2927
			{
2928
				if (!bit8)
2929
				{
2930
					printf("\r					 \r");
2931
					printf("%s%s",kbprompt,instr);
2932
				}
2933
				else
2934
				{
2935
					strcpy(fbuff,instr);
2936
					if ((strlen(fbuff) % 2) > 0) fbuff[strlen(fbuff)-1] = 0;
2937
					printf("\r					 \r");
2938
					printf("%s",kbprompt);
2939
					if (!NoSJIS) FixSJIS(fbuff);
2940
					KOut(fbuff);
2941
				}
2942
			}
2943
			/* JIS ShiftOUT, so terminate input		*/
2944
			if ((instr[i] == 'B')&&(instr[i-1] == '(')&&(instr[i-2] == 0x1b)) break;
2945
		}
2946
		fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
2947
		/* "bye" is the end of the run			*/
2948
		if ((instr[2] == 'e')&&(instr[1] == 'y')&&(instr[0] == 'b')) 
2949
		{
2950
			cbreakoff();
2951
			exit(0);
2952
		}
2953
		/* sort out the special commands		*/
2954
		if(cmdmode)
2955
		{
2956
			if (c == '{')   /* } to balance {}  */
2957
			{
2958
				printf("\r                                    \r");
2959
				printf("Now in Clipboard Mode\n");
2960
				clipmode = TRUE;
2961
				continue;
2962
			}
2963
			if (c == '}')   /* matching {  */
2964
			{
2965
				printf("\r                                      \r");
2966
				RVtoggle();
2967
				continue;
2968
			}
2969
			if (c == '/')
2970
			{
2971
				printf("\r                                      \r");
2972
				togglemode();
2973
				continue;
2974
			}
2975
			if (c == '!')
2976
			{
2977
				printf("\r                                      \r");
2978
				DispLic();
2979
				continue;
2980
			}
2981
			if (c == '|')
2982
			{
2983
				printf("\r                                      \r");
2984
				toggleraw();
2985
				continue;
2986
			}
2987
			if (c == '+')
2988
			{
2989
				printf("\r                                      \r");
2990
				engpritoggle();
2991
				continue;
2992
			}
2993
			if (c == '&')
2994
			{
2995
				printf("\r                                      \r");
2996
				togglekana();
2997
				continue;
2998
			}
2999
			if (c == '-')
3000
			{
3001
				printf("\r                                      \r");
3002
				togglekanji();
3003
				continue;
3004
			}
3005
			if (c == ']')
3006
			{
3007
				printf("\r                                      \r");
3008
				ExtFileDisp();
3009
				continue;
3010
			}
3011
			if (c == '=')
3012
			{
3013
				printf("\r                                      \r");
3014
				altdic(+1);
3015
				continue;
3016
			}
3017
			if (c == '^')
3018
			{
3019
				printf("\r                                      \r");
3020
				altdic(-1);
3021
				continue;
3022
			}
3023
			if (c == '_')
3024
			{
3025
				printf("\r                                      \r");
3026
				seldic();
3027
				continue;
3028
			}
3029
			if (c == '\'')
3030
			{
3031
				printf("\r                                      \r");
3032
				OneShot();
3033
				continue;
3034
			}
3035
			if (c == ';')
3036
			{
3037
				printf("\r                                      \r");
3038
				FiltSet();
3039
				continue;
3040
			}
3041
			if (c == ':')
3042
			{
3043
				printf("\r                                      \r");
3044
				Verbtoggle();
3045
				continue;
3046
			}
3047
			if (c == '[')
3048
			{
3049
				printf("\r                                      \r");
3050
				EMtoggle();
3051
				continue;
3052
			}
3053
			if (c == '$')
3054
			{
3055
				printf("\r                                      \r");
3056
				GDicSet();
3057
				continue;
3058
			}
3059
			if (c == '`')
3060
			{
3061
				printf("\r                                      \r");
3062
				GDicALLtoggle();
3063
				continue;
3064
			}
3065
			if (c == '%')
3066
			{
3067
				printf("\r                                      \r");
3068
				GDictoggle();
3069
				continue;
3070
			}
3071
			if (c == '*')
3072
			{
3073
#ifdef DEMAND_PAGING
3074
				printf("\r                                      \r");
3075
				BuffStats();
3076
#else
3077
				printf("\nNo statistics in this mode!\n");
3078
#endif
3079
				continue;
3080
			}
3081
			/* none of the above, so it must be a Kanji search	*/
3082
			printf("\r                                      \r");
3083
			printf("%sKANJI LOOKUP TYPE:%s ",RVon,RVoff);
3084
			c = getcharxx();
3085
3086
			switch (c)
3087
			{
3088
				case 'c' :	/* a KANJIDIC Index Code	*/
3089
				case 'C' :
3090
					
3091
					printf("\r                                      \r");
3092
					printf("%sINDEX CODE:%s",RVon,RVoff);
3093
					GetKBStr("INDEX CODE:");
3094
					fflush(stdin);
3095
					Dmode =1;
3096
					strcat(fbuff," ");
3097
			/* For bushu or classical radical, ask for stroke count	*/
3098
					if (((fbuff[0] | 0x20) == 'b')||((fbuff[0] | 0x20) == 'c'))
3099
					{
3100
						if (fbuff[1] >= 0xb0)  /* test for "bKANJI"  */
3101
						{
3102
							for (i=0;i<250;i++)
3103
							{
3104
								if((fbuff[1] != radkanj[i][0]) || (fbuff[2] != radkanj[i][1])) continue;
3105
								sprintf(strtmp,"%d",radnos[i]);
3106
								strcpy(fbuff+1,strtmp);
3107
								strcat(fbuff," ");
3108
								printf("Bushu: %s\n",strtmp);
3109
								break;
3110
							}
3111
							if (i == 250)
3112
							{
3113
								printf("Invalid Bushu. Assuming B1 \n");
3114
								strcpy(fbuff,"B1 ");
3115
							}
3116
						}
3117
						printf("\r                                      \r");
3118
						printf("%sSTROKE COUNT (0 selects all):%s",RVon,RVoff);
3119
						cbreakoff();
3120
						fgets(instr,3,stdin);
3121
						i = atoi(instr);
3122
						sprintf(strfilt,"S%d",i);
3123
						cbreakon();
3124
						fflush(stdin);
3125
						fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
3126
						strf = TRUE;
3127
						if (atoi(strfilt+1) == 0) strf = FALSE;
3128
					}
3129
					KLookup();
3130
					instr[0] = 0;
3131
					break;
3132
				case 'm' :	/* "meaning"	*/
3133
				case 'M' :
3134
					printf("\r                                      \r");
3135
					printf("%sMEANING:%s",RVon,RVoff);
3136
					cbreakoff();
3137
					scanf("%s",instr);
3138
					cbreakon();
3139
					fflush(stdin);
3140
					Dmode =1;
3141
					strcpy(fbuff,instr);
3142
					fseek(stdin,0L,SEEK_END); /*kill any leftovers*/
3143
					KLookup();
3144
					instr[0] = 0;
3145
					break;
3146
				case 'j' :	/* JIS code	*/
3147
				case 'J' :
3148
					printf("\r                                      \r");
3149
					printf("%sJIS CODE:%s",RVon,RVoff);
3150
					DoJIS();
3151
					break;
3152
				case 'r' :
3153
				case 'R' :
3154
					DoRADICALS();
3155
					break;
3156
				case 'l' :
3157
				case 'L' :
3158
					DoLOOKUP();
3159
					break;
3160
				case 'k' :
3161
				case 'K' :
3162
					DoKANJI();
3163
					break;
3164
				default :
3165
					ungetc(c,stdin);
3166
					DoKANJI();
3167
					break;
3168
			}
3169
		}
3170
 		if (clipmode)
3171
 		{
3172
 			while(TRUE)
3173
 			{
3174
 				sleep(2);
3175
 				fclip = fopen(Clip_File,"r");
3176
 				if (fclip == NULL)
3177
 				{
3178
 					printf("\nNo Clipboard file! (returning to normal mode.)\n");
3179
 					strcpy(clipstring1,"XXXX");
3180
 					strcpy(clipstring2,"XXXX");
3181
 					clipmode = FALSE;
3182
 					break;
3183
 				}
3184
 				fgets(clipstring1,50,fclip);
3185
 				fclose(fclip);
3186
 				if (clipstring1[strlen(clipstring1)-1] < 32) clipstring1[strlen(clipstring1)-1] = 0;
3187
 				if (strcmp(clipstring1,"quit") == 0)
3188
 				{
3189
 					clipmode = FALSE;
3190
 					printf("\nLeaving Clipboard mode\n");
3191
 					break;
3192
 				}
3193
 				if (strcmp(clipstring1,clipstring2) == 0)
3194
 				{
3195
 					continue;
3196
 				}
3197
 				else
3198
 				{
3199
 					strcpy(clipstring2,clipstring1);
3200
 					strcpy(instr,clipstring1);
3201
 					break;
3202
 				}
3203
			}
3204
		}
3205
		if(strlen(instr) < 2) continue;
3206
		GetEUC(fbuff);
3207
		if (escf) KOut(fbuff);
3208
		sprintf(tempout,"\nSearching for: %s%s%s\n",RVon,fbuff,RVoff);
3209
		KOut(tempout);
3210
		Dmode = 0;
3211
		if (prieng && (fbuff[0] < 128)) /* if priority English key only */
3212
		{
3213
			j = strlen(fbuff);
3214
			fbuff[j+1] = 0;
3215
			for (i=0 ; i < j ; i++) fbuff[i+1] = fbuff[i];
3216
			fbuff[0] = SPTAG;
3217
		}
3218
		Lookup();
3219
	}
3220
}
(-)xjdic.orig/xjdic.h (-1 / +1 lines)
Lines 3-9 Link Here
3
#define NRKANA 250*2   /*  size of the romaji to kana tables*/
3
#define NRKANA 250*2   /*  size of the romaji to kana tables*/
4
#define NOFILT 10      /* maximum number of general filters */
4
#define NOFILT 10      /* maximum number of general filters */
5
#define VMAX 600       /* maximum entries in the verb table */
5
#define VMAX 600       /* maximum entries in the verb table */
6
#define DEFAULT_DICDIR ""    /* could be: "/usr/local/lib/xjdic"   */
6
#define DEFAULT_DICDIR "/usr/share/xjdic"    /* could be: "/usr/local/lib/xjdic"   */
7
#define NOHITS 1000       /* size of hittab  */
7
#define NOHITS 1000       /* size of hittab  */
8
#define SPTAG '@'         /* character that flags priority entries */
8
#define SPTAG '@'         /* character that flags priority entries */
9
#define KFBUFFSIZE 2000   /* size of the KFlush buffer */
9
#define KFBUFFSIZE 2000   /* size of the KFlush buffer */
(-)xjdic.orig/xjdserver.c (-3 / +3 lines)
Lines 28-33 Link Here
28
#include <netdb.h>
28
#include <netdb.h>
29
#include <stdlib.h>
29
#include <stdlib.h>
30
#include <signal.h>
30
#include <signal.h>
31
#include <errno.h>
32
#include <unistd.h>
31
#include "xjdic.h"
33
#include "xjdic.h"
32
34
33
#define SVERBOSE 0
35
#define SVERBOSE 0
Lines 56-63 Link Here
56
int thisdic = 0;
58
int thisdic = 0;
57
int DicNum;
59
int DicNum;
58
60
59
extern int errno;
60
61
extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
61
extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
62
extern unsigned char *dicbufft[10];
62
extern unsigned char *dicbufft[10];
63
extern unsigned long diclent[10], indlent[10],indptrt[10];
63
extern unsigned long diclent[10], indlent[10],indptrt[10];
Lines 123-129 Link Here
123
	for (i = 0; i < strlen(pdu_out.xjdrsp_resstr); i++)
123
	for (i = 0; i < strlen(pdu_out.xjdrsp_resstr); i++)
124
		temp+= pdu_out.xjdrsp_resstr[i];
124
		temp+= pdu_out.xjdrsp_resstr[i];
125
	pdu_out.xjdrsp_checksum = htonl(temp);
125
	pdu_out.xjdrsp_checksum = htonl(temp);
126
	if (SVERBOSE) printf("PDU sent: %d %d %d %d %d %d %s %d\n",
126
	if (SVERBOSE) printf("PDU sent: %d %d %d %d %d %d %s %ld\n",
127
			ntohs(pdu_out.xjdrsp_type),
127
			ntohs(pdu_out.xjdrsp_type),
128
			ntohs(pdu_out.xjdrsp_seq),
128
			ntohs(pdu_out.xjdrsp_seq),
129
			ntohl(pdu_out.xjdrsp_resindex),
129
			ntohl(pdu_out.xjdrsp_resindex),
(-)xjdic.orig/xjdserver.c~ (+476 lines)
Line 0 Link Here
1
/**************************************************************************
2
*                 X  J  D  S E R V E R                                    *
3
*              Dictionary Server Program                                  *
4
*         Japanese-English Dictionary program (X11 version)               *
5
*                                                   Author: Jim Breen     *
6
***************************************************************************/
7
/*  This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 1, or (at your option)
10
    any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
20
21
#include <stdio.h>
22
#include <ctype.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <sys/stat.h>
26
#include <sys/socket.h>
27
#include <netinet/in.h>
28
#include <netdb.h>
29
#include <stdlib.h>
30
#include <signal.h>
31
#include <errno.h>
32
#include "xjdic.h"
33
34
#define SVERBOSE 0
35
36
void xjdicrc();
37
void DicSet ();
38
unsigned char *DicName(int dn);
39
40
int portno=XJ_PORTNO;
41
int DontFork = FALSE;
42
int alen;
43
char host[51];
44
45
REQ_PDU pdu_in;
46
RSP_PDU pdu_out;
47
48
struct sockaddr_in fsin;
49
int sock;
50
51
long lo, hi, itok, lo2, hi2, schix, schiy;
52
int res, i;
53
long it;
54
unsigned char ENVname[50];
55
unsigned char cl_rcfile[100];
56
int jiver = 14; 
57
int thisdic = 0;
58
int DicNum;
59
60
extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
61
extern unsigned char *dicbufft[10];
62
extern unsigned long diclent[10], indlent[10],indptrt[10];
63
extern int NoDics,CurrDic;
64
65
char DicDir[100];
66
67
int passiveUDP(int portno)
68
{
69
	struct protoent *ppe;
70
	struct sockaddr_in sin;
71
	int s;
72
73
	bzero((char *)&sin, sizeof(sin));
74
	sin.sin_family = AF_INET;
75
	sin.sin_addr.s_addr = INADDR_ANY;
76
	sin.sin_port = htons(portno);
77
	if ((ppe = getprotobyname("udp")) == 0)
78
	{
79
		printf ("Cannot set up UDP!!\n");
80
		exit(1);
81
	}
82
	s = socket(PF_INET, SOCK_DGRAM, ppe->p_proto);
83
	if (s < 0)
84
	{
85
		printf ("Cannot create socket!!: %s\n",strerror(errno));
86
		exit(1);
87
	}
88
	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
89
	{
90
		printf ("Cannot bind to port: %d\n",portno);
91
		exit(1);
92
	}
93
94
	return (s);
95
}
96
97
int checksum_in()
98
{
99
	long temp;
100
101
	temp = ntohs(pdu_in.xjdreq_type);
102
	temp+= ntohs(pdu_in.xjdreq_seq);
103
	temp+= ntohs(pdu_in.xjdreq_dicno);
104
	temp+= ntohl(pdu_in.xjdreq_indexpos);
105
	temp+= ntohs(pdu_in.xjdreq_schlen);
106
	for (i = 0; i < strlen(pdu_in.xjdreq_schstr); i++) temp+= pdu_in.xjdreq_schstr[i];
107
108
	if (temp == ntohl(pdu_in.xjdreq_checksum)) return(TRUE);
109
	return(FALSE);
110
}
111
112
void sendresponse()
113
{
114
	long temp;
115
116
	temp = ntohs(pdu_out.xjdrsp_type);
117
	temp+= ntohs(pdu_out.xjdrsp_seq);
118
	temp+= ntohl(pdu_out.xjdrsp_resindex);
119
	temp+= ntohl(pdu_out.xjdrsp_dicloc);
120
	temp+= ntohs(pdu_out.xjdrsp_hitposn);
121
	temp+= ntohs(pdu_out.xjdrsp_reslen);
122
	for (i = 0; i < strlen(pdu_out.xjdrsp_resstr); i++)
123
		temp+= pdu_out.xjdrsp_resstr[i];
124
	pdu_out.xjdrsp_checksum = htonl(temp);
125
	if (SVERBOSE) printf("PDU sent: %d %d %d %d %d %d %s %d\n",
126
			ntohs(pdu_out.xjdrsp_type),
127
			ntohs(pdu_out.xjdrsp_seq),
128
			ntohl(pdu_out.xjdrsp_resindex),
129
			ntohl(pdu_out.xjdrsp_dicloc),
130
			ntohs(pdu_out.xjdrsp_hitposn),
131
			ntohs(pdu_out.xjdrsp_reslen),
132
			pdu_out.xjdrsp_resstr,
133
			temp);
134
	i = sendto (sock, (RSP_PDU *) &pdu_out, sizeof(pdu_out)-512+strlen(pdu_out.xjdrsp_resstr)+1,
135
			0, (struct sockaddr *)&fsin, sizeof(fsin));
136
	if (i < 0)
137
	{
138
		printf ("sendto error!: %s\n",strerror(errno));
139
	}
140
}
141
142
/*                  M A I N                                      */
143
144
main(argc,argv)
145
int argc;
146
unsigned char **argv;
147
148
{
149
	int iterlimit,i;
150
  	unsigned char *dicenv,strtmp[50];
151
	unsigned char xap[50];
152
153
	
154
  	printf("XJDSERVER Version %s (Japanese Dictionary Server) Copyright J.W.Breen 2003.\n",SVER);
155
156
	dicenv = (unsigned char *)getenv("XJDIC");
157
	if (!dicenv) dicenv = (unsigned char *)DEFAULT_DICDIR;
158
	if (strlen(dicenv) <= 2)
159
	{
160
		dicenv = (unsigned char *)getcwd(ENVname,sizeof(ENVname));
161
		if (dicenv == NULL)
162
		{
163
			printf("Cannot extract working directory!\n");
164
			exit(1);
165
		}
166
	}
167
	else
168
	{
169
		strcpy (ENVname,dicenv);
170
	}
171
        strcpy (Dnamet[0], "kanjidic");
172
        strcpy (Dnamet[1], "edict");
173
        strcpy (XJDXnamet[0], "kanjidic.xjdx");
174
	strcpy (XJDXnamet[1], "edict.xjdx");
175
	NoDics = 1;
176
	cl_rcfile[0] = 0;
177
/* process command-line options*/
178
	if (argc > 1)
179
	{
180
		for (i = 1; i < argc; i++)
181
		{
182
			strcpy(xap,argv[i]);
183
			if ((xap[0] == '-') && (xap[1] == 'h'))
184
			{
185
				printf("Command-line options:\n");
186
				printf("\t -c control_file\n");
187
				printf("\t -K (do not create daemon)\n");
188
				printf("\t -P nnnn (use UDP port nnnn\n");
189
				printf("\t -k kanji_dic_file\n");
190
				printf("\t -d dictionary_file (up to 9)\n");
191
				printf("\t -h (this information)\n");
192
				exit(0);
193
			}
194
			if ((xap[0] == '-') && (xap[1] == 'c'))
195
			{
196
				if(xap[2] != 0)
197
				{
198
					strcpy(strtmp,xap+2);
199
				}
200
				else
201
				{
202
					i++;
203
					strcpy(xap,argv[i]);
204
					strcpy (strtmp,xap);
205
				}
206
				strcpy (cl_rcfile,strtmp);
207
				printf ("Using control-file: %s\n",cl_rcfile);
208
			}
209
			if ((xap[0] == '-') && (xap[1] == 'd'))
210
			{
211
				if (thisdic == 0)
212
				{
213
					thisdic = 1;
214
				}
215
				else
216
				{
217
					thisdic++;
218
					NoDics++;
219
				}
220
				if(xap[2] != 0)
221
				{
222
					strcpy(strtmp,xap+2);
223
				}
224
				else
225
				{
226
					i++;
227
					strcpy(xap,argv[i]);
228
					strcpy (strtmp,xap);
229
				}
230
				strcpy (Dnamet[thisdic],strtmp);
231
				strcpy (XJDXnamet[thisdic],strtmp);
232
				strcat (XJDXnamet[thisdic],".xjdx");
233
				printf("Command-line request to use dictionary files: %s and %s\n",Dnamet[thisdic],XJDXnamet[thisdic]);
234
				continue;
235
			}
236
			if ((xap[0] == '-') && (xap[1] == 'K'))
237
			{
238
				DontFork = TRUE;
239
				continue;
240
			}
241
			if ((xap[0] == '-') && (xap[1] == 'P'))
242
			{
243
				if(xap[2] != 0)
244
				{
245
					portno = atoi(xap+2);
246
				}
247
				else
248
				{
249
					i++;
250
					strcpy(xap,argv[i]);
251
					portno = atoi(xap);
252
				}
253
				printf("Command-line request to use port: %d\n",portno);
254
				continue;
255
			}
256
			if ((xap[0] == '-') && (xap[1] == 'k'))
257
			{
258
				if(xap[2] != 0)
259
				{
260
					strcpy(strtmp,xap+2);
261
				}
262
				else
263
				{
264
					i++;
265
					strcpy(xap,argv[i]);
266
					strcpy (strtmp,xap);
267
				}
268
				strcpy (Dnamet[0],strtmp);
269
				strcpy (XJDXnamet[0],strtmp);
270
				strcat (XJDXnamet[0],".xjdx");
271
				printf("Command-line request to use kanji dictionary files: %s and %s\n",Dnamet[0],XJDXnamet[0]);
272
				continue;
273
			}
274
		}
275
	}
276
	xjdicrc();
277
  	printf ("Loading Dictionary and Index files.  Please wait...\n");
278
/* printf("TEST! NoDics = %d\n",NoDics); */
279
/* for(i = 0;i <= NoDics; i++) printf("TEST! Dnamet[] = %s\n",Dnamet[i]); */
280
	if (!DontFork)
281
	{
282
		i = fork();
283
		if (i < 0)
284
		{
285
			printf("error when forking: %s\n",strerror(errno));
286
			exit(1);
287
		}
288
		if (i) exit(0);
289
	}
290
  	DicSet ();
291
	sock = passiveUDP(portno);
292
	printf ("Server Loaded & Ready\n");
293
/*
294
	From here on, the code loops endlessly, reading  requests from the
295
	UDP port, looking up the dictionary, and sending back the answers.
296
*/
297
  	while (TRUE)
298
	{
299
		alen = sizeof(fsin);
300
		if (recvfrom(sock, (REQ_PDU *) &pdu_in, sizeof(pdu_in), 0, 
301
			(struct sockaddr *)&fsin, &alen) < 0)
302
		{
303
			fprintf (stderr,"recvfrom failure: %s\n",strerror(errno));
304
			continue;
305
		}
306
307
		if (!checksum_in) continue; 	/* ignore requests with bad checksums*/
308
		pdu_out.xjdrsp_seq = pdu_in.xjdreq_seq;
309
		pdu_out.xjdrsp_resindex = htonl(0);
310
		pdu_out.xjdrsp_dicloc = htonl(0);
311
		pdu_out.xjdrsp_hitposn = htons(0);
312
		pdu_out.xjdrsp_reslen= htons(0);
313
		pdu_out.xjdrsp_resstr[0] = 0;
314
		pdu_in.xjdreq_type = ntohs(pdu_in.xjdreq_type);
315
		pdu_in.xjdreq_dicno = ntohs(pdu_in.xjdreq_dicno);
316
		pdu_in.xjdreq_schlen = ntohs(pdu_in.xjdreq_schlen);
317
		pdu_in.xjdreq_indexpos = ntohl(pdu_in.xjdreq_indexpos);
318
319
320
		/* printf ("PDU: %d received\n",pdu_in.xjdreq_type);*/
321
		if (pdu_in.xjdreq_type == XJ_HULLO)
322
		{
323
			pdu_out.xjdrsp_type = htons(XJ_OK);
324
			pdu_out.xjdrsp_hitposn = htons(NoDics);
325
			pdu_out.xjdrsp_resstr[0] = 0;
326
			for (i = 0; i<=NoDics;i++)
327
			{
328
				sprintf(strtmp,"#%d%s",i,DicName(i));
329
				strcat(pdu_out.xjdrsp_resstr,strtmp);
330
			}
331
			pdu_out.xjdrsp_reslen = htons(strlen(pdu_out.xjdrsp_resstr));
332
			sendresponse();
333
			continue;
334
		}
335
336
		if (pdu_in.xjdreq_dicno > NoDics)
337
		{
338
			pdu_out.xjdrsp_type = htons(XJ_PROTERR);
339
			sendresponse();
340
			continue;
341
		}
342
		DicNum = pdu_in.xjdreq_dicno;
343
		hi = indptrt[pdu_in.xjdreq_dicno];
344
345
		if (pdu_in.xjdreq_type == XJ_FIND)
346
		{
347
			lo = 1;
348
			iterlimit = MAXITER;
349
			while(TRUE)
350
			{
351
				if (!iterlimit--) break;
352
  				it = (lo+hi)/2;
353
				res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
354
				if (res == 0)
355
				{
356
					itok = it;
357
					lo2 = lo;
358
					hi2 = it;
359
					while (TRUE)
360
					{
361
						if(lo2+1 >= hi2) break;
362
						it = (lo2+hi2)/2;
363
						res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
364
						if (res == 0)
365
						{
366
							hi2 = it;
367
							itok = it;
368
							continue;
369
						}
370
						else
371
						{
372
							lo2 = it+1;
373
						}
374
					}
375
					it = itok;
376
					res = 0;
377
					break;
378
				}
379
				if (res < 0)
380
				{
381
					hi = it-1;
382
				}
383
				else
384
				{
385
					lo = it+1;
386
				}
387
				if (lo > hi) break;
388
			}
389
			if (res != 0)
390
			{
391
				pdu_out.xjdrsp_type = htons(XJ_NBG);
392
				sendresponse();
393
				continue;
394
			}
395
	/* as the above sometimes misses the first matching entry, step back to the
396
    	first  */
397
			while (TRUE)
398
			{
399
				if(Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr) == 0)
400
				{
401
					it--;
402
					if (it == 0)
403
					{
404
						it = 1;
405
						break;
406
					}
407
					continue;
408
				}
409
				else
410
				{
411
					it++;
412
					break;
413
				}
414
			}
415
		}
416
	
417
	/*	Get next entry. Check (a) if the caller hasn't gone off the end of the
418
		table, and (b) if the (next) entry matches.		*/
419
	
420
		if (pdu_in.xjdreq_type == XJ_ENTRY)
421
		{
422
			if (pdu_in.xjdreq_indexpos > hi)
423
			{
424
				pdu_out.xjdrsp_type = htons(XJ_NBG);
425
				sendresponse();
426
				continue;
427
			}
428
			it = pdu_in.xjdreq_indexpos;
429
			res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
430
			if (res != 0)
431
			{
432
				pdu_out.xjdrsp_type = htons(XJ_NBG);
433
				sendresponse();
434
				continue;
435
			}
436
		}
437
		if (pdu_in.xjdreq_type == XJ_GET)
438
		{
439
			if (pdu_in.xjdreq_indexpos > hi)
440
			{
441
				pdu_out.xjdrsp_type = htons(XJ_NBG);
442
				sendresponse();
443
				continue;
444
			}
445
			it = pdu_in.xjdreq_indexpos;
446
		}
447
	
448
	/*  Common code to set up the return parameters for both call types  */
449
		schix = jindex(it);
450
		schiy = schix;
451
		pdu_out.xjdrsp_resindex = htonl(it);
452
	/* back off to the start of this line   */
453
		while ((dbchar(schix) != 0x0a) && (schix >= 0)) schix--;
454
		schix++;
455
		pdu_out.xjdrsp_dicloc = htonl(schix);
456
		pdu_out.xjdrsp_hitposn = htons(schiy - schix);
457
		if (pdu_in.xjdreq_type == XJ_ENTRY)
458
		{
459
			pdu_out.xjdrsp_resindex = htonl(schix);
460
		}
461
		if (pdu_in.xjdreq_type == XJ_GET)
462
		{
463
			pdu_out.xjdrsp_resindex = htonl(schix);
464
		}
465
		for (i = 0; dbchar(schix+i) != 0x0a; i++)
466
		{
467
			pdu_out.xjdrsp_resstr[i] = dbchar(schix+i);
468
		}
469
		pdu_out.xjdrsp_resstr[i+0] = 0x0a; /* NL tells user s/w that it is the end of an entry */
470
		pdu_out.xjdrsp_resstr[i+1] = 0;
471
		pdu_out.xjdrsp_reslen = htons(strlen(pdu_out.xjdrsp_resstr));
472
		pdu_out.xjdrsp_type = htons(XJ_OK);
473
		sendresponse();
474
		continue;
475
	}
476
}
(-)xjdic.orig/xjdxgen.c (-1 / +2 lines)
Lines 23-31 Link Here
23
#include <sys/stat.h>
23
#include <sys/stat.h>
24
24
25
#include <stdio.h>
25
#include <stdio.h>
26
/*#include <stdlib.h>*/
26
#include <stdlib.h>
27
#include <ctype.h>
27
#include <ctype.h>
28
#include <string.h>
28
#include <string.h>
29
#include <unistd.h>
29
#include "xjdic.h"
30
#include "xjdic.h"
30
31
31
#define TRUE 1
32
#define TRUE 1
(-)xjdic.orig/xjdxgen.c~ (+434 lines)
Line 0 Link Here
1
/**************************************************************************
2
*                     X J D X G E N
3
*                                                   Author: Jim Breen
4
*           Index (.xjdx) generator program fron XJDIC            
5
*
6
*		V2.3 - indexes JIS X 0212 (3-byte EUC) kanji
7
***************************************************************************/
8
/*  This program is free software; you can redistribute it and/or modify
9
    it under the terms of the GNU General Public License as published by
10
    the Free Software Foundation; either version 1, or (at your option)
11
    any later version.
12
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */
21
22
#include <sys/types.h>
23
#include <sys/stat.h>
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <ctype.h>
28
#include <string.h>
29
#include "xjdic.h"
30
31
#define TRUE 1
32
#define FALSE 0
33
#define SPTAG '@'
34
#define EXLIM 100
35
#define TOKENLIM 40
36
37
unsigned char *db;
38
unsigned char ENVname[50];
39
unsigned char *dicenv;
40
struct stat *buf;
41
unsigned long dbyte;
42
unsigned long  *jindex;
43
unsigned long indptr,llone;
44
unsigned char ctl_file[80] = {".xjdicrc"};
45
unsigned char Dname[80] = {"edict"};
46
unsigned char JDXname[80] = {"edict.xjdx"};
47
unsigned char EDname[80] = {"edict"};
48
unsigned char EJDXname[80] = {"edict.xjdx"};
49
unsigned char exlist[EXLIM][11];	/* list of words to be excluded */
50
int excount,exlens[EXLIM];
51
int jiver = 14;		/*The last time the index structure changed was Version1.4*/
52
53
/*====== prototypes=================================================*/
54
int stringcomp(unsigned char *s1, unsigned char *s2);
55
void jqsort(long i, long j);
56
int Kstrcmp(unsigned long lhs, unsigned long rhs);
57
void xjdicrc();
58
int alphaoreuc(unsigned char x);
59
60
int stringcomp(unsigned char *s1, unsigned char *s2)
61
{
62
	int i;
63
	unsigned char c1,c2;
64
65
	for(i = 0; i < strlen(s1);i++) 
66
	{
67
		c1 = s1[i];
68
		if (c1 < 0x60) c1 = (c1|0x20);
69
		c2 = s2[i];
70
		if (c2 < 0x60) c2 = (c2|0x20);
71
		if (c1 != c2) return(1);
72
	}
73
	return (0);
74
}
75
76
/*====function to Load Dictionary and load/create index table=======*/
77
main(argc,argv)
78
int argc;
79
unsigned char **argv;
80
{
81
  FILE *fp,*fopen();
82
  unsigned long possav,schi,diclen,indlen;
83
  int i,inwd,cstrp,saving,isc,nodread;
84
  int arg_c;
85
  unsigned char c;
86
  unsigned char currstr[TOKENLIM],strtmp[50];
87
  unsigned char **ap;
88
89
  printf("\nXJDXGEN V2.4 Index Table Generator for XJDIC. \n      Copyright J.W. Breen, 2003\n");
90
  ap = argv;
91
  arg_c = argc;
92
  while (arg_c > 1)
93
  {
94
	ap++;
95
	if(strcmp(*ap,"-h") == 0)
96
	{
97
		printf("\nThe command-line options are:\n");
98
		printf("  -h  this display\n");
99
		printf("  -c  control file\n");
100
		printf("  filename - file to be indexed\n\n");
101
		exit(0);
102
	}
103
	if(strcmp(*ap,"-c") == 0)
104
	{
105
		ap++;
106
		strcpy(ctl_file,*ap);
107
    		printf("Commandline request to use control file %s\n",ctl_file);
108
		arg_c-=2;
109
		continue;
110
	}
111
	strcpy(strtmp,*ap);
112
	strcpy(Dname,*ap);
113
	strcpy(JDXname,*ap);
114
	strcat(JDXname,".xjdx");
115
    	printf("Commandline request to use files %s and %s \n",Dname,JDXname);
116
	ap++;
117
	arg_c--;
118
  }
119
  xjdicrc();
120
  inwd = FALSE;
121
  indptr = 1;
122
  llone = 1;
123
  buf = (void *)malloc(1000);
124
  if(stat(Dname, buf) != 0)
125
  {
126
	 perror(NULL);
127
	 printf("Cannot stat: %s \n",Dname);
128
	 exit(1);
129
  }
130
  diclen = buf->st_size;
131
  printf("\nWARNING!!  This program may take a long time to run .....\n");
132
133
  puts ("\nLoading Dictionary file.  Please wait.....\n");
134
  fp=fopen(Dname,"rb");
135
  if (fp==NULL )
136
  {
137
	printf("\nCannot open dictionary file\n");
138
	exit(1);
139
  }
140
  db = (unsigned char *)malloc((diclen+100) * sizeof(unsigned char));
141
  if(db == NULL)
142
  {
143
      fprintf(stderr,"malloc() for dictionary failed.\n");
144
      fclose(fp);
145
      exit(1);
146
  }
147
  nodread = diclen/1024;
148
  dbyte = fread((unsigned char *)db+1, 1024, nodread, fp);
149
  nodread = diclen % 1024;
150
  dbyte = fread((unsigned char *)(db+(diclen/1024)*1024)+1, nodread,1, fp);
151
  fclose(fp);
152
  diclen++;
153
  dbyte = diclen;
154
  db[diclen] = 10;
155
  db[0] = 10;
156
  printf("Dictionary size: %ld bytes.\n",dbyte);
157
  indlen = (diclen * sizeof(long))/4;
158
  jindex = (unsigned long *)malloc(indlen);
159
  if(jindex == NULL)
160
  {
161
	  fprintf(stderr,"malloc() for index table failed.\n");
162
	  fclose(fp);
163
	  exit(1);
164
  }
165
  printf("Parsing.... \n");
166
  /*this is the dictionary parser. It places an entry in jindex for every
167
   kana/kanji string and every alphabetic string it finds which is >=3
168
   characters and is not on the "exclude" list */
169
  indptr = 1;
170
  saving = FALSE;
171
  cstrp = 0;
172
  for (schi =0; schi < dbyte; schi++) /* scan whole dictionary  */
173
  {
174
	  c = db[schi];
175
	  if (inwd)
176
	  {
177
		  if ((alphaoreuc(c))||(c == '-')||(c == '.')||((c >= '0') && (c <= '9')))
178
		  {
179
			  currstr[cstrp] = c;
180
			  if(cstrp < TOKENLIM-1) cstrp++;
181
		  }
182
		  else
183
		  {
184
			  currstr[cstrp] = '\0';
185
			  inwd = FALSE;
186
			  if ((strlen(currstr) <= 2) && (currstr[0] < 127))saving = FALSE;
187
			  if ((strlen(currstr) == 2) && (currstr[1] <= '9'))saving = TRUE;
188
			  if (saving && (currstr[0] > 127))
189
			  {
190
				  possav = jindex[indptr];
191
				  indptr++;
192
					if (indptr > indlen/sizeof(long))
193
					{
194
					printf("Index table overflow. Dictionary too large?\n");
195
					exit(1);
196
					}
197
/* generate index for *every* kanji in key */
198
				i = 2;
199
				if (currstr[0] == 0x8f) i++;
200
				for ( ; i < strlen(currstr); i+=2)
201
				{
202
					if((currstr[i] >= 0xb0) || (currstr[i] == 0x8f))
203
					{
204
						jindex[indptr] = possav+i;
205
						indptr++;
206
                                  		if (indptr > indlen/sizeof(long))
207
                                  		{
208
                                          		printf("Index table overflow. Dictionary too large?\n");
209
                                          		exit(1);
210
                                  		}
211
					}
212
					if (currstr[i] == 0x8f) i++;
213
				}
214
			  }
215
			  if (saving && (currstr[0] < 127))
216
			  {
217
				  indptr++;
218
				  if (indptr > indlen/sizeof(long))
219
				  {
220
					  printf("Index table overflow. Dictionary too large?\n");
221
					  exit(1);
222
				  }
223
/* If this is non-Japanese, and has a 'SPTAGn' tag, generate two indices */
224
				  if ( currstr[0] == SPTAG)
225
				  {
226
				  	jindex[indptr] = jindex[indptr-1]+1;
227
				  	strcpy(currstr,currstr+1);
228
				  	indptr++;
229
				  	if (indptr > indlen/sizeof(long))
230
				  	{
231
					  	printf("Index table overflow. Dictionary too large?\n");
232
					  	exit(1);
233
				  	}
234
				  }
235
				  if (currstr[0] < 128)
236
				  {
237
					  for (isc = 0; isc <= excount; isc++)
238
					  {
239
						  if (( exlens[isc] == strlen(currstr)) &&
240
						  (stringcomp(currstr,exlist[isc]) == 0)   )
241
						  {
242
							  indptr--;
243
							  break;
244
						  }
245
					  }
246
				  }
247
			  }
248
		  }
249
	  }
250
	  else
251
	  {
252
		  if (alphaoreuc(c) || c == SPTAG)
253
		  {
254
			  inwd = TRUE;
255
			  jindex[indptr] = schi;
256
			  cstrp = 1;
257
			  currstr[0] = c;
258
			  currstr[1] = '\0';
259
			  saving = TRUE;
260
		  }
261
	  }
262
    }
263
    indptr--;
264
    printf("Index entries: %ld  \nSorting (this is slow)......\n",indptr);
265
    jqsort(llone,indptr);
266
    printf("Sorted\nWriting index file ....\n");
267
    fp = fopen(JDXname,"wb");
268
    if (fp==NULL )
269
    {
270
    printf("\nCannot open %s output file\n",JDXname);
271
    exit(1);
272
  }
273
  jindex[0] = diclen+jiver;
274
  fwrite(jindex,sizeof(long),indptr+1,fp);
275
  i = fclose(fp);
276
  if (i != 0)
277
  {
278
		printf("\nDictionary Index File Close Failure\n");
279
		exit(1);
280
  }
281
  exit (0);
282
}
283
/*======function to sort jindex table====================*/
284
void jqsort(long lhs, long rhs)
285
{
286
	long i,last,midp;
287
	unsigned long temp;
288
	if (lhs >= rhs) return;
289
	/* Swap ( lhs , (lhs+rhs)/2);*/
290
	midp = (lhs+rhs)/2;
291
	temp = jindex[lhs];
292
	jindex[lhs] = jindex[midp];
293
	jindex[midp] = temp;
294
	last = lhs;
295
	for (i = lhs+1;i <= rhs; i++)
296
		{
297
			if (Kstrcmp(jindex[i],jindex[lhs]) < 0)
298
			{
299
				/* Swap(++last,i);*/
300
				last++;
301
				temp = jindex[i];
302
				jindex[i] = jindex[last];
303
				jindex[last] = temp;
304
			}
305
		}
306
/*	Swap (lhs,last);*/
307
	temp = jindex[lhs];
308
	jindex[lhs] = jindex[last];
309
	jindex[last] = temp;
310
	jqsort(lhs,last-1);
311
	jqsort(last+1,rhs);
312
}
313
/*=====string comparison used by jqsort==========================*/
314
int Kstrcmp(unsigned long lhs, unsigned long rhs)
315
{
316
	int i,c1,c2;
317
/* effectively does a strnicmp on two "strings" within the dictionary,
318
   except it will make katakana and hirgana match (EUC A4 & A5) */
319
320
	for (i = 0; i<20 ; i++)
321
	{
322
		c1 = db[lhs+i];
323
		c2 = db[rhs+i];
324
		if ((i % 2) == 0)
325
		{
326
			if (c1 == 0xA5)
327
			{
328
				c1 = 0xA4;
329
			}
330
			if (c2 == 0xA5)
331
			{
332
				c2 = 0xA4;
333
			}
334
		}
335
		if ((c1 >= 'A') && (c1 <= 'Z')) c1 |= 0x20;
336
		if ((c2 >= 'A') && (c2 <= 'Z')) c2 |= 0x20;
337
		if (c1 != c2 ) break;
338
	}
339
	return(c1-c2);
340
}
341
/*=====xjdicrc - access and analyze "xjdicrc" file (if any)==============*/
342
void xjdicrc()
343
{
344
	unsigned char xjdicdir[128],rcstr[80],*rcwd;
345
	int iex;
346
	FILE *fm,*fopen();
347
348
	iex = 0;
349
	xjdicdir[0] = '\0';
350
        dicenv = (unsigned char *)getenv("XJDIC");
351
        if (!dicenv) dicenv = (unsigned char *)DEFAULT_DICDIR;
352
        if (strlen(dicenv) <= 2) 
353
	{
354
		dicenv = (unsigned char *)getcwd(ENVname,sizeof(ENVname));
355
		if (dicenv == NULL) 
356
		{
357
			printf("Cannot extract working directory!\n");
358
			exit(1);
359
		}
360
	}
361
	else
362
	{
363
		strcpy (ENVname,dicenv);
364
        }
365
	if (strlen(ENVname) > 2)
366
	{
367
		strcpy(xjdicdir,ENVname);
368
		strcat(xjdicdir,"/");
369
	}
370
	else    
371
	{
372
		strcpy(xjdicdir,(unsigned char *)getenv("HOME"));
373
		strcat(xjdicdir,"/");
374
	}
375
376
	strcat(xjdicdir,ctl_file);
377
	fm = fopen(xjdicdir,"r");
378
	if (fm == NULL)
379
	{
380
		strcpy(xjdicdir,ctl_file);
381
		fm = fopen(xjdicdir,"r");
382
	}
383
	if (fm != NULL)
384
	{
385
		while(fgets(rcstr,79,fm) != NULL)
386
		{
387
			rcwd = (unsigned char *)strtok(rcstr," \t");
388
                        if( stringcomp((unsigned char *)"exlist",rcwd) == 0)
389
                        {
390
				while (TRUE)
391
				{
392
                                	rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
393
					if (rcwd == NULL) break;
394
					strcpy(exlist[iex],rcwd);
395
					exlens[iex] = strlen(rcwd);
396
					if (iex < EXLIM) iex++;
397
				}
398
				excount = iex-1;
399
                                continue;
400
                        }
401
		}
402
	}
403
	if (fm == NULL)
404
	{
405
		printf("No control file detected!\n");
406
		return;
407
	}
408
	else
409
	{
410
		fclose(fm);
411
		return;
412
	}
413
}
414
/*=======function to test a character for alpha or kana/kanji====*/
415
int alphaoreuc(unsigned char x)
416
{
417
	int c;
418
419
	c = x & 0xff;
420
	if(((c >= 65) && (c <= 90)) || ((c >= 97) && (c <= 122)))
421
	{
422
		return (TRUE);
423
	}
424
	if ((c >= '0') && (c <= '9'))
425
	{
426
		return(TRUE);
427
	}
428
	if ((c & 0x80) > 0)
429
	{
430
		return(TRUE);
431
	}
432
	return (FALSE);
433
}
434

Return to bug 282904