2012-07-02 50 views
1

我试图为游戏中的lua控制台制作自动完成和历史记录功能。我被提议使用readline库(它的BSD模拟libedit,准确地说,但它具有类似的apis和rl-code构建,稍微改变了标题),并且lua绑定了我选择的lua-rlcompleter with history patches。历史运作良好,但我有一些readline功能的问题。对于自动完成,我需要将lua字符串传递给readline函数,但是这个函数默认从stdin读取。我发现solution将rl_instream更改为FILE *。为此,我创建tmpfile并写入它。但这很奇怪,readline以这种方式读取字符串时不会返回任何内容。readline(libedit)非stdin输入

// This definitions is just example, not working code 
// it shows the environment 
static FILE *tempfile = tmpfile(); 
rl_instream = tempfile; 
rl_initialize(); 

static int lreadline(lua_State *L) 
{ 
    const char *prompt = lua_tostring(L, 1); 
    char *line; 
    if(rl_instream == NULL) 
    // In case we using stdin 
    line = readline(prompt); 
    else{ 
     fputs(prompt, tempfile); 
     /* maybe I need a fseek here? It not helps though. 
     * fseek(tempfile, -strlen(prompt), SEEK_CUR); 
     */ 
     line = readline(NULL); 
    } 
    lua_pushstring(L, line); 
    free(line); 
    return 1; 
} 

我不确定发生了什么,但认为它可能与读取函数的文件中的位置有关。我试图阅读libedit的来源,但没有道理为什么我的代码不能按我的预期工作。由于使用eclipse调试共享库的问题,但我计划使用纯粹的gdb,所以我无法调试这个功能,不知道它会有帮助。

另外,也许我做错了,还有另一种简单的方法来自动完成和历史在基于lua的控制台模拟器?

回答

0

如果您仍然遇到与libedit有关的问题,请参阅linenoise