/*
    KEYWORD.C

    1991.01.11  make by Ken
*/
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <ctype.h>
#include    <mos.h>
#include    <dos.h>
#include    "scrn.h"
#include    "keyword.h"
#include    "graphic.h"
#include    "dir.h"
#include    "file.h"
#include    "event.h"
#include    "coldef.h"

#define	TRUE	1
#define	FALSE	0
#define	ERR	(-1)

#define KEY_HAS     8

	int	prg_max=0;
	int	key_max=0;
	int	prg_cnt=0;
	int	prg_hit=0;
	int	prg_ofs=0;
	char	*crent_drive=NULL;

	PRGPTR	*prg_top=NULL;
	PRGPTR	*prg_tbl[PRG_MAX];

	KEYPTR	*key_tbl[KEY_MAX];
static	KEYPTR	*key_now=NULL;
static	KEYPTR	*key_hash[KEY_HAS]={
			NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL };

	int	act_fwc_cdrom = 0;
static	int	fwc_cdrom = 0;

char	*strdup(char *str)
{
    char    *p;

    if ( (p = (char *)malloc(strlen(str)+1)) != NULL )
	strcpy(p,str);
    return p;
}
static int  hash_calc(char *str)
{
    int     hs=0;

    while ( *str != '\0' )
        hs = hs * 31 + *(str++);
    return (hs & (KEY_HAS-1));
}
static KEYPTR *key_srch(char *key)
{
    int     hs;
    KEYPTR  *kp;

    hs = hash_calc(key);
    kp = key_hash[hs];
    while ( kp != NULL ) {
        if ( strcmp(key,kp->key) == 0 )
	    return kp;
        kp = kp->next;
    }

    if ( (kp = (KEYPTR *)malloc(sizeof(KEYPTR))) == NULL )
        return NULL;
 
    kp->next = key_hash[hs];
    key_hash[hs] = kp;

    kp->key = strdup(key);
    kp->flg = kp->cnt = 0;
    kp->over = kp->grop = NULL;

    return kp;
}
static void key_set(char *key,PRGPTR *pp)
{
    int     i,hs;
    KEYPTR  *kp;

    hs = hash_calc(key);
    kp = key_hash[hs];
    while ( kp != NULL ) {
        if ( strcmp(key,kp->key) == 0 ) {
	    key_now = kp;
	    if ( kp->grop != NULL )
		kp = kp->grop;
            while ( kp->cnt >= PRG_QUE ) {
		for ( i = 0 ; i < kp->cnt ; i++ ) {
		    if ( kp->prog[i] == pp )
			return;
		}
                if ( kp->over == NULL ) {
                    if ( (kp->over =
                         (KEYPTR *)malloc(sizeof(KEYPTR))) == NULL )
                        return;
                    kp = kp->over;
		    kp->cnt = 0;
		    kp->over = NULL;
                } else
                    kp = kp->over;
            }
            goto ENDOF;
        }
        kp = kp->next;
    }

    if ( (kp = (KEYPTR *)malloc(sizeof(KEYPTR))) == NULL )
        return;
 
    kp->next = key_hash[hs];
    key_hash[hs] = kp;

    kp->key = strdup(key);
    kp->flg = kp->cnt = 0;
    kp->over = kp->grop = NULL;
    key_now = kp;

    if ( key_max < KEY_MAX ) {
	KEY_putstr(key_max,kp->key);
	key_tbl[key_max++] = kp;
    }

ENDOF:
    if ( pp != NULL ) {
	for ( i = 0 ; i < kp->cnt ; i++ ) {
	    if ( kp->prog[i] == pp )
		return;
	}
	kp->prog[kp->cnt++] = pp;
    }
}
static	int	prog_cmp(PRGPTR *sp, PRGPTR *dp)
{
    int c;

    if ( sp->make != NULL && dp->make != NULL &&
	 (c = strcmp(sp->make, dp->make)) != 0 )
	return c;

    if ( sp->name == NULL )
	return (-1);
    else if ( dp->name == NULL )
	return 1;

    return strcmp(sp->name, dp->name);
}
static	PRGPTR	*prog_sort(PRGPTR *np)
{
    PRGPTR *tp, *bp;
    PRGPTR *lp, *rp;

    if ( np == NULL || np->next == NULL )
        return np;

    lp = rp = NULL;
    tp = np->next;

    while ( tp != NULL ) {
        bp = tp->next;
        if ( prog_cmp(np, tp) > 0 ) {
            tp->next = lp;
            lp = tp;
        } else {
            tp->next = rp;
            rp = tp;
        }
        tp = bp;
    }

    lp = prog_sort(lp);
    rp = prog_sort(rp);

    if ( lp != NULL ) {
        tp = lp;
        while ( lp->next != NULL )
            lp = lp->next;
        lp->next = np;
    } else
        tp = np;

    np->next = rp;

    return tp;
}
static	char	*chk_drv(char *dir)
{
    static char tmp[BUFSIZ];

    if ( dir[0] != '\0' && dir[1] == ':' )
	return dir;

    if ( crent_drive == NULL )
	return dir;

    strcpy(tmp,crent_drive);
    strcat(tmp,dir);
    return tmp;
}
int	DB_init(char *file)
{
    int     i,n;
    FILE    *fp;
    PRGPTR  *pp=NULL;
    KEYPTR  *kp,*tp;
    char    *p,*s;
    char    tmp[BUFSIZ];
    char    key[BUFSIZ];

    int     pos, len;
    char    buf[4096];

    prg_max = key_max = 0;

    if ( (fp = fopen(file,"rb")) == NULL )
	return ERR;

    pos = len = 0;
    for ( ; ; ) {
	for ( i = 0 ; ; ) {
	    if ( pos >= len ) {
	        if ( (len = fread(buf, 1, 4096, fp)) <= 0 )
	            goto ENDOF;
	        pos = 0;
	    }
	    n = buf[pos++];
	    if ( n == '\n' ) {
		tmp[i] = '\0';
		break;
	    } else if ( n != '\r' && n != '\x1A' )
	        tmp[i++] = n;
	}

/**************************
    while ( fgets(tmp,BUFSIZ,fp) != NULL ) {

	if ( (p = strchr(tmp,'\n')) != NULL )
	    *p = '\0';
****************************/

	if ( tmp[0] == '\0' || tmp[0] == '#' )
	    continue;

	if ( strncmp(tmp,"FEP:",4) == 0 ) {
	    p = &(tmp[4]);
	    while ( isspace(*p) )
		p++;
	    for ( s = p ; *s != '\0' && !isspace(*s) ; s++ )
		;
	    if ( *s != '\0' )
		*(s++) = '\0';
	    while ( isspace(*s) )
		s++;
	    if ( *p != '\0' && *s != '\0' )
		PROG_append(p, s);
	    continue;

	} else if ( strncmp(tmp,"FWC:",4) == 0 ) {
	    p = &(tmp[4]); while ( isspace(*p) ) p++;
	    fwc_cdrom = atoi(p);
	    if ( act_fwc_cdrom == 0 )
		act_fwc_cdrom = fwc_cdrom;
	    continue;

	} else if ( strncmp(tmp,"DRIVE:",6) == 0 ) {
	    p = &(tmp[6]); while ( isspace(*p) ) p++;
	    if ( *p == '\0' )
		continue;
	    crent_drive = strdup(p);
	    continue;

	} else if ( strncmp(tmp,"PROGRAM:",8) == 0 ) {
	    p = &(tmp[8]); while ( isspace(*p) ) p++;
	    if ( *p == '\0' )
		continue;

	    if ( (pp = (PRGPTR *)malloc(sizeof(PRGPTR))) == NULL )
		break;

	    pp->next = prg_top;
	    prg_top = pp;

	    prg_max++;
	    pp->flg = pp->copycnt = pp->bits = 0;
	    pp->name = strdup(p);
	    pp->make = pp->readme = pp->manual = pp->dir = pp->exec = NULL;
	    pp->cdrom = fwc_cdrom;
	    continue;

	} else if ( strncmp(tmp,"KEYWORD:",8) == 0 ) {
	    p = &(tmp[8]);
	    while ( *p != '\0' ) {
		while ( isspace(*p) ) p++;
		for ( s = key ; !isspace(*p) && *p != '\0' ; )
		     *(s++) = *(p++);
		*s = '\0';
		if ( key[0] != '\0' )
		    key_set(key,pp);
	    }	    
	    continue;

	} else	if ( strncmp(tmp,"GROUP:",6) == 0 ) {
	    tp = NULL;
	    p = &(tmp[6]);
	    while ( *p != '\0' ) {
		while ( isspace(*p) ) p++;
		for ( s = key ; !isspace(*p) && *p != '\0' ; )
		     *(s++) = *(p++);
		*s = '\0';
		if ( key[0] != '\0' ) {
		    if ( tp == NULL ) {
		        key_set(key,NULL);
			tp = key_now;
		    } else {
			if ( (kp = key_srch(key)) != NULL )
			    kp->grop = tp;
		    }
		}
	    }	    
	    continue;
	}

	if ( pp == NULL )
	    continue;

	if ( strncmp(tmp,"MAKE:",5) == 0 ) {
	    p = &(tmp[6]); while ( isspace(*p) ) p++;
	    if ( *p != '\0' )
		pp->make = strdup(p);

	} else if ( strncmp(tmp,"README:",7) == 0 ) {
	    p = &(tmp[7]); while ( isspace(*p) ) p++;
	    if ( *p != '\0' )
		pp->readme = strdup(chk_drv(p));

	} else if ( strncmp(tmp,"MANUAL:",7) == 0 ) {
	    p = &(tmp[7]); while ( isspace(*p) ) p++;
	    if ( *p != '\0' )
		pp->manual = strdup(chk_drv(p));

	} else if ( strncmp(tmp,"COPY:",5) == 0 ) {
	    p = &(tmp[5]);
	    while ( *p != '\0' ) {
		while ( isspace(*p) ) p++;
		for ( s = key ; !isspace(*p) && *p != '\0' ; )
		     *(s++) = *(p++);
		*s = '\0';
		if ( key[0] != '\0' && pp->copycnt < 16 )
		    pp->copy[pp->copycnt++] = strdup(chk_drv(key));
	    }	    

	} else if ( strncmp(tmp,"DIR:",4) == 0 ) {
	    p = &(tmp[4]); while ( isspace(*p) ) p++;
	    if ( *p != '\0' )
		pp->dir = strdup(chk_drv(p));

	} else if ( strncmp(tmp,"EXEC:",5) == 0 ) {
	    p = &(tmp[5]); while ( isspace(*p) ) p++;
	    if ( *p != '\0' )
		pp->exec = strdup(chk_drv(p));
	}

    }

ENDOF:
    fclose(fp);

/***************************************************
    for ( i = 0 ; i < 4 && i < key_max ; i++ ) {
	kp = key_tbl[i];
	do {
	    for ( n = 0 ; n < kp->cnt ; n++ )
	        kp->prog[n]->bits |= (1 << i);
	    kp = kp->over;
        } while ( kp != NULL );
    }
*****************************************************/

    prg_top = prog_sort(prg_top);

    return FALSE;
}
void	PRG_alldisp(int ofs)
{
    int     i;
    PRGPTR  *pp;

    prg_ofs = ofs;
    i = 0;
    for ( pp = prg_top ; prg_cnt > 0 && pp != NULL ; pp = pp->next ) {
	if ( pp->flg >= prg_cnt ) {
	    if ( i >= ofs && (i - ofs) < PRG_MAX ) {
		PRG_disp((i-ofs),pp->name,pp->make,pp->bits);
		prg_tbl[i-ofs] = pp;
	    }
	    i++;
	}
    }
    prg_hit = i;

    MENU_mask(BACK_NO,ofs > 0 ? ON:OFF);
    MENU_mask(NEXT_NO,(i-ofs) > PRG_MAX ? ON:OFF);
    MENU_mask(CLER_NO,prg_cnt > 0 ? ON:OFF);

    while ( i >= ofs && (i - ofs) < PRG_MAX ) {
	PRG_disp((i - ofs),"","",0);
	prg_tbl[i-ofs] = NULL;
	i++;
    }

    if ( prg_cnt == 0 )
	MSG2_disp("%24s","");
    else if ( prg_hit == 0 )
	MSG2_disp("Not Found               ");
    else
	MSG2_disp("%3d Found               ",prg_hit);
}
void	KEY_clic(int no)
{
    int     i,cd;
    KEYPTR  *kp;

    if ( no >= key_max )
	return;

    kp = key_tbl[no];
    if ( (kp->flg = (kp->flg == 0 ? 1:0)) == 0 ) {
	cd = (-1);
    } else {
	cd = 1;
    }

    KEY_disp(no);
    prg_cnt += cd;
    do {
	for ( i = 0 ; i < kp->cnt ; i++ )
	    kp->prog[i]->flg += cd;
	kp = kp->over;
    } while ( kp != NULL );

    PRG_alldisp(0);
}

