diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-09 23:03:21 +0100 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-15 19:17:57 +0100 |
commit | d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2 (patch) | |
tree | 2d4f86ec87eb87a77f6663924aaaa9286756ce3e /src/environment.c | |
parent | d4222ce6da2a2d7179fc79f9d0cc65fd6c09a686 (diff) | |
download | luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.gz luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.bz2 luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.zip |
feat(*): add environment variable and random functions
Diffstat (limited to 'src/environment.c')
-rw-r--r-- | src/environment.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/environment.c b/src/environment.c new file mode 100644 index 0000000..5f1c3da --- /dev/null +++ b/src/environment.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /// @submodule system | ||
2 | #include <lua.h> | ||
3 | #include <lauxlib.h> | ||
4 | #include "compat.h" | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | |||
8 | #ifdef _WIN32 | ||
9 | #include "windows.h" | ||
10 | #endif | ||
11 | |||
12 | /*** | ||
13 | Gets the value of an environment variable. | ||
14 | |||
15 | __NOTE__: Windows has multiple copies of environment variables. For this reason, | ||
16 | the `setenv` function will not work with Lua's `os.getenv` on Windows. If you want | ||
17 | to use `setenv` then consider patching `os.getenv` with this implementation of `getenv`. | ||
18 | @function getenv | ||
19 | @tparam string name name of the environment variable | ||
20 | @treturn string|nil value of the environment variable, or nil if the variable is not set | ||
21 | */ | ||
22 | static int lua_get_environment_variable(lua_State* L) { | ||
23 | const char* variableName = luaL_checkstring(L, 1); | ||
24 | |||
25 | #ifdef _WIN32 | ||
26 | // On Windows, use GetEnvironmentVariable to retrieve the value | ||
27 | DWORD bufferSize = GetEnvironmentVariable(variableName, NULL, 0); | ||
28 | if (bufferSize > 0) { | ||
29 | char* buffer = (char*)malloc(bufferSize); | ||
30 | if (GetEnvironmentVariable(variableName, buffer, bufferSize) > 0) { | ||
31 | lua_pushstring(L, buffer); | ||
32 | free(buffer); | ||
33 | return 1; | ||
34 | } | ||
35 | free(buffer); | ||
36 | } | ||
37 | #else | ||
38 | // On non-Windows platforms, use getenv to retrieve the value | ||
39 | const char* variableValue = getenv(variableName); | ||
40 | if (variableValue != NULL) { | ||
41 | lua_pushstring(L, variableValue); | ||
42 | return 1; | ||
43 | } | ||
44 | #endif | ||
45 | |||
46 | // If the variable is not set or an error occurs, push nil | ||
47 | lua_pushnil(L); | ||
48 | return 1; | ||
49 | } | ||
50 | |||
51 | |||
52 | /*** | ||
53 | Returns a table with all environment variables. | ||
54 | @function getenvs | ||
55 | @treturn table table with all environment variables and their values | ||
56 | */ | ||
57 | static int lua_list_environment_variables(lua_State* L) { | ||
58 | lua_newtable(L); | ||
59 | |||
60 | #ifdef _WIN32 | ||
61 | char* envStrings = GetEnvironmentStrings(); | ||
62 | char* envString = envStrings; | ||
63 | |||
64 | if (envStrings == NULL) { | ||
65 | lua_pushnil(L); | ||
66 | return 1; | ||
67 | } | ||
68 | |||
69 | while (*envString != '\0') { | ||
70 | const char* envVar = envString; | ||
71 | |||
72 | // Split the environment variable into key and value | ||
73 | char* equals = strchr(envVar, '='); | ||
74 | if (equals != NULL) { | ||
75 | lua_pushlstring(L, envVar, equals - envVar); // Push the key | ||
76 | lua_pushstring(L, equals + 1); // Push the value | ||
77 | lua_settable(L, -3); // Set the key-value pair in the table | ||
78 | } | ||
79 | |||
80 | envString += strlen(envString) + 1; | ||
81 | } | ||
82 | |||
83 | FreeEnvironmentStrings(envStrings); | ||
84 | #else | ||
85 | extern char** environ; | ||
86 | |||
87 | if (environ != NULL) { | ||
88 | for (char** envVar = environ; *envVar != NULL; envVar++) { | ||
89 | const char* envVarStr = *envVar; | ||
90 | |||
91 | // Split the environment variable into key and value | ||
92 | char* equals = strchr(envVarStr, '='); | ||
93 | if (equals != NULL) { | ||
94 | lua_pushlstring(L, envVarStr, equals - envVarStr); // Push the key | ||
95 | lua_pushstring(L, equals + 1); // Push the value | ||
96 | lua_settable(L, -3); // Set the key-value pair in the table | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | #endif | ||
101 | |||
102 | return 1; | ||
103 | } | ||
104 | |||
105 | |||
106 | /*** | ||
107 | Sets an environment variable. | ||
108 | |||
109 | __NOTE__: Windows has multiple copies of environment variables. For this reason, the | ||
110 | `setenv` function will not work with Lua's `os.getenv` on Windows. If you want to use | ||
111 | it then consider patching `os.getenv` with the implementation of `system.getenv`. | ||
112 | @function setenv | ||
113 | @tparam string name name of the environment variable | ||
114 | @tparam[opt] string value value of the environment variable, if `nil` the variable will be deleted (on | ||
115 | Windows, setting an empty string, will also delete the variable) | ||
116 | @treturn boolean success | ||
117 | */ | ||
118 | static int lua_set_environment_variable(lua_State* L) { | ||
119 | const char* variableName = luaL_checkstring(L, 1); | ||
120 | const char* variableValue = luaL_optstring(L, 2, NULL); | ||
121 | |||
122 | #ifdef _WIN32 | ||
123 | // if (variableValue == NULL) { | ||
124 | // // If the value is nil, delete the environment variable | ||
125 | // if (SetEnvironmentVariable(variableName, NULL)) { | ||
126 | // lua_pushboolean(L, 1); | ||
127 | // } else { | ||
128 | // lua_pushboolean(L, 0); | ||
129 | // } | ||
130 | // } else { | ||
131 | // Set the environment variable with the provided value | ||
132 | if (SetEnvironmentVariable(variableName, variableValue)) { | ||
133 | lua_pushboolean(L, 1); | ||
134 | } else { | ||
135 | lua_pushboolean(L, 0); | ||
136 | } | ||
137 | // } | ||
138 | #else | ||
139 | if (variableValue == NULL) { | ||
140 | // If the value is nil, delete the environment variable | ||
141 | if (unsetenv(variableName) == 0) { | ||
142 | lua_pushboolean(L, 1); | ||
143 | } else { | ||
144 | lua_pushboolean(L, 0); | ||
145 | } | ||
146 | } else { | ||
147 | // Set the environment variable with the provided value | ||
148 | if (setenv(variableName, variableValue, 1) == 0) { | ||
149 | lua_pushboolean(L, 1); | ||
150 | } else { | ||
151 | lua_pushboolean(L, 0); | ||
152 | } | ||
153 | } | ||
154 | #endif | ||
155 | |||
156 | return 1; | ||
157 | } | ||
158 | |||
159 | |||
160 | |||
161 | static luaL_Reg func[] = { | ||
162 | { "getenv", lua_get_environment_variable }, | ||
163 | { "setenv", lua_set_environment_variable }, | ||
164 | { "getenvs", lua_list_environment_variables }, | ||
165 | { NULL, NULL } | ||
166 | }; | ||
167 | |||
168 | /*------------------------------------------------------------------------- | ||
169 | * Initializes module | ||
170 | *-------------------------------------------------------------------------*/ | ||
171 | void environment_open(lua_State *L) { | ||
172 | luaL_setfuncs(L, func, 0); | ||
173 | } | ||