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 ERRLIB   1
 
#define ERRFUNC   2
 

Typedefs

typedef void(* voidf) (void)
 

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 * getnextfilename (char **path, char *end)
 
static void pusherrornotfound (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 char *const CLIBS = "_CLIBS"
 
static const luaL_Reg pk_funcs []
 
static const luaL_Reg ll_funcs []
 

Macro Definition Documentation

◆ DLMSG

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

Definition at line 237 of file lua-5.4.3/src/loadlib.c.

Referenced by lsys_load(), and lsys_sym().

◆ ERRFUNC

#define ERRFUNC   2

Definition at line 377 of file lua-5.4.3/src/loadlib.c.

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

◆ ERRLIB

#define ERRLIB   1

Definition at line 376 of file lua-5.4.3/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.4.3/src/loadlib.c.

Referenced by ll_loadlib().

◆ LIB_FAIL [2/2]

#define LIB_FAIL   "absent"

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

◆ loadlib_c

#define loadlib_c

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

◆ LUA_CPATH_VAR

#define LUA_CPATH_VAR   "LUA_CPATH"

Definition at line 277 of file lua-5.4.3/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_CSUBSEP

#define LUA_CSUBSEP   LUA_DIRSEP

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

Referenced by searcher_C(), and searcher_Croot().

◆ LUA_IGMARK

#define LUA_IGMARK   "-"

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

Referenced by loadfunc(), and luaopen_package().

◆ LUA_LIB

#define LUA_LIB

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

◆ LUA_LSUBSEP

#define LUA_LSUBSEP   LUA_DIRSEP

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

Referenced by searcher_Lua().

◆ LUA_OFSEP

#define LUA_OFSEP   "_"

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

Referenced by loadfunc().

◆ LUA_PATH_VAR

#define LUA_PATH_VAR   "LUA_PATH"

Definition at line 273 of file lua-5.4.3/src/loadlib.c.

Referenced by luaopen_package().

◆ LUA_POF

#define LUA_POF   "luaopen_"

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

Referenced by loadfunc().

◆ setprogdir

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

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

Referenced by setpath().

Typedef Documentation

◆ voidf

typedef void(* voidf) (void)

Definition at line 74 of file lua-5.4.3/src/loadlib.c.

Function Documentation

◆ addtoclib()

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

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

349 {
351 lua_pushlightuserdata(L, plib);
352 lua_pushvalue(L, -1);
353 lua_setfield(L, -3, path); /* CLIBS[path] = plib */
354 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
355 lua_pop(L, 1); /* pop CLIBS table */
356}
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)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define LUA_REGISTRYINDEX
#define lua_pop(L, n)
LUALIB_API int luaL_len(lua_State *L, int idx)
static const char *const CLIBS

References CLIBS, lua_getfield(), lua_pop, lua_pushlightuserdata(), lua_pushvalue(), 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 335 of file lua-5.4.3/src/loadlib.c.

335 {
336 void *plib;
338 lua_getfield(L, -1, path);
339 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
340 lua_pop(L, 2); /* pop CLIBS table and 'plib' */
341 return plib;
342}
LUA_API void * lua_touserdata(lua_State *L, int idx)

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

Referenced by lookforfunc().

◆ checkload()

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

Definition at line 534 of file lua-5.4.3/src/loadlib.c.

534 {
535 if (l_likely(stat)) { /* module loaded successfully? */
536 lua_pushstring(L, filename); /* will be 2nd argument to module */
537 return 2; /* return open function and file name */
538 }
539 else
540 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
541 lua_tostring(L, 1), filename, lua_tostring(L, -1));
542}
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 730 of file lua-5.4.3/src/loadlib.c.

730 {
731 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
732 lua_createtable(L, 0, 1); /* create metatable for CLIBS */
734 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
735 lua_setmetatable(L, -2);
736}
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)
LUALIB_API int luaL_getsubtable(lua_State *L, int idx, const char *fname)
static int gctm(lua_State *L)

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

Referenced by luaopen_package().

◆ createsearcherstable()

static void createsearcherstable ( lua_State * L)
static

Definition at line 710 of file lua-5.4.3/src/loadlib.c.