static	int	titl_flg = 0;
static	int	writ_flg = 0;

void	TITL_clic()
{
    int fg = 0;
    PRGPTR  *pp;
    static char tmp[130];

    if ( titl_flg ) {
	prg_cnt -= 1;
	for ( pp = prg_top ; pp != NULL ; pp = pp->next ) {
	    if ( wild_mach(tmp, pp->name) )
	        pp->flg -= 1;
	}
	titl_flg = 0;
	fg = 1;
    }

    if ( EXEC_input(tmp) || tmp[0] == '\0' )
	goto ENDOF;

    prg_cnt += 1;
    for ( pp = prg_top ; pp != NULL ; pp = pp->next ) {
	if ( wild_mach(tmp, pp->name) )
	    pp->flg += 1;
    }
    titl_flg = 1;
    fg = 1;

ENDOF:
    if ( fg )
	PRG_alldisp(0);
}
void	WRIT_clic()
{
    int fg = 0;
    PRGPTR  *pp;
    static char tmp[130];

    if ( writ_flg ) {
	prg_cnt -= 1;
	for ( pp = prg_top ; pp != NULL ; pp = pp->next ) {
	    if ( wild_mach(tmp, pp->make) )
	        pp->flg -= 1;
	}
	writ_flg = 0;
	fg = 1;
    }

    if ( EXEC_input(tmp) || tmp[0] == '\0' )
	goto ENDOF;

    prg_cnt += 1;
    for ( pp = prg_top ; pp != NULL ; pp = pp->next ) {
	if ( wild_mach(tmp, pp->make) )
	    pp->flg += 1;
    }
    writ_flg = 1;
    fg = 1;

ENDOF:
    if ( fg )
	PRG_alldisp(0);
}
void	KEY_cler(void)
{
    int     i;
    PRGPTR  *pp;

    for ( i = 0 ; i < key_max ; i++ ) {
	if ( key_tbl[i]->flg != 0 ) {
	    key_tbl[i]->flg = 0;
	    KEY_disp(i);
	}
    }

    for ( pp = prg_top ; pp != NULL ; pp = pp->next )
	pp->flg = 0;

    for ( i = 0 ; i < PRG_MAX ; i++ ) {
	PRG_disp(i,"","",0);
	prg_tbl[i] = NULL;
    }

    titl_flg = writ_flg = 0;
    prg_cnt = prg_hit = 0;

    MENU_mask(BACK_NO,OFF);
    MENU_mask(NEXT_NO,OFF);
    MENU_mask(CLER_NO,OFF);

    MSG2_disp("%24s","");
}
void	PRG_status(void)
{
    MENU_mask(BACK_NO,prg_ofs > 0 ? ON:OFF);
    MENU_mask(NEXT_NO,(prg_hit - prg_ofs) > PRG_MAX ? ON:OFF);
    MENU_mask(CLER_NO,prg_cnt > 0 ? ON:OFF);
}
void	PRG_back(void)
{
    if ( prg_ofs <= 0 )
	return;
    PRG_alldisp(prg_ofs - PRG_MAX);
}
void	PRG_next(void)
{
    if ( (prg_ofs + PRG_MAX) >= prg_hit )
	return;
    PRG_alldisp(prg_ofs + PRG_MAX);
}

	int	CTRL_work(int mode, char *buf);

