Advanced Programming in UNIX Environment Episode 120

Our definition of cbreak mode is the following:

  • Noncanonical mode. As we mentioned at the beginning of this section, this mode turns off some input character processing. It does not turn off signal handling, so the user can always type one of the characters that triggers a terminal-generated signal. Be aware that the caller should catch these signals; otherwise, there’s a chance that the signal will terminate the program, and the terminal will be left in cbreak mode.
    As a general rule, whenever we write a program that changes the terminal mode, we should catch most signals. This allows us to reset the terminal mode before terminating.
  • Echo off.
  • One byte at a time input. To do this, we set MIN to 1 and TIME to 0. This is case B from Figure 18.19. A read won’t return until at least one byte is available.

We define raw mode as follows:

  • Noncanonical mode. We also turn off processing of the signal-generating characters (ISIG) and the extended input character processing (IEXTEN). Additionally, we disable a BREAK character from generating a signal, by turning off BRKINT.
  • Echo off.
  • We disable the CR-to-NL mapping on input (ICRNL), input parity detection (INPCK), the stripping of the eighth bit on input (ISTRIP), and output flow control (IXON).
  • Eight-bit characters (CS8), and parity checking is disabled (PARENB).
  • All output processing is disabled (OPOST).
  • One byte at a time input (MIN = 1, TIME = 0).
#include "apue.h"

static void sig_catch(int signo)
{
    printf("signal caught\n");
    tty_reset(STDIN_FILENO);
    return 0;
}

int main(void)
{
    int i;
    char c;

    if(signal(SIGINT, sig_catch)==SIG_ERR)
        err_sys("signal(SIGINT) error");
    if(signal(SIGQUIT,sig_catch)==SIG_ERR)
        err_sys("signal(SIGQUIT) error");
    if(signal(SIGTERM, sig_catch)==SIG_ERR)
        err_sys("signal(SIGTERM) error");
    
    if(tty_raw(STDIN_FILENO)<0)
        err_sys("tty_raw error");
    printf("Enter raw mode characters, terminate with DELETE\n");
    while((i=read(STDIN_FILENO, &c, 1))==1)
    {
        if((c&=255)==0177)
            break;
        printf("%o\n",c);
    }
    if(tty_reset(STDIN_FILENO)<0)
        err_sys("tty_reset error");
    if(i<=0)
        err_sys("read error");
    if(tty_cbreak(STDIN_FILENO)<0)
        err_sys("tty_cbreak error");
    printf("\nEnter cbreak mode characters, terminate with SIGINT\n");
    while((i=read(STDIN_FILENO, &c, 1))==1)
    {
        c&=255;
        printf("%o\n",c);
    }

    if(tty_reset(STDIN_FILENO)<0)
        err_sys("tty_reset error");
    if(i<=0)
        err_sys("read error");

    return 0;
}

Test raw and cbreak terminal modes

Terminal Window Size

Most UNIX systems provide a way to keep track of the current terminal window size and to have the kernel notify the foreground process group when the size changes. The kernel maintains a winsize structure for every terminal and pseudo terminal:

struct winsize {
	unsigned short ws_row; /* rows, in characters */
	unsigned short ws_col; /* columns, in characters */
	unsigned short ws_xpixel; /* horizontal size, pixels (unused) */
	unsigned short ws_ypixel; /* vertical size, pixels (unused) */
};

The rules for this structure are as follows:

  • We can fetch the current value of this structure using an ioctl (Section 3.15) of TIOCGWINSZ.
  • We can store a new value of this structure in the kernel using an ioctl of TIOCSWINSZ. If this new value differs from the current value stored in the kernel, a SIGWINCH signal is sent to the foreground process group.
  • Other than storing the current value of the structure and generating a signal when the value changes, the kernel does nothing else with this structure. Interpreting the structure is entirely up to the application.
#include "apue.h"
#include <termios.h>
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif

static void pr_winsize(int fd)
{
    struct winsize size;

    if(ioctl(fd, TIOCGWINSZ,(char *)&size)<0)
        err_sys("TIOCGWINSZ error");
    printf("%d rows, %d columns\n", size.ws_row, size.ws_col);
}

static void sig_winch(int signo)
{
    printf("SIGWINCH received\n");
    pr_winsize(STDIN_FILENO);
}

int main(void)
{
    if(isatty(STDIN_FILENO)==0)
        return 1;
    if(signal(SIGWINCH, sig_winch)==SIG_ERR)
        err_sys("signal error");
    pr_winsize(STDIN_FILENO);
    for( ; ; )
        pause();
}

Print window size

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值