moonbridge

changeset 9:757902555204

Proper treatment of double semicolons in package.path and added MOONBR_LUA_CPATH in addition to MOONBR_LUA_PATH
author jbe
date Thu Jan 29 15:14:58 2015 +0100 (2015-01-29)
parents 32e0838d16e6
children 7e6faff049c3
files Makefile README moonbridge.c
line diff
     1.1 --- a/Makefile	Thu Jan 29 03:22:06 2015 +0100
     1.2 +++ b/Makefile	Thu Jan 29 15:14:58 2015 +0100
     1.3 @@ -28,10 +28,14 @@
     1.4  MOONBR_LUA_PATH_DEFINE = "-DMOONBR_LUA_PATH=\"$(MOONBR_LUA_PATH)\""
     1.5  .endif
     1.6  
     1.7 +.ifdef MOONBR_LUA_CPATH
     1.8 +MOONBR_LUA_CPATH_DEFINE = "-DMOONBR_LUA_CPATH=\"$(MOONBR_LUA_CPATH)\""
     1.9 +.endif
    1.10 +
    1.11  all:: moonbridge
    1.12  
    1.13  moonbridge: moonbridge.c
    1.14 -	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)
    1.15 +	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)
    1.16  
    1.17  clean::
    1.18  	rm -f moonbridge
     2.1 --- a/README	Thu Jan 29 03:22:06 2015 +0100
     2.2 +++ b/README	Thu Jan 29 15:14:58 2015 +0100
     2.3 @@ -14,11 +14,12 @@
     2.4  
     2.5  Further notes:
     2.6  
     2.7 -The moonbridge binary may be compiled with a string that gets prepended to
     2.8 -LUA_PATH in order to allow proper inclusion of "moonbridge_http.lua"
     2.9 -independent of the current working directory. Set the MOONBR_LUA_PATH variable
    2.10 -to a string consisting of the path where "moonbridge_http.lua" will be
    2.11 -installed plus "/?.lua" if you want to use this feature, e.g.:
    2.12 +The moonbridge binary may be compiled with a string that gets appended
    2.13 +to LUA_PATH (package.path) in order to allow proper inclusion of
    2.14 +"moonbridge_http.lua" independent of the current working directory.
    2.15 +Set the MOONBR_LUA_PATH variable to a string consisting of the path
    2.16 +where "moonbridge_http.lua" will be installed plus "/?.lua" if you want
    2.17 +to use this feature, e.g.:
    2.18  
    2.19  $ make MOONBR_LUA_PATH=/usr/local/lib/moonbridge/?.lua
    2.20  
     3.1 --- a/moonbridge.c	Thu Jan 29 03:22:06 2015 +0100
     3.2 +++ b/moonbridge.c	Thu Jan 29 15:14:58 2015 +0100
     3.3 @@ -2523,6 +2523,36 @@
     3.4  }
     3.5  
     3.6  
     3.7 +/*** Function to modify Lua's library path and/or cpath ***/
     3.8 +
     3.9 +#if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
    3.10 +static void moonbr_modify_path(lua_State *L, char *key, char *value) {
    3.11 +  int stackbase;
    3.12 +  stackbase = lua_gettop(L);
    3.13 +  lua_getglobal(L, "package");
    3.14 +  lua_getfield(L, stackbase+1, key);
    3.15 +  {
    3.16 +    const char *current_str;
    3.17 +    size_t current_strlen;
    3.18 +    luaL_Buffer buf;
    3.19 +    current_str = lua_tolstring(L, stackbase+2, &current_strlen);
    3.20 +    luaL_buffinit(L, &buf);
    3.21 +    if (current_str) {
    3.22 +      lua_pushvalue(L, stackbase+2);
    3.23 +      luaL_addvalue(&buf);
    3.24 +      if (current_strlen && current_str[current_strlen-1] != ';') {
    3.25 +        luaL_addchar(&buf, ';');
    3.26 +      }
    3.27 +    }
    3.28 +    luaL_addstring(&buf, value);
    3.29 +    luaL_pushresult(&buf);
    3.30 +  }
    3.31 +  lua_setfield(L, stackbase+1, key);
    3.32 +  lua_settop(L, stackbase);
    3.33 +}
    3.34 +#endif
    3.35 +
    3.36 +
    3.37  /*** Main function and command line invokation ***/
    3.38  
    3.39  static void moonbr_usage(int err, const char *cmd) {
    3.40 @@ -2683,19 +2713,10 @@
    3.41      lua_atpanic(L, moonbr_lua_panic);
    3.42      luaL_openlibs(L);
    3.43  #ifdef MOONBR_LUA_PATH
    3.44 -    lua_getglobal(L, "package");  // on stack position 1
    3.45 -    lua_getfield(L, 1, "path");  // on stack position 2
    3.46 -    {
    3.47 -      luaL_Buffer buf;
    3.48 -      luaL_buffinit(L, &buf);
    3.49 -      luaL_addstring(&buf, MOONBR_LUA_PATH);
    3.50 -      luaL_addchar(&buf, ';');
    3.51 -      lua_pushvalue(L, 2);
    3.52 -      luaL_addvalue(&buf);
    3.53 -      luaL_pushresult(&buf);
    3.54 -    }
    3.55 -    lua_setfield(L, 1, "path");
    3.56 -    lua_settop(L, 0);
    3.57 +    moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
    3.58 +#endif
    3.59 +#ifdef MOONBR_LUA_CPATH
    3.60 +    moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
    3.61  #endif
    3.62      if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
    3.63        moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");

Impressum / About Us