710 {
711 static const lua_CFunction searchers[] =
713 int i;
714 /* create 'searchers' table */
715 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
716 /* fill it with predefined searchers */
717 for (i=0; searchers[i] != NULL; i++) {
718 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
719 lua_pushcclosure(L, searchers[i], 1);
720 lua_rawseti(L, -2, i+1);
721 }
722 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
723}
#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 522 of file lua-5.4.3/src/loadlib.c.

524 {
525 const char *path;
526 lua_getfield(L, lua_upvalueindex(1), pname);
527 path = lua_tostring(L, -1);
528 if (l_unlikely(path == NULL))
529 luaL_error(L, "'package.%s' must be a string", pname);
530 return searchpath(L, name, path, ".", dirsep);
531}
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 624 of file lua-5.4.3/src/loadlib.c.

624 {
625 int i;
626 luaL_Buffer msg; /* to build error message */
627 /* push 'package.searchers' to index 3 in the stack */
628 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
629 != LUA_TTABLE))
630 luaL_error(L, "'package.searchers' must be a table");
631 luaL_buffinit(L, &msg);
632 /* iterate over available searchers to find a loader */
633 for (i = 1; ; i++) {
634 luaL_addstring(&msg, "\n\t"); /* error-message prefix */
635 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
636 lua_pop(L, 1); /* remove nil */
637 luaL_buffsub(&msg, 2); /* remove prefix */
638 luaL_pushresult(&msg); /* create error message */
639 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
640 }
642 lua_call(L, 1, 2); /* call it */
643 if (lua_isfunction(L, -2)) /* did it find a loader? */
644 return; /* module loader found */
645 else if (lua_isstring(L, -2)) { /* searcher returned error message? */
646 lua_pop(L, 1); /* remove extra return */
647 luaL_addvalue(&msg); /* concatenate error message */
648 }
649 else { /* no error message */
650 lua_pop(L, 2); /* remove both returns */
651 luaL_buffsub(&msg, 2); /* remove prefix */
652 }
653 }
654}
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)
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
#define LUA_TTABLE
#define lua_isfunction(L, n)
#define LUA_TNIL
#define lua_call(L, n, r)
#define luaL_buffsub(B, s)

References lua_call, lua_getfield(), lua_isfunction, lua_isstring(), lua_pop, lua_pushstring(), lua_rawgeti(), LUA_TNIL, lua_tostring, LUA_TTABLE, lua_upvalueindex, luaL_addstring(), luaL_addvalue(), luaL_buffinit(), luaL_buffsub, luaL_error(), luaL_pushresult(), and name.

Referenced by ll_require().

◆ gctm()

static int gctm ( lua_State * L)
static

Definition at line 363 of file lua-5.4.3/src/loadlib.c.

363 {
364 lua_Integer n = luaL_len(L, 1);
365 for (; n >= 1; n--) { /* for each handle, in reverse order */
366 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
368 lua_pop(L, 1); /* pop handle */
369 }
370 return 0;
371}
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().

◆ getnextfilename()

static const char * getnextfilename ( char ** path,
char * end )
static

Definition at line 447 of file lua-5.4.3/src/loadlib.c.

447 {
448 char *sep;
449 char *name = *path;
450 if (name == end)
451 return NULL; /* no more names */
452 else if (*name == '\0') { /* from previous iteration? */
453 *name = *LUA_PATH_SEP; /* restore separator */
454 name++; /* skip it */
455 }
456 sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
457 if (sep == NULL) /* separator not found? */
458 sep = end; /* name goes until the end */
459 *sep = '\0'; /* finish file name */
460 *path = sep; /* will start next search from here */
461 return name;
462}
#define LUA_PATH_SEP

References LUA_PATH_SEP, name, and NULL.

Referenced by searchpath().

◆ ll_loadlib()

static int ll_loadlib ( lua_State * L)
static

Definition at line 411 of file lua-5.4.3/src/loadlib.c.

