Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
loadlib.c File Reference
#include "lprefix.h"
#include <stdio.h>
#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_IGMARK   "-"
 
#define LUA_CSUBSEP   LUA_DIRSEP
 
#define LUA_LSUBSEP   LUA_DIRSEP
 
#define LUA_POF   "luaopen_"
 
#define LUA_OFSEP   "_"
 
#define LIB_FAIL   "open"
 
#define setprogdir(L)   ((void)0)
 
#define LIB_FAIL   "absent"
 
#define DLMSG   "dynamic libraries not enabled; check your Lua installation"
 
#define LUA_PATH_VAR   "LUA_PATH"
 
#define LUA_CPATH_VAR   "LUA_CPATH"
 
#define AUXMARK   "\1" /* auxiliary mark */
 
#define ERRLIB   1
 
#define ERRFUNC   2
 

Functions

static void lsys_unloadlib (void *lib)
 
static void * lsys_load (lua_State *L, const char *path, int seeglb)
 
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym)
 
static int noenv (lua_State *L)
 
static void setpath (lua_State *L, const char *fieldname, const char *envname, const char *dft)
 
static void * checkclib (lua_State *L, const char *path)
 
static void addtoclib (lua_State *L, const char *path, void *plib)
 
static int gctm (lua_State *L)
 
static int lookforfunc (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 void createsearcherstable (lua_State *L)
 
static void createclibstable (lua_State *L)
 
LUAMOD_API int luaopen_package (lua_State *L)
 

Variables

static const int CLIBS = 0
 
static const luaL_Reg pk_funcs []
 
static const luaL_Reg ll_funcs []
 

Macro Definition Documentation

◆ AUXMARK

#define AUXMARK   "\1" /* auxiliary mark */

Definition at line 272 of file lua-5.3.6/src/loadlib.c.

Referenced by setpath().

◆ DLMSG

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

Definition at line 228 of file lua-5.3.6/src/loadlib.c.

Referenced by lsys_load(), and lsys_sym().

◆ ERRFUNC

#define ERRFUNC   2

Definition at line 359 of file lua-5.3.6/src/loadlib.c.

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

◆ ERRLIB

#define ERRLIB   1

Definition at line 358 of file lua-5.3.6/src/loadlib.c.

Referenced by ll_loadlib(), and lookforfunc().

◆ LIB_FAIL [1/2]

#define LIB_FAIL   "open"

Definition at line 64 of file lua-5.3.6/src/loadlib.c.

Referenced by ll_loadlib().

◆ LIB_FAIL [2/2]

#define LIB_FAIL   "absent"

Definition at line 64 of file lua-5.3.6/src/loadlib.c.

◆ loadlib_c

#define loadlib_c

Definition at line 11 of file lua-5.3.6/src/loadlib.c.

◆ LUA_CPATH_VAR

#define LUA_CPATH_VAR   "LUA_CPATH"

Definition at line 268 of file lua-5.3.6/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_CSUBSEP

#define LUA_CSUBSEP   LUA_DIRSEP

Definition at line 43 of file lua-5.3.6/src/loadlib.c.

Referenced by searcher_C(), and searcher_Croot().

◆ LUA_IGMARK

#define LUA_IGMARK   "-"

Definition at line 32 of file lua-5.3.6/src/loadlib.c.

Referenced by loadfunc(), and luaopen_package().

◆ LUA_LIB

#define LUA_LIB

Definition at line 12 of file lua-5.3.6/src/loadlib.c.

◆ LUA_LSUBSEP

#define LUA_LSUBSEP   LUA_DIRSEP

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

Referenced by searcher_Lua().

◆ LUA_OFSEP

#define LUA_OFSEP   "_"

Definition at line 55 of file lua-5.3.6/src/loadlib.c.

Referenced by loadfunc().

◆ LUA_PATH_VAR

#define LUA_PATH_VAR   "LUA_PATH"

Definition at line 264 of file lua-5.3.6/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_POF

#define LUA_POF   "luaopen_"

Definition at line 52 of file lua-5.3.6/src/loadlib.c.

Referenced by loadfunc().

◆ setprogdir

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

Definition at line 67 of file lua-5.3.6/src/loadlib.c.

Referenced by setpath().

Function Documentation

◆ addtoclib()

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

Definition at line 331 of file lua-5.3.6/src/loadlib.c.

331 {
333 lua_pushlightuserdata(L, plib);
334 lua_pushvalue(L, -1);
335 lua_setfield(L, -3, path); /* CLIBS[path] = plib */
336 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
337 lua_pop(L, 1); /* pop CLIBS table */
338}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
#define LUA_REGISTRYINDEX
#define lua_pop(L, n)
LUA_API void lua_rawgetp(lua_State *L, int idx, const void *p)
LUALIB_API int luaL_len(lua_State *L, int idx)
static const int CLIBS

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

Referenced by lookforfunc().

◆ checkclib()

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

Definition at line 317 of file lua-5.3.6/src/loadlib.c.

317 {
318 void *plib;
320 lua_getfield(L, -1, path);
321 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
322 lua_pop(L, 2); /* pop CLIBS table and 'plib' */
323 return plib;
324}
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)

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

