#ifndef TERM_H
#define TERM_H
#include "util.h"
#ifndef ECHO
#define ECHO 8
#endif
#ifdef TIOCOUTQ
#define output_pending() (ioctl(1, TIOCOUTQ, &iocount),iocount)
#endif
#define setimage(of,to) (mvaddch(of->posy+1,of->posx*2,of->image=(to)))
#define mvaddch(y,x,ch) move((y),(x),(ch))
#define mvaddc(y,x,ch) move((y),(x),(ch))
#define addc(ch) (write(1,&(ch),1), real_x++)
#define addspace() (write(1," ",1), real_x++)
#define mvaddstr(y,x,s) (move((y),(x),0), tmpstr = (s), \
tmplen = strlen(tmpstr), write(1, tmpstr, tmplen), real_x += tmplen)
EXT size_t tmplen;
EXT const char *tmpstr;
#define beg_qwrite() (maxcmstring = cmbuffer)
#ifdef vax
#define qwrite() asm("movc3 _gfillen,_filler,*_maxcmstring"); maxcmstring += gfillen
#else
#define qwrite() (movc3(gfillen,filler,maxcmstring), maxcmstring += gfillen)
#endif
#define qaddc(ch) (*maxcmstring++ = (ch), real_x++)
#define qaddch(ch) (*maxcmstring++ = (ch), real_x++)
#define qaddspace() (*maxcmstring++ = ' ', real_x++)
#define end_qwrite() (write(1,cmbuffer,maxcmstring-cmbuffer))
EXT int CMsize;
EXT int BCsize INIT(1);
EXT int DOsize INIT(1000);
EXT int UPsize INIT(1000);
EXT int NDsize INIT(1000);
EXT int charsperhalfsec;
EXT int real_y INIT(-100);
EXT int real_x INIT(-100);
#ifdef DOINIT
char filler[] = {0,'\b',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
#else
EXT char filler[];
#endif
EXT char *bsptr INIT(filler+1);
EXT char term[12];
EXT char gfillen INIT(25);
EXT char *maxcmstring;
EXT char cmbuffer[512];
#define BREAKCH '\0'
EXT char INTRCH INIT('\03');
#ifdef PUSHBACK
EXT char circlebuf[PUSHSIZE];
EXT int nextin INIT(0);
EXT int nextout INIT(0);
# ifdef PENDING
# ifdef FIONREAD
EXT long iocount INIT(0);
# ifndef lint
# define input_pending() (nextin!=nextout || \
(ioctl(0, FIONREAD, &iocount),(int)iocount))
# else
# define input_pending() bizarre
# endif
# else
int circfill();
# ifdef RDCHK
# ifndef lint
# define input_pending() rdchk(0)
# else
# define input_pending() bizarre
# endif
# else
# ifndef O_NDELAY
??? PENDING is not defined correctly in warp.h
# endif
EXT int devtty INIT(0);
# ifndef lint
# define input_pending() (nextin!=nextout || circfill())
# else
# define input_pending() bizarre
# endif
# endif
# endif
# else
# ??? warp will not work without PENDING
# ifndef lint
# define input_pending() (nextin!=nextout)
# else
# define input_pending() bizarre
# endif
# endif
#else
# ifdef PENDING
# ifdef FIONREAD
# define read_tty(addr,size) read(0,addr,size)
# ifndef lint
# define input_pending() (ioctl(0, FIONREAD, &iocount), \
(int)iocount)
# else
# define input_pending() bizarre
# endif
EXT long iocount INIT(0);
# else
# ifdef RDCHK
# define read_tty(addr,size) read(0,addr,size)
# ifndef lint
# define input_pending() rdchk(0)
# else
# define input_pending() bizarre
# endif
# else
# ifndef O_NDELAY
??? PENDING is not defined correctly in warp.h
# endif
EXT int devtty INIT(0);
EXT bool is_input INIT(false);
EXT char pending_ch INIT(0);
# ifndef lint
# define input_pending() (is_input || \
(is_input=read(devtty,&pending_ch,1)))
# else
# define input_pending() bizarre
# endif
# endif
# endif
# else
??? warp will not work without PENDING
# define read_tty(addr,size) read(0,addr,size)
# define input_pending() (false)
# endif
#endif
#ifdef TERMIOS
EXT struct termios _tty, _oldtty;
#elif defined(TERMIO)
typedef int speed_t;
EXT struct termio _tty, _oldtty;
#define tcsetattr(fd, how, ti) ioctl(fd, how, ti)
#define tcgetattr(fd, ti) ioctl(fd, TCGETA, ti)
#define cfgetospeed(ti) ((ti)->c_cflag & CBAUD)
#else
typedef int speed_t;
EXT struct sgttyb _tty;
EXT int _res_flg INIT(0);
#endif
EXT int _tty_ch INIT(2);
EXT bool bizarre INIT(false);
#if defined(TERMIO) || defined(TERMIOS)
#define raw() ((bizarre=1),_tty.c_lflag &=~ISIG,_tty.c_cc[VMIN] = 1,tcsetattr(_tty_ch,TCSAFLUSH,&_tty))
#define noraw() ((bizarre=1),_tty.c_lflag |= ISIG,_tty.c_cc[VEOF] = CEOF,tcsetattr(_tty_ch,TCSAFLUSH,&_tty))
#define crmode() ((bizarre=1),_tty.c_lflag &=~ICANON,_tty.c_cc[VMIN] = 1,tcsetattr(_tty_ch,TCSAFLUSH,&_tty))
#define nocrmode() ((bizarre=1),_tty.c_lflag |= ICANON,_tty.c_cc[VEOF] = CEOF,tcsetattr(_tty_ch,TCSAFLUSH,&_tty))
#define echo() ((bizarre=1),_tty.c_lflag |= ECHO, tcsetattr(_tty_ch, TCSANOW, &_tty))
#define noecho() ((bizarre=1),_tty.c_lflag &=~ECHO, tcsetattr(_tty_ch, TCSANOW, &_tty))
#define nl() ((bizarre=1),_tty.c_iflag |= ICRNL,_tty.c_oflag |= ONLCR,tcsetattr(_tty_ch, TCSANOW, &_tty))
#define nonl() ((bizarre=1),_tty.c_iflag &=~ICRNL,_tty.c_oflag &=~ONLCR,tcsetattr(_tty_ch, TCSANOW, &_tty))
#define savetty() (tcgetattr(_tty_ch, &_oldtty),tcgetattr(_tty_ch, &_tty))
#define resetty() ((bizarre=0),tcsetattr(_tty_ch, TCSAFLUSH, &_oldtty))
#define unflush_output()
#else
#define raw() ((bizarre=1),_tty.sg_flags|=RAW, stty(_tty_ch,&_tty))
#define noraw() ((bizarre=1),_tty.sg_flags&=~RAW,stty(_tty_ch,&_tty))
#define crmode() ((bizarre=1),_tty.sg_flags |= CBREAK, stty(_tty_ch,&_tty))
#define nocrmode() ((bizarre=1),_tty.sg_flags &= ~CBREAK,stty(_tty_ch,&_tty))
#define echo() ((bizarre=1),_tty.sg_flags |= ECHO, stty(_tty_ch, &_tty))
#define noecho() ((bizarre=1),_tty.sg_flags &= ~ECHO, stty(_tty_ch, &_tty))
#define nl() ((bizarre=1),_tty.sg_flags |= CRMOD,stty(_tty_ch, &_tty))
#define nonl() ((bizarre=1),_tty.sg_flags &= ~CRMOD, stty(_tty_ch, &_tty))
#define savetty() (gtty(_tty_ch, &_tty), _res_flg = _tty.sg_flags)
#define resetty() ((bizarre=0),_tty.sg_flags = _res_flg, stty(_tty_ch, &_tty))
#endif
#ifdef TIOCSTI
#ifdef lint
#define forceme(c) ioctl(_tty_ch,TIOCSTI,Null(long*))
#else
#define forceme(c) ioctl(_tty_ch,TIOCSTI,c)
#endif
#else
#define forceme(c)
#endif
#ifdef HAVETERMLIB
#if 0
EXT char *BC INIT(NULL);
EXT char *UP INIT(NULL);
#endif
EXT char *myUP;
EXT char *ND INIT(NULL);
EXT char *myND;
EXT char *DO INIT(NULL);
EXT char *myDO;
EXT char *CR INIT(NULL);
EXT char *VB INIT(NULL);
EXT char *CL INIT(NULL);
EXT char *CE INIT(NULL);
EXT char *CM INIT(NULL);
EXT char *HO INIT(NULL);
EXT char *CD INIT(NULL);
EXT char *SO INIT(NULL);
EXT char *SE INIT(NULL);
EXT int SG INIT(0);
EXT char *US INIT(NULL);
EXT char *UE INIT(NULL);
EXT char *UC INIT(NULL);
EXT int UG INIT(0);
EXT bool AM INIT(false);
EXT bool XN INIT(false);
#if 0
EXT char PC INIT(0);
EXT short ospeed INIT(0);
#endif
EXT int LINES INIT(0), COLS INIT(0);
EXT int just_a_sec INIT(960);
EXT char ERASECH;
EXT char KILLCH;
#define clear() (do_tc(CL,LINES),real_y=real_x=0)
#define erase_eol() do_tc(CE,1)
#define backspace() (do_tc(BC,0),real_x--)
#define clear_rest() do_tc(CD,LINES)
#define underline() do_tc(US,1)
#define un_underline() do_tc(UE,1)
#define underchar() do_tc(UC,0)
#define standout() do_tc(SO,1)
#define un_standout() do_tc(SE,1)
#define up_line() do_tc(UP,1)
#define dingaling() do_tc(VB,1)
#else
????????
#endif
void term_init(void);
void term_set(char *);
#ifdef PUSHBACK
void pushchar(int);
void mac_init(char *);
void mac_line(char *, char *, size_t);
#endif
void page(const char *filename, size_t);
void move(int, int, int);
void do_tc(const char *, int);
int comp_tc(char *, const char *, int);
void helper(void);
void rewrite(void);
int cmstore(int);
void eat_typeahead(void);
void settle_down(void);
#ifndef read_tty
int read_tty(char *, ssize_t);
#endif
int read_nd(char *, size_t);
void getcmd(char *);
void pushstring(char *);
#endif