用于命令替换时,ncurses程序无法正常工作

ncurses program not working correctly when used for command substitution

本文关键字:常工作 工作 替换 命令 程序 ncurses 用于      更新时间:2023-10-16

我希望能够通过命令替换来调用基于ncurses的程序,这样它的输出就可以作为命令行参数传递给另一个程序。例如,当程序不进入curses模式时,在bash中很容易做到这一点:

echo $(pwd) # should be the same as just calling pwd alone

当我尝试用我的程序(其代码如下)执行此操作时,永远不会进入curses模式,也永远不会打印字符串"test"。进入curses模式很重要,因为理论上,用户会在这里以某种方式操作最终打印到stdout的字符串(现在该字符串只是静态的)。

echo $(./a.out) # just prints an empty line

进入curses模式后,我的程序在正常运行时会返回字符串"这不是测试","测试"打印到屏幕上,用户按下一个键。

./a.out # normal run

以下是违规代码:

// test.cpp
#include <ncurses.h>
#include <iostream>
using namespace std;
/* get curses up and running */
void init() {
    initscr(); // start curses mode, might clear screen
    raw(); // disable line buff, and C-z/C-c won't gen sigals; see cbreak()
    keypad(stdscr, TRUE); // enable arrow keys and function keys
    noecho(); // don't echo chars user types
}
/* shut curses down */
void end() {
   endwin(); // end curses mode
}
int main()
{
    init();
    printw("test");
    getch();
    end();
    cout << "this is not a test" << endl;
    return 0;
}

我用这个命令编译:

g++ test.cpp -lcurses

谢谢你的帮助!

这里有一个简单的解决方案,使用newterm:

#define _XOPEN_SOURCE
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
    // start curses mode
    SCREEN* s = NULL;
    FILE* out = stdout;
    if(!isatty(fileno(stdout))) {
        out = fopen("/dev/tty", "w");
        // Should really test `out` to make sure that worked.
        setbuf(out, NULL);
    }
    // Here, we don't worry about the case where stdin has been
    // redirected, but we could do something similar to out
    // for input, opening "/dev/tty" in mode "r" for in if necessary.
    s = newterm(NULL, out, stdin);
    printw("test");
    getch();
    endwin(); // end curses mode
    delscreen(s);
    puts("/home/matt");
    return 0;
}