-void lua_setpath(lua_State *l) {
- char fullpath[LUA_PATHLEN], *prev = NULL;
-
- int top = lua_gettop(l);
-
- lua_getglobal(l, "package");
- if(!lua_istable(l, 1)) {
- Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
- return;
- }
-
- lua_pushstring(l, "path");
- lua_gettable(l, -2);
-
- if(lua_isstring(l, -1))
- prev = (char *)lua_tostring(l, -1);
-
- if(prev) {
- snprintf(fullpath, sizeof(fullpath), "%s;%s/?%s", prev, cpath->content, suffix->content);
- } else {
- snprintf(fullpath, sizeof(fullpath), "%s/?%s", cpath->content, suffix->content);
- }
-
- /* pop broke! */
-
- lua_getglobal(l, "package");
- if(!lua_istable(l, 1)) {
- Error("lua", ERR_ERROR, "Unable to set package.path (package is not a table).");
- return;
- }
-
- lua_pushstring(l, "path");
-
- lua_pushstring(l, fullpath);
- lua_settable(l, -3);