1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/* vttest.c -- written by Charles Howes (chowes@sfu.ca) */
/* 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.
Completely re-written 10/9/93 for better portability
-Sam Lantinga (slouken@toadflax.cs.ucdavis.edu)
*/
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_TERMIO_H
#include <termio.h>
#elif defined HAVE_TERMIOS_H
# include <termios.h>
# include <sys/ioctl.h>
# ifndef termio
# define termio termios
# endif
#else /* !HAVE_TERMIO_H && !HAVE_TERMIOS_H */
#include <setjmp.h>
#include <signal.h>
#include <sys/ioctl.h>
#define termio sgttyb
#define TCGETA TIOCGETP
#define TCSETAW TIOCSETP
#endif /* HAVE_TERMIO_H */
#ifdef NEED_COMPAT_H
#include <sys/ioctl_compat.h>
#endif /* NEED_COMPAT_H */
#ifdef HAVE_BSDTTY_H
#include <sys/bsdtty.h>
#endif /* HAVE_BSDTTY_H */
#ifndef OLCUC
# define OLCUC 0 /* Missing inf FreeBSD and GNU/kFreeBSD. */
#endif
#ifdef MAIN
int main() { if ( vttest() ) printf("vt100\n"); }
#endif
/* Register that we are alarmed. (called by SIG_ALRM on BSD) */
static int alarmed;
#if !defined(HAVE_TERMIO_H) && !defined(HAVE_TERMIOS_H)
static jmp_buf alarm_buf;
static void alrm_trap() { alarmed=1; longjmp(alarm_buf, 1); }
#endif /* No termio.h */
int vttest()
{
char buff[512];
int x=0, rc=0, fd;
struct termio ttold, ttraw;
/* Disabled since at least on sparc it causes a hang. */
return 1;
/* Set the terminal in a raw mode */
if ( (fd=open("/dev/tty", O_RDWR, 0666)) < 0 )
return(0);
#ifdef HAVE_TERMIOS_H
if ( tcgetattr(fd, &ttold) < 0 )
return(0);
(void) tcgetattr(fd, &ttraw);
#else /* !HAVE_TERMIOS_H */
if ( ioctl(fd, TCGETA, (char *)&ttold) < 0 )
return(0);
(void) ioctl(fd, TCGETA, (char *)&ttraw);
#endif
#if defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)
#ifdef SEVEN_BIT
ttraw.c_iflag=(IGNBRK | ISTRIP); /* turn off all input control */
#else
ttraw.c_iflag=(IGNBRK); /* turn off all input control */
#endif /* SEVEN_BIT */
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 */
#else /* !HAVE_TERMIO_H && !HAVE_TERMIOS_H */
ttraw.sg_flags |= RAW; /* turn RAW mode on */
ttraw.sg_flags &= ~ECHO; /* turn ECHO off */
#endif /* HAVE_TERMIO_H */
#ifdef HAVE_TERMIOS_H
if (tcsetattr(fd, TCSADRAIN, &ttraw) < 0)
#else /* !HAVE_TERMIOS_H */
if (ioctl(fd, TCSETAW, (char *)&ttraw) < 0)
#endif
return(0);
write(fd,"\033[c", 3); /* Vt100 test: ESC [ c */
#if !defined(HAVE_TERMIO_H) && !defined(HAVE_TERMIOS_H)
/* We need to set an alarm */
signal(SIGALRM, alrm_trap);
alarmed=0;
alarm(1);
setjmp(alarm_buf);
#endif
while ( !alarmed && (x < 20) ) {
if ( read(fd, &buff[x++], 1) <= 0 )
break;
}
buff[x]='\0'; /* For printing, if we desire. */
if ( buff[0] == '\033' ) /* An escape sequence? :) */
rc=1;
#if !defined(HAVE_TERMIO_H) && !defined(HAVE_TERMIOS_H)
alarm(0);
signal(SIGALRM, SIG_DFL);
#endif
#if HAVE_TERMIOS_H
(void) tcsetattr(fd, TCSADRAIN, &ttold);
#else /* !HAVE_TERMIOS_H */
(void) ioctl(fd, TCSETAW, (char *)&ttold);
#endif
(void) close(fd);
#ifdef not_defined /* Print out the response for debugging */
for ( w=0; buff[w]; ++w )
if ( buff[w] < ' ' )
printf("^%c", buff[w]+'@');
else
printf("%c", buff[w]);
printf("\n");
#endif /* not_defined */
return rc;
}
|