2016-04-30 44 views
1

如果我使用editline作爲一個小shell,是否有libedit(editline)做標籤頁完成的方法?似乎rl_parse_and_bind不可用,如果我包括histedit.h - 爲什麼它不起作用?是否editline選項卡完成應該工作?

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

它肯定會工作,使用'rl_parse_and_bind'包括'readline.h',爲什麼不問一個具體的問題? – fluter

+0

@fluter我失敗了,因爲我把'editline'和'readline'混在一起。如果我只包含'histedit.h',那麼'rs_parse_and_bind'不可用,'readline.h'我包含爲'#include '是嗎?如果我希望代碼能夠在OpenBSD和Ubuntu上運行和編譯,我不確定包含哪些內容。 –

+1

要使用它,你應該'#include '。 – fluter

回答

1

的問題是根據系統的不同,有的標題是不可用的,所以需要檢查包含條件。

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