411 {
412 const char *path = luaL_checkstring(L, 1);
413 const char *init = luaL_checkstring(L, 2);
414 int stat = lookforfunc(L, path, init);
415 if (l_likely(stat == 0)) /* no errors? */
416 return 1; /* return the loaded function */
417 else { /* error; error message is on stack top */
418 luaL_pushfail(L);
419 lua_insert(L, -2);
420 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
421 return 3; /* return fail, error message, and where */
422 }
423}
#define luaL_checkstring(L, n)
#define lua_insert(L, idx)
#define luaL_pushfail(L)
#define ERRLIB
static int lookforfunc(lua_State *L, const char *path, const char *sym)
#define LIB_FAIL

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

◆ ll_require()

static int ll_require ( lua_State * L)
static

Definition at line 657 of file lua-5.4.3/src/loadlib.c.

657 {
658 const char *name = luaL_checkstring(L, 1);
659 lua_settop(L, 1); /* LOADED table will be at index 2 */
661 lua_getfield(L, 2, name); /* LOADED[name] */
662 if (lua_toboolean(L, -1)) /* is it there? */
663 return 1; /* package is already loaded */
664 /* else must load package */
665 lua_pop(L, 1); /* remove 'getfield' result */
666 findloader(L, name);
667 lua_rotate(L, -2, 1); /* function <-> loader data */
668 lua_pushvalue(L, 1); /* name is 1st argument to module loader */
669 lua_pushvalue(L, -3); /* loader data is 2nd argument */
670 /* stack: ...; loader data; loader function; mod. name; loader data */
671 lua_call(L, 2, 1); /* run loader to load module */
672 /* stack: ...; loader data; result from loader */
673 if (!lua_isnil(L, -1)) /* non-nil return? */
674 lua_setfield(L, 2, name); /* LOADED[name] = returned value */
675 else
676 lua_pop(L, 1); /* pop nil */
677 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
678 lua_pushboolean(L, 1); /* use true as result */
679 lua_copy(L, -1, -2); /* replace loader result */
680 lua_setfield(L, 2, name); /* LOADED[name] = true */
681 }
682 lua_rotate(L, -2, 1); /* loader data <-> module result */
683 return 2; /* return module result and loader data */
684}
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)
LUA_API void lua_copy(lua_State *L, int fromidx, int toidx)
LUA_API void lua_rotate(lua_State *L, int idx, int n)
#define LUA_LOADED_TABLE
static void findloader(lua_State *L, const char *name)

References findloader(), lua_call, lua_copy(), lua_getfield(), lua_isnil, LUA_LOADED_TABLE, lua_pop, lua_pushboolean(), lua_pushvalue(), LUA_REGISTRYINDEX, lua_rotate(), 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 508 of file lua-5.4.3/src/loadlib.c.