void	KEYWORD_load()
{
    int i, n;
    int bit, mark;
    char tmp[128];
    KEYPTR  *kp;

    if ( CTRL_work(0, tmp) || *((int *)(tmp + 0)) != 0x04030201 )
	return;

    mark = *((int *)(tmp + 4));
    prg_ofs = *((int *)(tmp + 8));

    bit = 1;
    for ( i = 0 ; i < key_max ; i++ ) {
	if ( (mark & bit) != 0 ) {
	    kp = key_tbl[i];
	    kp->flg = 1;
	    KEY_disp(i);
	    prg_cnt++;
	    do {
		for ( n = 0 ; n < kp->cnt ; n++ )
		    kp->prog[n]->flg += 1;
		kp = kp->over;
	    } while ( kp != NULL );
	}
	bit <<= 1;
    }

    PRG_alldisp(prg_ofs);

    *((int *)(tmp + 4)) = 0;
    *((int *)(tmp + 8)) = 0;

    CTRL_work(1, tmp);
}
void	KEYWORD_save()
{
    int i;
    int bit, mark;
    char tmp[128];

    if ( CTRL_work(0, tmp) || *((int *)(tmp + 0)) != 0x04030201 )
	return;

    bit = 1;
    mark = 0;
    for ( i = 0 ; i < key_max ; i++ ) {
	if ( key_tbl[i]->flg != 0 )
	    mark |= bit;
	bit <<= 1;
    }

    *((int *)(tmp + 4)) = mark;
    *((int *)(tmp + 8)) = prg_ofs;

    CTRL_work(1, tmp);
}


