I am unable to have an application in windows that uses readline and does not exit with ctrl+c
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h> /* for free() */
#include <unistd.h> /* for usleep() */
#include <windows.h>
char running = 1;
// The function that'll get passed each line of input
void my_rlhandler(char* line);
void my_rlhandler(char* line){
if(line==NULL){
// Ctrl-D will allow us to exit nicely
printf("\nNULLBURGER\n");
running = 0;
}else{
if(*line!=0){
// If line wasn't empty, store it so that uparrow retrieves it
add_history(line);
}
printf("Your input was:\n%s\n", line);
free(line);
}
}
BOOL CtrlHandler(DWORD fdwCtrlType);
int main(int argc, char *argv[])
{
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE))
{
printf("the control handler is installed.\n");
}
else
{
printf("ERROR: could not set control handler.\n");
}
const char *prompt = "WOOP> ";
// Install the handler
rl_callback_handler_install(prompt, (rl_vcpfunc_t*) &my_rlhandler);
// Enter the event loop (simple example, so it doesn't do much except wait)
running = 1;
while(running){
usleep(10000);
rl_callback_read_char();
};
printf("\nEvent loop has exited\n");
// Remove the handler
rl_callback_handler_remove();
return 0; // happy ending
}
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
/* handle the CTRL-C signal */
case CTRL_C_EVENT:
printf("CTRL-C event\n");
Beep(750, 300);
return TRUE;
/* handle the CTRL-BREAK signal */
case CTRL_BREAK_EVENT:
printf("CTRL-BREAK event\n");
Beep(900, 200);
return TRUE;
/* handle the CTRL-CLOSE signal */
case CTRL_CLOSE_EVENT:
printf("CTRL-CLOSE event\n");
Beep(600, 200);
return TRUE;
/* handle the CTRL-LOGOFF signal */
case CTRL_LOGOFF_EVENT:
printf("CTRL-LOGOFF event\n");
Beep(1000, 200);
return TRUE;
/* handle the CTRL-SHUTDOWN signal */
case CTRL_SHUTDOWN_EVENT:
printf("CTRL-SHUTDOWN event\n");
Beep(750, 500);
return TRUE;
default:
return FALSE;
}
}
the control handler is installed.
Pressing Ctrl+C simply exits.