如何製作對按下按鈕做出反應的程序(例如“q”上的“更多”)

如何製作對按下按鈕做出反應的程序(例如“q”上的“更多”)

我試圖了解 pg\more\less 實用程式是如何運作的。例如,cat somebigfile |更多的。現在有更多互動模式。他的fd表是:0(從cat讀取管道)1(stdout)2(stderr)

我可以在 3 fd 上開啟 /dev/tty 並從中讀取命令。但更多的是無需按回車鍵即可執行某些操作。在linux上我可以使用ncurses。要在 Solaris 上實現它,我需要了解什麼?

答案1

基本思想是從輸入中 read() 一個字元;看http://bazaar.launchpad.net/~vcs-imports/util-linux-ng/trunk/view/head:/text-utils/more.c#L1908作為一個例子(我透過谷歌搜尋結果發現https://stackoverflow.com/questions/9854267/implementing-the-more-unix-utility-command):

int readch(void)
{
    unsigned char c;

    errno = 0;
    if (read(fileno(stderr), &c, 1) <= 0) {
        if (errno != EINTR)
            end_it(0);
        else
            c = otty.c_cc[VKILL];
    }
    return (c);
}

相關內容