Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
loadlib.c File Reference
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define loadlib_c
 
#define LUA_LIB
 
#define LUA_PATH   "LUA_PATH"
 
#define LUA_CPATH   "LUA_CPATH"
 
#define LUA_PATHSUFFIX   "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
 
#define LUA_PATHVERSION   LUA_PATH LUA_PATHSUFFIX
 
#define LUA_CPATHVERSION   LUA_CPATH LUA_PATHSUFFIX
 
#define LUA_PATH_SEP   ";"
 
#define LUA_PATH_MARK   "?"
 
#define LUA_EXEC_DIR   "!"
 
#define LUA_IGMARK   "-"
 
#define LUA_CSUBSEP   LUA_DIRSEP
 
#define LUA_LSUBSEP   LUA_DIRSEP
 
#define LUA_POF   "luaopen_"
 
#define LUA_OFSEP   "_"
 
#define CLIBS   "_CLIBS"
 
#define LIB_FAIL   "open"
 
#define ERRLIB   1
 
#define ERRFUNC   2
 
#define setprogdir(L)   ((void)0)
 
#define LIB_FAIL   "absent"
 
#define DLMSG   "dynamic libraries not enabled; check your Lua installation"
 
#define AUXMARK   "\1"
 

Functions

static void ll_unloadlib (void *lib)
 
static void * ll_load (lua_State *L, const char *path, int seeglb)
 
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym)
 
static void * ll_checkclib (lua_State *L, const char *path)
 
static void ll_addtoclib (lua_State *L, const char *path, void *plib)
 
static int gctm (lua_State *L)
 
static int ll_loadfunc (lua_State *L, const char *path, const char *sym)
 
static int ll_loadlib (lua_State *L)
 
static int readable (const char *filename)
 
static const char * pushnexttemplate (lua_State *L, const char *path)
 
static const char * searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep)
 
static int ll_searchpath (lua_State *L)
 
static const char * findfile (lua_State *L, const char *name, const char *pname, const char *dirsep)
 
static int checkload (lua_State *L, int stat, const char *filename)
 
static int searcher_Lua (lua_State *L)
 
static int loadfunc (lua_State *L, const char *filename, const char *modname)
 
static int searcher_C (lua_State *L)
 
static int searcher_Croot (lua_State *L)
 
static int searcher_preload (lua_State *L)
 
static void findloader (lua_State *L, const char *name)
 
static int ll_require (lua_State *L)
 
static int noenv (lua_State *L)
 
static void setpath (lua_State *L, const char *fieldname, const char *envname1, const char *envname2, const char *def)
 
static void createsearcherstable (lua_State *L)
 
LUAMOD_API int luaopen_package (lua_State *L)
 

Variables

static const luaL_Reg pk_funcs []
 
static const luaL_Reg ll_funcs []
 

Macro Definition Documentation

◆ AUXMARK

#define AUXMARK   "\1"

Definition at line 622 of file lua-5.2.4/src/loadlib.c.

Referenced by setpath().

◆ CLIBS

#define CLIBS   "_CLIBS"

Definition at line 96 of file lua-5.2.4/src/loadlib.c.

Referenced by ll_addtoclib(), ll_checkclib(), and luaopen_package().

◆ DLMSG

#define DLMSG   "dynamic libraries not enabled; check your Lua installation"

Definition at line 226 of file lua-5.2.4/src/loadlib.c.

Referenced by ll_load(), and ll_sym().

◆ ERRFUNC

#define ERRFUNC   2

Definition at line 103 of file lua-5.2.4/src/loadlib.c.

Referenced by ll_loadfunc(), loadfunc(), and searcher_Croot().

◆ ERRLIB

#define ERRLIB   1

Definition at line 102 of file lua-5.2.4/src/loadlib.c.

Referenced by ll_loadfunc(), and ll_loadlib().

◆ LIB_FAIL [1/2]

#define LIB_FAIL   "open"

Definition at line 98 of file lua-5.2.4/src/loadlib.c.

Referenced by ll_loadlib().

◆ LIB_FAIL [2/2]

#define LIB_FAIL   "absent"

Definition at line 98 of file lua-5.2.4/src/loadlib.c.

◆ loadlib_c

#define loadlib_c

Definition at line 24 of file lua-5.2.4/src/loadlib.c.

◆ LUA_CPATH

#define LUA_CPATH   "LUA_CPATH"

