Linux uses the tcgetattr function and the tcsetattr function control terminal

Source: Internet
Author: User
Tags control characters uppercase character

To facilitate obtaining and modifying terminal parameters through a program, Linux also provides tcgetattr and tcsetattr functions. Tcgetattr is used to obtain terminal-related parameters, while the tcsetattr function is used to set terminal parameters. The specific information of these two functions is shown in table 6.2.

Table 6.2 tcgetattr and tcsetattr Functions

Header file

<Termios. h>

<Unistd. h>

Function Form

Int tcgetattr (int fd, struct termios * termios_p );

Int tcsetattr (int fd, int optional_actions, const struct termios * termios_p );

Return Value

Successful

Failed

Whether to set errno

0

−1

Yes

Description: The tcgetattr function is used to obtain terminal-related parameters. The FD parameter is the file descriptor of the terminal, and the returned results are stored in the termios struct. This struct generally includes the following members:

Tcflag_t c_iflag;
Tcflag_t c_oflag;
Tcflag_t c_cflag;
Tcflag_t c_lflag;
Cc_t c_cc [NCCs];

Its significance is as follows:

C_iflag: indicates the input mode and controls the input mode of the terminal. The specific parameters are shown in table 6.3.

Table 6.3 c_iflag parameter table

Key Value

Description

Ignbrk

Ignore break key input

Brkint

If ignbrk is set, the break key input is ignored. If brkint is set, SIGINT interruption occurs.

Ignpar

Ignore parity errors

Parmrk

Incorrect ID parity

Inpck

Allow input parity

Istrip

Remove 8th characters

Inlcr

Convert input NL (line feed) to Cr (Press ENTER)

Igncr

Ignore input carriage return

Icrnl

Convert the input carriage return to line feed (if igncr is not set)

Iuclc

Converts an input uppercase character to a lowercase character (non-POSIX)

Ixon

Allow control of the Xon/xoff stream during Input

Ixany

Output that will restart and stop when any character is entered

Ixoff

Allow control of the Xon/xoff stream during Input

Imaxbel

When the input queue is full, the system starts to ring the bell. in Linux, the system uses this parameter and considers that this parameter has always been set.

C_oflag: the output mode flag that controls the terminal output mode. The specific parameters are shown in table 6.4.

Table 6.4 c_oflag Parameters

Key Value

Description

Opost

Output after processing

Olcuc

Converts lowercase characters to uppercase (non-POSIX)

Onlcr

Convert the input NL (line feed) to Cr (Press ENTER) and NL (line feed)

Ocrnl

Convert input Cr (Press ENTER) to Nl (line feed)

Onocr

The first line does not output a carriage return.

Onlret

No carriage return

Ofill

Send fill characters to delay terminal output

Ofdel

Use the del of the ASCII code as the filling character. If this parameter is not set, the filling character will be NUL ('/0') (non-POSIX)

Nldly

Line feed output delay, which can be nl0 (no delay) or nl1 (0.1 s delay)

Crdly

Carriage Return delay, value range: Cr0, CR1, CR2

Tabdly

Latency of horizontal tab output. Values: tab0, tab1, tab2, and tab3

Bsdly

Space output delay, which can be bs0 or BS1

Vtdly

Vertical tab output latency, which can be vt0 or vt1

Ffdly

Page feed delay, which can be ff0 or ff1

C_cflag: Control Mode flag that specifies the hardware control information of the terminal. The specific parameters are shown in table 6.5.

Table 6.5 c_oflag Parameters

Key Value

Description

Cbaud

Baud rate (4 + 1 bits) (non-POSIX)

Cbaudex

Additional baud rate (1 bit) (non-POSIX)

Csize

Length, value range: cs5, CS6, cs7, or cs8

Cstopb

Set two stop bits

Cread

Use Receiver

Parenb

Use parity

Parodd

Use parity for input and even for output

Hupcl

Suspended when the device is disabled

