Listing 1: echotest.c
/****************************************************
*
* For testing escape sequences and control
* characters on ASCII terminals,
* emulators, and workstation term windows.
*
* Usage:
* echotest 8-bit without xon-xoff
* echotest -7 7-bit without xon-xoff
* echotest -x 8-bit with xon-xoff
* echotest -x -7 7-bit with xon-xoff
*
****************************************************/
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <termio.h>
struct termio Crt_normal;
struct termio Crt_raw;
/* = = = = = = = = = = main = = = = = = = = = = = = =*/
main(argc, argv)
int argc;
char **argv;
{
int eight_bit = 1;
int xon_xoff = 0;
int quit();
while(argc > 1)
{
if (strcmp(argv[1], "-7") == 0)
{
eight_bit = 0;
argc--;
argv++;
}
else if (strcmp(argv[1], "-x") == 0)
{
xon_xoff = 1;
argc--;
argv++;
}
else
{
printf("Usage: %s [-7] [-x]\n", argv[0]);
exit(1);
}
}
fprintf(stderr, "Press qqqq to quit\n");
termio_init(0, xon_xoff, eight_bit, &Crt_normal,
&Crt_raw);
signal(SIGTERM, quit);
signal(SIGHUP, quit);
termio_set(0, &Crt_raw);
go();
}
/* = = = = = = = = = = quit = = = = = = = = = = = =*/
/*
* Reset the terminal back to normal and exit.
*
*/
quit()
{
termio_set(0, &Crt_normal);
exit(0);
}
/* = = = = = = = = = = go = = = = = = = = = = = =*/
/*
* Main processing loop.
*
*/
go()
{
char c;
static cues=0;
int status;
while(1)
{
status = read(0, &c, 1);
if (status != 1)
{
perror("Read terminal");
quit();
}
if (c == 'q')
{
if(++cues>=4)
quit();
}
else
cues=0;
write(1, &c, 1);
}
}
/* = = = = = = = = termio_init = = = = = = = = = =*/
/*
* Set a termio structure to raw and save the
* normal setting.
*
*/
termio_init(fd, use_xon_xoff, use_input_8_bit, normal,
raw)
int use_xon_xoff;
int use_input_8_bit;
struct termio *normal;
struct termio *raw;
{
int status;
int baud;
status = ioctl(fd, TCGETA, normal);
if (status < 0)
{
perror("ioctl TCGETA failed");
return(-1);
}
status = ioctl(fd, TCGETA, raw);
if (status < 0)
{
perror("ioctl TCGETA 2 failed");
return(-1);
}
raw->c_iflag |= (0);
raw->c_iflag &= ~(IGNBRK | BRKINT | PARMRK |
INLCR | IGNCR | ICRNL | IUCLC |
IXANY | IXOFF);
if (use_input_8_bit)
raw->c_iflag &= ~(ISTRIP);
if (use_xon_xoff)
raw->c_iflag |= (IXON);
else
raw->c_iflag &= ~(IXON);
raw->c_oflag |= (0);
raw->c_oflag &= ~(OPOST | OLCUC | ONLCR | OCRNL |
ONOCR | ONLRET | OFILL |
OFDEL | TAB3);
if (use_input_8_bit)
{
raw->c_cflag &= ~(PARENB | CSIZE);
raw->c_cflag |= (CS8);
}
raw->c_lflag |= (NOFLSH);
raw->c_lflag &= ~(ISIG | ICANON | XCASE | ECHO |
ECHOE | ECHOK);
raw->c_cc[VMIN] = 1;
raw->c_cc[VTIME] = 0;
return(0);
}
/* = = = = = = = = termio_set = = = = = = = = = = = =*/
/*
* Use the given termio structure to set the line.
*
*/
termio_set(fd, saved)
int fd;
struct termio *saved;
{
int status;
status = ioctl(fd, TCSETAW, saved);
if (status < 0)
{
perror("ioctl TCSETAW set failed");
return(-1);
}
return(0);
}
|