This series of articles written by muge0913, reproduced please note the Source: http://blog.csdn.net/muge0913/article/details/7341664
The terminal processing in Linux is a very large system, which needs to handle many different types of devices and requirements. The involved content includes modem, terminal simulation, and Pseudo Terminal.
The Linux system processing terminal communicates with the system and runs the program through the console connected through a serial interface. As more and more manufacturers are involved in terminal production, and each vendor designs their own command sets for their own terminals, there is a need for a method to generalize terminal access. In Linux, a capability database terminfo is used to describe the capabilities of each terminal and call these functions.
In some cases, programmers want to provide Terminal Driver functions for devices that are not terminals. In this case, pseudo terminals are required. Pseudo terminals provide a way for programmers to pretend to be a real terminal and 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 3int 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: When compiling this program, you must manually connect libcurses. A. The compilation method is GCC test. C-o Test-lcurses.