/*************************************************************
01234567890123456789012345678901234567890123456789012345678901234567890123456789                01234567890123456789012345678901234567890123456
                +---------------------------------------------+
                | PROGRAM  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | 
                | MAKE     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                | KEYWORD  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                | COPY     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                | README   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                | MANUAL   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
		| EXEC     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                | DIR      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
                |             | 設  定 | | 終  了 |           |
                +---------------------------------------------+
**************************************************************/

extern char	index_file[];
extern int	cut_mode;
extern char	cut_buf[2][128];

#define	FILE_IDX	index_file

#define	FSEL_X1		(5*8)
#define	FSEL_Y1		(235)
#define	FSEL_X2		(75*8)
#define	FSEL_Y2		(FSEL_Y1+220)

#define	FSEL_PROG_X	(FSEL_X1+11*8)
#define	FSEL_PROG_Y	(FSEL_Y1+4)

#define	FSEL_MAKE_X	(FSEL_X1+11*8)
#define	FSEL_MAKE_Y	(FSEL_Y1+1*24+4)

#define	FSEL_KEYW_X	(FSEL_X1+11*8)
#define	FSEL_KEYW_Y	(FSEL_Y1+2*24+4)

#define	FSEL_COPY_X	(FSEL_X1+11*8)
#define	FSEL_COPY_Y	(FSEL_Y1+3*24+4)