Definition at line 42 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_CPATHVERSION

#define LUA_CPATHVERSION   LUA_CPATH LUA_PATHSUFFIX

Definition at line 48 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_CSUBSEP

#define LUA_CSUBSEP   LUA_DIRSEP

Definition at line 80 of file lua-5.2.4/src/loadlib.c.

Referenced by searcher_C(), and searcher_Croot().

◆ LUA_EXEC_DIR

#define LUA_EXEC_DIR   "!"

Definition at line 66 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_IGMARK

#define LUA_IGMARK   "-"

Definition at line 69 of file lua-5.2.4/src/loadlib.c.

Referenced by loadfunc(), and luaopen_package().

◆ LUA_LIB

#define LUA_LIB

Definition at line 25 of file lua-5.2.4/src/loadlib.c.

◆ LUA_LSUBSEP

#define LUA_LSUBSEP   LUA_DIRSEP

Definition at line 84 of file lua-5.2.4/src/loadlib.c.

Referenced by searcher_Lua().

◆ LUA_OFSEP

#define LUA_OFSEP   "_"

Definition at line 92 of file lua-5.2.4/src/loadlib.c.

Referenced by loadfunc().

◆ LUA_PATH

#define LUA_PATH   "LUA_PATH"

Definition at line 38 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_PATH_MARK

#define LUA_PATH_MARK   "?"

Definition at line 63 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package(), and searchpath().

◆ LUA_PATH_SEP

#define LUA_PATH_SEP   ";"

◆ LUA_PATHSUFFIX

#define LUA_PATHSUFFIX   "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR

Definition at line 45 of file lua-5.2.4/src/loadlib.c.

◆ LUA_PATHVERSION

#define LUA_PATHVERSION   LUA_PATH LUA_PATHSUFFIX

Definition at line 47 of file lua-5.2.4/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_POF

#define LUA_POF   "luaopen_"

Definition at line 89 of file lua-5.2.4/src/loadlib.c.

Referenced by loadfunc().

◆ setprogdir

#define setprogdir ( L)    ((void)0)

Definition at line 105 of file lua-5.2.4/src/loadlib.c.

Referenced by setpath().

Function Documentation

◆ checkload()

static int checkload ( lua_State * L,
int stat,
const char * filename )
static

Definition at line 398 of file lua-5.2.4/src/loadlib.c.

398 {
399 if (stat) { /* module loaded successfully? */
400 lua_pushstring(L, filename); /* will be 2nd argument to module */
401 return 2; /* return open function and file name */
402 }
403 else
404 return luaL_error(L, "error loading module " LUA_QS
405 " from file " LUA_QS ":\n\t%s",
406 lua_tostring(L, 1), filename, lua_tostring(L, -1));
407}
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define lua_tostring(L, i)
#define LUA_QS

References lua_pushstring(), LUA_QS, lua_tostring, and luaL_error().

Referenced by searcher_C(), searcher_Croot(), and searcher_Lua().

◆ createsearcherstable()

static void createsearcherstable ( lua_State * L)
static

Definition at line 675 of file lua-5.2.4/src/loadlib.c.

675 {
676 static const lua_CFunction searchers[] =
678 int i;
679 /* create 'searchers' table */
680 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
681 /* fill it with pre-defined searchers */
682 for (i=0; searchers[i] != NULL; i++) {
683 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
684 lua_pushcclosure(L, searchers[i], 1);
685 lua_rawseti(L, -2, i+1);
686 }
687}
#define NULL
Definition gmacros.h:924
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
int(* lua_CFunction)(lua_State *L)
static int searcher_Croot(lua_State *L)
static int searcher_preload(lua_State *L)
static int searcher_Lua(lua_State *L)
static int searcher_C(lua_State *L)

References lua_createtable(), lua_pushcclosure(), lua_pushvalue(), lua_rawseti(), NULL, searcher_C(), searcher_Croot(), searcher_Lua(), and searcher_preload().

Referenced by luaopen_package().

◆ findfile()

static const char * findfile ( lua_State * L,
const char * name,
const char * pname,
const char * dirsep )
static

Definition at line 386 of file lua-5.2.4/src/loadlib.c.

