/* ** If your system does not support `stdout', you can just remove this function. ** If you need, you can define your own `print' function, following this ** model but changing `fputs' to put the strings at a proper place ** (a console window or a log file, for instance). */ 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 " LUA_QL("print")); #if defined(LUA_USE_STDIO) if (i>1) c_fputs("\t", c_stdout); c_fputs(s, c_stdout); #else if (i>1) luai_writestring("\t", 1); luai_writestring(s, c_strlen(s)); #endif lua_pop(L, 1); /* pop result */ } #if defined(LUA_USE_STDIO) c_fputs("\n", c_stdout); #else luai_writeline(); #endif return 0; }
void * lua_create_instance(void) { luaServer_t * p; lua_State * L; p = (luaServer_t *)calloc(1, sizeof(luaServer_t)); if (p == NULL) return NULL; L = luaL_newstate(); if ( L == NULL ) { free(p); return NULL; } p->client_socket = -1; p->server_socket = -1; p->in = NULL; p->out = NULL; p->err = NULL; luaL_setprivate(L, p); // Make sure we display the copyright string for Lua. luai_writestring(L, LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); luai_writeline(L); _lua_openlib(L); return L; }
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; size_t l; lua_pushvalue(L, -1); /* function to be called */ lua_pushvalue(L, i); /* value to print */ lua_call(L, 1, 1); s = lua_tolstring(L, -1, &l); /* get result */ if (s == NULL) return luaL_error(L, "'tostring' must return a string to 'print'"); if (i>1) luai_writestring("\t", 1); luai_writestring(s, l); lua_pop(L, 1); /* pop result */ } luai_writeline(); return 0; }
static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; lua_getfield(L, LUA_ENVIRONINDEX, "tostring"); for (i=1; i<=n; i++) { const char *s; size_t l; lua_pushvalue(L, -1); /* function to be called */ lua_pushvalue(L, i); /* value to print */ lua_call(L, 1, 1); s = lua_tolstring(L, -1, &l); /* get result */ if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print")); if (i>1) luai_writestring("\t", 1); luai_writestring(s, l); lua_pop(L, 1); /* pop result */ } luai_writestring("\n", 1); return 0; }
static void dotty (lua_State *L) { int status; const char *oldprogname = progname; progname = NULL; while ((status = loadline(L)) != -1) { if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); report(L, status); if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); lua_getglobal(L, "print"); lua_insert(L, 1); if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) l_message(progname, lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", lua_tostring(L, -1))); } } lua_settop(L, 0); /* clear stack */ luai_writestring("\n", 1); progname = oldprogname; }
static void print_version (void) { luai_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT)); luai_writeline(); }