/************************************************************

    FM-TOWNS Text Screen Editer Program

    1989.7.27   v1.00   Make By Ken

************************************************************/
#include    <stdio.h>
#include    <stdlib.h>
#include    <setjmp.h>
#include    <string.h>
#define MAIN
#include    "defs.h"

#ifdef	FMR70
#include    <signal.h>
#endif

#ifdef  TOWNS
#include    <egb.h>
#include    <snd.h>
#include    <fmcfrb.h>
#include    <kkstr2.h>
#define	unlink(f) remove(f)
#endif
/* ------------------ SIDEWORK 92.01.31 Pumpkin ------------- */
#include    <mos.h>
#include    <sidework.h>
/* ---------------------------------------------------------- */
/* ------------------ KYB init 92.03.20 Pumpkin ------------- */
#include    "int24.h"
/* ---------------------------------------------------------- */
/* ------------------ Debug  92.02.07 Pumpkin --------------- */
extern int  diskReady(int drive);
extern	int	retdir(char *name);
extern	int	chdir(char *name);
/* ---------------------------------------------------------- */
/* --------------- Color Pal   91.12.29 Pumpkin ------------- */
void pal_init(char *pal_file);
/* ---------------------------------------------------------- */
/* ------------------ SIDEWORK 92.01.31 Pumpkin ------------- */
char 	*mwork;
static	int vol,mute;
/* ---------------------------------------------------------- */

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
/* ------------------ OAK4 92.12.28 Pumpkin ----------------- */
#include    <dos.h>
int modeW = KAN_EISYOU | KAN_EISUU | KAN_HANKAKU;
/* ---------------------------------------------------------- */
/* ------------------ CD No Stop 91.10.18 Pumpkin ----------- */
#ifdef	TOWNS
extern 	int	cd_stop;
#endif
/* ---------------------------------------------------------- */

/* --------------------- EUP play 92.03.30 ------------------ */
#ifdef	TOWNS
char 	*swork;
#define BUFF_SIZE (66000)	/* customaized by YAMA */
extern int ML_Start( char * ,int , char * );
extern int ML_End();
extern char buff[];
#endif
/* ---------------------------------------------------------- */

extern void wrtstr();
extern void Dsp_ins_mode();
extern void Dsp_free();
extern void Ins_buf();
extern void Cut_buf();
extern int  Dsp_init();
extern int  Bfp_init();
extern void Set_vram();
extern void Dsp_vram();
extern BOOL edit();
extern int  Del_wind();
extern void Bfp_end();
extern void RSB_end();
extern void Dmy_form();
extern void Del_buf();
extern void Auto_log_init();
extern void CD_endof();

extern UCHAR vram[];
extern char  *SPCSTR;
extern char  *Auto_log_file;

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
/*******
extern short int modeW;
*******/
/* ---------------------------------------------------------- */

	char 	*work;
	jmp_buf mark;
	char	*file_name=NULL;
	char	*Tmp_device_name=NULL;

static BOOL	old_wrt_flg=ERR;
static char     tmp_name[160];
static int	Back_up = FALSE;
static int	Apend_Z = FALSE;

void	Start_Msg()
{
    static char *msg[]={
	"           WINK @ｰ            ",
	"   Multi Screen Text Editor   ",
	"    ＷＩＮＫ海賊版[ver1.24]   ",
	" Copyright (c) 1989-93 FTOWNS ",
	"    Created by K.Miyazaki     ",
	"       thanks to RED++        ",
	"    customaized by Pumpkin    ",
	NULL
    };
    int     i;
    char    tmp[160];

    Dmy_form(tmp,30,0x98,0x95,0x99); 
    wrtstr(tmp,20,9,0x07);
    Dmy_form(tmp,30,0x96,0x20,0x96); 
    for ( i = 0 ; msg[i] != NULL ; i++ ) {
        wrtstr(tmp,20,10+i,0x07);
        wrtstr(msg[i],21,10+i,i == 0 ? 0x14 : 0x07);
    }
    Dmy_form(tmp,30,0x9A,0x95,0x9B); 
    wrtstr(tmp,20,17,0x07);
    wrtstr("ＷＩＮＫ",25,12,0x16);
}

void	GetDspPos(int *x, int *y)
{
    *x = (OFF_X + Cur_X) * 8;
    *y = (OFF_Y + Cur_Y) * 16;
}