Referenced by lookforfunc().

◆ checkload()

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

Definition at line 484 of file lua-5.3.6/src/loadlib.c.

484 {
485 if (stat) { /* module loaded successfully? */
486 lua_pushstring(L, filename); /* will be 2nd argument to module */
487 return 2; /* return open function and file name */
488 }
489 else
490 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
491 lua_tostring(L, 1), filename, lua_tostring(L, -1));
492}
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)

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

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

◆ createclibstable()

static void createclibstable ( lua_State * L)
static

Definition at line 757 of file lua-5.3.6/src/loadlib.c.

757 {
758 lua_newtable(L); /* create CLIBS table */
759 lua_createtable(L, 0, 1); /* create metatable for CLIBS */
761 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
762 lua_setmetatable(L, -2);
763 lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
764}
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
#define lua_pushcfunction(L, f)
#define lua_newtable(L)
LUA_API void lua_rawsetp(lua_State *L, int idx, const void *p)
static int gctm(lua_State *L)

References CLIBS, gctm(), lua_createtable(), lua_newtable, lua_pushcfunction, lua_rawsetp(), LUA_REGISTRYINDEX, lua_setfield(), and lua_setmetatable().

Referenced by luaopen_package().

◆ createsearcherstable()

static void createsearcherstable ( lua_State * L)
static

Definition at line 733 of file lua-5.3.6/src/loadlib.c.

733 {
734 static const lua_CFunction searchers[] =
736 int i;
737 /* create 'searchers' table */
738 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
739 /* fill it with predefined searchers */
740 for (i=0; searchers[i] != NULL; i++) {
741 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
742 lua_pushcclosure(L, searchers[i], 1);
743 lua_rawseti(L, -2, i+1);
744 }
745#if defined(LUA_COMPAT_LOADERS)
746 lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
747 lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */
748#endif
749 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
750}
#define NULL
Definition gmacros.h:924
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, 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(), lua_setfield(), 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 472 of file lua-5.3.6/src/loadlib.c.

474 {
475 const char *path;
476 lua_getfield(L, lua_upvalueindex(1), pname);
477 path = lua_tostring(L, -1);
478 if (path == NULL)
479 luaL_error(L, "'package.%s' must be a string", pname);
480 return searchpath(L, name, path, ".", dirsep);
481}
const char * name
Definition lsqlite3.c:2154
#define lua_upvalueindex(i)
static const char * searchpath(lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep)

References lua_getfield(), 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 569 of file lua-5.3.6/src/loadlib.c.

569 {
570 int i;
571 luaL_Buffer msg; /* to build error message */
572 luaL_buffinit(L, &msg);
573 /* push 'package.searchers' to index 3 in the stack */
574 if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
575 luaL_error(L, "'package.searchers' must be a table");
576 /* iterate over available searchers to find a loader */
577 for (i = 1; ; i++) {
578 if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
579 lua_pop(L, 1); /* remove nil */
580 luaL_pushresult(&msg); /* create error message */
581 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
582 }
584 lua_call(L, 1, 2); /* call it */
585 if (lua_isfunction(L, -2)) /* did it find a loader? */
586 return; /* module loader found */
587 else if (lua_isstring(L, -2)) { /* searcher returned error message? */
588 lua_pop(L, 1); /* remove extra return */
589 luaL_addvalue(&msg); /* concatenate error message */
590 }
591 else
592 lua_pop(L, 2); /* remove both returns */
593 }
594}
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_TTABLE
#define lua_isfunction(L, n)
#define LUA_TNIL
#define lua_call(L, n, r)

References lua_call, lua_getfield(), lua_isfunction, lua_isstring(), lua_pop, lua_pushstring(), lua_rawgeti(), LUA_TNIL, lua_tostring, LUA_TTABLE, 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 345 of file lua-5.3.6/src/loadlib.c.

345 {
346 lua_Integer n = luaL_len(L, 1);
347 for (; n >= 1; n--) { /* for each handle, in reverse order */
348 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
350 lua_pop(L, 1); /* pop handle */
351 }
352 return 0;
353}
LUA_INTEGER lua_Integer
static void lsys_unloadlib(void *lib)

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

