File: xjdcomm.c

package info (click to toggle)
xjdic 24-13
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,080 kB
  • sloc: ansic: 23,275; makefile: 191; sh: 55
file content (445 lines) | stat: -rw-r--r-- 11,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**************************************************************************
*                 X  J  D  C  O  M  M                                     *
*              some common routines                                       *
*         Japanese-English Dictionary program (X11 version)               *
*                                                   Author: Jim Breen     *
***************************************************************************/
/*  This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#ifdef MMAP
#include <fcntl.h>
#endif
#include "xjdic.h"

#define FALSE 0
#define TRUE 1

#ifdef XJDCLSERV
extern int portno;
extern unsigned char host[];
#endif

extern unsigned char Dnamet[10][100];

int stringcomp(unsigned char *s1, unsigned char *s2);
struct stat buf;

#ifdef MMAP
/*+++++++++ xopen +++ does a open, but tries the dicdir first +++*/
/*
	xopen generalizes the dictionary and other file opening, by:

		trying the "dicdir", and then current directories
		returning the file length as well as the pointer to FILE
	Note that it is read-only.
								*/
int xopen(char *file_name, int *xfilelen)
{
	int fx;

	extern char DicDir[];
	char *fnbuff;

	fnbuff = (char *)malloc(strlen(DicDir) + strlen(file_name)+10);
	if (fnbuff == NULL)
	{
		printf("malloc failure opening: %s\n",file_name);
		exit(1);
	}
	strcpy(fnbuff,DicDir);
	if (fnbuff[strlen(fnbuff)-1] != '/') strcat (fnbuff,"/");
	strcat(fnbuff,file_name);
	fx = open(fnbuff,O_RDONLY);
	if (fx >= 0)
	{
		if(stat(fnbuff, &buf) != 0)
		{
			printf ("Stat() error (l)for %s [%s]\n",fnbuff,strerror(errno));
			exit(1);
		}
		*xfilelen = (buf.st_size);
		free(fnbuff);
		return(fx);
	}
	fx = open(file_name,O_RDONLY);
	if (fx >= 0) 
	{
		if(stat(file_name, &buf) != 0)
		{
			printf ("Stat() error (s) for %s [%s]\n",file_name,strerror(errno));
			exit(1);
		}
		*xfilelen = buf.st_size;
		return(fx);
	}
	printf("Unable to open: %s\n",file_name);
	exit(1);
}
#endif

/*+++++++++ xfopen +++ does a fopen, but tries the dicdir first +++*/
/*
	xfopen generalizes the dictionary and other file opening, by:

		trying the "dicdir", and then current directories
		returning the file length as well as the pointer to FILE
								*/
