Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ TESTDIR := $(SRC)/tests
SRCS := $(SRC)/src/tomlua.c \
$(SRC)/src/decode.c \
$(SRC)/src/encode.c \
$(SRC)/src/env.c \
$(SRC)/src/dates.c

CLI_SRCS := $(SRC)/src/tomlua_cli.c \
Expand Down
54 changes: 54 additions & 0 deletions src/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#ifdef _WIN32
#include <windows.h>
#endif

static int env__newindex(lua_State *L) {
const char *key = luaL_checkstring(L, 2);
#ifdef _WIN32
if (lua_isnil(L, 3)) {
if (SetEnvironmentVariable(key, NULL) == 0) {
return luaL_error(L, "failed to unset env var");
}
} else if (lua_type(L, 3) == LUA_TSTRING) {
if (SetEnvironmentVariable(key, lua_tostring(L, 3)) == 0) {
return luaL_error(L, "failed to set env var");
}
#else
if (lua_isnil(L, 3)) {
if (unsetenv(key) != 0) {
return luaL_error(L, "failed to unset env var");
}
} else if (lua_type(L, 3) == LUA_TSTRING) {
if (setenv(key, lua_tostring(L, 3), 1) != 0) {
return luaL_error(L, "failed to set env var");
}
#endif
} else {
return luaL_error(L, "env values must be strings or nil");
}
return 0;
}

static int env__index(lua_State *L) {
const char *key = luaL_checkstring(L, 2);
const char *val = getenv(key);
if (val)
lua_pushstring(L, val);
else
lua_pushnil(L);
return 1;
}

int luaopen_tomlua_env(lua_State *L) {
lua_newtable(L); // module table
lua_newtable(L); // metatable
lua_pushcfunction(L, env__index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, env__newindex);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2); // setmetatable(t, mt)
return 1; // return env table
}
4 changes: 4 additions & 0 deletions src/tomlua_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string.h>

extern int luaopen_tomlua(lua_State *L);
extern int luaopen_tomlua_env(lua_State *L);

static void inject_tomlua(lua_State *L) {
lua_getglobal(L, "package");
Expand All @@ -16,6 +17,9 @@ static void inject_tomlua(lua_State *L) {
lua_pushcfunction(L, luaopen_tomlua);
lua_setfield(L, -2, "tomlua");

lua_pushcfunction(L, luaopen_tomlua_env);
lua_setfield(L, -2, "tomlua.env");

lua_pop(L, 2);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/opts_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ define("opts({ int_keys = true }) doesn't copy", function()
local opts = tomlua.opts()
ok(type(opts) == "table", "opts when called with no args should return a table")
end)

define("require('tomlua.env') tests", function()
local env = require("tomlua.env")
ok(env ~= nil, "tomlua.env should be available")
env.TESTVARIABLE = "HELLO"
ok(env.TESTVARIABLE == os.getenv("TESTVARIABLE"), "tomlua.env should get the env var")
env.TESTVARIABLE = nil
ok(env.TESTVARIABLE == os.getenv("TESTVARIABLE"), "tomlua.env should be removed just like the env var")
end)