editline标签完成应该有效吗?

时间:2016-04-30 07:28:18

标签: c readline

如果我使用editline作为小shell,libedit(editline)是否有办法完成制表符?如果我添加rl_parse_and_bind,似乎无法使用histedit.h - 为什么它不起作用?

int main(int argc, char *argv[]) {
    struct sigaction sh;
    /* char *shell_prompt[100];*/
    sh.sa_handler = handler;
    sigemptyset(&sh.sa_mask);
    sh.sa_flags = 0;
    sigaction(SIGINT, &sh, NULL);
    int index = 0;
    int i;
    EditLine *el = el_init(argv[0], stdin, stdout, stderr);
    el_set(el, EL_PROMPT_ESC, &prompt, '\1');
    el_set(el, EL_EDITOR, "emacs");
    rl_parse_and_bind("bind ^I rl_complete");
    HistEvent ev;
    History *myhistory;
    while (1) {
        index = 0;
        i = getopt_long(argc, argv, "p:vh",
                        options, &index);
        if (i == -1)
            break;
        switch (i) {
            case 'p': {
                /* store_parameter(optarg); */
                break;
            }
            case 'v': {
                printf("OpenShell version 0.1(a)\n");
                printf("Version: %s\n", VERSION);
                exit(EXIT_SUCCESS);

            }
            case 'h': {
                printf("Usage: ./shell\n");
                /*print_help();*/
                exit(EXIT_SUCCESS);

            }
            default: {
                /* fprintf(stderr, "Error (%s): unrecognized option.\n", __FUNCTION__);*/
                /* print_help();*/
                return 1;/*RETURN_FAILURE;*/

            }
        }
    }
    getPath();
    myhistory = history_init();
    if (myhistory == 0) {
        fprintf(stderr, "history could not be initialized\n");
        return 1;
    }
    /* Set the size of the history */
    history(myhistory, &ev, H_SETSIZE, 800);

    /* This sets up the call back functions for history functionality */
    el_set(el, EL_HIST, history, myhistory);

    while (1) {
        int count;
        char const *line = el_gets(el, &count);
        char *pos;
        if (line && (pos = strchr(line, '\n')) != NULL)
            *pos = '\0';
        if (line && count > 0)
            command(line);
        /* In order to use our history we have to explicitly add commands
   to the history */
        if (count > 0) {
            history(myhistory, &ev, H_ENTER, line);
        }
    }
    el_end(el);
    return 0;
}

现在这是我的相关标题的外观,它似乎适用于OpenBSD和Ubuntu:

#include <histedit.h>
#ifdef linux
#include <editline/readline.h>
#endif
#ifdef __OpenBSD__
#include <readline/readline.h>
#endif

1 个答案:

答案 0 :(得分:1)

问题取决于系统,某些标头不可用,因此需要检查包含条件。

#if defined(__linux__) // can also use Linux here
#include <editline/readline.h>
#elif defined(__OpenBSD__)
#include <readline/readline.h>
#endif