I am doing a parser getting characters until EOF
, I am handling the signals SIGTERM
, SIGINT
, SIGTSTP
and SIGQUIT
. So when I send a signal with Ctrl+C (for example to SIGINT
) the signal handler prints Ctrl+C and then must continue, but is not continuing.
I figured out that after each Ctrl signal, an EOF
is filling the buffer, but I do not know how to get rid of it. I tried to do while(getc(stdin) != EOF);
inside the handler, but the parser behave normally, "eating" every character typed until EOF
.
How can I receive a Ctrl signal without messing up my stdin
?
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
static void nsh_signal_handler(int code)
{
char c;
switch(code)
{
case SIGQUIT: c = '\\'; break;
}
fprintf(stdout, "I will do nothing about your Ctrl - %c\n", c);
}
int main(void)
{
int c;
struct sigaction s;
s.sa_flags = 0;
s.sa_handler = nsh_signal_handler;
sigfillset(&s.sa_mask);
sigaction(SIGQUIT, &s, NULL);
do
{
c = getc(stdin);
printf("[%c][%d]\n", c, c);
}
while(c != EOF);
return 0;
}
The code above show this error.