void	Tango_Touroku(char *str);

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
void	Gra_init(int kan_sw)
/*****
void	Gra_init()
******/
/* ---------------------------------------------------------- */
{
#ifdef	TOWNS
    KANJ    kana;
/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
    static union REGS	reg;
    int     col[16];
/* ---------------------------------------------------------- */

    work = (char *)malloc(EgbWorkSize);
    memset(work,0,EgbWorkSize);
/* --------------------- EUP play 92.03.30 ------------------ */
    swork = (char *)malloc(16384);		/* customaized by YAMA */
    memset(swork,0,16384);			/* customaized by YAMA */
/* ---------------------------------------------------------- */
    EGB_init(work,EgbWorkSize);
    EGB_resolution(work, 0, 3);
    EGB_resolution(work, 1, 3);
    EGB_displayPage(work, 1, 3);
    EGB_pen(work,0);
    EGB_penSize(work,1);
/* ------------------ SIDEWORK 92.02.14 Pumpkin ------------- */
    SND_elevol_read(1,&vol,&vol);
    SND_get_elevol_mute(&mute);
/* ---------------------------------------------------------- */
/* --------------------- EUP play 92.03.30 ------------------ */
    SND_init(swork);			/* customaized by YAMA */
/* ---------------------------------------------------------- */
/* ------------------ SIDEWORK 92.02.14 Pumpkin ------------- */
    SND_elevol_set(1,vol,vol);
    SND_elevol_mute(mute | 0x33);
/******
    SND_elevol_init();
    SND_elevol_mute(0x30);
    SND_elevol_set(1,64,64);
******/
/* ---------------------------------------------------------- */
/* --------------------- EUP play 92.03.30 ------------------ */
    ML_Start(buff,BUFF_SIZE,swork);		/* customaized by YAMA */
/* ---------------------------------------------------------- */

    kana.scrn   = KAN_SCRN16;   /*  画面ﾓｰﾄﾞは16色モード               */
    kana.egbw   = work;         /*  EGBのﾜｰｸｱﾄﾞﾚｽを設定する            */
    kana.wPage  = 0;            /*  表示ページを1にする                */
    kana.mosAp  = KAN_MOSON;    /*  マウスの制御をする                 */
    kana.gets   = Tango_Touroku;/*  文字取得ﾙｰﾁﾝの設定                 */
    kana.getPos = GetDspPos;    /*  未確定文字列表示位置取得関数の設定 */
    kana.maxX   = 639;          /*  画面の最大X座標                    */
    kana.maxY   = 479;          /*  画面の最大Y座標                    */

    KAN_prepare(&kana);

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
    /*  従来のＷＩＮＫライクなカラーテーブルの設定 */
    col[ 0] = 0x0f;
    col[ 1] = 0x01;
    col[ 2] = 0x02;
    col[ 3] = 0x03;
    col[ 4] = 0x04;
    col[ 5] = 0x05;
    col[ 6] = 0x06;
    col[ 7] = 0x00;
    col[ 8] = 0x08;
    col[ 9] = 0x09;
    col[10] = 0x0a;
    col[11] = 0x0b;
    col[12] = 0x04;
    col[13] = 0x06;
    col[14] = 0x03;
    col[15] = 0x01;
    KAN_setClrTbl( col);

/* ---------------------------------------------------------- */
    KAN_disp(KAN_DISPON);

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
/* ------------------ OAK4 92.12.28 Pumpkin ----------------- */
    /* ＯＡＫ入力モード設定 */

    if (kan_sw != FALSE) {
	KAN_setMode(modeW);
    }
/* ---------------------------------------------------------- */


#endif
}
void	Dsp_file_name()
{
    char    tmp[160];

    memset(tmp,' ',60); tmp[60] = '\0';
    wrtstr(tmp,0,1,0x1F);
    if ( file_name != NULL ) {
	sprintf(tmp," %-12s ",file_name);
	wrtstr(tmp,0,1,0x14);
    } else
	wrtstr("  [Not File]  ",1,1,0x1F);
}
void	Dsp_wrt_flg()
{
    if ( Wrt_flg != old_wrt_flg ) {
	if ( (old_wrt_flg = Wrt_flg) == FALSE )
	    wrtstr("表示",0,0,0x17);
	else
	    wrtstr("編集",0,0,0x17);
    }
}
void    Dsp_PFKEY()
{
    int     i;
    static char *menu[]={
	" MENU1"," MENU2"," MENU3"," MENU4","CD演奏",
	" 選択 "," 削除 "," 確定 "," 複写 "," 通信 " };

    for ( i = 0 ; i < 10 ; i++ )
        wrtstr(menu[i],i*7+(i/5),MENU_Y,0x1F);
}
void	Dsp_status()
{
    UCHAR   tmp[100];

    memset(tmp,' ',80); tmp[80] = '\0';
    wrtstr(tmp,0,0,0x1F);
    wrtstr(tmp,0,1,0x1F);
    Dsp_ins_mode();
    Dsp_file_name();
    Dsp_wrt_flg();
    Dsp_free();
    Dsp_PFKEY();
}
int     Buf_init(file)
char    *file;
{
    int	    n;
    FILE    *fp;
    UCHAR   tmp[512];

    top_ptr = lin_ptr = ent_ptr = btm_ptr = 0;
    if ( file != (char *)NULL && (fp = fopen(file,"rb")) != (FILE *)NULL ) {
	wrtstr("ファイルを読み込んでいます",30,1,0x15);
	while ( (n = fread(tmp,1,512,fp)) > 0 ) {
	    Ins_buf(btm_ptr,n,tmp);
	    btm_ptr += n;
	}
	if ( btm_ptr > 0 && *Cnv_ptr(btm_ptr-1) == '\x1A' )
	    Del_buf(--btm_ptr,1L);
	file_name = file;
	fclose(fp);
	wrtstr(SPCSTR,30,1,0x1F);
    } else
	file_name = file;

    chk_ptr = cke_ptr = ERR;
    Wrt_flg = FALSE;
    old_wrt_flg = TRUE;

    return FALSE;
}
int	save(file)
char	*file;
{
    FILE    *fp;
    UCHAR   tmp[256];
    int	    n;
    LONG    sp,sz;
    char    *p;

    if ( file != NULL ) {
	file_name = file;
	Dsp_file_name();
    }
    if ( file_name == NULL )
	return ERR;

    if ( Back_up != FALSE ) {
	strcpy((char *)tmp,file_name);
	if ( (p = strrchr((char *)tmp,'.')) == NULL )
	    for ( p = (char *)tmp ; *p != '\0' ; p++ );
	strcpy(p,".BAK");
	unlink((char *)tmp);
	rename(file_name,(char *)tmp);
    }

    if ( (fp = fopen(file_name,"wb")) == NULL )
	return ERR;

    for ( sp = 0,sz = btm_ptr ; sz > 0 ; ) {
	if ( sz < 256 ) n = sz; else n = 256;
        Cut_buf(tmp,n,sp);
/* ------------------ bug fix  93.02.18 Pumpkin ---------------- */
	if (fwrite(tmp,1,n,fp) == 0) break;
/* -------------------------------------------------------------- */
/**********
	fwrite(tmp,1,n,fp);
**********/
	sp += n;
	sz -= n;
    }

    if ( Apend_Z == FALSE ) {
	tmp[0] = 0x1A;
	fwrite(tmp,1,1,fp);
    }

    if ( ferror(fp) ) {
	fclose(fp);
	return ERR;
    }
    fclose(fp);
    return FALSE;
}
void	Set_file_name(file)
char	*file;
{
    strcpy(tmp_name,file);
    file_name = tmp_name;
    Dsp_file_name();
}
void	Set2_file_name(file)
char	*file;
{
    strcpy(tmp_name,file);
    file_name = tmp_name;
}
void	main(argc,argv)
int	argc;
char	*argv[];
{
    int     i;
    char    tmp[40];
    char    *p;
    char    *file=NULL;

    CONTRL = 0;
    TAB = 8;
/* ------------------ Debug  92.02.07 Pumpkin --------------- */
    char	firstdir[128];
/* ---------------------------------------------------------- */
/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
    int kan_mode = FALSE;
/* ---------------------------------------------------------- */
/* --------------- Color Pal   91.08.28 Pumpkin ------------- */
    char    *pal_file = NULL;
/* ---------------------------------------------------------- */
/* ------------------ KYB init 92.03.20 Pumpkin ------------- */
    int	 mojip[22][2];
    char moji[22][20];

    set24h();
/* ---------------------------------------------------------- */
/* ------------------ Debug  92.02.07 Pumpkin --------------- */
    retdir(firstdir);
/* ---------------------------------------------------------- */

#ifdef	FMR70
    signal(SIGINT,SIG_IGN);
#endif

    while ( --argc > 0 ) {
	p = *(++argv);
	if ( *p == '-' || *p == '/' ) {
	    switch(*(++p)) {
		case 'A': case 'a':
		    Auto_log_file = ++p;
		    break;
		case 'B': case 'b':
		    Back_up = TRUE;
		    break;
		case 'Z': case 'z':
		    Apend_Z = TRUE;
		    break;
/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
		case 'R': case 'r':
    		    kan_mode = TRUE;
    		    modeW = modeW & ~0x30l;
    		    modeW = modeW | KAN_ROUMAZI;
		    break;
		case 'F': case 'f':
    		    kan_mode = TRUE;
    		    modeW = modeW & ~0xc0l;
    		    modeW = modeW | KAN_ZENKAKU;
		    break;
		case 'K': case 'k':
    		    kan_mode = TRUE;
    		    modeW = modeW & ~0x700l;
    		    modeW = modeW | KAN_KATAKANA;
		    break;
		case 'H': case 'h':
    		    kan_mode = TRUE;
    		    modeW = modeW & ~0x700l;
    		    modeW = modeW | KAN_HIRAGANA;
		    break;
		case 'C': case 'c':
    		    kan_mode = TRUE;
    		    modeW = modeW & ~0x20000l;
    		    modeW = modeW | KAN_EIDAI;
		    break;
/* ---------------------------------------------------------- */
/**************************************************
		case 'R': case 'r':
		    modeW = (modeW & 0xFFF3) | 0x0008;
		    break;
		case 'F': case 'f':
		    modeW = (modeW & 0xFFCF) | 0x0020;
		    break;
		case 'K': case 'k':
		    modeW = (modeW & 0xFE3F) | 0x0080;
		    break;
		case 'H': case 'h':
		    modeW = (modeW & 0xFE3F) | 0x0100;
		    break;
**************************************************/
		case 'T': case 't':
		    Tmp_device_name = ++p;
		    break;
		case 'D': case 'd':
		    TAB = atoi(++p);
		    break;
/* --------------- Color Pal   91.08.28 Pumpkin ------------- */
#ifdef	TOWNS
		case 'P': case 'p':
		    pal_file = ++p;
		    break;
#endif
/* ---------------------------------------------------------- */
/* ------------------ CD No Stop 91.10.18 Pumpkin ----------- */
#ifdef	TOWNS
		case 'N': case 'n':
		    cd_stop = FALSE;
		    break;
#endif

/* ---------------------------------------------------------- */


	    }
	} else
	    file = p;
    }

/* ------------------ OAK4 91.12.28 Pumpkin ----------------- */
    Gra_init(kan_mode);
/*******
    Gra_init();
*******/
/* ---------------------------------------------------------- */
/* --------------- Color Pal   91.08.28 Pumpkin ------------- */
#ifdef	TOWNS
    pal_init(pal_file);
#endif
/* ---------------------------------------------------------- */
/* --------------- RSB for L20 92.12.27 Pumpkin ------------- */
    RSB_start();
/*******
    RSB_Init();
*******/
    Dsp_init();
/* ------------------ KYB init 92.03.20 Pumpkin ------------- */
    for (i=1;i<21;i++) 
        KYB_rdasign(&mojip[i][1],0x8000+i,&mojip[i][0],&moji[i][0]);
    KYB_rdasign(&mojip[21][1],0x7f,&mojip[21][0],&moji[21][0]);
    KYB_asign(0,0x7f,1,"\x7f");
    KYB_setbuf(1);
/********
    KYB_clic(1);
********/
/* ---------------------------------------------------------- */
    Auto_log_init();
    Dsp_status();
    Start_Msg();

    if ( Bfp_init() != FALSE ) {
	wrtstr("メモリ管理の初期化に失敗しました",30,1,0x12);
	exit(0);
    }
    if ( Buf_init(file) != FALSE ) {
	wrtstr("ファイルの初期化に失敗しました",30,1,0x12);
	exit(0);
    }

    Dsp_status();
    Set_vram();
    Dsp_vram(vram);

    if ( (i = setjmp(mark)) != 0 ) {
	sprintf(tmp,"致命的なエラ−が発生しました #%d",i);
	wrtstr(tmp,30,1,0x12);
    }

/* ------------------ SIDEWORK 92.01.31 Pumpkin ------------- */
    mwork = (char *)malloc(4096);
    MOS_start(mwork,4096);
    sdk_control(0);	/* ホットキーによるサイドワーク起動の禁止 */
/* ---------------------------------------------------------- */

    do {
        while ( edit() == FALSE );
    } while ( Del_wind() == FALSE );

/* --------------------- EUP play 92.03.30 ------------------ */
    ML_End();				/* customaized by YAMA */
/* ---------------------------------------------------------- */
    Bfp_end();
    RSB_end();
/* ------------------ KYB init 92.03.20 Pumpkin ------------- */
    KYB_init();
    for (i=1;i<21;i++) 
        KYB_asign(mojip[i][1],0x8000+i,mojip[i][0],&moji[i][1]);
    KYB_asign(mojip[21][1],0x7f,mojip[21][0],&moji[21][1]);
/* ---------------------------------------------------------- */
    KYB_clic(1);

#ifdef	TOWNS
    CD_endof();
/* ------------------ SIDEWORK 92.01.31 Pumpkin ------------- */
    MOS_end();
    free(mwork);
/* ---------------------------------------------------------- */
/* --------------------- EUP play 92.03.30 ------------------ */
    SND_end();				/* customaized by YAMA */
/* ---------------------------------------------------------- */
    KAN_disp(KAN_DISPOFF);
    KAN_finish();
    RSB_End();
/* --------------- Color Pal   91.12.29 Pumpkin ------------- */
    EGB_init(work,EgbWorkSize);
/* ---------------------------------------------------------- */
#endif
/* ------------------ Debug  92.02.07 Pumpkin --------------- */
    if (diskReady(firstdir[0] - 'A' + 1) == FALSE) 
        chdir(firstdir);
    reset24h();
/* ---------------------------------------------------------- */

#ifdef	FMR70
    cprintf("\x1B[2J");
#endif
}