Referenced by createclibstable().

◆ ll_loadlib()

static int ll_loadlib ( lua_State * L)
static

Definition at line 393 of file lua-5.3.6/src/loadlib.c.

393 {
394 const char *path = luaL_checkstring(L, 1);
395 const char *init = luaL_checkstring(L, 2);
396 int stat = lookforfunc(L, path, init);
397 if (stat == 0) /* no errors? */
398 return 1; /* return the loaded function */
399 else { /* error; error message is on stack top */
400 lua_pushnil(L);
401 lua_insert(L, -2);
402 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
403 return 3; /* return nil, error message, and where */
404 }
405}
LUA_API void lua_pushnil(lua_State *L)
#define luaL_checkstring(L, n)
#define ERRLIB
static int lookforfunc(lua_State *L, const char *path, const char *sym)
#define LIB_FAIL
#define lua_insert(L, idx)

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

◆ ll_require()

static int ll_require ( lua_State * L)
static

Definition at line 597 of file lua-5.3.6/src/loadlib.c.

597 {
598 const char *name = luaL_checkstring(L, 1);
599 lua_settop(L, 1); /* LOADED table will be at index 2 */
601 lua_getfield(L, 2, name); /* LOADED[name] */
602 if (lua_toboolean(L, -1)) /* is it there? */
603 return 1; /* package is already loaded */
604 /* else must load package */
605 lua_pop(L, 1); /* remove 'getfield' result */
606 findloader(L, name);
607 lua_pushstring(L, name); /* pass name as argument to module loader */
608 lua_insert(L, -2); /* name is 1st argument (before search data) */
609 lua_call(L, 2, 1); /* run loader to load module */
610 if (!lua_isnil(L, -1)) /* non-nil return? */
611 lua_setfield(L, 2, name); /* LOADED[name] = returned value */
612 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
613 lua_pushboolean(L, 1); /* use true as result */
614 lua_pushvalue(L, -1); /* extra copy to be returned */
615 lua_setfield(L, 2, name); /* LOADED[name] = true */
616 }
617 return 1;
618}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_pushboolean(lua_State *L, int b)
LUA_API void lua_settop(lua_State *L, int idx)
#define lua_isnil(L, n)
#define LUA_LOADED_TABLE
static void findloader(lua_State *L, const char *name)

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

◆ ll_searchpath()

static int ll_searchpath ( lua_State * L)
static

Definition at line 458 of file lua-5.3.6/src/loadlib.c.

458 {
459 const char *f = searchpath(L, luaL_checkstring(L, 1),
460 luaL_checkstring(L, 2),
461 luaL_optstring(L, 3, "."),
463 if (f != NULL) return 1;
464 else { /* error message is on top of the stack */
465 lua_pushnil(L);
466 lua_insert(L, -2);
467 return 2; /* return nil + error message */
468 }
469}
#define luaL_optstring(L, n, d)
#define LUA_DIRSEP

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

◆ loadfunc()

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

Definition at line 512 of file lua-5.3.6/src/loadlib.c.

512 {
513 const char *openfunc;
514 const char *mark;
515 modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
516 mark = strchr(modname, *LUA_IGMARK);
517 if (mark) {
518 int stat;
519 openfunc = lua_pushlstring(L, modname, mark - modname);
520 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
521 stat = lookforfunc(L, filename, openfunc);
522 if (stat != ERRFUNC) return stat;
523 modname = mark + 1; /* else go ahead and try old-style name */
524 }
525 openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
526 return lookforfunc(L, filename, openfunc);
527}
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)
#define ERRFUNC
#define LUA_IGMARK
#define LUA_OFSEP
#define LUA_POF

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

Referenced by searcher_C(), and searcher_Croot().

◆ lookforfunc()

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

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

372 {
373 void *reg = checkclib(L, path); /* check loaded C libraries */
374 if (reg == NULL) { /* must load library? */
375 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
376 if (reg == NULL) return ERRLIB; /* unable to load library */
377 addtoclib(L, path, reg);
378 }
379 if (*sym == '*') { /* loading only library (no function)? */
380 lua_pushboolean(L, 1); /* return 'true' */
381 return 0; /* no errors */
382 }
383 else {
384 lua_CFunction f = lsys_sym(L, reg, sym);
385 if (f == NULL)
386 return ERRFUNC; /* unable to find function */
387 lua_pushcfunction(L, f); /* else create new function */
388 return 0; /* no errors */
389 }
390}
static void * lsys_load(lua_State *L, const char *path, int seeglb)
static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym)
static void * checkclib(lua_State *L, const char *path)
static void addtoclib(lua_State *L, const char *path, void *plib)

