Listing 2: keytest.c
/****************************************************
*
* For testing what the keys send on ASCII terminals,
* emulators, and workstation term windows.
*
* Usage:
* keytest 8-bit without xon-xoff
* keytest -7 7-bit without xon-xoff
* keytest -x 8-bit with xon-xoff
* keytest -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);
signal(SIGINT, quit);
termio_set(0, &Crt_raw);
go();
}
/* = = = = = = = = = = quit = = = = = = = = = = = =*/
/*
* Reset the terminal back to normal and exit.
*
*/
quit()
{
termio_set(0, &Crt_normal);
write(1, "\nExiting.\n", 10);
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;
visible(c);
}
}
/* = = = = = = = = = visible = = = = = = = = = =*/
/*
* Print out a character in human readable form.
*
*/
static visible(ch)
char ch;
{
int c;
int i;
c = ch & 0x00FF;
if (c == ' ')
{
visible_out('\\');
visible_out('s');
}
else if (c < ' ')
{
if (c == 0x1B)
{
visible_out('\\');
visible_out('E');
}
else if (c == '\b')
{
visible_out('\\');
visible_out('b');
}
else if (c == '\r')
{
visible_out('\\');
visible_out('r');
visible_out('\r');
visible_out('\n');
}
else if (c == '\n')
{
visible_out('\\');
visible_out('n');
}
else if (c == '\t')
{
visible_out('\\');
visible_out('t');
}
else
{
visible_out('^');
visible_out('@' + c);
}
}
else if (c < 0x7F)
{
if (c == '\\')
{
visible_out('\\');
visible_out(c);
}
else if (c == '^')
{
visible_out('\\');
visible_out(c);
}
else
{
visible_out(c);
}
}
else if (c == 0x7F)
{
visible_out('^');
visible_out('?');
}
else
{
visible_out('\\');
visible_out('0' + ((c >> 6) & 0x3));
visible_out('0' + ((c >> 3) & 0x7));
visible_out('0' + ( c & 0x7));
}
}
/* = = = = = = visible_out = = = = = = =*/
/*
* Output a single character.
*
*/
static visible_out(c)
int c;
{
/* use write so that flush will be immediate */
write(1, &c, 1);
return;
}
|