/*
 * \217\254\212\167\212\331\203\211\203\223\203\137\203\200\203\156\203\105\203\130\211\160\214\352\216\253\223\124\214\237\215\365\203\206\201\133\203\145\203\102\203\212\203\145\203\102\201\133 - csrd
 *
 *	Written by Junn Ohta (ohta@src.ricoh.co.jp), Public Domain.
 */

char	*progname = "csrd";
char	*version  = "1.0";
char	*date     = "1999/07/19";

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef MSDOS
#include <io.h>
#endif

typedef	unsigned char	uchr;
typedef	unsigned int	unt;

#ifndef O_BINARY
#define	O_BINARY	0
#endif

#define	OK		0
#define	ERR		(-1)

#define	TRUE		1
#define	FALSE		0

#define	SJIS1(c)	((c)>=0x81 && (c)<=0x9f || (c)>=0xe0 && (c)<=0xef)

#ifdef DEBUG
int	debug = FALSE;		/* \203\146\203\157\203\142\203\117\203\164\203\211\203\117		*/
int	debug2 = FALSE;		/* \203\146\203\157\203\142\203\117\203\164\203\211\203\117		*/
#endif

/* -------------------- \203\206\201\133\203\145\203\102\203\212\203\145\203\102\201\133\212\326\220\224 -------------------- */ 

#define	POOLSIZ		1024	/* \225\266\216\232\227\361\203\166\201\133\203\213\202\314\212\204\202\350\223\226\202\304\222\120\210\312	*/

uchr	*pool = NULL;		/* \225\266\216\232\227\361\203\166\201\133\203\213			*/
uchr	*pfree = NULL;		/* \225\266\216\232\227\361\203\166\201\133\203\213\202\314\213\363\202\253\227\314\210\346\220\346\223\252	*/

int	hexval();
uchr	*escchar();
uchr	*skipeq();
uchr	*skipsp();
uchr	*strpool();
void	bsltosl();
int	nspaces();
void	lower();

/*
 * hexval - 2\214\205\202\31416\220\151\220\224\202\252\216\246\202\267\222\154\202\360\225\324\202\267
 */
int
hexval(p)
uchr	*p;
{
    int		i, n;

    n = 0;
    for (i = 0; i < 2; i++) {
	n <<= 4;
	if (*p >= '0' && *p <= '9')
	    n += *p - '0';
	else if (*p >= 'a' && *p <= 'f')
	    n += *p - 'a' + 10;
	else if (*p >= 'A' && *p <= 'F')
	    n += *p - 'A' + 10;
	else
	    n = 0;
	p++;
    }
    return n;
}

/*
 * escchar - '\'\202\305\203\107\203\130\203\120\201\133\203\166\202\263\202\352\202\275\225\266\216\232\202\360\222\262\202\327\201\101\216\237\202\314\210\312\222\165\202\360\225\324\202\267
 */
uchr *
escchar(str, cp)
uchr	*str;
int	*cp;
{
    uchr	*p;

    p = str;
    if (*p++ != '\\')
	return str;
    if (*p >= '0' && *p <= '7') {
	sscanf(p, "%3o", cp);
	return p + 3;
    }
    switch (*p) {
    case 'x':
	*cp = hexval(p + 1);
	p += 3;
	break;
    case 'n':
	*cp = '\n';
	p++;
	break;
    case 't':
	*cp = '\t';
	p++;
	break;
    default:
	*cp = *p++;
	break;
    }
    return p;
}

/*
 * skipeq - '='\202\334\202\305\202\360\223\307\202\335\224\362\202\316\202\265\201\101\216\237\202\314\210\312\222\165\202\360\225\324\202\267
 */
uchr *
skipeq(str)
uchr	*str;
{
    while (*str && *str != '=')
	str++;
    if (*str == '=')
	str++;
    return str;
}

/*
 * skipsp - \213\363\224\222\202\360\223\307\202\335\224\362\202\316\202\265\201\101\216\237\202\314\210\312\222\165\202\360\225\324\202\267
 */
uchr *
skipsp(str)
uchr	*str;
{
    while (*str == ' ' || *str == '\t')
	str++;
    return str;
}

/*
 * strpool - \225\266\216\232\227\361\202\360\203\166\201\133\203\213\202\311\212\151\224\133\202\265\202\304\202\273\202\314\203\101\203\150\203\214\203\130\202\360\225\324\202\267
 *	     (\211\360\225\372\202\360\215\154\227\266\202\265\202\310\202\242strdup)
 */
uchr *
strpool(str)
uchr	*str;
{
    int		len;
    uchr	*p;

    if (str == NULL)
	return NULL;
    len = strlen(str) + 1;
    if (pool == NULL || pfree + len > pool + POOLSIZ) {
	pool = (uchr *)malloc(POOLSIZ); /* \210\310\221\117\202\314pool\202\314\222\154\202\315\226\131\202\352\202\351 */
	if (pool == NULL)
	    return NULL;
	pfree = pool;
    }
    p = pfree;
    pfree += len;
    return strcpy(p, str);
}

/*
 * bsltosl - \225\266\216\232\227\361\222\206\202\314\203\157\203\142\203\116\203\130\203\211\203\142\203\126\203\205(\)\202\360\203\130\203\211\203\142\203\126\203\205(/)\202\311\225\317\212\267\202\267\202\351
 */
void
bsltosl(str)
uchr	*str;
{
    if (str == NULL)
	return;
    while (*str) {
	if (*str == '\\')
	    *str = '/';
	if (SJIS1(*str))
	    str++;
	str++;
    }
}

/*
 * nspaces - \225\266\216\232\227\361\202\314\220\346\223\252\202\311\202\240\202\351\203\130\203\171\201\133\203\130\202\314\227\312\202\360\220\224\202\246\202\351
 */
int
nspaces(str)
uchr	*str;
{
    int		n;

    if (str == NULL)
	return 0;
    n = 0;
    while (*str && isspace(*str)) {
	switch (*str++) {
	case ' ':
	    n++;
	    break;
	case '\t':
	    n = (n | 0x07) + 1;
	    break;
	case '\n':
	case '\r':
	    n = 0;
	    break;
	case '\b':
	    if (n > 0)
		n--;
	    break;
	}
    }
    return n;
}

/*
 * lower - \211\160\225\266\216\232\227\361\202\360\217\254\225\266\216\232\211\273\202\267\202\351
 */
void
lower(str)
uchr	*str;
{
    if (str == NULL)
	return;
    while (*str) {
	if (isascii(*str) && isupper(*str))
	    *str = tolower(*str);
	str++;
    }
}

/* -------------------- \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315 -------------------- */

#define	BBUFSIZ		4096

typedef	struct _bfile {
    int		fd;
    long	size;
    long	pos;
    uchr	*p, *ep;
    uchr	buf[BBUFSIZ];
} BFILE;

BFILE	*bopen();
long	btell();
int	bseek();
int	bread();
int	bgetc();
int	bgetcbw();
int	bclose();

/*
 * bopen - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\305\203\164\203\100\203\103\203\213\202\360\203\111\201\133\203\166\203\223\202\267\202\351
 *	   bseek()\202\305\210\312\222\165\202\303\202\257\202\360\202\267\202\351\202\334\202\305\223\307\202\335\215\236\202\335\202\315\202\305\202\253\202\310\202\242
 */
BFILE *
bopen(file)
char	*file;
{
    int		fd;
    BFILE	*bp;
    struct stat	st;

    if ((fd = open(file, O_RDONLY|O_BINARY)) < 0)
	return NULL;
    if (fstat(fd, &st) < 0) {
	close(fd);
	return NULL;
    }
    bp = (BFILE *)malloc(sizeof(BFILE));
    if (bp == NULL) {
	close(fd);
	return NULL;
    }
    bp->fd = fd;
    bp->size = st.st_size;
    bp->pos = 0L;
    bp->p = bp->ep = NULL;
    return bp;
}

/*
 * btell - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\314tell()\202\340\202\307\202\253
 */
long
btell(bp)
BFILE	*bp;
{
    if (bp->p == NULL)
	return (long)ERR;
    return bp->pos + (bp->p - bp->buf);
}

/*
 * bseek - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\314lseek()\202\340\202\307\202\253
 */
int
bseek(bp, pos)
BFILE	*bp;
long	pos;
{
    int		n;

    if (bp->p
	&& pos >= bp->pos
	&& pos < bp->pos + (bp->ep - bp->buf)) {
	bp->p = bp->buf + (pos - bp->pos);
	return OK;
    }
    if (pos >= bp->size
	|| lseek(bp->fd, pos, SEEK_SET) < 0
	|| (n = read(bp->fd, bp->buf, BBUFSIZ)) <= 0) {
	bp->pos = 0L;
	bp->p = bp->ep = NULL;
	return ERR;
    }
    bp->pos = pos;
    bp->p = bp->buf;
    bp->ep = bp->buf + n;
    return OK;
}

/*
 * bread - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\314read()\202\340\202\307\202\253
 */
int
bread(bp, buf, len)
BFILE	*bp;
uchr	*buf;
int	len;
{
    int		n;
    uchr	*p;

    if (bp->p == NULL)
	return ERR;
    p = buf;
    while (len > 0) {
	n = bp->ep - bp->p;
	if (n >= len) {
	    memcpy(p, bp->p, (size_t)len);
	    p += len;
	    bp->p += len;
	    len = 0;
	} else if (n > 0) {
	    memcpy(p, bp->p, (size_t)n);
		p += n;
		bp->p += n;
	    len -= n;
	} else {
	    n = read(bp->fd, bp->buf, BBUFSIZ);
	    if (n <= 0) {
		bp->pos = 0L;
		bp->p = bp->ep = NULL;
		break;
	    }
	    bp->pos += bp->ep - bp->buf;
	    bp->p = bp->buf;
	    bp->ep = bp->buf + n;
	}
    }
    return p - buf;
}

/*
 * bgetc - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\314getc()\202\340\202\307\202\253
 */
int
bgetc(bp)
BFILE	*bp;
{
    int		n;

    if (bp->p == NULL)
	return EOF;
    if (bp->p < bp->ep)
	return (int)*(bp->p++);
    n = read(bp->fd, bp->buf, BBUFSIZ);
    if (n <= 0) {
	bp->pos = 0L;
	bp->p = bp->ep = NULL;
	return EOF;
    }
    bp->pos += bp->ep - bp->buf;
    bp->p = bp->buf;
    bp->ep = bp->buf + n;
    return (int)*(bp->p++);
}

/*
 * bgetcbw - \214\273\215\335\202\314\210\312\222\165\202\251\202\347\203\164\203\100\203\103\203\213\220\346\223\252\225\373\214\374\202\311bgetc()\202\267\202\351
 */
int
bgetcbw(bp)
BFILE	*bp;
{
    int		n;
    long	npos;

    if (bp->p == NULL)
	return EOF;
    if (bp->p > bp->buf)
	return (int)*(--bp->p);
    n = BBUFSIZ;
    npos = bp->pos - n;
    if (bp->pos < 0L) {
	npos = 0L;
	n = bp->pos - npos;
	if (n == 0)
	    return EOF;
    }
    if (lseek(bp->fd, npos, SEEK_SET) < 0
	|| read(bp->fd, bp->buf, n) < n) {
	bp->pos = 0L;
	bp->p = bp->ep = NULL;
	return EOF;
    }
    bp->pos = npos;
    bp->ep = bp->buf + n;
    bp->p = bp->ep;
    return (int)*(--bp->p);
}

/*
 * bclose - \203\157\203\142\203\164\203\100\201\133\203\150\223\374\227\315\202\314close()\202\340\202\307\202\253
 */
int
bclose(bp)
BFILE	*bp;
{
    int		fd;

    fd = bp->fd;
    free((char *)bp);
    return close(fd);
}

/* -------------------- \223\372\226\173\214\352\217\210\227\235 -------------------- */

#define	KC_JIS		0
#define	KC_EUC		1
#define	KC_SJIS		2

#ifdef UNIX
#define	KC_DEF		KC_EUC
#endif
#ifdef MSDOS
#define	KC_DEF		KC_SJIS
#endif

/*
 * \212\117\216\232\225\134
 */
uchr	*gaijitbl[8][256] = {
    { /* G0 */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"/",
	0,0,0,0,0,0,0,0,0,0,":",0,0,"=",0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,"...",0,0,0,0,0,0,0,0,0,0,
	0,0,0,"\"","\"",0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,"(C)",0,0,0,0,"(R)",0,
	0,0,0,0,0,0,0,"/",0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G1 */
    	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,"(\203\136\203\165\201\133)",0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G2 */
    	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,"\201\245",0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G3 */
    	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,"(P)",0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,"\201\251\201\250",0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G4 */
    	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,"\201\250",0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"\201\163",
	"\201\164","\201\153","\201\154",0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G5 */
    	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G6 */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    }, { /* G99 */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,"#",0,0,0,0,0,0,0,0,0,0,0,"/",
	0,0,0,0,0,0,0,0,0,0,0,0,"<",0,">",0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,"...",0,0,0,0,0,0,0,0,0,0,
	0,0,0,"\"","\"",0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	"A","A",0,0,0,0,0,0,"E","E",0,0,"I","I",0,0,
	0,0,"O","O",0,0,0,0,0,"U","U",0,0,0,0,0,
	"a","a",0,0,0,0,0,0,"e","e",0,0,"i","i",0,0,
	0,0,"o","o",0,0,0,0,0,"u","u",0,0,"y",0,0
    }
};

/*
 * \203\146\203\164\203\110\203\213\203\147\212\117\216\232
 */
uchr	*gdeftbl[8] = { 0, 0, 0, 0, 0, 0 ,"\201\254", 0 };

typedef	struct _alttbl_t {
    uchr	org[4];			/* \214\263\225\134\213\114			*/
    uchr	*alt;			/* \221\343\221\326\225\134\213\114			*/
} ALTTBL;

#define	ATBLINC		10		/* \221\343\221\326\225\134\213\114\225\134\202\314\221\235\225\252		*/

uchr	have_alt[256];			/* \202\273\202\314\225\266\216\232\202\315\221\343\221\326\225\134\213\114\202\360\216\235\202\302\202\251?	*/
ALTTBL	*alttbl = NULL;			/* \221\343\221\326\225\134\213\114\225\134			*/
int	altnum = 0;			/* \221\343\221\326\225\134\213\114\225\134\227\166\221\146\220\224		*/
int	in_alt = FALSE;			/* \221\343\221\326\225\134\213\114\222\206\217\363\221\324\202\251?		*/

int	curpos = 0;			/* \214\273\215\335\202\314\225\134\216\246\210\312\222\165		*/
int	kcode = KC_DEF;			/* \223\374\217\157\227\315\212\277\216\232\203\122\201\133\203\150		*/
int	jiskanji = 'B';			/* JIS\212\277\216\232\203\122\201\133\203\150\202\314\212\277\216\232\221\111\221\360	*/
int	jisalpha = 'B';			/* JIS\212\277\216\232\203\122\201\133\203\150\202\314\211\160\216\232\221\111\221\360	*/
int	width = 0;			/* \217\157\227\315\215\163\225\235			*/
int	indent = 0;			/* \203\103\203\223\203\146\203\223\203\147\227\312			*/
int	kmode = FALSE;			/* \222\133\226\226\202\252JIS\212\277\216\232\216\167\216\246\217\363\221\324\202\251?	*/
int	rawmode = FALSE;		/* \217\157\227\315\226\263\211\301\215\110\203\202\201\133\203\150		*/
int	gfont = -1;			/* \212\117\216\232\203\164\203\110\203\223\203\147\224\324\215\206		*/
int	hlink = FALSE;			/* \203\156\203\103\203\160\201\133\203\212\203\223\203\116\216\167\222\350\223\340		*/

int	add_alt();
int	init_alt();
uchr	*get_gaiji();
int	load_gaiji();
uchr	*tosjis();
void	outchar();
void	outstr();
void	set_indent();
void	newline();

/*
 * add_alt - \221\343\221\326\225\134\213\114\225\134\202\311\221\343\221\326\225\134\213\114\202\360\222\307\211\301\202\267\202\351
 */
int
add_alt(org, alt)
uchr	*org, *alt;
{
    int		i;
    uchr	*p;
    static int	atblsiz = 0;

    for (i = 0; i < altnum; i++) {
	if (!strcmp(alttbl[i].org, org)) {
	    p = strpool(alt);
	    if (p == NULL)
		return ERR;
	    alttbl[i].alt = p;
	    return OK;
	}
    }
    if (alttbl == NULL) {
	atblsiz = ATBLINC;
	alttbl = (ALTTBL *)malloc(sizeof(ALTTBL) * atblsiz);
	altnum = 0;
    } else if (altnum >= atblsiz) {
	atblsiz += ATBLINC;
	alttbl = (ALTTBL *)realloc(alttbl, sizeof(ALTTBL) * atblsiz);
    }
    if (alttbl == NULL)
	return ERR;
    p = strpool(alt);
    if (p == NULL)
	return ERR;
    strcpy(alttbl[altnum].org, org);
    alttbl[altnum].alt = p;
    have_alt[*org] = TRUE;
    altnum++;
    return OK;
}

/*
 * init_alt - \221\343\221\326\225\134\213\114\225\134\202\311\203\146\203\164\203\110\203\213\203\147\221\343\221\326\225\134\213\114\202\360\223\157\230\136\202\267\202\351
 */
int
init_alt()
{
    int		err;

    err = 0;
    if (add_alt("\xa5", "/") == ERR) /* 1\203\157\203\103\203\147\203\112\203\151\202\314\222\206\223\137 */
	err++;
    return err;
}