References addtoclib(), checkclib(), ERRFUNC, ERRLIB, lsys_load(), lsys_sym(), lua_pushboolean(), lua_pushcfunction, and NULL.

Referenced by ll_loadlib(), and loadfunc().

◆ lsys_load()

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

Definition at line 236 of file lua-5.3.6/src/loadlib.c.

236 {
237 (void)(path); (void)(seeglb); /* not used */
239 return NULL;
240}
#define lua_pushliteral(L, s)
#define DLMSG

References DLMSG, lua_pushliteral, and NULL.

Referenced by lookforfunc().

◆ lsys_sym()

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

Definition at line 243 of file lua-5.3.6/src/loadlib.c.

243 {
244 (void)(lib); (void)(sym); /* not used */
246 return NULL;
247}

References DLMSG, lua_pushliteral, and NULL.

Referenced by lookforfunc().

◆ lsys_unloadlib()

static void lsys_unloadlib ( void * lib)
static

Definition at line 231 of file lua-5.3.6/src/loadlib.c.

231 {
232 (void)(lib); /* not used */
233}

Referenced by gctm().

◆ luaopen_package()

LUAMOD_API int luaopen_package ( lua_State * L)

Definition at line 767 of file lua-5.3.6/src/loadlib.c.

767 {
769 luaL_newlib(L, pk_funcs); /* create 'package' table */
771 /* set paths */
774 /* store config information */
776 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
777 lua_setfield(L, -2, "config");
778 /* set field 'loaded' */
780 lua_setfield(L, -2, "loaded");
781 /* set field 'preload' */
783 lua_setfield(L, -2, "preload");
785 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
786 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
787 lua_pop(L, 1); /* pop global table */
788 return 1; /* return 'package' table */
789}
#define LUA_CPATH_DEFAULT
#define LUA_PATH_MARK
#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_EXEC_DIR
#define LUA_PATH_SEP
#define lua_pushglobaltable(L)
#define LUA_PRELOAD_TABLE
static const luaL_Reg ll_funcs[]
static void createclibstable(lua_State *L)
static void createsearcherstable(lua_State *L)
#define LUA_PATH_VAR
static void setpath(lua_State *L, const char *fieldname, const char *envname, const char *dft)
#define LUA_CPATH_VAR
static const luaL_Reg pk_funcs[]

References createclibstable(), createsearcherstable(), ll_funcs, LUA_CPATH_DEFAULT, LUA_CPATH_VAR, LUA_DIRSEP, LUA_EXEC_DIR, LUA_IGMARK, LUA_LOADED_TABLE, LUA_PATH_DEFAULT, LUA_PATH_MARK, LUA_PATH_SEP, LUA_PATH_VAR, lua_pop, LUA_PRELOAD_TABLE, lua_pushglobaltable, lua_pushliteral, lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), luaL_getsubtable(), luaL_newlib, luaL_setfuncs(), pk_funcs, and setpath().

◆ noenv()

static int noenv ( lua_State * L)
static

Definition at line 278 of file lua-5.3.6/src/loadlib.c.

278 {
279 int b;
280 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
281 b = lua_toboolean(L, -1);
282 lua_pop(L, 1); /* remove value */
283 return b;
284}

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 424 of file lua-5.3.6/src/loadlib.c.

424 {
425 const char *l;
426 while (*path == *LUA_PATH_SEP) path++; /* skip separators */
427 if (*path == '\0') return NULL; /* no more templates */
428 l = strchr(path, *LUA_PATH_SEP); /* find next separator */
429 if (l == NULL) l = path + strlen(path);
430 lua_pushlstring(L, path, l - path); /* template */
431 return l;
432}

References LUA_PATH_SEP, lua_pushlstring(), and NULL.

Referenced by searchpath().

◆ readable()

static int readable ( const char * filename)
static

Definition at line 416 of file lua-5.3.6/src/loadlib.c.

416 {
417 FILE *f = fopen(filename, "r"); /* try to open file */
418 if (f == NULL) return 0; /* open failed */
419 fclose(f);
420 return 1;
421}

References NULL.

Referenced by searchpath().

◆ searcher_C()

static int searcher_C ( lua_State * L)
static

Definition at line 530 of file lua-5.3.6/src/loadlib.c.

530 {
531 const char *name = luaL_checkstring(L, 1);
532 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
533 if (filename == NULL) return 1; /* module not found in this path */
534 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
535}
#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 538 of file lua-5.3.6/src/loadlib.c.

