In that year, I learned Linux C-underlying terminal programming instances step by step.

Source: Internet
Author: User


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.

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.