Clocal

Ignore modem line status

Crtscts

Use RTS/CTS Stream Control

C_lflag: indicates the local mode flag and controls the terminal editing function. The specific parameters are shown in table 6.6.

Table 6.6 c_lflag Parameters

Key Value

Description

Isig

When intr, quit, susp, or dsusp is input, corresponding signals are generated.

Icanon

Use standard input mode

Xcase

When both icanon and xcase are set, the terminal only uses uppercase letters. If only xcase is set, the input characters are converted to lowercase characters, unless the characters use escape characters (non-POSIX, and Linux does not support this parameter)

Echo

Show input characters

Echoe

If icanon is set at the same time, erase deletes the entered characters and werase deletes the entered words.

Echok

If icanon is set at the same time, kill will delete the current row

Echonl

If icanon is set at the same time, even if ECHO is not set, the line break is still displayed.

Echoprt

If echo and icanon are both set, the printed characters (non-POSIX) are deleted)

Tostop

Send sigttou signal to the background

C_cc [NCCs]: A control character used to save special characters in the terminal driver, such as the input Terminator. C_cc defines the control characters shown in table 6.7.

Table 6.7 control characters supported by c_cc

Macro

Description

Macro

Description

Vintr

Interrupt characters

Veol

Additional end-of-file characters

Vquit

Quit character

Vtime

Timeout value for reading in non-standard mode

Verase

Erase characters

Vstop

Stop character

Vkill

Kill characters

Vstart

Start character

Veof

End-of-file character

Vsusp

Suspend character

Vmin

Minimum number of characters for reading in non-standard mode

 

 

The tcsetattr function is used to set terminal parameters. The FD parameter is the terminal file descriptor that is opened. The optional_actions parameter is used to control the time when the modification takes effect. The termios_p struct stores the parameters to be modified.
Optional_actions can take the following values.
Tcsanow: changes the attributes immediately after data transmission is completed.
Tcsadrain: the attribute is changed only after all data transmission ends.
Tcsaflush: the attribute is changed only when the input and output buffer is cleared.

Error message:
Ebadf: invalid file descriptor.
Eintr: the call to the tcsetattr function is interrupted by the signal.
Einval: The optional_actions parameter uses an invalid value or an invalid value in the termios parameter.
Enctty: Non-terminal file descriptor.

Instance drill:
The program p6.2.c changes the terminal control character from Ctrl + D to Ctrl + G ". First, the program calls the tcgetattr function to obtain the termios information of the standard input, and changes the c_cc [veof] control character in the termios struct to 0x07 (CTRL + G). Then, use the tcsetattr function to set the modified termios parameter to the terminal. The Code is as follows:

// P6.2.c terminal control character modification example # include <stdio. h> # include <termios. h> # include <unistd. h> # include <errno. h>

Int main (void) {// term is used to store the obtained terminal parameter information struct termios term; int err;

// Obtain the standard input terminal parameters and save the obtained information in the term variable if (tcgetattr (stdin_fileno, & term) =-1) {perror ("cannot get standard input description"); return 1 ;}

// Modify the ending control character of the obtained terminal information. Term. c_cc [veof] = (cc_t) 0x07;

// Use the tcsetattr function to set the modified terminal parameters to the standard input. // err is used to save the call result of the function. Err = tcsetattr (stdin_fileno, tcsaflush, & term );

// If err is-1 or an eintr error occurs (function execution is interrupted by signal), // provide the relevant error message if (ERR =-1 & err = eintr) {perror ("failed to change EOF character"); return 1 ;}

Return 0 ;}

Use GCC to compile the p6.2.c program and obtain the executable program p6.2. Before executing the p6.2 program, press Ctrl + D to end the terminal. After executing the p6.2 program, pressing "Ctrl + D" has no effect, and entering "Ctrl + G" implements the original "Ctrl + D" function.

Address: http://blog.csdn.net/xuefu2008/article/details/4662368

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.