Readline (libedit) 非标准输入的输入

我试图为游戏内的lua控制台制作自动完成和历史记录功能。有人建议我使用readline库(确切地说是它的BSD模拟库libedit,但它具有相似的api和带有轻微头文件更改的rl-code构建),使用我选择的lua-rlcompleter带有历史记录补丁。历史记录可以正常工作,但我对readline函数有一些问题。对于自动完成,我需要将lua字符串传递给readline函数,但是默认情况下此函数从stdin读取。我在更改rl_instream为FILE*中找到了解决方案。为此,我创建了tmpfile并写入它。但它的工作方式很奇怪,读取字符串时readline返回了空值。

// 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;
}

我不确定发生了什么,但认为它可能与readline函数中的文件读取位置有关。我尝试阅读libedit的源代码,但是我无法理解为什么我的代码不按我预期的那样运行。由于在eclipse中调试共享库的问题,我无法对此进行调试,但打算使用纯gdb,但不确定它是否有所帮助。

此外,也许我做错了,有另一种简单的方式可以在基于lua的控制台仿真器中自动完成和记录历史记录?

点赞
用户33252
用户33252

如果您继续在libedit上遇到问题,请查看linenoise

2012-07-03 17:16:54