/******************************
 *	Consol Low I/O        *
 ******************************/
#include    <stdio.h>
#include    <stdlib.h>
#include    <stdarg.h>
#include	<string.h>
#include    <ctype.h>
#include    <egb.h>
#include    <fmc.h>
#include    "oaklib.h"
#include	"dabe.h"

extern char work[];

#define	PIX_BYTE 1	/* 画面モ−ドにより変化 16色=1 256色=1 32K色=2 */

#define	TOP_X	wp->top_x
#define	TOP_Y	wp->top_y
#define	BTM_X	wp->btm_x
#define	BTM_Y	wp->btm_y
#define	SIZ_X	wp->siz_x
#define	SIZ_Y	wp->siz_y
#define	CUR_X	wp->cur_x
#define	CUR_Y	wp->cur_y
#define	CHR_COL	wp->chr_col
#define	CHR_BAK	wp->chr_bak
#define	CUR_COL	wp->cur_col
#define	BAK_COL	wp->bak_col
#define	FNT_X	wp->fnt_x
#define	FNT_Y	wp->fnt_y
#define	FNT_STL	wp->fnt_stl
#define	KAN_COD	wp->kan_cod
#define	DSP_X	wp->dsp.x
#define	DSP_Y	wp->dsp.y
#define	DSP_LEN	wp->dsp.len
#define	DSP_BUF	wp->dsp.buf

typedef struct _WP {
	struct _WP *next;
	short	top_x,top_y;
	short	btm_x,btm_y;
	short	siz_x,siz_y;
	short	cur_x,cur_y;
	short	chr_col,chr_bak;
	short	cur_col,bak_col;
	short	fnt_x,fnt_y,fnt_stl;
	short	kan_cod;
	struct {
	    short	x,y;
	    short	len;
	    char	buf[256];
	} dsp;
} WIND;

WIND	*act_wind=NULL;

static int      bch=0;
static unsigned bec=0;

extern	short	INS_STAT;		
static	char	charmap[40][100];
static	short	charmap2[40][100];
static	short	LOC_Y;
static	short	MAX_X[40];
static	short	MAX_Y;


void	init_charmap(void)
{
    register WIND *wp=act_wind;
	short	i;
	char	*pcharmap;
	
	for( i = 0; i < 40; i++ ){
		pcharmap = &charmap[i][0];
		*pcharmap = 0x00;
		MAX_X[i] = (-1);
	}	 
	KAN_COD = 0;
}

short	kbhit(void)
{
    if ( (bch != 0xFFFF) || ( (bch = KAN_read(1,&bec) ) != 0xFFFF) )
    	return 1;
    else
        return 0;
}

short	getch(void)
{
    short   ch;
    short	ret;
	    
    while ( (ret = kbhit()) == 0 && put_oakcur() == 0 );
    if( ret == 0 ) 
    	return	ESC;
    else{	
    	ch = bch; bch = 0xFFFF;
    	return ch;
	}
}


void	cur_dsp(sw)
short	sw;
{
    register WIND *wp=act_wind;
    char    para[8];

	EGB_writePage( work, 0x40 );
    EGB_paintMode(work,0x02);
    EGB_color(work,0,sw == 0 ? CUR_COL:CHR_BAK);
    WORD(para+0) = DSP_X;
    WORD(para+2) = DSP_Y + 1;
    WORD(para+4) = DSP_X + FNT_X - 1;
    WORD(para+6) = DSP_Y + 1;
    EGB_rectangle(work,para);
    EGB_paintMode(work,0x22);
}

void	chr_bak(void)
{
    register WIND *wp=act_wind;
    char    para[8];

    if ( DSP_LEN <= 0 )
	return;
    EGB_paintMode(work,0x22);
    EGB_color(work,0,CHR_BAK);
    EGB_color(work,2,CHR_BAK);
    WORD(para+0) = DSP_X;
    WORD(para+2) = DSP_Y;
    WORD(para+4) = DSP_X + FNT_X * DSP_LEN + ((FNT_STL & 2) ? 2:-1);
    WORD(para+6) = DSP_Y - (FNT_Y - 2);
    EGB_rectangle(work,para);
}