388 {
389 const char *path;
390 lua_getfield(L, lua_upvalueindex(1), pname);
391 path = lua_tostring(L, -1);
392 if (path == NULL)
393 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
394 return searchpath(L, name, path, ".", dirsep);
395}
const char * name
Definition lsqlite3.c:2154
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define lua_upvalueindex(i)
#define LUA_QL(x)
static const char * searchpath(lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep)

References lua_getfield(), LUA_QL, lua_tostring, lua_upvalueindex, luaL_error(), name, NULL, and searchpath().

Referenced by searcher_C(), searcher_Croot(), and searcher_Lua().

◆ findloader()

static void findloader ( lua_State * L,
const char * name )
static

Definition at line 478 of file lua-5.2.4/src/loadlib.c.

478 {
479 int i;
480 luaL_Buffer msg; /* to build error message */
481 luaL_buffinit(L, &msg);
482 lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
483 if (!lua_istable(L, 3))
484 luaL_error(L, LUA_QL("package.searchers") " must be a table");
485 /* iterate over available searchers to find a loader */
486 for (i = 1; ; i++) {
487 lua_rawgeti(L, 3, i); /* get a searcher */
488 if (lua_isnil(L, -1)) { /* no more searchers? */
489 lua_pop(L, 1); /* remove nil */
490 luaL_pushresult(&msg); /* create error message */
491 luaL_error(L, "module " LUA_QS " not found:%s",
492 name, lua_tostring(L, -1));
493 }
495 lua_call(L, 1, 2); /* call it */
496 if (lua_isfunction(L, -2)) /* did it find a loader? */
497 return; /* module loader found */
498 else if (lua_isstring(L, -2)) { /* searcher returned error message? */
499 lua_pop(L, 1); /* remove extra return */
500 luaL_addvalue(&msg); /* concatenate error message */
501 }
502 else
503 lua_pop(L, 2); /* remove both returns */
504 }
505}
LUA_API int lua_isstring(lua_State *L, int idx)
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
#define lua_istable(L, n)
#define lua_isfunction(L, n)
#define lua_isnil(L, n)
#define lua_pop(L, n)
#define lua_call(L, n, r)

References lua_call, lua_getfield(), lua_isfunction, lua_isnil, lua_isstring(), lua_istable, lua_pop, lua_pushstring(), LUA_QL, LUA_QS, lua_rawgeti(), lua_tostring, lua_upvalueindex, luaL_addvalue(), luaL_buffinit(), luaL_error(), luaL_pushresult(), and name.

Referenced by ll_require().

◆ gctm()

static int gctm ( lua_State * L)
static

Definition at line 275 of file lua-5.2.4/src/loadlib.c.

275 {
276 int n = luaL_len(L, 1);
277 for (; n >= 1; n--) { /* for each handle, in reverse order */
278 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
280 lua_pop(L, 1); /* pop handle */
281 }
282 return 0;
283}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUALIB_API int luaL_len(lua_State *L, int idx)
static void ll_unloadlib(void *lib)

References ll_unloadlib(), lua_pop, lua_rawgeti(), lua_touserdata(), and luaL_len().

Referenced by luaopen_package().

◆ ll_addtoclib()

static void ll_addtoclib ( lua_State * L,
const char * path,
void * plib )
static

Definition at line 261 of file lua-5.2.4/src/loadlib.c.

261 {
263 lua_pushlightuserdata(L, plib);
264 lua_pushvalue(L, -1);
265 lua_setfield(L, -3, path); /* CLIBS[path] = plib */
266 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
267 lua_pop(L, 1); /* pop CLIBS table */
268}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
#define LUA_REGISTRYINDEX
#define CLIBS

References CLIBS, lua_getfield(), lua_pop, lua_pushlightuserdata(), lua_pushvalue(), lua_rawseti(), LUA_REGISTRYINDEX, lua_setfield(), and luaL_len().

Referenced by ll_loadfunc().

◆ ll_checkclib()

static void * ll_checkclib ( lua_State * L,
const char * path )
static

Definition at line 251 of file lua-5.2.4/src/loadlib.c.

251 {
252 void *plib;
254 lua_getfield(L, -1, path);
255 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
256 lua_pop(L, 2); /* pop CLIBS table and 'plib' */
257 return plib;
258}

References CLIBS, lua_getfield(), lua_pop, LUA_REGISTRYINDEX, and lua_touserdata().

Referenced by ll_loadfunc().

◆ ll_load()

static void * ll_load ( lua_State * L,
const char * path,
int seeglb )
static

