Readline: Get a new prompt on SIGINT

前端 未结 4 1286
暗喜
暗喜 2021-01-04 03:52

I\'ve got code similar to the following, using readline:

#include 
#include 
#include 
#include          


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 03:59

    Creating a jump seems hacky and error-prone to me. The shell implementation I was adding this support to didn't allow for this change.

    Luckily, readlinehas a clearer, alternative solution. My SIGINT handler looks like this:

    static void
    int_handler(int status) {
        printf("\n"); // Move to a new line
        rl_on_new_line(); // Regenerate the prompt on a newline
        rl_replace_line("", 0); // Clear the previous text
        rl_redisplay();
    }
    

    This took no other additional code elsewhere to get this working — no global variables, no setting jumps.

提交回复
热议问题