FILE  *xfopen(char *file_name, char *file_mode, int *xfilelen)
{
	FILE *fx;

	extern char DicDir[];
	char *fnbuff;

/* printf ("XFOPEN: fn=%s mode=%s stream_p=%p\n",file_name,file_mode,fx); */
	fnbuff = (char *)malloc(strlen(DicDir) + strlen(file_name)+10);
	if (fnbuff == NULL)
	{
		printf("malloc failure opening: %s\n",file_name);
		exit(1);
	}
	strcpy(fnbuff,DicDir);
	if (fnbuff[strlen(fnbuff)-1] != '/') strcat (fnbuff,"/");
	strcat(fnbuff,file_name);
	fx = fopen(fnbuff,file_mode);
	if (fx != NULL)
	{
		if(stat(fnbuff, &buf) != 0)
		{
			printf ("Stat() error (l)for %s [%s]\n",fnbuff,strerror(errno));
			exit(1);
		}
		*xfilelen = (buf.st_size);
		free(fnbuff);
/* printf ("XFOPEN: stream_p=%p addr = %p\n",fx,&fx); */
		return(fx);
	}
	fx = fopen(file_name,file_mode);
	if (fx != NULL) 
	{
		if(stat(file_name, &buf) != 0)
		{
			printf ("Stat() error (s) for %s [%s]\n",file_name,strerror(errno));
			exit(1);
		}
		*xfilelen = buf.st_size;
/* printf ("XFOPEN: stream_p=%p addr = %p\n",fx,&fx); */
		return(fx);
	}
	printf("Unable to open: %s\n",file_name);
	exit(1);
}
/*=========DicName====returns name of dictionary================*/
unsigned char *DicName(int dn)
{
	register unsigned char *dp,*dp2;

	dp  = Dnamet[dn];
	if((dp2 = strrchr(dp,'/')) == NULL) return(dp);
	return (dp2+1);
}
/*=====xjdicrc - access and analyze "xjdicrc" file (if any)==============*/
void xjdicrc()
{
	unsigned char xjdicdir[128],rcstr[80],*rcwd;
	int ft,fn;
	extern int thisdic;
	extern char DicDir[];
	FILE *fm;

#ifdef XJDDIC
	extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
	extern unsigned char *dicbufft[10];
	extern unsigned long diclent[10], indkent[10],indptrt[10];
	extern int NoDics;
#endif
	extern unsigned char ENVname[], KDNSlist[];
	extern unsigned char EXTJDXname[], EXTname[], Rname[], Vname[], ROMname[];
	extern unsigned char RKname[];
	extern unsigned char filtnames[NOFILT][50],filtcodes[NOFILT][10][10];
	extern int Omode, Jverb, nofilts, filtact[], filtcoden[], filttype[], filton[];
	extern int RVACTIVE;
	extern unsigned char cl_rcfile[];
#ifdef XJDFRONTEND
	extern unsigned char GPL_File[];
	extern unsigned char Clip_File[];
	extern int KImode;
#endif

	strcpy(DicDir,ENVname); /* added by nakahara@debian.org */
	while(TRUE)
	{
		if (strlen(cl_rcfile) > 0)
		{
			fm = fopen(cl_rcfile,"r");
			if (fm != NULL) break;
			else
			{
				printf("Control file: %s cannot be accessed!\n",cl_rcfile);
			}
		}
		xjdicdir[0] = '\0';
		if (strlen(ENVname) > 2)
		{
			strcpy(xjdicdir,ENVname);
			strcat(xjdicdir,"/");
		}
		else    
		{
			strcpy(xjdicdir,getenv("HOME"));
			strcat(xjdicdir,"/");
		}
		strcat(xjdicdir,".xjdicrc");
		fm = fopen(xjdicdir,"r");
		if (fm != NULL) break;
		strcpy(xjdicdir,".xjdicrc");
		fm = fopen(xjdicdir,"r");
		if (fm != NULL) break;
		if (getenv("HOME") != NULL)
		{
			strcpy(xjdicdir,getenv("HOME"));
			strcat(xjdicdir,"/");
			strcat(xjdicdir,".xjdicrc");
			fm = fopen(xjdicdir,"r");
			if (fm != NULL) break;
		}
		printf("No control file detected!\n");
		return;
	}
	if (fm != NULL)
	{
		while(fgets(rcstr,79,fm) != NULL)
		{
			rcwd = (unsigned char *)strtok(rcstr," \t");
			if (rcwd == NULL) continue; /* Debian fix for bug #716515 */
/*  dicdir works for all modes   */
			if( stringcomp((unsigned char *)"dicdir",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(DicDir,rcwd);
				continue;
			}
#ifdef XJDCLSERV
			if( stringcomp((unsigned char *)"port",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				portno = atoi(rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"server",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(host,rcwd);
				continue;
			}
#endif
#ifdef XJDFRONTEND
			if( stringcomp((unsigned char *)"omode",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				rcwd[0] = rcwd[0] | 0x20;
				if(rcwd[0] == 'j') Omode = 0;
				if(rcwd[0] == 'e') Omode = 1;
				if(rcwd[0] == 's') Omode = 2;
				continue;
			}
			if( stringcomp((unsigned char *)"kanamode",rcwd) == 0)
			{
				KImode = 0;
				continue;
			}
			if( stringcomp((unsigned char *)"exactmatch",rcwd) == 0)
			{
				EMtoggle ();
				continue;
			}
#endif
#ifdef XJDDIC
			if( stringcomp((unsigned char *)"dicfile",rcwd) == 0)
			{
				if (thisdic == 0)
				{
					thisdic = 1;
				}
				else
				{
					thisdic++;
					NoDics++;
				}
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(Dnamet[thisdic],rcwd);
				strcpy(XJDXnamet[thisdic],rcwd);
				strcat(XJDXnamet[thisdic],".xjdx");
				continue;
			}
#endif
#ifdef XJDFRONTEND
                        if( stringcomp((unsigned char *)"gnufile",rcwd) == 0)
                        {
                                rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
                                strcpy(GPL_File,rcwd);
                                continue;
                        }
#endif
#ifdef XJDFRONTEND
                        if( stringcomp((unsigned char *)"clipfile",rcwd) == 0)
                        {
                                rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
                                strcpy(Clip_File,rcwd);
                                continue;
                        }
#endif
#ifdef XJDFRONTEND
                        if( stringcomp((unsigned char *)"extfile",rcwd) == 0)
                        {
                                rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
                                strcpy(EXTname,rcwd);
                                strcpy(EXTJDXname,rcwd);
                                strcat(EXTJDXname, ".xjdx");
                                continue;
                        }
#endif
#ifdef XJDFRONTEND
			if( stringcomp((unsigned char *)"rvdisplay",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				if( stringcomp((unsigned char *)"on",rcwd) == 0) RVACTIVE = TRUE;
				if( stringcomp((unsigned char *)"off",rcwd) == 0) RVACTIVE = FALSE;
				continue;
			}
#endif
#ifdef XJDFRONTEND
			if( stringcomp((unsigned char *)"kdnoshow",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(KDNSlist,rcwd);
				continue;
			}
#endif
#ifdef XJDDIC
			if( stringcomp((unsigned char *)"kdicfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(Dnamet[0],rcwd);
				strcpy(XJDXnamet[0],rcwd);
				strcat(XJDXnamet[0],".xjdx");
				continue;
			}
#endif
#ifdef XJDFRONTEND
			if( stringcomp((unsigned char *)"radkfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(RKname,rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"radfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(Rname,rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"verbfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(Vname,rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"romfile",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				strcpy(ROMname,rcwd);
				continue;
			}
			if( stringcomp((unsigned char *)"jverb",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				if(stringcomp(rcwd,(unsigned char *)"on") == 0) Jverb = TRUE;
				if(stringcomp(rcwd,(unsigned char *)"off") == 0) Jverb = FALSE;
				continue;
			}
			if( stringcomp((unsigned char *)"filt",rcwd) == 0)
			{
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				sscanf(rcwd,"%d",&fn);
				if ((fn < 0)||(fn > NOFILT)) continue;
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				sscanf(rcwd,"%d",&ft);
				if (ft > 2) continue;
				filttype[fn] = ft;
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				filton[fn] = FALSE;
				if(stringcomp((unsigned char *)"on",rcwd) == 0) 
				{
					filton[fn] = TRUE;
					nofilts  = TRUE;
				}
				rcwd = (unsigned char *)strtok(NULL,"\"");
				strcpy(filtnames[fn],rcwd);
				ft=0;
				rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				while(rcwd != NULL)
				{
					strcpy(filtcodes[fn][ft],rcwd);
					ft++;
					rcwd = (unsigned char *)strtok(NULL," \t\f\r\n");
				}
				if(ft==0)continue;
				filtcoden[fn] = ft;
				filtact[fn] = TRUE;
				continue;
			}
#endif
		}
	}
	else
	{
		printf("No .xjdicrc file detected\n");
		return;
	}
	fclose(fm);
}

/*====stringcomp==stricmp & strcasecmp pulled together=========*/
/*    (my own routine, because different systems need one or the other    */
int stringcomp(unsigned char *s1, unsigned char *s2)
{	
	int i;	 unsigned char c1,c2;
	
	for(i = 0; i < strlen(s1);i++)
	{
		c1 = s1[i];
		if (c1 < 0x60) c1 = (c1|0x20);
		c2 = s2[i];
		if (c2 < 0x60) c2 = (c2|0x20);
		if (c1 != c2) return(1);
	}
	return (0);
}
/*====SeekErr==Common error routine for seeking==================*/
void SeekErr(int iores)
{
	printf("\nSeek error %d\n",iores);
	exit(1);
}