Definition at line 234 of file lua-5.2.4/src/loadlib.c.

234 {
235 (void)(path); (void)(seeglb); /* not used */
237 return NULL;
238}
#define lua_pushliteral(L, s)
#define DLMSG

References DLMSG, lua_pushliteral, and NULL.

Referenced by ll_loadfunc().

◆ ll_loadfunc()

static int ll_loadfunc ( lua_State * L,
const char * path,
const char * sym )
static

Definition at line 286 of file lua-5.2.4/src/loadlib.c.

286 {
287 void *reg = ll_checkclib(L, path); /* check loaded C libraries */
288 if (reg == NULL) { /* must load library? */
289 reg = ll_load(L, path, *sym == '*');
290 if (reg == NULL) return ERRLIB; /* unable to load library */
291 ll_addtoclib(L, path, reg);
292 }
293 if (*sym == '*') { /* loading only library (no function)? */
294 lua_pushboolean(L, 1); /* return 'true' */
295 return 0; /* no errors */
296 }
297 else {
298 lua_CFunction f = ll_sym(L, reg, sym);
299 if (f == NULL)
300 return ERRFUNC; /* unable to find function */
301 lua_pushcfunction(L, f); /* else create new function */
302 return 0; /* no errors */
303 }
304}
LUA_API void lua_pushboolean(lua_State *L, int b)
#define lua_pushcfunction(L, f)
#define ERRLIB
#define ERRFUNC
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
static void * ll_checkclib(lua_State *L, const char *path)
static void * ll_load(lua_State *L, const char *path, int seeglb)
static void ll_addtoclib(lua_State *L, const char *path, void *plib)

References ERRFUNC, ERRLIB, ll_addtoclib(), ll_checkclib(), ll_load(), ll_sym(), lua_pushboolean(), lua_pushcfunction, and NULL.

Referenced by ll_loadlib(), and loadfunc().

◆ ll_loadlib()

static int ll_loadlib ( lua_State * L)
static

Definition at line 307 of file lua-5.2.4/src/loadlib.c.

307 {
308 const char *path = luaL_checkstring(L, 1);
309 const char *init = luaL_checkstring(L, 2);
310 int stat = ll_loadfunc(L, path, init);
311 if (stat == 0) /* no errors? */
312 return 1; /* return the loaded function */
313 else { /* error; error message is on stack top */
314 lua_pushnil(L);
315 lua_insert(L, -2);
316 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
317 return 3; /* return nil, error message, and where */
318 }
319}
LUA_API void lua_pushnil(lua_State *L)
#define luaL_checkstring(L, n)
static int ll_loadfunc(lua_State *L, const char *path, const char *sym)
#define LIB_FAIL
#define lua_insert(L, idx)

References ERRLIB, LIB_FAIL, ll_loadfunc(), lua_insert, lua_pushnil(), lua_pushstring(), and luaL_checkstring.

◆ ll_require()

static int ll_require ( lua_State * L)
static

Definition at line 508 of file lua-5.2.4/src/loadlib.c.

508 {
509 const char *name = luaL_checkstring(L, 1);
510 lua_settop(L, 1); /* _LOADED table will be at index 2 */
511 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
512 lua_getfield(L, 2, name); /* _LOADED[name] */
513 if (lua_toboolean(L, -1)) /* is it there? */
514 return 1; /* package is already loaded */
515 /* else must load package */
516 lua_pop(L, 1); /* remove 'getfield' result */
517 findloader(L, name);
518 lua_pushstring(L, name); /* pass name as argument to module loader */
519 lua_insert(L, -2); /* name is 1st argument (before search data) */
520 lua_call(L, 2, 1); /* run loader to load module */
521 if (!lua_isnil(L, -1)) /* non-nil return? */
522 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
523 lua_getfield(L, 2, name);
524 if (lua_isnil(L, -1)) { /* module did not set a value? */
525 lua_pushboolean(L, 1); /* use true as result */
526 lua_pushvalue(L, -1); /* extra copy to be returned */
527 lua_setfield(L, 2, name); /* _LOADED[name] = true */
528 }
529 return 1;
530}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_settop(lua_State *L, int idx)
static void findloader(lua_State *L, const char *name)

References findloader(), lua_call, lua_getfield(), lua_insert, lua_isnil, lua_pop, lua_pushboolean(), lua_pushstring(), lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), lua_settop(), lua_toboolean(), luaL_checkstring, and name.

