-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosenv.c
More file actions
295 lines (271 loc) · 8.61 KB
/
Copy pathosenv.c
File metadata and controls
295 lines (271 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif
static void clear_process_env(lua_State *L) {
#ifdef _WIN32
LPCH env = GetEnvironmentStringsA();
if (!env) return;
for (LPCH p = env; *p; p += strlen(p) + 1) {
const char *eq = strchr(p, '=');
// skip hidden vars like "=C:=..."
if (!eq || eq == p)
continue;
size_t len = (size_t)(eq - p);
char *key = (char *)malloc(len + 1);
if (!key) continue;
memcpy(key, p, len);
key[len] = '\0';
SetEnvironmentVariableA(key, NULL);
// sync CRT
if (_putenv_s(key, "") != 0)
luaL_error(L, "failed to sync CRT env (putenv)");
free(key);
}
FreeEnvironmentStringsA(env);
#else
#ifdef __GLIBC__
clearenv();
#else
extern char **environ;
if (!environ) return;
while (*environ) {
const char *entry = *environ;
const char *eq = strchr(entry, '=');
if (!eq) {
unsetenv(entry);
continue;
}
size_t len = (size_t)(eq - entry);
char key[len + 1];
memcpy(key, entry, len);
key[len] = '\0';
unsetenv(key);
}
#endif
#endif
}
static int env_unset(lua_State *L, const char *key) {
#ifdef _WIN32
if (!SetEnvironmentVariableA(key, NULL))
return luaL_error(L, "failed to unset env var");
// sync CRT
if (_putenv_s(key, "") != 0)
return luaL_error(L, "failed to sync CRT env (putenv)");
#else
if (unsetenv(key) != 0)
return luaL_error(L, "failed to unset env var");
#endif
return 0;
}
static int env_set(lua_State *L, const char *key, const char *val, const int force) {
#ifdef _WIN32
if (!force) {
DWORD ret = GetEnvironmentVariableA(key, NULL, 0);
if (ret > 0) return 0;
if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) return 0;
}
if (!SetEnvironmentVariableA(key, val))
return luaL_error(L, "failed to set env var");
// sync CRT
if (_putenv_s(key, val) != 0)
return luaL_error(L, "failed to sync CRT env (putenv)");
#else
if (setenv(key, val, force) != 0)
return luaL_error(L, "failed to set env var");
#endif
return 0;
}
static int env_get_all(lua_State *L) {
lua_newtable(L);
#ifdef _WIN32
LPCH env = GetEnvironmentStringsA();
if (!env) return 1;
for (LPCH p = env; *p; p += strlen(p) + 1) {
const char *eq = strchr(p, '=');
if (!eq || eq == p) continue;
lua_pushlstring(L, p, eq - p);
lua_pushstring(L, eq + 1);
lua_settable(L, -3);
}
FreeEnvironmentStringsA(env);
#else
extern char **environ;
if (environ && *environ) {
for (char **p = environ; *p; p++) {
const char *eq = strchr(*p, '=');
if (!eq) continue;
lua_pushlstring(L, *p, eq - *p);
lua_pushstring(L, eq + 1);
lua_settable(L, -3);
}
}
#endif
return 1;
}
static int get_meta(lua_State *L, const char *name, int idx) {
if (!luaL_getmetafield(L, idx, name)) return 0;
int type = lua_type(L, -1);
if (type == LUA_TFUNCTION) {
return 1;
} else if (type != LUA_TNIL) {
int count = get_meta(L, "__call", -1);
if (count == 1) {
lua_insert(L, -2);
return 2;
} else if (count == 2) {
lua_remove(L, -3);
return 2;
}
}
lua_pop(L, 1);
return 0;
}
static int call_tostring(lua_State *L) {
int count = get_meta(L, "__tostring", -1);
if (count == 1) {
lua_insert(L, -2);
lua_call(L, 1, 1);
return 1;
} else if (count == 2) {
lua_insert(L, -3);
lua_insert(L, -3);
lua_call(L, 2, 1);
return 1;
}
return 0;
}
static int env_assign(lua_State *L, const char *key, int respect) {
const int vt = lua_type(L, -1);
if (vt == LUA_TNIL) {
return env_unset(L, key);
} else if (vt == LUA_TSTRING || vt == LUA_TNUMBER) {
return env_set(L, key, lua_tostring(L, -1), !respect);
} else if (call_tostring(L)) {
return env_set(L, key, luaL_checkstring(L, -1), !respect);
} else {
return luaL_error(L, "env values must be nil to unset, or be strings or numbers, or have a __tostring metamethod. Received a value of type: %s", lua_typename(L, vt));
}
return 0;
}
static int env__newindex(lua_State *L) {
lua_settop(L, 3);
if (lua_istable(L, 2)) {
// CASE 1: set if unset via env[{"KEY"}] = "VAR"
lua_pushinteger(L, 1);
lua_gettable(L, 2);
int type = lua_type(L, -1);
if (type != LUA_TNIL) {
if (type == LUA_TSTRING) {
const char *key = lua_tostring(L, -1);
lua_pop(L, 1);
return env_assign(L, key, 1);
} else if (call_tostring(L)) {
const char *key = luaL_checkstring(L, -1);
lua_pop(L, 1);
return env_assign(L, key, 1);
} else {
return luaL_error(L, "Key for setting if unset must be convertable to a string, but received %s", lua_typename(L, lua_type(L, -1)));
}
} else {
// CASE 2: bulk unset via env[{}] = {...} or env[{}] = "VAR"
lua_pop(L, 1);
}
if (lua_type(L, 3) == LUA_TSTRING) {
const char *key = lua_tostring(L, 3);
return env_unset(L, key);
} else if (lua_type(L, 3) == LUA_TTABLE) {
lua_pushnil(L);
while (lua_next(L, 3) != 0) {
const char *key = luaL_checkstring(L, -1);
int code = env_unset(L, key);
if (code != 0) return code;
lua_pop(L, 1);
}
return 0;
}
return luaL_error(L, "expected string or string[] of variable names to unset when key is a table. Received a value of type: %s", lua_typename(L, lua_type(L, 3)));
}
// CASE 3: normal assignment env["KEY"] = value
return env_assign(L, luaL_checkstring(L, 2), 0);
}
static int env__index(lua_State *L) {
lua_settop(L, 2);
const char *key = luaL_checkstring(L, 2);
#ifdef _WIN32
DWORD size = GetEnvironmentVariableA(key, NULL, 0);
if (size == 0) {
lua_pushnil(L);
return 1;
}
char *buf = (char *)malloc(size);
if (!buf) return luaL_error(L, "out of memory");
GetEnvironmentVariableA(key, buf, size);
lua_pushstring(L, buf);
free(buf);
#else
const char *val = getenv(key);
if (val)
lua_pushstring(L, val);
else
lua_pushnil(L);
#endif
return 1;
}
// -- implicit self param and then:
//@param: table? -- new environment values
//@param: boolean? -- overwrite process env
//@returns: table -- current environment if no args, otherwise self
static int env__call(lua_State *L) {
int nargs = lua_gettop(L);
// env()
if (nargs == 1) return env_get_all(L);
// env(table)
luaL_checktype(L, 2, LUA_TTABLE);
if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env(L);
lua_pushnil(L);
while (lua_next(L, 2) != 0) {
// CASE 1: set if unset via env({ [{"KEY"}] = "VAL" })
if (lua_istable(L, -2)) {
lua_pushinteger(L, 1);
lua_gettable(L, -3);
int type = lua_type(L, -1);
if (type != LUA_TNIL) {
if (type == LUA_TSTRING) {
const char *key = lua_tostring(L, -1);
lua_pop(L, 1);
env_assign(L, key, 1);
} else if (call_tostring(L)) {
const char *key = luaL_checkstring(L, -1);
lua_pop(L, 1);
env_assign(L, key, 1);
} else {
return luaL_error(L, "Key for setting if unset must be convertable to a string, but received %s", lua_typename(L, lua_type(L, -1)));
}
} else {
return luaL_error(L, "Setting only if unset requires a key as in { [{\"KEY\"}] = \"value\" }");
}
} else {
// CASE 2: normal assignment env({ KEY = "VAL" })
env_assign(L, luaL_checkstring(L, -2), 0);
}
lua_pop(L, 1);
}
lua_settop(L, 1);
return 1;
}
int luaopen_osenv(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__call);
lua_setfield(L, -2, "__call");
lua_pushcfunction(L, env__newindex);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2); // setmetatable(t, mt)
return 1; // return env table
}