#include    <stdio.h>
#include    <stdlib.h>
#include    <stdarg.h>
#include    <string.h>
#include    <ctype.h>
#include    <mos.h>
#include    "dir.h"
#include    "scrn.h"
#include    "keyword.h"
#include    "graphic.h"
#include    "event.h"
#include    "coldef.h"
#include    "bitmap.h"

#define	TRUE	1
#define	FALSE	0
#define	ERR	(-1)

#define	LIN_MAX		80
#define	TAB		8
#define	LPT_LINE	59

#define	PAGE		0x40000

#define	MAX_Y		25
#define	SCRN_Y		52

#define	SCRN_X1		0
#define	SCRN_Y1		47
#define	SCRN_X2		639
#define	SCRN_Y2		454

#define	BAR_SIZ		(25*8)
#define	BAR_X1		8
#define	BAR_Y1		458
#define	BAR_X2		(BAR_X1+BAR_SIZ+4)
#define	BAR_Y2		(BAR_Y1+19)

#define	KAKU_X1		(15*8-4)
#define	KAKU_Y1		180
#define	KAKU_X2		(64*8+3)
#define	KAKU_Y2		280

#define	KAKU_MSG_X	((KAKU_X1+KAKU_X2)/2)
#define	KAKU_MSG_Y	(KAKU_Y1+30)

#define	KAKU_CHK_X	((KAKU_X1+KAKU_X2)/2-(4*8))
#define	KAKU_CHK_Y	(KAKU_Y1+70)

#define	KAKU_YES_X	((KAKU_X1+KAKU_X2)/2-(10*8))
#define	KAKU_YES_Y	(KAKU_Y1+70)

#define	KAKU_NO_X	((KAKU_X1+KAKU_X2)/2+(2*8))
#define	KAKU_NO_Y	(KAKU_Y1+70)

#define	BREAK_X1	(13*8-4)
#define	BREAK_Y1	160
#define	BREAK_X2	(66*8+3)
#define	BREAK_Y2	300

#define	BREAK_MSG_X	((BREAK_X1+BREAK_X2)/2)
#define	BREAK_MSG_Y	(BREAK_Y1+30)

#define	BREAK_BAR_SIZ	(41*8)
#define	BREAK_BAR_X	(BREAK_X1+6*8)
#define	BREAK_BAR_Y	(BREAK_Y1+70)

#define	BREAK_BAR_X1	(BREAK_BAR_X-2)
#define	BREAK_BAR_Y1	(BREAK_BAR_Y-2)
#define	BREAK_BAR_X2	(BREAK_BAR_X+BREAK_BAR_SIZ+2)
#define	BREAK_BAR_Y2	(BREAK_BAR_Y+17)

#define	BREAK_CHK_X	((BREAK_X1+BREAK_X2)/2-(4*8))
#define	BREAK_CHK_Y	(BREAK_Y1+110)

typedef struct _LP {
    struct _LP	*next;
    struct _LP	*back;
    char        buf[1];
} LINPTR;

void	INKEY_start(void);
void	INKEY_end(void);
int	getch(void);
int	CLIP_box();

extern int	cur_x;
extern int	cur_y;

       int      cut_mode = FALSE;
       char     cut_buf[2][128];

static int	old_line=(-1);
static int	now_line=0;
static int	max_line=0;
static int	now_ofs=0;
static int	slow_ofs=0;
static int	scr_speed = 0;
static int      old_timer = 0;
static int	bar_old_x1=(-1);
static int	bar_old_x2=(-1);
static LINPTR	*top_ptr=NULL;
static LINPTR	*now_ptr=NULL;
static BLOCK	*save=NULL;
static char	file_name[128];
static int	break_bar=0;
static BLOCK	*break_save=NULL;
static EVENT	*break_ep=NULL;

int	pause(char *form,...)
{
    va_list arg;
    int     n,x1,x2;
    int     cd,sw,bx,by;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    char    tmp[256];

    va_start(arg,form);
    vsprintf((char *)tmp,form,arg);
    va_end(arg);

    if ( (n = (strlen(tmp) * 8 + 32) / 2) < 160 )
	n = 160;
    x1 = (KAKU_X1 + KAKU_X2) / 2 - n;
    x2 = (KAKU_X1 + KAKU_X2) / 2 + n;

    MOS_disp(OFF);
    sp = DSP_push_vram(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_opbox(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_wbox(x1,KAKU_Y1,x2,KAKU_Y2,LINE_COL,FILD_COL,M_PSET);
    gputs(KAKU_MSG_X-strlen(tmp)*4,KAKU_MSG_Y,CHR_COL,FILD_COL,tmp);

/*******
    ep = EVT_sw(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,"CONTINUE");
    ep = EVT_sw(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  " CANCEL ");
********/
    ep = EVT_img(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,64,continue_img);
    ep = EVT_img(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  64,cancel_img);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((KAKU_X1+KAKU_X2)/2,KAKU_YES_Y+8);
    MOS_disp(ON);

    cd = (EVT_wait(ep) == 0 ? TRUE:ERR);
    EVT_free(ep);

    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(x1,KAKU_Y1,x2,KAKU_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return cd;
}
int	yesno2(char *form,...)
{
    va_list arg;
    int     n,x1,x2;
    int     cd,sw,bx,by;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    char    tmp[256];

    va_start(arg,form);
    vsprintf((char *)tmp,form,arg);
    va_end(arg);

    if ( (n = (strlen(tmp) * 8 + 32) / 2) < 160 )
	n = 160;
    x1 = (KAKU_X1 + KAKU_X2) / 2 - n;
    x2 = (KAKU_X1 + KAKU_X2) / 2 + n;

    MOS_disp(OFF);
    sp = DSP_push_vram(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_opbox(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_wbox(x1,KAKU_Y1,x2,KAKU_Y2,LINE_COL,FILD_COL,M_PSET);
    gputs(KAKU_MSG_X-strlen(tmp)*4,KAKU_MSG_Y,CHR_COL,FILD_COL,tmp);

/*********
    ep = EVT_sw(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,"  YES  ");
    ep = EVT_sw(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  "   NO  ");
**********/
    ep = EVT_img(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,64,yes_img);
    ep = EVT_img(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  64,no_img);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((KAKU_X1+KAKU_X2)/2,KAKU_YES_Y+8);
    MOS_disp(ON);

    cd = (EVT_wait(ep) == 0 ? TRUE:ERR);
    EVT_free(ep);

    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(x1,KAKU_Y1,x2,KAKU_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return cd;
}
int	yesno(char *form,...)
{
    va_list arg;
    int     n,x1,x2;
    int     cd,sw,bx,by;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    char    tmp[256];

    va_start(arg,form);
    vsprintf((char *)tmp,form,arg);
    va_end(arg);

    if ( (n = (strlen(tmp) * 8 + 32) / 2) < 160 )
	n = 160;
    x1 = (KAKU_X1 + KAKU_X2) / 2 - n;
    x2 = (KAKU_X1 + KAKU_X2) / 2 + n;

    MOS_disp(OFF);
    sp = DSP_push_vram(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_opbox(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_wbox(x1,KAKU_Y1,x2,KAKU_Y2,LINE_COL,FILD_COL,M_PSET);
    gputs(KAKU_MSG_X-strlen(tmp)*4,KAKU_MSG_Y,CHR_COL,FILD_COL,tmp);

/***********
    ep = EVT_sw(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,"  YES  ");
    ep = EVT_sw(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  "   NO  ");
***********/
    ep = EVT_img(ep,0,KAKU_YES_X,KAKU_YES_Y,CHR_COL,WIND_COL,64,yes_img);
    ep = EVT_img(ep,1,KAKU_NO_X,KAKU_NO_Y,CHR_COL,WIND_COL,  64,no_img);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((KAKU_X1+KAKU_X2)/2,KAKU_YES_Y+8);
    MOS_disp(ON);

    cd = (EVT_wait(ep) == 0 ? TRUE:ERR);
    EVT_free(ep);

    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(x1,KAKU_Y1,x2,KAKU_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return cd;
}
void	kakunin(char *form,...)
{
    va_list arg;
    int     n,x1,x2;
    int     sw,bx,by;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    char    tmp[256];

    va_start(arg,form);
    vsprintf((char *)tmp,form,arg);
    va_end(arg);

    if ( (n = (strlen(tmp) * 8 + 32) / 2) < 160 )
	n = 160;
    x1 = (KAKU_X1 + KAKU_X2) / 2 - n;
    x2 = (KAKU_X1 + KAKU_X2) / 2 + n;

    MOS_disp(OFF);
    sp = DSP_push_vram(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_opbox(x1,KAKU_Y1,x2,KAKU_Y2);
    DSP_wbox(x1,KAKU_Y1,x2,KAKU_Y2,LINE_COL,FILD_COL,M_PSET);
    gputs(KAKU_MSG_X-strlen(tmp)*4,KAKU_MSG_Y,CHR_COL,FILD_COL,tmp);

/******
    ep = EVT_sw(ep,0,KAKU_CHK_X,KAKU_CHK_Y,XERR_COL,WIND_COL," CHECK ");
*******/
    ep = EVT_img(ep,0,KAKU_CHK_X,KAKU_CHK_Y,XERR_COL,WIND_COL,64,check_img);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((KAKU_X1+KAKU_X2)/2,KAKU_CHK_Y+8);
    MOS_disp(ON);

    EVT_wait(ep);
    EVT_free(ep);

    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(x1,KAKU_Y1,x2,KAKU_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);
}
void	BREAK_open(char *form,...)
{
    va_list arg;
    char    tmp[256];

    va_start(arg,form);
    vsprintf((char *)tmp,form,arg);
    va_end(arg);

    if ( break_save != NULL ) {
	MOS_disp(OFF);
	gputs(BREAK_MSG_X-strlen(tmp)*4,BREAK_MSG_Y, CHR_COL,FILD_COL,tmp);
	MOS_disp(ON);
	EVT_msg_no = ERR;
	return;
    }

    MOS_disp(OFF);
    break_save = DSP_push_vram(BREAK_X1,BREAK_Y1,BREAK_X2,BREAK_Y2);
    DSP_opbox(BREAK_X1,BREAK_Y1,BREAK_X2,BREAK_Y2);
    DSP_wbox(BREAK_X1,BREAK_Y1,BREAK_X2,BREAK_Y2,
					LINE_COL,FILD_COL,M_PSET);
    DSP_rbox(BREAK_BAR_X1,BREAK_BAR_Y1,BREAK_BAR_X2,BREAK_BAR_Y2,
					LINE_COL,WIND_COL,M_PSET);
    gputs(BREAK_MSG_X-strlen(tmp)*4,BREAK_MSG_Y,
					CHR_COL,FILD_COL,tmp);
/*******
    break_ep = EVT_sw(NULL,0,BREAK_CHK_X,BREAK_CHK_Y,
					CHR_COL,WIND_COL," ABORT ");
*******/
    break_ep = EVT_img(NULL,0,BREAK_CHK_X,BREAK_CHK_Y,
					CHR_COL,WIND_COL,64,abort_img);

    DSP_mos(2);
    MOS_disp(ON);

    break_bar = 0;
    EVT_msg_no = ERR;
}
int	BREAK_chk(int max, int pos)
{
    int     n;

    if ( max > 0 ) {
	n = BREAK_BAR_SIZ * pos / max;
	if ( break_bar != n ) {
	    MOS_disp(OFF);
	    if ( n > break_bar )
		DSP_box(BREAK_BAR_X + break_bar, BREAK_BAR_Y,
			BREAK_BAR_X + n, BREAK_BAR_Y + 15,
			7, M_PSET);
	    else
		DSP_box(BREAK_BAR_X + n + 1, BREAK_BAR_Y,
			BREAK_BAR_X + break_bar, BREAK_BAR_Y + 15,
			WIND_COL, M_PSET);
	    MOS_disp(ON);
	}
	break_bar = n;
    }

    EVT_loop(break_ep);

    return (EVT_msg_no == 0 ? TRUE:FALSE);
}
void	BREAK_close(void)
{
    EVT_free(break_ep); break_ep = NULL;
    MOS_disp(OFF);
    DSP_pop_vram(break_save); break_save = NULL;
    DSP_clbox(BREAK_X1,BREAK_Y1,BREAK_X2,BREAK_Y2);
    DSP_mos(0);
    MOS_disp(ON);
}
static LINPTR  *get_ptr(str)
char    *str;
{
    register LINPTR *lp;

    if ( (lp = (LINPTR *)malloc(strlen(str)+sizeof(LINPTR))) != NULL ) {
        lp->next = lp->back = NULL;
	strcpy(lp->buf,str);
    }
    return lp;
}
static LINPTR  *read_line(fp)
FILE    *fp;
{
    int     i,j,ch;
    char    tmp[LIN_MAX + 1];

    for ( i = 0 ; i < LIN_MAX ; ) {
        if ( (ch = getc(fp)) == EOF || ch == '\n' )
            break;
	if ( iskanji(ch) ) {
            if ( i == (LIN_MAX - 1) ) {
                ungetc(ch,fp);
		break;
            }
	    tmp[i++] = ch;
            if ( (ch = getc(fp)) == EOF ) {
                i--;
                break;
            }
            tmp[i++] = ch;
        } else if ( ch == '\t' ) {
	    j = TAB - (i % TAB);
	    while ( i < LIN_MAX && j-- > 0 )
		tmp[i++] = ' ';
	} else
	    tmp[i++] = ch;
    }
    tmp[i] = '\0';

    if ( tmp[0] == '\0' && feof(fp) )
	return NULL;

    return get_ptr(tmp);
}
static LINPTR	*read_file(file)
char    *file;
{
    FILE    *fp;
    LINPTR  *lp;
    LINPTR  tp;
    char    *p;

    file_name[0] = '\0';

    if ( file == NULL || (fp = fopen(file,"r")) == NULL )
	return NULL;

    strcpy(file_name,file);
    if ( (p = strrchr(file_name,'\\')) != NULL )
	*p = '\0';

    lp = &tp;
    while ( (lp->next = read_line(fp)) != NULL ) {
	lp->next->back = lp;
	lp = lp->next;
	max_line++;
    }

    fclose(fp);
    if ( tp.next != NULL )
	tp.next->back = NULL;
    return tp.next;
}
static void	free_file(LINPTR *tp)
{
    LINPTR *lp;

    while ( tp != NULL ) {
	lp = tp->next;
	free(tp);
	tp = lp;
    }
}
static void	scrool_bar_disp(void)
{
    int     x1,x2,s;

    if ( (s = max_line - now_line) > MAX_Y )
	s = MAX_Y;

    if ( bar_old_x1 == (-1) )
	DSP_rbox(BAR_X1,BAR_Y1,BAR_X2,BAR_Y2,LINE_COL,PRG_COL,M_PSET);
    else
	DSP_box(bar_old_x1,BAR_Y1+2,bar_old_x2,BAR_Y2-2,PRG_COL,M_PSET);

    if ( max_line > 0 ) {
        x1 = BAR_X1 + 2 + BAR_SIZ * now_line / max_line;
        x2 = BAR_X1 + 2 + BAR_SIZ * (now_line + s) / max_line;
	DSP_box(x1,BAR_Y1+2,x2,BAR_Y2-2,XPRG_COL,M_PSET);
	bar_old_x1 = x1;
	bar_old_x2 = x2;
    }

    MENU_mask(BACK_NO,now_line > 0 ? ON:OFF);
    MENU_mask(NEXT_NO,(now_line + MAX_Y) < max_line ? ON:OFF);
}
static void	FILE_disp(int line)
{
    int     i;
    LINPTR  *tp;

    while ( line > now_line && now_ptr != NULL && now_ptr->next != NULL ) {
	now_line++;
	now_ptr = now_ptr->next;
    }
    while ( line < now_line && now_ptr != NULL && now_ptr->back != NULL ) {
	now_line--;
	now_ptr = now_ptr->back;
    }

    if ( old_line == now_line )
	return;
    old_line = now_line;

    MOS_disp(OFF);
    now_ofs += slow_ofs; now_ofs &= 511;
    slow_ofs = scr_speed = 0;
    DSP_offset(1,0,(now_ofs - SCRN_Y)&511);
    tp = now_ptr;
    for ( i = 0 ; i < MAX_Y ; i++ ) {
	if ( tp != NULL ) {
	    putstr(tp->buf,PAGE,(i*16+now_ofs)&511);
	    tp = tp->next;
	} else
	    putstr("",PAGE,(i*16+now_ofs)&511);
    }
    scrool_bar_disp();
    MOS_disp(ON);
}
void	FILE_open(char *file)
{
    MOS_disp(OFF);
    if ( save == NULL )
	save = DSP_push_vram(SCRN_X1,SCRN_Y1,SCRN_X2,479);
    DSP_rbox(SCRN_X1,SCRN_Y1,SCRN_X2,SCRN_Y2,LINE_COL,SCRN_COL,M_PSET);
    gputs(240,220,MENU_COL,SCRN_COL,"Now Loading......");
    DSP_mos(2);
    MOS_disp(ON);

    free_file(top_ptr);
    bar_old_x1 = bar_old_x2 = old_line = (-1);
    max_line = now_line = 0;
    slow_ofs = now_ofs = 0;
    top_ptr = now_ptr = read_file(file);
    cut_buf[0][0] = cut_buf[1][0] = '\0';
    FILE_disp(now_line);

    MOS_disp(OFF);
    DSP_offset(1,0,(now_ofs - SCRN_Y)&511);
    DSP_box(0,SCRN_Y,639,SCRN_Y+MAX_Y*16-1,0,M_PSET);
    gprintf(49*8,460,CHR_COL,PRG_COL,"%-30.30s",file_name);
    DSP_mos(0);
    MOS_disp(ON);
    MENU_mask(PRNT_NO,top_ptr == NULL ? OFF:ON);
}
void	FILE_close(void)
{
    free_file(top_ptr);
    top_ptr = now_ptr = NULL;
    MOS_disp(OFF);
    DSP_pop_vram(save);
    DSP_clear(1);
    MOS_disp(ON);
    save = NULL;
}
void	FILE_redisp()
{
    if ( save == NULL || top_ptr == NULL )
	return;
    old_line = (-1);
    FILE_disp(now_line);
}
void	FILE_back(void)
{
    if ( top_ptr == NULL || now_line <= 0 )
	return;
    FILE_disp(now_line - MAX_Y);
}
void	FILE_next(void)
{
    if ( top_ptr == NULL || (now_line + MAX_Y) >= max_line )
	return;
    FILE_disp(now_line + MAX_Y);
}
static	int	FILE_one_up(void)
{
    int     i;
    LINPTR  *tp;

    if ( (now_line + MAX_Y) >= max_line ||
	 now_ptr == NULL || now_ptr->next == NULL )
	return ERR;

    now_line++;
    old_line++;
    tp = now_ptr = now_ptr->next;
    for ( i = 1 ; tp != NULL && i < MAX_Y ; i++ )
	tp = tp->next;

    MOS_disp(OFF);
    putstr((tp == NULL ? "":tp->buf),PAGE,(MAX_Y*16+now_ofs+slow_ofs)&511);
    slow_ofs += 16;
    scrool_bar_disp();
    MOS_disp(ON);

    return FALSE;
}
static	int	FILE_one_down(void)
{
    if ( now_ptr == NULL || now_ptr->back == NULL )
	return ERR;

    now_line--;
    old_line--;
    now_ptr = now_ptr->back;

    MOS_disp(OFF);
    putstr(now_ptr->buf,PAGE,(now_ofs+slow_ofs-16)&511);
    slow_ofs -= 16;
    scrool_bar_disp();
    MOS_disp(ON);

    return FALSE;
}
static	void	FILE_offset(int ofs)
{
    int n;

    while ( ofs > 0 ) {
	while ( slow_ofs <= 0 ) {
	    if ( FILE_one_up() )
		return;
	}
	n = (ofs < slow_ofs ? ofs : slow_ofs);
	now_ofs = (now_ofs + n) & 511;
	DSP_offset(1, 0, (now_ofs - SCRN_Y) & 511);
	slow_ofs -= n;
	ofs -= n;
    }

    while ( ofs < 0 ) {
	while ( slow_ofs >= 0 ) {
	    if ( FILE_one_down() )
		return;
	}
	n = (-ofs < -slow_ofs ? -ofs : -slow_ofs);
	now_ofs = (now_ofs - n) & 511;
	DSP_offset(1, 0, (now_ofs - SCRN_Y) & 511);
	slow_ofs += n;
	ofs += n;
    }
}
void	FILE_stop()
{
    int n;

    while ( scr_speed != 0 ) {
	if ( (n = tick_timer - old_timer) != 0 ) {
	    old_timer = tick_timer;
	    if ( n < 0 )
		n = 0 - n;
	    scr_speed /= 2;
	    if ( (n *= scr_speed) > 128 )
		n = 128;
	    else if ( n < -128 )
		n = -128;
	    FILE_offset(n);
	}
    }

    while ( 0 != slow_ofs ) {
	if ( tick_timer != old_timer ) {
	    old_timer = tick_timer;
	    if ( slow_ofs > 0 )
		FILE_offset(1);
	    else
		FILE_offset(-1);
	}
    }
}
void	FILE_irq(void)
{
    int i, n, rc, fg;
    int sw, x, y, sx, sy;
    int of, nk1, nk2, oy1, oy2;

    if ( top_ptr == NULL )
	return;

    MOS_rdpos(&sw, &x, &y);

    if ( sw == 0 ) {
	if ( scr_speed != 0 && (n = tick_timer - old_timer) != 0 ) {
	    if ( n < 0 )
		n = 0 - n;
	    old_timer = tick_timer;
	    if ( (n *= scr_speed) > 128 )
		n = 128;
	    else if ( n < -128 )
		n = -128;
	    FILE_offset(n);
	}
	return;
    }

    fg = rc = FALSE;
    of = 0;
    nk1 = nk2 = tick_timer;
    oy1 = oy2 = y;
    FILE_stop();

    if ( y >= SCRN_Y1 && y <= SCRN_Y2 ) {
	DSP_mos(1);
	while ( sw != 0 ) {
	    MOS_rdpos(&sw, &sx, &sy);
	    if ( cut_mode != FALSE && y >= SCRN_Y && sw != 0 &&
		 ((sx - x) >= 16 || fg != FALSE) ) {
		rc = CLIP_box(sw == 1 ? 0:1,
			      x / 8,(y - SCRN_Y) / 16,(sx - x) / 8);
		if ( rc != FALSE )
		    fg = TRUE;
	    }

	    if ( fg != FALSE )
		continue;

	    if ( sw == 0 ) {			/* end of */
		if ( (n = tick_timer - nk2) > 0 && n < 100 )
		    scr_speed = (oy2 - sy) / n;
		else
		    scr_speed = 0;
		old_timer = tick_timer;
		break;

	    } else if ( cut_mode != FALSE ) {
		if ( (n = y - sy) <= -16 || n >= 16 && n != of ) {
		    FILE_offset(n - of);
		    of = n;
		}
	    } else if ( (n = y - sy) != of ) {
		FILE_offset(n - of);
		of = n;
	    }

	    if ( nk1 != tick_timer ) {
		oy2 = oy1;
		nk2 = nk1;
		oy1 = sy;
		nk1 = tick_timer;
	    }
	}
	DSP_mos(0);

    } else if ( x >= BAR_X1 && x <= BAR_X2 &&
	 	y >= BAR_Y1 && y <= BAR_Y2 ) {
	DSP_mos(1);
	MOS_horizon(BAR_X1,BAR_X2);
	MOS_vertical(BAR_Y1,BAR_Y2-8);
	do {
	    FILE_disp((x - BAR_X1) * max_line / BAR_SIZ);
	    MOS_rdpos(&sw,&x,&y);
	} while ( sw != 0 );
	MOS_horizon(0,632);
	MOS_vertical(0,470);
	DSP_mos(0);
    }
}
/****************************
static int	LPT_putc(char ch,FILE *fp)
{
    for ( ; ; ) {
	putc(ch,fp);
	if ( !ferror(fp) )
	    break;
	if ( pause("プリンタ出力にエラ−が発生しました！") == ERR )
	    return ERR;
	clearerr(fp);
    }
    return FALSE;
}
void	FILE_lpt(void)
{
    int     n,ln;
    LINPTR  *lp;
    FILE    *fp;
    char    *p;

    if ( top_ptr == NULL )
	return;

    if ( yesno("表示している文書をプリンタに出力しますか？") == ERR )
	return;

    if ( (fp = fopen("PRN","w")) == NULL ) {
	kakunin("プリンタ出力がオ−プン出来ません");
	return;
    }

    BREAK_open("プリンタに出力しています ちょっと待ってね");

    n = ln = 0;
    for ( lp = top_ptr ; lp != NULL ; lp = lp->next ) {
	for ( p = lp->buf ; *p != '\0' ; p++ ) {
	    if ( LPT_putc(*p,fp) )
		goto ENDOF;
	}
	if ( LPT_putc('\r',fp) || LPT_putc('\n',fp) )
	    goto ENDOF;
	if ( ++ln >= LPT_LINE ) {
	    if ( LPT_putc('\x0C',fp) )
		goto ENDOF;
	    ln = 0;
	}
	if ( BREAK_chk(max_line,++n) )
	    break;
    }
    if ( ln > 0 )
	LPT_putc('\x0C',fp);

ENDOF:
    fclose(fp);
    BREAK_close();
}
*****************************************/

void	FILE_lpt(void)
{
    int     n;
    LINPTR  *lp;

    if ( top_ptr == NULL )
	return;

/*********
    if ( yesno("Are You Sure ?") == ERR )
	return;
**********/

    if ( LPT_open() == ERR )
	return;

    BREAK_open("Working.....");

    n = 0;
    for ( lp = top_ptr ; lp != NULL ; lp = lp->next ) {
	if ( PRN_puts(lp->buf) )
	    goto ENDOF;
	if ( BREAK_chk(max_line,++n) )
	    break;
    }
    PRN_close();

ENDOF:
    BREAK_close();
}

int	iskan(char *p)
{
    if ( iskanji(*p) && iskanji2(*(p+1)) )
	return TRUE;
    else
	return FALSE;
}
int	kan_pos(char *p,int n)
{
    int     i;

    for ( i = n ; i > 0 ; ) {
	if ( *p == '\0' ) {
	    return (n - i);

	} else if ( iskan(p) ) {
	    i -= 2;
	    p += 2;

	} else {
	    i--;
	    p++;
	}
    }
    return (n + i);
}

int	input(int x,int y,int max,char *str)
{
    int     i,n,ch,cd;
    int     ofs=0,len=0,pos=0;
    BLOCK   *sp;
    char    *p;
    char    tmp[256];

    INKEY_start();
    pos = len = strlen(str);

    memset(tmp,' ',max);
    tmp[max] = '\0';
    gputs(x,y,CHR_COL,WIND_COL,tmp);

    for ( ch = 0 ; ; ) {
	ofs = 0;
	n = pos;
	while ( n >= (max - 4) ) {
	    ofs += (max / 2);
	    ofs = kan_pos(str,ofs);
	    n = pos - ofs;
	}

	if ( (i = len - ofs) >= max )
	    i = kan_pos(str,ofs+max) - ofs;

	strncpy(tmp,&(str[ofs]),i);
	tmp[i] = '\0';

        sp = DSP_push_vram(x,y,x+(i+3)*8,y+15);
        gputs(x,y,CHR_COL,WIND_COL,tmp);
	DSP_box(x+n*8,y+14,x+n*8+7,y+15,13,M_XOR);

	cur_x = x + n * 8;
	cur_y = y;

	while ( (ch = getch()) == EOF );

	do {

	    if ( ch == 0x08 && pos > 0 ) {
		pos = kan_pos(str,pos-1);
		p = &(str[pos]);
		n = (iskan(p) ? 2:1);
		strcpy(p,p+n);
		len -= n;

	    } else if ( ch == 0x7F ) {
		if ( pos < len ) {
		    p = &(str[pos]);
		    n = (iskan(p) ? 2:1);
		    strcpy(p,p+n);
		    len -= n;
		}

	    } else if ( ch == 0x1C ) {
		if ( pos < len )
		    pos += (iskan(&(str[pos])) ? 2:1);

	    } else if ( ch == 0x1D ) {
		if ( pos > 0 )
		    pos = kan_pos(str,pos-1);

	    } else if ( ch >= ' ' && len < 128 ) {
		if ( pos < len ) {
		    p = &(str[len]);
		    for ( i = len - pos ; i > 0 ; i--,p-- )
			*p = *(p-1);
		}
		str[pos++] = ch;
		len++;

	    } else if ( ch == 0x0D ) {
		cd = FALSE;
		goto ENDOF;
	    } else if ( ch == 0x1B ) {
		cd = ERR;
		goto ENDOF;
	    }

	} while ( (ch = getch()) != EOF );

        DSP_pop_vram(sp);
    }

ENDOF:
    DSP_pop_vram(sp);
    str[len] = '\0';
    INKEY_end();
    if ( (i = len) >= max )
	 i = kan_pos(str,max);
    strncpy(tmp,&(str[ofs]),i);
    tmp[i] = '\0';
    gputs(x,y,CHR_COL,WIND_COL,tmp);

    return cd;
}
int	CLIP_box(box,cx,cy,len)
int	box,cx,cy,len;
{
    int     i,n;
    int     cut_x1,cut_y1,cut_x2,cut_y2;
    LINPTR  *tp;

    tp = now_ptr;
    for ( i = 0 ; tp != NULL && i < cy ; i++ )
	    tp = tp->next;

    if ( tp == NULL )
	return FALSE;

    for ( i = 0 ; tp->buf[i] != '\0' && i < cx ; i++ ) {
	if ( iskan(&(tp->buf[i])) ) {
	    if ( (i + 1) >= cx )
		break;
	    i++;
	}
    }
    cx = i;

    for ( n = 0 ; tp->buf[i] != '\0' && n < len ; ) {
	if ( iskan(&(tp->buf[i])) ) {
	    cut_buf[box][n++] = tp->buf[i++];
	    cut_buf[box][n++] = tp->buf[i++];
	} else {
	    cut_buf[box][n++] = tp->buf[i++];
	}
    }
    cut_buf[box][n] = '\0';

    if ( (len = n) == 0 )
	return FALSE;

    MOS_disp(OFF);
    DSP_page(1);
    cut_x1 = cx * 8;
    cut_y1 = (cy * 16 + now_ofs + slow_ofs) & 511;
    cut_x2 = (cx + len) * 8 - 1;
    cut_y2 = (cy * 16 + 15 + now_ofs + slow_ofs) & 511;
    DSP_vsync();
    DSP_box(cut_x1,cut_y1,cut_x2,cut_y2,8,M_XOR);
    DSP_vsync();
    DSP_box(cut_x1,cut_y1,cut_x2,cut_y2,8,M_XOR);
    DSP_page(0);
    MOS_disp(ON);

    return TRUE;
}
/********************************************************************

	COPY Funcsion

*********************************************************************/

static int	dir_make(char *file)
{
    char    *p;

    if ( (p = strchr(file,'\\')) != NULL ) {
	*p = '\0';
	if ( chdir(file) ) {
	    if ( mkdir(file) ) {
		kakunin("ディレクトリの作成に失敗しました");
		return ERR;
	    } else if ( chdir(file) ) {
		kakunin("ディレクトリの移動に失敗しました");
		return ERR;
	    }
	}
	*p = '\\';
	dir_make(p+1);
	chdir("..");
    }
    return FALSE;
}
static int	copy(char *src,char *dis)
{
    int     cd,i,n;
    long    fs;
    FILE    *ifp,*ofp;
    char    *p;
    char    tmp[4096];

    if ( (ifp = fopen(dis,"rb")) != NULL ) {
	fclose(ifp);
	if ( yesno("%sが存在しますｺﾋﾟ-を行いますか？",dis) == ERR )
	    return TRUE;
    }

    if ( dir_make(dis) == ERR )
	return ERR;

    if ( (ifp = fopen(src,"rb")) == NULL ) {
	kakunin("%sﾌｧｲﾙがオ−プンできません",src);
	return ERR;
    }

    if ( (ofp = fopen(dis,"wb")) == NULL ) {
	fclose(ifp);
	kakunin("%sﾌｧｲﾙがオ−プンできません",dis);
	return ERR;
    }

    cd = FALSE;
    n = 0;
    fseek(ifp,0L,SEEK_END);
    fs = ftell(ifp);
    rewind(ifp);

    if ( (p = strrchr(dis,'\\')) != NULL )
	p++;
    else
	p = dis;
    BREAK_open("Making a copy %s", p);

    while ( (i = fread(tmp,1,4096,ifp)) > 0 ) {
	fwrite(tmp,1,i,ofp);
	n += i;
	if ( ferror(ifp) || ferror(ofp) ) {
	    kakunin("ファイルコピ−にエラ−が発生しました");
	    cd = ERR;
	    break;
	}
	if ( BREAK_chk(fs,n) ) {
	    cd = ERR;
	    break;
	}
    }

    fclose(ifp);
    fclose(ofp);
    if ( cd == ERR )
	remove(dis);
    BREAK_close();

    return cd;
}
static	int	cmds_xcopy(char *home,char *src,char *dis)
{
    int     n;
    int     cd;
    int     wd=FALSE;
    DIR     *dirp;
    DIRECT  *dp;
    char    *p;
    char    tmp[128];
    char    dmy[128];

    if ( src == NULL || *src == '\0' )
	src = "*.*";

    if ( src[1] != ':' && home != NULL ) {
	strcpy(tmp,home);
	joint_path(tmp,src);
    } else
	strcpy(tmp,src);

    for ( p = tmp ; *p != '\0' ; p++ );

    if ( strchr(tmp,'*') != NULL || strchr(tmp,'?') )
	wd = TRUE;
    else if ( (p != tmp && *(p-1) == '\\') || isdir(tmp) ) {
	joint_path(tmp,"*.*");
	wd = TRUE;
    }

    DSP_mos(2);
    dirp = opendir(tmp);
    DSP_mos(0);

    if ( dirp == NULL ) {
	kakunin("%sファイルが見当たりません",tmp);
	return ERR;
    }

    if ( (src = strrchr(tmp,'\\')) != NULL ||
	 (src = strrchr(tmp,':')) != NULL )
	src++;
    else
	src = tmp;

    if ( dis == NULL )
	dis = "";

    strcpy(dmy,dis);

    for ( n = 0 ; dmy[n] != '\0' ; n++ );
    if ( n == 0 || dmy[n-1] == '\\' || dmy[n-1] == ':' )
	 wd = TRUE;

    if ( wd != FALSE ) {
	for ( n = 0 ; dmy[n] != '\0' ; n++ );
	if ( n > 0 && dmy[n-1] != '\\' && dmy[n-1] != ':' )
	    strcat(dmy,"\\");
	dis = dmy;
	while ( *dis != '\0' ) dis++;
    }

    while ( (dp = readdir(dirp)) != NULL ) {
	if ( !IS_DIR(dp) ) {

	    strcpy(src,dp->d_name);

	    if ( wd != FALSE )
		strcpy(dis,dp->d_name);

	    while ( (cd = copy(tmp,dmy)) == ERR ) {
		if ( pause("コピ−を続行しますか？") == ERR )
		    goto ENDOF;
	    }
	} else if ( strcmp(dp->d_name,".") != 0 &&
		    strcmp(dp->d_name,"..") != 0 ) {
	    strcpy(src,dp->d_name);
	    if ( wd != FALSE )
		strcpy(dis,dp->d_name);
	    if ( cmds_xcopy(home,tmp,dmy) ) {
		if ( pause("コピ−を続行しますか？") == ERR )
		    goto ENDOF;
	    }
	}
    }

ENDOF:
    closedir(dirp);
    return (cd == ERR ? ERR:FALSE);
}
static	int	cmds_copy(char *home,char *src,char *dis)
{
    int     n;
    int     cd;
    int     wd=FALSE;
    DIR     *dirp;
    DIRECT  *dp;
    char    *p;
    char    tmp[128];
    char    dmy[128];

    if ( src == NULL || *src == '\0' )
	src = "*.*";

    if ( src[1] != ':' && home != NULL ) {
	strcpy(tmp,home);
	joint_path(tmp,src);
    } else
	strcpy(tmp,src);

    for ( p = tmp ; *p != '\0' ; p++ );

    if ( strchr(tmp,'*') != NULL || strchr(tmp,'?') )
	wd = TRUE;
    else if ( (p != tmp && *(p-1) == '\\') || isdir(tmp) ) {
	joint_path(tmp,"*.*");
	wd = TRUE;
    }

    DSP_mos(2);
    dirp = opendir(tmp);
    DSP_mos(0);

    if ( dirp == NULL ) {
	kakunin("%sファイルが見当たりません",tmp);
	return ERR;
    }

    if ( (src = strrchr(tmp,'\\')) != NULL ||
	 (src = strrchr(tmp,':')) != NULL )
	src++;
    else
	src = tmp;

    if ( dis == NULL )
	dis = "";

    strcpy(dmy,dis);

    for ( n = 0 ; dmy[n] != '\0' ; n++ );
    if ( n == 0 || dmy[n-1] == '\\' || dmy[n-1] == ':' )
	 wd = TRUE;

    if ( wd != FALSE ) {
	for ( n = 0 ; dmy[n] != '\0' ; n++ );
	if ( n > 0 && dmy[n-1] != '\\' && dmy[n-1] != ':' )
	    strcat(dmy,"\\");
	dis = dmy;
	while ( *dis != '\0' ) dis++;
    }

    while ( (dp = readdir(dirp)) != NULL ) {
	if ( !IS_DIR(dp) ) {

	    strcpy(src,dp->d_name);

	    if ( wd != FALSE )
		strcpy(dis,dp->d_name);

	    while ( (cd = copy(tmp,dmy)) == ERR ) {
		if ( pause("コピ−を続行しますか？") == ERR )
		    goto ENDOF;
	    }
	}
    }

ENDOF:
    closedir(dirp);
    return (cd == ERR ? ERR:FALSE);
}
static	int	cmds_rename(char *src,char *dis)
{
    if ( src[0] != '\0' && src[1] == ':' ) {
	kakunin("絶対パスでのファイル名変更はできません");
	return ERR;
    }

    if ( rename(src,dis) ) {
	kakunin("ファイル名の変更ができませんでした");
	return ERR;
    }
    return FALSE;
}
static	int	cmds_mkdir(char *dir)
{
    if ( dir[0] != '\0' && dir[1] == ':' ) {
	kakunin("絶対パスでのディレクトリ作成はできません");
	return ERR;
    }

    if ( mkdir(dir) ) {
	kakunin("ディレクトリの作成ができませんでした");
	return ERR;
    }
    return FALSE;
}
static	int	cmds_chdir(char *dir)
{
    if ( dir[0] != '\0' && dir[1] == ':' ) {
	kakunin("絶対パスでのディレクトリ移動はできません");
	return ERR;
    }

    if ( chdir(dir) ) {
	kakunin("ディレクトリの移動ができませんでした");
	return ERR;
    }
    return FALSE;
}
static	int	cmdstr(char *src,char *ptn)
{
    while ( *ptn != '\0' ) {
	if ( toupper(*src) != *ptn )
	    return FALSE;
	src++;
	ptn++;
    }
    if ( *src == '\0' || isspace(*src) )
	return TRUE;
    else
	return FALSE;
}

#define	ST_EOF		0
#define	ST_ELSE		1
#define	ST_ENDIF	2

static	char	cmds_dmy[BUFSIZ];
static	char	cmds_home[BUFSIZ];

static	int	cmds_line(LINPTR **fp,int rc)
{
    int     n;
    char    *p;
    char    *s;
    char    *r;

    while ( (*fp)->next != NULL ) {
	(*fp) = (*fp)->next;
	p = (*fp)->buf;

	while ( isspace(*p) ) p++;

	if ( *p == '\0' || *p == '#' )
	    continue;

	if ( cmdstr(p,"IF") ) {
	    if ( rc == FALSE ) {
		while ( (n = cmds_line(fp,FALSE)) == ST_ELSE );
		if ( n == ST_EOF )
		    break;

		continue;
	    }

	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    if ( yesno2(p) == ERR ) {	/* else */
		if ( (n = cmds_line(fp,FALSE)) == ST_ELSE )
		   n = cmds_line(fp,TRUE);

	    } else {			/* then */
		if ( (n = cmds_line(fp,TRUE)) == ST_ELSE )
		   n = cmds_line(fp,FALSE);
	    }
	    if ( n == ST_EOF )
		break;

	    continue;

	} else if ( cmdstr(p,"ELSE") ) {
	    return ST_ELSE;

	} else if ( cmdstr(p,"ENDIF") ) {
	    return ST_ENDIF;
	}

	if ( rc == FALSE )
	    continue;

	if ( cmdstr(p,"XCOPY") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    s = p;
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    if ( *p != '\0' ) *(p++) = '\0';
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmds_xcopy(cmds_home,s,p) )
		break;

	} else if ( cmdstr(p,"COPY") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    s = p;
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    if ( *p != '\0' ) *(p++) = '\0';
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmds_copy(cmds_home,s,p) )
		break;

	} else if ( cmdstr(p,"RENAME") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    s = p;
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    if ( *p != '\0' ) *(p++) = '\0';
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmds_rename(s,p) )
		break;

	} else if ( cmdstr(p,"MKDIR") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmds_mkdir(p) )
		break;

	} else if ( cmdstr(p,"CHDIR") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmds_chdir(p) )
		break;

	} else if ( cmdstr(p,"PAUSE") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    if ( pause(p) == ERR )
		break;

	} else if ( cmdstr(p,"INST") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    strcpy(cmds_dmy,cmds_home);
	    joint_path(cmds_dmy,p);
	    if ( CMDS_file(cmds_dmy) )
		break;

	} else if ( cmdstr(p,"LOOK") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    strcpy(cmds_dmy,cmds_home);
	    joint_path(cmds_dmy,p);
	    if ( !JOKE_run(cmds_dmy) )
		break;

	} else if ( cmdstr(p,"PLAY") ) {
	    while ( !isspace(*p) && *p != '\0' ) p++;
	    while ( isspace(*p) ) p++;
	    r = p;
	    while ( !isspace(*r) && *r != '\0' ) r++;
	    *r = '\0';
	    if ( cmdstr(p,"STOP") ) {
		END_eup();
	    } else {
	    	strcpy(cmds_dmy,cmds_home);
	    	joint_path(cmds_dmy,p);
	    	PLAY_eup(cmds_dmy);
	    }

	} else if ( cmdstr(p,"PUSH") ) {
	    SCRN_saver(1);

	} else if ( cmdstr(p,"POP") ) {
	    SCRN_saver(0);

	} else if ( cmdstr(p,"EXIT") ) {
	    break;

	}
    }

    return ST_EOF;
}
int	CMDS_file(char *file)
{
    int     n;
    LINPTR  tp;
    LINPTR  *fp;
    char    *p;
    char    home[BUFSIZ];

    n = max_line;
    if ( (tp.next = read_file(file)) == NULL ) {
	kakunin("'%s'コマンドが見当たりません",file);
	return ERR;
    }
    max_line = n;

    strcpy(home,cmds_home);
    strcpy(cmds_home,file);
    if ( (p = strrchr(cmds_home,'\\')) == NULL )
	p = cmds_home;
    *p = '\0';

    fp = &tp;
    cmds_line(&fp,TRUE);

    strcpy(cmds_home,home);
    free_file(tp.next);
    return FALSE;
}

	unsigned long	fcopy_max = 0;
	unsigned long	fcopy_pos = 0;

static int	fcopy(char *src, char *dis, long sz)
{
    int     cd,i,n;
    long    fs;
    FILE    *ifp,*ofp;
    char    *p;
    char    tmp[4096];

    if ( (ifp = fopen(dis,"rb")) != NULL ) {
	fclose(ifp);
	if ( yesno("%sが存在しますｺﾋﾟ-を行いますか？",dis) == ERR )
	    return TRUE;
    }

    if ( dir_make(dis) == ERR )
	return ERR;

    if ( (ifp = fopen(src,"rb")) == NULL ) {
	kakunin("%sﾌｧｲﾙがオ−プンできません",src);
	return ERR;
    }

    if ( (ofp = fopen(dis,"wb")) == NULL ) {
	fclose(ifp);
	kakunin("%sﾌｧｲﾙがオ−プンできません",dis);
	return ERR;
    }

    cd = FALSE;

    if ( (p = strrchr(dis,'\\')) != NULL )
	p++;
    else
	p = dis;

    BREAK_open("%s %-12.12s", strsize(sz), p);

    fs = fcopy_pos;
    while ( (i = fread(tmp,1,4096,ifp)) > 0 ) {
	if ( fwrite(tmp, 1, i, ofp) != i )
	    break;
	fs += i;
	if ( BREAK_chk(fcopy_max, fs) ) {
	    cd = ERR;
	    break;
	}
    }

    if ( ferror(ifp) || ferror(ofp) ) {
	kakunin("ファイルコピ−にエラ−が発生しました");
	cd = ERR;
    }

    fclose(ifp);
    fclose(ofp);

    if ( cd == ERR )
	remove(dis);
    else
	fcopy_pos = fs;

    return cd;
}
static int	wcopy(char *wild)
{
    int     cd = ERR;
    long    sz;
    DIR     *dirp;
    DIRECT  *dp;
    char    *p;
    char    *src,*dis;
    char    tmp[256];

    if ( (dirp = opendir(wild)) == NULL )
	return ERR;

    strcpy(tmp,wild);
    if ( (p = strrchr(tmp,'\\')) != NULL )
	p++;
    else
	p = tmp;

    while ( (dp = readdir(dirp)) != NULL ) {
	if ( !IS_DIR(dp) ) {

	    strcpy(p,dp->d_name);
	    src = tmp;
	    dis = dp->d_name;

	    while ( (cd = fcopy(src, dis, dp->d_size)) == ERR ) {
		if ( yesno("コピ−を続行しますか？") == ERR )
		    goto ENDOF;
	    }

	} else if ( dp->d_name[0] != '.' ) {
	    strcpy(p, dp->d_name);
	    strcat(tmp, "\\*.*");

	    if ( chdir(dp->d_name) ) {
		if ( mkdir(dp->d_name) ) {
		    kakunin("サブディレクトリの作成に失敗しました");
		    goto ENDOF;
		} else if ( chdir(dp->d_name) ) {
		    kakunin("サブディレクトリへの移動に失敗しました");
		    goto ENDOF;
		}
	    }

	    if ( (cd = wcopy(tmp)) == ERR )
		goto ENDOF;

	    if ( chdir("..") ) {
		kakunin("親ディレクトリへの移動に失敗しました");
		goto ENDOF;
	    }
	}
    }

    cd = FALSE;

ENDOF:
    closedir(dirp);
    return cd;
}
int	zcopy(int ac, char *av[])
{
    int n;
    char *p;
    unsigned long fr, ta;

    for ( n = 0 ; n < ac ; n++ ) {
	if ( (p = strrchr(av[n], '.')) != NULL && strcmp(p,".QQQ") == 0 )
	    return CMDS_file(av[n]);
    }

    if ( !disk_free((-1), &fr, &ta) && (fcopy_max / 1024) > fr ) {
	if ( yesno("空き容量が足りませんコピ−を続行しますか？") == ERR )
	    return ERR;
    }

    BREAK_open("Total %4dK", fcopy_max / 1000);
    for ( n = 0 ; n < ac ; n++ ) {
	if ( wcopy(av[n]) ) {
	    BREAK_close();
	    return ERR;
	}
    }
    BREAK_close();

    return FALSE;
}

#define	EXEC_X1		(13 * 8 - 4)
#define	EXEC_Y1		210
#define	EXEC_X2		(65 * 8 + 3)
#define	EXEC_Y2		(EXEC_Y1 + 60)

#define	EXEC_MSG_X	(14 * 8)
#define	EXEC_MSG_Y	(EXEC_Y1 + 10)

#define	EXEC_YES_X	((EXEC_X1+EXEC_X2)/2-(10*8))
#define	EXEC_YES_Y	(EXEC_Y1 + 34)

#define	EXEC_NO_X	((EXEC_X1+EXEC_X2)/2+(2*8))
#define	EXEC_NO_Y	(EXEC_Y1 + 34)

int	EXEC_input(char *tmp)
{
    int n, cd;
    int sw,bx,by;
    BLOCK *sp;
    EVENT *ep=NULL;
    char  dmy[256];

    MOS_disp(OFF);
    sp = DSP_push_vram(EXEC_X1,EXEC_Y1,EXEC_X2,EXEC_Y2);
    DSP_opbox(EXEC_X1,EXEC_Y1,EXEC_X2,EXEC_Y2);

    DSP_wbox(EXEC_X1,EXEC_Y1,EXEC_X2,EXEC_Y2,LINE_COL,FILD_COL,M_PSET);
/*****************
    DSP_wbox(EXEC_MSG_X - 4, EXEC_MSG_Y - 2,
	     EXEC_MSG_X + 50 * 8 + 3, EXEC_MSG_Y + 17,
	     LINE_COL,WIND_COL,M_PSET);
******************/

    ep = EVT_img(ep,0,EXEC_YES_X,EXEC_YES_Y,CHR_COL,WIND_COL,64,exec_img);
    ep = EVT_img(ep,1,EXEC_NO_X, EXEC_NO_Y, CHR_COL,WIND_COL,64,cancel_img);
    sprintf(dmy, "%-50.50s", tmp);
    ep = EVT_sw(ep,5,EXEC_MSG_X,EXEC_MSG_Y,CHR_COL,WIND_COL, dmy);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((EXEC_X1+EXEC_X2)/2,(EXEC_Y1+EXEC_Y2)/2);
    MOS_disp(ON);

    for ( ; ; ) {
	n = EVT_wait(ep);
	if ( n == 0 ) {
	    cd = FALSE;
	    break;
	} else if ( n == 1 ) {
	    cd = ERR;
	    break;
	} else {
	    MOS_disp(OFF);
	    input(EXEC_MSG_X, EXEC_MSG_Y, 50, tmp);
	    gprintf(EXEC_MSG_X,EXEC_MSG_Y,CHR_COL,WIND_COL,
		"%-50.50s", tmp);
	    MOS_disp(ON);
	}
    }

    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(EXEC_X1,EXEC_Y1,EXEC_X2,EXEC_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return cd;
}
/*************************************************************
01234567890123456789012345678901234567890123456789012345678901234567890123456789                01234567890123456789012345678901234567890123456
                +---------------------------------------------+
0               |             ふぁいる せれくた〜       | × ||
1               |+---++---++---++---++---++---++---++---++---+|
2               || A || B || C || D || E || F || G || H || I ||
3               |+---++---++---++---++---++---++---++---++---+|
4               |+---++---++---++---++---++---++---++---++---+|
5               || J || K || L || M || N || O || P || Q || R ||
6               |+---++---++---++---++---++---++---++---++---+|
7               |******************************************** |
8               |xxxxxxxxxx xxx|xxxxxxxxxx xxx|xxxxxxxxxx xxx |
9               |xxxxxxxxxx xxx|xxxxxxxxxx xxx|xxxxxxxxxx xxx |
10              |xxxxxxxxxx xxx|xxxxxxxxxx xxx|xxxxxxxxxx xxx |
11              |xxxxxxxxxx xxx|xxxxxxxxxx xxx|xxxxxxxxxx xxx |
12              |xxxxxxxxxx xxx|xxxxxxxxxx xxx|xxxxxxxxxx xxx |
13              |ooooooooooooooooooooooooooooooooooooo|＜||＞||
14              +---------------------------------------------+
**************************************************************/

#define	FSEL_X1		(16*8)
#define	FSEL_Y1		(240-8*20)
#define	FSEL_X2		(62*8)
#define	FSEL_Y2		(FSEL_Y1+264)

#define	FSEL_MSG_X	(FSEL_X1+23*8)
#define	FSEL_MSG_Y	(FSEL_Y1+4)

#define	FSEL_TIFF_X	(FSEL_X1+8)
#define	FSEL_TIFF_Y	(FSEL_Y1+4)

#define	FSEL_EXIT_X	(FSEL_X1+43*8-2)
#define	FSEL_EXIT_Y	(FSEL_Y1+4)

#define	FSEL_DRIV_X	(FSEL_X1+1*8)
#define	FSEL_DRIV_Y	(FSEL_Y1+24)

#define	FSEL_DIR_X	(FSEL_X1+1*8)
#define	FSEL_DIR_Y	(FSEL_Y1+103)

#define	FSEL_FILE_X	(FSEL_X1+1*8)
#define	FSEL_FILE_Y	(FSEL_Y1+124)

#define	FSEL_BACK_X	((FSEL_X1+FSEL_X2)/2-8*8)
#define	FSEL_BACK_Y	(FSEL_Y1+240)

#define	FSEL_NEXT_X	((FSEL_X1+FSEL_X2)/2+2*8)
#define	FSEL_NEXT_Y	(FSEL_Y1+240)

#define	FSEL_DPOS_X1(n)	(FSEL_DRIV_X+((n)%9)*40)
#define	FSEL_DPOS_Y1(n)	(FSEL_DRIV_Y+((n)/9)*40)
#define	FSEL_DPOS_X2(n)	(FSEL_DRIV_X+((n)%9)*40+33)
#define	FSEL_DPOS_Y2(n)	(FSEL_DRIV_Y+((n)/9)*40+33)

#define	FSEL_FPOS_X1(n)	(FSEL_FILE_X+((n)/5)*15*8-4)
#define	FSEL_FPOS_Y1(n)	(FSEL_FILE_Y+((n)%5)*22)
#define	FSEL_FPOS_X2(n)	(FSEL_FILE_X+((n)/5)*15*8+15*8-6)
#define	FSEL_FPOS_Y2(n)	(FSEL_FILE_Y+((n)%5)*22+19)

static	char	*HIS_dir(void);

char	*formname(name)
char	*name;
{
    int     i;
    char    *p;
    static char tmp[16];

    if ( strcmp(name,"..") == 0 || (p = strchr(name,'.')) == NULL )
        for ( p = name ; *p != '\0' ; p++ );

    for ( i = 0 ; name < p && i < 8 ; i++ )
	tmp[i] = *(name++);

    for ( ; i < 11 ; i++ )
	tmp[i] = ' ';

    if ( *p == '.' ) p++;

    while ( *p != '\0' )
	tmp[i++] = *(p++);

    for ( ; i < 14 ; i++ )
	tmp[i] = ' ';

    tmp[i] = '\0';
    return tmp;
}
char	*FILE_select(void)
{
    int     i,n,cd;
    int     sw,bx,by;
    int     drv_old,drv_now;
    int     pos_old,pos_now;
    int     dir_flg,dir_max;
    BLOCK   *sp;
    EVENT   *ep=NULL;
    DIR     *dirp;
    DIRECT  *dp;
    DIRECT  *file_ptr[15];
    char    drv_no[26];
    char    *p;
    static char tmp[128];

    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,FILD_COL,M_PSET);

    DSP_strimg(file_selecter_img,
		FSEL_MSG_X - 46, FSEL_MSG_Y + 3,
		CHR_COL, FILD_COL, M_PSET);
/**********
    gputs(FSEL_MSG_X,FSEL_MSG_Y,CHR_COL,FILD_COL,"FILE SELECTOR");
***********/

    ep = EVT_sw(ep,0,FSEL_EXIT_X,FSEL_EXIT_Y,CHR_COL,WIND_COL,"×");
    ep = EVT_sw(ep,1,FSEL_BACK_X,FSEL_BACK_Y,CHR_COL,WIND_COL,"  ≪  ");
    ep = EVT_sw(ep,2,FSEL_NEXT_X,FSEL_NEXT_Y,CHR_COL,WIND_COL,"  ≫  ");
    ep = EVT_sw(ep,3,FSEL_TIFF_X,FSEL_TIFF_Y,CHR_COL,WIND_COL,"☆");

    for ( i = n = 0 ; n < 18 && i < 26 ; i++ ) {
	if ( drv_tbl[i] != IS_NON ) {
	    ICON_disp(FSEL_DPOS_X1(n),FSEL_DPOS_Y1(n),i);
	    ep = EVT_set(ep,i+100,
			FSEL_DPOS_X1(n),FSEL_DPOS_Y1(n),
		        FSEL_DPOS_X2(n),FSEL_DPOS_Y2(n),EVT_proc);
	    drv_no[i] = n;
	    n++;
	} else
	    drv_no[i] = 0;
    }

    for ( i = 0 ; i < 15 ; i++ ) {
	ep = EVT_set(ep,i+200,
		FSEL_FPOS_X1(i),FSEL_FPOS_Y1(i),
	        FSEL_FPOS_X2(i),FSEL_FPOS_Y2(i),EVT_proc);
	DSP_wbox(FSEL_FPOS_X1(i),FSEL_FPOS_Y1(i),
	         FSEL_FPOS_X2(i),FSEL_FPOS_Y2(i),LINE_COL,WIND_COL,M_PSET);
    }

    DSP_wbox(FSEL_DIR_X-4,FSEL_DIR_Y-2,
	     FSEL_DIR_X+(44*8)+2,FSEL_DIR_Y+17,LINE_COL,WIND_COL,M_PSET);

    ep = EVT_set(ep, 5,
		FSEL_DIR_X-4, FSEL_DIR_Y-2,
		FSEL_DIR_X+(44*8)+2, FSEL_DIR_Y+17, EVT_proc);

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((FSEL_X1+FSEL_X2)/2,(FSEL_Y1+FSEL_Y2)/2);
    MOS_disp(ON);

    drv_now = getdrv();
    drv_old = ERR;
    dir_flg = TRUE;
    dirp = NULL;
    pos_old = ERR;
    pos_now = 0;

    for ( cd = FALSE ; cd == FALSE ; ) {

	if ( drv_old != drv_now ) {
	    MOS_disp(OFF);
	    if ( drv_old != ERR )
		DSP_box(FSEL_DPOS_X1(drv_no[drv_old]),
			FSEL_DPOS_Y1(drv_no[drv_old]),
			FSEL_DPOS_X2(drv_no[drv_old]),
			FSEL_DPOS_Y2(drv_no[drv_old]),
			11,M_XOR);

	    DSP_box(FSEL_DPOS_X1(drv_no[drv_now]),
		    FSEL_DPOS_Y1(drv_no[drv_now]),
		    FSEL_DPOS_X2(drv_no[drv_now]),
		    FSEL_DPOS_Y2(drv_no[drv_now]),
		    11,M_XOR);
	    MOS_disp(ON);

	    DSP_mos(2);
	    chdrv(drv_now);
	    DSP_mos(0);

	    drv_old = drv_now;
	    dir_flg = TRUE;
	}

	if ( dir_flg != FALSE ) {
	    DSP_mos(2);
	    getdir(tmp);

	    MOS_disp(OFF);
	    gprintf(FSEL_DIR_X,FSEL_DIR_Y,CHR_COL,WIND_COL,"%-44.44s",tmp);
	    MOS_disp(ON);

	    if ( tmp[1] != '\0' )
		strcat(tmp,"\\");
	    strcat(tmp,"*.*");
	    if ( dirp !=  NULL )
		closedir(dirp);
	    dirp = opendir(tmp);
	    dir_max = countdir(dirp);
	    pos_now = 0;
	    pos_old = ERR;
	    dir_flg = FALSE;
	    DSP_mos(0);
	}

	if ( pos_old != pos_now ) {
	    seekdir(dirp,pos_now);
	    MOS_disp(OFF);
	    for ( i = 0 ; i < 15 ; i++ ) {
		dp = readdir(dirp);
		gputs(FSEL_FPOS_X1(i)+4,FSEL_FPOS_Y1(i)+2,
			(IS_DIR(dp) ? KEY_COL:CHR_COL),WIND_COL,
			(dp != NULL ? 
				  formname(dp->d_name):"              "));
		file_ptr[i] = dp;
	    }
	    MOS_disp(ON);
	    pos_old = pos_now;
	}

	i = EVT_wait(ep);

	if ( i == 0 ) {
	    cd = ERR;

	} else if ( i == 1 ) {
	    if ( pos_now > 0 )
		pos_now -= 15;

	} else if ( i == 2 ) {
	    if ( (pos_now + 15) < dir_max )
	         pos_now += 15;

	} else if ( i == 3 ) {		/* TIFF catalog */
	    getdir(tmp);
	    TIFF_catalog(tmp);

	} else if ( i == 5 ) {
	    p = HIS_dir();
	    chdir(p);
	    if ( p[0] != '\0' && p[1] == ':' )
	        drv_now = toupper(p[0]) - 'A';
	    dir_flg = TRUE;

	} else if ( i >= 200 ) {
	    if ( (dp = file_ptr[i-200]) != NULL ) {
		if ( IS_DIR(dp) ) {
		    chdir(dp->d_name);
		    dir_flg = TRUE;
		} else {
		    strcpy(tmp,dp->d_name);
		    cd = TRUE;
		}
	    }

	} else if ( i >= 100 ) {
	    drv_now = i - 100;
	    dir_flg = TRUE;
	}
    }

    if ( dirp != NULL )
	closedir(dirp);

    EVT_free(ep);
    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(FSEL_X1,FSEL_Y1,FSEL_X2,FSEL_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return (cd == ERR ? NULL:tmp);
}

#define	HIS_X1		(18 * 8)
#define	HIS_X2		(HIS_X1 + 30 * 8 + 8)
#define	HIS_Y1		(240 - 6 * 20)
#define	HIS_Y2		(HIS_Y1 + 10 * 22 + 8)

#define	HIS_FPOS_X1(n)	(HIS_X1 + 4)
#define	HIS_FPOS_X2(n)	(HIS_X1 + 30 * 8 + 4)
#define	HIS_FPOS_Y1(n)	(HIS_Y1 + 4 + (n) * 22)
#define	HIS_FPOS_Y2(n)	(HIS_Y1 + 4 + (n) * 22 + 19)

static	char	*HIS_dir(void)
{
    int     n;
    int     sw, bx, by;
    BLOCK   *sp;
    EVENT   *ep = NULL;
    static char *his_tab[] = {
		"\\MS_DOS",
		"\\T_OS",
		"\\FB386",
		"\\WINDOWS",
		"\\GEAR",
		"\\GRAPHICS",
		"\\MOVIE",
		"\\MUSIC\\MIDI",
		"\\MUSIC\\NAIZOU",
		"\\CIRCLE",
	};

    MOS_disp(OFF);
    sp = DSP_push_vram(HIS_X1,HIS_Y1,HIS_X2,HIS_Y2);
    DSP_opbox(HIS_X1,HIS_Y1,HIS_X2,HIS_Y2);
    DSP_wbox(HIS_X1,HIS_Y1,HIS_X2,HIS_Y2,LINE_COL,FILD_COL,M_PSET);

    for ( n = 0 ; n < 10 ; n++ ) {
	ep = EVT_set(ep, n,
		HIS_FPOS_X1(n), HIS_FPOS_Y1(n),
	        HIS_FPOS_X2(n), HIS_FPOS_Y2(n), EVT_proc);
	DSP_wbox(HIS_FPOS_X1(n), HIS_FPOS_Y1(n),
	         HIS_FPOS_X2(n), HIS_FPOS_Y2(n), LINE_COL, WIND_COL, M_PSET);
	gputs(HIS_FPOS_X1(n) + 4, HIS_FPOS_Y1(n) + 2,
		CHR_COL, WIND_COL, his_tab[n]);
    }

    MOS_rdpos(&sw,&bx,&by);
    MOS_setpos((HIS_X1+HIS_X2)/2,(HIS_Y1+HIS_Y2)/2);
    MOS_disp(ON);

    n = EVT_wait(ep);

    EVT_free(ep);
    MOS_disp(OFF);
    DSP_pop_vram(sp);
    DSP_clbox(HIS_X1,HIS_Y1,HIS_X2,HIS_Y2);
    MOS_setpos(bx,by);
    MOS_disp(ON);

    return his_tab[n];
}
