extern "C" {
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
}

#include "util.H"

bye()
{
    system("stty cooked echo");
}


char*
charfmt(char c)
{
    static char bfr[40];

    if (c == 0)
	return "NUL";
    else if (c == 127)
	return "DEL";
    else if (c > 0 && c < ' ') {
	sprintf(bfr, "^%c", c+'@');
	return bfr;
    }
    else if (c >= ' ' && c < 127) {
	bfr[0] = c;
	bfr[1] = 0;
	return bfr;
    }
    sprintf(bfr, "\\%02x", 0xff & (int) c);
    return bfr;
}

main()
{
    Buffer input(0,5);
    int c;

    system("stty raw -echo");
    atexit(bye);

    printf("waiting 5 seconds for some input..."); fflush(stdout);
    c = input.readchar();
    if (c == EOF)
	printf("EOF\r\n");
    else
	printf("%s\r\n", charfmt(c));

    input.alarm(0);
    printf("waiting forever for some input..."); fflush(stdout);
    c = input.readchar();
    printf("%s\r\n", charfmt(c));
    exit(0);
}
