我試圖爲遊戲中的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的控制檯模擬器?