◆ ll_searchpath()

static int ll_searchpath ( lua_State * L)
static

Definition at line 372 of file lua-5.2.4/src/loadlib.c.

372 {
373 const char *f = searchpath(L, luaL_checkstring(L, 1),
374 luaL_checkstring(L, 2),
375 luaL_optstring(L, 3, "."),
377 if (f != NULL) return 1;
378 else { /* error message is on top of the stack */
379 lua_pushnil(L);
380 lua_insert(L, -2);
381 return 2; /* return nil + error message */
382 }
383}
#define luaL_optstring(L, n, d)
#define LUA_DIRSEP

References LUA_DIRSEP, lua_insert, lua_pushnil(), luaL_checkstring, luaL_optstring, NULL, and searchpath().

◆ ll_sym()

static lua_CFunction ll_sym ( lua_State * L,
void * lib,
const char * sym )
static

Definition at line 241 of file lua-5.2.4/src/loadlib.c.

241 {
242 (void)(lib); (void)(sym); /* not used */
244 return NULL;
245}

References DLMSG, lua_pushliteral, and NULL.

Referenced by ll_loadfunc().

◆ ll_unloadlib()

static void ll_unloadlib ( void * lib)
static

Definition at line 229 of file lua-5.2.4/src/loadlib.c.

229 {
230 (void)(lib); /* not used */
231}

Referenced by gctm().

◆ loadfunc()

static int loadfunc ( lua_State * L,
const char * filename,
const char * modname )
static

Definition at line 419 of file lua-5.2.4/src/loadlib.c.

419 {
420 const char *funcname;
421 const char *mark;
422 modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
423 mark = strchr(modname, *LUA_IGMARK);
424 if (mark) {
425 int stat;
426 funcname = lua_pushlstring(L, modname, mark - modname);
428 stat = ll_loadfunc(L, filename, funcname);
429 if (stat != ERRFUNC) return stat;
430 modname = mark + 1; /* else go ahead and try old-style name */
431 }
432 funcname = lua_pushfstring(L, LUA_POF"%s", modname);
433 return ll_loadfunc(L, filename, funcname);
434}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
LUALIB_API const char * luaL_gsub(lua_State *L, const char *s, const char *p, const char *r)
static int funcname(LexState *ls, expdesc *v)
#define LUA_IGMARK
#define LUA_OFSEP
#define LUA_POF

References ERRFUNC, funcname(), ll_loadfunc(), LUA_IGMARK, LUA_OFSEP, LUA_POF, lua_pushfstring(), lua_pushlstring(), and luaL_gsub().

Referenced by searcher_C(), and searcher_Croot().

◆ luaopen_package()

LUAMOD_API int luaopen_package ( lua_State * L)

Definition at line 690 of file lua-5.2.4/src/loadlib.c.

690 {
691 /* create table CLIBS to keep track of loaded C libraries */
693 lua_createtable(L, 0, 1); /* metatable for CLIBS */
695 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
696 lua_setmetatable(L, -2);
697 /* create `package' table */
700#if defined(LUA_COMPAT_LOADERS)
701 lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
702 lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
703#endif
704 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
705 /* set field 'path' */
707 /* set field 'cpath' */
709 /* store config information */
711 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
712 lua_setfield(L, -2, "config");
713 /* set field `loaded' */
714 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
715 lua_setfield(L, -2, "loaded");
716 /* set field `preload' */
717 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
718 lua_setfield(L, -2, "preload");
720 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
721 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
722 lua_pop(L, 1); /* pop global table */
723 return 1; /* return 'package' table */
724}
LUA_API int lua_setmetatable(lua_State *L, int objindex)
#define LUA_CPATH_DEFAULT
#define LUA_PATH_DEFAULT
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
LUALIB_API int luaL_getsubtable(lua_State *L, int idx, const char *fname)
#define luaL_newlib(L, l)
#define LUA_CPATH
#define LUA_EXEC_DIR
static const luaL_Reg ll_funcs[]
static int gctm(lua_State *L)
#define LUA_PATH_MARK
#define LUA_PATHVERSION
static void createsearcherstable(lua_State *L)
#define LUA_PATH
static void setpath(lua_State *L, const char *fieldname, const char *envname1, const char *envname2, const char *def)
#define LUA_CPATHVERSION
#define LUA_PATH_SEP
static const luaL_Reg pk_funcs[]
#define lua_pushglobaltable(L)

References CLIBS, createsearcherstable(), gctm(), ll_funcs, LUA_CPATH, LUA_CPATH_DEFAULT, LUA_CPATHVERSION, lua_createtable(), LUA_DIRSEP, LUA_EXEC_DIR, LUA_IGMARK, LUA_PATH, LUA_PATH_DEFAULT, LUA_PATH_MARK, LUA_PATH_SEP, LUA_PATHVERSION, lua_pop, lua_pushcfunction, lua_pushglobaltable, lua_pushliteral, lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), lua_setmetatable(), luaL_getsubtable(), luaL_newlib, luaL_setfuncs(), pk_funcs, and setpath().

