tcgetattr function and tcsetattr function __ function

Source: Internet
Author: User
Tags control characters lowercase posix

Linux also provides tcgetattr functions and TCSETATTR functions to facilitate the acquisition and modification of terminal parameters through programs. Tcgetattr is used to get the relevant parameters for the terminal, and the TCSETATTR function is used to set 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 a terminal file descriptor, and the returned result is stored in the Termios structure body, 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 terminal input mode, specific parameters as shown in table 6.3.

Table 6.3 C_iflag Parameter table

Key value

Description

Ignbrk

Ignore break key input

Brkint

If the input with the Ignbrk,break key is set to be 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 the character

Inlcr

Converts the entered NL (newline) 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

Control of Xon/xoff flow when input is allowed

Ixany

Enter any character to reboot the stopped output

Ixoff

Control of Xon/xoff flow when input is allowed

Imaxbel

When the input queue starts ringing when it is full, Linux uses this parameter instead of the argument that it 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

Description

Opost

After processing output

Olcuc

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

Onlcr

Converts the input NL (newline) to CR (carriage return) and NL (linefeed).

Ocrnl

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

Onocr

The first line does not output a return character

Onlret

Do not output carriage return characters

Ofill

Send fill characters to delay terminal output

Ofdel

Fills the character with the ASCII key, and if this argument is not set, the padding character is nul (' ") (non-POSIX)

nldly

NewLine output delay, can take NL0 (without delay) or NL1 (delay 0.1s)

crdly

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

tabdly

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

bsdly

Space output delay, can take BS0 or BS1

vtdly

Vertical tab Output delay, you can take VT0 or VT1

ffdly

Change page delay, can take FF0 or FF1



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

Key value

Description

Cbaud

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

Cbaudex

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

CSize

Character length, which ranges from CS5, CS6, CS7, or CS8

Cstopb

Set two stop bits

Cread

Using the receiver

Parenb

Using parity

Parodd

Use parity for input, parity on output

Hupcl

Suspend 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

Description

Isig

When input intr, QUIT, Susp or DSUSP, the corresponding signal is generated

Icanon

Use standard input mode

Xcase

When Icanon and Xcase are set simultaneously, the terminal uses only 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

Display input characters

Echoe

If Icanon is set at the same time, erase will delete the entered characters and Werase will delete the entered words

Echok

If the Icanon is set at the same time, kill will delete 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 will be deleted (non-POSIX)

Tostop

Send Sigttou signal to background output



C_CC[NCCS]: A control character that holds special characters in a terminal driver, such as an input terminator. The control characters as shown in table 6.7 are defined in C_CC.

Table 6.7 C_CC supported control characters

Macro

Description

Macro

Description

Vintr

Interrupt characters

Veol

Additional End-of-file characters

Vquit

Quit characters

Vtime

Timeout when 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 non-canonical mode reads



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 takes effect, and the parameters to be modified are saved in the termios_p of the struct body.
Optional_actions can take the following values.

Tcsanow: Change the attribute immediately after the data transfer is complete.
Tcsadrain: Wait for all data transfer to end before changing properties.
Tcsaflush: Clears the input-output buffer before changing the property.

Error message:
EBADF: Illegal file descriptor.
The EINTR:TCSETATTR function call was interrupted by a signal.
Einval: Parameter optional_actions uses an illegal value, or an illegal value is used in Parameter Termios.
Enctty: A non-terminal file descriptor.

Instance Walkthrough:
By modifying the terminal control characters, the program p6.2.c the terminal input terminator from "Ctrl+d" to "ctrl+g". First, the program calls the TCGETATTR function to obtain the standard input Termios information, modifies the c_cc[veof in Termios structure to 0x07 (i.e. ctrl+g); then, use tcsetattr The function sets 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;

Obtains the standard input terminal parameter, saves the obtained 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 results 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 the relevant 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 executing p6.2 program, press "Ctrl+d" to lose function, and input "ctrl+g" realize original "ctrl+d" function

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.