void	cflush(void)
{
    register WIND *wp=act_wind;

    cur_dsp(1);
    chr_bak();
    EGB_color(work,0,CHR_COL);
    EGB_textSpace(work,FNT_X - 8);
    EGB_fontStyle(work,FNT_STL);
    EGB_sjisString(work,(char *)&(wp->dsp));
    DSP_X = CUR_X * FNT_X + TOP_X;
    DSP_Y = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2);
    DSP_LEN = 0;
    cur_dsp(0);
}


WIND	*wind(short x1,short y1,short x2,short y2,
	      short col,short bak,short cur,
	      short fx,short fy,short stl)
{
    register WIND *wp;
    char    para[8];

    if ( (wp = (WIND *)malloc(sizeof(WIND))) == NULL )
	return NULL;

    wp->next = act_wind;
    act_wind = wp;

	EGB_writePage( work, 0x40 );
    EGB_paintMode(work,0x22);
    EGB_color(work,0,col);
    EGB_color(work,2,bak);
    WORD(para+0) = x1;
    WORD(para+2) = y1;
    WORD(para+4) = x2;
    WORD(para+6) = y2;
    EGB_rectangle(work,para);

    x1 += 2; y1 += 2; x2 -= 2; y2 -= 2;

    TOP_X = x1;
    TOP_Y = y1;
    BTM_X = x2;
    BTM_Y = y2;
    FNT_X = fx;
    FNT_Y = fy;
    FNT_STL = stl;
    SIZ_X = (x2 - x1 + ((stl & 2) ? -2:1)) / FNT_X;
    SIZ_Y = (y2 - y1 + 1) / FNT_Y;
    CUR_X = 0;    CUR_Y = 0;	LOC_Y = 0;	MAX_Y = 0;
    CHR_COL = col;
    CHR_BAK = BAK_COL = bak;
    CUR_COL = cur;
    KAN_COD = 0;
    DSP_X = TOP_X;
    DSP_Y = TOP_Y + (FNT_Y - 2);
    DSP_LEN = 0;
    cur_dsp(0);

    return wp;
}

void	locate(short x,short y)
{
    register WIND *wp=act_wind;

    if ( (CUR_X = x) >= SIZ_X )
		CUR_X = SIZ_X - 1;
    if ( (CUR_Y = y) >= SIZ_Y )
		CUR_Y = SIZ_Y - 1;
    cflush();
}

void	color(short col)
{
    register WIND *wp=act_wind;

    CHR_COL = col;
}

void	roll_up()
{
    register WIND *wp=act_wind;
	char	p[120];
    
	cflush();
   
	cur_dsp(1);
	
	WORD(p + 0) = TOP_X;
	WORD(p + 2) = TOP_Y;
	WORD(p + 4) = BTM_X;
	WORD(p + 6) = ( SIZ_Y - 1 ) * FNT_Y + TOP_Y + (FNT_Y - 2);
    EGB_color( work, 1, BAK_COL);
    EGB_partScroll( work, 1, 0, FNT_Y, p );
    
	EGB_color(work, 0, CHR_COL);
   	WORD( p + 0 ) = TOP_X;
	WORD( p + 2 ) = TOP_Y + (FNT_Y - 2);
	WORD( p + 4 ) = MAX_X[LOC_Y] + 1;
	strncpy( ( p + 6 ), &charmap[LOC_Y][0], MAX_X[LOC_Y] + 1);
    EGB_sjisString( work, p );
    
    cur_dsp(0);
}

void	roll_down()
{
    register WIND *wp=act_wind;
	char	p[120];
    
	cflush();
	
    cur_dsp(1);
	
	WORD(p + 0) = TOP_X;
	WORD(p + 2) = TOP_Y;
	WORD(p + 4) = BTM_X;
	WORD(p + 6) = ( SIZ_Y - 1 ) * FNT_Y + TOP_Y + (FNT_Y - 2);
    EGB_color( work, 1, BAK_COL);
    EGB_partScroll( work, 1, 0, -FNT_Y, p );
    
	EGB_color(work, 0, CHR_COL);
	WORD( p + 0 ) = TOP_X;
	WORD( p + 2 ) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2);
	WORD( p + 4 ) = MAX_X[LOC_Y] + 1;
	strncpy( ( p + 6 ), &charmap[LOC_Y][0], MAX_X[LOC_Y] + 1);
	EGB_sjisString( work, p );
    
    cur_dsp(0);
}

