/** * @brief Gets the zone * * @usage gfx = sys:getZone() * @luaparam sys System to get zone of. * @luareturn The zone of the system. * @luafunc getZone( p ) */ static int systemL_getZone( lua_State *L ) { StarSystem *sys = luaL_validsystem(L, 1); lua_pushstring(L,sys->zone); return 1; }
static int os_getenv (lua_State *L) { lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ return 1; }
int luaC_mean_energies(lua_State *L) { const FluidEquations &fluid = *HydroModule::Mara->fluid; const EquationOfState &eos = *HydroModule::Mara->eos; const int Nq = HydroModule::Mara->domain->get_Nq(); const std::valarray<double> &P = HydroModule::Mara->PrimitiveArray; double kinetic = 0.0; double internal = 0.0; double magnetic = 0.0; double total = 0.0; for (size_t m=0; m<P.size(); m+=Nq) { if (absolute_index_is_ghost(m/Nq)) continue; const std::slice M(m, Nq, 1); const std::valarray<double> P0 = P[M]; double *U0 = new double[Nq]; fluid.PrimToCons(&P0[0], &U0[0]); const double v2 = P0[vx]*P0[vx] + P0[vy]*P0[vy] + P0[vz]*P0[vz]; const double T0 = eos.Temperature_p(P0[rho], P0[pre]); const double u0 = eos.Internal(P0[rho], T0); if (typeid(fluid) == typeid(AdiabaticIdealSrhd) || typeid(fluid) == typeid(AdiabaticIdealRmhd)) { const double rhoh = P0[rho] + u0 + P0[pre]; const double W0 = 1.0 / sqrt(1.0 - v2); const double e_f = rhoh * W0*W0 - P0[pre] - U0[ddd]; const double e_m = U0[tau] - e_f; magnetic += e_m; kinetic += P0[rho] * W0 * (W0-1); internal += W0 * u0; total += U0[tau]; } else if (typeid(fluid) == typeid(AdiabaticIdealEulers)) { magnetic += 0.0; kinetic += 0.5 * P0[rho] * v2; internal += u0; total += U0[tau]; } delete [] U0; } magnetic = Mara_mpi_dbl_sum(magnetic) / TTL_ZONES; kinetic = Mara_mpi_dbl_sum(kinetic ) / TTL_ZONES; internal = Mara_mpi_dbl_sum(internal) / TTL_ZONES; total = Mara_mpi_dbl_sum(total ) / TTL_ZONES; lua_newtable(L); lua_pushstring(L, "magnetic"); lua_pushnumber(L, magnetic); lua_settable(L, -3); lua_pushstring(L, "kinetic"); lua_pushnumber(L, kinetic); lua_settable(L, -3); lua_pushstring(L, "internal"); lua_pushnumber(L, internal); lua_settable(L, -3); lua_pushstring(L, "total"); lua_pushnumber(L, total); lua_settable(L, -3); return 1; }
int merger_map_newindex( lua_State *L ) { const char *field; if ( !lua_isstring( L, 2 ) ) { lua_pushstring( L, "Cannot index the map-userdata with a non-string." ); lua_error( L ); } field = lua_tostring( L, 2 ); if ( !strcmp( field, "mode" ) ) { const char *value; if ( !lua_isstring( L, 3 ) ) { lua_pushstring( L, "map.mode accepts only string values." ); lua_error( L ); } value = lua_tostring( L, 3 ); if ( !strcmp( value, "none" ) ) mode = NONE; else if ( !strcmp( value, "get_unlost" ) ) mode = GET_UNLOST; else if ( !strcmp( value, "following" ) || !strcmp( value, "follow" ) ) mode = FOLLOWING; else if ( !strcmp( value, "creating" ) || !strcmp( value, "create" ) ) mode = CREATING; else { lua_pushstring( L, "map.mode accepts only 'none', " "'get_unlost', 'following', or 'creating'." ); lua_error( L ); } } else if ( !strcmp( field, "current_room" ) ) { if ( lua_isnil( L, 3 ) ) { current_room = NULL; current_area = NULL; } else if ( !lua_isuserdata( L, 3 ) ) { lua_pushstring( L, "map.current_room accepts only 'nil' or a room-userdata." ); lua_error( L ); } else { ROOM_DATA *room; room = *(ROOM_DATA**) lua_touserdata( L, 3 ); current_room = room; if ( current_room ) current_area = current_room->area; } } else { lua_pushfstring( L, "'map' does not accept a writable '%s' property.", field ); lua_error( L ); } return 0; }
int merger_exits_call( lua_State *L ) { ROOM_DATA *room; const char *val; int dir, i; if ( !lua_isuserdata( L, 1 ) ) { lua_pushstring( L, "Calling non-userdata. Internal error!" ); lua_error( L ); } room = *(ROOM_DATA**) lua_touserdata( L, 1 ); /* No arguments: create an iterator. */ if ( lua_gettop( L ) == 1 ) { /* It needs to return itself (already on the stack), * followed by a state and an initial value. */ lua_pushnil( L ); lua_pushstring( L, "start" ); return 3; } if ( lua_gettop( L ) != 3 || !lua_isstring( L, 3 ) ) { lua_pushstring( L, "Invalid call to an exits iterator." ); lua_error( L ); } /* Find out the next direction. */ val = lua_tostring( L, 3 ); if ( !strcmp( val, "start" ) ) dir = 0; else { for ( dir = 0; dir_name[dir]; dir++ ) if ( !strcmp( val, dir_name[dir] ) ) break; if ( !dir_name[dir] ) { lua_pushnil( L ); return 1; } } for ( i = dir + 1; dir_name[i]; i++ ) if ( room->exits[i] ) break; if ( !dir_name[i] ) { lua_pushnil( L ); return 1; } lua_pushstring( L, dir_name[i] ); merger_push_room( L, room->exits[i] ); return 2; }
static int dolibrary (lua_State *L, const char *name) { lua_getglobal(L, "require"); lua_pushstring(L, name); return report(L, docall(L, 1, 1)); }
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = cast_func(dlsym(lib, sym)); if (f == NULL) lua_pushstring(L, dlerror()); return f; }
static int luaB_type (lua_State *L) { luaL_checkany(L, 1); lua_pushstring(L, lua_typename(L, lua_type(L, 1))); return 1; }
LUALIB_API void lua_baselibopen (lua_State *L) { luaL_openl(L, base_funcs); lua_pushstring(L, LUA_VERSION); lua_setglobal(L, "_VERSION"); deprecated_funcs(L); }
static int se_accept_error(lua_State *L, const char *errmsg) { lua_pushnil(L); lua_pushstring(L, errmsg); return 2; }
/** * Converts a t_string object to a string (__tostring metamethod); * that is, performs a translation. */ static int impl_tstring_tostring(lua_State *L) { t_string *t = static_cast<t_string *>(lua_touserdata(L, 1)); lua_pushstring(L, t->c_str()); return 1; }
static int se_write_error(lua_State *L, int nwrite, const char *errmsg) { lua_pushstring(L, errmsg); lua_pushinteger(L, nwrite); return 2; }
//ftp.list(ltype, otype [,dir]) //=================================== static int lftp_list( lua_State* L ) { int err = 0; uint16_t dptr = 0; uint8_t n = 0; uint8_t i = 0; int nlin = 0; char buf[255] = {0}; uint32_t tmo; uint8_t ltype = luaL_checkinteger(L, 1); uint8_t otype = luaL_checkinteger(L, 2); if (otype == 1) ltype = 1; err = _getFName(L, 3); err = 0; if ((gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED))) { ftp_log("[FTP usr] Login first\r\n" ); err = -1; goto exit; } list_type = ltype; data_done = 0; ftpCmdSocket->clientFlag = REQ_ACTION_LIST; if (ftpCmdSocket->list_cb != LUA_NOREF) { goto exit; } // no cb function, wait until List received (max 10 sec) tmo = mico_get_time(); while (data_done == 0) { if ((mico_get_time() - tmo) > 10000) break; mico_thread_msleep(60); luaWdgReload(); } if (data_done == 0) { ftp_log("[FTP usr] Timeout: list not received\r\n" ); err = -3; goto exit; } if ((recvDataBuf == NULL) || (recvDataLen == 0)) { ftp_log("[FTP usr] List not received\r\n" ); err = -4; goto exit; } if (otype != 1) { printf("===================\r\n"); printf("FTP directory list:"); if (recvDataLen == (max_recv_datalen-16)) { printf(" (buffer full)"); } printf("\r\n"); } else { lua_newtable( L ); } while (dptr < recvDataLen) { if (*(recvDataBuf+dptr) == '\0') break; if ((*(recvDataBuf+dptr) == '\n') || (*(recvDataBuf+dptr) == '\r') || (n >= 254)) { // EOL, print line if (n > 0) { nlin++; if (otype != 1) printf("%s\r\n", &buf[0]); else { lua_pushstring( L, &buf[0] ); lua_rawseti(L,-2,i++); } n = 0; } } if (*(recvDataBuf+dptr) >= ' ') buf[n++] = *(recvDataBuf+dptr); buf[n] = '\0'; dptr++; } if (n > 0) { // last line nlin++; if (otype != 1) printf("%s\r\n", &buf[0]); else { lua_pushstring( L, &buf[0] ); lua_rawseti(L,-2,i++); } } if (otype != 1) { printf("===================\r\n"); lua_pushinteger(L, nlin); return 1; } else { ftp_log("[FTP usr] List received to table\r\n" ); lua_pushinteger(L, nlin); return 2; } exit: if (otype == 1) { lua_newtable( L ); lua_pushinteger(L, err); return 2; } lua_pushinteger(L, err); return 1; }
/* ** To get a environment variables */ static void io_getenv (void) { char *env = getenv(lua_check_string(1, "getenv")); if (env == NULL) lua_pushnil(); else lua_pushstring(env); }
void CharPToLua(lua_State *L, Variable& var) { lua_pushstring(L, var.GetValue<char*>()); }
void cLuaInterpreter::RegisterFunction(const char *fncname, int (*fncptr)(lua_State *)) { lua_pushstring(mL, fncname); lua_pushcfunction(mL, fncptr); lua_rawset(mL, -3); }
void EntropyScript::pushString(const char* stringToPush) { lua_pushstring(_lua_state, stringToPush); }
bool cLuaInterpreter::CallFunction(const char * func, char * args[], cConnDC *conn) { lua_settop(mL, 0); int base = lua_gettop(mL); lua_pushliteral(mL, "_TRACEBACK"); #if defined LUA_GLOBALSINDEX lua_rawget(mL, LUA_GLOBALSINDEX); // <=5.1 #else lua_pushglobaltable(mL); // >=5.2 #endif lua_insert(mL, base); lua_getglobal(mL, func); if (lua_isnil(mL, -1)) { // function not exists lua_pop(mL, -1); // remove nil value lua_remove(mL, base); // remove _TRACEBACK } else { int i = 0; while (args[i] != NULL) { lua_pushstring(mL, args[i]); i++; } int result = lua_pcall(mL, i, 1, base); if (result) { const char *msg = lua_tostring(mL, -1); if (msg == NULL) msg = "Unknown error"; cout << "Lua error: " << msg << endl; ReportLuaError((char*)msg); lua_pop(mL, 1); lua_remove(mL, base); // remove _TRACEBACK return true; } bool ret = true; if (lua_istable(mL, -1)) { /* * new style, advanced table return: * * table index = 1, type = string: * value: data = protocol message to send * value: empty = dont send anything * * table index = 2, type = boolean: * value: 0 = discard * value: 1 = dont discard * * table index = 3, type = boolean: * value: 0 = disconnect user * value: 1 = dont disconnect */ i = lua_gettop(mL); lua_pushnil(mL); while (lua_next(mL, i) != 0) { if (lua_isnumber(mL, -2)) { // table keys must not be named int key = (int)lua_tonumber(mL, -2); if (key == 1) { // message? if (lua_isstring(mL, -1) && (conn != NULL)) { // value at index 1 must be a string, connection is required string data = lua_tostring(mL, -1); if (!data.empty()) conn->Send(data, false); // send data, script must add the ending pipe } } else if (key == 2) { // discard? if (lua_isnumber(mL, -1)) { // value at index 2 must be a boolean if ((int)lua_tonumber(mL, -1) == 0) ret = false; } else { // accept boolean and nil if ((int)lua_toboolean(mL, -1) == 0) ret = false; } } else if (key == 3) { // disconnect? if (conn != NULL) { // connection is required if (lua_isnumber(mL, -1)) { // value at index 3 must be a boolean if ((int)lua_tonumber(mL, -1) == 0) { conn->CloseNow(); // disconnect user ret = false; // automatically discard due disconnect } } else { // accept boolean and nil if ((int)lua_toboolean(mL, -1) == 0) { conn->CloseNow(); // disconnect user ret = false; // automatically discard due disconnect } } } } } lua_pop(mL, 1); } } else if (lua_isnumber(mL, -1)) { /* * old school, simple boolean return for backward compatibility: * * type = boolean: * value: 0 = discard * value: 1 = dont discard */ if ((int)lua_tonumber(mL, -1) == 0) ret = false; //} else { // accept boolean and nil // same as above //if ((int)lua_toboolean(mL, -1) == 0) ret = false; } lua_pop(mL, 1); lua_remove(mL, base); // remove _TRACEBACK return ret; } return true; }
int cache_parse_lua(server *srv, connection *con, plugin_data *p, buffer *fn) { lua_State *L; readme rm; int ret = -1; buffer *b = buffer_init(); int header_tbl = 0; rm.done = 0; stream_open(&rm.st, fn); /* push the lua file to the interpreter and see what happends */ L = luaL_newstate(); luaL_openlibs(L); /* register functions */ lua_register(L, "md5", f_crypto_md5); lua_register(L, "file_mtime", f_file_mtime); lua_register(L, "file_isreg", f_file_isreg); lua_register(L, "file_isdir", f_file_isreg); lua_register(L, "dir_files", f_dir_files); #ifdef HAVE_MEMCACHE_H lua_pushliteral(L, "memcache_get_long"); lua_pushlightuserdata(L, p->conf.mc); lua_pushcclosure(L, f_memcache_get_long, 1); lua_settable(L, LUA_GLOBALSINDEX); lua_pushliteral(L, "memcache_get_string"); lua_pushlightuserdata(L, p->conf.mc); lua_pushcclosure(L, f_memcache_get_string, 1); lua_settable(L, LUA_GLOBALSINDEX); lua_pushliteral(L, "memcache_exists"); lua_pushlightuserdata(L, p->conf.mc); lua_pushcclosure(L, f_memcache_exists, 1); lua_settable(L, LUA_GLOBALSINDEX); #endif /* register CGI environment */ lua_pushliteral(L, "request"); lua_newtable(L); lua_settable(L, LUA_GLOBALSINDEX); lua_pushliteral(L, "request"); header_tbl = lua_gettop(L); lua_gettable(L, LUA_GLOBALSINDEX); c_to_lua_push(L, header_tbl, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con->request.orig_uri)); c_to_lua_push(L, header_tbl, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con->uri.path)); c_to_lua_push(L, header_tbl, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(con->physical.path)); c_to_lua_push(L, header_tbl, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con->physical.doc_root)); if (!buffer_is_empty(con->request.pathinfo)) { c_to_lua_push(L, header_tbl, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con->request.pathinfo)); } c_to_lua_push(L, header_tbl, CONST_STR_LEN("CWD"), CONST_BUF_LEN(p->basedir)); c_to_lua_push(L, header_tbl, CONST_STR_LEN("BASEURL"), CONST_BUF_LEN(p->baseurl)); /* register GET parameter */ lua_pushliteral(L, "get"); lua_newtable(L); lua_settable(L, LUA_GLOBALSINDEX); lua_pushliteral(L, "get"); header_tbl = lua_gettop(L); lua_gettable(L, LUA_GLOBALSINDEX); buffer_copy_string_buffer(b, con->uri.query); cache_export_get_params(L, header_tbl, b); buffer_reset(b); /* 2 default constants */ lua_pushliteral(L, "CACHE_HIT"); lua_pushnumber(L, 0); lua_settable(L, LUA_GLOBALSINDEX); lua_pushliteral(L, "CACHE_MISS"); lua_pushnumber(L, 1); lua_settable(L, LUA_GLOBALSINDEX); /* load lua program */ if (lua_load(L, load_file, &rm, fn->ptr) || lua_pcall(L,0,1,0)) { log_error_write(srv, __FILE__, __LINE__, "s", lua_tostring(L,-1)); goto error; } /* get return value */ ret = (int)lua_tonumber(L, -1); lua_pop(L, 1); /* fetch the data from lua */ lua_to_c_get_string(L, "trigger_handler", p->trigger_handler); if (0 == lua_to_c_get_string(L, "output_contenttype", b)) { response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(b)); } if (ret == 0) { /* up to now it is a cache-hit, check if all files exist */ int curelem; time_t mtime = 0; if (!lua_to_c_is_table(L, "output_include")) { log_error_write(srv, __FILE__, __LINE__, "s", "output_include is missing or not a table"); ret = -1; goto error; } lua_pushstring(L, "output_include"); curelem = lua_gettop(L); lua_gettable(L, LUA_GLOBALSINDEX); /* HOW-TO build a etag ? * as we don't just have one file we have to take the stat() * from all base files, merge them and build the etag from * it later. * * The mtime of the content is the mtime of the freshest base file * * */ lua_pushnil(L); /* first key */ while (lua_next(L, curelem) != 0) { stat_cache_entry *sce = NULL; /* key' is at index -2 and value' at index -1 */ if (lua_isstring(L, -1)) { const char *s = lua_tostring(L, -1); /* the file is relative, make it absolute */ if (s[0] != '/') { buffer_copy_string_buffer(b, p->basedir); buffer_append_string(b, lua_tostring(L, -1)); } else { buffer_copy_string(b, lua_tostring(L, -1)); } if (HANDLER_ERROR == stat_cache_get_entry(srv, con, b, &sce)) { /* stat failed */ switch(errno) { case ENOENT: /* a file is missing, call the handler to generate it */ if (!buffer_is_empty(p->trigger_handler)) { ret = 1; /* cache-miss */ log_error_write(srv, __FILE__, __LINE__, "s", "a file is missing, calling handler"); break; } else { /* handler not set -> 500 */ ret = -1; log_error_write(srv, __FILE__, __LINE__, "s", "a file missing and no handler set"); break; } break; default: break; } } else { chunkqueue_append_file(con->write_queue, b, 0, sce->st.st_size); if (sce->st.st_mtime > mtime) mtime = sce->st.st_mtime; } } else { /* not a string */ ret = -1; log_error_write(srv, __FILE__, __LINE__, "s", "not a string"); break; } lua_pop(L, 1); /* removes value'; keeps key' for next iteration */ } lua_settop(L, curelem - 1); if (ret == 0) { data_string *ds; char timebuf[sizeof("Sat, 23 Jul 2005 21:20:01 GMT")]; buffer tbuf; con->file_finished = 1; ds = (data_string *)array_get_element(con->response.headers, "Last-Modified"); /* no Last-Modified specified */ if ((mtime) && (NULL == ds)) { strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&mtime)); response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), timebuf, sizeof(timebuf) - 1); tbuf.ptr = timebuf; tbuf.used = sizeof(timebuf); tbuf.size = sizeof(timebuf); } else if (ds) { tbuf.ptr = ds->value->ptr; tbuf.used = ds->value->used; tbuf.size = ds->value->size; } else { tbuf.size = 0; tbuf.used = 0; tbuf.ptr = NULL; } if (HANDLER_FINISHED == http_response_handle_cachable(srv, con, &tbuf)) { /* ok, the client already has our content, * no need to send it again */ chunkqueue_reset(con->write_queue); ret = 0; /* cache-hit */ } } else { chunkqueue_reset(con->write_queue); } } if (ret == 1 && !buffer_is_empty(p->trigger_handler)) { /* cache-miss */ buffer_copy_string_buffer(con->uri.path, p->baseurl); buffer_append_string_buffer(con->uri.path, p->trigger_handler); buffer_copy_string_buffer(con->physical.path, p->basedir); buffer_append_string_buffer(con->physical.path, p->trigger_handler); chunkqueue_reset(con->write_queue); } error: lua_close(L); stream_close(&rm.st); buffer_free(b); return ret /* cache-error */; }
bool cLuaInterpreter::Init() { luaL_openlibs(mL); lua_newtable(mL); RegisterFunction("SendDataToUser", &_SendToUser); /* back compatibility */ RegisterFunction("SendToUser", &_SendToUser); RegisterFunction("SendDataToAll", &_SendToClass); /* back compatibility */ RegisterFunction("SendToClass", &_SendToClass); RegisterFunction("SendToAll", &_SendToAll); RegisterFunction("SendPMToAll", &_SendPMToAll); RegisterFunction("CloseConnection", &_Disconnect); /* back compatibility */ RegisterFunction("Disconnect", &_Disconnect); RegisterFunction("DisconnectByName", &_Disconnect); /* back compatibility */ RegisterFunction("StopHub", &_StopHub); RegisterFunction("GetUserCC", &_GetUserCC); RegisterFunction("GetIPCC", &_GetIPCC); RegisterFunction("GetIPCN", &_GetIPCN); RegisterFunction("GetMyINFO", &_GetMyINFO); RegisterFunction("GetUpTime", &_GetUpTime); RegisterFunction("RegBot", &_RegBot); RegisterFunction("AddRobot", &_RegBot); /* back compatibility */ RegisterFunction("UnRegBot", &_UnRegBot); RegisterFunction("DelRobot", &_UnRegBot); /* back compatibility */ RegisterFunction("EditBot", &_EditBot); RegisterFunction("IsBot", &_IsBot); RegisterFunction("GetHubIp", &_GetHubIp); RegisterFunction("GetHubSecAlias", &_GetHubSecAlias); RegisterFunction("AddRegUser", &_AddRegUser); RegisterFunction("DelRegUser", &_DelRegUser); RegisterFunction("GetUserClass", &_GetUserClass); RegisterFunction("GetUserHost", &_GetUserHost); RegisterFunction("GetUserIP", &_GetUserIP); RegisterFunction("IsUserOnline", &_IsUserOnline); RegisterFunction("InUserSupports", &_InUserSupports); RegisterFunction("Ban", &_Ban); RegisterFunction("KickUser", &_KickUser); RegisterFunction("ReportUser", &_ReportUser); RegisterFunction("SendToOpChat", &_SendToOpChat); RegisterFunction("ParseCommand", &_ParseCommand); RegisterFunction("SetConfig", &_SetConfig); RegisterFunction("GetConfig", &_GetConfig); RegisterFunction("SQLQuery", &_SQLQuery); RegisterFunction("SQLFetch", &_SQLFetch); RegisterFunction("SQLFree", &_SQLFree); RegisterFunction("GetUsersCount", &_GetUsersCount); RegisterFunction("GetTotalShareSize", &_GetTotalShareSize); RegisterFunction("GetNickList", &_GetNickList); RegisterFunction("GetOPList", &_GetOPList); RegisterFunction("GetBotList", &_GetBotList); RegisterFunction("GetLuaBots", &_GetLuaBots); RegisterFunction("GetBots", &_GetLuaBots); /* back compatibility */ RegisterFunction("GetTempRights", &_GetTempRights); RegisterFunction("SetTempRights", &_SetTempRights); RegisterFunction("GetVHCfgDir", &_GetVHCfgDir); RegisterFunction("GetTopic", &_GetTopic); RegisterFunction("SetTopic", &_SetTopic); RegisterFunction("ScriptCommand", &_ScriptCommand); lua_setglobal(mL, "VH"); int status = luaL_dofile(mL, (char *)mScriptName.c_str()); if(status) { unsigned char *error = (unsigned char *) luaL_checkstring (mL, 1); ReportLuaError((char *) error); return false; } lua_pushstring(mL, LUA_PI_VERSION); lua_setglobal(mL, "_PLUGINVERSION"); lua_pushstring(mL, VERSION); lua_setglobal(mL, "_HUBVERSION"); return true; }
int merger_locate_faraway( lua_State *L ) { ROOM_DATA *room; const char *title; int matches; int detected_exits[13]; int *de = NULL; if ( lua_gettop( L ) < 1 || !lua_isstring( L, 1 ) ) { lua_pushstring( L, "No valid arguments provided to locate_faraway." ); lua_error( L ); } title = lua_tostring( L, 1 ); if ( lua_gettop( L ) >= 2 && lua_istable( L, 2 ) ) { const char *dirstring; int dir; int i = 1; for ( dir = 0; dir < 13; dir++ ) detected_exits[dir] = 0; de = detected_exits; while ( 1 ) { lua_pushinteger( L, i ); lua_gettable( L, 2 ); if ( lua_isstring( L, -1 ) ) { dirstring = lua_tostring( L, -1 ); for ( dir = 1; dir_name[dir]; dir++ ) if ( !strcmp( dirstring, dir_name[dir] ) || !strcmp( dirstring, dir_small_name[dir] ) ) break; if ( dir_name[dir] ) detected_exits[dir] = 1; } if ( lua_isnil( L, -1 ) ) { lua_pop( L, 1 ); break; } lua_pop( L, 1 ); i++; } } room = locate_faraway_room( title, de, &matches ); merger_push_room( L, room ); lua_pushinteger( L, matches ); return 2; }
bool eae6320::cEffectBuilder::LoadRenderStatesValues(lua_State& io_luaState) { bool wereThereErrors = false; { const char* const key = "renderStates"; lua_pushstring(&io_luaState, key); lua_gettable(&io_luaState, -2); if (!lua_istable(&io_luaState, -1)) { lua_pop(&io_luaState, 1); wereThereErrors = true; std::stringstream errorMessage; errorMessage << "Render States must return a table (instead of a " << luaL_typename(&io_luaState, -1) << ")\n"; eae6320::OutputErrorMessage(errorMessage.str().c_str()); goto OnExit; } // Setting Alpha Trransparency from Lua { const char* const key = "alphaTransparency"; lua_pushstring(&io_luaState, key); lua_gettable(&io_luaState, -2); if (lua_isnil(&io_luaState, -1)) m_renderStates = 0; else if (lua_toboolean(&io_luaState, -1)) m_renderStates = AlphaTransparency; else m_renderStates = 0; lua_pop(&io_luaState, 1); } // Setting Depth Testing from Lua { const char* const key = "depthTesting"; lua_pushstring(&io_luaState, key); lua_gettable(&io_luaState, -2); if (lua_isnil(&io_luaState, -1)) m_renderStates |= DepthTest; else if (lua_toboolean(&io_luaState, -1)) m_renderStates |= DepthTest; lua_pop(&io_luaState, 1); } // Setting Depth Writing from Lua { const char* const key = "depthWriting"; lua_pushstring(&io_luaState, key); lua_gettable(&io_luaState, -2); if (lua_isnil(&io_luaState, -1)) m_renderStates |= DepthWrite; else if (lua_toboolean(&io_luaState, -1)) m_renderStates |= DepthWrite; lua_pop(&io_luaState, 1); } // Setting Face Culling from Lua { const char* const key = "faceCulling"; lua_pushstring(&io_luaState, key); lua_gettable(&io_luaState, -2); if (lua_isnil(&io_luaState, -1)) m_renderStates |= FaceCulling; else if (lua_toboolean(&io_luaState, -1)) m_renderStates |= FaceCulling; lua_pop(&io_luaState, 1); } lua_pop(&io_luaState, 1); } OnExit: return !wereThereErrors; }
int merger_room_index( lua_State *L ) { ROOM_DATA *room; EXIT_DATA *spexit; const char *field; if ( !lua_isuserdata( L, 1 ) ) { lua_pushstring( L, "Indexing non-userdata. Internal error!" ); lua_error( L ); } if ( !lua_isstring( L, 2 ) ) { lua_pushstring( L, "Cannot index a room-userdata with a non-string." ); lua_error( L ); } room = *(ROOM_DATA**) lua_touserdata( L, 1 ); field = lua_tostring( L, 2 ); if ( !strcmp( field, "name" ) || !strcmp( field, "title" ) ) { lua_pushstring( L, room->name ); } else if ( !strcmp( field, "vnum" ) ) { lua_pushinteger( L, room->vnum ); } else if ( !strcmp( field, "underwater" ) ) { lua_pushboolean( L, room->underwater ); } else if ( !strcmp( field, "pf_next" ) ) { merger_push_room( L, room->pf_parent ); } else if ( !strcmp( field, "pf_command" ) ) { if ( room->pf_direction == 0 || !room->pf_parent ) lua_pushnil( L ); else if ( room->pf_direction > 0 ) lua_pushstring( L, dir_name[room->pf_direction] ); else { for ( spexit = room->special_exits; spexit; spexit = spexit->next ) if ( spexit->to == room->pf_parent ) break; if ( spexit && spexit->command ) lua_pushstring( L, spexit->command ); else lua_pushnil( L ); } } else if ( !strcmp( field, "exits" ) ) { ROOM_DATA **r; r = (ROOM_DATA**) lua_newuserdata( L, sizeof(ROOM_DATA*) ); *r = room; lua_getfield( L, LUA_REGISTRYINDEX, "merger_exits_metatable" ); lua_setmetatable( L, -2 ); } else if ( !strcmp( field, "special_exits" ) ) { ROOM_DATA **r; r = (ROOM_DATA**) lua_newuserdata( L, sizeof(ROOM_DATA*) ); *r = room; lua_getfield( L, LUA_REGISTRYINDEX, "merger_spexits_metatable" ); lua_setmetatable( L, -2 ); } else if ( !strcmp( field, "area" ) ) { AREA_DATA **a; if ( !room->area ) lua_pushnil( L ); else { a = (AREA_DATA**) lua_newuserdata( L, sizeof(AREA_DATA*) ); *a = room->area; lua_getfield( L, LUA_REGISTRYINDEX, "merger_area_metatable" ); lua_setmetatable( L, -2 ); } } else if ( !strcmp( field, "environment" ) || !strcmp( field, "type" ) ) { ROOM_TYPE **t; if ( !room->room_type ) lua_pushnil( L ); else { t = (ROOM_TYPE**) lua_newuserdata( L, sizeof(ROOM_TYPE*) ); *t = room->room_type; lua_getfield( L, LUA_REGISTRYINDEX, "merger_environment_metatable" ); lua_setmetatable( L, -2 ); } } else { int dir; /* room.east; Shortcut to room.exits.east.room. */ for ( dir = 1; dir_name[dir]; dir++ ) if ( !strcmp( dir_name[dir], field ) || !strcmp( dir_small_name[dir], field ) ) break; if ( dir_name[dir] ) merger_push_room( L, room->exits[dir] ); else lua_pushnil( L ); } return 1; }
int LuaGlobalFunctions_GetLUAEngine(lua_State * L) //also mapped to GetLuaEngine() { lua_pushstring(L, ENGINE_NAME); return 1; }
int ModApiUtil::l_get_builtin_path(lua_State *L) { std::string path = porting::path_share + DIR_DELIM + "builtin"; lua_pushstring(L, path.c_str()); return 1; }
/* ** Variations of 'lua_settable', used by 'db_getinfo' to put results ** from 'lua_getinfo' into result table. Key is always a string; ** value can be a string, an int, or a boolean. */ static void settabss (lua_State *L, const char *k, const char *v) { lua_pushstring(L, v); lua_setfield(L, -2, k); }
void luaopen_cryptobox (lua_State * L) { luaL_newmetatable (L, "rspamd{cryptobox_pubkey}"); lua_pushstring (L, "__index"); lua_pushvalue (L, -2); lua_settable (L, -3); lua_pushstring (L, "class"); lua_pushstring (L, "rspamd{cryptobox_pubkey}"); lua_rawset (L, -3); luaL_register (L, NULL, cryptoboxpubkeylib_m); rspamd_lua_add_preload (L, "rspamd_cryptobox_pubkey", lua_load_pubkey); luaL_newmetatable (L, "rspamd{cryptobox_keypair}"); lua_pushstring (L, "__index"); lua_pushvalue (L, -2); lua_settable (L, -3); lua_pushstring (L, "class"); lua_pushstring (L, "rspamd{cryptobox_keypair}"); lua_rawset (L, -3); luaL_register (L, NULL, cryptoboxkeypairlib_m); rspamd_lua_add_preload (L, "rspamd_cryptobox_keypair", lua_load_keypair); luaL_newmetatable (L, "rspamd{cryptobox_signature}"); lua_pushstring (L, "__index"); lua_pushvalue (L, -2); lua_settable (L, -3); lua_pushstring (L, "class"); lua_pushstring (L, "rspamd{cryptobox_signature}"); lua_rawset (L, -3); luaL_register (L, NULL, cryptoboxsignlib_m); rspamd_lua_add_preload (L, "rspamd_cryptobox_signature", lua_load_signature); luaL_newmetatable (L, "rspamd{cryptobox_hash}"); lua_pushstring (L, "__index"); lua_pushvalue (L, -2); lua_settable (L, -3); lua_pushstring (L, "class"); lua_pushstring (L, "rspamd{cryptobox_hash}"); lua_rawset (L, -3); luaL_register (L, NULL, cryptoboxhashlib_m); rspamd_lua_add_preload (L, "rspamd_cryptobox_hash", lua_load_hash); rspamd_lua_add_preload (L, "rspamd_cryptobox", lua_load_cryptobox); lua_settop (L, 0); }
int _lua_write_connection(lua_State *lua_state) { /* allocates the data (original message data) */ const char *data; /* allocates the (final) buffer to send the message */ unsigned char *buffer; /* allocates the data size */ unsigned int data_size; /* allocates the HTTP parser */ struct http_parser_t *http_parser; /* allocates the connection */ struct connection_t *connection; /* retrieves the number of (received) arguments */ unsigned int number_arguments = lua_gettop(lua_state); /* in case the number of arguments is invalid */ if(number_arguments != 3) { /* prints a warning message */ V_WARNING("Invalid number of arguments\n"); /* pushes an error message to Lua */ lua_pushstring(lua_state, "Invalid number of arguments"); lua_error(lua_state); } if(!lua_isnumber(lua_state, -1)) { /* prints a warning message */ V_WARNING("Incorrect argument 'expected number'\n"); /* pushes an error message to Lua */ lua_pushstring(lua_state, "Incorrect argument to 'expected number'"); lua_error(lua_state); } if(!lua_isstring(lua_state, -2)) { /* prints a warning message */ V_WARNING("Incorrect argument 'expected string'\n"); /* pushes an error message to Lua */ lua_pushstring(lua_state, "Incorrect argument to 'expected string'"); lua_error(lua_state); } if(!lua_islightuserdata(lua_state, -3)) { /* prints a warning message */ V_WARNING("Incorrect argument 'expected lightuserdata'\n"); /* pushes an error message to Lua */ lua_pushstring(lua_state, "Incorrect argument 'expected lightuserdata'"); lua_error(lua_state); } /* converts the third argument into an integer */ data_size = lua_tointeger(lua_state, -1); /* converts the second argument into a string */ data = lua_tostring(lua_state, -2); /* converts the first argument into HTTP parser structure */ http_parser = (struct http_parser_t *) lua_touserdata(lua_state, -3); /* retrieves the connection from the HTTP parser parameters */ connection = (struct connection_t *) http_parser->parameters; /* allocates the data buffer (in a safe maner) then copies the data (from Lua) into the buffer */ connection->alloc_data(connection, data_size * sizeof(unsigned char), (void **) &buffer); memcpy(buffer, data, data_size); /* writes the response to the connection */ connection->write_connection( connection, buffer, data_size, _send_response_callback_handler_lua, (void *) (size_t) http_parser->flags ); /* return the number of results */ return 0; }
static void *ll_load(lua_State *L, const char *path, int gl) { void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL)); if (lib == NULL) lua_pushstring(L, dlerror()); return lib; }
/* Map constant number * It assigns a constant number into the current module (or class) */ TOLUA_API void tolua_constant (lua_State* L, const char* name, lua_Number value) { lua_pushstring(L,name); tolua_pushnumber(L,value); lua_rawset(L,-3); }