From a0b536718ccd8541917631deab07daa1f69b5cc5 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 21:41:08 +0800 Subject: [PATCH] config can read ENV --- skynet-src/skynet_main.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 9c38fd25..4eb18a59 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -11,6 +11,7 @@ #include #include #include +#include static int optint(const char *key, int opt) { @@ -51,7 +52,6 @@ optstring(const char *key,const char * opt) { static void _init_env(lua_State *L) { - lua_pushglobaltable(L); lua_pushnil(L); /* first key */ while (lua_next(L, -2) != 0) { int keyt = lua_type(L, -2); @@ -83,6 +83,19 @@ int sigign() { return 0; } +static const char * load_config = "\ + local config_name = ...\ + local f = assert(io.open(config_name))\ + local code = assert(f:read \'*a\')\ + local function getenv(name) return assert(os.getenv(name), name) end\ + code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\ + print(code)\ + f:close()\ + local result = {}\ + assert(load(code,\'=(load)\',\'t\',result))()\ + return result\ +"; + int main(int argc, char *argv[]) { const char * config_file = "config"; @@ -98,11 +111,12 @@ main(int argc, char *argv[]) { struct lua_State *L = lua_newstate(skynet_lalloc, NULL); luaL_openlibs(L); // link lua lib - lua_close(L); - L = luaL_newstate(); - - int err = luaL_dofile(L, config_file); + int err = luaL_loadstring(L, load_config); + assert(err == LUA_OK); + lua_pushstring(L, config_file); + + err = lua_pcall(L, 1, 1, 0); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L);