void	roll_left( short	byte )
{	
    register WIND *wp=act_wind;
    char	para[8];
	
   	cur_dsp(1);
	EGB_color(work, 1, BAK_COL);
	
	WORD(para + 0) = CUR_X * FNT_X + TOP_X;
	WORD(para + 2) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
	WORD(para + 4) = BTM_X;
	WORD(para + 6) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2);
	EGB_partScroll(work, 1, -FNT_X * byte, 0, para);
   	
   	cur_dsp(0);
}

void	roll_left2( short byte, short linecount )
{	
    register WIND *wp=act_wind;
    char	para[8];
	
   	cur_dsp(1);
	EGB_color(work, 1, BAK_COL);
	
	WORD(para + 0) = TOP_X;
	WORD(para + 2) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
	WORD(para + 4) = BTM_X;
	WORD(para + 6) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2);
	EGB_partScroll(work, 1, -FNT_X * byte, 0, para);
   	
   	cur_dsp(0);
}

void 	roll_right()
{
    register WIND *wp=act_wind;
    char	para[8];
	
   	cur_dsp(1);
	
	WORD(para + 0) = CUR_X * FNT_X + TOP_X;
	WORD(para + 2) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
	WORD(para + 4) = (MAX_X[LOC_Y] + 1) * FNT_X + TOP_X;
	WORD(para + 6) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2);
	EGB_color(work, 1, BAK_COL);
	EGB_partScroll(work, 1, FNT_X, 0, para);
   	
	if( (MAX_X[LOC_Y] + 1) < BTM_X ){
		WORD(para + 0) = (MAX_X[LOC_Y] + 1) * FNT_X + TOP_X;
		WORD(para + 2) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
		WORD(para + 4) = BTM_X;
		WORD(para + 6) = CUR_Y * FNT_Y + TOP_Y + (FNT_Y - 2);
   		EGB_paintMode(work,0x22);
	    EGB_color(work, 0, BAK_COL);
		EGB_color(work, 2, BAK_COL);
		EGB_rectangle(work, para);
    }
    	
   	cur_dsp(0);
}

void 	roll_right2( short  linecount, short	byte )
{
    register WIND *wp=act_wind;
    char	para[8];
	
   	cur_dsp(1);
	
	WORD(para + 0) = TOP_X;
	WORD(para + 2) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
	WORD(para + 4) = (MAX_X[LOC_Y + linecount] + 1) * FNT_X + TOP_X;
	WORD(para + 6) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2);
	EGB_color(work, 1, BAK_COL);
	EGB_partScroll(work, 1, (FNT_X * byte), 0, para);
   	
	if( (MAX_X[LOC_Y + linecount] + 1) < BTM_X ){
		WORD(para + 0) = (MAX_X[LOC_Y + linecount] + 1) * FNT_X + TOP_X;
		WORD(para + 2) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2) - 16;
		WORD(para + 4) = BTM_X;
		WORD(para + 6) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2);
	    EGB_paintMode(work,0x22);
    	EGB_color(work, 0, BAK_COL);
		EGB_color(work, 2, BAK_COL);
		EGB_rectangle(work, para);
	}
   	cur_dsp(0);
}

short	kanji_twoback()
{
   	register WIND *wp=act_wind;
   	
   	if( charmap2[LOC_Y][CUR_X - 2] == 1 )
		return	1;
	else
		return	0;		
}

short	kanji_here()
{
   	register WIND *wp=act_wind;
   	
   	if( charmap2[LOC_Y][CUR_X] == 1 )
		return	1;
	else
		return	0;		
}

short	kanji_move( short	linecount )
{
   	register WIND *wp=act_wind;
   	
   	if( charmap2[LOC_Y + linecount][0] == 1 )
		return	1;
	else
		return	0;		
}

short	cur_left(void)
{
    register WIND *wp=act_wind;
	
	if( ( LOC_Y > 0 ) || ( CUR_X > 0 ) ){
		if( CUR_X <= 0 ){
	    	if( --LOC_Y < 0 )
	    		LOC_Y = 0;
    		else {	
    			CUR_X = strlen( &charmap[LOC_Y][0] );
		    	if( kanji_twoback() )
    				CUR_X -= 2;
    			else
    				CUR_X--;	
				if ( --CUR_Y < 0 ) {
				    CUR_Y = 0;
			    	roll_up();
				}
		    }
		} else {
		    if( kanji_twoback() )
    			CUR_X--;
			CUR_X--;
		}
		return	1;
	} else
		return 	0;
}