#define	FSEL_READ_X	(FSEL_X1+11*8)
#define	FSEL_READ_Y	(FSEL_Y1+4*24+4)

#define	FSEL_MANU_X	(FSEL_X1+11*8)
#define	FSEL_MANU_Y	(FSEL_Y1+5*24+4)

#define	FSEL_EXEC_X	(FSEL_X1+11*8)
#define	FSEL_EXEC_Y	(FSEL_Y1+6*24+4)

#define	FSEL_DIR_X	(FSEL_X1+11*8)
#define	FSEL_DIR_Y	(FSEL_Y1+7*24+4)

#define	FSEL_EXT_X	((FSEL_X1+FSEL_X2)/2-22*8)
#define	FSEL_EXT_Y	(FSEL_Y1+8*24+4)

#define	FSEL_YES_X	((FSEL_X1+FSEL_X2)/2-10*8)
#define	FSEL_YES_Y	(FSEL_Y1+8*24+4)

#define	FSEL_RET_X	((FSEL_X1+FSEL_X2)/2+2*8)
#define	FSEL_RET_Y	(FSEL_Y1+8*24+4)

#define LIST_X      	(17*8)
#define LIST_Y      	26
#define LIST_M      	"マニュアル表示"
#define	LIST_X1	    	(LIST_X-4)
#define	LIST_Y1	    	(LIST_Y-2)
#define	LIST_X2	    	(LIST_X+14*8+3)
#define	LIST_Y2	    	(LIST_Y+17)

#define KEY_X       	8
#define KEY_Y       	69
#define KEY_L	    	(18*8)
#define KEY_S	    	(20*8)
#define KEY_POS_X(n)	((n%4)*KEY_S+KEY_X)
#define KEY_POS_Y(n)	((n/4)*23+KEY_Y)

