#include "io.h"

#ifdef SYSV
#include <termio.h>

struct termio tty_state, tty_new ;

get_tty()
{
	if(ioctl(1,TCGETA,&tty_state) < 0) {
		perror("Arggg, ioctl failed on reading tty state") ;
		exit(-1) ;
	}
	return 1 ;
}

init_tty()
{
	bcopy(&tty_state, &tty_new, sizeof(tty_new)) ;
	tty_new.c_iflag = IGNBRK ;
	tty_new.c_oflag = 0 ;
	tty_new.c_lflag = 0 ;
	tty_new.c_line = 0 ;
	tty_new.c_cc[VMIN] = 1 ;
	tty_new.c_cc[VTIME] = 0 ;
	if(ioctl(1,TCSETA,&tty_new) < 0)
		perror("ioctl failed on writing new tty state") ;
}

reset_tty()
{
	if(ioctl(1,TCSETA,&tty_state) < 0)
		perror("ioctl failed in reset_tty") ;
}

restore_tty()
{
	if(ioctl(1,TCSETA,&tty_new) < 0)
		perror("ioctl failed in reset_tty") ;
}
#else
struct sgttyb tty_state, tty_new ;

get_tty()
{
    if(gtty(1,&tty_state) < 0) {
        fprintf(stderr,"gtty failed\n") ;
        exit(-1) ;
    }
    return 1 ;
}

init_tty()
{
    bcopy(&tty_state, &tty_new, sizeof(tty_new)) ;
/*    tty_new.sg_flags |= CBREAK ;
    tty_new.sg_flags &= ~(TANDEM|RAW|LCASE|ECHO|CRMOD) ;*/
    tty_new.sg_flags |= RAW ;
    tty_new.sg_flags &= ~(TANDEM|CBREAK|LCASE|ECHO|CRMOD) ;
    stty(1,&tty_new) ;
}

reset_tty()
{
    stty(1,&tty_state) ;
}

restore_tty()
{
    stty(1,&tty_new) ;
}
#endif

#define TERMCOMSIZE (80)

int dumb_term = YEA ;

char clearbuf[TERMCOMSIZE] ;
int clearbuflen ;

char cleolbuf[TERMCOMSIZE] ;
int cleolbuflen ;

char cursorm[TERMCOMSIZE] ;
char *cm ;

char scrollrev[TERMCOMSIZE] ;
int scrollrevlen ;

char strtstandout[TERMCOMSIZE] ;
int strtstandoutlen ;

char endstandout[TERMCOMSIZE] ;
int endstandoutlen ;

int t_lines  = 24;
int t_columns = 80 ;

int automargins ;

char *outp ;
int  *outlp ;

outcf(ch)
char ch ;
{
    if(*outlp >= TERMCOMSIZE)
      return ;
    (*outlp)++ ;
    *outp++ = ch ;
}

term_init(term)
char *term ;
{
    extern char PC, *UP, *BC ;
    extern short ospeed ;
    static char UPbuf[TERMCOMSIZE] ;
    static char BCbuf[TERMCOMSIZE] ;
    static char buf[4096] ;
    char sbuf[4096] ;
    char *sbp, *s ;
    char *tgetstr() ;

#ifdef SYSV
    ospeed = 0 ;
#else
    ospeed = tty_state.sg_ospeed ;
#endif

    if(tgetent(buf, term) != 1)
      return NA ;
    sbp = sbuf ;
    s = tgetstr("pc", &sbp) ; /* get pad character */
    if(s)
      PC = *s ;
    t_lines = tgetnum("li") ;
    t_columns = tgetnum("co") ;
    automargins = tgetflag("am") ;
    outp = clearbuf ;         /* fill clearbuf with clear screen command */
    outlp = &clearbuflen ;
    clearbuflen = 0 ;
    sbp = sbuf ;
    s = tgetstr("cl", &sbp) ;
    if(s)
      tputs(s,t_lines, outcf) ;
    outp = cleolbuf ;         /* fill cleolbuf with clear to eol command */
    outlp = &cleolbuflen ;
    cleolbuflen = 0 ;
    sbp = sbuf ;
    s = tgetstr("ce",&sbp) ;
    if(s)
      tputs(s,1,outcf) ;
    outp = scrollrev ;
    outlp = &scrollrevlen ;
    scrollrevlen = 0 ;
    sbp = sbuf ;
    s = tgetstr("sr",&sbp) ;
    if(s)
      tputs(s,1,outcf) ;
    outp = strtstandout ;
    outlp = &strtstandoutlen ;
    strtstandoutlen = 0 ;
    sbp = sbuf ;
    s = tgetstr("so",&sbp) ;
    if(s)
      tputs(s,1,outcf) ;
    outp = endstandout ;
    outlp = &endstandoutlen ;
    endstandoutlen = 0 ;
    sbp = sbuf ;
    s = tgetstr("se",&sbp) ;
    if(s)
      tputs(s,1,outcf) ;
    sbp = cursorm ;
    cm = tgetstr("cm",&sbp) ;
    if(cm)
      dumb_term = NA ;
    else
      dumb_term = YEA ;
    sbp = UPbuf ;
    UP = tgetstr("up",&sbp) ;
    sbp = BCbuf ;
    BC = tgetstr("bc",&sbp) ;
    if(dumb_term) {
        t_lines = 24 ;
        t_columns = 80 ;
    }
    return YEA ;
}


do_move(destcol,destline,outc)
int destcol,destline ;
int (*outc)() ;
{
    tputs(tgoto(cm,destcol,destline), 0, outc) ;
}

