tcgetattr function and tcsetattr function control terminal

Source: Internet
Author: User
Tags control characters

6.4.4 using the Tcgetattr function and the TCSETATTR function to control the terminal

To facilitate the process of obtaining and modifying terminal parameters, Linux also provides tcgetattr functions and tcsetattr functions. The tcgetattr is used to obtain the relevant parameters of the terminal, while the TCSETATTR function sets the terminal parameters. The specific information for these two functions is shown in Table 6.2.

Table 6.2 tcgetattr functions 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

Success

Failed

Whether to set errno

0

−1

Is

Description: The TCGETATTR function is used to obtain terminal-related parameters. The parameter fd is the file descriptor of the terminal, and the returned result is stored in the Termios structure, which 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 specific meaning is as follows.
?
C_iflag: Input mode flag, control the terminal input method, the specific parameters as shown in table 6.3.

Table 6.3 C_iflag Parameter table

Key Value

Speak clearly

Ignbrk

Ignore break key input

Brkint

If the input setting of the Ignbrk,break key is ignored, if Brkint is set, a SIGINT interrupt is generated

Ignpar

Ignore parity errors

Parmrk

Identify parity errors

Inpck

Allow input parity

Istrip

Remove the 8th bit of a character

Inlcr

Converts the input NL (line break) to CR (carriage return)

Igncr

Ignore input carriage return

Icrnl

Converts the entered carriage return to a newline (if IGNCR is not set)

Iuclc

Convert uppercase characters entered into lowercase characters (non-POSIX)

Ixon

Allow control of xon/xoff flow at input

Ixany

Enter any characters that will restart the stopped output

Ixoff

Allow control of xon/xoff flow at input

Imaxbel

When the input queue starts ringing when it is full, Linux uses this parameter instead to assume that the parameter is always set

C_oflag: Output mode flag, control the terminal output mode, the specific parameters as shown in Table 6.4.

Table 6.4 C_oflag Parameters

Key Value

Speak clearly

Opost

Post-processing output

Olcuc

Converts the input lowercase characters to uppercase characters (non-POSIX)

Onlcr

Converts the input NL (line break) to CR (carriage return) and NL (line break)

Ocrnl

Converts the input CR (carriage return) to NL (line break)

Onocr

The first line does not output a carriage return character

Onlret

Do not output carriage return characters

Ofill

Send padding characters to delay terminal output

Ofdel

With the ASCII code del as the fill character, if the parameter is not set, the padding character will be nul (' + ') (non-POSIX)

nldly

NewLine output delay, NL0 (no delay) or NL1 (delay of 0.1s) can be taken

crdly

Carriage return delay, value range: CR0, CR1, CR2, and CR3

tabdly

Horizontal tab output delay, value range: TAB0, TAB1, TAB2, and TAB3

bsdly

Space output delay, can take BS0 or BS1

vtdly

Vertical tab Output delay, can take VT0 or VT1

ffdly

Page change delay, can take FF0 or FF1

C_cflag: Control mode flag, specify the terminal hardware control information, the specific parameters as shown in table 6.5.

Table 6.5 C_oflag Parameters

Key Value

Speak clearly

Cbaud

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

Cbaudex

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

CSIZE

Character length, with values ranging from CS5, CS6, CS7, or CS8

Cstopb

Set two stop bits

Cread

Using receivers

Parenb

Using the Parity check

Parodd

Using parity for input, using even parity for output

Hupcl

Hang when device is turned off

Clocal

Ignore Modem line Status

Crtscts

Using Rts/cts flow control

C_lflag: Local mode flag, control the terminal editing function, the specific parameters as shown in Table 6.6.

Table 6.6 C_lflag Parameters

Key Value

Speak clearly

Isig

Generates a corresponding signal when entering Intr, QUIT, Susp, or Dsusp

Icanon

Use standard input mode

Xcase

In cases where Icanon and xcase are set simultaneously, the terminal only uses uppercase. If only xcase is set, the input character is converted to lowercase characters unless the character uses an escape character (not POSIX, and Linux does not support this parameter)

ECHO

Show input characters

Echoe

If Icanon is set at the same time, erase will delete the input characters, werase will delete the input words

Echok

If Icanon is set at the same time, kill deletes the forward

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 set simultaneously, the printed characters (non-POSIX) will be deleted

Tostop

Send Sigttou signal to background output

C_CC[NCCS]: A control character used to hold special characters in the terminal driver, such as an input terminator. The control characters shown in table 6.7 are defined in C_CC.

Table 6.7 C_CC supported control characters

Macro

Speak clearly

Macro

Speak clearly

Vintr

Interrupt characters

Veol

Additional End-of-file characters

Vquit

Quit character

Vtime

Timeout for non-canonical mode reads

Verase

Erase characters

Vstop

Stop character

Vkill

Kill character

Vstart

Start character

Veof

End-of-file characters

Vsusp

Suspend characters

VMIN

Minimum number of characters when reading non-canonical mode

The TCSETATTR function is used to set the relevant parameters for the terminal. The parameter FD is an open terminal file descriptor, and the parameter optional_actions is used to control when the modification is in effect, while the struct termios_p holds the parameters to be modified.
Optional_actions can take the following values.
?
Tcsanow: Changes the properties immediately after the data transfer is complete.
Tcsadrain: Wait for all data transfers to end before changing the properties.
Tcsaflush: Clears the input and output buffers before changing the properties.

Error message:
EBADF: Illegal file descriptor.
The EINTR:TCSETATTR function call was interrupted by the signal.
EINVAL: The parameter optional_actions used an illegal value, or an illegal value was used in the parameter Termios.
Enctty: A non-terminal file descriptor.

Example Walkthrough:
Program p6.2.c by modifying the terminal control character, the terminal input terminator from "Ctrl+d", modified to "Ctrl+g". First, the program calls the TCGETATTR function to obtain the Termios information of the standard input, modifying the c_cc[veof] control character in the Termios struct to 0x07 (that is, ctrl+g); Use the TCSETATTR function to set the modified Termios parameter to the terminal. The specific code looks like this:

P6.2.C Modify Terminal Control Word Descriptor example
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

int main (void) {
Term used to store the obtained terminal parameter information
struct Termios term;
int err;

Get the terminal parameters of the standard input and save the information in the term variable
if (tcgetattr (stdin_fileno,&term) ==-1) {
Perror ("Cannot get standard input description");
return 1;
}

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

Use the TCSETATTR function to set the modified terminal parameter to standard input
Err is used to save the result after a function call
Err=tcsetattr (Stdin_fileno,tcsaflush,&term);

If err is-1 or a EINTR error occurs (function execution is interrupted by a signal),
Give a related error message
if (err==-1 && err==eintr) {
Perror ("Failed to change EOF character");
return 1;
}

return 0;
}

Compile the P6.2.C program with GCC and get the executable program named p6.2. Press "Ctrl+d" to end the terminal before executing the p6.2 program. After the execution of the p6.2 program, press "Ctrl+d" lost its role, and input "ctrl+g" to achieve the original "ctrl+d" function.

tcgetattr function and tcsetattr function control terminal

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.