void	cur_right(void)
{
    register WIND *wp=act_wind;
    
    if( ( MAX_X[LOC_Y] >= CUR_X ) || ( MAX_Y > LOC_Y ) ){
    	if( kanji_here() ){
	   		if( ( CUR_X == ( MAX_X[LOC_Y] - 1 ) ) && ( MAX_Y > LOC_Y ) ){
    			CUR_X = 0;	
				if( ++LOC_Y > MAX_Y )
					MAX_Y = LOC_Y;
				if( ++CUR_Y >= SIZ_Y ) {
	   				CUR_Y = SIZ_Y - 1;
	   				roll_down();
				} 
    		} else
    			CUR_X += 2;
	    } else {
		   	if( ( CUR_X == MAX_X[LOC_Y] ) && ( MAX_Y > LOC_Y ) ){
    			CUR_X = 0;
				if( ++LOC_Y > MAX_Y )
					MAX_Y = LOC_Y;
				if( ++CUR_Y >= SIZ_Y ) {
		   			CUR_Y = SIZ_Y - 1;
	   				roll_down();
				} 
 		   	} else
    			CUR_X++;
	    }			
	}
}

void	cur_up()
{
    register WIND *wp=act_wind;
	short	*pcharmap2;
	
	if( LOC_Y > 0 ){
		LOC_Y--;
		if( CUR_X > MAX_X[LOC_Y] )
  			CUR_X = MAX_X[LOC_Y];
  		pcharmap2 = &charmap2[LOC_Y][CUR_X];
		if( *pcharmap2 == 2 )
			CUR_X--;
		if ( --CUR_Y < 0 ) {
		    CUR_Y = 0;
	    	roll_up();
		}
	}
}

void	cur_down()
{
    register WIND *wp=act_wind;
	short	*pcharmap2;
	 
	if( LOC_Y < MAX_Y ){
		LOC_Y++;
		if( CUR_X > MAX_X[LOC_Y] ){
  			if( MAX_X[LOC_Y] < 0 )
  				CUR_X = 0;
  			else
  				CUR_X = MAX_X[LOC_Y];
  		}
  		pcharmap2 = &charmap2[LOC_Y][CUR_X];
		if( *pcharmap2 == 2 )
			CUR_X--;
		if ( ++CUR_Y >= SIZ_Y ) {
		    CUR_Y = SIZ_Y - 1;
	    	roll_down();
		}
	}
	 
}


void	smemcpy( short *mem1, short *mem2, short byte )
{
	short	i;
	
	for( i = 1; i <= byte; i++ ){
		*mem1 = *mem2;
		mem1++;
		mem2++;
	}	
}

void	cmemcpy( char *mem1, char *mem2, short byte )
{
	short	i;
	
	for( i = 1; i <= byte; i++ ){
		*mem1 = *mem2;
		mem1++;
		mem2++;
	}	
}

void	cmemset( char *mem, char chr, short byte )
{
	short	i;
	
	for( i = 1; i <= byte; i++ ){
		*mem = chr;
		mem++;
	}	
}
 
void	move_left( short byte, short linecount )
{
   	register WIND *wp=act_wind;
   	char	p[8];
   	
   	if( ( CUR_Y + linecount ) <= ( SIZ_Y - 1 ) ) 
	   	roll_left2( byte, linecount );
    
   	if( ( CUR_Y + linecount - 1 ) <= ( SIZ_Y - 1 ) ){ 
   		WORD( p + 0 ) = ( MAX_X[LOC_Y + linecount - 1] + 1 ) * FNT_X + TOP_X;
    	WORD( p + 2 ) = (CUR_Y + linecount - 1 ) * FNT_Y + TOP_Y + (FNT_Y - 2);
   		WORD( p + 4 ) = byte;
    	strncpy( ( p + 6 ), &charmap[LOC_Y + linecount][0], byte );
    	EGB_sjisString( work, p );
    }
  		
  	strncat( &charmap[LOC_Y + linecount - 1][0], &charmap[LOC_Y + linecount][0], byte);
  	smemcpy( &charmap2[LOC_Y + linecount - 1][ MAX_X[LOC_Y + linecount - 1] + 1 ], &charmap2[LOC_Y + linecount][0], byte);
	MAX_X[LOC_Y + linecount - 1] = strlen( &charmap[LOC_Y + linecount - 1][0] ) - 1; 
	
	strcpy( &charmap[LOC_Y + linecount][0], &charmap[LOC_Y + linecount][byte] );
	smemcpy( &charmap2[LOC_Y + linecount][0], &charmap2[LOC_Y + linecount][byte], 80 - byte); 
	MAX_X[LOC_Y + linecount] = strlen( &charmap[LOC_Y + linecount][0] ) - 1; 
	
}

