简体   繁体   English

如何让 readline() 在 Ctrl+C 上退出?

[英]How do I make readline() exit on Ctrl+C?

I need to make readline exit when it encounters Ctrl+C .我需要在遇到Ctrl+C时退出 readline 。

if (chkCmd(1, farg[0], "INPUT$")) {
    cerr = 0;
    ftype = 1;
    if (fargct > 1) {cerr = 3; goto fexit;}
    if (fargct == 1 && fargt[1] != 1) {cerr = 2; goto fexit;}
    outbuf[0] = 0;
    char* tmp = NULL;
    if (fargct == 1) {
        tmp = readline(farg[1]);
    } else {
        tmp = readline("?: ");
    }
    if (tmp != NULL) {
        copyStr(tmp, outbuf);
        free(tmp);
    }
    if (debug) printf("input output: {%s}\n", outbuf);
    goto fexit;
}

I have no clue how to do this without modifying the source code of readline (which I want to avoid at all costs).如果不修改 readline 的源代码(我想不惜一切代价避免),我不知道如何做到这一点。 I have tried rl_set_signals();我试过rl_set_signals(); and rl_clear_signals();rl_clear_signals(); but those do not work.但那些不起作用。

I have done numerous Google searches only to come up with irrelevant results so I assume this question is a first.我做了很多谷歌搜索,结果却是不相关的结果,所以我认为这个问题是第一个。

rl_getc_function = getc;

Changing readline's getc function works due to getc returning -1 on a Ctrl+C, which causes readline to return NULL.更改 readline 的 getc function 会起作用,因为 getc 在 Ctrl+C 上返回 -1,这会导致 readline 返回 NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM