1 /*
2 * ���شۥ�����ϥ����Ѹ켭ŵ�����桼�ƥ���ƥ��� - csrd
3 *
4 * Written by Junn Ohta (ohta@src.ricoh.co.jp), Public Domain.
5 */
6
7 char *progname = "csrd";
8 char *version = "1.0";
9 char *date = "1999/07/19";
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #ifdef UNIX
20 #include <unistd.h>
21 #endif
22 #ifdef MSDOS
23 #include <io.h>
24 #endif
25
26 typedef unsigned char uchr;
27 typedef unsigned int unt;
28
29 #ifndef O_BINARY
30 #define O_BINARY 0
31 #endif
32
33 #define OK 0
34 #define ERR (-1)
35
36 #define TRUE 1
37 #define FALSE 0
38
39 #define SJIS1(c) ((c)>=0x81 && (c)<=0x9f || (c)>=0xe0 && (c)<=0xef)
40
41 #ifdef DEBUG
42 int debug = FALSE; /* �ǥХå��ե饰 */
43 int debug2 = FALSE; /* �ǥХå��ե饰 */
44 #endif
45
46 /* -------------------- �桼�ƥ���ƥ����ؿ� -------------------- */
47
48 #define POOLSIZ 1024 /* ʸ����ס���γ������ñ�� */
49
50 uchr *pool = NULL; /* ʸ����ס��� */
51 uchr *pfree = NULL; /* ʸ����ס���ζ����ΰ���Ƭ */
52
53 int hexval();
54 uchr *escchar();
55 uchr *skipeq();
56 uchr *skipsp();
57 uchr *strpool();
58 void bsltosl();
59 int nspaces();
60 void lower();
61
62 /*
63 * hexval - 2���16�ʿ��������ͤ��֤�
64 */
65 int
hexval(p)66 hexval(p)
67 uchr *p;
68 {
69 int i, n;
70
71 n = 0;
72 for (i = 0; i < 2; i++) {
73 n <<= 4;
74 if (*p >= '0' && *p <= '9')
75 n += *p - '0';
76 else if (*p >= 'a' && *p <= 'f')
77 n += *p - 'a' + 10;
78 else if (*p >= 'A' && *p <= 'F')
79 n += *p - 'A' + 10;
80 else
81 n = 0;
82 p++;
83 }
84 return n;
85 }
86
87 /*
88 * escchar - '\'�ǥ��������פ��줿ʸ����Ĵ�١����ΰ��֤��֤�
89 */
90 uchr *
escchar(str,cp)91 escchar(str, cp)
92 uchr *str;
93 int *cp;
94 {
95 uchr *p;
96
97 p = str;
98 if (*p++ != '\\')
99 return str;
100 if (*p >= '0' && *p <= '7') {
101 sscanf(p, "%3o", cp);
102 return p + 3;
103 }
104 switch (*p) {
105 case 'x':
106 *cp = hexval(p + 1);
107 p += 3;
108 break;
109 case 'n':
110 *cp = '\n';
111 p++;
112 break;
113 case 't':
114 *cp = '\t';
115 p++;
116 break;
117 default:
118 *cp = *p++;
119 break;
120 }
121 return p;
122 }
123
124 /*
125 * skipeq - '='�ޤǤ��ɤ����Ф������ΰ��֤��֤�
126 */
127 uchr *
skipeq(str)128 skipeq(str)
129 uchr *str;
130 {
131 while (*str && *str != '=')
132 str++;
133 if (*str == '=')
134 str++;
135 return str;
136 }
137
138 /*
139 * skipsp - ������ɤ����Ф������ΰ��֤��֤�
140 */
141 uchr *
skipsp(str)142 skipsp(str)
143 uchr *str;
144 {
145 while (*str == ' ' || *str == '\t')
146 str++;
147 return str;
148 }
149
150 /*
151 * strpool - ʸ�����ס���˳�Ǽ���Ƥ��Υ��ɥ쥹���֤�
152 * (�������θ���ʤ�strdup)
153 */
154 uchr *
strpool(str)155 strpool(str)
156 uchr *str;
157 {
158 int len;
159 uchr *p;
160
161 if (str == NULL)
162 return NULL;
163 len = strlen(str) + 1;
164 if (pool == NULL || pfree + len > pool + POOLSIZ) {
165 pool = (uchr *)malloc(POOLSIZ); /* ������pool���ͤ�˺��� */
166 if (pool == NULL)
167 return NULL;
168 pfree = pool;
169 }
170 p = pfree;
171 pfree += len;
172 return strcpy(p, str);
173 }
174
175 /*
176 * bsltosl - ʸ������ΥХå�����å���(\)��å���(/)���Ѵ�����
177 */
178 void
bsltosl(str)179 bsltosl(str)
180 uchr *str;
181 {
182 if (str == NULL)
183 return;
184 while (*str) {
185 if (*str == '\\')
186 *str = '/';
187 if (SJIS1(*str))
188 str++;
189 str++;
190 }
191 }
192
193 /*
194 * nspaces - ʸ�������Ƭ�ˤ��륹�ڡ������̤������
195 */
196 int
nspaces(str)197 nspaces(str)
198 uchr *str;
199 {
200 int n;
201
202 if (str == NULL)
203 return 0;
204 n = 0;
205 while (*str && isspace(*str)) {
206 switch (*str++) {
207 case ' ':
208 n++;
209 break;
210 case '\t':
211 n = (n | 0x07) + 1;
212 break;
213 case '\n':
214 case '\r':
215 n = 0;
216 break;
217 case '\b':
218 if (n > 0)
219 n--;
220 break;
221 }
222 }
223 return n;
224 }
225
226 /*
227 * lower - ��ʸ�����ʸ��������
228 */
229 void
lower(str)230 lower(str)
231 uchr *str;
232 {
233 if (str == NULL)
234 return;
235 while (*str) {
236 if (isascii(*str) && isupper(*str))
237 *str = tolower(*str);
238 str++;
239 }
240 }
241
242 /* -------------------- �Хåե��������� -------------------- */
243
244 #define BBUFSIZ 4096
245
246 typedef struct _bfile {
247 int fd;
248 long size;
249 long pos;
250 uchr *p, *ep;
251 uchr buf[BBUFSIZ];
252 } BFILE;
253
254 BFILE *bopen();
255 long btell();
256 int bseek();
257 int bread();
258 int bgetc();
259 int bgetcbw();
260 int bclose();
261
262 /*
263 * bopen - �Хåե��������Ϥǥե�������ץ�
264 * bseek()�ǰ��֤Ť���ޤ��ɤ߹��ߤϤǤ��ʤ�
265 */
266 BFILE *
bopen(file)267 bopen(file)
268 char *file;
269 {
270 int fd;
271 BFILE *bp;
272 struct stat st;
273
274 if ((fd = open(file, O_RDONLY|O_BINARY)) < 0)
275 return NULL;
276 if (fstat(fd, &st) < 0) {
277 close(fd);
278 return NULL;
279 }
280 bp = (BFILE *)malloc(sizeof(BFILE));
281 if (bp == NULL) {
282 close(fd);
283 return NULL;
284 }
285 bp->fd = fd;
286 bp->size = st.st_size;
287 bp->pos = 0L;
288 bp->p = bp->ep = NULL;
289 return bp;
290 }
291
292 /*
293 * btell - �Хåե��������Ϥ�tell()��ɤ�
294 */
295 long
btell(bp)296 btell(bp)
297 BFILE *bp;
298 {
299 if (bp->p == NULL)
300 return (long)ERR;
301 return bp->pos + (bp->p - bp->buf);
302 }
303
304 /*
305 * bseek - �Хåե��������Ϥ�lseek()��ɤ�
306 */
307 int
bseek(bp,pos)308 bseek(bp, pos)
309 BFILE *bp;
310 long pos;
311 {
312 int n;
313
314 if (bp->p
315 && pos >= bp->pos
316 && pos < bp->pos + (bp->ep - bp->buf)) {
317 bp->p = bp->buf + (pos - bp->pos);
318 return OK;
319 }
320 if (pos >= bp->size
321 || lseek(bp->fd, pos, SEEK_SET) < 0
322 || (n = read(bp->fd, bp->buf, BBUFSIZ)) <= 0) {
323 bp->pos = 0L;
324 bp->p = bp->ep = NULL;
325 return ERR;
326 }
327 bp->pos = pos;
328 bp->p = bp->buf;
329 bp->ep = bp->buf + n;
330 return OK;
331 }
332
333 /*
334 * bread - �Хåե��������Ϥ�read()��ɤ�
335 */
336 int
bread(bp,buf,len)337 bread(bp, buf, len)
338 BFILE *bp;
339 uchr *buf;
340 int len;
341 {
342 int n;
343 uchr *p;
344
345 if (bp->p == NULL)
346 return ERR;
347 p = buf;
348 while (len > 0) {
349 n = bp->ep - bp->p;
350 if (n >= len) {
351 memcpy(p, bp->p, (size_t)len);
352 p += len;
353 bp->p += len;
354 len = 0;
355 } else if (n > 0) {
356 memcpy(p, bp->p, (size_t)n);
357 p += n;
358 bp->p += n;
359 len -= n;
360 } else {
361 n = read(bp->fd, bp->buf, BBUFSIZ);
362 if (n <= 0) {
363 bp->pos = 0L;
364 bp->p = bp->ep = NULL;
365 break;
366 }
367 bp->pos += bp->ep - bp->buf;
368 bp->p = bp->buf;
369 bp->ep = bp->buf + n;
370 }
371 }
372 return p - buf;
373 }
374
375 /*
376 * bgetc - �Хåե��������Ϥ�getc()��ɤ�
377 */
378 int
bgetc(bp)379 bgetc(bp)
380 BFILE *bp;
381 {
382 int n;
383
384 if (bp->p == NULL)
385 return EOF;
386 if (bp->p < bp->ep)
387 return (int)*(bp->p++);
388 n = read(bp->fd, bp->buf, BBUFSIZ);
389 if (n <= 0) {
390 bp->pos = 0L;
391 bp->p = bp->ep = NULL;
392 return EOF;
393 }
394 bp->pos += bp->ep - bp->buf;
395 bp->p = bp->buf;
396 bp->ep = bp->buf + n;
397 return (int)*(bp->p++);
398 }
399
400 /*
401 * bgetcbw - ���ߤΰ��֤���ե�������Ƭ������bgetc()����
402 */
403 int
bgetcbw(bp)404 bgetcbw(bp)
405 BFILE *bp;
406 {
407 int n;
408 long npos;
409
410 if (bp->p == NULL)
411 return EOF;
412 if (bp->p > bp->buf)
413 return (int)*(--bp->p);
414 n = BBUFSIZ;
415 npos = bp->pos - n;
416 if (bp->pos < 0L) {
417 npos = 0L;
418 n = bp->pos - npos;
419 if (n == 0)
420 return EOF;
421 }
422 if (lseek(bp->fd, npos, SEEK_SET) < 0
423 || read(bp->fd, bp->buf, n) < n) {
424 bp->pos = 0L;
425 bp->p = bp->ep = NULL;
426 return EOF;
427 }
428 bp->pos = npos;
429 bp->ep = bp->buf + n;
430 bp->p = bp->ep;
431 return (int)*(--bp->p);
432 }
433
434 /*
435 * bclose - �Хåե��������Ϥ�close()��ɤ�
436 */
437 int
bclose(bp)438 bclose(bp)
439 BFILE *bp;
440 {
441 int fd;
442
443 fd = bp->fd;
444 free((char *)bp);
445 return close(fd);
446 }
447
448 /* -------------------- ���ܸ���� -------------------- */
449
450 #define KC_JIS 0
451 #define KC_EUC 1
452 #define KC_SJIS 2
453
454 #ifdef UNIX
455 #define KC_DEF KC_EUC
456 #endif
457 #ifdef MSDOS
458 #define KC_DEF KC_SJIS
459 #endif
460
461 /*
462 * ����ɽ
463 */
464 uchr *gaijitbl[8][256] = {
465 { /* G0 */
466 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
467 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
468 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"/",
469 0,0,0,0,0,0,0,0,0,0,":",0,0,"=",0,0,
470 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
471 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
472 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
473 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
474 0,0,0,0,0,"...",0,0,0,0,0,0,0,0,0,0,
475 0,0,0,"\"","\"",0,0,0,0,0,0,0,0,0,0,0,
476 0,0,0,0,0,0,0,0,0,"(C)",0,0,0,0,"(R)",0,
477 0,0,0,0,0,0,0,"/",0,0,0,0,0,0,0,0,
478 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
479 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
480 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
481 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
482 }, { /* G1 */
483 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
484 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
485 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
486 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
487 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
488 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
489 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
490 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
491 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
492 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
493 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
494 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
495 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
496 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
497 0,"(���֡�)",0,0,0,0,0,0,0,0,0,0,0,0,0,0,
498 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
499 }, { /* G2 */
500 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
501 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
502 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
503 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
504 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
505 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
506 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
507 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
508 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
509 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
510 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
511 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
512 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
513 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
514 0,0,0,0,"��",0,0,0,0,0,0,0,0,0,0,0,
515 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
516 }, { /* G3 */
517 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
518 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
519 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
520 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
521 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
522 0,0,"(P)",0,0,0,0,0,0,0,0,0,0,0,0,0,
523 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
524 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
525 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
526 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
527 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
528 0,0,0,0,0,0,0,0,0,0,"����",0,0,0,0,0,
529 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
530 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
531 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
532 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
533 }, { /* G4 */
534 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
535 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
536 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
537 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
538 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
539 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
540 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
541 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
542 0,0,0,0,0,0,0,0,0,"��",0,0,0,0,0,0,
543 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"��",
544 "��","��","��",0,0,0,0,0,0,0,0,0,0,0,0,0,
545 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
546 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
547 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
548 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
549 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
550 }, { /* G5 */
551 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
552 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
553 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
554 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
555 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
556 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
557 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
558 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
559 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
560 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
561 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
562 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
563 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
564 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
565 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
566 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
567 }, { /* G6 */
568 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
569 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
570 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
571 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
572 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
573 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
574 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
575 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
576 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
577 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
578 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
579 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
580 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
581 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
582 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
583 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
584 }, { /* G99 */
585 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
586 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
587 0,0,0,"#",0,0,0,0,0,0,0,0,0,0,0,"/",
588 0,0,0,0,0,0,0,0,0,0,0,0,"<",0,">",0,
589 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
590 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
591 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
592 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
593 0,0,0,0,0,"...",0,0,0,0,0,0,0,0,0,0,
594 0,0,0,"\"","\"",0,0,0,0,0,0,0,0,0,0,0,
595 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
596 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
597 "A","A",0,0,0,0,0,0,"E","E",0,0,"I","I",0,0,
598 0,0,"O","O",0,0,0,0,0,"U","U",0,0,0,0,0,
599 "a","a",0,0,0,0,0,0,"e","e",0,0,"i","i",0,0,
600 0,0,"o","o",0,0,0,0,0,"u","u",0,0,"y",0,0
601 }
602 };
603
604 /*
605 * �ǥե���ȳ���
606 */
607 uchr *gdeftbl[8] = { 0, 0, 0, 0, 0, 0 ,"��", 0 };
608
609 typedef struct _alttbl_t {
610 uchr org[4]; /* ��ɽ�� */
611 uchr *alt; /* ����ɽ�� */
612 } ALTTBL;
613
614 #define ATBLINC 10 /* ����ɽ��ɽ����ʬ */
615
616 uchr have_alt[256]; /* ����ʸ��������ɽ������Ĥ�? */
617 ALTTBL *alttbl = NULL; /* ����ɽ��ɽ */
618 int altnum = 0; /* ����ɽ��ɽ���ǿ� */
619 int in_alt = FALSE; /* ����ɽ������֤�? */
620
621 int curpos = 0; /* ���ߤ�ɽ������ */
622 int kcode = KC_DEF; /* �����ϴ��������� */
623 int jiskanji = 'B'; /* JIS���������ɤδ������� */
624 int jisalpha = 'B'; /* JIS���������ɤαѻ����� */
625 int width = 0; /* ���Ϲ��� */
626 int indent = 0; /* ����ǥ���� */
627 int kmode = FALSE; /* ü����JIS�����ؼ����֤�? */
628 int rawmode = FALSE; /* ����̵�ù��⡼�� */
629 int gfont = -1; /* �����ե�����ֹ� */
630 int hlink = FALSE; /* �ϥ��ѡ�������� */
631
632 int add_alt();
633 int init_alt();
634 uchr *get_gaiji();
635 int load_gaiji();
636 uchr *tosjis();
637 void outchar();
638 void outstr();
639 void set_indent();
640 void newline();
641
642 /*
643 * add_alt - ����ɽ��ɽ������ɽ�����ɲä���
644 */
645 int
add_alt(org,alt)646 add_alt(org, alt)
647 uchr *org, *alt;
648 {
649 int i;
650 uchr *p;
651 static int atblsiz = 0;
652
653 for (i = 0; i < altnum; i++) {
654 if (!strcmp(alttbl[i].org, org)) {
655 p = strpool(alt);
656 if (p == NULL)
657 return ERR;
658 alttbl[i].alt = p;
659 return OK;
660 }
661 }
662 if (alttbl == NULL) {
663 atblsiz = ATBLINC;
664 alttbl = (ALTTBL *)malloc(sizeof(ALTTBL) * atblsiz);
665 altnum = 0;
666 } else if (altnum >= atblsiz) {
667 atblsiz += ATBLINC;
668 alttbl = (ALTTBL *)realloc(alttbl, sizeof(ALTTBL) * atblsiz);
669 }
670 if (alttbl == NULL)
671 return ERR;
672 p = strpool(alt);
673 if (p == NULL)
674 return ERR;
675 strcpy(alttbl[altnum].org, org);
676 alttbl[altnum].alt = p;
677 have_alt[*org] = TRUE;
678 altnum++;
679 return OK;
680 }
681
682 /*
683 * init_alt - ����ɽ��ɽ�˥ǥե��������ɽ������Ͽ����
684 */
685 int
init_alt()686 init_alt()
687 {
688 int err;
689
690 err = 0;
691 if (add_alt("\xa5", "/") == ERR) /* 1�Х��ȥ��ʤ����� */
692 err++;
693 return err;
694 }
695
696 /*
697 * get_gaiji - �ե�����ֹ�ȥ����ɤ��鳰��ʸ������֤�
698 */
699 uchr *
get_gaiji(g,ch)700 get_gaiji(g, ch)
701 int g, ch;
702 {
703 uchr *p;
704
705 if (g == 9)
706 g = 7;
707 p = gaijitbl[g][ch];
708 if (p == NULL)
709 p = gdeftbl[g];
710 return p;
711 }
712
713 /*
714 * load_gaiji - ��������ɽ���ե�������ɤ�dz�������Ͽ����
715 */
716 int
load_gaiji(file)717 load_gaiji(file)
718 uchr *file;
719 {
720 int c, ch, g, isalt;
721 int line, err;
722 uchr *p, *q;
723 FILE *fp;
724 uchr org[4], buf[256], tmp[256];
725
726 if ((fp = fopen(file, "r")) == NULL) {
727 outstr("ERR: ��������ɽ���ե����뤬�����ץ�Ǥ��ޤ���\n");
728 return ERR;
729 }
730 line = 0;
731 err = 0;
732 while (fgets(buf, 256, fp) != NULL) {
733 line++;
734 buf[strlen(buf) - 1] = '\0';
735 p = skipsp(buf);
736 if (*p == '\0' || *p == ';')
737 continue;
738 if (*p == '"') { /* ����ɽ�� */
739 p++;
740 if (*p == '\\') {
741 p = escchar(p, &c);
742 org[0] = c;
743 org[1] = '\0';
744 } else if (SJIS1(*p)) {
745 org[0] = *p++;
746 org[1] = *p++;
747 org[2] = '\0';
748 } else {
749 org[0] = *p++;
750 org[1] = '\0';
751 }
752 if (*p != '"') {
753 sprintf(tmp,
754 "ERR: line %d: ���ظ�ʸ��������������ޤ���\n", line);
755 outstr(tmp);
756 err++;
757 continue;
758 }
759 p++;
760 isalt = TRUE;
761 } else { /* ������� */
762 if (*p != 'G' || !strchr("01234569", p[1]) || p[2] != '-' ||
763 !(isxdigit(p[3]) && isxdigit(p[4]) || p[3] == '*')) {
764 sprintf(tmp,
765 "ERR: line %d: �����ֹ椬����������ޤ���\n", line);
766 outstr(tmp);
767 err++;
768 continue;
769 }
770 g = p[1] - '0';
771 if (g == 9)
772 g = 7;
773 if (p[3] == '*') {
774 ch = -1;
775 p += 4;
776 } else {
777 ch = hexval(p + 3);
778 p += 5;
779 }
780 isalt = FALSE;
781 }
782 p = skipsp(p);
783 if (*p == '=')
784 p++;
785 p = skipsp(p);
786 if (*p != '"') {
787 sprintf(tmp,
788 "ERR: line %d: ����ɽ��ʸ��������������ޤ���\n", line);
789 outstr(tmp);
790 err++;
791 continue;
792 }
793 p++;
794 q = tmp;
795 while (*p && *p != '"') {
796 if (*p == '\\') {
797 p = escchar(p, &c);
798 if (c == '\\' || c == '<' || c == '>')
799 *q++ = '\\';
800 *q++ = c;
801 continue;
802 }
803 if (SJIS1(*p)) {
804 *q++ = *p++;
805 *q++ = *p++;
806 continue;
807 }
808 if (*p == '<' || *p == '>' || *p == '#')
809 *q++ = '\\';
810 *q++ = *p++;
811 }
812 if (*p != '"') {
813 sprintf(tmp,
814 "ERR: line %d: ����ɽ��ʸ��������������ޤ���\n", line);
815 outstr(tmp);
816 err++;
817 continue;
818 }
819 *q = '\0';
820 if (isalt) {
821 if (add_alt(org, tmp) == ERR) {
822 outstr("ERR: �������ޤ���\n");
823 err++;
824 }
825 continue;
826 }
827 if ((q = strpool(tmp)) == NULL) {
828 outstr("ERR: �������ޤ���\n");
829 err++;
830 continue;
831 }
832 if (ch >= 0)
833 gaijitbl[g][ch] = q;
834 else
835 gdeftbl[g] = q;
836 }
837 fclose(fp);
838 if (err)
839 return ERR;
840 return OK;
841 }
842
843 #ifdef MSDOS
844 uchr *
tosjis(str)845 tosjis(str)
846 uchr *str;
847 {
848 return str;
849 }
850
851 void
outchar(c)852 outchar(c)
853 int c;
854 {
855 if (c == 0)
856 return;
857 putchar(c);
858 }
859 #endif
860
861 #ifdef UNIX
862 /*
863 * tosjis - kcode��ɽ�����줿ʸ����ե�JIS���Ѵ�����
864 * (���ޥ�ɹ������Ѵ��˻Ȥ�)
865 */
866 uchr *
tosjis(str)867 tosjis(str)
868 uchr *str;
869 {
870 int c, c1;
871 int c2, km;
872 uchr *p;
873 static uchr buf[128];
874
875 if (kcode == KC_SJIS) {
876 strcpy((char *)buf, str);
877 return buf;
878 }
879 km = FALSE;
880 p = buf;
881 c1 = 0;
882 while ((c = *str++) != '\0') {
883 if (c1 != 0) {
884 c1 &= 0x7f;
885 c &= 0x7f;
886 if ((c1 & 0x01) == 0) {
887 c += 0x7e;
888 } else {
889 c += 0x1f;
890 if (c > 0x7e)
891 c++;
892 }
893 c1 = (c1 + 0xe1) >> 1;
894 if (c1 > 0x9f)
895 c1 += 0x40;
896 *p++ = c1;
897 *p++ = c;
898 c1 = 0;
899 continue;
900 }
901 if (c == '\033') {
902 c1 = *str++;
903 c2 = *str++;
904 if (c1 == '$' && (c2 == '@' || c2 == 'B'))
905 km = TRUE;
906 else if (c1 == '(' && (c2 == 'J' || c2 == 'B'))
907 km = FALSE;
908 continue;
909 }
910 if ((c & 0x80) || km == 1) {
911 c1 = c;
912 continue;
913 }
914 *p++ = c;
915 }
916 *p = '\0';
917 return buf;
918 }
919
920 /*
921 * outchar - ���ե�JIS��ʸ����������ܤ�������ʤ���
922 * kcode���Ѵ����ƽ��Ϥ���
923 * ������0�ʤ����¦�ξ��֤�ѻ����᤹(JIS�ΤȤ�ɬ��)
924 */
925 void
outchar(c)926 outchar(c)
927 int c;
928 {
929 static int knj1 = 0;
930
931 if (c == '\0') {
932 if (kmode) {
933 printf("\033(%c", jisalpha);
934 kmode = FALSE;
935 }
936 return;
937 }
938 if (knj1 != 0) {
939 if (kcode == KC_JIS && !kmode) {
940 printf("\033$%c", jiskanji);
941 kmode = TRUE;
942 }
943 if (kcode == KC_JIS || kcode == KC_EUC) {
944 if (knj1 > 0x9f)
945 knj1 -= 0x40;
946 knj1 += knj1;
947 if (c <= 0x9e) {
948 knj1 -= 0xe1;
949 if (c >= 0x80)
950 c -= 1;
951 c -= 0x1f;
952 } else {
953 knj1 -= 0xe0;
954 c -= 0x7e;
955 }
956 if (kcode == KC_EUC) {
957 knj1 += 0x80;
958 c += 0x80;
959 }
960 }
961 putchar(knj1);
962 putchar(c);
963 knj1 = 0;
964 return;
965 }
966 if (c >= 0x81) {
967 knj1 = c;
968 return;
969 }
970 if (kmode) {
971 printf("\033(%c", jisalpha);
972 kmode = FALSE;
973 }
974 putchar(c);
975 }
976 #endif
977
978 /*
979 * outstr - �������Ѵ����ʤ���outchar��ͳ��ʸ������Ϥ���
980 */
981 void
outstr(p)982 outstr(p)
983 uchr *p;
984 {
985 int i, g;
986 uchr *gstr;
987 uchr tmp[16];
988
989 while (*p) {
990 if (rawmode) {
991 outchar(*p++);
992 continue;
993 }
994 if (*p == '<') {
995 if (p[1] == '/') {
996 if (p[2] == 'G' && isdigit(p[3]))
997 gfont = -1;
998 else if (!strncmp(p+2, "RUBY>", 5))
999 outstr(")");
1000 else if (!strncmp(p+2, "HL>", 3))
1001 hlink = FALSE;
1002 } else {
1003 if (p[1] == 'G')
1004 gfont = p[2] - '0';
1005 else if (!strncmp(p+1, "RUBY>", 5))
1006 outstr("(");
1007 else if (!strncmp(p+1, "HL>", 3))
1008 hlink = TRUE;
1009 }
1010 while (*p != '>')
1011 p++;
1012 p++;
1013 continue;
1014 }
1015 if (*p == ':' && hlink) {
1016 /*
1017 * �ϥ��ѡ��������Ρ�:�װʹߤϤ��٤�̵��
1018 */
1019 p++;
1020 while (*p && (*p != '<' || strncmp(p, "</HL>", 5))) {
1021 if (SJIS1(*p))
1022 p++;
1023 p++;
1024 }
1025 p += 5;
1026 hlink = FALSE;
1027 continue;
1028 }
1029 if (*p == '#' && p[3] == '#') {
1030 g = (gfont == -1)? 0: gfont;
1031 gstr = get_gaiji(g, hexval(p+1));
1032 if (gstr) {
1033 outstr(gstr);
1034 } else {
1035 sprintf(tmp, "\\<G%d-%c%c\\>", g, p[1], p[2]);
1036 outstr(tmp);
1037 }
1038 p += 4;
1039 continue;
1040 }
1041 if (!in_alt && have_alt[*p]) {
1042 for (i = 0; i < altnum; i++) {
1043 if (*p == alttbl[i].org[0] &&
1044 (!SJIS1(*p) || p[1] == alttbl[i].org[1]))
1045 break;
1046 }
1047 if (i < altnum) {
1048 in_alt = TRUE;
1049 outstr(alttbl[i].alt);
1050 in_alt = FALSE;
1051 if (SJIS1(*p))
1052 p++;
1053 p++;
1054 continue;
1055 }
1056 }
1057 if (*p == '\\')
1058 p++;
1059 if (SJIS1(*p)) {
1060 if (width > 0 && curpos >= width - 1)
1061 newline();
1062 outchar(*p++);
1063 outchar(*p++);
1064 curpos += 2;
1065 continue;
1066 }
1067 if (*p < ' ' || *p == 0x7f) {
1068 switch (*p) {
1069 case '\n':
1070 newline();
1071 p++;
1072 break;
1073 case '\t':
1074 if (width > 0 && ((curpos | 0x07) + 1) > width)
1075 newline();
1076 outchar(*p++);
1077 curpos = (curpos | 0x07) + 1;
1078 break;
1079 default:
1080 outchar(*p++);
1081 break;
1082 }
1083 continue;
1084 }
1085 if (width > 0 && curpos >= width && !strchr("!),.:;?]}", *p))
1086 newline();
1087 outchar(*p++);
1088 curpos++;
1089 }
1090 outchar(0);
1091 }
1092
1093 /*
1094 * set_indent - ����ǥ���̤����ꤹ��
1095 */
1096 void
set_indent(n)1097 set_indent(n)
1098 int n;
1099 {
1100 indent = n;
1101 }
1102
1103 /*
1104 * newline - ���Ԥ��������̤�������ǥ�Ȥ���
1105 */
1106 void
newline()1107 newline()
1108 {
1109 outchar('\n');
1110 for (curpos = 0; curpos < indent; curpos++)
1111 outchar(' ');
1112 }
1113
1114 /* -------------------- ɽ������ -------------------- */
1115
1116 /*
1117 * ��ʸ�ǡ�����Ƭ�Υ�٥�
1118 */
1119 #define LBL_ERROR (-1)
1120 #define LBL_CMT 0 /* ���(��ɽ��) */
1121 #define LBL_DR 1 /* ������ */
1122 #define LBL_EN 2 /* ������ */
1123 #define LBL_ENN 3 /* �����Ѱ�ɽ��???(��ɽ��) */
1124 #define LBL_ENV 4 /* ��ɽ�� */
1125 #define LBL_ENVN 5 /* ���֤��ɽ��??? */
1126 #define LBL_ET 6 /* ���??? */
1127 #define LBL_EV 7 /* Ʊ���� */
1128 #define LBL_EX 8 /* ���� */
1129 #define LBL_EXKEY 9 /* ���㸡������ */
1130 #define LBL_EXSUB 10 /* ���㥵�ֹ��� */
1131 #define LBL_F 11 /* IF�θ�???(tallith) */
1132 #define LBL_ID 12 /* ���� */
1133 #define LBL_IDSUB 13 /* ���祵�ֹ��� */
1134 #define LBL_IF 14 /* �Ե�§���� */
1135 #define LBL_IMG 15 /* ���ǥǡ���(��ɽ��) */
1136 #define LBL_J2E 16 /* ���ܸ줫�����(adsuki bean) */
1137 #define LBL_KEY 17 /* ��������(��ɽ��) */
1138 #define LBL_KEYEX 18 /* ���㸡������(��ɽ��) */
1139 #define LBL_KEYID 19 /* ���縡������(��ɽ��) */
1140 #define LBL_LOC 20 /* ��ʸ��Ǽ����(��ɽ��) */
1141 #define LBL_MEN 21 /* MN�θ�??? */
1142 #define LBL_MN 22 /* ��̣ */
1143 #define LBL_MNSUB 23 /* ��̣���ֹ��� */
1144 #define LBL_NB 24 /* �� */
1145 #define LBL_NBP 25 /* ɽ���� */
1146 #define LBL_NBPSUB 26 /* ɽ���ֹ��� */
1147 #define LBL_NBSUB 27 /* �ֹ��� */
1148 #define LBL_NSBUS 28 /* NBSUB�θ�???(and) */
1149 #define LBL_OT 29 /* ��졢ȯ����... */
1150 #define LBL_OTSUB 30 /* ��쥵�ֹ��� */
1151 #define LBL_PR 31 /* ȯ�� */
1152 #define LBL_PS 32 /* �ʻ� */
1153 #define LBL_RA 33 /* ???(��ɽ��) */
1154 #define LBL_RES 34 /* ???(��ɽ��) */
1155 #define LBL_RN 35 /* ???(��ɽ��) */
1156 #define LBL_SM 36 /* ???(��ɽ��) */
1157 #define LBL_SND 37 /* �����ǡ���(��ɽ��) */
1158 #define LBL_SRC 38 /* �츻 */
1159 #define LBL_SRCSUB 39 /* �츻���ֹ��� */
1160 #define LBL_TL 40 /* ʸ�������ȥ� */
1161 #define LBL_TLSUB 41 /* ʸ�������ȥ륵�ֹ��� */
1162 #define LBL_TQ 42 /* ��̣ʬ��(get�Ρ�I ��ͭ�����)*/
1163 #define LBL_TR 43 /* ��� */
1164 #define LBL_TRSUB 44 /* ��ե��ֹ��� */
1165 /*
1166 * �ʲ���csrd����������
1167 */
1168 #define LBL_ZEX 45 /* ���硦���㸡����̤����� */
1169 #define LBL_ZID 46 /* ���硦���㸡����̤����� */
1170
1171 typedef struct _lbltbl_t {
1172 int id; /* ��٥뼱��ID */
1173 int disp; /* ���Ϥ��뤫? */
1174 uchr *label; /* ��٥�ʸ���� */
1175 uchr *leader; /* ��Ƭ�˽��Ϥ���ʸ���� */
1176 } LBLTBL;
1177
1178 /*
1179 * ��٥�ɽ
1180 */
1181 LBLTBL labeltbl[] = {
1182 { LBL_CMT, FALSE, "CMT", "" },
1183 { LBL_DR, TRUE, "DR", "" },
1184 { LBL_EN, TRUE, "EN", "�� " },
1185 { LBL_ENN, FALSE, "ENN", "" },
1186 { LBL_ENV, TRUE, "ENV", "" },
1187 { LBL_ENVN, TRUE, "ENVN", "" },
1188 { LBL_ET, TRUE, "ET", "" },
1189 { LBL_EV, TRUE, "EV", "" },
1190 { LBL_EX, TRUE, "EX", " " },
1191 { LBL_EXKEY, FALSE, "EXKEY", "" },
1192 { LBL_EXSUB, TRUE, "EXSUB", "" },
1193 { LBL_F, TRUE, "F", "" },
1194 { LBL_ID, TRUE, "ID", "" },
1195 { LBL_IDSUB, TRUE, "IDSUB", "" },
1196 { LBL_IF, TRUE, "IF", "" },
1197 { LBL_IMG, FALSE, "IMG", "" },
1198 { LBL_J2E, TRUE, "J2E", "" },
1199 { LBL_KEY, FALSE, "KEY", "" },
1200 { LBL_KEYEX, FALSE, "KEYEX", "" },
1201 { LBL_KEYID, FALSE, "KEYID", "" },
1202 { LBL_LOC, FALSE, "LOC", "" },
1203 { LBL_MEN, TRUE, "MEN", "" },
1204 { LBL_MN, TRUE, "MN", "" },
1205 { LBL_MNSUB, TRUE, "MNSUB", "" },
1206 { LBL_NB, TRUE, "NB", "" },
1207 { LBL_NBP, TRUE, "NBP", " " },
1208 { LBL_NBPSUB, TRUE, "NBPSUB", "" },
1209 { LBL_NBSUB, TRUE, "NBSUB", "" },
1210 { LBL_NSBUS, TRUE, "NSBUS", "" },
1211 { LBL_OT, TRUE, "OT", "" },
1212 { LBL_OTSUB, TRUE, "OTSUB", "" },
1213 { LBL_PR, TRUE, "PR", " " },
1214 { LBL_PS, TRUE, "PS", "\n��" },
1215 { LBL_RA, FALSE, "RA", "" },
1216 { LBL_RES, FALSE, "RES", "" },
1217 { LBL_RN, FALSE, "RN", "" },
1218 { LBL_SM, TRUE, "SM", "" },
1219 { LBL_SND, FALSE, "SND", "" },
1220 { LBL_SRC, TRUE, "SRC", "" },
1221 { LBL_SRCSUB, TRUE, "SRCSUB", "" },
1222 { LBL_TL, TRUE, "TL", "" },
1223 { LBL_TLSUB, TRUE, "TLSUB", "" },
1224 { LBL_TQ, TRUE, "TQ", "\n" },
1225 { LBL_TR, TRUE, "TR", "" },
1226 { LBL_TRSUB, TRUE, "TRSUB", "" },
1227 { LBL_ZEX, TRUE, "ZEX", "" },
1228 { LBL_ZID, TRUE, "ZID", "" },
1229 { LBL_ERROR, FALSE, NULL, NULL }
1230 };
1231
1232 LBLTBL *get_label();
1233 int load_format();
1234 int suppress();
1235
1236 /*
1237 * get_label - ��Ƭ�Υ�٥���̤���
1238 */
1239 LBLTBL *
get_label(str)1240 get_label(str)
1241 uchr *str;
1242 {
1243 uchr *p, *q;
1244 LBLTBL *lp;
1245 uchr tmp[128];
1246
1247 p = str;
1248 q = tmp;
1249 while (*p && isalpha(*p) && isupper(*p))
1250 *q++ = *p++;
1251 *q = '\0';
1252 if (!strncmp(str, "J2E", 3))
1253 strcpy(tmp, "J2E");
1254 lp = labeltbl;
1255 while (lp->label != NULL) {
1256 if (!strncmp(tmp, lp->label, strlen(tmp)))
1257 break;
1258 lp++;
1259 }
1260 if (lp->id == LBL_OT) {
1261 if (isdigit(str[2]) && str[3] == 'S')
1262 lp++; /* OT�������OTSUB��ؤ��褦�ˤ��� */
1263 }
1264 return lp;
1265 }
1266
1267 /*
1268 * load_format - �ե�������ɤ�ǥ�٥�ɽ������
1269 */
1270 int
load_format(file)1271 load_format(file)
1272 uchr *file;
1273 {
1274 int c, line, err, disp;
1275 uchr *p, *q;
1276 FILE *fp;
1277 LBLTBL *lp;
1278 uchr buf[256], tmp[256];
1279
1280 if ((fp = fopen(file, "r")) == NULL) {
1281 outstr("ERR: ɽ���ե����뤬�����ץ�Ǥ��ޤ���\n");
1282 return ERR;
1283 }
1284 line = 0;
1285 err = 0;
1286 while (fgets(buf, 256, fp) != NULL) {
1287 line++;
1288 buf[strlen(buf) - 1] = '\0';
1289 disp = TRUE;
1290 p = skipsp(buf);
1291 if (*p == '\0' || *p == ';')
1292 continue;
1293 if (*p == '!') {
1294 disp = FALSE;
1295 p++;
1296 }
1297 p = skipsp(p);
1298 lp = get_label(p);
1299 if (lp->id == LBL_ERROR) {
1300 sprintf(tmp,
1301 "ERR: line %d: ����̾������������ޤ���\n", line);
1302 outstr(tmp);
1303 err++;
1304 continue;
1305 }
1306 while (*p && (isupper(*p) || isdigit(*p)))
1307 p++;
1308 p = skipsp(p);
1309 if (*p == '=')
1310 p++;
1311 p = skipsp(p);
1312 if (*p != '"') {
1313 sprintf(tmp,
1314 "ERR: line %d: ��ʸ��������������ޤ���\n", line);
1315 outstr(tmp);
1316 err++;
1317 continue;
1318 }
1319 p++;
1320 q = tmp;
1321 while (*p && *p != '"') {
1322 if (*p == '\\') {
1323 p = escchar(p, &c);
1324 if (c == '\\' || c == '<' || c == '>')
1325 *q++ = '\\';
1326 *q++ = c;
1327 continue;
1328 }
1329 if (SJIS1(*p)) {
1330 *q++ = *p++;
1331 *q++ = *p++;
1332 continue;
1333 }
1334 if (*p == '<' || *p == '>' || *p == '#')
1335 *q++ = '\\';
1336 *q++ = *p++;
1337 }
1338 if (*p != '"') {
1339 sprintf(tmp,
1340 "ERR: line %d: ��ʸ��������������ޤ���\n", line);
1341 outstr(tmp);
1342 err++;
1343 continue;
1344 }
1345 *q = '\0';
1346 if ((q = strpool(tmp)) == NULL) {
1347 outstr("ERR: �������ޤ���\n");
1348 err++;
1349 continue;
1350 }
1351 lp->leader = q;
1352 lp->disp = disp;
1353 }
1354 fclose(fp);
1355 if (err)
1356 return ERR;
1357 return OK;
1358 }
1359
1360 /*
1361 * suppress - ʸ����ǻ��ꤵ�줿������ܤ�ɽ������������
1362 */
1363 int
suppress(str)1364 suppress(str)
1365 uchr *str;
1366 {
1367 if (str == NULL)
1368 return OK;
1369 if (!strcmp(str, "a"))
1370 str = "teio";
1371 while (*str) {
1372 switch (*str) {
1373 case 't':
1374 get_label("TR")->disp = FALSE;
1375 get_label("TRSUB")->disp = FALSE;
1376 break;
1377 case 'e':
1378 get_label("EX")->disp = FALSE;
1379 get_label("EXSUB")->disp = FALSE;
1380 get_label("ZEX")->disp = FALSE;
1381 break;
1382 case 'i':
1383 get_label("ID")->disp = FALSE;
1384 get_label("IDSUB")->disp = FALSE;
1385 get_label("ZID")->disp = FALSE;
1386 break;
1387 case 'o':
1388 get_label("OT")->disp = FALSE;
1389 get_label("OTSUB")->disp = FALSE;
1390 break;
1391 default:
1392 return ERR;
1393 }
1394 str++;
1395 }
1396 return OK;
1397 }
1398
1399 /* -------------------- ��ʸɽ�� -------------------- */
1400
1401 #define MAIN_DAT "main.txt" /* ��ʸ */
1402
1403 #define REV_LF 0xf5 /* 0x0a(LF)�Υӥå�ȿž */
1404
1405 #define LBUFSIZ 4096 /* �ԥХåե����Υ����� */
1406 /* ���С�������main.dat�κ��� */
1407 /* ��Ĺ��2389����;͵��ߤƤ��� */
1408
1409 /*
1410 * ȯ��������ϡ�{;�פȡ�2}�פǰϤޤ��(VEGA�б�����)
1411 */
1412 #define BP_LEADER '{' /* csrd¦����ȯ�������� */
1413 #define BP_START ';' /* ����ȯ�������� */
1414 #define BP_END '2' /* ����ȯ��������λ */
1415 #define BP_TRAILER '}' /* csrd¦����ȯ��������λ */
1416
1417 /*
1418 * BPTBL.gc�Υޥ���
1419 */
1420 #define CHMASK 0x00ff /* ʸ�������� */
1421 #define GRMASK 0x1f00 /* �ե���ȥ��롼�� */
1422 #define ITMASK 0x8000 /* ������å� */
1423
1424 #define GRNMASK 0x0f00 /* �ե���ȥ��롼���ֹ� */
1425 #define GRUNDEF 0x1000 /* �ե���ȥ��롼��̤��� */
1426
1427 #define GNUM(g) (((g) == 0x0900)? 99: ((g) >> 8))
1428
1429 typedef struct _bptbl_t {
1430 unt gc; /* �ե���ȥ��롼��+ʸ�������� */
1431 uchr bps[4]; /* ����ȯ�������� */
1432 } BPTBL;
1433
1434 /*
1435 * ����ȯ��������ɽ
1436 */
1437 BPTBL bptbl[] = {
1438 { 'a', "A" }, /* a */
1439 { 'b', "B" }, /* b */
1440 { 'd', "D" }, /* d */
1441 { 'e', "E" }, /* e */
1442 { 'f', "F" }, /* f */
1443 { 'g', "G" }, /* g */
1444 { 'h', "H" }, /* h */
1445 { 'i', "I" }, /* i */
1446 { 'j', "J" }, /* j */
1447 { 'k', "K" }, /* k */
1448 { 'l', "L" }, /* l */
1449 { 'm', "M" }, /* m */
1450 { 'n', "N" }, /* n */
1451 { 'o', "O" }, /* o */
1452 { 'p', "P" }, /* p */
1453 { 'r', "R" }, /* r */
1454 { 's', "S" }, /* s */
1455 { 't', "T" }, /* t */
1456 { 'u', "U" }, /* u */
1457 { 'v', "V" }, /* v */
1458 { 'w', "W" }, /* w */
1459 { 'z', "Z" }, /* z */
1460 { 0x00e0, "^A" }, /* a�� */
1461 { 0x00e1, "_A" }, /* a�� */
1462 { 0x00e6, "%" }, /* ae�� */
1463 { 0x00e8, "^E" }, /* e�� */
1464 { 0x00e9, "_E" }, /* e�� */
1465 { 0x00ec, "^I" }, /* i�� */
1466 { 0x00ed, "_I" }, /* i�� */
1467 { 0x00f0, "\\]" }, /* the��th */
1468 { 0x00f2, "^O" }, /* o�� */
1469 { 0x00f3, "_O" }, /* o�� */
1470 { 0x00f9, "^U" }, /* u�� */
1471 { 0x00fa, "_U" }, /* u�� */
1472 { 0x0141, "*" }, /* �Ȥ����Τʤ�a */
1473 { 0x0142, "\\<" }, /* �������o */
1474 { 0x0143, "_+" }, /* �����Τʤ�A�ˡ� */
1475 { 0x014a, "_%" }, /* ae�ι���ˡ� */
1476 { 0x014b, "^%" }, /* ae�ι���ˡ� */
1477 { 0x014c, "_5" }, /* �����ޤ��첻�ˡ� */
1478 { 0x014d, "^5" }, /* �����ޤ��첻�ˡ� */
1479 { 0x014e, "_*" }, /* �Ȥ����Τʤ�a�ˡ� */
1480 { 0x014f, "^*" }, /* �Ȥ����Τʤ�a�ˡ� */
1481 { 0x0150, ":" }, /* she��sh */
1482 { 0x0151, "_\\<" }, /* ���γ�����o�ˡ� */
1483 { 0x0152, "^\\<" }, /* ���γ�����o�ˡ� */
1484 { 0x0153, "_\\>" }, /* �ݤ�E�ˡ� */
1485 { 0x0154, "^\\>" }, /* �ݤ�E�ˡ� */
1486 { 0x016a, "?" }, /* through��th */
1487 { 0x0173, "!" }, /* ���餫��g */
1488 { 0x0176, "$" }, /* sing��ng */
1489 { 0x02e1, "^+" }, /* �����Τʤ�A�ˡ� */
1490 { 0x03b9, "5" }, /* �����ޤ��첻 */
1491 { 0x0477, "3" } /* ������ */
1492 };
1493
1494 #define NBPS (sizeof(bptbl) / sizeof(bptbl[0]))
1495
1496 int do_braille = FALSE; /* ����ȯ�������Ѵ���Ԥ���? */
1497 int nobracket = FALSE; /* ����ȯ���Ϥ�[]�������뤫? */
1498 int firstidiom; /* �����ʻ�κǽ������ */
1499 int have_en; /* ���Ф�ñ��ɽ��ľ��(̵���Ի�) */
1500 uchr line[LBUFSIZ]; /* 1�ԥǡ���ɽ���Ѻ���ΰ� */
1501
1502 uchr *bpstr();
1503 uchr *bp_transfer();
1504 BFILE *dic_open();
1505 int dgetline();
1506 int showline();
1507 int show_data();
1508 int show_idiom();
1509 int show_header();
1510 int show_version();
1511
1512 /*
1513 * bpstr - ʸ�����б���������ȯ����������֤�
1514 */
1515 uchr *
bpstr(gc)1516 bpstr(gc)
1517 unt gc;
1518 {
1519 int i;
1520
1521 gc &= (GRNMASK|CHMASK); /* GRUNDEF��<G0>��Ʊ�� */
1522 for (i = 0; i < NBPS; i++) {
1523 if (gc == bptbl[i].gc)
1524 return bptbl[i].bps;
1525 }
1526 return NULL;
1527 }
1528
1529 /*
1530 * bp_transfer - ȯ����������������ߤ�
1531 * ��̤��Ϥ��줿�Хåե�����������ǽ���
1532 * �Ѵ����ʸ����Ǥϥ����γ��Ĥ����äƤ����ǽ������
1533 */
1534 uchr *
bp_transfer(line)1535 bp_transfer(line)
1536 uchr *line;
1537 {
1538 int level;
1539 unt c, it, nit, g, ng;
1540 uchr *p, *q;
1541 unt *u, *uu, *upto;
1542 static unt tmp[1024]; /* �Ѵ����ǡ����Ϻ���776�Х��� */
1543
1544 #ifdef DEBUG
1545 if (debug)
1546 printf("line in= \"%s\"\n", line);
1547 #endif
1548 /*
1549 * tmp�˥ե����/������å������դ�ʸ������Ȥ�Ω�Ƥ�
1550 */
1551 g = GRUNDEF;
1552 it = 0;
1553 p = line;
1554 u = tmp;
1555 while (*p) {
1556 if (*p == '<') {
1557 if (p[1] == 'G')
1558 g = (p[2] - '0') << 8;
1559 else if (p[1] == '/' && p[2] == 'G')
1560 g = GRUNDEF;
1561 else if (p[1] == 'I' && p[2] == '>')
1562 it = ITMASK;
1563 else if (p[1] == '/' && p[2] == 'I' && p[3] == '>')
1564 it = 0;
1565 else {
1566 while (*p != '>')
1567 *u++ = it | g | *p++;
1568 *u++ = it | g | *p++;
1569 continue;
1570 }
1571 while (*p++ != '>')
1572 ;
1573 continue;
1574 }
1575 if (SJIS1(*p)) {
1576 *u++ = it | g | *p++;
1577 *u++ = it | g | *p++;
1578 continue;
1579 }
1580 if (*p == '#' && p[3] == '#') {
1581 *u++ = it | g | hexval(p + 1);
1582 p += 4;
1583 continue;
1584 }
1585 *u++ = it | g | *p++;
1586 }
1587 *u = '\0';
1588 /*
1589 * ȯ����������ᤷ�ʤ���line����ľ��
1590 */
1591 g = GRUNDEF;
1592 it = 0;
1593 level = 0;
1594 p = line;
1595 u = tmp;
1596 upto = NULL;
1597 while (*u) {
1598 nit = *u & ITMASK;
1599 if (nit != it) {
1600 strcpy(p, nit? "<I>": "</I>");
1601 while (*p++ != '>')
1602 ;
1603 it = nit;
1604 }
1605 ng = *u & GRMASK;
1606 if (ng != g) {
1607 if (g != GRUNDEF) {
1608 sprintf(p, "</G%d>", GNUM(g));
1609 while (*p++ != '>')
1610 ;
1611 }
1612 if (ng != GRUNDEF) {
1613 sprintf(p, "<G%d>", GNUM(ng));
1614 while (*p++ != '>')
1615 ;
1616 }
1617 g = ng;
1618 }
1619 if ((*u & GRUNDEF) != 0 && SJIS1(*u & CHMASK)) {
1620 *p++ = (*u++ & CHMASK);
1621 *p++ = (*u++ & CHMASK);
1622 continue;
1623 }
1624 if (upto && u < upto)
1625 goto notrans;
1626 switch (*u & (GRNMASK|CHMASK)) {
1627 case '[':
1628 if (!nobracket)
1629 *p++ = *u & CHMASK;
1630 level++;
1631 break;
1632 case ']':
1633 if (!nobracket)
1634 *p++ = *u & CHMASK;
1635 if (level > 0)
1636 level--;
1637 break;
1638 default:
1639 if (level > 0 && bpstr(*u) != NULL) {
1640 uu = u;
1641 while (*uu && bpstr(*uu)) {
1642 uu++;
1643 }
1644 c = *uu & (GRMASK|CHMASK);
1645 if (c == ' ' || c == ';' || c == ',' ||
1646 c == '-' || c == ']' || c == '|') {
1647 if (g != GRUNDEF) {
1648 sprintf(p, "</G%d>", GNUM(g));
1649 while (*p++ != '>')
1650 ;
1651 g = GRUNDEF;
1652 }
1653 *p++ = BP_LEADER;
1654 *p++ = BP_START;
1655 while (u < uu) {
1656 q = bpstr(*u++);
1657 while (*q)
1658 *p++ = *q++;
1659 }
1660 *p++ = BP_END;
1661 *p++ = BP_TRAILER;
1662 u = uu;
1663 continue;
1664 }
1665 upto = uu;
1666 }
1667 notrans:
1668 ng = *u & GRMASK;
1669 c = *u & CHMASK;
1670 if (ng == GRUNDEF) {
1671 *p++ = c;
1672 } else if (ng != 0 ||
1673 c >= 0x80 || c == '/' || c == ':' || c == '=') {
1674 sprintf(p, "#%02x#", c);
1675 p += 4;
1676 } else {
1677 *p++ = c;
1678 }
1679 }
1680 u++;
1681 }
1682 *p = '\0';
1683 if (it) {
1684 strcpy(p, "</I>");
1685 p += 4;
1686 }
1687 if (g != GRUNDEF)
1688 sprintf(p, "</G%d>", GNUM(g));
1689 #ifdef DEBUG
1690 if (debug)
1691 printf("line out=\"%s\"\n", line);
1692 #endif
1693 return line;
1694 }
1695
1696 /*
1697 * dic_open - ����ǡ����ե������bopen()����
1698 */
1699 BFILE *
dic_open(dicdir,dicfile)1700 dic_open(dicdir, dicfile)
1701 uchr *dicdir, *dicfile;
1702 {
1703 BFILE *bfp;
1704 uchr buf[256];
1705
1706 sprintf(buf, "%s/%s", dicdir, dicfile);
1707 if ((bfp = bopen(buf)) == NULL) {
1708 outstr("ERR: ����ե����뤬�����ץ�Ǥ��ޤ���(");
1709 outstr(buf);
1710 outstr(")\n");
1711 return NULL;
1712 }
1713 return bfp;
1714 }
1715
1716 /*
1717 * dgetline - ��ʸ�ǡ�����1���ɤ߹��ߡ����Ԥ������Ĺ�����֤�
1718 */
1719 int
dgetline(bfp,buf)1720 dgetline(bfp, buf)
1721 BFILE *bfp;
1722 uchr *buf;
1723 {
1724 int c;
1725 uchr *p;
1726
1727 p = buf;
1728 while ((c = bgetc(bfp)) != REV_LF)
1729 *p++ = ~c;
1730 *p = '\0';
1731 return p - buf;
1732 }
1733
1734 /*
1735 * showline - 1�Ԥ���ʸ�ǡ�����ɽ������
1736 */
1737 int
showline(line)1738 showline(line)
1739 uchr *line;
1740 {
1741 uchr *p, *q;
1742 LBLTBL *lp;
1743
1744 if (rawmode) {
1745 outstr(line);
1746 outstr("\n");
1747 return OK;
1748 }
1749 lp = get_label(line);
1750 if (lp->id == LBL_ERROR) {
1751 #ifdef DEBUG
1752 if (debug) {
1753 outstr("ERR> ");
1754 outstr(line);
1755 outstr("\n");
1756 }
1757 #endif
1758 return ERR;
1759 }
1760 if (!lp->disp) {
1761 #ifdef DEBUG
1762 if (debug) {
1763 outstr("[");
1764 outstr(lp->label);
1765 outstr("*]\n");
1766 }
1767 #endif
1768 return OK;
1769 }
1770 if (have_en && lp->id != LBL_PR) {
1771 set_indent(0);
1772 outstr("\n");
1773 }
1774 have_en = FALSE;
1775 #ifdef DEBUG
1776 if (debug) {
1777 outstr("[");
1778 outstr(lp->label);
1779 outstr("] ");
1780 }
1781 #endif
1782 p = skipeq(line);
1783 if (*p == '\0')
1784 return OK;
1785 set_indent(nspaces(lp->leader));
1786 outstr(lp->leader);
1787 switch (lp->id) {
1788 case LBL_DR:
1789 q = p;
1790 while (*q && *q != '|')
1791 q++;
1792 *q = 0;
1793 outstr(p);
1794 set_indent(0);
1795 outstr("\n");
1796 break;
1797 case LBL_EN:
1798 q = p;
1799 while (*q && *q != '|')
1800 q++;
1801 *q = 0;
1802 outstr(p);
1803 have_en = TRUE;
1804 break;
1805 case LBL_ID:
1806 if (firstidiom) {
1807 outstr("\n�������\n");
1808 firstidiom = FALSE;
1809 }
1810 goto normal;
1811 case LBL_PS:
1812 firstidiom = TRUE;
1813 goto normal;
1814 case LBL_TRSUB:
1815 if (!strncmp(p, "��", 2)) {
1816 p += 2;
1817 while (*p && strncmp(p, "��", 2)) {
1818 if (SJIS1(*p))
1819 p++;
1820 p++;
1821 }
1822 if (*p)
1823 p += 2;
1824 }
1825 goto normal;
1826 case LBL_PR:
1827 case LBL_IF:
1828 if (do_braille)
1829 p = bp_transfer(p);
1830 goto normal;
1831 default:
1832 normal:
1833 outstr(p);
1834 set_indent(0);
1835 outstr("\n");
1836 break;
1837 }
1838 return OK;
1839 }
1840
1841 /*
1842 * show_data - ��ʸ�ǡ�����ɽ������
1843 */
1844 int
show_data(bfp,dpos,dlen)1845 show_data(bfp, dpos, dlen)
1846 BFILE *bfp;
1847 long dpos, dlen;
1848 {
1849 int len;
1850
1851 if (bseek(bfp, dpos) == ERR)
1852 return ERR;
1853 firstidiom = TRUE;
1854 while (dlen > 0L) {
1855 len = dgetline(bfp, line);
1856 showline(line);
1857 dlen -= (long)(len + 1);
1858 }
1859 return OK;
1860 }
1861
1862 /*
1863 * show_idiom - ���硦����θ�����̤�ɽ������
1864 */
1865 int
show_idiom(bfp,dpos)1866 show_idiom(bfp, dpos)
1867 BFILE *bfp;
1868 long dpos;
1869 {
1870 int idiom;
1871 long npos;
1872 uchr *p, *q;
1873 LBLTBL *lp;
1874 uchr ps[128], tr[32];
1875
1876 /*
1877 * ���Ȥǹ�����Ƭ�ޤ��̤�ɬ�פ�����Τ�
1878 * ���硦������ɤ߽Ф��������;͵��Ĥ���
1879 * �ե�������Ƭ���ˤ��ä���bseek()��
1880 * ���줫������ΰ��֤�bseek()����
1881 * �������뤳�Ȥ�read()�β�������餻��Ϥ�
1882 */
1883 npos = dpos - (BBUFSIZ - 512);
1884 if (npos < 0L)
1885 npos = 0;
1886 if (bseek(bfp, npos) == ERR)
1887 return ERR;
1888 if (bseek(bfp, dpos) == ERR)
1889 return ERR;
1890 dgetline(bfp, line);
1891 if (rawmode) {
1892 outstr(line);
1893 outstr("\n");
1894 return OK;
1895 }
1896 if (*line == 'I' || *line == 'D') {
1897 lp = get_label("ZID");
1898 idiom = TRUE;
1899 } else {
1900 lp = get_label("ZEX");
1901 idiom = FALSE;
1902 }
1903 if (!lp->disp)
1904 return OK;
1905 set_indent(nspaces(lp->leader));
1906 outstr(lp->leader);
1907 outstr(skipeq(line));
1908 /*
1909 * �ե�������Ƭ���̤ä�EN=��õ����
1910 * �������鸽�߰��֤ޤ�ɬ�פʾ�����ɤ��ɽ������
1911 */
1912 if (bseek(bfp, dpos) == ERR)
1913 return ERR;
1914 for (;;) {
1915 if ((bgetcbw(bfp) ^ 0xff) == '=' &&
1916 (bgetcbw(bfp) ^ 0xff) == 'N' &&
1917 (bgetcbw(bfp) ^ 0xff) == 'E' &&
1918 (bgetcbw(bfp) ^ 0xff) == '\n')
1919 break;
1920 }
1921 bgetc(bfp);
1922 outstr(" (");
1923 dgetline(bfp, line);
1924 p = q = skipeq(line);
1925 while (*q && *q != '|')
1926 q++;
1927 *q = '\0';
1928 outstr(p);
1929 *ps = *tr = '\0';
1930 while (btell(bfp) < dpos) {
1931 dgetline(bfp, line);
1932 if (!strncmp(line, "PS=", 3)) {
1933 strcpy(ps, line + 3);
1934 *tr = '\0';
1935 continue;
1936 }
1937 if (!idiom && !strncmp(line, "TR", 2)) {
1938 p = line + 2;
1939 if (*p == 'S') /* TRSUB */
1940 p += 3;
1941 q = tr;
1942 while (*p && *p != '=')
1943 *q++ = *p++;
1944 *q = '\0';
1945 }
1946 }
1947 if (*ps) {
1948 outstr(", ");
1949 outstr(ps);
1950 if (*tr)
1951 outstr(tr);
1952 if (idiom)
1953 outstr(" ����");
1954 }
1955 outstr(")");
1956 set_indent(0);
1957 outstr("\n");
1958 return OK;
1959 }
1960
1961 /*
1962 * show_header - EN�ˤ��븫�Ф��ǡ�����ɽ������
1963 */
1964 int
show_header(bfp,dpos)1965 show_header(bfp, dpos)
1966 BFILE *bfp;
1967 long dpos;
1968 {
1969 uchr *p, *q;
1970
1971 if (bseek(bfp, dpos) == ERR)
1972 return ERR;
1973 for (;;) {
1974 if (dgetline(bfp, line) <= 0)
1975 return ERR;
1976 if (get_label(line)->id != LBL_EN)
1977 continue;
1978 p = skipeq(line);
1979 q = p;
1980 while (*q && *q != '|')
1981 q++;
1982 *q = 0;
1983 outstr(p);
1984 break;
1985 }
1986 return OK;
1987 }
1988
1989 /*
1990 * show_version - ������ʸ�ǡ����ΥС�������ɽ������
1991 */
1992 int
show_version(dicdir)1993 show_version(dicdir)
1994 uchr *dicdir;
1995 {
1996 uchr *p;
1997 BFILE *bfp;
1998
1999 if ((bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
2000 return ERR;
2001 bseek(bfp, 0L);
2002 while (dgetline(bfp, line) > 0 && !strncmp(line, "CMT=", 4)) {
2003 if (rawmode) {
2004 outstr(line);
2005 outstr("\n");
2006 continue;
2007 }
2008 p = line + 4;
2009 if (!strncmp(p, "***", 3) ||
2010 !strncmp(p, "TITLE:", 6) ||
2011 !strncmp(p, "Based", 5) ||
2012 !strncmp(p, "DATE:", 5)) {
2013 outstr(p);
2014 outstr("\n");
2015 }
2016 }
2017 bclose(bfp);
2018 return OK;
2019 }
2020
2021 /* --------------- ���硦���㸡����ȥե�������� --------------- */
2022
2023 #define TT0SIZ 1000 /* ������̳�Ǽ��������ǿ� */
2024
2025 /*
2026 * �ǡ�����Ǽ��
2027 */
2028 #define TF_USE0 0 /* ����0������Ȥ� */
2029 #define TF_USE01 1 /* ����0�ȥե�����1��Ȥ� */
2030 #define TF_USE1 2 /* �ե�����1������Ȥ� */
2031 #define TF_USE12 3 /* �ե�����1�ȥե�����2��Ȥ� */
2032
2033 typedef struct _tftbl {
2034 long val; /* �ǡ����� */
2035 long num; /* �ǡ����� */
2036 int n0; /* tt0�Υǡ����� */
2037 int n0x; /* tt0�����㳫�ϰ���/���߰��� */
2038 long n1; /* tf1�Υǡ����� */
2039 long n1x; /* tf1�θ��߰��� */
2040 long n2; /* tf2�Υǡ����� */
2041 long n2x; /* tf2�θ��߰��� */
2042 long *tt0; /* �������0 */
2043 uchr *tf1; /* ��ȥե�����1 */
2044 uchr *tf2; /* ��ȥե�����2 */
2045 FILE *fp1; /* ��ȥե�����1 FP */
2046 FILE *fp2; /* ��ȥե�����2 FP */
2047 long v1; /* tf1�����ɤߥǡ��� */
2048 long v2; /* tf2�����ɤߥǡ��� */
2049 int use; /* �ǡ�����Ǽ�� */
2050 int open; /* �����ץ�Ƥ��뤫? */
2051 } TFTBL;
2052
2053 TFTBL *tftbl; /* ��ȥե��������ɽ */
2054
2055 int setuptmp();
2056 void cleantmp();
2057 int twopen();
2058 int twrite();
2059 int twclose();
2060 int tropen();
2061 long tread();
2062 int trclose();
2063 int tisopen();
2064 void tswap();
2065 void tfree();
2066 int lcmp();
2067 long getlval();
2068
2069 /*
2070 * setuptmp - ��ȥե��������ɽ���������
2071 */
2072 int
setuptmp()2073 setuptmp()
2074 {
2075 int n, len;
2076 uchr *tmpdir;
2077 TFTBL *tp;
2078 uchr tmp[128];
2079
2080 tftbl = (TFTBL *)malloc(sizeof(TFTBL) * 3);
2081 if (tftbl == NULL)
2082 return ERR;
2083 len = 0;
2084 if ((tmpdir = getenv("TMP")) != NULL ||
2085 (tmpdir = getenv("TEMP")) != NULL) {
2086 strcpy(tmp, tmpdir);
2087 bsltosl(tmp);
2088 len = strlen(tmp);
2089 if (tmp[len-1] != '/')
2090 tmp[len++] = '/';
2091 }
2092 tp = tftbl;
2093 for (n = 0; n < 3; n++) {
2094 sprintf(tmp+len, "cs%d1XXXXXX", n);
2095 tp->tf1 = strpool(tmp);
2096 sprintf(tmp+len, "cs%d2XXXXXX", n);
2097 tp->tf2 = strpool(tmp);
2098 if (tp->tf1 == NULL || tp->tf2 == NULL)
2099 return ERR;
2100 mktemp(tp->tf1);
2101 mktemp(tp->tf2);
2102 tp->fp1 = NULL;
2103 tp->fp2 = NULL;
2104 tp->open = FALSE;
2105 tp++;
2106 }
2107 tp = tftbl;
2108 for (n = 0; n < 3; n++) {
2109 tp->tt0 = (long *)malloc(TT0SIZ * sizeof(long));
2110 /*
2111 * ��������ʤ�tt0�ϻȤ��ʤ��Ƥ�褤
2112 * ���顼�ˤϤ��ʤ�
2113 */
2114 tp++;
2115 }
2116 #ifdef DEBUG
2117 if (debug2) {
2118 tp = tftbl;
2119 for (n = 0; n < 3; n++) {
2120 printf("tftbl[%d]:\n", n);
2121 printf(" tt0=%s\n", tp->tt0? "(in use)": "(not in use)");
2122 printf(" tf1=%s\n", tp->tf1);
2123 printf(" tf2=%s\n", tp->tf2);
2124 tp++;
2125 }
2126 }
2127 #endif
2128 return OK;
2129 }
2130
2131 /*
2132 * cleantmp - �ĤäƤ��뤫�⤷��ʤ���ȥե������������
2133 */
2134 void
cleantmp()2135 cleantmp()
2136 {
2137 int n;
2138 TFTBL *tp;
2139
2140 if (tftbl == NULL)
2141 return;
2142 tp = tftbl;
2143 for (n = 0; n < 3; n++) {
2144 if (tp->fp1 != NULL) {
2145 fclose(tp->fp1);
2146 tp->fp1 = NULL;
2147 }
2148 if (tp->fp2 != NULL) {
2149 fclose(tp->fp2);
2150 tp->fp2 = NULL;
2151 }
2152 unlink(tp->tf1);
2153 unlink(tp->tf2);
2154 tp->open = FALSE;
2155 tp++;
2156 }
2157 }
2158
2159 /*
2160 * twopen - ��ȥե����륻�åȤ�Ф������ץ�
2161 */
2162 int
twopen(tn)2163 twopen(tn)
2164 int tn;
2165 {
2166 TFTBL *tp;
2167
2168 tp = &tftbl[tn];
2169 tp->val = 0L;
2170 tp->num = 0L;
2171 tp->n0 = tp->n0x = 0;
2172 tp->n1 = tp->n1x = 0L;
2173 tp->n2 = tp->n2x = 0L;
2174 tp->fp1 = tp->fp2 = NULL;
2175 tp->use = (tp->tt0 != NULL)? TF_USE0: TF_USE1;
2176 tp->open = TRUE;
2177 return OK;
2178 }
2179
2180 /*
2181 * twrite - ��ȥե����륻�åȤ˽Ф�
2182 */
2183 int
twrite(tn,val)2184 twrite(tn, val)
2185 int tn;
2186 long val;
2187 {
2188 int n;
2189 TFTBL *tp;
2190
2191 tp = &tftbl[tn];
2192 switch (tp->use) {
2193 case TF_USE0:
2194 if (tp->n0 >= TT0SIZ) {
2195 /*
2196 * ����˼��ޤ�ʤ��ä��Τ�tf1��Ȥ�
2197 */
2198 if ((tp->fp1 = fopen(tp->tf1, "w")) == NULL)
2199 goto openerr;
2200 #ifdef DEBUG
2201 if (debug2) {
2202 printf("twrite(%d): open tf1(%s), num=%ld, val=%07lx\n",
2203 tn, tp->tf1, tp->num, val);
2204 }
2205 #endif
2206 for (n = tp->n0x; n < TT0SIZ; n++) {
2207 if (fprintf(tp->fp1, "%07lx\n", tp->tt0[n]) == EOF)
2208 goto writeerr;
2209 }
2210 tp->n0 = tp->n0x;
2211 tp->n1 = (long)(TT0SIZ - tp->n0x);
2212 if (tp->n0x > 0) {
2213 /*
2214 * ����������������ζ��������ä�
2215 * ����ϻĤ����������tf1�˰�ư���줿
2216 * ���Ȥ�tt0���Ȥ���ɬ�פϤʤ�(n0x=0)
2217 */
2218 tp->n0x = 0;
2219 tp->use = TF_USE01;
2220 goto use01;
2221 }
2222 /*
2223 * ���Τ�tf1�˰�ư���줿
2224 */
2225 tp->use = TF_USE1;
2226 goto use1;
2227 }
2228 if (val < tp->val) {
2229 /*
2230 * ���������ζ������褿
2231 */
2232 tp->n0x = tp->n0;
2233 }
2234 tp->tt0[tp->n0++] = val;
2235 break;
2236 case TF_USE01:
2237 use01:
2238 if (val < tp->val)
2239 goto fmterr;
2240 goto use1write;
2241 case TF_USE1:
2242 use1:
2243 if (val < tp->val) {
2244 /*
2245 * ���������ζ������褿
2246 * �ʸ��tf2��Ȥ�
2247 */
2248 if ((tp->fp2 = fopen(tp->tf2, "w")) == NULL)
2249 goto openerr;
2250 #ifdef DEBUG
2251 if (debug2) {
2252 printf("twrite(%d): open tf2(%s), num=%ld, val=%07lx\n",
2253 tn, tp->tf1, tp->num, val);
2254 }
2255 #endif
2256 tp->use = TF_USE12;
2257 goto use12;
2258 }
2259 use1write:
2260 if (fprintf(tp->fp1, "%07lx\n", val) == EOF)
2261 goto writeerr;
2262 tp->n1++;
2263 break;
2264 case TF_USE12:
2265 if (val < tp->val)
2266 goto fmterr;
2267 use12:
2268 if (fprintf(tp->fp2, "%07lx\n", val) == EOF)
2269 return ERR;
2270 tp->n2++;
2271 break;
2272 }
2273 tp->val = val;
2274 tp->num++;
2275 return OK;
2276 openerr:
2277 outstr("ERR: ����ѥե����뤬���������Ǥ��ޤ���\n");
2278 return ERR;
2279 writeerr:
2280 outstr("ERR: �ǥ����������äѤ��Ǥ�\n");
2281 return ERR;
2282 fmterr:
2283 outstr("PANIC: ���硦���㥤��ǥå�����ͽ�����ʤ���¤�Ǥ�\n");
2284 #ifdef DEBUG
2285 if (debug2) {
2286 printf("num=%ld, cur=%lx, prev=%lx\n", tp->num, val, tp->val);
2287 }
2288 #endif
2289 return ERR;
2290 }
2291
2292 /*
2293 * twclose - ��ȥե����륻�åȤ�������
2294 */
2295 int
twclose(tn)2296 twclose(tn)
2297 int tn;
2298 {
2299 TFTBL *tp;
2300
2301 tp = &tftbl[tn];
2302 #ifdef DEBUG
2303 if (debug2) {
2304 printf("twclose(%d): n0=%d, n1=%ld, n2=%ld\n",
2305 tn, tp->n0, tp->n1, tp->n2);
2306 }
2307 #endif
2308 switch (tp->use) {
2309 case TF_USE0:
2310 if (tp->n0x > 0) {
2311 /*
2312 * tt0��������硦����ζ���������
2313 * tt0���Ȥ���ɬ�פ�����
2314 */
2315 qsort((uchr *)tp->tt0, (size_t)tp->n0, sizeof(long), lcmp);
2316 }
2317 break;
2318 case TF_USE12:
2319 /*
2320 * tf1�����硢tf2�����㤬����
2321 */
2322 if (fclose(tp->fp2) == EOF)
2323 goto closeerr;
2324 tp->fp2 = NULL;
2325 /* fall thru ... */
2326 case TF_USE01:
2327 /*
2328 * tt0�����硢tf1�����㤬����
2329 */
2330 /* fall thru ... */
2331 case TF_USE1:
2332 /*
2333 * tf1��������������Τ����줫�ޤ��Ϥ���ξ��������
2334 * �ͤ�ñĴ���ä��Ƥ���
2335 */
2336 if (fclose(tp->fp1) == EOF)
2337 goto closeerr;
2338 tp->fp1 = NULL;
2339 break;
2340 }
2341 tp->open = FALSE;
2342 return OK;
2343 closeerr:
2344 outstr("ERR: �ǥ����������äѤ��Ǥ�\n");
2345 return ERR;
2346 }
2347
2348 /*
2349 * tropen - ��ȥե����륻�åȤ��ɤ߹��ߥ����ץ�
2350 */
2351 int
tropen(tn)2352 tropen(tn)
2353 int tn;
2354 {
2355 TFTBL *tp;
2356
2357 tp = &tftbl[tn];
2358 switch (tp->use) {
2359 case TF_USE0:
2360 tp->n0x = 0;
2361 break;
2362 case TF_USE01:
2363 tp->n0x = 0;
2364 goto open1;
2365 case TF_USE12:
2366 if ((tp->fp2 = fopen(tp->tf2, "r")) == NULL)
2367 goto openerr;
2368 tp->v2 = getlval(tp->fp2);
2369 tp->n2x = 0L;
2370 /* fall thru ... */
2371 case TF_USE1:
2372 open1:
2373 if ((tp->fp1 = fopen(tp->tf1, "r")) == NULL)
2374 goto openerr;
2375 tp->v1 = getlval(tp->fp1);
2376 tp->n1x = 0L;
2377 break;
2378 }
2379 tp->open = TRUE;
2380 return OK;
2381 openerr:
2382 outstr("ERR: ����ѥե����뤬�����ץ�Ǥ��ޤ���\n");
2383 return ERR;
2384 }
2385
2386 /*
2387 * tread - ��ȥե����륻�åȤ����ɤ߹���
2388 */
2389 long
tread(tn)2390 tread(tn)
2391 int tn;
2392 {
2393 long v;
2394 TFTBL *tp;
2395
2396 tp = &tftbl[tn];
2397 switch (tp->use) {
2398 case TF_USE0:
2399 if (tp->n0x >= tp->n0)
2400 return EOF;
2401 v = tp->tt0[tp->n0x++];
2402 break;
2403 case TF_USE01:
2404 if (tp->n0x >= tp->n0) {
2405 if (tp->n1x >= tp->n1) {
2406 return EOF;
2407 } else {
2408 v = tp->v1;
2409 tp->v1 = getlval(tp->fp1);
2410 tp->n1x++;
2411 }
2412 } else {
2413 if (tp->n1x >= tp->n1) {
2414 v = tp->tt0[tp->n0x++];
2415 } else {
2416 if (tp->tt0[tp->n0x] < tp->v1) {
2417 v = tp->tt0[tp->n0x++];
2418 } else {
2419 v = tp->v1;
2420 tp->v1 = getlval(tp->fp1);
2421 tp->n1x++;
2422 }
2423 }
2424 }
2425 break;
2426 case TF_USE1:
2427 if (tp->n1x >= tp->n1)
2428 return EOF;
2429 v = tp->v1;
2430 tp->v1 = getlval(tp->fp1);
2431 tp->n1x++;
2432 break;
2433 case TF_USE12:
2434 if (tp->n1x >= tp->n1) {
2435 if (tp->n2x >= tp->n2) {
2436 return EOF;
2437 } else {
2438 v = tp->v2;
2439 tp->v2 = getlval(tp->fp2);
2440 tp->n2x++;
2441 }
2442 } else {
2443 if (tp->n2x >= tp->n2) {
2444 v = tp->v1;
2445 tp->v1 = getlval(tp->fp1);
2446 tp->n1x++;
2447 } else {
2448 if (tp->v1 < tp->v2) {
2449 v = tp->v1;
2450 tp->v1 = getlval(tp->fp1);
2451 tp->n1x++;
2452 } else {
2453 v = tp->v2;
2454 tp->v2 = getlval(tp->fp2);
2455 tp->n2x++;
2456 }
2457 }
2458 }
2459 break;
2460 }
2461 return v;
2462 }
2463
2464 /*
2465 * trclose - ��ȥե����륻�åȤ�������
2466 */
2467 int
trclose(tn)2468 trclose(tn)
2469 int tn;
2470 {
2471 TFTBL *tp;
2472
2473 tp = &tftbl[tn];
2474 switch (tp->use) {
2475 case TF_USE0:
2476 break;
2477 case TF_USE12:
2478 fclose(tp->fp2);
2479 tp->fp2 = NULL;
2480 /* fall thru ... */
2481 case TF_USE01:
2482 case TF_USE1:
2483 fclose(tp->fp1);
2484 tp->fp1 = NULL;
2485 break;
2486 }
2487 tp->open = FALSE;
2488 return OK;
2489 }
2490
2491 /*
2492 * tisopen - ��ȥե����륻�åȤϥ����ץ�Ƥ��뤫?
2493 */
2494 int
tisopen(tn)2495 tisopen(tn)
2496 int tn;
2497 {
2498 return tftbl[tn].open;
2499 }
2500
2501 /*
2502 * tswap - TFTBL�����
2503 */
2504 void
tswap(tn0,tn1)2505 tswap(tn0, tn1)
2506 int tn0, tn1;
2507 {
2508 TFTBL tftmp;
2509
2510 tftmp = tftbl[tn0];
2511 tftbl[tn0] = tftbl[tn1];
2512 tftbl[tn1] = tftmp;
2513 }
2514
2515 /*
2516 * tfree - tt0���������
2517 */
2518 void
tfree(tn)2519 tfree(tn)
2520 int tn;
2521 {
2522 TFTBL *tp;
2523
2524 if (tftbl == NULL)
2525 return;
2526 tp = &tftbl[tn];
2527 if (tp->tt0) {
2528 free((char *)tp->tt0);
2529 tp->tt0 = NULL;
2530 }
2531 }
2532
2533 /*
2534 * lcmp - long�ͤɤ��������(qsort��)
2535 */
2536 int
lcmp(p1,p2)2537 lcmp(p1, p2)
2538 long *p1, *p2;
2539 {
2540 if (*p1 < *p2)
2541 return -1;
2542 if (*p1 > *p2)
2543 return 1;
2544 return 0;
2545 }
2546
2547 /*
2548 * getlval - �ե����뤫��1�Ԥ�16��ʸ������ɤ��long�Ȥ����֤�
2549 */
2550 long
getlval(fp)2551 getlval(fp)
2552 FILE *fp;
2553 {
2554 uchr tmp[16];
2555 long l;
2556
2557 if (fgets(tmp, 16, fp) == NULL)
2558 return (long)EOF;
2559 sscanf(tmp, "%lx", &l);
2560 return l;
2561 }
2562
2563 /* -------------------- ���� -------------------- */
2564
2565 #define EN_SRCH 0 /* ��ñ�측�� */
2566 #define TR_SRCH 1 /* ���측�� */
2567 #define ID_SRCH 2 /* ���硦���㸡�� */
2568
2569 #define X_MATCH 0 /* ���������� */
2570 #define F_MATCH 1 /* ���������� */
2571 #define R_MATCH 2 /* ���������� */
2572 #define W_MATCH 3 /* �磻��ɥ����ɸ��� */
2573
2574 #define EN_F_IDX "enf.idx" /* ��ñ���������ץ���ǥå��� */
2575 #define EN_R_IDX "enr.idx" /* ��ñ��������ץ���ǥå��� */
2576 #define TR_F_IDX "trf.idx" /* �����������ץ���ǥå��� */
2577 #define TR_R_IDX "trr.idx" /* ����������ץ���ǥå��� */
2578 #define EN_LIST "en.dat" /* ��ñ��ꥹ�� */
2579 #define TR_LIST "tr.dat" /* ����ꥹ�� */
2580 #define ID_IDX "id.idx" /* ���硦���㥤��ǥå��� */
2581 #define ID_LIST "id.dat" /* ���硦����ꥹ�� */
2582 #define EN_M_LIST "miden.txt" /* ��ñ����ְ��ץꥹ�� */
2583 #define TR_M_LIST "midtr.txt" /* ������ְ��ץꥹ�� */
2584
2585 #define INODETOP 0x00000100L /* ����ǥå�������Ƭ�Ρ��ɰ��� */
2586 #define LENTTOP 0x00000100L /* �ꥹ�ȥ���ȥ����Ƭ���� */
2587
2588 /*
2589 * ����ǥå�������ȥ
2590 */
2591 typedef struct _ient {
2592 uchr lstpos[4]; /* �ꥹ�ȥե�������ΰ��� */
2593 uchr next[4]; /* ���̥Ρ��ɤΥΡ����ֹ� */
2594 } IENT;
2595
2596 /*
2597 * ����ǥå����Ρ���
2598 */
2599 typedef struct _inode {
2600 uchr pageno[4]; /* ���� */
2601 uchr last[4]; /* �Ρ�����κǽ�����ȥ�ֹ� */
2602 IENT ent[7]; /* �Ρ�����Υ���ȥ */
2603 } INODE;
2604
2605 /*
2606 * �ꥹ�ȥ���ȥ
2607 */
2608 typedef struct _lent {
2609 long dpos; /* ��ʸ�ǡ������� */
2610 long dlen; /* ��ʸ�ǡ��������� */
2611 uchr fkey[128]; /* �������ץ��� */
2612 uchr rkey[128]; /* �������ץ��� */
2613 uchr kstr[256]; /* ���Ф�ʸ���� */
2614 } LENT;
2615
2616 /*
2617 * ���硦����ꥹ�ȥ���ȥ
2618 */
2619 typedef struct _ilent {
2620 unt entid; /* ����ֹ� */
2621 unt nent; /* Ʊ��ñ��Ŀ�??? */
2622 long dpos; /* ��ʸ�ǡ������� */
2623 uchr idkey[24]; /* ����ñ��ʸ����(����17ʸ��) */
2624 } ILENT;
2625
2626 int stype = EN_SRCH; /* ��������(��ñ��/����/����) */
2627 int mtype = X_MATCH; /* �ޥå�(����/����/����/���) */
2628 uchr *idxfile = NULL; /* �����ѥ���ǥå����ե����� */
2629 uchr *lstfile = NULL; /* �����ѥꥹ�ȥե����� */
2630
2631 INODE inode; /* ����� */
2632 LENT lent; /* ����� */
2633 ILENT ilent; /* ����� */
2634
2635 long val();
2636 uchr *fmtkstr();
2637 void getlistkey();
2638 int getfirstent();
2639 void getlistent();
2640 int index_search();
2641 void getilistent();
2642 int idiom_search();
2643 uchr *mgetent();
2644 int match();
2645 int wild_search();
2646 uchr *getword();
2647 int search();
2648
2649 /*
2650 * val - ʸ�������4�Х����ͤ�long���Ѵ�
2651 */
2652 long
val(str)2653 val(str)
2654 register uchr *str;
2655 {
2656 return ((long)str[3] << 24) +
2657 ((long)str[2] << 16) +
2658 ((long)str[1] << 8) +
2659 (long)str[0];
2660 }
2661
2662 /*
2663 * fmtkstr - ���Ф�ʸ������Ρ�@�פ���Ԥ��Ѵ�����
2664 */
2665 uchr *
fmtkstr(kstr)2666 fmtkstr(kstr)
2667 uchr *kstr;
2668 {
2669 int in;
2670 uchr *p, *q;
2671 static uchr knew[256];
2672
2673 in = FALSE;
2674 p = kstr;
2675 q = knew;
2676 while (*p) {
2677 if (*p == '<') {
2678 if (!strncmp(p, "<G99>", 5))
2679 in = TRUE;
2680 else if (!strncmp(p, "</G99>", 6))
2681 in = FALSE;
2682 while (*p != '>')
2683 *q++ = *p++;
2684 *q++ = *p++;
2685 continue;
2686 }
2687 if (!in && *p == '@') {
2688 while (*p == '@')
2689 p++;
2690 *q++ = '\n';
2691 continue;
2692 }
2693 *q++ = *p++;
2694 }
2695 if (q > knew && q[-1] == '\n')
2696 q--;
2697 *q = '\0';
2698 return knew;
2699 }
2700
2701 /*
2702 * getlistkey - �ꥹ�ȥե����뤫�鸫�Ф�ʸ�������Ф�
2703 */
2704 void
getlistkey(lfp,buf,mtype)2705 getlistkey(lfp, buf, mtype)
2706 BFILE *lfp;
2707 uchr *buf;
2708 int mtype;
2709 {
2710 int len;
2711 uchr *p;
2712
2713 bgetc(lfp);
2714 bgetc(lfp);
2715 if (mtype == R_MATCH) {
2716 len = bgetc(lfp);
2717 while (len-- > 0)
2718 bgetc(lfp);
2719 bgetc(lfp);
2720 bgetc(lfp);
2721 }
2722 len = bgetc(lfp);
2723 p = buf;
2724 while (len-- > 0)
2725 *p++ = bgetc(lfp);
2726 *p = '\0';
2727 }
2728
2729 /*
2730 * getfirstent - �����˥ޥå�����ǽ�Υ���ȥ��õ��
2731 */
2732 int
getfirstent(ifp,lfp,word,ip,np)2733 getfirstent(ifp, lfp, word, ip, np)
2734 BFILE *ifp, *lfp;
2735 uchr *word;
2736 INODE *ip;
2737 int *np;
2738 {
2739 int i, n, last, len, cmp;
2740 long ipos, lpos, nodeno;
2741 uchr key[128];
2742 #ifdef DEBUG
2743 int first, level;
2744 #endif
2745
2746 ipos = INODETOP;
2747 #ifdef DEBUG
2748 level = 0;
2749 #endif
2750 /*
2751 * �ȥåס�����ʤν���
2752 */
2753 for (;;) {
2754 if (bseek(ifp, ipos) == ERR ||
2755 bread(ifp, (uchr *)ip, sizeof(INODE)) < sizeof(INODE)) {
2756 outstr("PANIC: ����ǥå����ե�����������礬����ޤ�\n");
2757 return ERR;
2758 }
2759 if (val(ip->ent[0].next) == 0L) {
2760 /*
2761 * �ǽ��ʢ��̤���ˡ�ǽ�������
2762 */
2763 break;
2764 }
2765 #ifdef DEBUG
2766 if (debug) {
2767 printf("<level %d>\n", ++level);
2768 printf("pageno=%08lx, last=%08lx\n",
2769 val(ip->pageno), val(ip->last));
2770 for (i = 0; i <= (int)val(ip->last); i++) {
2771 lpos = val(ip->ent[i].lstpos);
2772 printf("e[%d].lstpos=%08lx, e[%d].next=%08lx",
2773 i, lpos, i, val(ip->ent[i].next));
2774 if (lpos > 0L) {
2775 bseek(lfp, lpos);
2776 getlistkey(lfp, key, mtype);
2777 printf(", key=[%s]", key);
2778 }
2779 printf("\n");
2780 }
2781 }
2782 #endif
2783 last = (int)val(ip->last);
2784 for (i = 0; i < last; i++) {
2785 lpos = val(ip->ent[i+1].lstpos);
2786 if (bseek(lfp, lpos) == ERR) {
2787 outstr("PANIC: �ꥹ�ȥե�����������礬����ޤ�\n");
2788 return ERR;
2789 }
2790 getlistkey(lfp, key, mtype);
2791 #ifdef DEBUG
2792 if (debug)
2793 printf("i=%d, word=[%s], key=[%s]\n", i, word, key);
2794 #endif
2795 if (strcmp(word, key) <= 0)
2796 break;
2797 }
2798 nodeno = val(ip->ent[i].next);
2799 ipos = (nodeno - 1L) * sizeof(INODE) + INODETOP;
2800 #ifdef DEBUG
2801 if (debug)
2802 printf("nodeno=%08lx, ipos=%08lx\n", nodeno, ipos);
2803 #endif
2804 }
2805 /*
2806 * �ǽ��ʤν���
2807 */
2808 len = strlen(word);
2809 last = (int)val(ip->last);
2810 n = 0;
2811 #ifdef DEBUG
2812 first = TRUE;
2813 #endif
2814 for (;;) {
2815 if (n > last) {
2816 if (bread(ifp, (uchr *)ip, sizeof(INODE)) < sizeof(INODE) ||
2817 val(ip->pageno) == 0L) {
2818 /*
2819 * ����ǥå����ν�ü��ã����
2820 */
2821 return ERR;
2822 }
2823 last = (int)val(ip->last);
2824 n = 0;
2825 }
2826 #ifdef DEBUG
2827 if (debug && n == 0) {
2828 if (first) {
2829 printf("<last level>\n");
2830 first = FALSE;
2831 } else {
2832 printf("<next node>\n");
2833 }
2834 printf("pageno=%08lx, last=%08lx\n",
2835 val(ip->pageno), val(ip->last));
2836 for (i = 0; i <= (int)val(ip->last); i++) {
2837 lpos = val(ip->ent[i].lstpos);
2838 bseek(lfp, lpos);
2839 getlistkey(lfp, key, mtype);
2840 printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
2841 }
2842 }
2843 #endif
2844 lpos = val(ip->ent[n].lstpos);
2845 if (bseek(lfp, lpos) == ERR) {
2846 outstr("PANIC: �ꥹ�ȥե�����������礬����ޤ�\n");
2847 return ERR;
2848 }
2849 getlistkey(lfp, key, mtype);
2850 cmp = strncmp(word, key, len);
2851 if (cmp < 0)
2852 return ERR;
2853 if (cmp == 0) {
2854 if (mtype == X_MATCH && strcmp(word, key) != 0)
2855 return ERR;
2856 *np = n;
2857 return OK;
2858 }
2859 n++;
2860 }
2861 /* NOTREACHED */
2862 }
2863
2864 /*
2865 * getlistent - �ꥹ�ȥե����뤫�鸫�Ф��ǡ�������Ф�
2866 */
2867 void
getlistent(lfp,lp)2868 getlistent(lfp, lp)
2869 BFILE *lfp;
2870 LENT *lp;
2871 {
2872 int i, len;
2873 uchr *p;
2874
2875 bgetc(lfp);
2876 bgetc(lfp);
2877 len = bgetc(lfp);
2878 p = lp->fkey;
2879 while (len-- > 0)
2880 *p++ = bgetc(lfp);
2881 *p = '\0';
2882 bgetc(lfp);
2883 bgetc(lfp);
2884 len = bgetc(lfp);
2885 p = lp->rkey;
2886 while (len-- > 0)
2887 *p++ = bgetc(lfp);
2888 *p = '\0';
2889 bgetc(lfp);
2890 bgetc(lfp);
2891 lp->dpos = 0L;
2892 for (i = 0; i < 32; i += 8)
2893 lp->dpos |= ((long)bgetc(lfp) << i);
2894 bgetc(lfp);
2895 bgetc(lfp);
2896 lp->dlen = 0L;
2897 for (i = 0; i < 32; i += 8)
2898 lp->dlen |= ((long)bgetc(lfp) << i);
2899 for (i = 0; i < 8; i++)
2900 bgetc(lfp);
2901 len = bgetc(lfp);
2902 p = lp->kstr;
2903 while (len-- > 0)
2904 *p++ = bgetc(lfp);
2905 *p = '\0';
2906 }
2907
2908 /*
2909 * index_search - ����/����/�������פǥ���ǥå���������Ԥ�
2910 */
2911 int
index_search(dicdir,showall,word)2912 index_search(dicdir, showall, word)
2913 uchr *dicdir, *word;
2914 int showall;
2915 {
2916 int n, last, len, cmp, ret;
2917 long lpos;
2918 uchr *keyp;
2919 BFILE *ifp, *lfp, *bfp;
2920 #ifdef DEBUG
2921 int i;
2922 uchr key[128];
2923 #endif
2924
2925 ret = OK;
2926 ifp = lfp = bfp = NULL;
2927 if ((ifp = dic_open(dicdir, idxfile)) == NULL ||
2928 (lfp = dic_open(dicdir, lstfile)) == NULL)
2929 goto err;
2930 if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
2931 goto err;
2932 keyp = (mtype == R_MATCH)? lent.rkey: lent.fkey;
2933 len = strlen(word);
2934 last = (int)val(inode.last);
2935 for (;;) {
2936 if (n > last) {
2937 if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
2938 val(inode.pageno) == 0L) {
2939 /*
2940 * ����ǥå����ν�ü��ã����
2941 */
2942 break;
2943 }
2944 last = (int)val(inode.last);
2945 n = 0;
2946 }
2947 #ifdef DEBUG
2948 if (debug && n == 0) {
2949 printf("<next node>\n");
2950 printf("pageno=%08lx, last=%08lx\n",
2951 val(inode.pageno), val(inode.last));
2952 for (i = 0; i <= (int)val(inode.last); i++) {
2953 lpos = val(inode.ent[i].lstpos);
2954 bseek(lfp, lpos);
2955 getlistkey(lfp, key, mtype);
2956 printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
2957 }
2958 }
2959 #endif
2960 lpos = val(inode.ent[n].lstpos);
2961 if (bseek(lfp, lpos) == ERR) {
2962 outstr("PANIC: �ꥹ�ȥե�����������礬����ޤ�\n");
2963 goto err;
2964 }
2965 getlistent(lfp, &lent);
2966 cmp = strncmp(word, keyp, len);
2967 if (cmp < 0)
2968 break;
2969 if (cmp > 0) {
2970 n++;
2971 continue;
2972 }
2973 if (showall || mtype == X_MATCH) {
2974 if (mtype == X_MATCH && strcmp(word, keyp) != 0)
2975 break;
2976 if (bfp == NULL) {
2977 if ((bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
2978 goto err;
2979 } else {
2980 outstr("\n");
2981 }
2982 #ifdef DEBUG
2983 if (debug)
2984 printf("show_dat: key=[%s], dpos=%lx, dlen=%lx\n",
2985 lent.fkey, lent.dpos, lent.dlen);
2986 #endif
2987 if (show_data(bfp, lent.dpos, lent.dlen) == ERR)
2988 goto err;
2989 } else {
2990 outstr(fmtkstr(lent.kstr));
2991 if (stype == TR_SRCH) {
2992 outstr(" [");
2993 outstr(lent.fkey);
2994 outstr("]");
2995 }
2996 outstr("\n");
2997 }
2998 n++;
2999 }
3000 goto done;
3001 err:
3002 ret = ERR;
3003 done:
3004 if (ifp != NULL)
3005 bclose(ifp);
3006 if (lfp != NULL)
3007 bclose(lfp);
3008 if (bfp != NULL)
3009 bclose(bfp);
3010 return ret;
3011 }
3012
3013 /*
3014 * getilistent - ���硦����ꥹ�ȥե����뤫�鹽��ñ��ǡ�������Ф�
3015 */
3016 void
getilistent(lfp,ip)3017 getilistent(lfp, ip)
3018 BFILE *lfp;
3019 ILENT *ip;
3020 {
3021 int i, len;
3022 uchr *p;
3023
3024 ip->entid = bgetc(lfp);
3025 ip->entid += (bgetc(lfp) << 8);
3026 len = bgetc(lfp);
3027 p = ip->idkey;
3028 while (len-- > 0)
3029 *p++ = bgetc(lfp);
3030 *p = '\0';
3031 bgetc(lfp);
3032 bgetc(lfp);
3033 ip->dpos = 0L;
3034 for (i = 0; i < 32; i += 8)
3035 ip->dpos |= ((long)bgetc(lfp) << i);
3036 bgetc(lfp);
3037 bgetc(lfp);
3038 ip->nent = bgetc(lfp);
3039 ip->nent += (bgetc(lfp) << 8);
3040 for (i = 0; i < 4; i++)
3041 bgetc(lfp);
3042 }
3043
3044 /*
3045 * idiom_search - ���硦���㸡����Ԥ�
3046 */
3047 int
idiom_search(dicdir,ac,av)3048 idiom_search(dicdir, ac, av)
3049 uchr *dicdir;
3050 int ac;
3051 uchr **av;
3052 {
3053 int i, n, last, cmp, ret, nomore;
3054 long lpos, odpos;
3055 long l0, l1, num0, num1, num2, n0, n1;
3056 uchr *word;
3057 BFILE *ifp, *lfp, *bfp;
3058 #ifdef DEBUG
3059 uchr key[128];
3060 #endif
3061
3062 /*
3063 * ������
3064 */
3065 for (i = 0; i < ac; i++) {
3066 /*
3067 * ��������ʸ�������狼�ɤ���
3068 * ��˥����å��������Ƥ���
3069 */
3070 if (getword(tosjis(av[i])) == NULL)
3071 return ERR;
3072 }
3073 ret = OK;
3074 ifp = lfp = bfp = NULL;
3075 if ((ifp = dic_open(dicdir, idxfile)) == NULL ||
3076 (lfp = dic_open(dicdir, lstfile)) == NULL ||
3077 (bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
3078 goto err;
3079 if (setuptmp() == ERR) {
3080 outstr("ERR: �������ޤ���\n");
3081 goto err;
3082 }
3083 /*
3084 * �ǽ��ñ�������
3085 * ��̤Ϻ�ȥ��å�0�˽Ф�
3086 * �������Ǹ��ñ��ʤ餽�Τޤ�ɽ�����ƽ����
3087 */
3088 word = getword(tosjis(*av));
3089 if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
3090 goto err;
3091 #ifdef DEBUG
3092 if (debug2)
3093 printf("search1 start: word=%s\n", word);
3094 #endif
3095 nomore = (ac == 1);
3096 num0 = 0L;
3097 odpos = 0L;
3098 last = (int)val(inode.last);
3099 for (;;) {
3100 if (n > last) {
3101 if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
3102 val(inode.pageno) == 0L)
3103 break;
3104 last = (int)val(inode.last);
3105 n = 0;
3106 }
3107 #ifdef DEBUG
3108 if (debug && n == 0) {
3109 printf("<next node>\n");
3110 printf("pageno=%08lx, last=%08lx\n",
3111 val(inode.pageno), val(inode.last));
3112 for (i = 0; i <= (int)val(inode.last); i++) {
3113 lpos = val(inode.ent[i].lstpos);
3114 bseek(lfp, lpos);
3115 getlistkey(lfp, key, X_MATCH);
3116 printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
3117 }
3118 }
3119 #endif
3120 lpos = val(inode.ent[n].lstpos);
3121 if (bseek(lfp, lpos) == ERR) {
3122 outstr("PANIC: �ꥹ�ȥե�����������礬����ޤ�\n");
3123 goto err;
3124 }
3125 getilistent(lfp, &ilent);
3126 cmp = strcmp(word, ilent.idkey);
3127 if (cmp < 0)
3128 break;
3129 if (cmp > 0) {
3130 n++;
3131 continue;
3132 }
3133 #ifdef DEBUG
3134 if (debug)
3135 printf("idkey=[%s], entid=%04x, nent=%04x, dpos=%08lx\n",
3136 ilent.idkey, ilent.entid, ilent.nent, ilent.dpos);
3137 #endif
3138 if (ilent.dpos == odpos) {
3139 n++;
3140 continue;
3141 }
3142 odpos = ilent.dpos;
3143 if (nomore) {
3144 /*
3145 * ��������ʸ����1�Ĥ������ä�
3146 */
3147 #ifdef DEBUG
3148 if (debug2) {
3149 printf("dpos=%08lx\n", ilent.dpos);
3150 num0++;
3151 n++;
3152 continue;
3153 }
3154 #endif
3155 if (show_idiom(bfp, ilent.dpos) == ERR)
3156 goto err;
3157 num0++;
3158 n++;
3159 continue;
3160 }
3161 /*
3162 * ������̤�Ͽ����
3163 */
3164 if (!tisopen(0) && twopen(0) == ERR ||
3165 twrite(0, ilent.dpos) == ERR)
3166 goto err;
3167 num0++;
3168 n++;
3169 }
3170 #ifdef DEBUG
3171 if (debug2)
3172 printf("num0=%ld\n", num0);
3173 #endif
3174 if (tisopen(0) && twclose(0) == ERR)
3175 goto err;
3176 if (nomore || num0 == 0L) {
3177 /*
3178 * �Ǹ��ñ�줫����̤�0����ä�
3179 */
3180 goto done;
3181 }
3182 ac--, av++;
3183 /*
3184 * �Ĥ��ñ�������
3185 * ��̤Ϻ�ȥ��å�1�˽Ф�
3186 * ��ȥ��å�0�Ⱥ�ȥ��å�1���ͤ���碌��
3187 * ξ����¸�ߤ����Τ������ȥ��å�2�˽Ф�
3188 * ���뤤�ϺǸ��ñ��ʤ��̤�ɽ������
3189 */
3190 while (ac > 0) {
3191 word = getword(tosjis(*av));
3192 if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
3193 goto err;
3194 #ifdef DEBUG
3195 if (debug2)
3196 printf("search2 start: word=%s\n", word);
3197 #endif
3198 nomore = (ac == 1);
3199 num1 = 0L;
3200 odpos = 0L;
3201 last = (int)val(inode.last);
3202 for (;;) {
3203 if (n > last) {
3204 if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
3205 val(inode.pageno) == 0L)
3206 break;
3207 last = (int)val(inode.last);
3208 n = 0;
3209 }
3210 #ifdef DEBUG
3211 if (debug && n == 0) {
3212 printf("<next node>\n");
3213 printf("pageno=%08lx, last=%08lx\n",
3214 val(inode.pageno), val(inode.last));
3215 for (i = 0; i <= (int)val(inode.last); i++) {
3216 lpos = val(inode.ent[i].lstpos);
3217 bseek(lfp, lpos);
3218 getlistkey(lfp, key, X_MATCH);
3219 printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
3220 }
3221 }
3222 #endif
3223 lpos = val(inode.ent[n].lstpos);
3224 if (bseek(lfp, lpos) == ERR) {
3225 outstr("PANIC: �ꥹ�ȥե�����������礬����ޤ�\n");
3226 goto err;
3227 }
3228 getilistent(lfp, &ilent);
3229 cmp = strcmp(word, ilent.idkey);
3230 if (cmp < 0)
3231 break;
3232 if (cmp > 0) {
3233 n++;
3234 continue;
3235 }
3236 #ifdef DEBUG
3237 if (debug)
3238 printf("idkey=[%s], entid=%04x, nent=%04x, dpos=%08lx\n",
3239 ilent.idkey, ilent.entid, ilent.nent, ilent.dpos);
3240 #endif
3241 if (ilent.dpos == odpos) {
3242 n++;
3243 continue;
3244 }
3245 odpos = ilent.dpos;
3246 /*
3247 * ������̤�Ͽ����
3248 */
3249 if (!tisopen(1) && twopen(1) == ERR ||
3250 twrite(1, ilent.dpos) == ERR)
3251 goto err;
3252 num1++;
3253 n++;
3254 }
3255 #ifdef DEBUG
3256 if (debug2)
3257 printf("num1=%ld\n", num1);
3258 #endif
3259 if (tisopen(1) && twclose(1) == ERR)
3260 goto err;
3261 if (num1 == 0L) {
3262 /*
3263 * ��̤�0����ä�
3264 */
3265 goto done;
3266 }
3267 /*
3268 * ��ȥ��å�0�Ⱥ�ȥ��å�1���ͤ���碌��
3269 * ξ����¸�ߤ����Τ������ȥ��å�2�˽Ф�
3270 * ���η�̤��ʺ�ȥ��å�0�Ȥ���
3271 */
3272 if (tropen(0) == ERR || tropen(1) == ERR)
3273 goto err;
3274 #ifdef DEBUG
3275 if (debug2) {
3276 printf("compare start\n");
3277 }
3278 #endif
3279 n0 = n1 = num2 = 0L;
3280 l0 = tread(0);
3281 l1 = tread(1);
3282 for (;;) {
3283 if (l0 < l1) {
3284 if (++n0 >= num0)
3285 break;
3286 l0 = tread(0);
3287 continue;
3288 }
3289 if (l1 < l0) {
3290 if (++n1 >= num1)
3291 break;
3292 l1 = tread(1);
3293 continue;
3294 }
3295 if (nomore) {
3296 #ifdef DEBUG
3297 if (debug2) {
3298 printf("dpos=%08lx\n", l0);
3299 goto next;
3300 }
3301 #endif
3302 if (show_idiom(bfp, l0) == ERR)
3303 goto err;
3304 goto next;
3305 }
3306 /*
3307 * ������̤�Ͽ����
3308 */
3309 if (!tisopen(2) && twopen(2) == ERR ||
3310 twrite(2, l0) == ERR)
3311 goto err;
3312 next:
3313 if (++n0 >= num0)
3314 break;
3315 l0 = tread(0);
3316 if (++n1 >= num1)
3317 break;
3318 l1 = tread(1);
3319 num2++;
3320 }
3321 #ifdef DEBUG
3322 if (debug2)
3323 printf("num2=%ld\n", num2);
3324 #endif
3325 trclose(0);
3326 trclose(1);
3327 if (tisopen(2) && twclose(2) == ERR)
3328 goto err;
3329 if (num2 == 0L) {
3330 /*
3331 * ��̤�0����ä�
3332 */
3333 goto done;
3334 }
3335 tswap(0, 2);
3336 ac--, av++;
3337 }
3338 goto done;
3339 err:
3340 ret = ERR;
3341 done:
3342 tfree(0);
3343 tfree(1);
3344 tfree(2);
3345 if (ifp != NULL)
3346 bclose(ifp);
3347 if (lfp != NULL)
3348 bclose(lfp);
3349 if (bfp != NULL)
3350 bclose(bfp);
3351 #ifdef DEBUG
3352 if (debug2)
3353 return ret;
3354 #endif
3355 cleantmp();
3356 return ret;
3357 }
3358
3359 /*
3360 * mgetent - ��ְ��ץꥹ�Ȥ�1���ɤ߹���
3361 */
3362 uchr *
mgetent(bfp,buf)3363 mgetent(bfp, buf)
3364 BFILE *bfp;
3365 uchr *buf;
3366 {
3367 int c;
3368 uchr *p;
3369
3370 if ((c = bgetc(bfp)) == EOF)
3371 return NULL;
3372 p = buf;
3373 do {
3374 *p++ = c;
3375 } while ((c = bgetc(bfp)) != '\r');
3376 *p = '\0';
3377 return buf;
3378 }
3379
3380 /*
3381 * match - ʸ����ȥѥ�����ξȹ��Ԥ�
3382 */
3383 int
match(str,pat)3384 match(str, pat)
3385 uchr *str, *pat;
3386 {
3387 unt c;
3388 uchr *s, *p;
3389
3390 #ifdef DEBUG
3391 if (debug2) {
3392 printf("str=\"%s\", pat=\"%s\"\n", str, pat);
3393 }
3394 #endif
3395 if (stype == EN_SRCH) {
3396 /*
3397 * 1�Х���ʸ���ξȹ�
3398 */
3399 while (*str) {
3400 switch (*pat) {
3401 case '?':
3402 /*
3403 * Ǥ�դ�ʸ���ȥޥå�����
3404 */
3405 break;
3406 case '*':
3407 /*
3408 * str�λĤ��1ʸ�����ĸ��餷�ʤ���
3409 * pat�λĤ�ȥޥå����뤫�ɤ���Ĵ�٤�
3410 */
3411 s = str;
3412 do {
3413 if (match(s, pat + 1))
3414 return TRUE;
3415 } while (*s++);
3416 break;
3417 case '[':
3418 for (p = pat + 1; *p != ']'; p++) {
3419 if (*str == *p)
3420 break;
3421 if (p[1] == '-') {
3422 if (*str >= *p && *str <= p[2])
3423 break;
3424 p += 2;
3425 }
3426 }
3427 if (*p == ']') {
3428 /*
3429 * �ޥå����ʤ��ä�
3430 */
3431 return FALSE;
3432 }
3433 while (*p != ']')
3434 p++;
3435 pat = p;
3436 break;
3437 case '\0':
3438 /*
3439 * �ѥ�������˿Ԥ���
3440 */
3441 return FALSE;
3442 default:
3443 if (*str != *pat) {
3444 /*
3445 * �ޥå����ʤ��ä�
3446 */
3447 return FALSE;
3448 }
3449 break;
3450 }
3451 str++;
3452 pat++;
3453 }
3454 if (*pat == '\0' || (*pat == '*' && pat[1] == '\0')) {
3455 /*
3456 * �ե�����̾�ȥѥ�����Ʊ���˿Ԥ���
3457 */
3458 return TRUE;
3459 }
3460 return FALSE;
3461 }
3462 /*
3463 * 2�Х���ʸ���ξȹ�
3464 */
3465 while (*str) {
3466 switch (*pat) {
3467 case '?':
3468 /*
3469 * Ǥ�դ�ʸ���ȥޥå�����
3470 */
3471 str += 2;
3472 pat++;
3473 break;
3474 case '*':
3475 /*
3476 * str�λĤ��1ʸ�����ĸ��餷�ʤ���
3477 * pat�λĤ�ȥޥå����뤫�ɤ���Ĵ�٤�
3478 */
3479 s = str;
3480 for (;;) {
3481 if (match(s, pat + 1))
3482 return TRUE;
3483 if (*s == '\0')
3484 break;
3485 s += 2;
3486 }
3487 str += 2;
3488 pat++;
3489 break;
3490 case '[':
3491 p = pat + 1;
3492 while (*p != ']') {
3493 if (*str == *p && str[1] == p[1])
3494 break;
3495 if (p[2] == '-') {
3496 c = (*str << 8 | str[1]);
3497 if (c >= (p[0] << 8 | p[1]) &&
3498 c <= (p[3] << 8 | p[4]))
3499 break;
3500 p += 3;
3501 }
3502 p += 2;
3503 }
3504 if (*p == ']') {
3505 /*
3506 * �ޥå����ʤ��ä�
3507 */
3508 return FALSE;
3509 }
3510 while (*p != ']') {
3511 if (*p == '-') {
3512 p++;
3513 continue;
3514 }
3515 p += 2;
3516 }
3517 str += 2;
3518 pat = p + 1;
3519 break;
3520 case '\0':
3521 /*
3522 * �ѥ�������˿Ԥ���
3523 */
3524 return FALSE;
3525 default:
3526 if (*str != *pat || str[1] != pat[1]) {
3527 /*
3528 * �ޥå����ʤ��ä�
3529 */
3530 return FALSE;
3531 }
3532 str += 2;
3533 pat += 2;
3534 break;
3535 }
3536 }
3537 if (*pat == '\0' || (*pat == '*' && pat[1] == '\0')) {
3538 /*
3539 * �ե�����̾�ȥѥ�����Ʊ���˿Ԥ���
3540 */
3541 return TRUE;
3542 }
3543 return FALSE;
3544 }
3545
3546 /*
3547 * wild_search - �磻��ɥ����ɸ�����Ԥ�
3548 */
3549 int
wild_search(dicdir,showall,pat)3550 wild_search(dicdir, showall, pat)
3551 uchr *dicdir, *pat;
3552 int showall;
3553 {
3554 int ret, first;
3555 long dpos, dlen, odpos;
3556 uchr *p;
3557 BFILE *lfp, *bfp;
3558 uchr buf[256];
3559
3560 ret = OK;
3561 lfp = bfp = NULL;
3562 if ((lfp = dic_open(dicdir, lstfile)) == NULL ||
3563 (bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
3564 goto err;
3565 bseek(lfp, 0L);
3566 first = TRUE;
3567 odpos = 0L;
3568 while (mgetent(lfp, buf) != NULL) {
3569 p = buf;
3570 while (*p && *p != '0')
3571 p++;
3572 *p = '\0';
3573 if (!match(buf, pat))
3574 continue;
3575 sscanf(p+1, "%7lx%lx", &dpos, &dlen);
3576 #ifdef DEBUG
3577 if (debug)
3578 printf("show_dat: key=[%s], dpos=%lx, dlen=%lx\n",
3579 buf, dpos, dlen);
3580 #endif
3581 if (showall) {
3582 if (first)
3583 first = FALSE;
3584 else
3585 outstr("\n");
3586 if (dpos == odpos)
3587 continue;
3588 if (show_data(bfp, dpos, dlen) == ERR)
3589 goto err;
3590 } else {
3591 if (show_header(bfp, dpos) == ERR)
3592 goto err;
3593 if (stype == EN_SRCH)
3594 lower(buf);
3595 outstr(" [");
3596 outstr(buf);
3597 outstr("]\n");
3598 }
3599 }
3600 goto done;
3601 err:
3602 ret = ERR;
3603 done:
3604 if (lfp != NULL)
3605 bclose(lfp);
3606 if (bfp != NULL)
3607 bclose(bfp);
3608 return ret;
3609 }
3610
3611 /*
3612 * getword - ��������ʸ����������äƸ���ñ�줽��¾�������Ԥ�
3613 */
3614 uchr *
getword(str)3615 getword(str)
3616 uchr *str;
3617 {
3618 int n, len, err;
3619 int eng, knj, any, meta, bra;
3620 uchr *s, *p, *q, ch;
3621 static uchr buf[128];
3622
3623 len = strlen(str);
3624 eng = knj = any = meta = 0;
3625 err = 0;
3626 bra = FALSE;
3627 s = str;
3628 while (*s) {
3629 if (strchr("*?[]-", *s)) {
3630 if (*s == '*' && (bra || s[1] == '*') ||
3631 *s == '?' && bra ||
3632 *s == '[' && (bra || s[1] == '-' || s[1] == ']') ||
3633 *s == '-' && (!bra || s[1] == ']') ||
3634 *s == ']' && !bra) {
3635 err++;
3636 break;
3637 }
3638 if (*s == '[')
3639 bra = TRUE;
3640 else if (*s == ']')
3641 bra = FALSE;
3642 else if (*s == '?')
3643 any++;
3644 s++;
3645 meta++;
3646 continue;
3647 }
3648 if (SJIS1(*s)) {
3649 s += 2;
3650 knj++;
3651 continue;
3652 }
3653 s++;
3654 eng++;
3655 }
3656 if (err || bra) {
3657 outstr("ERR: �磻��ɥ����ɸ�������˸�꤬����ޤ�\n");
3658 return NULL;
3659 }
3660 if (knj > 0 && eng > 0) {
3661 outstr("ERR: ����ñ��˱Ѹ�����ܸ줬���ߤ��Ƥ��ޤ�\n");
3662 return NULL;
3663 }
3664 if (knj == 0 && eng == 0 && any == 0) {
3665 outstr("ERR: ����ñ�줬���Ǥ�\n");
3666 return NULL;
3667 }
3668 if (stype == ID_SRCH) {
3669 if (knj) {
3670 outstr("ERR: ���硦���㸡�������ܸ줬���ꤵ��Ƥ��ޤ�\n");
3671 return NULL;
3672 }
3673 if (mtype != X_MATCH) {
3674 outstr("ERR: ���硦���㸡�����Դ��������������ꤵ��Ƥ��ޤ�\n");
3675 return NULL;
3676 }
3677 }
3678 if (meta == 0)
3679 mtype = X_MATCH;
3680 else if (meta == 1 && str[len-1] == '*')
3681 mtype = F_MATCH;
3682 else if (meta == 1 && *str == '*')
3683 mtype = R_MATCH;
3684 else
3685 mtype = W_MATCH;
3686 if (knj)
3687 stype = TR_SRCH;
3688 s = str;
3689 p = buf;
3690 if (mtype == F_MATCH || mtype == R_MATCH) {
3691 len--;
3692 if (mtype == R_MATCH)
3693 s++;
3694 }
3695 n = len;
3696 while (n > 0) {
3697 if (SJIS1(*s)) {
3698 *p++ = *s++;
3699 *p++ = *s++;
3700 n -= 2;
3701 continue;
3702 }
3703 if (islower(*s)) {
3704 *p++ = toupper(*s);
3705 s++;
3706 } else {
3707 *p++ = *s++;
3708 }
3709 n--;
3710 }
3711 *p = '\0';
3712 if (mtype == R_MATCH) {
3713 /*
3714 * ����ʸ�����ս�ˤ���
3715 */
3716 p = buf;
3717 while (*p) {
3718 if (SJIS1(*p)) {
3719 ch = *p;
3720 *p = p[1];
3721 p[1] = ch;
3722 p++;
3723 }
3724 p++;
3725 }
3726 p = buf;
3727 q = buf + len - 1;
3728 while (p < q) {
3729 ch = *p;
3730 *p++ = *q;
3731 *q-- = ch;
3732 }
3733 }
3734 return buf;
3735 }
3736
3737 /*
3738 * search - �����Υᥤ��롼����
3739 */
3740 int
search(dicdir,showall,ac,av)3741 search(dicdir, showall, ac, av)
3742 uchr *dicdir;
3743 int showall;
3744 int ac;
3745 uchr **av;
3746 {
3747 int ret;
3748 uchr *word;
3749
3750 if ((word = getword(tosjis(*av))) == NULL)
3751 return ERR;
3752 switch (stype) {
3753 case EN_SRCH:
3754 switch (mtype) {
3755 case X_MATCH:
3756 case F_MATCH:
3757 idxfile = EN_F_IDX;
3758 lstfile = EN_LIST;
3759 ret = index_search(dicdir, showall, word);
3760 break;
3761 case R_MATCH:
3762 idxfile = EN_R_IDX;
3763 lstfile = EN_LIST;
3764 ret = index_search(dicdir, showall, word);
3765 break;
3766 case W_MATCH:
3767 lstfile = EN_M_LIST;
3768 ret = wild_search(dicdir, showall, word);
3769 break;
3770 }
3771 break;
3772 case TR_SRCH:
3773 switch (mtype) {
3774 case X_MATCH:
3775 case F_MATCH:
3776 idxfile = TR_F_IDX;
3777 lstfile = TR_LIST;
3778 ret = index_search(dicdir, showall, word);
3779 break;
3780 case R_MATCH:
3781 idxfile = TR_R_IDX;
3782 lstfile = TR_LIST;
3783 ret = index_search(dicdir, showall, word);
3784 break;
3785 case W_MATCH:
3786 lstfile = TR_M_LIST;
3787 ret = wild_search(dicdir, showall, word);
3788 break;
3789 }
3790 break;
3791 case ID_SRCH:
3792 idxfile = ID_IDX;
3793 lstfile = ID_LIST;
3794 ret = idiom_search(dicdir, ac, av);
3795 break;
3796 }
3797 return ret;
3798 }
3799
3800 /* -------------------- �ᥤ�� -------------------- */
3801
3802 #define CSRD_ENV "CSRD" /* ���ץ��������ꤹ��Ķ��ѿ� */
3803
3804 #define MINWIDTH 20 /* ���Ϲ����β��� */
3805
3806 int showall = FALSE; /* �Դ������פǤ����ɽ�����뤫?*/
3807 int showver = FALSE; /* ����ǡ����С������ɽ�� */
3808 uchr *dicdir = NULL; /* ����ǥ��쥯�ȥ */
3809 uchr *fmtfile = NULL; /* ɽ���ե����� */
3810 uchr *gaifile = NULL; /* ��������ɽ���ե����� */
3811 uchr *supstr = NULL; /* ɽ���������� */
3812
3813 void usage();
3814 int parse_opt();
3815 #if defined(UNIX) && defined(RC_PATH)
3816 int init_rcfile();
3817 #endif
3818 int init_env();
3819 void onintr();
3820 int main();
3821
3822 /*
3823 * usage - ����ˡ��ɽ�����ƽ�λ����
3824 */
3825 void
usage()3826 usage()
3827 {
3828 outstr("���شۥ�����ϥ����Ѹ켭ŵ�����桼�ƥ���ƥ��� Ver.");
3829 outstr(version);
3830 outstr(" (");
3831 outstr(date);
3832 outstr(")\n Written by Junn Ohta (ohta@src.ricoh.co.jp),");
3833 outstr(" Public Domain.\n\n����ˡ: ");
3834 outstr(progname);
3835 outstr(" [-d\\<����DIR\\>] [-f\\<�ե�����\\>] [-g\\<�����ե�����\\>]\n");
3836 outstr(" [-w\\<���\\>] [-s\\<��ɽ������\\>] [-b[x]] [-a] [-v]");
3837 #ifdef UNIX
3838 outstr("\n [-c\\<������\\>]");
3839 #endif
3840 outstr(" \\<��������\\>\n\n");
3841 outstr("���ץ����: (�Ķ��ѿ�");
3842 outstr(CSRD_ENV);
3843 outstr("�ǻ��ꤹ�뤳�Ȥ�Ǥ��ޤ�)\n");
3844 outstr(" -d: ����ǥ��쥯�ȥ\n");
3845 outstr(" -f: ɽ���ե�����\n");
3846 outstr(" -g: ��������ɽ���ե�����\n");
3847 outstr(" -w: ���Ϲ��� (-w0:���Ԥʤ�(ɸ��))\n");
3848 outstr(" -s: ������ܤ���ɽ���ˤ���\n");
3849 outstr(" (-st:��̣, -se:����, -si:����, -so:���, -sa:���٤�)\n");
3850 outstr(" -b: ȯ����������������� (-bx:[]�������)\n");
3851 outstr(" -a: �Դ������פǤ�������Ƥ�ɽ������\n");
3852 outstr(" -v: ����ǡ����ΥС�������ɽ������\n");
3853 #ifdef UNIX
3854 outstr(" -c: ���ܸ쥳���� (-cj:JIS, -ce:EUC(ɸ��), -cs:���ե�JIS)\n");
3855 #endif
3856 outstr("\n��������: (���硦���㸡���ʳ��Ǥ����줫��⸡���Ǥ��ޤ�)\n");
3857 outstr(" ����������: \\<ʸ����\\> �磻��ɥ����ɸ���: *?[]-��ޤ�ʸ����\n");
3858 outstr(" ����������: \\<ʸ����\\>* ���硦���㸡��: -i \\<ʸ����\\> ...\n");
3859 outstr(" ����������: *\\<ʸ����\\>\n");
3860 exit(1);
3861 }
3862
3863 /*
3864 * parse_opt - �Ķ��ѿ��ޤ��ϥ��ޥ�ɹ����ǻ��ꤵ�줿���ץ�����
3865 * ���Ϥ���ɬ�פ������Ԥ�
3866 */
3867 int
parse_opt(acp,avp)3868 parse_opt(acp, avp)
3869 int *acp;
3870 uchr ***avp;
3871 {
3872 int n, ac;
3873 uchr *s, **av;
3874
3875 ac = *acp;
3876 av = *avp;
3877 while (ac > 0 && **av == '-') {
3878 s = *av + 1;
3879 switch (*s++) {
3880 case 'D':
3881 case 'd':
3882 if (!*s) {
3883 ac--, av++;
3884 if (ac == 0 || **av == '-')
3885 return ERR;
3886 s = *av;
3887 }
3888 dicdir = strpool(s);
3889 bsltosl(dicdir);
3890 n = strlen(dicdir);
3891 if (dicdir[n-1] == '/')
3892 dicdir[n-1] = '\0';
3893 break;
3894 case 'F':
3895 case 'f':
3896 if (!*s) {
3897 ac--, av++;
3898 if (ac == 0 || **av == '-')
3899 return ERR;
3900 s = *av;
3901 }
3902 fmtfile = strpool(s);
3903 bsltosl(fmtfile);
3904 break;
3905 case 'G':
3906 case 'g':
3907 if (!*s) {
3908 ac--, av++;
3909 if (ac == 0 || **av == '-')
3910 return ERR;
3911 s = *av;
3912 }
3913 gaifile = strpool(s);
3914 bsltosl(gaifile);
3915 break;
3916 case 'A':
3917 case 'a':
3918 showall = TRUE;
3919 break;
3920 case 'W':
3921 case 'w':
3922 if (!*s) {
3923 ac--, av++;
3924 if (ac == 0 || **av == '-')
3925 return ERR;
3926 s = *av;
3927 }
3928 if (!isdigit(*s))
3929 return ERR;
3930 width = atoi(s);
3931 if (width != 0 && width < MINWIDTH)
3932 width = MINWIDTH;
3933 break;
3934 case 'B':
3935 case 'b':
3936 do_braille = TRUE;
3937 if (*s == 'x' || *s == 'X')
3938 nobracket = TRUE;
3939 else
3940 nobracket = FALSE;
3941 break;
3942 #ifdef UNIX
3943 case 'C':
3944 case 'c':
3945 if (!*s) {
3946 ac--, av++;
3947 if (ac == 0 || **av == '-')
3948 return ERR;
3949 s = *av;
3950 }
3951 switch (*s) {
3952 case 'J':
3953 case 'j':
3954 kcode = KC_JIS;
3955 if (s[1] && s[2]) {
3956 jiskanji = s[1];
3957 jisalpha = s[2];
3958 }
3959 break;
3960 case 'E':
3961 case 'e':
3962 kcode = KC_EUC;
3963 break;
3964 case 'S':
3965 case 's':
3966 kcode = KC_SJIS;
3967 break;
3968 default:
3969 return ERR;
3970 }
3971 break;
3972 #endif
3973 case 'S':
3974 case 's':
3975 if (!*s) {
3976 ac--, av++;
3977 if (ac == 0 || **av == '-')
3978 return ERR;
3979 s = *av;
3980 }
3981 supstr = strpool(s);
3982 break;
3983 case 'I':
3984 case 'i':
3985 stype = ID_SRCH;
3986 break;
3987 case 'V':
3988 case 'v':
3989 showver = TRUE;
3990 break;
3991 case 'R':
3992 case 'r':
3993 rawmode = TRUE;
3994 break;
3995 #ifdef DEBUG
3996 case 'Z':
3997 case 'z':
3998 if (*s == 'z' || *s == 'Z')
3999 debug2 = TRUE;
4000 else
4001 debug = TRUE;
4002 break;
4003 #endif
4004 default:
4005 return ERR;
4006 }
4007 ac--, av++;
4008 }
4009 *acp = ac;
4010 *avp = av;
4011 return OK;
4012 }
4013
4014 #if defined(UNIX) && defined(RC_PATH)
4015 /*
4016 * init_rcfile - �ե�����ǻ��ꤵ�줿���ץ������᤹��
4017 * Contributed by ��� ���� (Nokubi Takatsugu)
4018 */
4019 int
init_rcfile()4020 init_rcfile()
4021 {
4022 int ec;
4023 uchr *env, *s, **ev, **pp;
4024 FILE *fp;
4025 struct stat st;
4026
4027 if (stat(RC_PATH, &st) < 0)
4028 return OK;
4029 if ((fp = fopen(RC_PATH, "r")) == NULL)
4030 return ERR;
4031 env = (uchr *)malloc(st.st_size + 1);
4032 if (env == NULL)
4033 return ERR;
4034 if (fread(env, st.st_size, 1, fp) < 1)
4035 return ERR;
4036 *(env + st.st_size - 1) = '\0';
4037 fclose(fp);
4038 ec = 1;
4039 for (s = env; *s; s++) {
4040 if (*s == ' ' || *s == '\t')
4041 ec++;
4042 }
4043 ev = (uchr **)malloc(sizeof(uchr *) * (ec + 1));
4044 pp = ev;
4045 while ((s = strtok(env, " \t")) != NULL) {
4046 *pp++ = s;
4047 env = NULL;
4048 }
4049 *pp = NULL;
4050 pp = ev;
4051 if (parse_opt(&ec, &ev) != OK) {
4052 outstr("ERR: �ե����� ");
4053 outstr(RC_PATH);
4054 outstr(" �˸�꤬����ޤ�\n");
4055 return ERR;
4056 }
4057 free((char *)pp);
4058 free((char *)env);
4059 return OK;
4060 }
4061 #endif
4062
4063 /*
4064 * init_env - �Ķ��ѿ�CSRD�ǻ��ꤵ�줿���ץ������᤹��
4065 */
4066 int
init_env()4067 init_env()
4068 {
4069 int ec;
4070 uchr *env, *s, **ev, **pp;
4071
4072 if ((env = getenv(CSRD_ENV)) == NULL)
4073 return OK;
4074 ec = 1;
4075 for (s = env; *s; s++) {
4076 if (*s == ' ' || *s == '\t')
4077 ec++;
4078 }
4079 ev = (uchr **)malloc(sizeof(uchr *) * (ec + 1));
4080 pp = ev;
4081 while ((s = strtok(env, " \t")) != NULL) {
4082 *pp++ = s;
4083 env = NULL;
4084 }
4085 *pp = NULL;
4086 pp = ev;
4087 if (parse_opt(&ec, &ev) != OK) {
4088 outstr("ERR: �Ķ��ѿ� ");
4089 outstr(CSRD_ENV);
4090 outstr(" �˸�꤬����ޤ�\n");
4091 return ERR;
4092 }
4093 free((char *)pp);
4094 return OK;
4095 }
4096
4097 /*
4098 * onintr - �������ν�λ����
4099 */
4100 void
onintr()4101 onintr()
4102 {
4103 outchar(0); /* JIS�����⡼�ɤʤ�ASCII���᤹(UNIX) */
4104 cleantmp(); /* ���硦���㸡���Ѥΰ���ե������������ */
4105 exit(0);
4106 }
4107
4108 /*
4109 * main - �ᥤ��롼����
4110 */
4111 int
main(ac,av)4112 main(ac, av)
4113 int ac;
4114 char **av;
4115 {
4116 signal(SIGINT, onintr);
4117 #if defined(UNIX) && defined(RC_PATH)
4118 if (init_rcfile() != OK)
4119 exit(1);
4120 #endif
4121 if (init_env() != OK)
4122 exit(1);
4123 ac--, av++;
4124 if (parse_opt(&ac, &av) != OK)
4125 usage();
4126 if (!showver && (ac == 0 || stype != ID_SRCH && ac > 1))
4127 usage();
4128 if (dicdir == NULL) {
4129 outstr("ERR: ����ǥ��쥯�ȥ�����ꤵ��Ƥ��ޤ���\n");
4130 exit(1);
4131 }
4132 if (showver) {
4133 show_version(dicdir);
4134 exit(0);
4135 }
4136 if (init_alt() == ERR)
4137 exit(1);
4138 if (gaifile != NULL) {
4139 if (load_gaiji(gaifile) == ERR)
4140 exit(1);
4141 }
4142 if (fmtfile != NULL) {
4143 if (load_format(fmtfile) == ERR)
4144 exit(1);
4145 }
4146 if (suppress(supstr) == ERR) {
4147 outstr("ERR: ɽ����������(-s���ץ����)�˸�꤬����ޤ�\n");
4148 exit(1);
4149 }
4150 if (search(dicdir, showall, ac, av) == ERR)
4151 exit(1);
4152 exit(0);
4153 }
4154