The terminal processing of Linux systems is a very large system that needs to deal with many different types of devices and requirements. The content includes: modem, terminal emulation, pseudo terminal and so on.
The way the Linux system handles the terminal is to communicate with the system and run the program through a serial interface connected to the console. Because more and more manufacturers are involved in the production of terminals, and each manufacturer for their own terminal design their own command set, so there needs to be a way to the terminal access to the general processing. The Linux system uses a capability database terminfo to describe the capabilities of each terminal and the methods by which these functions are invoked.
In some cases, the programmer wants to be able to provide terminal driver functionality for certain devices that are not terminals, and a pseudo terminal is required. Pseudo-Terminals provide a way for programmers to pretend to be a real terminal and to interact well with the system.
The function of the following program is to query and print some capabilities of the current terminal
#include <stdlib.h> #include <stdio.h> #include <term.h> #include <curses.h>
#define NUMCAPS 3 int main () {int J;
int retval = 0;
char * BUF;
Char *boolcaps[numcaps] = {"AM", "BCE", "km"};
Char *numcaps[numcaps] = {"Cols", "lines", "colors"};
Char *strcaps[numcaps] = {"cup", "Flash", "hpa"};
if (Setupterm (Null,fileno (stdin), NULL)!= OK) {perror ("setupterm ()");
Exit (Exit_failure);
for (j = 0;j<numcaps;++j) {retval = Tigetflag (Boolcaps[j]);
if (retval = = FALSE) printf ("%s unsuported\n", Boolcaps[j]);
else printf ("%s suported\n", Boolcaps[j]);
for (j = 0;j<numcaps;++j) {retval = Tigetnum (Numcaps[j]);
if (retval = ERR) printf ("%s unspported\n", Numcaps[j]); else printf ("%s is%d\ n ", numcaps[j],retval);
for (j = 0;j<numcaps;++j) {buf = Tigetstr (Strcaps[j]);
if (buf = NULL) printf ("%s unspported\n", Strcaps[j]);
else printf ("%s is%d\n", strcaps[j],buf[0]); }
}
Note: You must manually connect LIBCURSES.A when compiling this program, which is compiled in GCC test.c-o test-lcurses
View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm