I'm working on a problem similar to Readline: Get a new prompt on SIGINT, but the answers given for that question don't completely satisfy the problem given.
I'm using the regular (non-callback) version of GNU readline and am writing a signal handler to handle SIGINT in C. The handler should clear the current readline buffer and display a clean prompt on a new line.
My primary concern at the moment is that I can't find any confirmation if the functions I'm calling in my signalHandler function are safe to use. The only example I've seen of readline signal handling is in the readline docs using the callback alternate interface.
The other option is to use siglongjmp as answered in the question I linked above. The problem with this solution is that it doesn't offer a way to free the memory allocated line buffer after a signal call. Raising an interrupt signal will call readline() again, but will not free the line allocated from the previous call to readline. Free() is not a signal safe function.
Has anybody written a signal safe SIGINT handler in C with the readline library using the regular interface and not the callback interface?
Sample of my current signal handler:
void signalHandler(int sig) {
if (inReadline==1)
{
rl_reset_line_state(); // Resets the display state to a clean state
rl_cleanup_after_signal(); // Resets the terminal to the state before readline() was called
rl_replace_line("",0); // Clears the current prompt
rl_crlf(); // Moves the cursor to the next line
rl_redisplay(); // Redisplays the prompt
}
}