The Linux Programming Interface

(nextflipdebug5) #1
Terminals 1315

if (sigaction(SIGTSTP, NULL, &prev) == -1)
errExit("sigaction");
if (prev.sa_handler != SIG_IGN)
if (sigaction(SIGTSTP, &sa, NULL) == -1)
errExit("sigaction");
} else { / Use raw mode /
d if (ttySetRaw(STDIN_FILENO, &userTermios) == -1)
errExit("ttySetRaw");
}


f sa.sa_handler = handler;
if (sigaction(SIGTERM, &sa, NULL) == -1)
errExit("sigaction");


setbuf(stdout, NULL); /* Disable stdout buffering */

g for (;;) { / Read and echo stdin /
n = read(STDIN_FILENO, &ch, 1);
if (n == -1) {
errMsg("read");
break;
}


if (n == 0) /* Can occur after terminal disconnect */
break;

h if (isalpha((unsigned char) ch)) / Letters --> lowercase /
putchar(tolower((unsigned char) ch));
else if (ch == '\n' || ch == '\r')
putchar(ch);
else if (iscntrl((unsigned char) ch))
printf("^%c", ch ^ 64); / Echo Control-A as ^A, etc. /
else
putchar(''); / All other chars as '' /


j if (ch == 'q') / Quit loop /
break;
}


k if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &userTermios) == -1)
errExit("tcsetattr");
exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––– tty/test_tty_functions.c
Here is an example of what we see when we ask the program in Listing 62-4 to use
raw mode:


$ stty Initial terminal mode is sane (cooked)
speed 38400 baud; line = 0;
$ ./test_tty_functions
abc Type abc, and Control-J
def Type DEF, Control-J, and Enter
^C^Z Type Control-C, Control-Z, and Control-J
q$ Type q to exit
Free download pdf