◆ noenv()

static int noenv ( lua_State * L)
static

Definition at line 628 of file lua-5.2.4/src/loadlib.c.

628 {
629 int b;
630 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
631 b = lua_toboolean(L, -1);
632 lua_pop(L, 1); /* remove value */
633 return b;
634}

References lua_getfield(), lua_pop, LUA_REGISTRYINDEX, and lua_toboolean().

Referenced by setpath().

◆ pushnexttemplate()

static const char * pushnexttemplate ( lua_State * L,
const char * path )
static

Definition at line 338 of file lua-5.2.4/src/loadlib.c.

338 {
339 const char *l;
340 while (*path == *LUA_PATH_SEP) path++; /* skip separators */
341 if (*path == '\0') return NULL; /* no more templates */
342 l = strchr(path, *LUA_PATH_SEP); /* find next separator */
343 if (l == NULL) l = path + strlen(path);
344 lua_pushlstring(L, path, l - path); /* template */
345 return l;
346}

References LUA_PATH_SEP, lua_pushlstring(), and NULL.

Referenced by searchpath().

◆ readable()

static int readable ( const char * filename)
static

Definition at line 330 of file lua-5.2.4/src/loadlib.c.

330 {
331 FILE *f = fopen(filename, "r"); /* try to open file */
332 if (f == NULL) return 0; /* open failed */
333 fclose(f);
334 return 1;
335}

References NULL.

Referenced by searchpath().

◆ searcher_C()

static int searcher_C ( lua_State * L)
static

Definition at line 437 of file lua-5.2.4/src/loadlib.c.

437 {
438 const char *name = luaL_checkstring(L, 1);
439 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
440 if (filename == NULL) return 1; /* module not found in this path */
441 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
442}
#define LUA_CSUBSEP
static int loadfunc(lua_State *L, const char *filename, const char *modname)
static int checkload(lua_State *L, int stat, const char *filename)
static const char * findfile(lua_State *L, const char *name, const char *pname, const char *dirsep)

References checkload(), findfile(), loadfunc(), LUA_CSUBSEP, luaL_checkstring, name, and NULL.

Referenced by createsearcherstable().

◆ searcher_Croot()

static int searcher_Croot ( lua_State * L)
static

Definition at line 445 of file lua-5.2.4/src/loadlib.c.

445 {
446 const char *filename;
447 const char *name = luaL_checkstring(L, 1);
448 const char *p = strchr(name, '.');
449 int stat;
450 if (p == NULL) return 0; /* is root */
451 lua_pushlstring(L, name, p - name);
452 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
453 if (filename == NULL) return 1; /* root not found */
454 if ((stat = loadfunc(L, filename, name)) != 0) {
455 if (stat != ERRFUNC)
456 return checkload(L, 0, filename); /* real error */
457 else { /* open function not found */
458 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
459 name, filename);
460 return 1;
461 }
462 }
463 lua_pushstring(L, filename); /* will be 2nd argument to module */
464 return 2;
465}

References checkload(), ERRFUNC, findfile(), loadfunc(), LUA_CSUBSEP, lua_pushfstring(), lua_pushlstring(), lua_pushstring(), LUA_QS, lua_tostring, luaL_checkstring, name, and NULL.

Referenced by createsearcherstable().

◆ searcher_Lua()

static int searcher_Lua ( lua_State * L)
static

Definition at line 410 of file lua-5.2.4/src/loadlib.c.

410 {
411 const char *filename;
412 const char *name = luaL_checkstring(L, 1);
413 filename = findfile(L, name, "path", LUA_LSUBSEP);
414 if (filename == NULL) return 1; /* module not found in this path */
415 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
416}
#define luaL_loadfile(L, f)
#define LUA_LSUBSEP
#define LUA_OK