538 {
539 const char *filename;
540 const char *name = luaL_checkstring(L, 1);
541 const char *p = strchr(name, '.');
542 int stat;
543 if (p == NULL) return 0; /* is root */
544 lua_pushlstring(L, name, p - name);
545 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
546 if (filename == NULL) return 1; /* root not found */
547 if ((stat = loadfunc(L, filename, name)) != 0) {
548 if (stat != ERRFUNC)
549 return checkload(L, 0, filename); /* real error */
550 else { /* open function not found */
551 lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
552 return 1;
553 }
554 }
555 lua_pushstring(L, filename); /* will be 2nd argument to module */
556 return 2;
557}

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

Referenced by createsearcherstable().

◆ searcher_Lua()

static int searcher_Lua ( lua_State * L)
static

Definition at line 495 of file lua-5.3.6/src/loadlib.c.

495 {
496 const char *filename;
497 const char *name = luaL_checkstring(L, 1);
498 filename = findfile(L, name, "path", LUA_LSUBSEP);
499 if (filename == NULL) return 1; /* module not found in this path */
500 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
501}
#define luaL_loadfile(L, f)
#define LUA_OK
#define LUA_LSUBSEP

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 560 of file lua-5.3.6/src/loadlib.c.

560 {
561 const char *name = luaL_checkstring(L, 1);
563 if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
564 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
565 return 1;
566}

References lua_getfield(), LUA_PRELOAD_TABLE, lua_pushfstring(), LUA_REGISTRYINDEX, LUA_TNIL, 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 435 of file lua-5.3.6/src/loadlib.c.

438 {
439 luaL_Buffer msg; /* to build error message */
440 luaL_buffinit(L, &msg);
441 if (*sep != '\0') /* non-empty separator? */
442 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
443 while ((path = pushnexttemplate(L, path)) != NULL) {
444 const char *filename = luaL_gsub(L, lua_tostring(L, -1),
446 lua_remove(L, -2); /* remove path template */
447 if (readable(filename)) /* does file exist and is readable? */
448 return filename; /* return that file name */
449 lua_pushfstring(L, "\n\tno file '%s'", filename);
450 lua_remove(L, -2); /* remove file name */
451 luaL_addvalue(&msg); /* concatenate error msg. entry */
452 }
453 luaL_pushresult(&msg); /* create error message */
454 return NULL; /* not found */
455}
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_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 * envname,
const char * dft )
static

Definition at line 290 of file lua-5.3.6/src/loadlib.c.

292 {
293 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
294 const char *path = getenv(nver); /* use versioned name */
295 if (path == NULL) /* no environment variable? */
296 path = getenv(envname); /* try unversioned name */
297 if (path == NULL || noenv(L)) /* no environment variable? */
298 lua_pushstring(L, dft); /* use default */
299 else {
300 /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
301 path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
303 luaL_gsub(L, path, AUXMARK, dft);
304 lua_remove(L, -2); /* remove result from 1st 'gsub' */
305 }
306 setprogdir(L);
307 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
308 lua_pop(L, 1); /* pop versioned variable name */
309}
static int noenv(lua_State *L)
#define setprogdir(L)
#define AUXMARK
#define LUA_VERSUFFIX

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

Referenced by luaopen_package().

Variable Documentation

◆ CLIBS

const int CLIBS = 0
static

Definition at line 62 of file lua-5.3.6/src/loadlib.c.

Referenced by addtoclib(), checkclib(), and createclibstable().

◆ ll_funcs

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

Definition at line 724 of file lua-5.3.6/src/loadlib.c.

724 {
725#if defined(LUA_COMPAT_MODULE)
726 {"module", ll_module},
727#endif
728 {"require", ll_require},
729 {NULL, NULL}
730};
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},
{"preload", NULL},
{"cpath", NULL},
{"path", NULL},
{"searchers", NULL},
{"loaded", NULL},
}
static int ll_searchpath(lua_State *L)
static int ll_loadlib(lua_State *L)

Definition at line 708 of file lua-5.3.6/src/loadlib.c.

708 {
709 {"loadlib", ll_loadlib},
710 {"searchpath", ll_searchpath},
711#if defined(LUA_COMPAT_MODULE)
712 {"seeall", ll_seeall},
713#endif
714 /* placeholders */
715 {"preload", NULL},
716 {"cpath", NULL},
717 {"path", NULL},
718 {"searchers", NULL},
719 {"loaded", NULL},
720 {NULL, NULL}
721};
static int ll_seeall(lua_State *L)

Referenced by luaopen_package().