首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Linux C 編程:終端機能力查詢與 curses 庫實戰

     編輯:關於C語言

Linux 終端機處理架構

  • 透過串列介面連接控制台
  • 廠商命令集不一,需通用抽象層
  • terminfo 能力資料庫描述各終端機功能與調用序列
  • 偽終端:讓程式模擬真實終端機與系統互動

查詢終端機能力程式

#include <stdlib.h>
#include <stdio.h>
#include <term.h>
#include <curses.h>

#define NUMCAPS 3

int main(void) {
    char *boolcaps[] = {"am", "bce", "km"};
    char *numcaps[]  = {"cols", "lines", "colors"};
    char *strcaps[]  = {"cup", "flash", "hpa"};
    int retval;
    char *buf;

    if (setupterm(NULL, fileno(stdin), NULL) != OK) {
        perror("setupterm");
        exit(EXIT_FAILURE);
    }

    for (int j = 0; j < NUMCAPS; j++) {
        retval = tigetflag(boolcaps[j]);
        printf("%s %s\n", boolcaps[j], retval ? "supported" : "unsupported");
    }
    for (int j = 0; j < NUMCAPS; j++) {
        retval = tigetnum(numcaps[j]);
        printf("%s %s%d\n", numcaps[j], retval == -1 ? "unsupported: " : "=", retval);
    }
    for (int j = 0; j < NUMCAPS; j++) {
        buf = tigetstr(strcaps[j]);
        printf("%s %s\n", strcaps[j], buf ? "supported" : "unsupported");
    }
    return 0;
}

編譯與執行

gcc test.c -o test -lcurses
./test

需連結 libcurses。程式查詢布林、數值、字串三類終端機能力並輸出結果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved