bool _env_optbool(const char* key, bool opt){ const char *val = tiny_getenv(key); if(val == NULL){ if(opt){ tiny_setenv(key, "true"); }else{ tiny_setenv(key, "false"); } return opt; } return (strcmp(val, "true") == 0); }
const char* _env_optstring(const char* key, const char* opt){ const char *val = tiny_getenv(key); if(val == NULL){ if(opt){ tiny_setenv(key, opt); return opt; } } return val; }
int _env_optint(const char *key, int opt){ const char *val = tiny_getenv(key); if(val == NULL){ char tmp[16]; sprintf(tmp, "%d", opt); tiny_setenv(key, tmp); return opt; } return atoi(val); }
void _env_init(lua_State *L){ lua_pushglobaltable(L); lua_pushnil(L); while(lua_next(L, -2) != 0){ int tkey = lua_type(L, -2); if(tkey != LUA_TSTRING){ fprintf(stderr, "parse config failed\n"); exit(1); } const char *key = lua_tostring(L, -2); if(lua_type(L, -1) == LUA_TBOOLEAN){ int b = lua_toboolean(L, -1); tiny_setenv(key, b? "true" : "false"); }else { const char * val = lua_tostring(L, -1); tiny_setenv(key, val); } lua_pop(L, 1); } lua_pop(L, 1); }
static int fill_env(char **buffer, char ***tmp_env, int i) { char **orig_env; orig_env = g_env; g_env = *tmp_env; while (buffer[i] && ft_strchr(buffer[i], '=')) { tiny_setenv(buffer[i]); i++; } *tmp_env = g_env; g_env = orig_env; return (i); }