/*
 * get_gaiji - \203\164\203\110\203\223\203\147\224\324\215\206\202\306\203\122\201\133\203\150\202\251\202\347\212\117\216\232\225\266\216\232\227\361\202\360\225\324\202\267
 */
uchr *
get_gaiji(g, ch)
int	g, ch;
{
    uchr	*p;

    if (g == 9)
	g = 7;
    p = gaijitbl[g][ch];
    if (p == NULL)
	p = gdeftbl[g];
    return p;
}

/*
 * load_gaiji - \212\117\216\232\221\343\221\326\225\134\213\114\203\164\203\100\203\103\203\213\202\360\223\307\202\361\202\305\212\117\216\232\202\360\223\157\230\136\202\267\202\351
 */
int
load_gaiji(file)
uchr	*file;
{
    int		c, ch, g, isalt;
    int		line, err;
    uchr	*p, *q;
    FILE	*fp;
    uchr	org[4], buf[256], tmp[256];

    if ((fp = fopen(file, "r")) == NULL) {
	outstr("ERR: \212\117\216\232\221\343\221\326\225\134\213\114\203\164\203\100\203\103\203\213\202\252\203\111\201\133\203\166\203\223\202\305\202\253\202\334\202\271\202\361\n");
	return ERR;
    }
    line = 0;
    err = 0;
    while (fgets(buf, 256, fp) != NULL) {
	line++;
	buf[strlen(buf) - 1] = '\0';
	p = skipsp(buf);
	if (*p == '\0' || *p == ';')
	    continue;
	if (*p == '"') {	/* \221\343\221\326\225\134\213\114 */
	    p++;
	    if (*p == '\\') {
		p = escchar(p, &c);
		org[0] = c;
		org[1] = '\0';
	    } else if (SJIS1(*p)) {
		org[0] = *p++;
		org[1] = *p++;
		org[2] = '\0';
	    } else {
		org[0] = *p++;
		org[1] = '\0';
	    }
	    if (*p != '"') {
		sprintf(tmp,
		    "ERR: line %d: \221\343\221\326\214\263\225\266\216\232\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
		outstr(tmp);
		err++;
		continue;
	    }
	    p++;
	    isalt = TRUE;
	} else {	/* \212\117\216\232\222\350\213\140 */
	    if (*p != 'G' || !strchr("01234569", p[1]) || p[2] != '-' ||
		!(isxdigit(p[3]) && isxdigit(p[4]) || p[3] == '*')) {
		sprintf(tmp,
		    "ERR: line %d: \212\117\216\232\224\324\215\206\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
		outstr(tmp);
		err++;
		continue;
	    }
	    g = p[1] - '0';
	    if (g == 9)
		g = 7;
	    if (p[3] == '*') {
		ch = -1;
		p += 4;
	    } else {
		ch = hexval(p + 3);
		p += 5;
	    }
	    isalt = FALSE;
	}
	p = skipsp(p);
	if (*p == '=')
	    p++;
	p = skipsp(p);
	if (*p != '"') {
	    sprintf(tmp,
		"ERR: line %d: \221\343\221\326\225\134\213\114\225\266\216\232\227\361\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
	    outstr(tmp);
	    err++;
	    continue;
	}
	p++;
	q = tmp;
	while (*p && *p != '"') {
	    if (*p == '\\') {
		p = escchar(p, &c);
		if (c == '\\' || c == '<' || c == '>')
		    *q++ = '\\';
		*q++ = c;
		continue;
	    }
	    if (SJIS1(*p)) {
		*q++ = *p++;
		*q++ = *p++;
		continue;
	    }
	    if (*p == '<' || *p == '>' || *p == '#')
		*q++ = '\\';
	    *q++ = *p++;
	}
	if (*p != '"') {
	    sprintf(tmp,
		"ERR: line %d: \221\343\221\326\225\134\213\114\225\266\216\232\227\361\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
	    outstr(tmp);
	    err++;
	    continue;
	}
	*q = '\0';
	if (isalt) {
	    if (add_alt(org, tmp) == ERR) {
		outstr("ERR: \203\201\203\202\203\212\201\133\202\252\221\253\202\350\202\334\202\271\202\361\n");
		err++;
	    }
	    continue;
	}
	if ((q = strpool(tmp)) == NULL) {
	    outstr("ERR: \203\201\203\202\203\212\201\133\202\252\221\253\202\350\202\334\202\271\202\361\n");
	    err++;
	    continue;
	}
	if (ch >= 0)
	    gaijitbl[g][ch] = q;
	else
	    gdeftbl[g] = q;
    }
    fclose(fp);
    if (err)
	return ERR;
    return OK;
}

#ifdef MSDOS
uchr *
tosjis(str)
uchr	*str;
{
    return str;
}

void
outchar(c)
int	c;
{
    if (c == 0)
	return;
    putchar(c);
}
#endif

#ifdef UNIX
/*
 * tosjis - kcode\202\305\225\134\214\273\202\263\202\352\202\275\225\266\216\232\227\361\202\360\203\126\203\164\203\147JIS\202\311\225\317\212\267\202\267\202\351
 *	    (\203\122\203\175\203\223\203\150\215\163\210\370\220\224\202\314\225\317\212\267\202\311\216\147\202\244)
 */
uchr *
tosjis(str)
uchr	*str;
{
    int		c, c1;
    int		c2, km;
    uchr	*p;
    static uchr	buf[128];

    if (kcode == KC_SJIS) {
	strcpy((char *)buf, str);
	return buf;
    }
    km = FALSE;
    p = buf;
    c1 = 0;
    while ((c = *str++) != '\0') {
	if (c1 != 0) {
	    c1 &= 0x7f;
	    c &= 0x7f;
	    if ((c1 & 0x01) == 0) {
		c += 0x7e;
	    } else {
		c += 0x1f;
		if (c > 0x7e)
		    c++;
	    }
	    c1 = (c1 + 0xe1) >> 1;
	    if (c1 > 0x9f)
		c1 += 0x40;
	    *p++ = c1;
	    *p++ = c;
	    c1 = 0;
	    continue;
	}
	if (c == '\033') {
	    c1 = *str++;
	    c2 = *str++;
	    if (c1 == '$' && (c2 == '@' || c2 == 'B'))
		km = TRUE;
	    else if (c1 == '(' && (c2 == 'J' || c2 == 'B'))
		km = FALSE;
	    continue;
	}
	if ((c & 0x80) || km == 1) {
	    c1 = c;
	    continue;
	}
	*p++ = c;
    }
    *p = '\0';
    return buf;
}

/*
 * outchar - \203\126\203\164\203\147JIS\202\314\225\266\216\232\202\360\217\363\221\324\221\112\210\332\202\360\212\307\227\235\202\265\202\310\202\252\202\347
 *	     kcode\202\311\225\317\212\267\202\265\202\304\217\157\227\315\202\267\202\351
 *	     \210\370\220\224\202\2520\202\310\202\347\217\157\227\315\221\244\202\314\217\363\221\324\202\360\211\160\216\232\202\311\226\337\202\267(JIS\202\314\202\306\202\253\225\113\227\166)
 */
void
outchar(c)
int	c;
{
    static int	knj1 = 0;

    if (c == '\0') {
	if (kmode) {
	    printf("\033(%c", jisalpha);
	    kmode = FALSE;
	}
	return;
    }
    if (knj1 != 0) {
	if (kcode == KC_JIS && !kmode) {
	    printf("\033$%c", jiskanji);
	    kmode = TRUE;
	}
	if (kcode == KC_JIS || kcode == KC_EUC) {
	    if (knj1 > 0x9f)
		knj1 -= 0x40;
	    knj1 += knj1;
	    if (c <= 0x9e) {
		knj1 -= 0xe1;
		if (c >= 0x80)
		    c -= 1;
		c -= 0x1f;
	    } else {
		knj1 -= 0xe0;
		c -= 0x7e;
	    }
	    if (kcode == KC_EUC) {
		knj1 += 0x80;
		c += 0x80;
	    }
	}
	putchar(knj1);
	putchar(c);
	knj1 = 0;
	return;
    }
    if (c >= 0x81) {
	knj1 = c;
	return;
    }
    if (kmode) {
	printf("\033(%c", jisalpha);
	kmode = FALSE;
    }
    putchar(c);
}
#endif

/*
 * outstr - \212\117\216\232\202\360\225\317\212\267\202\265\202\310\202\252\202\347outchar\214\157\227\122\202\305\225\266\216\232\202\360\217\157\227\315\202\267\202\351
 */
void
outstr(p)
uchr	*p;
{
    int		i, g;
    uchr	*gstr;
    uchr	tmp[16];

    while (*p) {
	if (rawmode) {
	    outchar(*p++);
	    continue;
	}
	if (*p == '<') {
	    if (p[1] == '/') {
		if (p[2] == 'G' && isdigit(p[3]))
		    gfont = -1;
		else if (!strncmp(p+2, "RUBY>", 5))
		    outstr(")");
		else if (!strncmp(p+2, "HL>", 3))
		    hlink = FALSE;
	    } else {
		if (p[1] == 'G')
		    gfont = p[2] - '0';
		else if (!strncmp(p+1, "RUBY>", 5))
		    outstr("(");
		else if (!strncmp(p+1, "HL>", 3))
		    hlink = TRUE;
	    }
	    while (*p != '>')
		p++;
	    p++;
	    continue;
	}
	if (*p == ':' && hlink) {
	    /*
	     * \203\156\203\103\203\160\201\133\203\212\203\223\203\116\216\167\222\350\223\340\202\314\201\165:\201\166\210\310\215\176\202\315\202\267\202\327\202\304\226\263\216\213
	     */
	    p++;
	    while (*p && (*p != '<' || strncmp(p, "</HL>", 5))) {
		if (SJIS1(*p))
		    p++;
		p++;
	    }
	    p += 5;
	    hlink = FALSE;
	    continue;
	}
	if (*p == '#' && p[3] == '#') {
	    g = (gfont == -1)? 0: gfont;
	    gstr = get_gaiji(g, hexval(p+1));
	    if (gstr) {
		outstr(gstr);
	    } else {
		sprintf(tmp, "\\<G%d-%c%c\\>", g, p[1], p[2]);
		outstr(tmp);
	    }
	    p += 4;
	    continue;
	}
	if (!in_alt && have_alt[*p]) {
	    for (i = 0; i < altnum; i++) {
		if (*p == alttbl[i].org[0] &&
		    (!SJIS1(*p) || p[1] == alttbl[i].org[1]))
		    break;
	    }
	    if (i < altnum) {
		in_alt = TRUE;
		outstr(alttbl[i].alt);
		in_alt = FALSE;
		if (SJIS1(*p))
		    p++;
		p++;
		continue;
	    }
	}
	if (*p == '\\')
	    p++;
	if (SJIS1(*p)) {
	    if (width > 0 && curpos >= width - 1)
		newline();
	    outchar(*p++);
	    outchar(*p++);
	    curpos += 2;
	    continue;
	}
	if (*p < ' ' || *p == 0x7f) {
	    switch (*p) {
	    case '\n':
		newline();
		p++;
		break;
	    case '\t':
		if (width > 0 && ((curpos | 0x07) + 1) > width)
		    newline();
		outchar(*p++);
		curpos = (curpos | 0x07) + 1;
		break;
	    default:
		outchar(*p++);
		break;
	    }
	    continue;
	}
	if (width > 0 && curpos >= width && !strchr("!),.:;?]}", *p))
	    newline();
	outchar(*p++);
	curpos++;
    }
    outchar(0);
}

/*
 * set_indent - \203\103\203\223\203\146\203\223\203\147\227\312\202\360\220\335\222\350\202\267\202\351
 */
void
set_indent(n)
int	n;
{
    indent = n;
}

/*
 * newline - \211\374\215\163\202\265\202\304\220\335\222\350\227\312\202\276\202\257\203\103\203\223\203\146\203\223\203\147\202\267\202\351
 */
void
newline()
{
    outchar('\n');
    for (curpos = 0; curpos < indent; curpos++)
	outchar(' ');
}

/* -------------------- \225\134\216\246\217\221\216\256\217\210\227\235 -------------------- */

/*
 * \226\173\225\266\203\146\201\133\203\136\215\163\223\252\202\314\203\211\203\170\203\213
 */
#define	LBL_ERROR	(-1)
#define	LBL_CMT		 0		/* \222\215\216\337(\224\361\225\134\216\246)			*/
#define	LBL_DR		 1		/* \224\150\220\266\214\140			*/
#define	LBL_EN		 2		/* \214\251\217\157\202\265\214\352			*/
#define	LBL_ENN		 3		/* \214\237\215\365\227\160\210\331\225\134\213\114???(\224\361\225\134\216\246)	*/
#define	LBL_ENV		 4		/* \210\331\225\134\213\114			*/
#define	LBL_ENVN	 5		/* \225\312\222\324\202\350\210\331\225\134\213\114???		*/
#define	LBL_ET		 6		/* \217\211\217\157???			*/
#define	LBL_EV		 7		/* \223\257\213\140\214\352			*/
#define	LBL_EX		 8		/* \227\160\227\341				*/
#define	LBL_EXKEY	 9		/* \227\160\227\341\214\237\215\365\203\114\201\133			*/
#define	LBL_EXSUB	10		/* \227\160\227\341\203\124\203\165\215\200\226\332			*/
#define	LBL_F		11		/* IF\202\314\214\353\213\114???(tallith)		*/
#define	LBL_ID		12		/* \220\254\213\345				*/
#define	LBL_IDSUB	13		/* \220\254\213\345\203\124\203\165\215\200\226\332			*/
#define	LBL_IF		14		/* \225\163\213\113\221\245\212\210\227\160			*/
#define	LBL_IMG		15		/* \220\175\224\305\203\146\201\133\203\136(\224\361\225\134\216\246)		*/
#define	LBL_J2E		16		/* \223\372\226\173\214\352\202\251\202\347\216\330\227\160(adsuki bean)	*/
#define	LBL_KEY		17		/* \214\237\215\365\203\114\201\133(\224\361\225\134\216\246)		*/
#define	LBL_KEYEX	18		/* \227\160\227\341\214\237\215\365\203\114\201\133(\224\361\225\134\216\246)		*/
#define	LBL_KEYID	19		/* \220\254\213\345\214\237\215\365\203\114\201\133(\224\361\225\134\216\246)		*/
#define	LBL_LOC		20		/* \226\173\225\266\212\151\224\133\210\312\222\165(\224\361\225\134\216\246)		*/
#define	LBL_MEN		21		/* MN\202\314\214\353\213\114???			*/
#define	LBL_MN		22		/* \210\323\226\241				*/
#define	LBL_MNSUB	23		/* \210\323\226\241\203\124\203\165\215\200\226\332			*/
#define	LBL_NB		24		/* \222\220				*/
#define	LBL_NBP		25		/* \225\134\214\273\222\220			*/
#define	LBL_NBPSUB	26		/* \225\134\214\273\222\220\203\124\203\165\215\200\226\332		*/
#define	LBL_NBSUB	27		/* \222\220\203\124\203\165\215\200\226\332			*/
#define	LBL_NSBUS	28		/* NBSUB\202\314\214\353\213\114???(and)		*/
#define	LBL_OT		29		/* \227\336\214\352\201\101\224\255\211\271\201\101...		*/
#define	LBL_OTSUB	30		/* \227\336\214\352\203\124\203\165\215\200\226\332			*/
#define	LBL_PR		31		/* \224\255\211\271				*/
#define	LBL_PS		32		/* \225\151\216\214				*/
#define	LBL_RA		33		/* ???(\224\361\225\134\216\246)			*/
#define	LBL_RES		34		/* ???(\224\361\225\134\216\246)			*/
#define	LBL_RN		35		/* ???(\224\361\225\134\216\246)			*/
#define	LBL_SM		36		/* ???(\224\361\225\134\216\246)			*/
#define	LBL_SND		37		/* \211\271\220\272\203\146\201\133\203\136(\224\361\225\134\216\246)		*/
#define	LBL_SRC		38		/* \214\352\214\271				*/
#define	LBL_SRCSUB	39		/* \214\352\214\271\203\124\203\165\215\200\226\332			*/
#define	LBL_TL		40		/* \225\266\214\243\203\136\203\103\203\147\203\213			*/
#define	LBL_TLSUB	41		/* \225\266\214\243\203\136\203\103\203\147\203\213\203\124\203\165\215\200\226\332		*/
#define	LBL_TQ		42		/* \210\323\226\241\225\252\227\336(get\202\314\201\165I \217\212\227\114\202\267\202\351\201\166)*/
#define	LBL_TR		43		/* \214\352\210\323				*/
#define	LBL_TRSUB	44		/* \214\352\210\323\203\124\203\165\215\200\226\332			*/
/*
 * \210\310\211\272\202\315csrd\223\340\225\224\217\210\227\235\227\160
 */
#define	LBL_ZEX		45		/* \220\254\213\345\201\105\227\160\227\341\214\237\215\365\214\213\211\312\202\314\227\160\227\341	*/
#define	LBL_ZID		46		/* \220\254\213\345\201\105\227\160\227\341\214\237\215\365\214\213\211\312\202\314\220\254\213\345	*/

typedef	struct _lbltbl_t {
    int		id;			/* \203\211\203\170\203\213\216\257\225\312ID			*/
    int		disp;			/* \217\157\227\315\202\267\202\351\202\251?			*/
    uchr	*label;			/* \203\211\203\170\203\213\225\266\216\232\227\361			*/
    uchr	*leader;		/* \220\346\223\252\202\311\217\157\227\315\202\267\202\351\225\266\216\232\227\361		*/
} LBLTBL;

/*
 * \203\211\203\170\203\213\225\134
 */
LBLTBL	labeltbl[] = {
    { LBL_CMT,		FALSE,	"CMT",		""	},
    { LBL_DR,		TRUE,	"DR",		""	},
    { LBL_EN,		TRUE,	"EN",		"\201\240 "	},
    { LBL_ENN,		FALSE,	"ENN",		""	},
    { LBL_ENV,		TRUE,	"ENV",		""	},
    { LBL_ENVN,		TRUE,	"ENVN",		""	},
    { LBL_ET,		TRUE,	"ET",		""	},
    { LBL_EV,		TRUE,	"EV",		""	},
    { LBL_EX,		TRUE,	"EX",		"  "	},
    { LBL_EXKEY,	FALSE,	"EXKEY",	""	},
    { LBL_EXSUB,	TRUE,	"EXSUB",	""	},
    { LBL_F,		TRUE,	"F",		""	},
    { LBL_ID,		TRUE,	"ID",		""	},
    { LBL_IDSUB,	TRUE,	"IDSUB",	""	},
    { LBL_IF,		TRUE,	"IF",		""	},
    { LBL_IMG,		FALSE,	"IMG",		""	},
    { LBL_J2E,		TRUE,	"J2E",		""	},
    { LBL_KEY,		FALSE,	"KEY",		""	},
    { LBL_KEYEX,	FALSE,	"KEYEX",	""	},
    { LBL_KEYID,	FALSE,	"KEYID",	""	},
    { LBL_LOC,		FALSE,	"LOC",		""	},
    { LBL_MEN,		TRUE,	"MEN",		""	},
    { LBL_MN,		TRUE,	"MN",		""	},
    { LBL_MNSUB,	TRUE,	"MNSUB",	""	},
    { LBL_NB,		TRUE,	"NB",		""	},
    { LBL_NBP,		TRUE,	"NBP",		"  "	},
    { LBL_NBPSUB,	TRUE,	"NBPSUB",	""	},
    { LBL_NBSUB,	TRUE,	"NBSUB",	""	},
    { LBL_NSBUS,	TRUE,	"NSBUS",	""	},
    { LBL_OT,		TRUE,	"OT",		""	},
    { LBL_OTSUB,	TRUE,	"OTSUB",	""	},
    { LBL_PR,		TRUE,	"PR",		" "	},
    { LBL_PS,		TRUE,	"PS",		"\n\201\241"	},
    { LBL_RA,		FALSE,	"RA",		""	},
    { LBL_RES,		FALSE,	"RES",		""	},
    { LBL_RN,		FALSE,	"RN",		""	},
    { LBL_SM,		TRUE,	"SM",		""	},
    { LBL_SND,		FALSE,	"SND",		""	},
    { LBL_SRC,		TRUE,	"SRC",		""	},
    { LBL_SRCSUB,	TRUE,	"SRCSUB",	""	},
    { LBL_TL,		TRUE,	"TL",		""	},
    { LBL_TLSUB,	TRUE,	"TLSUB",	""	},
    { LBL_TQ,		TRUE,	"TQ",		"\n"	},
    { LBL_TR,		TRUE,	"TR",		""	},
    { LBL_TRSUB,	TRUE,	"TRSUB",	""	},
    { LBL_ZEX,		TRUE,	"ZEX",		""	},
    { LBL_ZID,		TRUE,	"ZID",		""	},
    { LBL_ERROR,	FALSE,	NULL,		NULL	}
};

LBLTBL	*get_label();
int	load_format();
int	suppress();

/*
 * get_label - \215\163\223\252\202\314\203\211\203\170\203\213\202\360\216\257\225\312\202\267\202\351
 */
LBLTBL *
get_label(str)
uchr	*str;
{
    uchr	*p, *q;
    LBLTBL	*lp;
    uchr	tmp[128];

    p = str;
    q = tmp;
    while (*p && isalpha(*p) && isupper(*p))
	*q++ = *p++;
    *q = '\0';
    if (!strncmp(str, "J2E", 3))
	strcpy(tmp, "J2E");
    lp = labeltbl;
    while (lp->label != NULL) {
	if (!strncmp(tmp, lp->label, strlen(tmp)))
	    break;
	lp++;
    }
    if (lp->id == LBL_OT) {
	if (isdigit(str[2]) && str[3] == 'S')
	    lp++;	/* OT\202\314\221\343\202\355\202\350\202\311OTSUB\202\360\216\167\202\267\202\346\202\244\202\311\202\267\202\351 */
    }
    return lp;
}

/*
 * load_format - \217\221\216\256\203\164\203\100\203\103\203\213\202\360\223\307\202\361\202\305\203\211\203\170\203\213\225\134\202\360\215\130\220\126\202\267\202\351
 */
int
load_format(file)
uchr	*file;
{
    int		c, line, err, disp;
    uchr	*p, *q;
    FILE	*fp;
    LBLTBL	*lp;
    uchr	buf[256], tmp[256];

    if ((fp = fopen(file, "r")) == NULL) {
	outstr("ERR: \225\134\216\246\217\221\216\256\203\164\203\100\203\103\203\213\202\252\203\111\201\133\203\166\203\223\202\305\202\253\202\334\202\271\202\361\n");
	return ERR;
    }
    line = 0;
    err = 0;
    while (fgets(buf, 256, fp) != NULL) {
	line++;
	buf[strlen(buf) - 1] = '\0';
	disp = TRUE;
	p = skipsp(buf);
	if (*p == '\0' || *p == ';')
	    continue;
	if (*p == '!') {
	    disp = FALSE;
	    p++;
	}
	p = skipsp(p);
	lp = get_label(p);
	if (lp->id == LBL_ERROR) {
	    sprintf(tmp,
		"ERR: line %d: \217\221\216\256\215\200\226\332\226\274\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
	    outstr(tmp);
	    err++;
	    continue;
	}
	while (*p && (isupper(*p) || isdigit(*p)))
	    p++;
	p = skipsp(p);
	if (*p == '=')
	    p++;
	p = skipsp(p);
	if (*p != '"') {
	    sprintf(tmp,
		"ERR: line %d: \217\221\216\256\225\266\216\232\227\361\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
	    outstr(tmp);
	    err++;
	    continue;
	}
	p++;
	q = tmp;
	while (*p && *p != '"') {
	    if (*p == '\\') {
		p = escchar(p, &c);
		if (c == '\\' || c == '<' || c == '>')
		    *q++ = '\\';
		*q++ = c;
		continue;
	    }
	    if (SJIS1(*p)) {
		*q++ = *p++;
		*q++ = *p++;
		continue;
	    }
	    if (*p == '<' || *p == '>' || *p == '#')
		*q++ = '\\';
	    *q++ = *p++;
	}
	if (*p != '"') {
	    sprintf(tmp,
		"ERR: line %d: \217\221\216\256\225\266\216\232\227\361\202\252\220\263\202\265\202\255\202\240\202\350\202\334\202\271\202\361\n", line);
	    outstr(tmp);
	    err++;
	    continue;
	}
	*q = '\0';
	if ((q = strpool(tmp)) == NULL) {
	    outstr("ERR: \203\201\203\202\203\212\201\133\202\252\221\253\202\350\202\334\202\271\202\361\n");
	    err++;
	    continue;
	}
	lp->leader = q;
	lp->disp = disp;
    }
    fclose(fp);
    if (err)
	return ERR;
    return OK;
}

/*
 * suppress - \225\266\216\232\227\361\202\305\216\167\222\350\202\263\202\352\202\275\223\301\222\350\215\200\226\332\202\314\225\134\216\246\202\360\227\175\220\247\202\267\202\351
 */
int
suppress(str)
uchr	*str;
{
    if (str == NULL)
	return OK;
    if (!strcmp(str, "a"))
	str = "teio";
    while (*str) {
	switch (*str) {
	case 't':
	    get_label("TR")->disp = FALSE;
	    get_label("TRSUB")->disp = FALSE;
	    break;
	case 'e':
	    get_label("EX")->disp = FALSE;
	    get_label("EXSUB")->disp = FALSE;
	    get_label("ZEX")->disp = FALSE;
	    break;
	case 'i':
	    get_label("ID")->disp = FALSE;
	    get_label("IDSUB")->disp = FALSE;
	    get_label("ZID")->disp = FALSE;
	    break;
	case 'o':
	    get_label("OT")->disp = FALSE;
	    get_label("OTSUB")->disp = FALSE;
	    break;
	default:
	    return ERR;
	}
	str++;
    }
    return OK;
}

/* -------------------- \226\173\225\266\225\134\216\246 -------------------- */

#define	MAIN_DAT	"main.txt"	/* \226\173\225\266				*/

#define	REV_LF		0xf5		/* 0x0a(LF)\202\314\203\162\203\142\203\147\224\275\223\135		*/

#define	LBUFSIZ		4096		/* \215\163\203\157\203\142\203\164\203\100\201\133\202\314\203\124\203\103\203\131		*/
					/* \214\273\203\157\201\133\203\127\203\207\203\223\202\314main.dat\202\314\215\305\221\345	*/
					/* \215\163\222\267\202\3152389\202\276\202\252\227\135\227\124\202\360\202\335\202\304\202\250\202\255	*/

/*
 * \224\255\211\271\213\114\215\206\227\361\202\315\201\165{;\201\166\202\306\201\1652}\201\166\202\305\210\315\202\334\202\352\202\351(VEGA\221\316\211\236\216\144\227\154)
 */
#define	BP_LEADER	'{'		/* csrd\221\244\223\137\216\232\224\255\211\271\213\114\215\206\227\361\212\112\216\156	*/
#define	BP_START	';'		/* \223\137\216\232\224\255\211\271\213\114\215\206\227\361\212\112\216\156		*/
#define	BP_END		'2'		/* \223\137\216\232\224\255\211\271\213\114\215\206\227\361\217\111\227\271		*/
#define	BP_TRAILER	'}'		/* csrd\221\244\223\137\216\232\224\255\211\271\213\114\215\206\227\361\217\111\227\271	*/

/*
 * BPTBL.gc\202\314\203\175\203\130\203\116
 */
#define	CHMASK		0x00ff		/* \225\266\216\232\203\122\201\133\203\150			*/
#define	GRMASK		0x1f00		/* \203\164\203\110\203\223\203\147\203\117\203\213\201\133\203\166		*/
#define	ITMASK		0x8000		/* \203\103\203\136\203\212\203\142\203\116			*/

#define	GRNMASK		0x0f00		/* \203\164\203\110\203\223\203\147\203\117\203\213\201\133\203\166\224\324\215\206		*/
#define	GRUNDEF		0x1000		/* \203\164\203\110\203\223\203\147\203\117\203\213\201\133\203\166\226\242\222\350\213\140	*/

#define	GNUM(g)		(((g) == 0x0900)? 99: ((g) >> 8))

typedef	struct _bptbl_t {
    unt		gc;			/* \203\164\203\110\203\223\203\147\203\117\203\213\201\133\203\166+\225\266\216\232\203\122\201\133\203\150	*/
    uchr	bps[4];			/* \223\137\216\232\224\255\211\271\213\114\215\206\227\361		*/
} BPTBL;

/*
 * \223\137\216\232\224\255\211\271\213\114\215\206\227\361\225\134
 */
BPTBL bptbl[] = {
    { 'a',	"A"	},		/* a				*/
    { 'b',	"B"	},		/* b				*/
    { 'd',	"D"	},		/* d				*/
    { 'e',	"E"	},		/* e				*/
    { 'f',	"F"	},		/* f				*/
    { 'g',	"G"	},		/* g				*/
    { 'h',	"H"	},		/* h				*/
    { 'i',	"I"	},		/* i				*/
    { 'j',	"J"	},		/* j				*/
    { 'k',	"K"	},		/* k				*/
    { 'l',	"L"	},		/* l				*/
    { 'm',	"M"	},		/* m				*/
    { 'n',	"N"	},		/* n				*/
    { 'o',	"O"	},		/* o				*/
    { 'p',	"P"	},		/* p				*/
    { 'r',	"R"	},		/* r				*/
    { 's',	"S"	},		/* s				*/
    { 't',	"T"	},		/* t				*/
    { 'u',	"U"	},		/* u				*/
    { 'v',	"V"	},		/* v				*/
    { 'w',	"W"	},		/* w				*/
    { 'z',	"Z"	},		/* z				*/
    { 0x00e0,	"^A"	},		/* a\201\115				*/
    { 0x00e1,	"_A"	},		/* a\201\114				*/
    { 0x00e6,	"%"	},		/* ae\202\314\215\207\216\232			*/
    { 0x00e8,	"^E"	},		/* e\201\115				*/
    { 0x00e9,	"_E"	},		/* e\201\114				*/
    { 0x00ec,	"^I"	},		/* i\201\115				*/
    { 0x00ed,	"_I"	},		/* i\201\114				*/
    { 0x00f0,	"\\]"	},		/* the\202\314th			*/
    { 0x00f2,	"^O"	},		/* o\201\115				*/
    { 0x00f3,	"_O"	},		/* o\201\114				*/
    { 0x00f9,	"^U"	},		/* u\201\115				*/
    { 0x00fa,	"_U"	},		/* u\201\114				*/
    { 0x0141,	"*"	},		/* \202\306\202\263\202\251\202\314\202\310\202\242a		*/
    { 0x0142,	"\\<"	},		/* \215\266\202\314\212\112\202\242\202\275o			*/
    { 0x0143,	"_+"	},		/* \211\241\226\137\202\314\202\310\202\242A\202\311\201\114		*/
    { 0x014a,	"_%"	},		/* ae\202\314\215\207\216\232\202\311\201\114			*/
    { 0x014b,	"^%"	},		/* ae\202\314\215\207\216\232\202\311\201\115			*/
    { 0x014c,	"_5"	},		/* \202\240\202\242\202\334\202\242\225\352\211\271\202\311\201\114		*/
    { 0x014d,	"^5"	},		/* \202\240\202\242\202\334\202\242\225\352\211\271\202\311\201\115		*/
    { 0x014e,	"_*"	},		/* \202\306\202\263\202\251\202\314\202\310\202\242a\202\311\201\114		*/
    { 0x014f,	"^*"	},		/* \202\306\202\263\202\251\202\314\202\310\202\242a\202\311\201\115		*/
    { 0x0150,	":"	},		/* she\202\314sh			*/
    { 0x0151,	"_\\<"	},		/* \215\266\202\314\212\112\202\242\202\275o\202\311\201\114		*/
    { 0x0152,	"^\\<"	},		/* \215\266\202\314\212\112\202\242\202\275o\202\311\201\115		*/
    { 0x0153,	"_\\>"	},		/* \212\333\202\242E\202\311\201\114			*/
    { 0x0154,	"^\\>"	},		/* \212\333\202\242E\202\311\201\115			*/
    { 0x016a,	"?"	},		/* through\202\314th			*/
    { 0x0173,	"!"	},		/* \217\137\202\347\202\251\202\242g			*/
    { 0x0176,	"$"	},		/* sing\202\314ng			*/
    { 0x02e1,	"^+"	},		/* \211\241\226\137\202\314\202\310\202\242A\202\311\201\115		*/
    { 0x03b9,	"5"	},		/* \202\240\202\242\202\334\202\242\225\352\211\271			*/
    { 0x0477,	"3"	}		/* \220\114\202\316\202\267\211\271			*/
};

#define	NBPS	(sizeof(bptbl) 	/ sizeof(bptbl[0]))

int	do_braille = FALSE;		/* \223\137\216\232\224\255\211\271\213\114\215\206\225\317\212\267\202\360\215\163\202\244\202\251?	*/
int	nobracket = FALSE;		/* \214\263\202\314\224\255\211\271\210\315\202\335[]\202\360\215\355\217\234\202\267\202\351\202\251?	*/
int	firstidiom;			/* \202\273\202\314\225\151\216\214\202\314\215\305\217\211\202\314\220\254\213\345		*/
int	have_en;			/* \214\251\217\157\202\265\222\120\214\352\225\134\216\246\222\274\214\343(\226\263\211\374\215\163\216\236)	*/
uchr	line[LBUFSIZ];			/* 1\215\163\203\146\201\133\203\136\225\134\216\246\227\160\215\354\213\306\227\314\210\346	*/

uchr	*bpstr();
uchr	*bp_transfer();
BFILE	*dic_open();
int	dgetline();
int	showline();
int	show_data();
int	show_idiom();
int	show_header();
int	show_version();

/*
 * bpstr - \225\266\216\232\202\311\221\316\211\236\202\267\202\351\223\137\216\232\224\255\211\271\213\114\215\206\227\361\202\360\225\324\202\267
 */
uchr *
bpstr(gc)
unt	gc;
{
    int		i;

    gc &= (GRNMASK|CHMASK);	/* GRUNDEF\202\315<G0>\202\306\223\257\202\266 */
    for (i = 0; i < NBPS; i++) {
	if (gc == bptbl[i].gc)
	    return bptbl[i].bps;
    }
    return NULL;
}

/*
 * bp_transfer - \224\255\211\271\213\114\215\206\227\361\202\314\223\137\226\363\202\360\216\216\202\335\202\351
 *		 \214\213\211\312\202\315\223\156\202\263\202\352\202\275\203\157\203\142\203\164\203\100\201\133\202\360\217\343\217\221\202\253\202\267\202\351\214\140\202\305\217\221\202\253\215\236\202\336
 *		 \225\317\212\267\214\213\211\312\225\266\216\232\227\361\202\305\202\315\203\136\203\117\202\314\212\112\225\302\202\252\213\266\202\301\202\304\202\242\202\351\211\302\224\134\220\253\202\240\202\350
 */
uchr *
bp_transfer(line)
uchr	*line;
{
    int		level;
    unt		c, it, nit, g, ng;
    uchr	*p, *q;
    unt		*u, *uu, *upto;
    static unt	tmp[1024];	/* \225\317\212\267\214\263\203\146\201\133\203\136\202\315\215\305\221\345776\203\157\203\103\203\147	*/

#ifdef DEBUG
    if (debug)
	printf("line in= \"%s\"\n", line);
#endif
    /*
     * tmp\202\311\203\164\203\110\203\223\203\147/\203\103\203\136\203\212\203\142\203\116\217\356\225\361\225\164\202\253\225\266\216\232\227\361\202\360\221\147\202\335\227\247\202\304\202\351
     */
    g = GRUNDEF;
    it = 0;
    p = line;
    u = tmp;
    while (*p) {
	if (*p == '<') {
	    if (p[1] == 'G')
		g = (p[2] - '0') << 8;
	    else if (p[1] == '/' && p[2] == 'G')
		g = GRUNDEF;
	    else if (p[1] == 'I' && p[2] == '>')
		it = ITMASK;
	    else if (p[1] == '/' && p[2] == 'I' && p[3] == '>')
		it = 0;
	    else {
		while (*p != '>')
		    *u++ = it | g | *p++;
		*u++ = it | g | *p++;
		continue;
	    }
	    while (*p++ != '>')
		;
	    continue;
	}
	if (SJIS1(*p)) {
	    *u++ = it | g | *p++;
	    *u++ = it | g | *p++;
	    continue;
	}
	if (*p == '#' && p[3] == '#') {
	    *u++ = it | g | hexval(p + 1);
	    p += 4;
	    continue;
	}
	*u++ = it | g | *p++;
    }
    *u = '\0';
    /*
     * \224\255\211\271\213\114\215\206\227\361\202\360\211\360\216\337\202\265\202\310\202\252\202\347line\202\360\215\354\202\350\222\274\202\267
     */
    g = GRUNDEF;
    it = 0;
    level = 0;
    p = line;
    u = tmp;
    upto = NULL;
    while (*u) {
	nit = *u & ITMASK;
	if (nit != it) {
	    strcpy(p, nit? "<I>": "</I>");
	    while (*p++ != '>')
		;
	    it = nit;
	}
	ng = *u & GRMASK;
	if (ng != g) {
	    if (g != GRUNDEF) {
		sprintf(p, "</G%d>", GNUM(g));
		while (*p++ != '>')
		    ;
	    }
	    if (ng != GRUNDEF) {
		sprintf(p, "<G%d>", GNUM(ng));
		while (*p++ != '>')
		    ;
	    }
	    g = ng;
	}
	if ((*u & GRUNDEF) != 0 && SJIS1(*u & CHMASK)) {
	    *p++ = (*u++ & CHMASK);
	    *p++ = (*u++ & CHMASK);
	    continue;
	}
	if (upto && u < upto)
	    goto notrans;
	switch (*u & (GRNMASK|CHMASK)) {
	case '[':
	    if (!nobracket)
		*p++ = *u & CHMASK;
	    level++;
	    break;
	case ']':
	    if (!nobracket)
		*p++ = *u & CHMASK;
	    if (level > 0)
		level--;
	    break;
	default:
	    if (level > 0 && bpstr(*u) != NULL) {
		uu = u;
		while (*uu && bpstr(*uu)) {
		    uu++;
		}
		c = *uu & (GRMASK|CHMASK);
		if (c == ' ' || c == ';' || c == ',' ||
		    c == '-' || c == ']' || c == '|') {
		    if (g != GRUNDEF) {
			sprintf(p, "</G%d>", GNUM(g));
			while (*p++ != '>')
			    ;
			g = GRUNDEF;
		    }
		    *p++ = BP_LEADER;
		    *p++ = BP_START;
		    while (u < uu) {
			q = bpstr(*u++);
			while (*q)
			    *p++ = *q++;
		    }
		    *p++ = BP_END;
		    *p++ = BP_TRAILER;
		    u = uu;
		    continue;
		}
		upto = uu;
	    }
	notrans:
	    ng = *u & GRMASK;
	    c = *u & CHMASK;
	    if (ng == GRUNDEF) {
		*p++ = c;
	    } else if (ng != 0 ||
		c >= 0x80 || c == '/' || c == ':' || c == '=') {
		sprintf(p, "#%02x#", c);
		p += 4;
	    } else {
		*p++ = c;
	    }
	}
	u++;
    }
    *p = '\0';
    if (it) {
	strcpy(p, "</I>");
	p += 4;
    }
    if (g != GRUNDEF)
	sprintf(p, "</G%d>", GNUM(g));
#ifdef DEBUG
    if (debug)
	printf("line out=\"%s\"\n", line);
#endif
    return line;
}

/*
 * dic_open - \216\253\217\221\203\146\201\133\203\136\203\164\203\100\203\103\203\213\202\360bopen()\202\267\202\351
 */
BFILE *
dic_open(dicdir, dicfile)
uchr	*dicdir, *dicfile;
{
    BFILE	*bfp;
    uchr	buf[256];

    sprintf(buf, "%s/%s", dicdir, dicfile);
    if ((bfp = bopen(buf)) == NULL) {
	outstr("ERR: \216\253\217\221\203\164\203\100\203\103\203\213\202\252\203\111\201\133\203\166\203\223\202\305\202\253\202\334\202\271\202\361(");
	outstr(buf);
	outstr(")\n");
	return NULL;
    }
    return bfp;
}

/*
 * dgetline - \226\173\225\266\203\146\201\133\203\136\202\3601\215\163\223\307\202\335\215\236\202\335\201\101\211\374\215\163\202\360\217\234\202\242\202\275\222\267\202\263\202\360\225\324\202\267
 */
int
dgetline(bfp, buf)
BFILE	*bfp;
uchr	*buf;
{
    int		c;
    uchr	*p;

    p = buf;
    while ((c = bgetc(bfp)) != REV_LF)
	*p++ = ~c;
    *p = '\0';
    return p - buf;
}

/*
 * showline - 1\215\163\202\314\226\173\225\266\203\146\201\133\203\136\202\360\225\134\216\246\202\267\202\351
 */
int
showline(line)
uchr	*line;
{
    uchr	*p, *q;
    LBLTBL	*lp;

    if (rawmode) {
	outstr(line);
	outstr("\n");
	return OK;
    }
    lp = get_label(line);
    if (lp->id == LBL_ERROR) {
#ifdef DEBUG
	if (debug) {
	    outstr("ERR> ");
	    outstr(line);
	    outstr("\n");
	}
#endif
	return ERR;
    }
    if (!lp->disp) {
#ifdef DEBUG
	if (debug) {
	    outstr("[");
	    outstr(lp->label);
	    outstr("*]\n");
	}
#endif
	return OK;
    }
    if (have_en && lp->id != LBL_PR) {
	set_indent(0);
	outstr("\n");
    }
    have_en = FALSE;
#ifdef DEBUG
    if (debug) {
	outstr("[");
	outstr(lp->label);
	outstr("] ");
    }
#endif
    p = skipeq(line);
    if (*p == '\0')
	return OK;
    set_indent(nspaces(lp->leader));
    outstr(lp->leader);
    switch (lp->id) {
    case LBL_DR:
	q = p;
	while (*q && *q != '|')
	    q++;
	*q = 0;
	outstr(p);
	set_indent(0);
	outstr("\n");
	break;
    case LBL_EN:
	q = p;
	while (*q && *q != '|')
	    q++;
	*q = 0;
	outstr(p);
	have_en = TRUE;
	break;
    case LBL_ID:
	if (firstidiom) {
	    outstr("\n\201\171\220\254\213\345\201\172\n");
	    firstidiom = FALSE;
	}
	goto normal;
    case LBL_PS:
	firstidiom = TRUE;
	goto normal;
    case LBL_TRSUB:
	if (!strncmp(p, "\201\171", 2)) {
	    p += 2;
	    while (*p && strncmp(p, "\201\172", 2)) {
		if (SJIS1(*p))
		    p++;
		p++;
	    }
	    if (*p)
		p += 2;
	}
	goto normal;
    case LBL_PR:
    case LBL_IF:
	if (do_braille)
	    p = bp_transfer(p);
	goto normal;
    default:
    normal:
	outstr(p);
	set_indent(0);
	outstr("\n");
	break;
    }
    return OK;
}

/*
 * show_data - \226\173\225\266\203\146\201\133\203\136\202\360\225\134\216\246\202\267\202\351
 */
int
show_data(bfp, dpos, dlen)
BFILE	*bfp;
long	dpos, dlen;
{
    int		len;

    if (bseek(bfp, dpos) == ERR)
	return ERR;
    firstidiom = TRUE;
    while (dlen > 0L) {
	len = dgetline(bfp, line);
	showline(line);
	dlen -= (long)(len + 1);
    }
    return OK;
}

/*
 * show_idiom - \220\254\213\345\201\105\227\160\227\341\202\314\214\237\215\365\214\213\211\312\202\360\225\134\216\246\202\267\202\351
 */
int
show_idiom(bfp, dpos)
BFILE	*bfp;
long	dpos;
{
    int		idiom;
    long	npos;
    uchr	*p, *q;
    LBLTBL	*lp;
    uchr	ps[128], tr[32];

    /*
     * \202\240\202\306\202\305\215\200\226\332\220\346\223\252\202\334\202\305\221\153\202\351\225\113\227\166\202\252\202\240\202\351\202\314\202\305
     * \220\254\213\345\201\105\227\160\227\341\202\360\223\307\202\335\217\157\202\271\202\351\202\276\202\257\202\314\227\135\227\124\202\360\216\143\202\265\202\304
     * \203\164\203\100\203\103\203\213\220\346\223\252\212\361\202\350\202\311\202\242\202\301\202\275\202\361bseek()\202\265
     * \202\273\202\352\202\251\202\347\226\173\227\210\202\314\210\312\222\165\202\311bseek()\202\267\202\351
     * \202\261\202\244\202\267\202\351\202\261\202\306\202\305read()\202\314\211\361\220\224\202\252\214\270\202\347\202\271\202\351\202\315\202\270
     */
    npos = dpos - (BBUFSIZ - 512);
    if (npos < 0L)
	npos = 0;
    if (bseek(bfp, npos) == ERR)
	return ERR;
    if (bseek(bfp, dpos) == ERR)
	return ERR;
    dgetline(bfp, line);
    if (rawmode) {
	outstr(line);
	outstr("\n");
	return OK;
    }
    if (*line == 'I' || *line == 'D') {
	lp = get_label("ZID");
	idiom = TRUE;
    } else {
	lp = get_label("ZEX");
	idiom = FALSE;
    }
    if (!lp->disp)
	return OK;
    set_indent(nspaces(lp->leader));
    outstr(lp->leader);
    outstr(skipeq(line));
    /*
     * \203\164\203\100\203\103\203\213\220\346\223\252\202\311\221\153\202\301\202\304EN=\202\360\222\124\202\265\201\101
     * \202\273\202\261\202\251\202\347\214\273\215\335\210\312\222\165\202\334\202\305\225\113\227\166\202\310\217\356\225\361\202\360\223\307\202\361\202\305\225\134\216\246\202\267\202\351
     */
    if (bseek(bfp, dpos) == ERR)
	return ERR;
    for (;;) {
	if ((bgetcbw(bfp) ^ 0xff) == '=' &&
	    (bgetcbw(bfp) ^ 0xff) == 'N' &&
	    (bgetcbw(bfp) ^ 0xff) == 'E' &&
	    (bgetcbw(bfp) ^ 0xff) == '\n')
	    break;
    }
    bgetc(bfp);
    outstr(" (");
    dgetline(bfp, line);
    p = q = skipeq(line);
    while (*q && *q != '|')
	q++;
    *q = '\0';
    outstr(p);
    *ps = *tr = '\0';
    while (btell(bfp) < dpos) {
	dgetline(bfp, line);
	if (!strncmp(line, "PS=", 3)) {
	    strcpy(ps, line + 3);
	    *tr = '\0';
	    continue;
	}
	if (!idiom && !strncmp(line, "TR", 2)) {
	    p = line + 2;
	    if (*p == 'S') /* TRSUB */
		p += 3;
	    q = tr;
	    while (*p && *p != '=')
		*q++ = *p++;
	    *q = '\0';
	}
    }
    if (*ps) {
	outstr(", ");
	outstr(ps);
	if (*tr)
	    outstr(tr);
	if (idiom)
	    outstr(" \220\254\213\345");
    }
    outstr(")");
    set_indent(0);
    outstr("\n");
    return OK;
}

/*
 * show_header - EN\202\311\202\240\202\351\214\251\217\157\202\265\203\146\201\133\203\136\202\360\225\134\216\246\202\267\202\351
 */
int
show_header(bfp, dpos)
BFILE	*bfp;
long	dpos;
{
    uchr	*p, *q;

    if (bseek(bfp, dpos) == ERR)
	return ERR;
    for (;;) {
	if (dgetline(bfp, line) <= 0)
	    return ERR;
	if (get_label(line)->id != LBL_EN)
	    continue;
	p = skipeq(line);
	q = p;
	while (*q && *q != '|')
	    q++;
	*q = 0;
	outstr(p);
	break;
    }
    return OK;
}

/*
 * show_version - \216\253\217\221\226\173\225\266\203\146\201\133\203\136\202\314\203\157\201\133\203\127\203\207\203\223\202\360\225\134\216\246\202\267\202\351
 */
int
show_version(dicdir)
uchr	*dicdir;
{
    uchr	*p;
    BFILE	*bfp;

    if ((bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
	return ERR;
    bseek(bfp, 0L);
    while (dgetline(bfp, line) > 0 && !strncmp(line, "CMT=", 4)) {
	if (rawmode) {
	    outstr(line);
	    outstr("\n");
	    continue;
	}
	p = line + 4;
	if (!strncmp(p, "***", 3) ||
	    !strncmp(p, "TITLE:", 6) ||
	    !strncmp(p, "Based", 5) ||
	    !strncmp(p, "DATE:", 5)) {
	    outstr(p);
	    outstr("\n");
	}
    }
    bclose(bfp);
    return OK;
}

/* --------------- \220\254\213\345\201\105\227\160\227\341\214\237\215\365\215\354\213\306\203\164\203\100\203\103\203\213\212\307\227\235 --------------- */

#define	TT0SIZ		1000		/* \214\237\215\365\214\213\211\312\212\151\224\133\224\172\227\361\202\314\227\166\221\146\220\224	*/

/*
 * \203\146\201\133\203\136\212\151\224\133\220\346
 */
#define	TF_USE0		0		/* \224\172\227\3610\202\276\202\257\202\360\216\147\202\244		*/
#define	TF_USE01	1		/* \224\172\227\3610\202\306\203\164\203\100\203\103\203\2131\202\360\216\147\202\244	*/
#define	TF_USE1		2		/* \203\164\203\100\203\103\203\2131\202\276\202\257\202\360\216\147\202\244		*/
#define	TF_USE12	3		/* \203\164\203\100\203\103\203\2131\202\306\203\164\203\100\203\103\203\2132\202\360\216\147\202\244	*/

typedef	struct _tftbl {
    long	val;			/* \203\146\201\133\203\136\222\154			*/
    long	num;			/* \203\146\201\133\203\136\220\224			*/
    int		n0;			/* tt0\202\314\203\146\201\133\203\136\220\224		*/
    int		n0x;			/* tt0\202\314\227\160\227\341\212\112\216\156\210\312\222\165/\214\273\215\335\210\312\222\165	*/
    long	n1;			/* tf1\202\314\203\146\201\133\203\136\220\224		*/
    long	n1x;			/* tf1\202\314\214\273\215\335\210\312\222\165		*/
    long	n2;			/* tf2\202\314\203\146\201\133\203\136\220\224		*/
    long	n2x;			/* tf2\202\314\214\273\215\335\210\312\222\165		*/
    long	*tt0;			/* \215\354\213\306\224\172\227\3610			*/
    uchr	*tf1;			/* \215\354\213\306\203\164\203\100\203\103\203\2131		*/
    uchr	*tf2;			/* \215\354\213\306\203\164\203\100\203\103\203\2132		*/
    FILE	*fp1;			/* \215\354\213\306\203\164\203\100\203\103\203\2131 FP		*/
    FILE	*fp2;			/* \215\354\213\306\203\164\203\100\203\103\203\2132 FP		*/
    long	v1;			/* tf1\202\314\220\346\223\307\202\335\203\146\201\133\203\136		*/
    long	v2;			/* tf2\202\314\220\346\223\307\202\335\203\146\201\133\203\136		*/
    int		use;			/* \203\146\201\133\203\136\212\151\224\133\220\346			*/
    int		open;			/* \203\111\201\133\203\166\203\223\202\263\202\352\202\304\202\242\202\351\202\251?	*/
} TFTBL;

TFTBL	*tftbl;				/* \215\354\213\306\203\164\203\100\203\103\203\213\212\307\227\235\225\134		*/

int	setuptmp();
void	cleantmp();
int	twopen();
int	twrite();
int	twclose();
int	tropen();
long	tread();
int	trclose();
int	tisopen();
void	tswap();
void	tfree();
int	lcmp();
long	getlval();

/*
 * setuptmp - \215\354\213\306\203\164\203\100\203\103\203\213\212\307\227\235\225\134\202\360\217\200\224\365\202\267\202\351
 */
int
setuptmp()
{
    int		n, len;
    uchr	*tmpdir;
    TFTBL	*tp;
    uchr	tmp[128];

    tftbl = (TFTBL *)malloc(sizeof(TFTBL) * 3);
    if (tftbl == NULL)
	return ERR;
    len = 0;
    if ((tmpdir = getenv("TMP")) != NULL ||
	(tmpdir = getenv("TEMP")) != NULL) {
	strcpy(tmp, tmpdir);
	bsltosl(tmp);
	len = strlen(tmp);
	if (tmp[len-1] != '/')
	    tmp[len++] = '/';
    }
    tp = tftbl;
    for (n = 0; n < 3; n++) {
	sprintf(tmp+len, "cs%d1XXXXXX", n);
	tp->tf1 = strpool(tmp);
	sprintf(tmp+len, "cs%d2XXXXXX", n);
	tp->tf2 = strpool(tmp);
	if (tp->tf1 == NULL || tp->tf2 == NULL)
	    return ERR;
	mktemp(tp->tf1);
	mktemp(tp->tf2);
	tp->fp1 = NULL;
	tp->fp2 = NULL;
	tp->open = FALSE;
	tp++;
    }
    tp = tftbl;
    for (n = 0; n < 3; n++) {
	tp->tt0 = (long *)malloc(TT0SIZ * sizeof(long));
	/*
	 * \203\201\203\202\203\212\201\133\202\252\225\163\221\253\202\310\202\347tt0\202\315\216\147\202\246\202\310\202\255\202\304\202\340\202\346\202\242
	 * \203\107\203\211\201\133\202\311\202\315\202\265\202\310\202\242
	 */
	tp++;
    }
#ifdef DEBUG
    if (debug2) {
	tp = tftbl;
	for (n = 0; n < 3; n++) {
	    printf("tftbl[%d]:\n", n);
	    printf(" tt0=%s\n", tp->tt0? "(in use)": "(not in use)");
	    printf(" tf1=%s\n", tp->tf1);
	    printf(" tf2=%s\n", tp->tf2);
	    tp++;
	}
    }
#endif
    return OK;
}

/*
 * cleantmp - \216\143\202\301\202\304\202\242\202\351\202\251\202\340\202\265\202\352\202\310\202\242\215\354\213\306\203\164\203\100\203\103\203\213\202\360\215\355\217\234\202\267\202\351
 */
void
cleantmp()
{
    int		n;
    TFTBL	*tp;

    if (tftbl == NULL)
	return;
    tp = tftbl;
    for (n = 0; n < 3; n++) {
	if (tp->fp1 != NULL) {
	    fclose(tp->fp1);
	    tp->fp1 = NULL;
	}
	if (tp->fp2 != NULL) {
	    fclose(tp->fp2);
	    tp->fp2 = NULL;
	}
	unlink(tp->tf1);
	unlink(tp->tf2);
	tp->open = FALSE;
	tp++;
    }
}

/*
 * twopen - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\360\217\221\202\253\217\157\202\265\203\111\201\133\203\166\203\223\202\267\202\351
 */
int
twopen(tn)
int	tn;
{
    TFTBL	*tp;

    tp = &tftbl[tn];
    tp->val = 0L;
    tp->num = 0L;
    tp->n0 = tp->n0x = 0;
    tp->n1 = tp->n1x = 0L;
    tp->n2 = tp->n2x = 0L;
    tp->fp1 = tp->fp2 = NULL;
    tp->use = (tp->tt0 != NULL)? TF_USE0: TF_USE1;
    tp->open = TRUE;
    return OK;
}

/*
 * twrite - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\311\217\221\202\253\217\157\202\267
 */
int
twrite(tn, val)
int	tn;
long	val;
{
    int		n;
    TFTBL	*tp;

    tp = &tftbl[tn];
    switch (tp->use) {
    case TF_USE0:
	if (tp->n0 >= TT0SIZ) {
	    /*
	     * \224\172\227\361\202\311\216\373\202\334\202\347\202\310\202\251\202\301\202\275\202\314\202\305tf1\202\360\216\147\202\244
	     */
	    if ((tp->fp1 = fopen(tp->tf1, "w")) == NULL)
		goto openerr;
#ifdef DEBUG
	    if (debug2) {
		printf("twrite(%d): open tf1(%s), num=%ld, val=%07lx\n",
		    tn, tp->tf1, tp->num, val);
	    }
#endif
	    for (n = tp->n0x; n < TT0SIZ; n++) {
		if (fprintf(tp->fp1, "%07lx\n", tp->tt0[n]) == EOF)
		    goto writeerr;
	    }
	    tp->n0 = tp->n0x;
	    tp->n1 = (long)(TT0SIZ - tp->n0x);
	    if (tp->n0x > 0) {
		/*
		 * \224\172\227\361\222\206\202\311\220\254\213\345\202\306\227\160\227\341\202\314\213\253\212\105\202\252\202\240\202\301\202\275
		 * \220\254\213\345\202\315\216\143\202\265\202\304\227\160\227\341\202\276\202\257tf1\202\311\210\332\223\256\202\263\202\352\202\275
		 * \202\240\202\306\202\305tt0\202\360\203\134\201\133\203\147\202\267\202\351\225\113\227\166\202\315\202\310\202\242(n0x=0)
		 */
		tp->n0x = 0;
		tp->use = TF_USE01;
		goto use01;
	    }
	    /*
	     * \221\123\221\314\202\252tf1\202\311\210\332\223\256\202\263\202\352\202\275
	     */
	    tp->use = TF_USE1;
	    goto use1;
	}
	if (val < tp->val) {
	    /*
	     * \220\254\213\345\202\306\227\160\227\341\202\314\213\253\212\105\202\252\227\210\202\275
	     */
	    tp->n0x = tp->n0;
	}
	tp->tt0[tp->n0++] = val;
	break;
    case TF_USE01:
    use01:
	if (val < tp->val)
	    goto fmterr;
	goto use1write;
    case TF_USE1:
    use1:
	if (val < tp->val) {
	    /*
	     * \220\254\213\345\202\306\227\160\227\341\202\314\213\253\212\105\202\252\227\210\202\275
	     * \210\310\214\343\202\315tf2\202\360\216\147\202\244
	     */
	    if ((tp->fp2 = fopen(tp->tf2, "w")) == NULL)
		goto openerr;
#ifdef DEBUG
	    if (debug2) {
		printf("twrite(%d): open tf2(%s), num=%ld, val=%07lx\n",
		    tn, tp->tf1, tp->num, val);
	    }
#endif
	    tp->use = TF_USE12;
	    goto use12;
	}
    use1write:
	if (fprintf(tp->fp1, "%07lx\n", val) == EOF)
	    goto writeerr;
	tp->n1++;
	break;
    case TF_USE12:
	if (val < tp->val)
	    goto fmterr;
    use12:
	if (fprintf(tp->fp2, "%07lx\n", val) == EOF)
	    return ERR;
	tp->n2++;
	break;
    }
    tp->val = val;
    tp->num++;
    return OK;
openerr:
    outstr("ERR: \215\354\213\306\227\160\203\164\203\100\203\103\203\213\202\252\220\126\213\113\215\354\220\254\202\305\202\253\202\334\202\271\202\361\n");
    return ERR;
writeerr:
    outstr("ERR: \203\146\203\102\203\130\203\116\202\252\202\242\202\301\202\317\202\242\202\305\202\267\n");
    return ERR;
fmterr:
    outstr("PANIC: \220\254\213\345\201\105\227\160\227\341\203\103\203\223\203\146\203\142\203\116\203\130\202\252\227\134\212\372\202\265\202\310\202\242\215\134\221\242\202\305\202\267\n");
#ifdef DEBUG
    if (debug2) {
	printf("num=%ld, cur=%lx, prev=%lx\n", tp->num, val, tp->val);
    }
#endif
    return ERR;
}

/*
 * twclose - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\360\203\116\203\215\201\133\203\131\202\267\202\351
 */
int
twclose(tn)
int	tn;
{
    TFTBL	*tp;

    tp = &tftbl[tn];
#ifdef DEBUG
    if (debug2) {
	printf("twclose(%d): n0=%d, n1=%ld, n2=%ld\n",
	    tn, tp->n0, tp->n1, tp->n2);
    }
#endif
    switch (tp->use) {
    case TF_USE0:
	if (tp->n0x > 0) {
	    /*
	     * tt0\202\314\222\206\202\311\220\254\213\345\201\105\227\160\227\341\202\314\213\253\212\105\202\252\202\240\202\351
	     * tt0\202\360\203\134\201\133\203\147\202\267\202\351\225\113\227\166\202\252\202\240\202\351
	     */
	    qsort((uchr *)tp->tt0, (size_t)tp->n0, sizeof(long), lcmp);
	}
	break;
    case TF_USE12:
	/*
	 * tf1\202\311\220\254\213\345\201\101tf2\202\311\227\160\227\341\202\252\202\240\202\351
	 */
	if (fclose(tp->fp2) == EOF)
	    goto closeerr;
	tp->fp2 = NULL;
	/* fall thru ... */
    case TF_USE01:
	/*
	 * tt0\202\311\220\254\213\345\201\101tf1\202\311\227\160\227\341\202\252\202\240\202\351
	 */
	/* fall thru ... */
    case TF_USE1:
	/*
	 * tf1\202\314\222\206\202\311\220\254\213\345\202\306\227\160\227\341\202\314\202\242\202\270\202\352\202\251\202\334\202\275\202\315\202\273\202\314\227\274\225\373\202\252\202\240\202\351
	 * \222\154\202\315\222\120\222\262\221\235\211\301\202\265\202\304\202\242\202\351
	 */
	if (fclose(tp->fp1) == EOF)
	    goto closeerr;
	tp->fp1 = NULL;
	break;
    }
    tp->open = FALSE;
    return OK;
closeerr:
    outstr("ERR: \203\146\203\102\203\130\203\116\202\252\202\242\202\301\202\317\202\242\202\305\202\267\n");
    return ERR;
}

/*
 * tropen - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\360\223\307\202\335\215\236\202\335\203\111\201\133\203\166\203\223\202\267\202\351
 */
int
tropen(tn)
int	tn;
{
    TFTBL	*tp;

    tp = &tftbl[tn];
    switch (tp->use) {
    case TF_USE0:
	tp->n0x = 0;
	break;
    case TF_USE01:
	tp->n0x = 0;
	goto open1;
    case TF_USE12:
	if ((tp->fp2 = fopen(tp->tf2, "r")) == NULL)
	    goto openerr;
	tp->v2 = getlval(tp->fp2);
	tp->n2x = 0L;
	/* fall thru ... */
    case TF_USE1:
    open1:
	if ((tp->fp1 = fopen(tp->tf1, "r")) == NULL)
	    goto openerr;
	tp->v1 = getlval(tp->fp1);
	tp->n1x = 0L;
	break;
    }
    tp->open = TRUE;
    return OK;
openerr:
    outstr("ERR: \215\354\213\306\227\160\203\164\203\100\203\103\203\213\202\252\203\111\201\133\203\166\203\223\202\305\202\253\202\334\202\271\202\361\n");
    return ERR;
}

/*
 * tread - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\251\202\347\223\307\202\335\215\236\202\336
 */
long
tread(tn)
int	tn;
{
    long	v;
    TFTBL	*tp;

    tp = &tftbl[tn];
    switch (tp->use) {
    case TF_USE0:
	if (tp->n0x >= tp->n0)
	    return EOF;
	v = tp->tt0[tp->n0x++];
	break;
    case TF_USE01:
	if (tp->n0x >= tp->n0) {
	    if (tp->n1x >= tp->n1) {
		return EOF;
	    } else {
		v = tp->v1;
		tp->v1 = getlval(tp->fp1);
		tp->n1x++;
	    }
	} else {
	    if (tp->n1x >= tp->n1) {
		v = tp->tt0[tp->n0x++];
	    } else {
		if (tp->tt0[tp->n0x] < tp->v1) {
		    v = tp->tt0[tp->n0x++];
		} else {
		    v = tp->v1;
		    tp->v1 = getlval(tp->fp1);
		    tp->n1x++;
		}
	    }
	}
	break;
    case TF_USE1:
	if (tp->n1x >= tp->n1)
	    return EOF;
	v = tp->v1;
	tp->v1 = getlval(tp->fp1);
	tp->n1x++;
	break;
    case TF_USE12:
	if (tp->n1x >= tp->n1) {
	    if (tp->n2x >= tp->n2) {
		return EOF;
	    } else {
		v = tp->v2;
		tp->v2 = getlval(tp->fp2);
		tp->n2x++;
	    }
	} else {
	    if (tp->n2x >= tp->n2) {
		v = tp->v1;
		tp->v1 = getlval(tp->fp1);
		tp->n1x++;
	    } else {
		if (tp->v1 < tp->v2) {
		    v = tp->v1;
		    tp->v1 = getlval(tp->fp1);
		    tp->n1x++;
		} else {
		    v = tp->v2;
		    tp->v2 = getlval(tp->fp2);
		    tp->n2x++;
		}
	    }
	}
	break;
    }
    return v;
}

/*
 * trclose - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\360\203\116\203\215\201\133\203\131\202\267\202\351
 */
int
trclose(tn)
int	tn;
{
    TFTBL	*tp;

    tp = &tftbl[tn];
    switch (tp->use) {
    case TF_USE0:
	break;
    case TF_USE12:
	fclose(tp->fp2);
	tp->fp2 = NULL;
	/* fall thru ... */
    case TF_USE01:
    case TF_USE1:
	fclose(tp->fp1);
	tp->fp1 = NULL;
	break;
    }
    tp->open = FALSE;
    return OK;
}

/*
 * tisopen - \215\354\213\306\203\164\203\100\203\103\203\213\203\132\203\142\203\147\202\315\203\111\201\133\203\166\203\223\202\263\202\352\202\304\202\242\202\351\202\251?
 */
int
tisopen(tn)
int	tn;
{
    return tftbl[tn].open;
}

/*
 * tswap - TFTBL\202\360\214\360\212\267\202\267\202\351
 */
void
tswap(tn0, tn1)
int	tn0, tn1;
{
    TFTBL	tftmp;

    tftmp = tftbl[tn0];
    tftbl[tn0] = tftbl[tn1];
    tftbl[tn1] = tftmp;
}

/*
 * tfree - tt0\202\360\211\360\225\372\202\267\202\351
 */
void
tfree(tn)
int	tn;
{
    TFTBL	*tp;

    if (tftbl == NULL)
	return;
    tp = &tftbl[tn];
    if (tp->tt0) {
	free((char *)tp->tt0);
	tp->tt0 = NULL;
    }
}

/*
 * lcmp - long\222\154\202\307\202\244\202\265\202\314\224\344\212\162(qsort\227\160)
 */
int
lcmp(p1, p2)
long	*p1, *p2;
{
    if (*p1 < *p2)
	    return -1;
    if (*p1 > *p2)
	    return 1;
    return 0;
}

/*
 * getlval - \203\164\203\100\203\103\203\213\202\251\202\3471\215\163\202\31416\220\151\225\266\216\232\227\361\202\360\223\307\202\361\202\305long\202\306\202\265\202\304\225\324\202\267
 */
long
getlval(fp)
FILE	*fp;
{
    uchr	tmp[16];
    long	l;

    if (fgets(tmp, 16, fp) == NULL)
	return (long)EOF;
    sscanf(tmp, "%lx", &l);
    return l;
}

/* -------------------- \214\237\215\365 -------------------- */

#define	EN_SRCH		0		/* \211\160\222\120\214\352\214\237\215\365			*/
#define	TR_SRCH		1		/* \226\363\214\352\214\237\215\365			*/
#define	ID_SRCH		2		/* \220\254\213\345\201\105\227\160\227\341\214\237\215\365		*/

#define	X_MATCH		0		/* \212\256\221\123\210\352\222\166\214\237\215\365			*/
#define	F_MATCH		1		/* \221\117\225\373\210\352\222\166\214\237\215\365			*/
#define	R_MATCH		2		/* \214\343\225\373\210\352\222\166\214\237\215\365			*/
#define	W_MATCH		3		/* \203\217\203\103\203\213\203\150\203\112\201\133\203\150\214\237\215\365		*/

#define	EN_F_IDX	"enf.idx"	/* \211\160\222\120\214\352\221\117\225\373\210\352\222\166\203\103\203\223\203\146\203\142\203\116\203\130	*/
#define	EN_R_IDX	"enr.idx"	/* \211\160\222\120\214\352\214\343\225\373\210\352\222\166\203\103\203\223\203\146\203\142\203\116\203\130	*/
#define	TR_F_IDX	"trf.idx"	/* \226\363\214\352\221\117\225\373\210\352\222\166\203\103\203\223\203\146\203\142\203\116\203\130	*/
#define	TR_R_IDX	"trr.idx"	/* \226\363\214\352\214\343\225\373\210\352\222\166\203\103\203\223\203\146\203\142\203\116\203\130	*/
#define	EN_LIST		"en.dat"	/* \211\160\222\120\214\352\203\212\203\130\203\147			*/
#define	TR_LIST		"tr.dat"	/* \226\363\214\352\203\212\203\130\203\147			*/
#define	ID_IDX		"id.idx"	/* \220\254\213\345\201\105\227\160\227\341\203\103\203\223\203\146\203\142\203\116\203\130	*/
#define	ID_LIST		"id.dat"	/* \220\254\213\345\201\105\227\160\227\341\203\212\203\130\203\147		*/
#define	EN_M_LIST	"miden.txt"	/* \211\160\222\120\214\352\222\206\212\324\210\352\222\166\203\212\203\130\203\147		*/
#define	TR_M_LIST	"midtr.txt"	/* \226\363\214\352\222\206\212\324\210\352\222\166\203\212\203\130\203\147		*/

#define	INODETOP	0x00000100L	/* \203\103\203\223\203\146\203\142\203\116\203\130\202\314\220\346\223\252\203\155\201\133\203\150\210\312\222\165	*/
#define	LENTTOP		0x00000100L	/* \203\212\203\130\203\147\203\107\203\223\203\147\203\212\201\133\202\314\220\346\223\252\210\312\222\165	*/

/*
 * \203\103\203\223\203\146\203\142\203\116\203\130\203\107\203\223\203\147\203\212\201\133
 */
typedef	struct _ient {
    uchr	lstpos[4];		/* \203\212\203\130\203\147\203\164\203\100\203\103\203\213\222\206\202\314\210\312\222\165	*/
    uchr	next[4];		/* \211\272\210\312\203\155\201\133\203\150\202\314\203\155\201\133\203\150\224\324\215\206	*/
} IENT;

/*
 * \203\103\203\223\203\146\203\142\203\116\203\130\203\155\201\133\203\150
 */
typedef	struct _inode {
    uchr	pageno[4];		/* \225\163\226\276				*/
    uchr	last[4];		/* \203\155\201\133\203\150\222\206\202\314\215\305\217\111\203\107\203\223\203\147\203\212\201\133\224\324\215\206	*/
    IENT	ent[7];			/* \203\155\201\133\203\150\222\206\202\314\203\107\203\223\203\147\203\212\201\133		*/
} INODE;

/*
 * \203\212\203\130\203\147\203\107\203\223\203\147\203\212\201\133
 */
typedef	struct _lent {
    long	dpos;			/* \226\173\225\266\203\146\201\133\203\136\210\312\222\165		*/
    long	dlen;			/* \226\173\225\266\203\146\201\133\203\136\203\124\203\103\203\131		*/
    uchr	fkey[128];		/* \221\117\225\373\210\352\222\166\203\114\201\133			*/
    uchr	rkey[128];		/* \214\343\225\373\210\352\222\166\203\114\201\133			*/
    uchr	kstr[256];		/* \214\251\217\157\202\265\225\266\216\232\227\361			*/
} LENT;

/*
 * \220\254\213\345\201\105\227\160\227\341\203\212\203\130\203\147\203\107\203\223\203\147\203\212\201\133
 */
typedef	struct _ilent {
    unt		entid;			/* \217\207\217\230\224\324\215\206			*/
    unt		nent;			/* \223\257\210\352\222\120\214\352\214\302\220\224???		*/
    long	dpos;			/* \226\173\225\266\203\146\201\133\203\136\210\312\222\165		*/
    uchr	idkey[24];		/* \215\134\220\254\222\120\214\352\225\266\216\232\227\361(\215\305\221\34517\225\266\216\232)	*/
} ILENT;

int	stype = EN_SRCH;		/* \214\237\215\365\216\355\225\312(\211\160\222\120\214\352/\226\363\214\352/\220\254\213\345)	*/
int	mtype = X_MATCH;		/* \203\175\203\142\203\140(\212\256\221\123/\221\117\225\373/\214\343\225\373/\222\206\212\324)	*/
uchr	*idxfile = NULL;		/* \214\237\215\365\227\160\203\103\203\223\203\146\203\142\203\116\203\130\203\164\203\100\203\103\203\213	*/
uchr	*lstfile = NULL;		/* \214\237\215\365\227\160\203\212\203\130\203\147\203\164\203\100\203\103\203\213		*/

INODE	inode;				/* \215\354\213\306\227\160			*/
LENT	lent;				/* \215\354\213\306\227\160			*/
ILENT	ilent;				/* \215\354\213\306\227\160			*/

long	val();
uchr	*fmtkstr();
void	getlistkey();
int	getfirstent();
void	getlistent();
int	index_search();
void	getilistent();
int	idiom_search();
uchr	*mgetent();
int	match();
int	wild_search();
uchr	*getword();
int	search();

/*
 * val - \225\266\216\232\227\361\222\206\202\3144\203\157\203\103\203\147\222\154\202\360long\202\311\225\317\212\267
 */
long
val(str)
register uchr	*str;
{
    return ((long)str[3] << 24) +
	   ((long)str[2] << 16) +
	   ((long)str[1] << 8) +
	   (long)str[0];
}

/*
 * fmtkstr - \214\251\217\157\202\265\225\266\216\232\227\361\222\206\202\314\201\165@\201\166\202\360\211\374\215\163\202\311\225\317\212\267\202\267\202\351
 */
uchr *
fmtkstr(kstr)
uchr	*kstr;
{
    int		in;
    uchr	*p, *q;
    static uchr	knew[256];

    in = FALSE;
    p = kstr;
    q = knew;
    while (*p) {
	if (*p == '<') {
	    if (!strncmp(p, "<G99>", 5))
		in = TRUE;
	    else if (!strncmp(p, "</G99>", 6))
		in = FALSE;
	    while (*p != '>')
		*q++ = *p++;
	    *q++ = *p++;
	    continue;
	}
	if (!in && *p == '@') {
	    while (*p == '@')
		p++;
	    *q++ = '\n';
	    continue;
	}
	*q++ = *p++;
    }
    if (q > knew && q[-1] == '\n')
	q--;
    *q = '\0';
    return knew;
}

/*
 * getlistkey - \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\251\202\347\214\251\217\157\202\265\225\266\216\232\227\361\202\360\216\346\202\350\217\157\202\267
 */
void
getlistkey(lfp, buf, mtype)
BFILE	*lfp;
uchr	*buf;
int	mtype;
{
    int		len;
    uchr	*p;

    bgetc(lfp);
    bgetc(lfp);
    if (mtype == R_MATCH) {
	len = bgetc(lfp);
	while (len-- > 0)
	    bgetc(lfp);
	bgetc(lfp);
	bgetc(lfp);
    }
    len = bgetc(lfp);
    p = buf;
    while (len-- > 0)
	*p++ = bgetc(lfp);
    *p = '\0';
}

/*
 * getfirstent - \203\114\201\133\202\311\203\175\203\142\203\140\202\267\202\351\215\305\217\211\202\314\203\107\203\223\203\147\203\212\201\133\202\360\222\124\202\267
 */
int
getfirstent(ifp, lfp, word, ip, np)
BFILE	*ifp, *lfp;
uchr	*word;
INODE	*ip;
int	*np;
{
    int		i, n, last, len, cmp;
    long	ipos, lpos, nodeno;
    uchr	key[128];
#ifdef DEBUG
    int		first, level;
#endif

    ipos = INODETOP;
#ifdef DEBUG
    level = 0;
#endif
    /*
     * \203\147\203\142\203\166\201\140\222\206\212\324\222\151\202\314\217\210\227\235
     */
    for (;;) {
	if (bseek(ifp, ipos) == ERR ||
	    bread(ifp, (uchr *)ip, sizeof(INODE)) < sizeof(INODE)) {
	    outstr("PANIC: \203\103\203\223\203\146\203\142\203\116\203\130\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
	    return ERR;
	}
	if (val(ip->ent[0].next) == 0L) {
	    /*
	     * \215\305\217\111\222\151\201\250\225\312\202\314\225\373\226\100\202\305\217\210\227\235\202\267\202\351
	     */
	    break;
	}
#ifdef DEBUG
	if (debug) {
	    printf("<level %d>\n", ++level);
	    printf("pageno=%08lx, last=%08lx\n",
		val(ip->pageno), val(ip->last));
	    for (i = 0; i <= (int)val(ip->last); i++) {
		lpos = val(ip->ent[i].lstpos);
		printf("e[%d].lstpos=%08lx, e[%d].next=%08lx",
		    i, lpos, i, val(ip->ent[i].next));
		if (lpos > 0L) {
		    bseek(lfp, lpos);
		    getlistkey(lfp, key, mtype);
		    printf(", key=[%s]", key);
		}
		printf("\n");
	    }
	}
#endif
	last = (int)val(ip->last);
	for (i = 0; i < last; i++) {
	    lpos = val(ip->ent[i+1].lstpos);
	    if (bseek(lfp, lpos) == ERR) {
		outstr("PANIC: \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
		return ERR;
	    }
	    getlistkey(lfp, key, mtype);
#ifdef DEBUG
	    if (debug)
		printf("i=%d, word=[%s], key=[%s]\n", i, word, key);
#endif
	    if (strcmp(word, key) <= 0)
		break;
	}
	nodeno = val(ip->ent[i].next);
	ipos = (nodeno - 1L) * sizeof(INODE) + INODETOP;
#ifdef DEBUG
	if (debug)
	    printf("nodeno=%08lx, ipos=%08lx\n", nodeno, ipos);
#endif
    }
    /*
     * \215\305\217\111\222\151\202\314\217\210\227\235
     */
    len = strlen(word);
    last = (int)val(ip->last);
    n = 0;
#ifdef DEBUG
    first = TRUE;
#endif
    for (;;) {
	if (n > last) {
	    if (bread(ifp, (uchr *)ip, sizeof(INODE)) < sizeof(INODE) ||
		val(ip->pageno) == 0L) {
		/*
		 * \203\103\203\223\203\146\203\142\203\116\203\130\202\314\217\111\222\133\202\311\222\102\202\265\202\275
		 */
		return ERR;
	    }
	    last = (int)val(ip->last);
	    n = 0;
	}
#ifdef DEBUG
	if (debug && n == 0) {
	    if (first) {
		printf("<last level>\n");
		first = FALSE;
	    } else {
		printf("<next node>\n");
	    }
	    printf("pageno=%08lx, last=%08lx\n",
		val(ip->pageno), val(ip->last));
	    for (i = 0; i <= (int)val(ip->last); i++) {
		lpos = val(ip->ent[i].lstpos);
		bseek(lfp, lpos);
		getlistkey(lfp, key, mtype);
		printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
	    }
	}
#endif
	lpos = val(ip->ent[n].lstpos);
	if (bseek(lfp, lpos) == ERR) {
	    outstr("PANIC: \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
	    return ERR;
	}
	getlistkey(lfp, key, mtype);
	cmp = strncmp(word, key, len);
	if (cmp < 0)
	    return ERR;
	if (cmp == 0) {
	    if (mtype == X_MATCH && strcmp(word, key) != 0)
		return ERR;
	    *np = n;
	    return OK;
	}
	n++;
    }
    /* NOTREACHED */
}

/*
 * getlistent - \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\251\202\347\214\251\217\157\202\265\203\146\201\133\203\136\202\360\216\346\202\350\217\157\202\267
 */
void
getlistent(lfp, lp)
BFILE	*lfp;
LENT	*lp;
{
    int		i, len;
    uchr	*p;

    bgetc(lfp);
    bgetc(lfp);
    len = bgetc(lfp);
    p = lp->fkey;
    while (len-- > 0)
	*p++ = bgetc(lfp);
    *p = '\0';
    bgetc(lfp);
    bgetc(lfp);
    len = bgetc(lfp);
    p = lp->rkey;
    while (len-- > 0)
	*p++ = bgetc(lfp);
    *p = '\0';
    bgetc(lfp);
    bgetc(lfp);
    lp->dpos = 0L;
    for (i = 0; i < 32; i += 8)
	lp->dpos |= ((long)bgetc(lfp) << i);
    bgetc(lfp);
    bgetc(lfp);
    lp->dlen = 0L;
    for (i = 0; i < 32; i += 8)
	lp->dlen |= ((long)bgetc(lfp) << i);
    for (i = 0; i < 8; i++)
	bgetc(lfp);
    len = bgetc(lfp);
    p = lp->kstr;
    while (len-- > 0)
	*p++ = bgetc(lfp);
    *p = '\0';
}

/*
 * index_search - \212\256\221\123/\221\117\225\373/\214\343\225\373\210\352\222\166\202\305\203\103\203\223\203\146\203\142\203\116\203\130\214\237\215\365\202\360\215\163\202\244
 */
int
index_search(dicdir, showall, word)
uchr	*dicdir, *word;
int	showall;
{
    int		n, last, len, cmp, ret;
    long	lpos;
    uchr	*keyp;
    BFILE	*ifp, *lfp, *bfp;
#ifdef DEBUG
    int		i;
    uchr	key[128];
#endif

    ret = OK;
    ifp = lfp = bfp = NULL;
    if ((ifp = dic_open(dicdir, idxfile)) == NULL ||
	(lfp = dic_open(dicdir, lstfile)) == NULL)
	goto err;
    if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
	goto err;
    keyp = (mtype == R_MATCH)? lent.rkey: lent.fkey;
    len = strlen(word);
    last = (int)val(inode.last);
    for (;;) {
	if (n > last) {
	    if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
		val(inode.pageno) == 0L) {
		/*
		 * \203\103\203\223\203\146\203\142\203\116\203\130\202\314\217\111\222\133\202\311\222\102\202\265\202\275
		 */
		break;
	    }
	    last = (int)val(inode.last);
	    n = 0;
	}
#ifdef DEBUG
	if (debug && n == 0) {
	    printf("<next node>\n");
	    printf("pageno=%08lx, last=%08lx\n",
		val(inode.pageno), val(inode.last));
	    for (i = 0; i <= (int)val(inode.last); i++) {
		lpos = val(inode.ent[i].lstpos);
		bseek(lfp, lpos);
		getlistkey(lfp, key, mtype);
		printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
	    }
	}
#endif
	lpos = val(inode.ent[n].lstpos);
	if (bseek(lfp, lpos) == ERR) {
	    outstr("PANIC: \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
	    goto err;
	}
	getlistent(lfp, &lent);
	cmp = strncmp(word, keyp, len);
	if (cmp < 0)
	    break;
	if (cmp > 0) {
	    n++;
	    continue;
	}
	if (showall || mtype == X_MATCH) {
	    if (mtype == X_MATCH && strcmp(word, keyp) != 0)
		break;
	    if (bfp == NULL) {
		if ((bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
		    goto err;
	    } else {
		outstr("\n");
	    }
#ifdef DEBUG
	    if (debug)
		printf("show_dat: key=[%s], dpos=%lx, dlen=%lx\n",
		    lent.fkey, lent.dpos, lent.dlen);
#endif
	    if (show_data(bfp, lent.dpos, lent.dlen) == ERR)
		goto err;
	} else {
	    outstr(fmtkstr(lent.kstr));
	    if (stype == TR_SRCH) {
		outstr(" [");
		outstr(lent.fkey);
		outstr("]");
	    }
	    outstr("\n");
	}
	n++;
    }
    goto done;
err:
    ret = ERR;
done:
    if (ifp != NULL)
	bclose(ifp);
    if (lfp != NULL)
	bclose(lfp);
    if (bfp != NULL)
	bclose(bfp);
    return ret;
}

/*
 * getilistent - \220\254\213\345\201\105\227\160\227\341\203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\251\202\347\215\134\220\254\222\120\214\352\203\146\201\133\203\136\202\360\216\346\202\350\217\157\202\267
 */
void
getilistent(lfp, ip)
BFILE	*lfp;
ILENT	*ip;
{
    int		i, len;
    uchr	*p;

    ip->entid = bgetc(lfp);
    ip->entid += (bgetc(lfp) << 8);
    len = bgetc(lfp);
    p = ip->idkey;
    while (len-- > 0)
	*p++ = bgetc(lfp);
    *p = '\0';
    bgetc(lfp);
    bgetc(lfp);
    ip->dpos = 0L;
    for (i = 0; i < 32; i += 8)
	ip->dpos |= ((long)bgetc(lfp) << i);
    bgetc(lfp);
    bgetc(lfp);
    ip->nent = bgetc(lfp);
    ip->nent += (bgetc(lfp) << 8);
    for (i = 0; i < 4; i++)
	bgetc(lfp);
}

/*
 * idiom_search - \220\254\213\345\201\105\227\160\227\341\214\237\215\365\202\360\215\163\202\244
 */
int
idiom_search(dicdir, ac, av)
uchr	*dicdir;
int	ac;
uchr	**av;
{
    int		i, n, last, cmp, ret, nomore;
    long	lpos, odpos;
    long	l0, l1, num0, num1, num2, n0, n1;
    uchr	*word;
    BFILE	*ifp, *lfp, *bfp;
#ifdef DEBUG
    uchr	key[128];
#endif

    /*
     * \221\117\217\200\224\365
     */
    for (i = 0; i < ac; i++) {
	/*
	 * \214\237\215\365\216\167\222\350\225\266\216\232\227\361\202\252\220\263\217\355\202\251\202\307\202\244\202\251
	 * \220\346\202\311\203\140\203\106\203\142\203\116\202\276\202\257\202\265\202\304\202\250\202\255
	 */
	if (getword(tosjis(av[i])) == NULL)
	    return ERR;
    }
    ret = OK;
    ifp = lfp = bfp = NULL;
    if ((ifp = dic_open(dicdir, idxfile)) == NULL ||
	(lfp = dic_open(dicdir, lstfile)) == NULL ||
	(bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
	goto err;
    if (setuptmp() == ERR) {
	outstr("ERR: \203\201\203\202\203\212\201\133\202\252\221\253\202\350\202\334\202\271\202\361\n");
	goto err;
    }
    /*
     * \215\305\217\211\202\314\222\120\214\352\202\360\214\237\215\365\202\267\202\351
     * \214\213\211\312\202\315\215\354\213\306\203\132\203\142\203\1470\202\311\217\221\202\253\217\157\202\267
     * \202\275\202\276\202\265\215\305\214\343\202\314\222\120\214\352\202\310\202\347\202\273\202\314\202\334\202\334\225\134\216\246\202\265\202\304\217\111\202\355\202\350
     */
    word = getword(tosjis(*av));
    if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
	goto err;
#ifdef DEBUG
    if (debug2)
	printf("search1 start: word=%s\n", word);
#endif
    nomore = (ac == 1);
    num0 = 0L;
    odpos = 0L;
    last = (int)val(inode.last);
    for (;;) {
	if (n > last) {
	    if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
		val(inode.pageno) == 0L)
		break;
	    last = (int)val(inode.last);
	    n = 0;
	}
#ifdef DEBUG
	if (debug && n == 0) {
	    printf("<next node>\n");
	    printf("pageno=%08lx, last=%08lx\n",
		val(inode.pageno), val(inode.last));
	    for (i = 0; i <= (int)val(inode.last); i++) {
		lpos = val(inode.ent[i].lstpos);
		bseek(lfp, lpos);
		getlistkey(lfp, key, X_MATCH);
		printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
	    }
	}
#endif
	lpos = val(inode.ent[n].lstpos);
	if (bseek(lfp, lpos) == ERR) {
	    outstr("PANIC: \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
	    goto err;
	}
	getilistent(lfp, &ilent);
	cmp = strcmp(word, ilent.idkey);
	if (cmp < 0)
	    break;
	if (cmp > 0) {
	    n++;
	    continue;
	}
#ifdef DEBUG
	if (debug)
	    printf("idkey=[%s], entid=%04x, nent=%04x, dpos=%08lx\n",
		ilent.idkey, ilent.entid, ilent.nent, ilent.dpos);
#endif
	if (ilent.dpos == odpos) {
	    n++;
	    continue;
	}
	odpos = ilent.dpos;
	if (nomore) {
	    /*
	     * \214\237\215\365\216\167\222\350\225\266\216\232\227\361\202\2521\202\302\202\276\202\257\202\276\202\301\202\275
	     */
#ifdef DEBUG
	    if (debug2) {
		printf("dpos=%08lx\n", ilent.dpos);
		num0++;
		n++;
		continue;
	    }
#endif
	    if (show_idiom(bfp, ilent.dpos) == ERR)
		goto err;
	    num0++;
	    n++;
	    continue;
	}
	/*
	 * \214\237\215\365\214\213\211\312\202\360\213\114\230\136\202\267\202\351
	 */
	if (!tisopen(0) && twopen(0) == ERR ||
	    twrite(0, ilent.dpos) == ERR)
	    goto err;
	num0++;
	n++;
    }
#ifdef DEBUG
    if (debug2)
	printf("num0=%ld\n", num0);
#endif
    if (tisopen(0) && twclose(0) == ERR)
	goto err;
    if (nomore || num0 == 0L) {
	/*
	 * \215\305\214\343\202\314\222\120\214\352\202\251\201\101\214\213\211\312\202\2520\214\217\202\276\202\301\202\275
	 */
	goto done;
    }
    ac--, av++;
    /*
     * \216\143\202\350\202\314\222\120\214\352\202\360\214\237\215\365\202\267\202\351
     * \214\213\211\312\202\315\215\354\213\306\203\132\203\142\203\1471\202\311\217\221\202\253\217\157\202\267
     * \215\354\213\306\203\132\203\142\203\1470\202\306\215\354\213\306\203\132\203\142\203\1471\202\360\223\313\202\253\215\207\202\355\202\271\202\304
     * \227\274\225\373\202\311\221\266\215\335\202\267\202\351\202\340\202\314\202\276\202\257\202\360\215\354\213\306\203\132\203\142\203\1472\202\311\217\221\202\253\217\157\202\267
     * \202\240\202\351\202\242\202\315\215\305\214\343\202\314\222\120\214\352\202\310\202\347\214\213\211\312\202\360\225\134\216\246\202\267\202\351
     */
    while (ac > 0) {
	word = getword(tosjis(*av));
	if (getfirstent(ifp, lfp, word, &inode, &n) == ERR)
	    goto err;
#ifdef DEBUG
	if (debug2)
	    printf("search2 start: word=%s\n", word);
#endif
	nomore = (ac == 1);
	num1 = 0L;
	odpos = 0L;
	last = (int)val(inode.last);
	for (;;) {
	    if (n > last) {
		if (bread(ifp, (uchr *)&inode, sizeof(INODE)) < sizeof(INODE) ||
		    val(inode.pageno) == 0L)
		    break;
		last = (int)val(inode.last);
		n = 0;
	    }
#ifdef DEBUG
	    if (debug && n == 0) {
		printf("<next node>\n");
		printf("pageno=%08lx, last=%08lx\n",
		    val(inode.pageno), val(inode.last));
		for (i = 0; i <= (int)val(inode.last); i++) {
		    lpos = val(inode.ent[i].lstpos);
		    bseek(lfp, lpos);
		    getlistkey(lfp, key, X_MATCH);
		    printf("e[%d].lstpos=%08lx, key=[%s]\n", i, lpos, key);
		}
	    }
#endif
	    lpos = val(inode.ent[n].lstpos);
	    if (bseek(lfp, lpos) == ERR) {
		outstr("PANIC: \203\212\203\130\203\147\203\164\203\100\203\103\203\213\202\311\225\163\220\256\215\207\202\252\202\240\202\350\202\334\202\267\n");
		goto err;
	    }
	    getilistent(lfp, &ilent);
	    cmp = strcmp(word, ilent.idkey);
	    if (cmp < 0)
		break;
	    if (cmp > 0) {
		n++;
		continue;
	    }
#ifdef DEBUG
	    if (debug)
		printf("idkey=[%s], entid=%04x, nent=%04x, dpos=%08lx\n",
		    ilent.idkey, ilent.entid, ilent.nent, ilent.dpos);
#endif
	    if (ilent.dpos == odpos) {
		n++;
		continue;
	    }
	    odpos = ilent.dpos;
	    /*
	     * \214\237\215\365\214\213\211\312\202\360\213\114\230\136\202\267\202\351
	     */
	    if (!tisopen(1) && twopen(1) == ERR ||
	        twrite(1, ilent.dpos) == ERR)
		goto err;
	    num1++;
	    n++;
	}
#ifdef DEBUG
	if (debug2)
	    printf("num1=%ld\n", num1);
#endif
	if (tisopen(1) && twclose(1) == ERR)
	    goto err;
	if (num1 == 0L) {
	    /*
	     * \214\213\211\312\202\2520\214\217\202\276\202\301\202\275
	     */
	    goto done;
	}
	/*
	 * \215\354\213\306\203\132\203\142\203\1470\202\306\215\354\213\306\203\132\203\142\203\1471\202\360\223\313\202\253\215\207\202\355\202\271\202\304
	 * \227\274\225\373\202\311\221\266\215\335\202\267\202\351\202\340\202\314\202\276\202\257\202\360\215\354\213\306\203\132\203\142\203\1472\202\311\217\221\202\253\217\157\202\267
	 * \202\273\202\314\214\213\211\312\202\360\220\126\202\275\202\310\215\354\213\306\203\132\203\142\203\1470\202\306\202\267\202\351
	 */
	if (tropen(0) == ERR || tropen(1) == ERR)
	    goto err;
#ifdef DEBUG
	if (debug2) {
	    printf("compare start\n");
	}
#endif
	n0 = n1 = num2 = 0L;
	l0 = tread(0);
	l1 = tread(1);
	for (;;) {
	    if (l0 < l1) {
		if (++n0 >= num0)
		    break;
		l0 = tread(0);
		continue;
	    }
	    if (l1 < l0) {
		if (++n1 >= num1)
		    break;
		l1 = tread(1);
		continue;
	    }
	    if (nomore) {
#ifdef DEBUG
		if (debug2) {
		    printf("dpos=%08lx\n", l0);
		    goto next;
		}
#endif
		if (show_idiom(bfp, l0) == ERR)
		    goto err;
		goto next;
	    }
	    /*
	     * \214\237\215\365\214\213\211\312\202\360\213\114\230\136\202\267\202\351
	     */
	    if (!tisopen(2) && twopen(2) == ERR ||
		twrite(2, l0) == ERR)
		goto err;
	next:
	    if (++n0 >= num0)
		break;
	    l0 = tread(0);
	    if (++n1 >= num1)
		break;
	    l1 = tread(1);
	    num2++;
	}
#ifdef DEBUG
	if (debug2)
	    printf("num2=%ld\n", num2);
#endif
	trclose(0);
	trclose(1);
	if (tisopen(2) && twclose(2) == ERR)
	    goto err;
	if (num2 == 0L) {
	    /*
	     * \214\213\211\312\202\2520\214\217\202\276\202\301\202\275
	     */
	    goto done;
	}
	tswap(0, 2);
	ac--, av++;
    }
    goto done;
err:
    ret = ERR;
done:
    tfree(0);
    tfree(1);
    tfree(2);
    if (ifp != NULL)
	bclose(ifp);
    if (lfp != NULL)
	bclose(lfp);
    if (bfp != NULL)
	bclose(bfp);
#ifdef DEBUG
    if (debug2)
	return ret;
#endif
    cleantmp();
    return ret;
}

/*
 * mgetent - \222\206\212\324\210\352\222\166\203\212\203\130\203\147\202\3601\215\163\223\307\202\335\215\236\202\336
 */
uchr *
mgetent(bfp, buf)
BFILE	*bfp;
uchr	*buf;
{
    int		c;
    uchr	*p;

    if ((c = bgetc(bfp)) == EOF)
	return NULL;
    p = buf;
    do {
	*p++ = c;
    } while ((c = bgetc(bfp)) != '\r');
    *p = '\0';
    return buf;
}

/*
 * match - \225\266\216\232\227\361\202\306\203\160\203\136\201\133\203\223\202\314\217\306\215\207\202\360\215\163\202\244
 */
int
match(str, pat)
uchr	*str, *pat;
{
    unt		c;
    uchr	*s, *p;

#ifdef DEBUG
    if (debug2) {
	printf("str=\"%s\", pat=\"%s\"\n", str, pat);
    }
#endif
    if (stype == EN_SRCH) {
	/*
	 * 1\203\157\203\103\203\147\225\266\216\232\202\314\217\306\215\207
	 */
	while (*str) {
	    switch (*pat) {
	    case '?':
		/* 
		 * \224\103\210\323\202\314\225\266\216\232\202\306\203\175\203\142\203\140\202\267\202\351
		 */
		break;
	    case '*':
		/*
		 * str\202\314\216\143\202\350\202\3601\225\266\216\232\202\270\202\302\214\270\202\347\202\265\202\310\202\252\202\347
		 * pat\202\314\216\143\202\350\202\306\203\175\203\142\203\140\202\267\202\351\202\251\202\307\202\244\202\251\222\262\202\327\202\351
		 */
		s = str;
		do {
		    if (match(s, pat + 1))
			return TRUE;
		} while (*s++);
		break;
	    case '[':
		for (p = pat + 1; *p != ']'; p++) {
		    if (*str == *p)
			break;
		    if (p[1] == '-') {
			if (*str >= *p && *str <= p[2])
			    break;
			p += 2;
		    }
		}
		if (*p == ']') {
		    /*
		     * \203\175\203\142\203\140\202\265\202\310\202\251\202\301\202\275
		     */
		    return FALSE;
		}
		while (*p != ']')
		    p++;
		pat = p;
		break;
	    case '\0':
		/* 
		 * \203\160\203\136\201\133\203\223\202\252\220\346\202\311\220\163\202\253\202\275
		 */
		return FALSE;
	    default:
		if (*str != *pat) {
		    /*
		     * \203\175\203\142\203\140\202\265\202\310\202\251\202\301\202\275
		     */
		    return FALSE;
		}
		break;
	    }
	    str++;
	    pat++;
	}
	if (*pat == '\0') {
	    /*
	     * \203\164\203\100\203\103\203\213\226\274\202\306\203\160\203\136\201\133\203\223\202\252\223\257\216\236\202\311\220\163\202\253\202\275
	     */
	    return TRUE;
	}
	return FALSE;
    }
    /*
     * 2\203\157\203\103\203\147\225\266\216\232\202\314\217\306\215\207
     */
    while (*str) {
	switch (*pat) {
	case '?':
	    /* 
	     * \224\103\210\323\202\314\225\266\216\232\202\306\203\175\203\142\203\140\202\267\202\351
	     */
	    str += 2;
	    pat++;
	    break;
	case '*':
	    /*
	     * str\202\314\216\143\202\350\202\3601\225\266\216\232\202\270\202\302\214\270\202\347\202\265\202\310\202\252\202\347
	     * pat\202\314\216\143\202\350\202\306\203\175\203\142\203\140\202\267\202\351\202\251\202\307\202\244\202\251\222\262\202\327\202\351
	     */
	    s = str;
	    for (;;) {
		if (match(s, pat + 1))
		    return TRUE;
		if (*s == '\0')
		    break;
		s += 2;
	    }
	    str += 2;
	    pat++;
	    break;
	case '[':
	    p = pat + 1;
	    while (*p != ']') {
		if (*str == *p && str[1] == p[1])
		    break;
		if (p[2] == '-') {
		    c = (*str << 8 | str[1]);
		    if (c >= (p[0] << 8 | p[1]) &&
			c <= (p[3] << 8 | p[4]))
			break;
		    p += 3;
		}
		p += 2;
	    }
	    if (*p == ']') {
		/*
		 * \203\175\203\142\203\140\202\265\202\310\202\251\202\301\202\275
		 */
		return FALSE;
	    }
	    while (*p != ']') {
		if (*p == '-') {
		    p++;
		    continue;
		}
		p += 2;
	    }
	    str += 2;
	    pat = p + 1;
	    break;
	case '\0':
	    /* 
	     * \203\160\203\136\201\133\203\223\202\252\220\346\202\311\220\163\202\253\202\275
	     */
	    return FALSE;
	default:
	    if (*str != *pat || str[1] != pat[1]) {
		/*
		 * \203\175\203\142\203\140\202\265\202\310\202\251\202\301\202\275
		 */
		return FALSE;
	    }
	    str += 2;
	    pat += 2;
	    break;
	}
    }
    if (*pat == '\0') {
	/*
	 * \203\164\203\100\203\103\203\213\226\274\202\306\203\160\203\136\201\133\203\223\202\252\223\257\216\236\202\311\220\163\202\253\202\275
	 */
	return TRUE;
    }
    return FALSE;
}

/*
 * wild_search - \203\217\203\103\203\213\203\150\203\112\201\133\203\150\214\237\215\365\202\360\215\163\202\244
 */
int
wild_search(dicdir, showall, pat)
uchr	*dicdir, *pat;
int	showall;
{
    int		ret, first;
    long	dpos, dlen, odpos;
    uchr	*p;
    BFILE	*lfp, *bfp;
    uchr	buf[256];

    ret = OK;
    lfp = bfp = NULL;
    if ((lfp = dic_open(dicdir, lstfile)) == NULL ||
	(bfp = dic_open(dicdir, MAIN_DAT)) == NULL)
	goto err;
    bseek(lfp, 0L);
    first = TRUE;
    odpos = 0L;
    while (mgetent(lfp, buf) != NULL) {
	p = buf;
	while (*p && *p != '0')
	    p++;
	*p = '\0';
	if (!match(buf, pat))
	    continue;
	sscanf(p+1, "%7lx%lx", &dpos, &dlen);
#ifdef DEBUG
	if (debug)
	    printf("show_dat: key=[%s], dpos=%lx, dlen=%lx\n",
		buf, dpos, dlen);
#endif
	if (showall) {
	    if (first)
		first = FALSE;
	    else
		outstr("\n");
	    if (dpos == odpos)
		continue;
	    if (show_data(bfp, dpos, dlen) == ERR)
		goto err;
	} else {
	    if (show_header(bfp, dpos) == ERR)
		goto err;
	    if (stype == EN_SRCH)
		lower(buf);
	    outstr(" [");
	    outstr(buf);
	    outstr("]\n");
	}
    }
    goto done;
err:
    ret = ERR;
done:
    if (lfp != NULL)
	bclose(lfp);
    if (bfp != NULL)
	bclose(bfp);
    return ret;
}

/*
 * getword - \214\237\215\365\216\167\222\350\225\266\216\232\227\361\202\360\216\363\202\257\216\346\202\301\202\304\214\237\215\365\222\120\214\352\202\273\202\314\221\274\202\314\220\335\222\350\202\360\215\163\202\244
 */
uchr *
getword(str)
uchr	*str;
{
    int		n, len, err;
    int		eng, knj, any, meta, bra;
    uchr	*s, *p, *q, ch;
    static uchr	buf[128];

    len = strlen(str);
    eng = knj = any = meta = 0;
    err = 0;
    bra = FALSE;
    s = str;
    while (*s) {
	if (strchr("*?[]-", *s)) {
	    if (*s == '*' && (bra || s[1] == '*') ||
		*s == '?' && bra ||
		*s == '[' && (bra || s[1] == '-' || s[1] == ']') ||
		*s == '-' && (!bra || s[1] == ']') ||
		*s == ']' && !bra) {
		err++;
		break;
	    }
	    if (*s == '[')
		bra = TRUE;
	    else if (*s == ']')
		bra = FALSE;
	    else if (*s == '?')
		any++;
	    s++;
	    meta++;
	    continue;
	}
	if (SJIS1(*s)) {
	    s += 2;
	    knj++;
	    continue;
	}
	s++;
	eng++;
    }
    if (err || bra) {
	outstr("ERR: \203\217\203\103\203\213\203\150\203\112\201\133\203\150\214\237\215\365\216\167\222\350\202\311\214\353\202\350\202\252\202\240\202\350\202\334\202\267\n");
	return NULL;
    }
    if (knj > 0 && eng > 0) {
	outstr("ERR: \214\237\215\365\222\120\214\352\202\311\211\160\214\352\202\306\223\372\226\173\214\352\202\252\215\254\215\335\202\265\202\304\202\242\202\334\202\267\n");
	return NULL;
    }
    if (knj == 0 && eng == 0 && any == 0) {
	outstr("ERR: \214\237\215\365\222\120\214\352\202\252\213\363\202\305\202\267\n");
	return NULL;
    }
    if (stype == ID_SRCH) {
	if (knj) {
	    outstr("ERR: \220\254\213\345\201\105\227\160\227\341\214\237\215\365\202\305\223\372\226\173\214\352\202\252\216\167\222\350\202\263\202\352\202\304\202\242\202\334\202\267\n");
	    return NULL;
	}
	if (mtype != X_MATCH) {
	    outstr("ERR: \220\254\213\345\201\105\227\160\227\341\214\237\215\365\202\305\225\163\212\256\221\123\210\352\222\166\214\237\215\365\202\252\216\167\222\350\202\263\202\352\202\304\202\242\202\334\202\267\n");
	    return NULL;
	}
    }
    if (meta == 0)
	mtype = X_MATCH;
    else if (meta == 1 && str[len-1] == '*')
	mtype = F_MATCH;
    else if (meta == 1 && *str == '*')
	mtype = R_MATCH;
    else
	mtype = W_MATCH;
    if (knj)
	stype = TR_SRCH;
    s = str;
    p = buf;
    if (mtype == F_MATCH || mtype == R_MATCH) {
	len--;
	if (mtype == R_MATCH)
	    s++;
    }
    n = len;
    while (n > 0) {
	if (SJIS1(*s)) {
	    *p++ = *s++;
	    *p++ = *s++;
	    n -= 2;
	    continue;
	}
	if (islower(*s)) {
	    *p++ = toupper(*s);
	    s++;
	} else {
	    *p++ = *s++;
	}
	n--;
    }
    *p = '\0';
    if (mtype == R_MATCH) {
	/*
	 * \214\237\215\365\225\266\216\232\227\361\202\360\213\164\217\207\202\311\202\267\202\351
	 */
	p = buf;
	while (*p) {
	    if (SJIS1(*p)) {
		ch = *p;
		*p = p[1];
		p[1] = ch;
		p++;
	    }
	    p++;
	}
	p = buf;
	q = buf + len - 1;
	while (p < q) {
	    ch = *p;
	    *p++ = *q;
	    *q-- = ch;
	}
    }
    return buf;
}

/*
 * search - \214\237\215\365\202\314\203\201\203\103\203\223\203\213\201\133\203\140\203\223
 */
int
search(dicdir, showall, ac, av)
uchr	*dicdir;
int	showall;
int	ac;
uchr	**av;
{
    int		ret;
    uchr	*word;

    if ((word = getword(tosjis(*av))) == NULL)
	return ERR;
    switch (stype) {
    case EN_SRCH:
	switch (mtype) {
	case X_MATCH:
	case F_MATCH:
	    idxfile = EN_F_IDX;
	    lstfile = EN_LIST;
	    ret = index_search(dicdir, showall, word);
	    break;
	case R_MATCH:
	    idxfile = EN_R_IDX;
	    lstfile = EN_LIST;
	    ret = index_search(dicdir, showall, word);
	    break;
	case W_MATCH:
	    lstfile = EN_M_LIST;
	    ret = wild_search(dicdir, showall, word);
	    break;
	}
	break;
    case TR_SRCH:
	switch (mtype) {
	case X_MATCH:
	case F_MATCH:
	    idxfile = TR_F_IDX;
	    lstfile = TR_LIST;
	    ret = index_search(dicdir, showall, word);
	    break;
	case R_MATCH:
	    idxfile = TR_R_IDX;
	    lstfile = TR_LIST;
	    ret = index_search(dicdir, showall, word);
	    break;
	case W_MATCH:
	    lstfile = TR_M_LIST;
	    ret = wild_search(dicdir, showall, word);
	    break;
	}
	break;
    case ID_SRCH:
	idxfile = ID_IDX;
	lstfile = ID_LIST;
	ret = idiom_search(dicdir, ac, av);
	break;
    }
    return ret;
}

/* -------------------- \203\201\203\103\203\223 -------------------- */

#define	CSRD_ENV	"CSRD"		/* \203\111\203\166\203\126\203\207\203\223\202\360\220\335\222\350\202\267\202\351\212\302\213\253\225\317\220\224	*/

#define	MINWIDTH	20		/* \217\157\227\315\215\163\225\235\202\314\211\272\214\300		*/

int	showall = FALSE;		/* \225\163\212\256\221\123\210\352\222\166\202\305\202\340\215\200\226\332\225\134\216\246\202\267\202\351\202\251?*/
int	showver = FALSE;		/* \216\253\217\221\203\146\201\133\203\136\203\157\201\133\203\127\203\207\203\223\225\134\216\246	*/
uchr	*dicdir = NULL;			/* \216\253\217\221\203\146\203\102\203\214\203\116\203\147\203\212\201\133		*/
uchr	*fmtfile = NULL;		/* \225\134\216\246\217\221\216\256\203\164\203\100\203\103\203\213		*/
uchr	*gaifile = NULL;		/* \212\117\216\232\221\343\221\326\225\134\213\114\203\164\203\100\203\103\203\213		*/
uchr	*supstr = NULL;			/* \225\134\216\246\227\175\220\247\215\200\226\332			*/

void	usage();
int	parse_opt();
#if defined(UNIX) && defined(RC_PATH)
int	init_rcfile();
#endif
int	init_env();
void	onintr();
int	main();

/*
 * usage - \216\147\227\160\226\100\202\360\225\134\216\246\202\265\202\304\217\111\227\271\202\267\202\351
 */
void
usage()
{
    outstr("\217\254\212\167\212\331\203\211\203\223\203\137\203\200\203\156\203\105\203\130\211\160\214\352\216\253\223\124\214\237\215\365\203\206\201\133\203\145\203\102\203\212\203\145\203\102\201\133 Ver.");
    outstr(version);
    outstr(" (");
    outstr(date);
    outstr(")\n    Written by Junn Ohta (ohta@src.ricoh.co.jp),");
    outstr(" Public Domain.\n\n\216\147\227\160\226\100: ");
    outstr(progname);
    outstr(" [-d\\<\216\253\217\221DIR\\>] [-f\\<\217\221\216\256\203\164\203\100\203\103\203\213\\>] [-g\\<\212\117\216\232\203\164\203\100\203\103\203\213\\>]\n");
    outstr("             [-w\\<\214\205\220\224\\>] [-s\\<\224\361\225\134\216\246\215\200\226\332\\>] [-b[x]] [-a] [-v]");
#ifdef UNIX
    outstr("\n             [-c\\<\203\122\201\133\203\150\\>]");
#endif
    outstr(" \\<\214\237\215\365\216\167\222\350\\>\n\n");
    outstr("\203\111\203\166\203\126\203\207\203\223: (\212\302\213\253\225\317\220\224");
    outstr(CSRD_ENV);
    outstr("\202\305\216\167\222\350\202\267\202\351\202\261\202\306\202\340\202\305\202\253\202\334\202\267)\n");
    outstr("    -d: \216\253\217\221\203\146\203\102\203\214\203\116\203\147\203\212\201\133\n");
    outstr("    -f: \225\134\216\246\217\221\216\256\203\164\203\100\203\103\203\213\n");
    outstr("    -g: \212\117\216\232\221\343\221\326\225\134\213\114\203\164\203\100\203\103\203\213\n");
    outstr("    -w: \217\157\227\315\215\163\225\235 (-w0:\211\374\215\163\202\310\202\265(\225\127\217\200))\n");
    outstr("    -s: \223\301\222\350\215\200\226\332\202\360\224\361\225\134\216\246\202\311\202\267\202\351\n");
    outstr("        (-st:\210\323\226\241, -se:\227\160\227\341, -si:\220\254\213\345, -so:\227\336\214\352, -sa:\202\267\202\327\202\304)\n");
    outstr("    -b: \224\255\211\271\213\114\215\206\227\361\202\360\223\137\226\363\202\267\202\351 (-bx:[]\202\360\216\346\202\350\217\234\202\255)\n");
    outstr("    -a: \225\163\212\256\221\123\210\352\222\166\202\305\202\340\215\200\226\332\223\340\227\145\202\360\225\134\216\246\202\267\202\351\n");
    outstr("    -v: \216\253\217\221\203\146\201\133\203\136\202\314\203\157\201\133\203\127\203\207\203\223\202\360\225\134\216\246\202\267\202\351\n");
#ifdef UNIX
    outstr("    -c: \223\372\226\173\214\352\203\122\201\133\203\150 (-cj:JIS, -ce:EUC(\225\127\217\200), -cs:\203\126\203\164\203\147JIS)\n");
#endif
    outstr("\n\214\237\215\365\216\167\222\350: (\220\254\213\345\201\105\227\160\227\341\214\237\215\365\210\310\212\117\202\305\202\315\226\363\214\352\202\251\202\347\202\340\214\237\215\365\202\305\202\253\202\334\202\267)\n");
    outstr("    \212\256\221\123\210\352\222\166\214\237\215\365: \\<\225\266\216\232\227\361\\>     \203\217\203\103\203\213\203\150\203\112\201\133\203\150\214\237\215\365: *?[]-\202\360\212\334\202\336\225\266\216\232\227\361\n");
    outstr("    \221\117\225\373\210\352\222\166\214\237\215\365: \\<\225\266\216\232\227\361\\>*    \220\254\213\345\201\105\227\160\227\341\214\237\215\365: -i \\<\225\266\216\232\227\361\\> ...\n");
    outstr("    \214\343\225\373\210\352\222\166\214\237\215\365: *\\<\225\266\216\232\227\361\\>\n");
    exit(1);
}

/*
 * parse_opt - \212\302\213\253\225\317\220\224\202\334\202\275\202\315\203\122\203\175\203\223\203\150\215\163\210\370\220\224\202\305\216\167\222\350\202\263\202\352\202\275\203\111\203\166\203\126\203\207\203\223\202\360
 *	       \211\360\220\315\202\265\202\304\225\113\227\166\202\310\220\335\222\350\202\360\215\163\202\244
 */
int
parse_opt(acp, avp)
int	*acp;
uchr	***avp;
{
    int		n, ac;
    uchr	*s, **av;

    ac = *acp;
    av = *avp;
    while (ac > 0 && **av == '-') {
	s = *av + 1;
	switch (*s++) {
	case 'D':
	case 'd':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    dicdir = strpool(s);
	    bsltosl(dicdir);
	    n = strlen(dicdir);
	    if (dicdir[n-1] == '/')
		dicdir[n-1] = '\0';
	    break;
	case 'F':
	case 'f':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    fmtfile = strpool(s);
	    bsltosl(fmtfile);
	    break;
	case 'G':
	case 'g':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    gaifile = strpool(s);
	    bsltosl(gaifile);
	    break;
	case 'A':
	case 'a':
	    showall = TRUE;
	    break;
	case 'W':
	case 'w':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    if (!isdigit(*s))
		return ERR;
	    width = atoi(s);
	    if (width != 0 && width < MINWIDTH)
		width = MINWIDTH;
	    break;
	case 'B':
	case 'b':
	    do_braille = TRUE;
	    if (*s == 'x' || *s == 'X')
		nobracket = TRUE;
	    else
		nobracket = FALSE;
	    break;
#ifdef UNIX
	case 'C':
	case 'c':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    switch (*s) {
	    case 'J':
	    case 'j':
		kcode = KC_JIS;
		if (s[1] && s[2]) {
		    jiskanji = s[1];
		    jisalpha = s[2];
		}
		break;
	    case 'E':
	    case 'e':
		kcode = KC_EUC;
		break;
	    case 'S':
	    case 's':
		kcode = KC_SJIS;
		break;
	    default:
		return ERR;
	    }
	    break;
#endif
	case 'S':
	case 's':
	    if (!*s) {
		ac--, av++;
		if (ac == 0 || **av == '-')
		    return ERR;
		s = *av;
	    }
	    supstr = strpool(s);
	    break;
	case 'I':
	case 'i':
	    stype = ID_SRCH;
	    break;
	case 'V':
	case 'v':
	    showver = TRUE;
	    break;
	case 'R':
	case 'r':
	    rawmode = TRUE;
	    break;
#ifdef DEBUG
	case 'Z':
	case 'z':
	    if (*s == 'z' || *s == 'Z')
		debug2 = TRUE;
	    else
		debug = TRUE;
	    break;
#endif
	default:
	    return ERR;
	}
	ac--, av++;
    }
    *acp = ac;
    *avp = av;
    return OK;
}

#if defined(UNIX) && defined(RC_PATH)
/*
 * init_rcfile - \203\164\203\100\203\103\203\213\202\305\216\167\222\350\202\263\202\352\202\275\203\111\203\166\203\126\203\207\203\223\202\360\211\360\216\337\202\267\202\351
 *		 Contributed by \226\354\216\361 \213\115\216\153 (Nokubi Takatsugu)
 */
int
init_rcfile()
{
    int		ec;
    uchr	*env, *s, **ev, **pp;
    FILE	*fp;
    struct stat	st;

    if (stat(RC_PATH, &st) < 0)
	return OK;
    if ((fp = fopen(RC_PATH, "r")) == NULL)
	return ERR;
    env = (uchr *)malloc(st.st_size + 1);
    if (env == NULL)
	return ERR;
    if (fread(env, st.st_size, 1, fp) < 1)
	return ERR;
    *(env + st.st_size - 1) = '\0';
    fclose(fp);
    ec = 1;
    for (s = env; *s; s++) {
	if (*s == ' ' || *s == '\t')
	    ec++;
    }
    ev = (uchr **)malloc(sizeof(uchr *) * (ec + 1));
    pp = ev;
    while ((s = strtok(env, " \t")) != NULL) {
	*pp++ = s;
	env = NULL;
    }
    *pp = NULL;
    pp = ev;
    if (parse_opt(&ec, &ev) != OK) {
	outstr("ERR: \203\164\203\100\203\103\203\213 ");
	outstr(RC_PATH);
	outstr(" \202\311\214\353\202\350\202\252\202\240\202\350\202\334\202\267\n");
	return ERR;
    }
    free((char *)pp);
    free((char *)env);
    return OK;
}
#endif

/*
 * init_env - \212\302\213\253\225\317\220\224CSRD\202\305\216\167\222\350\202\263\202\352\202\275\203\111\203\166\203\126\203\207\203\223\202\360\211\360\216\337\202\267\202\351
 */
int
init_env()
{
    int		ec;
    uchr	*env, *s, **ev, **pp;

    if ((env = getenv(CSRD_ENV)) == NULL)
	return OK;
    ec = 1;
    for (s = env; *s; s++) {
	if (*s == ' ' || *s == '\t')
	    ec++;
    }
    ev = (uchr **)malloc(sizeof(uchr *) * (ec + 1));
    pp = ev;
    while ((s = strtok(env, " \t")) != NULL) {
	*pp++ = s;
	env = NULL;
    }
    *pp = NULL;
    pp = ev;
    if (parse_opt(&ec, &ev) != OK) {
	outstr("ERR: \212\302\213\253\225\317\220\224 ");
	outstr(CSRD_ENV);
	outstr(" \202\311\214\353\202\350\202\252\202\240\202\350\202\334\202\267\n");
	return ERR;
    }
    free((char *)pp);
    return OK;
}

/*
 * onintr - \212\204\202\350\215\236\202\335\216\236\202\314\217\111\227\271\217\210\227\235
 */
void
onintr()
{
    outchar(0);	/* JIS\212\277\216\232\203\202\201\133\203\150\202\310\202\347ASCII\202\311\226\337\202\267(UNIX) */
    cleantmp();	/* \220\254\213\345\201\105\227\160\227\341\214\237\215\365\227\160\202\314\210\352\216\236\203\164\203\100\203\103\203\213\202\360\215\355\217\234\202\267\202\351 */
    exit(0);
}

/*
 * main - \203\201\203\103\203\223\203\213\201\133\203\140\203\223
 */
int
main(ac, av)
int	ac;
uchr	**av;
{
    signal(SIGINT, onintr);
#if defined(UNIX) && defined(RC_PATH)
    if (init_rcfile() != OK)
	exit(1);
#endif
    if (init_env() != OK)
	exit(1);
    ac--, av++;
    if (parse_opt(&ac, &av) != OK)
	usage();
    if (!showver && (ac == 0 || stype != ID_SRCH && ac > 1))
	usage();
    if (dicdir == NULL) {
	outstr("ERR: \216\253\217\221\203\146\203\102\203\214\203\116\203\147\203\212\201\133\202\252\220\335\222\350\202\263\202\352\202\304\202\242\202\334\202\271\202\361\n");
	exit(1);
    }
    if (showver) {
	show_version(dicdir);
	exit(0);
    }
    if (init_alt() == ERR)
	exit(1);
    if (gaifile != NULL) {
	if (load_gaiji(gaifile) == ERR)
	    exit(1);
    }
    if (fmtfile != NULL) {
	if (load_format(fmtfile) == ERR)
	    exit(1);
    }
    if (suppress(supstr) == ERR) {
	outstr("ERR: \225\134\216\246\227\175\220\247\216\167\222\350(-s\203\111\203\166\203\126\203\207\203\223)\202\311\214\353\202\350\202\252\202\240\202\350\202\334\202\267\n");
	exit(1);
    }
    if (search(dicdir, showall, ac, av) == ERR)
	exit(1);
    exit(0);
}


syntax highlighted by Code2HTML, v. 0.9.1