【发布时间】:2021-02-07 17:31:42
【问题描述】:
我的 Ncurses 应用程序正在循环输入键盘并将输出打印到屏幕上。当我停止我的应用程序 (ctrl+z) 并稍后恢复它时,我注意到输入缓冲区有时包含不需要的字符。所以我想用flushinp()来丰富ncurses默认提供的SIGCONT处理程序,简化代码如下:
#include <csignal>
#include <curses.h>
#include <stdlib.h>
#include <fstream>
struct sigaction oldact, newact;
extern "C" void sigContHandler(int sig)
{
std::ofstream of("sc", std::ofstream::app);
of << "Handling cont " << std::endl;
flushinp();
//Call old handler, but crash as it is always 0
(oldact.sa_handler)(sig);
}
int main(void)
{
initscr();
cbreak();
noecho();
clear();
newact.sa_handler = sigContHandler;
newact.sa_flags = 0;
sigemptyset (&newact.sa_mask);
sigaction(SIGCONT, &newact, &oldact);
int c = 0;
while (c = getch()) {
if (c == 'q')
break;
mvaddch(0, 0, c);
}
refresh();
endwin();
exit(0);
}
我不能调用旧的 SIGCONT 处理程序,因为它始终为 0。显然,如果我不向链信号处理程序添加代码,则会成功调用此默认处理程序。我不明白为什么我不能调用旧处理程序,我做错了什么?
【问题讨论】:
-
是什么阻止您在调用
sa_handler之前检查它是否为NULL?这是系统在尝试调用处理程序之前会执行的操作。