bug-readline
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-readline] setctrlhandler does not prevent exit when using readline


From: Mr Dupito
Subject: [Bug-readline] setctrlhandler does not prevent exit when using readline
Date: Fri, 25 Nov 2016 09:36:38 +1300

I am unable  to have an application in windows that uses readline and does not exit with ctrl+c
I am using mingw: with mingw-developer-toolkit 2013072300
Heres the minimum failing code I am able to get:


#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;
}
}


---
Heres my compilation line:
gcc -Wall -I/opt/local/i
nclude file.c -L/opt/local/lib -lreadline && a.exe

the control handler is installed.
WOOP>

--------
Pressing Ctrl+C simply exits.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]