Modify package.cpath (#1371)

* luaa.c: Remove useless stack operation

We get package.loaded and immediately throw away the result. That's
pointless, so remove this.

Signed-off-by: Uli Schlachter <psychon@znc.in>

* Refactor modification of package.path

Awesome adds various entries to package.path during startup. This commit
moves that into a helper function. No functional changes intended. The
only change I did to the code was changing a call to lua_type(L, 2) into
lua_type(L, -1);.

Signed-off-by: Uli Schlachter <psychon@znc.in>

* Modify package.cpath just like package.path

This adds, for example, paths specified via the --search argument also
to package.cpath.

Fixes: https://github.com/awesomeWM/awesome/issues/1248
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2017-01-09 01:03:16 +01:00 committed by Daniel Hahler
parent 75431d90c9
commit c96987909e
1 changed files with 53 additions and 29 deletions

82
luaa.c
View File

@ -630,6 +630,54 @@ setup_awesome_signals(lua_State *L)
lua_pop(L, 1);
}
/* Add things to the string on top of the stack */
static void
add_to_search_path(lua_State *L, string_array_t *searchpath, bool for_lua)
{
if (LUA_TSTRING != lua_type(L, -1))
{
warn("package.path is not a string");
return;
}
foreach(entry, *searchpath)
{
int components;
size_t len = a_strlen(*entry);
lua_pushliteral(L, ";");
lua_pushlstring(L, *entry, len);
if (for_lua)
lua_pushliteral(L, "/?.lua");
else
lua_pushliteral(L, "/?.so");
lua_concat(L, 3);
if (for_lua)
{
lua_pushliteral(L, ";");
lua_pushlstring(L, *entry, len);
lua_pushliteral(L, "/?/init.lua");
lua_concat(L, 3);
components = 2;
} else {
components = 1;
}
lua_concat(L, components + 1); /* concatenate with string on top of the stack */
}
/* add Lua lib path (/usr/share/awesome/lib by default) */
if (for_lua)
{
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
lua_concat(L, 3); /* concatenate with thing on top of the stack when we were called */
} else {
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.so");
lua_concat(L, 2); /* concatenate with thing on top of the stack when we were called */
}
}
/** Initialize the Lua VM
* \param xdg An xdg handle to use to get XDG basedir.
*/
@ -747,38 +795,14 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath)
return;
}
lua_getfield(L, 1, "path");
if (LUA_TSTRING != lua_type(L, 2))
{
warn("package.path is not a string");
lua_pop(L, 1);
return;
}
foreach(entry, *searchpath)
{
size_t len = a_strlen(*entry);
lua_pushliteral(L, ";");
lua_pushlstring(L, *entry, len);
lua_pushliteral(L, "/?.lua");
lua_concat(L, 3);
lua_pushliteral(L, ";");
lua_pushlstring(L, *entry, len);
lua_pushliteral(L, "/?/init.lua");
lua_concat(L, 3);
lua_concat(L, 3); /* concatenate with package.path */
}
/* add Lua lib path (/usr/share/awesome/lib by default) */
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
lua_concat(L, 3); /* concatenate with package.path */
add_to_search_path(L, searchpath, true);
lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
lua_getfield(L, 1, "loaded");
lua_getfield(L, 1, "cpath");
add_to_search_path(L, searchpath, false);
lua_setfield(L, 1, "cpath"); /* package.cpath = "concatenated string" */
lua_pop(L, 2); /* pop "package" and "package.loaded" */
lua_pop(L, 1); /* pop "package" */
}
static void