summaryrefslogtreecommitdiff
path: root/gnu/packages/patches/lua-5.x-search-path-helpers.patch
blob: 9bcd9fb9e734fac9246d33ce8618a3bde130b7e7 (plain)
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
Define two helper functions that are used in other Lua patches.

The functions construct default values for LUA_PATH and LUA_CPATH (using '?'s
as wildcards) from GUIX_LUA_PATH and GUIX_LUA_CPATH, respectively.

--- /dev/null
+++ b/src/guixpaths.c
@@ -0,0 +1,54 @@
+const char* guix_path (lua_State* L) {
+  luaL_Buffer buf;
+  luaL_buffinit(L, &buf);
+
+  const char* next;
+  const char* source = getenv("GUIX_LUA_PATH");
+  if (source != NULL) {
+    while ((next = strstr(source, ";")) != NULL) {
+      luaL_addlstring(&buf, source, next - source);  /* push prefix */
+      luaL_addstring(&buf, "/?.lua;");
+      luaL_addlstring(&buf, source, next - source);  /* push prefix */
+      luaL_addstring(&buf, "/?/init.lua;");
+      source = next + 1;  /* continue after the semicolon */
+    }
+    if (*source != '\0') {
+      luaL_addstring(&buf, source);
+      luaL_addstring(&buf, "/?.lua;");
+      luaL_addstring(&buf, source);
+      luaL_addstring(&buf, "/?/init.lua;");
+    }
+  }
+
+  /* Then add the local directory last */
+  luaL_addstring(&buf, "./?.lua;" "./?/init.lua");
+  luaL_pushresult(&buf);
+  return lua_tostring(L, -1);
+}
+
+
+const char* guix_cpath (lua_State* L) {
+  luaL_Buffer buf;
+  luaL_buffinit(L, &buf);
+
+  const char* next;
+  const char* source = getenv("GUIX_LUA_CPATH");
+  if (source != NULL) {
+    while ((next = strstr(source, ";")) != NULL) {
+      luaL_addlstring(&buf, source, next - source);  /* push prefix */
+      luaL_addstring(&buf, "/?.so;");
+      source = next + 1;  /* continue after the semicolon */
+    }
+    if (*source != '\0') {
+      luaL_addstring(&buf, source);
+      luaL_addstring(&buf, "/?.so;");
+    }
+  }
+
+  /* Then add the local directory last */
+  luaL_addstring(&buf, "./?.so");
+
+  luaL_pushresult(&buf);
+  return lua_tostring(L, -1);
+}
+