void	del(void)
{
    register WIND *wp=act_wind;
   	short		byte;
   	short		linecount;
   	short		freelen;
   		
   	if( kanji_here() )
   		byte = 2;
   	else
   		byte = 1;	
   	strcpy( &charmap[LOC_Y][CUR_X], &charmap[LOC_Y][CUR_X + byte] );	
  	smemcpy( &charmap2[LOC_Y][CUR_X], &charmap2[LOC_Y][CUR_X + byte], 80 - ( CUR_X + byte ) );
	MAX_X[LOC_Y] = strlen( &charmap[LOC_Y][0] ) - 1; 
	roll_left( byte );
   		
   	linecount = 1;
   	while( ( LOC_Y + linecount ) <= MAX_Y ){ 
		if( MAX_X[LOC_Y + linecount] >= 0 ){
			freelen = (SIZ_X - 1) - strlen( &charmap[ LOC_Y + linecount - 1 ][0] ) + 1 ;
			switch( freelen ){
				case 3:	if( kanji_move( linecount ) ){
							move_left( 2, linecount );
							if( kanji_move( linecount ) == 0 )
								move_left( 1, linecount );
						} else {
							move_left( 1, linecount );
							if( kanji_move( linecount ) )
								move_left( 2, linecount );
							else	
								move_left( 1, linecount );
						}	
						break;			
				case 2:	if( kanji_move( linecount ) )
							move_left( 2, linecount );
						else {
							move_left( 1, linecount );
							if( kanji_move( linecount ) == 0 )
								move_left( 1, linecount );
						}	
						break;
				case 1:	if( kanji_move( linecount ) == 0 )
							move_left( 1, linecount );
						break;			
				default:
					break;
			}
			linecount++;
		} else
			MAX_Y--;
	}
    
}

void	move_putchr( short linecount, short insbyte )
{
   	register WIND *wp=act_wind;
   	char	p[12];
   	
	if( linecount == 0 )
		roll_right();	
	else {	
		roll_right2( linecount, insbyte );
   		WORD( p + 0 ) = TOP_X;
	    WORD( p + 2 ) = (CUR_Y + linecount) * FNT_Y + TOP_Y + (FNT_Y - 2);
   		WORD( p + 4 ) = insbyte;
	    strncpy( ( p + 6 ), &charmap[LOC_Y + linecount][0], insbyte);
		EGB_color(work, 0, CHR_COL);
    	EGB_sjisString( work, p );
	}
}

void	move_right( short movebyte, short linecount )
{
   	register WIND *wp=act_wind;
   	char	*pcharmap;	
   	
	MAX_X[LOC_Y + linecount] = MAX_X[LOC_Y + linecount] - movebyte; 
	_rstrcpy( &charmap[LOC_Y + linecount + 1][movebyte], &charmap[LOC_Y + linecount + 1][0] );
  	pcharmap = &charmap[LOC_Y + linecount][ MAX_X[LOC_Y + linecount] + 1];
  	cmemcpy( &charmap[LOC_Y + linecount + 1][0], pcharmap, movebyte); 
  	*pcharmap = (char)0x00;
	_rmemcpy( &charmap2[LOC_Y + linecount + 1][movebyte], &charmap2[LOC_Y + linecount + 1][0], (80 - movebyte) * 2); 
  	smemcpy( &charmap2[LOC_Y + linecount + 1][0], &charmap2[LOC_Y + linecount][ MAX_X[LOC_Y + linecount] + 1], movebyte); 
   	
}

