Terminal I/O (on), terminal I/O (
I. Summary
Terminal I/O works in two different ways:
- Canonicalized input. In this mode, terminal input is processed in the unit of action. For each read requirement, the Terminal Driver returns a maximum of one row.
- Non-standard input processing. Input characters are not assembled in behavior units
If no special processing is performed, the default method is standard. The vi editing program uses an nonstandard mode because its command is composed of one or several characters that are not terminated by a linefeed.
POSIX.1 defines 11 special input characters. 9 of them can be changed.
The terminal device is generally controlled by the Terminal Driver in the kernel. Each terminal device has an input queue and an output queue. For details, see:
The following points must be described for this diagram:
- If you need to send back, there is an implicit connection between the input queue and the output queue.
- The length of the Input Queue MAX_INPUT is a finite value. When the input queue of a specific device is filled up, the implementation varies by system, and most UNIX systems send Bell characters.
- The figure shows no other input limit MAX_CANON, which is the maximum number of bytes in a standard input row.
- Although the output queue is usually of a limited length, the program cannot access constants that define its length. This is because when the output queue is to be filled up, the kernel enables the write process to sleep until there is available space in the write queue, so the program does not need to care about the length of the queue.
Most UNIX systems perform standard processing in a module called terminal line discipline. It is located between the kernel class Read and Write Functions and the actual device driver. For details, see
All terminal device features that we can detect and change are included in the termios structure. This structure is defined in the header file <termios. h>.
struct termios { tcflag_t c_iflag; /* input flag */ tcflag_t c_oflag; /* output flags */ tcflag_t c_cflag; /* control flags */ tcflag_t c_lflag; /* local flags */ cc_t c_cc[NCCS]; /* control characters */};
In a rough sense, the input flag is used by the terminal device driver to control the input features (excluding the input byte 8th bits, allowing input parity, etc.), and the output flag controls the output features (execute output processing, map the new line to CR/LF, etc.), the Control Mark affects the serial line of the RS-232 (ignore the modem status line, one or two stop bits of each character, etc ), the local flag affects the interface between the driver and the user (The on or off of the return, the erasers that can be tried, the signals generated by the terminal, and the control and stop signals output to the background job ).
The length of Type tcflag_t is to keep each flag value. It is often defined as unsigned long. The c_cc array contains all the special characters that can be changed. NCCS is the length of the array, POSIX.1 is defined as 11, slightly different implementations. The length of the cc_t type is sufficient to retain each special character, typically unsigned char.
The following table lists all terminal signs that can be changed to affect the characteristics of the terminal device.
The following table lists the functions that POSIX.1 defines to operate on terminal devices.
For Terminal devices, POSIX.1 does not use ioctl, but uses the 12 functions listed in the table above. The reason for doing so is: For the ioctl function of the terminal device, the Data Type of the last parameter varies with the execution action.
The relationships between the 12 functions in the table are as follows:
Ii. Special input characters
POSIX.1 defines 11 characters for special processing during input. SVR4 has 6 additional special characters, and 4.3 + BSD has 7 Additional characters. The following table lists these characters.
Of the 11 special characters, 9 can be changed to any value. The linefeed \ n and carriage return \ r cannot be changed. For modification, you only need to change the corresponding items of the c_cc array in the termios structure. The elements in this array are referenced using names as subscripts. Each name starts with the letter V.
3. Obtain and set terminal properties
Use the tcgetattr and tcsetattr functions to obtain/set termios. In this way, you can detect and modify various terminal selection logos and special characters so that the terminal can operate as needed.
# Include <termios. h> int tcgetattr (int filedes, struct termios * termpptr); int tcsetattr (int filedes, int opt, const struct termios * termpptr); \ return value: Success is 1, error:-1
Because these two functions only operate on the terminal device, if filedes does not reference a terminal device, an error is returned, and the error is set to ENOTTY.
The opt parameter of tcsetattr allows us to specify when new terminal properties will take effect. Opt can be specified as one of the following constants:
- TCSANOW changes occur immediately
- TCSADRAIN sends all the output before the change occurs. Use this option if you change the output parameter.
- TCSAFLUSH sends all the output before the change occurs. Furthermore, when the change occurs, all the input data that has not been read is deleted (flushed ).
The returned values of the tcsetattr function are prone to confusion. If any requested action is executed, it returns 0 (indicating success) even if it fails to perform all required actions ). If the function returns 0, it is necessary to call tcgetattr to obtain the actual terminal property and compare it with the expected terminal property to determine whether the modification is successful.