#ifdef	FMR70
#include    <dos.h>

int     KYB_clic(sw);
int	sw;
{
    union REGS	regs;

    regs.h.ah = 0x05;
    regs.h.al = sw;
    int86(0x90,&regs,&regs);
    return;
}
int	kbhit()
{
    union REGS	regs;

    regs.h.ah = 0x07;
    int86(0x90,&regs,&regs);
    return (int)regs.h.al;
}
int	KYB_read(sw,ec)
int	sw;
unsigned *ec;
{
    union REGS	regs;

    regs.h.ah = 0x09;
    regs.h.al = sw;
    int86(0x90,&regs,&regs);
    *ec = regs.x.bx;
    if ( *ec >= 0x5D00 && *ec <= 0x6600 ) {
        regs.h.ah = 0x09;
        regs.h.al = sw;
        int86(0x90,&regs,&regs);
        *ec = regs.x.bx;
    }
    return regs.x.dx;
}
#endif

/* --------------- Color Pal   91.08.28 Pumpkin ------------- */
/* --------------- Color Pal   91.12.29 Pumpkin ------------- */
#ifdef TOWNS
void pal_init(char *pal_file)
{
    int	    n=0,r=0,g=0,b=0,i,num;
    FILE *pfp;

    struct _pal_body {
		long	num;
		char	dat[8*16];
    } pal_body;


    if (pal_file == NULL) return;
    EGB_getPalette(0,(char *)&pal_body);

    if ( (pfp = fopen(pal_file,"r")) != NULL ) {
	if (fscanf( pfp, "%d",&num ) == 1 &&  num>0 && num<17 ) {
		for(i=0; i<num; i++) {
    			fscanf( pfp, "%d %d %d %d",&n,&b,&r,&g ); 
			if ( n<16 ) {
			pal_body.dat[n*8  ] = (long)n;
			pal_body.dat[n*8+4] = (char)b*16;
			pal_body.dat[n*8+5] = (char)r*16;
			pal_body.dat[n*8+6] = (char)g*16;
			}
	        }
        }
	fclose( pfp );
    }
    EGB_writePage(work, 0);
    EGB_palette(work,1,(char *)&pal_body);

}

#endif
/* ---------------------------------------------------------- */