void	ins()
{
    register WIND *wp=act_wind;
	short	insbyte;
	short	movebyte;
	short	*pcharmap2;
	short	linecount;
	
	insbyte = 1;
    
	_rstrcpy( &charmap[LOC_Y][CUR_X + insbyte], &charmap[LOC_Y][CUR_X] );
	cmemset( &charmap[LOC_Y][CUR_X], SPACE, insbyte);
	_rmemcpy( &charmap2[LOC_Y][CUR_X + insbyte], &charmap2[LOC_Y][CUR_X], (80 - (CUR_X + insbyte)) * 2 ); 
	
	linecount = 0;
	while( ((LOC_Y	+ linecount) < 40) && (insbyte != 0) ){
		MAX_X[LOC_Y + linecount] = MAX_X[LOC_Y + linecount] + insbyte; 
		movebyte = 0;
		while( ( MAX_X[LOC_Y + linecount] - movebyte ) > ( SIZ_X - 1 ) ){
			pcharmap2 = &charmap2[LOC_Y + linecount][ MAX_X[LOC_Y + linecount] - movebyte ];
			if( *pcharmap2 == 0 )
				movebyte += 1;
			else
				movebyte += 2;	
		}
		if( movebyte > 0 ){
			move_right( movebyte, linecount );
			MAX_Y = LOC_Y + linecount + 1;
		}
		if( (CUR_Y + linecount) <= (SIZ_Y - 1) )
			move_putchr( linecount, insbyte );
		insbyte = movebyte;
		linecount++;
	}	
}

void	chr_out(char ch)
{
    register WIND *wp=act_wind;
	char	*pch = &ch;
	char	*pcharmap = &charmap[LOC_Y][CUR_X];
	short	*pcharmap2 = &charmap2[LOC_Y][CUR_X];
    
    if( ( INS_STAT == ON ) && ( ( KAN_COD <= 0 ) || ( ch != SPACE ) ) )
		ins();
    
    DSP_BUF[DSP_LEN++] = ch;
    
    if( (KAN_COD == 0) || (ch != SPACE) ){  	
    	*pcharmap = *pch;
    	pcharmap++;
    	if( (LOC_Y == MAX_Y) && (CUR_X >= MAX_X[LOC_Y]) )
    		*pcharmap = (char)0x00;
    	if( KAN_COD != 0 ){
			if( KAN_COD > 0 )
				*pcharmap2 = 0x01;
			else				    		
				*pcharmap2 = 0x02;
		} else
			*pcharmap2 = 0x00;
   		if( CUR_X > MAX_X[LOC_Y] )
   			MAX_X[LOC_Y] = CUR_X;
    }
    
    if ( ++CUR_X >= SIZ_X ) {
		CUR_X = 0;
		if( ++LOC_Y > MAX_Y )
			MAX_Y = LOC_Y;
		if ( ++CUR_Y >= SIZ_Y ) {
		    CUR_Y = SIZ_Y - 1;
	    	roll_down();
		} else
		    cflush();
    } 
}

void	putch(char ch)
{
    register WIND *wp=act_wind;
	FILE	*fp;
	short	i, j;
	char	para[12];
	
	EGB_writePage( work, 0x40 );
    
    if ( KAN_COD != 0 ) {
		if ( iskanji2(ch) ) {
	    	if ( CUR_X == (SIZ_X - 1) )
				chr_out(' ');
	    	chr_out(KAN_COD);
	    	KAN_COD *= (-1);
	    	chr_out(ch);
	    	KAN_COD = 0;
	    	return;
		}
		KAN_COD = 0;
    }
    
    if ( iskanji(ch) && ( ch != DEL ) ) {
		KAN_COD = ch;
		return;
    }
    
    
    if ( ( ch >= SPACE) && ( ch != DEL ) ) {
		chr_out(ch);
		return;
    }
    
    switch(ch) {
    case ESC:
    	fp = fopen("a:charmap.txt", "w");
    	fprintf( fp, "CUR_X         %d\n", CUR_X);
    	fprintf( fp, "LOC_Y         %d\n", LOC_Y);
    	fprintf( fp, "SIZ_X - 1     %d\n", SIZ_X - 1);
    	fprintf( fp, "MAX_X[LOC_Y]  %d\n", MAX_X[LOC_Y]);
		for( i = 0; i < 40; i++ ){
			fputx( &charmap[i][0], fp); 
			for( j = 0; j < 80; j++ )
    			fprintf( fp, "%d", charmap2[i][j] );
    		fputc( '\n', fp );	
    	}
    	fclose( fp );	
	case RIGHT:  
		cur_right();
		break;
	case LEFT:
		cur_left();
		break;
	case UP:
		cur_up();
		break;
	case DOWN:
		cur_down();
		break;
	case INS:  	
	    EGB_paintMode( work, 0x22 );
    	EGB_color( work, 0, BOXCOL );
	    EGB_color( work, 2, BOXCOL );
    	WORD( para + 0 ) = 33.5 * 16;
	    WORD( para + 2 ) = 28.5 * 16 + 1;
    	WORD( para + 4 ) = 34.5 * 16;
    	WORD( para + 6 ) = 28.5 * 16 + 18;
    	EGB_rectangle( work, para );
		if( INS_STAT == ON ){
			INS_STAT = OFF;
    		strcpy( para + 6, "　" );
		} else {
			INS_STAT = ON;	 
    		strcpy( para + 6, "挿" );
	    }
    	EGB_color( work,0,CHR_COL);
    	EGB_textSpace( work, 0 );
    	EGB_fontStyle( work, 0 );
    	WORD( para + 0 ) = 33.5 * 16;
    	WORD( para + 2 ) = 29.5 * 16;
    	WORD( para + 4 ) = 2;
    	EGB_sjisString( work, para );
		break;
    case BS:
		if( cur_left() )
			del();
		break;
   	case DEL:
   		del();
   		break;
	default:
		break;
	}

}

