# HG changeset patch # User jbe # Date 1422540898 -3600 # Node ID 757902555204d96dbc5a69c8acc320684811bbdc # Parent 32e0838d16e679014898eb98831a54f7024b6f44 Proper treatment of double semicolons in package.path and added MOONBR_LUA_CPATH in addition to MOONBR_LUA_PATH diff -r 32e0838d16e6 -r 757902555204 Makefile --- a/Makefile Thu Jan 29 03:22:06 2015 +0100 +++ b/Makefile Thu Jan 29 15:14:58 2015 +0100 @@ -28,10 +28,14 @@ MOONBR_LUA_PATH_DEFINE = "-DMOONBR_LUA_PATH=\"$(MOONBR_LUA_PATH)\"" .endif +.ifdef MOONBR_LUA_CPATH +MOONBR_LUA_CPATH_DEFINE = "-DMOONBR_LUA_CPATH=\"$(MOONBR_LUA_CPATH)\"" +.endif + all:: moonbridge moonbridge: moonbridge.c - cc -Wall -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) + cc -Wall -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) $(MOONBR_LUA_CPATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) clean:: rm -f moonbridge diff -r 32e0838d16e6 -r 757902555204 README --- a/README Thu Jan 29 03:22:06 2015 +0100 +++ b/README Thu Jan 29 15:14:58 2015 +0100 @@ -14,11 +14,12 @@ Further notes: -The moonbridge binary may be compiled with a string that gets prepended to -LUA_PATH in order to allow proper inclusion of "moonbridge_http.lua" -independent of the current working directory. Set the MOONBR_LUA_PATH variable -to a string consisting of the path where "moonbridge_http.lua" will be -installed plus "/?.lua" if you want to use this feature, e.g.: +The moonbridge binary may be compiled with a string that gets appended +to LUA_PATH (package.path) in order to allow proper inclusion of +"moonbridge_http.lua" independent of the current working directory. +Set the MOONBR_LUA_PATH variable to a string consisting of the path +where "moonbridge_http.lua" will be installed plus "/?.lua" if you want +to use this feature, e.g.: $ make MOONBR_LUA_PATH=/usr/local/lib/moonbridge/?.lua diff -r 32e0838d16e6 -r 757902555204 moonbridge.c --- a/moonbridge.c Thu Jan 29 03:22:06 2015 +0100 +++ b/moonbridge.c Thu Jan 29 15:14:58 2015 +0100 @@ -2523,6 +2523,36 @@ } +/*** Function to modify Lua's library path and/or cpath ***/ + +#if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH) +static void moonbr_modify_path(lua_State *L, char *key, char *value) { + int stackbase; + stackbase = lua_gettop(L); + lua_getglobal(L, "package"); + lua_getfield(L, stackbase+1, key); + { + const char *current_str; + size_t current_strlen; + luaL_Buffer buf; + current_str = lua_tolstring(L, stackbase+2, ¤t_strlen); + luaL_buffinit(L, &buf); + if (current_str) { + lua_pushvalue(L, stackbase+2); + luaL_addvalue(&buf); + if (current_strlen && current_str[current_strlen-1] != ';') { + luaL_addchar(&buf, ';'); + } + } + luaL_addstring(&buf, value); + luaL_pushresult(&buf); + } + lua_setfield(L, stackbase+1, key); + lua_settop(L, stackbase); +} +#endif + + /*** Main function and command line invokation ***/ static void moonbr_usage(int err, const char *cmd) { @@ -2683,19 +2713,10 @@ lua_atpanic(L, moonbr_lua_panic); luaL_openlibs(L); #ifdef MOONBR_LUA_PATH - lua_getglobal(L, "package"); // on stack position 1 - lua_getfield(L, 1, "path"); // on stack position 2 - { - luaL_Buffer buf; - luaL_buffinit(L, &buf); - luaL_addstring(&buf, MOONBR_LUA_PATH); - luaL_addchar(&buf, ';'); - lua_pushvalue(L, 2); - luaL_addvalue(&buf); - luaL_pushresult(&buf); - } - lua_setfield(L, 1, "path"); - lua_settop(L, 0); + moonbr_modify_path(L, "path", MOONBR_LUA_PATH); +#endif +#ifdef MOONBR_LUA_CPATH + moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH); #endif if (luaL_newmetatable(L, LUA_FILEHANDLE)) { moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");