508 {
509 const char *f = searchpath(L, luaL_checkstring(L, 1),
510 luaL_checkstring(L, 2),
511 luaL_optstring(L, 3, "."),
513 if (f != NULL) return 1;
514 else { /* error message is on top of the stack */
515 luaL_pushfail(L);
516 lua_insert(L, -2);
517 return 2; /* return fail + error message */
518 }
519}
#define luaL_optstring(L, n, d)
#define LUA_DIRSEP

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

◆ loadfunc()

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

Definition at line 562 of file lua-5.4.3/src/loadlib.c.

562 {
563 const char *openfunc;
564 const char *mark;
565 modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
566 mark = strchr(modname, *LUA_IGMARK);
567 if (mark) {
568 int stat;
569 openfunc = lua_pushlstring(L, modname, mark - modname);
570 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
571 stat = lookforfunc(L, filename, openfunc);
572 if (stat != ERRFUNC) return stat;
573 modname = mark + 1; /* else go ahead and try old-style name */
574 }
575 openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
576 return lookforfunc(L, filename, openfunc);
577}
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 390 of file lua-5.4.3/src/loadlib.c.

390 {
391 void *reg = checkclib(L, path); /* check loaded C libraries */
392 if (reg == NULL) { /* must load library? */
393 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
394 if (reg == NULL) return ERRLIB; /* unable to load library */
395 addtoclib(L, path, reg);
396 }
397 if (*sym == '*') { /* loading only library (no function)? */
398 lua_pushboolean(L, 1); /* return 'true' */
399 return 0; /* no errors */
400 }
401 else {
402 lua_CFunction f = lsys_sym(L, reg, sym);
403 if (f == NULL)
404 return ERRFUNC; /* unable to find function */
405 lua_pushcfunction(L, f); /* else create new function */
406 return 0; /* no errors */
407 }
408}
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 245 of file lua-5.4.3/src/loadlib.c.

245 {
246 (void)(path); (void)(seeglb); /* not used */
248 return NULL;
249}
#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 252 of file lua-5.4.3/src/loadlib.c.

252 {
253 (void)(lib); (void)(sym); /* not used */
255 return NULL;
256}

References DLMSG, lua_pushliteral, and NULL.

Referenced by lookforfunc().

◆ lsys_unloadlib()

static void lsys_unloadlib ( void * lib)
static

Definition at line 240 of file lua-5.4.3/src/loadlib.c.

240 {
241 (void)(lib); /* not used */
242}

Referenced by gctm().

◆ luaopen_package()

LUAMOD_API int luaopen_package ( lua_State * L)

Definition at line 739 of file lua-5.4.3/src/loadlib.c.

739 {
741 luaL_newlib(L, pk_funcs); /* create 'package' table */
743 /* set paths */
746 /* store config information */
748 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
749 lua_setfield(L, -2, "config");
750 /* set field 'loaded' */
752 lua_setfield(L, -2, "loaded");
753 /* set field 'preload' */
755 lua_setfield(L, -2, "preload");
757 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
758 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
759 lua_pop(L, 1); /* pop global table */
760 return 1; /* return 'package' table */
761}
#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)
#define luaL_newlib(L, l)
#define LUA_EXEC_DIR
#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 285 of file lua-5.4.3/src/loadlib.c.

285 {
286 int b;
287 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
288 b = lua_toboolean(L, -1);
289 lua_pop(L, 1); /* remove value */
290 return b;
291}

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

Referenced by setpath().

◆ pusherrornotfound()

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

Definition at line 471 of file lua-5.4.3/src/loadlib.c.

471 {
472 luaL_Buffer b;
473 luaL_buffinit(L, &b);
474 luaL_addstring(&b, "no file '");
475 luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
476 luaL_addstring(&b, "'");
477 luaL_pushresult(&b);
478}
LUALIB_API void luaL_addgsub(luaL_Buffer *b, const char *s, const char *p, const char *r)

References LUA_PATH_SEP, luaL_addgsub(), luaL_addstring(), luaL_buffinit(), and luaL_pushresult().

Referenced by searchpath().

◆ readable()

static int readable ( const char * filename)
static

Definition at line 434 of file lua-5.4.3/src/loadlib.c.

434 {
435 FILE *f = fopen(filename, "r"); /* try to open file */
436 if (f == NULL) return 0; /* open failed */
437 fclose(f);
438 return 1;
439}

References NULL.

Referenced by searchpath().

◆ searcher_C()

static int searcher_C ( lua_State * L)
static

Definition at line 580 of file lua-5.4.3/src/loadlib.c.

580 {
581 const char *name = luaL_checkstring(L, 1);
582 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
583 if (filename == NULL) return 1; /* module not found in this path */
584 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
585}
#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 588 of file lua-5.4.3/src/loadlib.c.

588 {
589 const char *filename;
590 const char *name = luaL_checkstring(L, 1);
591 const char *p = strchr(name, '.');
592 int stat;
593 if (p == NULL) return 0; /* is root */
594 lua_pushlstring(L, name, p - name);
595 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
596 if (filename == NULL) return 1; /* root not found */
597 if ((stat = loadfunc(L, filename, name)) != 0) {
598 if (stat != ERRFUNC)
599 return checkload(L, 0, filename); /* real error */
600 else { /* open function not found */
601 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
602 return 1;
603 }
604 }
605 lua_pushstring(L, filename); /* will be 2nd argument to module */
606 return 2;
607}

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

545 {
546 const char *filename;
547 const char *name = luaL_checkstring(L, 1);
548 filename = findfile(L, name, "path", LUA_LSUBSEP);
549 if (filename == NULL) return 1; /* module not found in this path */
550 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
551}
#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 610 of file lua-5.4.3/src/loadlib.c.

610 {
611 const char *name = luaL_checkstring(L, 1);
613 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
614 lua_pushfstring(L, "no field package.preload['%s']", name);
615 return 1;
616 }
617 else {
618 lua_pushliteral(L, ":preload:");
619 return 2;
620 }
621}