void	getstr( char *sret )
{
	short	i;
	
	*sret = (char)0x00;
	for( i = 0; ( MAX_Y >= i ) && ( i < 40 ); i++ )
		strcat( sret, &charmap[i][0] );
	
}

/*********** 32K 
static unsigned int dmy_pal[]={
	0x00007FFF,0x000c7FFF,0x01807FFF,0x018c7FFF,
	0x30007FFF,0x300c7FFF,0x31807FFF,0x318c7FFF,
	0x42107FFF,0x00187FFF,0x03007FFF,0x03187FFF,
	0x60007FFF,0x60187FFF,0x63007FFF,0x63187FFF };
************/

static unsigned int dmy_pal[]={
	0x0000000F,0x0001000F,0x0002000F,0x0003000F,
	0x0004000F,0x0005000F,0x0006000F,0x0007000F,
	0x0008000F,0x0009000F,0x000A000F,0x000B000F,
	0x000C000F,0x000D000F,0x000E000F,0x000F000F };

void	putstr(short pos,short len,char *str,char *att)
{
    static short    bak_len=0;
    static char   *bak_buf[128];
    register WIND *wp=act_wind;
    short           i,x,y,n,m;
    char          *p;
    char          para[16];

    for ( i = 0 ; i < bak_len ; i++ ) {
	EGB_putBlock(work,0,bak_buf[i]);
	free(bak_buf[i]);
    }
    cflush();
    bak_len = 0;
    x = CUR_X; y = CUR_Y;
    if ( (m = len) > 0 && len == pos )
	m++;
    for ( i = 0 ; i < m ; i += n ) {
	n = ((i < len && iskanji((char)*str)) ? 2:1);

	if ( (x + n) > SIZ_X ) {
	    x = 0;
	    y++;
	}

	if ( (p = (char *)malloc(FNT_X * FNT_Y * n * PIX_BYTE + 14)) == NULL )
	    break;
		DWORD(p+0) = (unsigned int)(p+14);
    	WORD(p+4) = 0x014;			/* Data Selecter */
    	WORD(p+6) = TOP_X + x * FNT_X;
    	WORD(p+8) = TOP_Y + y * FNT_Y;
    	WORD(p+10) = WORD(p+6) + n * FNT_X - 1;
    	WORD(p+12) = WORD(p+8) + FNT_Y - 1;
    	EGB_getBlock(work,p);
	bak_buf[bak_len++] = p;

	if ( i < len ) {
            EGB_paintMode(work,0x22);
            EGB_color(work,0, ROAKCOL/*dmy_pal[*att]>>16*/);
            EGB_color(work,2, ROAKCOL/*dmy_pal[*att]>>16*/);
            WORD(para+0) = WORD(p+6);
            WORD(para+2) = WORD(p+8);
            WORD(para+4) = WORD(p+10);
            WORD(para+6) = WORD(p+12);
            EGB_rectangle(work,para);

            EGB_color(work,0,dmy_pal[*att]&0xFFFF);
            EGB_textSpace(work,FNT_X - 8);
            EGB_fontStyle(work,FNT_STL);
            WORD(para+0) = WORD(p+6);
            WORD(para+2) = WORD(p+8) + (FNT_Y - 2);
            WORD(para+4) = n;
            *(para+6) = *str;
            *(para+7) = *(str+1);
            EGB_sjisString(work,para);
            str += n;
   	    att += n;
	}

	if ( i == pos ) {
		EGB_color(work,0,CUR_COL);
        WORD(para+0) = WORD(p+6);
        WORD(para+2) = WORD(p+12);
        WORD(para+4) = WORD(p+10);
        WORD(para+6) = WORD(p+12);
        EGB_rectangle(work,para);
	}

	if ( (x += n) >= SIZ_X ) {
	    x = 0;
	    y++;
	}
    }
}
void	putsys(short len,char *str,char *att)
{
    static short  bak_len=0;
    static char   *bak_buf;
    register WIND *wp=act_wind;
    short         i,x,y,n;
    char          *p;
    char          para[16];

    if ( bak_len > 0 ) {
		EGB_putBlock(work,0,bak_buf);
		free(bak_buf);
    }

    bak_len = 0;
    if ( len == 0 )
	return;

    if ( (p = bak_buf = 
	 (char *)malloc(FNT_X * FNT_Y * len * PIX_BYTE + 14)) == NULL )
	return;

    DWORD(p+0) = (unsigned int)(p+14);
    WORD(p+4) = 0x014;			/* Data Selecter */
    WORD(p+6) = x = TOP_X;
    WORD(p+8) = y = BTM_Y + 8;
    WORD(p+10) = x + len * FNT_X - 1;
    WORD(p+12) = y + FNT_Y - 1;
    EGB_getBlock(work,p);
    bak_len = len;

    for ( i = 0 ; i < len ; i += n ) {
	n = (iskanji((char)*str) ? 2:1);

        EGB_paintMode(work,0x22);
        EGB_color(work,0,ROAKCOL/*dmy_pal[*att]>>16*/);
        EGB_color(work,2,ROAKCOL/*dmy_pal[*att]>>16*/);
        WORD(para+0) = x;
        WORD(para+2) = y;
        WORD(para+4) = x + n * FNT_X - 1;
        WORD(para+6) = y + FNT_Y - 1;
        EGB_rectangle(work,para);

        EGB_color(work,0,dmy_pal[*att]&0xFFFF);
        EGB_textSpace(work,FNT_X - 8);
        EGB_fontStyle(work,FNT_STL);
        WORD(para+0) = x;
        WORD(para+2) = y + (FNT_Y - 2);
        WORD(para+4) = n;
        *(para+6) = *str;
        *(para+7) = *(str+1);
        EGB_sjisString(work,para);
        str += n;
   	att += n;
	x += (FNT_X * n);
    }
}
void	putmode(short md,short sf,char *str)
{
    register WIND *wp=act_wind;
    char    para[40];
    
	EGB_writePage( work, 0x40 );
    EGB_paintMode( work, 0x22 );
    
    EGB_color( work, 0, DBXCOL );
    EGB_color( work, 2, BOXCOL );
    WORD( para + 0 ) = 32.5 * 16;
    WORD( para + 2 ) = 28.5 * 16;
    WORD( para + 4 ) = 39.5 * 16;
    WORD( para + 6 ) = 28.5 * 16 + 19;
    EGB_rectangle( work, para );

	WORD( para + 0 ) = 2;
	WORD( para + 2 ) = 32.5 * 16;
	WORD( para + 4 ) = 28.5 * 16;
	WORD( para + 6 ) = 32.5 * 16;
	WORD( para + 8 ) = 28.5 * 16 + 18;
    EGB_color( work, 0, UBXCOL );
	EGB_color( work, 2, UBXCOL );
	EGB_connect( work, para );

    if( INS_STAT == ON )
    	strcpy( para + 6, "挿" );
    else	
    	strcpy( para + 6, "　" );
    strcat( para + 6, str );
    EGB_color( work,0,CHR_COL);
    EGB_textSpace( work, 0 );
    EGB_fontStyle( work, 0 );
    WORD( para + 0 ) = 33.5 * 16;
    WORD( para + 2 ) = 29.5 * 16;
    WORD( para + 4 ) = 11;
    EGB_sjisString( work, para );
}