void	eof_chk(FILE *fp)
{
    int     ch;

    _setmode(fp,_BINARY);
    fseek(fp,(-50L),SEEK_END);
    while ( (ch = getc(fp)) != EOF && ch != 0x1A );
    if ( ch == 0x1A ) {
	fseek(fp,(-1L),SEEK_CUR);
    } else {
	clearerr(fp);
	fseek(fp,0L,SEEK_END);
    }
    _setmode(fp,_TEXT);
}
void	fileclose(char *file, FILE *fp)
{
    char *p;
    char tmp[256];

    fclose(fp);
    strcpy(tmp, file);
    if ( (p = strrchr(tmp, '\\')) == NULL )
	p = tmp;
    if ( (p = strrchr(p, '.')) == NULL )
	p = tmp + strlen(tmp);
    strcpy(p, ".L$K");
    remove(tmp);    
}
FILE	*fileopen(char *file)
{
    FILE *fp;
    int n, i;
    char *p;
    char tmp[256];

    strcpy(tmp, file);
    if ( (p = strrchr(tmp, '\\')) == NULL )
	p = tmp;
    if ( (p = strrchr(p, '.')) == NULL )
	p = tmp + strlen(tmp);
    strcpy(p, ".L$K");

    for ( n = 0 ; n < 10000 ; n++ ) {
	if ( !_dos_creatnew(tmp, _A_NORMAL, &i) )
	    break;
    }

    if ( n >= 10000 )
	return NULL;

    _dos_close(i);

    if ( (fp = fopen(FILE_IDX,"r+")) == NULL ) {
	if ( (fp = fopen(FILE_IDX,"w")) == NULL )
	    return NULL;
    } else
	eof_chk(fp);

    return fp;
}
void	KEY_input(void)
{
    int     i,n,j;
    int     sw,bx,by;
    FILE    *fp;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    PRGPTR  *pp;
    char    *p,*s;
    char    tmp[256];
    char    key[BUFSIZ];
    static char dmy[8][BUFSIZ];

/*******************
    if ( (fp = fopen(FILE_IDX,"r+")) == NULL ) {
	if ( (fp = fopen(FILE_IDX,"w")) == NULL )
	    return;
    } else
	eof_chk(fp);
********************/

    dmy[0][0] = dmy[1][0] = dmy[2][0] = dmy[3][0] = 
    dmy[4][0] = dmy[5][0] = dmy[6][0] = dmy[7][0] = '\0';

    MOS_disp(OFF);
    sp = DSP_push_vram(FSEL_X1,FSEL_Y1,FSEL_X2,FSEL_Y2);
    DSP_opbox(FSEL_X1,FSEL_Y1,FSEL_X2,FSEL_Y2);
    DSP_wbox(FSEL_X1,FSEL_Y1,FSEL_X2,FSEL_Y2,LINE_COL,MENU_COL,M_PSET);

    ep = EVT_set(ep,500,LIST_X1,LIST_Y1,LIST_X2,LIST_Y2,EVT_proc);

    for ( i = 0 ; i < 28 ; i++ ) {
	ep = EVT_set(ep,i+300,
		 KEY_POS_X(i)-4,KEY_POS_Y(i)-2,
		 KEY_POS_X(i)+KEY_L+3,KEY_POS_Y(i)+17,EVT_proc);
    }

    sprintf(tmp,"%-44.44s",dmy[0]);
    ep = EVT_sw(ep,0,FSEL_PROG_X,FSEL_PROG_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-31.31s",dmy[1]);
    ep = EVT_sw(ep,1,FSEL_MAKE_X,FSEL_MAKE_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[2]);
    ep = EVT_sw(ep,2,FSEL_KEYW_X,FSEL_KEYW_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[3]);
    ep = EVT_sw(ep,3,FSEL_COPY_X,FSEL_COPY_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[4]);
    ep = EVT_sw(ep,4,FSEL_READ_X,FSEL_READ_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[5]);
    ep = EVT_sw(ep,5,FSEL_MANU_X,FSEL_MANU_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[6]);
    ep = EVT_sw(ep,6,FSEL_EXEC_X,FSEL_EXEC_Y,CHR_COL,WIND_COL,tmp);
    sprintf(tmp,"%-58.58s",dmy[7]);
    ep = EVT_sw(ep,7,FSEL_DIR_X,FSEL_DIR_Y,CHR_COL,WIND_COL,tmp);

    ep = EVT_sw(ep,10,FSEL_X1+8,FSEL_PROG_Y,CHR_COL,KEY_COL,"PROGRAM");
    ep = EVT_sw(ep,11,FSEL_X1+8,FSEL_MAKE_Y,CHR_COL,KEY_COL,"MAKE   ");
    ep = EVT_sw(ep,12,FSEL_X1+8,FSEL_KEYW_Y,CHR_COL,KEY_COL,"KEYWORD");
    ep = EVT_sw(ep,13,FSEL_X1+8,FSEL_COPY_Y,CHR_COL,KEY_COL,"COPY   ");
    ep = EVT_sw(ep,14,FSEL_X1+8,FSEL_READ_Y,CHR_COL,KEY_COL,"README ");
    ep = EVT_sw(ep,15,FSEL_X1+8,FSEL_MANU_Y,CHR_COL,KEY_COL,"MANUAL ");
    ep = EVT_sw(ep,16,FSEL_X1+8,FSEL_EXEC_Y,CHR_COL,KEY_COL,"EXEC   ");
    ep = EVT_sw(ep,17,FSEL_X1+8,FSEL_DIR_Y, CHR_COL,KEY_COL,"DIR    ");

    ep = EVT_sw(ep,110,FSEL_EXT_X,FSEL_EXT_Y,CHR_COL,WIND_COL," 簡  単 ");
    ep = EVT_sw(ep,100,FSEL_YES_X,FSEL_YES_Y,CHR_COL,WIND_COL," 設  定 ");
    ep = EVT_sw(ep,200,FSEL_RET_X,FSEL_RET_Y,CHR_COL,WIND_COL," もどる ");

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((FSEL_X1+FSEL_X2)/2,(FSEL_Y1+FSEL_Y2)/2);
    MOS_disp(ON);

    for ( ; ; ) {
	i = EVT_wait(ep);

	if ( i == 0 ) {
	    MOS_disp(OFF);
	    input(FSEL_PROG_X,FSEL_PROG_Y+i*24,44,dmy[i]);
	    MOS_disp(ON);

	} else if ( i == 1 ) {
	    MOS_disp(OFF);
	    input(FSEL_PROG_X,FSEL_PROG_Y+i*24,31,dmy[i]);
	    MOS_disp(ON);

	} else if ( i >= 2 && i <= 7 ) {
	    MOS_disp(OFF);
	    input(FSEL_PROG_X,FSEL_PROG_Y+i*24,58,dmy[i]);
	    MOS_disp(ON);

	} else if ( i >= 10 && i <= 11 ) {
	    if ( (p = FILE_select()) != NULL ) {
		MENU_mask(RETN_NO,ON);
		cut_mode = TRUE;
		cut_buf[0][0] = '\0';
		FILE_open(p);
		while ( MENU_no(0) != CLER_NO )
		    FILE_irq();
		MENU_mask(CLER_NO,OFF);
		FILE_close();
		cut_mode = FALSE;

		n = i - 10;
		strcat(dmy[n],cut_buf[0]);

		MOS_disp(OFF);
		gprintf(FSEL_PROG_X,FSEL_PROG_Y+n*24,
			CHR_COL,WIND_COL,
			 n == 0 ? "%-44.44s":
			(n == 1 ? "%-31.31s":
				  "%-58.58s"),dmy[n]);
		MOS_disp(ON);
	    }

	} else if ( i == 110 ) {
	    if ( (p = FILE_select()) != NULL ) {
		MENU_mask(RETN_NO,ON);
		cut_mode = TRUE;
		cut_buf[0][0] = cut_buf[1][0] = '\0';
		FILE_open(p);
		while ( (n = MENU_no(0)) != CLER_NO ) {
		    FILE_irq();
		    if ( n == BACK_NO )
			FILE_back();
		    else if ( n == NEXT_NO )
			FILE_next();
		}
		MENU_mask(CLER_NO,OFF);
		FILE_close();
		cut_mode = FALSE;

		strcpy(dmy[0],cut_buf[0]);
		strcpy(dmy[1],cut_buf[1]);
		getdir(dmy[7]);

		MOS_disp(OFF);
		gprintf(FSEL_PROG_X,FSEL_PROG_Y,
			CHR_COL,WIND_COL,"%-44.44s",dmy[0]);
		gprintf(FSEL_MAKE_X,FSEL_MAKE_Y,
			CHR_COL,WIND_COL,"%-31.31s",dmy[1]);
			CHR_COL,WIND_COL,
		gprintf(FSEL_DIR_X,FSEL_DIR_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[7]);
	        MOS_disp(ON);
	    }

	} else if ( i >= 13 && i <= 16 ) {
	    if ( (p = FILE_select()) != NULL ) {
/********************************************
		tmp[0] = 'A' + getdrv();
		tmp[1] = ':';
		getdir(tmp+2);
		if ( tmp[3] != '\0' )
		    strcat(tmp,"\\");
		strcat(tmp,p);
*********************************************/
		getdir(tmp);
		if ( tmp[1] != '\0' )
		    strcat(tmp,"\\");
		strcat(tmp,p);

		n = i - 10;
		if ( dmy[n][0] != '\0' )
		    strcat(dmy[n]," ");
		strcat(dmy[n],tmp);

		MOS_disp(OFF);
		gprintf(FSEL_PROG_X,FSEL_PROG_Y+n*24,
				CHR_COL,WIND_COL,"%-58.58s",dmy[n]);
	        MOS_disp(ON);
	    }

	} else if ( i == 16 ) {
	    FILE_select();
/*******************************************
	    tmp[0] = 'A' + getdrv();
	    tmp[1] = ':';
	    getdir(tmp+2);
*******************************************/
	    getdir(tmp);

	    n = i - 10;
	    strcpy(dmy[n],tmp);

	    MOS_disp(OFF);
	    gprintf(FSEL_PROG_X,FSEL_PROG_Y+n*24,
			CHR_COL,WIND_COL,"%-58.58s",dmy[n]);
	    MOS_disp(ON);

	} else if ( i == 500 ) {
	    LIST_proc();

	} else if ( i >= 300 ) {
	    if ( key_tbl[i-300] != NULL ) {
		if ( dmy[2][0] != '\0' )
		    strcat(dmy[2]," ");
		strcat(dmy[2],key_tbl[i-300]->key);
		MOS_disp(OFF);
		gprintf(FSEL_KEYW_X,FSEL_KEYW_Y,
				CHR_COL,WIND_COL,"%-58.58s",dmy[2]);
		MOS_disp(ON);
	    }

	} else if ( i == 100 ) {
	    if ( dmy[0][0] == '\0' &&
		yesno("作品名が無いけど？") == ERR )
		continue;
	    if ( dmy[1][0] == '\0' &&
		yesno("作者名が無いけど？") == ERR )
		continue;
	    if ( dmy[2][0] == '\0' &&
		yesno("キ−ワ−ドが無いけど？") == ERR )
		continue;
	    if ( dmy[3][0] == '\0' && dmy[4][0] == '\0' &&
	         dmy[5][0] == '\0' && dmy[7][0] == '\0' &&
		yesno("ファイル指定が無いけど？") == ERR )
		continue;

	    if ( (fp = fileopen(FILE_IDX)) == NULL ) {
		yesno("書き込み中です。しばらくお待ちください");
		continue;
	    }

	    fprintf(fp,"\n");
	    fprintf(fp,"PROGRAM: %s\n",dmy[0]);
	    fprintf(fp,"MAKE:    %s\n",dmy[1]);
	    fprintf(fp,"KEYWORD: %s\n",dmy[2]);
	    if ( dmy[3][0] != '\0' )
		fprintf(fp,"COPY:    %s\n",dmy[3]);
	    if ( dmy[4][0] != '\0' )
		fprintf(fp,"README:  %s\n",dmy[4]);
	    if ( dmy[5][0] != '\0' )
		fprintf(fp,"MANUAL:  %s\n",dmy[5]);
	    if ( dmy[6][0] != '\0' )
		fprintf(fp,"EXEC:    %s\n",dmy[6]);
	    if ( dmy[7][0] != '\0' )
		fprintf(fp,"DIR:     %s\n",dmy[7]);

	    fileclose(FILE_IDX, fp);

	    if ( (pp = (PRGPTR *)malloc(sizeof(PRGPTR))) == NULL )
		break;
	    pp->next = prg_top;
	    prg_top = pp;
	    prg_max++;
	    pp->flg = pp->copycnt = pp->bits = 0;
	    pp->name = strdup(dmy[0]);
	    pp->make = pp->readme = pp->manual = pp->exec = pp->dir = NULL;

	    if ( dmy[1][0] != '\0' )
		pp->make = strdup(dmy[1]);
	    if ( dmy[4][0] != '\0' )
		pp->readme = strdup(chk_drv(dmy[4]));
	    if ( dmy[5][0] != '\0' )
		pp->manual = strdup(chk_drv(dmy[5]));
	    if ( dmy[6][0] != '\0' )
		pp->exec   = strdup(chk_drv(dmy[6]));
	    if ( dmy[7][0] != '\0' )
		pp->dir    = strdup(chk_drv(dmy[7]));

	    p = dmy[3];
	    while ( *p != '\0' ) {
		while ( isspace(*p) ) p++;
		for ( s = key ; !isspace(*p) && *p != '\0' ; )
		     *(s++) = *(p++);
		*s = '\0';
		if ( key[0] != '\0' && pp->copycnt < 16 )
		    pp->copy[pp->copycnt++] = strdup(chk_drv(key));
	    }	    

	    p = dmy[2];
	    while ( *p != '\0' ) {
		while ( isspace(*p) ) p++;
		for ( s = key ; !isspace(*p) && *p != '\0' ; )
		     *(s++) = *(p++);
		*s = '\0';
		if ( key[0] != '\0' )
		    key_set(key,pp);
	    }	    

	    dmy[0][0] = dmy[1][0] = dmy[2][0] = dmy[3][0] = 
	    dmy[4][0] = dmy[5][0] = dmy[6][0] = dmy[7][0] = '\0';

	    MOS_disp(OFF);
	    gprintf(FSEL_PROG_X,FSEL_PROG_Y,
			CHR_COL,WIND_COL,"%-44.44s",dmy[0]);
	    gprintf(FSEL_MAKE_X,FSEL_MAKE_Y,
			CHR_COL,WIND_COL,"%-31.31s",dmy[1]);
	    gprintf(FSEL_KEYW_X,FSEL_KEYW_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[2]);
	    gprintf(FSEL_COPY_X,FSEL_COPY_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[3]);
	    gprintf(FSEL_READ_X,FSEL_READ_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[4]);
	    gprintf(FSEL_MANU_X,FSEL_MANU_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[5]);
	    gprintf(FSEL_EXEC_X,FSEL_EXEC_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[6]);
	    gprintf(FSEL_DIR_X,FSEL_DIR_Y,
			CHR_COL,WIND_COL,"%-58.58s",dmy[7]);
	    MOS_disp(ON);

	} else if ( i == 200 ) {
	    break;
	}
    }

    EVT_free(ep);
    MOS_disp(OFF);
    DSP_pop_vram(sp);
    MOS_setpos(bx,by);
    MOS_disp(ON);
/***************
    fclose(fp);
****************/
}