References checkload(), findfile(), LUA_LSUBSEP, LUA_OK, luaL_checkstring, luaL_loadfile, name, and NULL.

Referenced by createsearcherstable().

◆ searcher_preload()

static int searcher_preload ( lua_State * L)
static

Definition at line 468 of file lua-5.2.4/src/loadlib.c.

468 {
469 const char *name = luaL_checkstring(L, 1);
470 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
471 lua_getfield(L, -1, name);
472 if (lua_isnil(L, -1)) /* not found? */
473 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
474 return 1;
475}

References lua_getfield(), lua_isnil, lua_pushfstring(), LUA_REGISTRYINDEX, luaL_checkstring, and name.

Referenced by createsearcherstable().

◆ searchpath()

static const char * searchpath ( lua_State * L,
const char * name,
const char * path,
const char * sep,
const char * dirsep )
static

Definition at line 349 of file lua-5.2.4/src/loadlib.c.

352 {
353 luaL_Buffer msg; /* to build error message */
354 luaL_buffinit(L, &msg);
355 if (*sep != '\0') /* non-empty separator? */
356 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
357 while ((path = pushnexttemplate(L, path)) != NULL) {
358 const char *filename = luaL_gsub(L, lua_tostring(L, -1),
360 lua_remove(L, -2); /* remove path template */
361 if (readable(filename)) /* does file exist and is readable? */
362 return filename; /* return that file name */
363 lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
364 lua_remove(L, -2); /* remove file name */
365 luaL_addvalue(&msg); /* concatenate error msg. entry */
366 }
367 luaL_pushresult(&msg); /* create error message */
368 return NULL; /* not found */
369}
static const char * pushnexttemplate(lua_State *L, const char *path)
static int readable(const char *filename)
#define lua_remove(L, idx)

References LUA_PATH_MARK, lua_pushfstring(), LUA_QS, lua_remove, lua_tostring, luaL_addvalue(), luaL_buffinit(), luaL_gsub(), luaL_pushresult(), name, NULL, pushnexttemplate(), and readable().

Referenced by findfile(), and ll_searchpath().

◆ setpath()

static void setpath ( lua_State * L,
const char * fieldname,
const char * envname1,
const char * envname2,
const char * def )
static

Definition at line 637 of file lua-5.2.4/src/loadlib.c.

638 {
639 const char *path = getenv(envname1);
640 if (path == NULL) /* no environment variable? */
641 path = getenv(envname2); /* try alternative name */
642 if (path == NULL || noenv(L)) /* no environment variable? */
643 lua_pushstring(L, def); /* use default */
644 else {
645 /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
646 path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
648 luaL_gsub(L, path, AUXMARK, def);
649 lua_remove(L, -2);
650 }
651 setprogdir(L);
652 lua_setfield(L, -2, fieldname);
653}
static int noenv(lua_State *L)
#define setprogdir(L)
#define AUXMARK

References AUXMARK, LUA_PATH_SEP, lua_pushstring(), lua_remove, lua_setfield(), luaL_gsub(), noenv(), NULL, and setprogdir.

Referenced by luaopen_package().

Variable Documentation

◆ ll_funcs

const luaL_Reg ll_funcs[]
static
Initial value:
= {
{"require", ll_require},
}
static int ll_require(lua_State *L)

Definition at line 666 of file lua-5.2.4/src/loadlib.c.

666 {
667#if defined(LUA_COMPAT_MODULE)
668 {"module", ll_module},
669#endif
670 {"require", ll_require},
671 {NULL, NULL}
672};
static int ll_module(lua_State *L)

Referenced by luaopen_package().

◆ pk_funcs

const luaL_Reg pk_funcs[]
static
Initial value:
= {
{"loadlib", ll_loadlib},
{"searchpath", ll_searchpath},
}
static int ll_searchpath(lua_State *L)
static int ll_loadlib(lua_State *L)

Definition at line 656 of file lua-5.2.4/src/loadlib.c.

656 {
657 {"loadlib", ll_loadlib},
658 {"searchpath", ll_searchpath},
659#if defined(LUA_COMPAT_MODULE)
660 {"seeall", ll_seeall},
661#endif
662 {NULL, NULL}
663};
static int ll_seeall(lua_State *L)

Referenced by luaopen_package().