lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi list,
I was wondering what possible issues and solutions might be to change
the FILE pointer Lua uses for functions like print and error at the
configuration or runtime startup level might be.  For instance, there
could be a global FILE* that these functions used other than stdout
that could possibly be swapped by a host application in order to
redirect the stream to a console widget.

FILE *lua_stdout = stdout;

static int luaB_print (lua_State *L) {
  int n = lua_gettop(L);  /* number of arguments */
  int i;
  lua_getglobal(L, "tostring");
  for (i=1; i<=n; i++) {
    const char *s;
    lua_pushvalue(L, -1);  /* function to be called */
    lua_pushvalue(L, i);   /* value to print */
    lua_call(L, 1, 1);
    s = lua_tostring(L, -1);  /* get result */
    if (s == NULL)
      return luaL_error(L, LUA_QL("tostring") " must return a string to "
    if (i>1) fputs("\t", lua_stdout);
    fputs(s, lua_stdout);
    lua_pop(L, 1);  /* pop result */
  fputs("\n", lua_stdout);
  return 0;

The advantage being that a host app wouldn't have to redirect stdout
(which is not really possible on Windows without ridiculous
contortions).  Instead they could set the lua_stdout pointer.

thanks for listening.