static int magnet_env_next(lua_State *L) { server *srv = magnet_get_server(L); connection *con = magnet_get_connection(L); const int pos = lua_tointeger(L, lua_upvalueindex(1)); buffer *dest; /* ignore previous key: use upvalue for current pos */ lua_settop(L, 0); if (NULL == magnet_env[pos].name) return 0; /* end of list */ /* Update our positional upval to reflect our new current position */ lua_pushinteger(L, pos + 1); lua_replace(L, lua_upvalueindex(1)); /* key to return */ lua_pushstring(L, magnet_env[pos].name); /* get value */ dest = magnet_env_get_buffer_by_id(srv, con, magnet_env[pos].type); if (!buffer_is_empty(dest)) { lua_pushlstring(L, CONST_BUF_LEN(dest)); } else { lua_pushnil(L); } /* return 2 items on the stack (key, value) */ return 2; }
static buffer *magnet_env_get_buffer(server *srv, connection *con, const char *key) { size_t i; for (i = 0; magnet_env[i].name; i++) { if (0 == strcmp(key, magnet_env[i].name)) break; } return magnet_env_get_buffer_by_id(srv, con, magnet_env[i].type); }
static int magnet_env_next(lua_State *L) { server *srv; connection *con; int pos = lua_tointeger(L, lua_upvalueindex(1)); buffer *dest; lua_pushstring(L, "lighty.srv"); lua_gettable(L, LUA_REGISTRYINDEX); srv = lua_touserdata(L, -1); lua_pop(L, 1); lua_pushstring(L, "lighty.con"); lua_gettable(L, LUA_REGISTRYINDEX); con = lua_touserdata(L, -1); lua_pop(L, 1); lua_settop(L, 0); if (NULL == magnet_env[pos].name) return 0; /* end of list */ lua_pushstring(L, magnet_env[pos].name); dest = magnet_env_get_buffer_by_id(srv, con, magnet_env[pos].type); if (dest && dest->used) { lua_pushlstring(L, dest->ptr, dest->used - 1); } else { lua_pushnil(L); } /* Update our positional upval to reflect our new current position */ pos++; lua_pushinteger(L, pos); lua_replace(L, lua_upvalueindex(1)); /* Returning 2 items on the stack (key, value) */ return 2; }