The Linux Programming Interface

(nextflipdebug5) #1
Terminals 1313

Listing 62-4: Demonstrating cbreak and raw modes
–––––––––––––––––––––––––––––––––––––––––––––––––– tty/test_tty_functions.c
#include <termios.h>
#include <signal.h>
#include <ctype.h>
#include "tty_functions.h" /* Declarations of ttySetCbreak()
and ttySetRaw() */
#include "tlpi_hdr.h"

qstatic struct termios userTermios;
/ Terminal settings as defined by user /


static void / General handler: restore tty settings and exit /
handler(int sig)
{
w if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &userTermios) == -1)
errExit("tcsetattr");
_exit(EXIT_SUCCESS);
}


static void / Handler for SIGTSTP /
etstpHandler(int sig)
{
struct termios ourTermios; / To save our tty settings /
sigset_t tstpMask, prevMask;
struct sigaction sa;
int savedErrno;


savedErrno = errno; /* We might change 'errno' here */

/* Save current terminal settings, restore terminal to
state at time of program startup */

r if (tcgetattr(STDIN_FILENO, &ourTermios) == -1)
errExit("tcgetattr");
t if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &userTermios) == -1)
errExit("tcsetattr");


/* Set the disposition of SIGTSTP to the default, raise the signal
once more, and then unblock it so that we actually stop */

if (signal(SIGTSTP, SIG_DFL) == SIG_ERR)
errExit("signal");
raise(SIGTSTP);

sigemptyset(&tstpMask);
sigaddset(&tstpMask, SIGTSTP);
if (sigprocmask(SIG_UNBLOCK, &tstpMask, &prevMask) == -1)
errExit("sigprocmask");

/* Execution resumes here after SIGCONT */

if (sigprocmask(SIG_SETMASK, &prevMask, NULL) == -1)
errExit("sigprocmask"); /* Reblock SIGTSTP */
Free download pdf