wsh/
wsh/binsrc/
wsh/docs/help/
wsh/docs/old/
wsh/etc/
wsh/src/util/
/* vttest.c -- written by Charles Howes (chowes@sfu.ca) */

/* Legalese:  If you want to make money from this, I get a (minor) cut. */
/*            I'd also like my name to stay on this code. */

/* Modified to (hopefully) be more portable, run as a function,
   and test only for vt100 terminal emulation on 9/30/93

   This function will return 1 if the tty is vt100 or 0 if an error
   occurred or the terminal doesn't appear to be a vt100 terminal.

	-Sam Lantinga		(slouken@toadflax.cs.ucdavis.edu)
*/

#include	<stdio.h>

#ifdef HAVE_TERMIO_H
#include	<fcntl.h>
#include        <termio.h>

#ifdef HAVE_BSDTTY_H
#include        <sys/bsdtty.h>
#endif /* HAVE_BSDTTY_H */

#ifdef MAIN
#define HAVE_TERMIO_H
main(){ if ( vttest() ) printf("Using a vt100 terminal\n"); }
#endif


int vttest() 
{
	char   buff[512];
	int    x=0, w, rc=0, fd;
	struct termio ttold, ttraw;

	/* Set the terminal in a raw mode */
	if ( (fd=open("/dev/tty", O_RDWR)) < 0 )
		return(0);

	if ( ioctl(fd, TCGETA, (char *)&ttold) < 0 )
		return(0);
	(void) ioctl(fd, TCGETA, (char *)&ttraw);

        ttraw.c_iflag=(IGNBRK | ISTRIP);   /* turn off all input control */
        ttraw.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONLRET);
                                        /* disable output post-processing */
        ttraw.c_lflag = 0;
        ttraw.c_cc[VMIN]=0;          /* 1 or more chars satisfy read */
        ttraw.c_cc[VTIME]=10;        /* 10'ths of seconds between chars */

        if (ioctl(fd, TCSETA, (char *)&ttraw) < 0)
                return(0);

	do { /* Vt100 test: ESC [ c */
  		write(fd,"\033[c", 3);
  		while ((w=read(fd,buff[x],1))!=0 && x++<20);
  		if (x) {
			rc=1;
			break;
		}
	} while (0);

        (void) ioctl(fd, TCSETA, (char *)&ttold);
	(void) close(fd);

	return rc;
}
#else
int vttest(){return(0);}
#endif /* HAVE_TERMIO_H */