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); if (keyt != LUA_TSTRING) { fprintf(stderr, "Invalid config table\n"); exit(1); } const char * key = lua_tostring(L,-2); if (lua_type(L,-1) == LUA_TBOOLEAN) { int b = lua_toboolean(L,-1); skynet_setenv(key,b ? "true" : "false" ); } else { const char * value = lua_tostring(L,-1); if (value == NULL) { fprintf(stderr, "Invalid config table key = %s\n", key); exit(1); } skynet_setenv(key,value); } lua_pop(L,1); } lua_pop(L,1); }
static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); if (str == NULL) { skynet_setenv(key, opt ? "true" : "false"); return opt; } return strcmp(str,"true")==0; }
static int optint(const char *key, int opt) { const char * str = skynet_getenv(key); if (str == NULL) { char tmp[20]; sprintf(tmp,"%d",opt); skynet_setenv(key, tmp); return opt; } return strtol(str, NULL, 10); }
/* static int optboolean(const char *key, int opt) { const char * str = skynet_getenv(key); if (str == NULL) { skynet_setenv(key, opt ? "true" : "false"); return opt; } return strcmp(str,"true")==0; } */ static const char * optstring(const char *key,const char * opt) { const char * str = skynet_getenv(key); if (str == NULL) { if (opt) { skynet_setenv(key, opt); } return opt; } return str; }