References lua_getfield(), LUA_PRELOAD_TABLE, lua_pushfstring(), lua_pushliteral, 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 481 of file lua-5.4.3/src/loadlib.c.

484 {
485 luaL_Buffer buff;
486 char *pathname; /* path with name inserted */
487 char *endpathname; /* its end */
488 const char *filename;
489 /* separator is non-empty and appears in 'name'? */
490 if (*sep != '\0' && strchr(name, *sep) != NULL)
491 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
492 luaL_buffinit(L, &buff);
493 /* add path to the buffer, replacing marks ('?') with the file name */
494 luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
495 luaL_addchar(&buff, '\0');
496 pathname = luaL_buffaddr(&buff); /* writable list of file names */
497 endpathname = pathname + luaL_bufflen(&buff) - 1;
498 while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
499 if (readable(filename)) /* does file exist and is readable? */
500 return lua_pushstring(L, filename); /* save and return name */
501 }
502 luaL_pushresult(&buff); /* push path to create error message */
503 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */
504 return NULL; /* not found */
505}
#define luaL_addchar(B, c)
#define luaL_buffaddr(bf)
#define luaL_bufflen(bf)
static const char * getnextfilename(char **path, char *end)
static void pusherrornotfound(lua_State *L, const char *path)
static int readable(const char *filename)

References getnextfilename(), LUA_PATH_MARK, lua_pushstring(), lua_tostring, luaL_addchar, luaL_addgsub(), luaL_buffaddr, luaL_buffinit(), luaL_bufflen, luaL_gsub(), luaL_pushresult(), name, NULL, pusherrornotfound(), 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 297 of file lua-5.4.3/src/loadlib.c.

299 {
300 const char *dftmark;
301 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
302 const char *path = getenv(nver); /* try versioned name */
303 if (path == NULL) /* no versioned environment variable? */
304 path = getenv(envname); /* try unversioned name */
305 if (path == NULL || noenv(L)) /* no environment variable? */
306 lua_pushstring(L, dft); /* use default */
307 else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
308 lua_pushstring(L, path); /* nothing to change */
309 else { /* path contains a ";;": insert default path in its place */
310 size_t len = strlen(path);
311 luaL_Buffer b;
312 luaL_buffinit(L, &b);
313 if (path < dftmark) { /* is there a prefix before ';;'? */
314 luaL_addlstring(&b, path, dftmark - path); /* add it */
316 }
317 luaL_addstring(&b, dft); /* add default */
318 if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
320 luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
321 }
322 luaL_pushresult(&b);
323 }
324 setprogdir(L);
325 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
326 lua_pop(L, 1); /* pop versioned variable name ('nver') */
327}
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
#define LUA_VERSUFFIX
static int noenv(lua_State *L)
#define setprogdir(L)

References LUA_PATH_SEP, lua_pop, lua_pushfstring(), lua_pushstring(), lua_setfield(), LUA_VERSUFFIX, luaL_addchar, luaL_addlstring(), luaL_addstring(), luaL_buffinit(), luaL_pushresult(), noenv(), NULL, and setprogdir.

Referenced by luaopen_package().

Variable Documentation

◆ CLIBS

const char* const CLIBS = "_CLIBS"
static

Definition at line 62 of file lua-5.4.3/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 704 of file lua-5.4.3/src/loadlib.c.

704 {
705 {"require", ll_require},
706 {NULL, NULL}
707};

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

691 {
692 {"loadlib", ll_loadlib},
693 {"searchpath", ll_searchpath},
694 /* placeholders */
695 {"preload", NULL},
696 {"cpath", NULL},
697 {"path", NULL},
698 {"searchers", NULL},
699 {"loaded", NULL},
700 {NULL, NULL}
701};

Referenced by luaopen_package().