moonbridge

diff moonbridge.c @ 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 78694b82078f
line diff
     1.1 --- a/moonbridge.c	Thu Jan 29 03:22:06 2015 +0100
     1.2 +++ b/moonbridge.c	Thu Jan 29 15:14:58 2015 +0100
     1.3 @@ -2523,6 +2523,36 @@
     1.4  }
     1.5  
     1.6  
     1.7 +/*** Function to modify Lua's library path and/or cpath ***/
     1.8 +
     1.9 +#if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
    1.10 +static void moonbr_modify_path(lua_State *L, char *key, char *value) {
    1.11 +  int stackbase;
    1.12 +  stackbase = lua_gettop(L);
    1.13 +  lua_getglobal(L, "package");
    1.14 +  lua_getfield(L, stackbase+1, key);
    1.15 +  {
    1.16 +    const char *current_str;
    1.17 +    size_t current_strlen;
    1.18 +    luaL_Buffer buf;
    1.19 +    current_str = lua_tolstring(L, stackbase+2, &current_strlen);
    1.20 +    luaL_buffinit(L, &buf);
    1.21 +    if (current_str) {
    1.22 +      lua_pushvalue(L, stackbase+2);
    1.23 +      luaL_addvalue(&buf);
    1.24 +      if (current_strlen && current_str[current_strlen-1] != ';') {
    1.25 +        luaL_addchar(&buf, ';');
    1.26 +      }
    1.27 +    }
    1.28 +    luaL_addstring(&buf, value);
    1.29 +    luaL_pushresult(&buf);
    1.30 +  }
    1.31 +  lua_setfield(L, stackbase+1, key);
    1.32 +  lua_settop(L, stackbase);
    1.33 +}
    1.34 +#endif
    1.35 +
    1.36 +
    1.37  /*** Main function and command line invokation ***/
    1.38  
    1.39  static void moonbr_usage(int err, const char *cmd) {
    1.40 @@ -2683,19 +2713,10 @@
    1.41      lua_atpanic(L, moonbr_lua_panic);
    1.42      luaL_openlibs(L);
    1.43  #ifdef MOONBR_LUA_PATH
    1.44 -    lua_getglobal(L, "package");  // on stack position 1
    1.45 -    lua_getfield(L, 1, "path");  // on stack position 2
    1.46 -    {
    1.47 -      luaL_Buffer buf;
    1.48 -      luaL_buffinit(L, &buf);
    1.49 -      luaL_addstring(&buf, MOONBR_LUA_PATH);
    1.50 -      luaL_addchar(&buf, ';');
    1.51 -      lua_pushvalue(L, 2);
    1.52 -      luaL_addvalue(&buf);
    1.53 -      luaL_pushresult(&buf);
    1.54 -    }
    1.55 -    lua_setfield(L, 1, "path");
    1.56 -    lua_settop(L, 0);
    1.57 +    moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
    1.58 +#endif
    1.59 +#ifdef MOONBR_LUA_CPATH
    1.60 +    moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
    1.61  #endif
    1.62      if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
    1.63        moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");

Impressum / About Us