A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 320 lines 9.9 kB view raw
1/* 2** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $ 3** Dynamic library loader for Lua 4** See Copyright Notice in lua.h 5** 6** This module contains an implementation of loadlib for Unix systems 7** that have dlfcn, an implementation for Darwin (Mac OS X), an 8** implementation for Windows, and a stub for other systems. 9*/ 10 11 12#include <stdlib.h> 13#include <string.h> 14 15 16#define loadlib_c 17#define LUA_LIB 18 19#include "lua.h" 20 21#include "lauxlib.h" 22#include "lualib.h" 23#include "rocklib.h" 24#include "rocklibc.h" 25 26#define setprogdir(L) ((void)0) /* ROCKLUA ADDED */ 27 28 29/* 30** {====================================================== 31** 'require' function 32** ======================================================= 33*/ 34 35 36static int readable (const char *filename) { 37 int f = rb->open(filename, O_RDONLY); /* try to open file */ 38 if (f < 0) return 0; /* open failed */ 39 rb->close(f); 40 return 1; 41} 42 43 44static const char *pushnexttemplate (lua_State *L, const char *path) { 45 const char *l; 46 while (*path == *LUA_PATHSEP) path++; /* skip separators */ 47 if (*path == '\0') return NULL; /* no more templates */ 48 l = strchr(path, *LUA_PATHSEP); /* find next separator */ 49 if (l == NULL) l = path + strlen(path); 50 lua_pushlstring(L, path, l - path); /* template */ 51 return l; 52} 53 54 55static const char *findfile (lua_State *L, const char *name, 56 const char *pname) { 57 get_current_path(L, 2); /* ROCKLUA ADDED */ 58 const char *current_path = lua_tostring(L, -1); 59 const char *path; 60 61 name = luaL_gsub(L, name, ".", LUA_DIRSEP); 62 lua_getfield(L, LUA_ENVIRONINDEX, pname); 63 path = lua_tostring(L, -1); 64 if (path == NULL) 65 luaL_error(L, LUA_QL("package.%s") " must be a string", pname); 66 lua_pushliteral(L, ""); /* error accumulator */ 67 while ((path = pushnexttemplate(L, path)) != NULL) { 68 const char *filename; 69 filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); 70 if(current_path != NULL) filename = luaL_gsub(L, filename, "$", current_path); 71 lua_remove(L, -2); /* remove path template */ 72 if (readable(filename)) /* does file exist and is readable? */ 73 return filename; /* return that file name */ 74 lua_pushfstring(L, "\n\tno file " LUA_QS, filename); 75 lua_remove(L, -2); /* remove file name */ 76 lua_concat(L, 2); /* add entry to possible error message */ 77 } 78 return NULL; /* not found */ 79} 80 81 82static void loaderror (lua_State *L, const char *filename) { 83 luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", 84 lua_tostring(L, 1), filename, lua_tostring(L, -1)); 85} 86 87 88static int loader_Lua (lua_State *L) { 89 const char *filename; 90 const char *name = luaL_checkstring(L, 1); 91 filename = findfile(L, name, "path"); 92 if (filename == NULL) return 1; /* library not found in this path */ 93 if (luaL_loadfile(L, filename) != 0) 94 loaderror(L, filename); 95 return 1; /* library loaded successfully */ 96} 97 98 99static int loader_preload (lua_State *L) { 100 const char *name = luaL_checkstring(L, 1); 101 lua_getfield(L, LUA_ENVIRONINDEX, "preload"); 102 if (!lua_istable(L, -1)) 103 luaL_error(L, LUA_QL("package.preload") " must be a table"); 104 lua_getfield(L, -1, name); 105 if (lua_isnil(L, -1)) /* not found? */ 106 lua_pushfstring(L, "\n\tno field package.preload['%s']", name); 107 return 1; 108} 109 110 111static const int sentinel_ = 0; 112#define sentinel ((void *)&sentinel_) 113 114 115static int ll_require (lua_State *L) { 116 const char *name = luaL_checkstring(L, 1); 117 int i; 118 lua_settop(L, 1); /* _LOADED table will be at index 2 */ 119 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 120 lua_getfield(L, 2, name); 121 if (lua_toboolean(L, -1)) { /* is it there? */ 122 if (lua_touserdata(L, -1) == sentinel) /* check loops */ 123 luaL_error(L, "loop or previous error loading module " LUA_QS, name); 124 return 1; /* package is already loaded */ 125 } 126 /* else must load it; iterate over available loaders */ 127 lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); 128 if (!lua_istable(L, -1)) 129 luaL_error(L, LUA_QL("package.loaders") " must be a table"); 130 lua_pushliteral(L, ""); /* error message accumulator */ 131 for (i=1; ; i++) { 132 lua_rawgeti(L, -2, i); /* get a loader */ 133 if (lua_isnil(L, -1)) { 134 lua_setfield(L, 2, name); /* _LOADED[name] = nil */ 135 luaL_error(L, "module " LUA_QS " not found:%s", 136 name, lua_tostring(L, -2)); 137 } 138 lua_pushstring(L, name); 139 lua_call(L, 1, 1); /* call it */ 140 if (lua_isfunction(L, -1)) /* did it find module? */ 141 break; /* module loaded successfully */ 142 else if (lua_isstring(L, -1)) /* loader returned error message? */ 143 lua_concat(L, 2); /* accumulate it */ 144 else 145 lua_pop(L, 1); 146 } 147 lua_pushlightuserdata(L, sentinel); 148 lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ 149 lua_pushstring(L, name); /* pass name as argument to module */ 150 lua_call(L, 1, 1); /* run loaded module */ 151 if (!lua_isnil(L, -1)) /* non-nil return? */ 152 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ 153 lua_getfield(L, 2, name); 154 if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ 155 lua_pushboolean(L, 1); /* use true as result */ 156 lua_pushvalue(L, -1); /* extra copy to be returned */ 157 lua_setfield(L, 2, name); /* _LOADED[name] = true */ 158 } 159 return 1; 160} 161 162/* }====================================================== */ 163 164 165 166/* 167** {====================================================== 168** 'module' function 169** ======================================================= 170*/ 171 172 173static void setfenv (lua_State *L) { 174 lua_Debug ar; 175 if (lua_getstack(L, 1, &ar) == 0 || 176 lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ 177 lua_iscfunction(L, -1)) 178 luaL_error(L, LUA_QL("module") " not called from a Lua function"); 179 lua_pushvalue(L, -2); 180 lua_setfenv(L, -2); 181 lua_pop(L, 1); 182} 183 184 185static void dooptions (lua_State *L, int n) { 186 int i; 187 for (i = 2; i <= n; i++) { 188 lua_pushvalue(L, i); /* get option (a function) */ 189 lua_pushvalue(L, -2); /* module */ 190 lua_call(L, 1, 0); 191 } 192} 193 194 195static void modinit (lua_State *L, const char *modname) { 196 const char *dot; 197 lua_pushvalue(L, -1); 198 lua_setfield(L, -2, "_M"); /* module._M = module */ 199 lua_pushstring(L, modname); 200 lua_setfield(L, -2, "_NAME"); 201 dot = strrchr(modname, '.'); /* look for last dot in module name */ 202 if (dot == NULL) dot = modname; 203 else dot++; 204 /* set _PACKAGE as package name (full module name minus last part) */ 205 lua_pushlstring(L, modname, dot - modname); 206 lua_setfield(L, -2, "_PACKAGE"); 207} 208 209 210static int ll_module (lua_State *L) { 211 const char *modname = luaL_checkstring(L, 1); 212 int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ 213 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 214 lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ 215 if (!lua_istable(L, -1)) { /* not found? */ 216 lua_pop(L, 1); /* remove previous result */ 217 /* try global variable (and create one if it does not exist) */ 218 if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) 219 return luaL_error(L, "name conflict for module " LUA_QS, modname); 220 lua_pushvalue(L, -1); 221 lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ 222 } 223 /* check whether table already has a _NAME field */ 224 lua_getfield(L, -1, "_NAME"); 225 if (!lua_isnil(L, -1)) /* is table an initialized module? */ 226 lua_pop(L, 1); 227 else { /* no; initialize it */ 228 lua_pop(L, 1); 229 modinit(L, modname); 230 } 231 lua_pushvalue(L, -1); 232 setfenv(L); 233 dooptions(L, loaded - 1); 234 return 0; 235} 236 237 238static int ll_seeall (lua_State *L) { 239 luaL_checktype(L, 1, LUA_TTABLE); 240 if (!lua_getmetatable(L, 1)) { 241 lua_createtable(L, 0, 1); /* create new metatable */ 242 lua_pushvalue(L, -1); 243 lua_setmetatable(L, 1); 244 } 245 lua_pushvalue(L, LUA_GLOBALSINDEX); 246 lua_setfield(L, -2, "__index"); /* mt.__index = _G */ 247 return 0; 248} 249 250 251/* }====================================================== */ 252 253 254 255/* auxiliary mark (for internal use) */ 256#define AUXMARK "\1" 257 258static void setpath (lua_State *L, const char *fieldname, const char *envname, 259 const char *def) { 260 (void)envname; 261 lua_pushstring(L, def); /* use default */ 262 setprogdir(L); 263 lua_setfield(L, -2, fieldname); 264} 265 266 267static const luaL_Reg pk_funcs[] = { 268 {"seeall", ll_seeall}, 269 {NULL, NULL} 270}; 271 272 273static const luaL_Reg ll_funcs[] = { 274 {"module", ll_module}, 275 {"require", ll_require}, 276 {NULL, NULL} 277}; 278 279 280static const lua_CFunction loaders[] = 281 {loader_preload, loader_Lua, NULL}; 282 283 284LUALIB_API int luaopen_package (lua_State *L) { 285 int i; 286 /* create new type _LOADLIB */ 287 luaL_newmetatable(L, "_LOADLIB"); 288 /* create `package' table */ 289 luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 290#if defined(LUA_COMPAT_LOADLIB) 291 lua_getfield(L, -1, "loadlib"); 292 lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); 293#endif 294 lua_pushvalue(L, -1); 295 lua_replace(L, LUA_ENVIRONINDEX); 296 /* create `loaders' table */ 297 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); 298 /* fill it with pre-defined loaders */ 299 for (i=0; loaders[i] != NULL; i++) { 300 lua_pushcfunction(L, loaders[i]); 301 lua_rawseti(L, -2, i+1); 302 } 303 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ 304 setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ 305 /* store config information */ 306 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" 307 LUA_EXECDIR "\n" LUA_IGMARK); 308 lua_setfield(L, -2, "config"); 309 /* set field `loaded' */ 310 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); 311 lua_setfield(L, -2, "loaded"); 312 /* set field `preload' */ 313 lua_newtable(L); 314 lua_setfield(L, -2, "preload"); 315 lua_pushvalue(L, LUA_GLOBALSINDEX); 316 luaL_register(L, NULL, ll_funcs); /* open lib into global table */ 317 lua_pop(L, 1); 318 return 1; /* return 'package' table */ 319} 320