fix workflow yaml
[RRG-proxmark3.git] / client / deps / liblua / loadlib.c
blobc5d34455c5c8086c10475383dcbbca8eade145d2
1 /*
2 ** $Id: loadlib.c $
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 Windows, and a stub for other
8 ** systems.
9 */
11 #define loadlib_c
12 #define LUA_LIB
14 #include "lprefix.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "lua.h"
23 #include "lauxlib.h"
24 #include "lualib.h"
28 ** LUA_CSUBSEP is the character that replaces dots in submodule names
29 ** when searching for a C loader.
30 ** LUA_LSUBSEP is the character that replaces dots in submodule names
31 ** when searching for a Lua loader.
33 #if !defined(LUA_CSUBSEP)
34 #define LUA_CSUBSEP LUA_DIRSEP
35 #endif
37 #if !defined(LUA_LSUBSEP)
38 #define LUA_LSUBSEP LUA_DIRSEP
39 #endif
42 /* prefix for open functions in C libraries */
43 #define LUA_POF "luaopen_"
45 /* separator for open functions in C libraries */
46 #define LUA_OFSEP "_"
50 ** key for table in the registry that keeps handles
51 ** for all loaded C libraries
53 static const char *const CLIBS = "_CLIBS";
55 #define LIB_FAIL "open"
58 #define setprogdir(L) ((void)0)
62 ** Special type equivalent to '(void*)' for functions in gcc
63 ** (to suppress warnings when converting function pointers)
65 typedef void (*voidf)(void);
69 ** system-dependent functions
73 ** unload library 'lib'
75 static void lsys_unloadlib(void *lib);
78 ** load C library in file 'path'. If 'seeglb', load with all names in
79 ** the library global.
80 ** Returns the library; in case of error, returns NULL plus an
81 ** error string in the stack.
83 static void *lsys_load(lua_State *L, const char *path, int seeglb);
86 ** Try to find a function named 'sym' in library 'lib'.
87 ** Returns the function; in case of error, returns NULL plus an
88 ** error string in the stack.
90 static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym);
95 #if defined(LUA_USE_DLOPEN) /* { */
97 ** {========================================================================
98 ** This is an implementation of loadlib based on the dlfcn interface.
99 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
100 ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
101 ** as an emulation layer on top of native functions.
102 ** =========================================================================
105 #include <dlfcn.h>
108 ** Macro to convert pointer-to-void* to pointer-to-function. This cast
109 ** is undefined according to ISO C, but POSIX assumes that it works.
110 ** (The '__extension__' in gnu compilers is only to avoid warnings.)
112 #if defined(__GNUC__)
113 #define cast_func(p) (__extension__ (lua_CFunction)(p))
114 #else
115 #define cast_func(p) ((lua_CFunction)(p))
116 #endif
119 static void lsys_unloadlib(void *lib) {
120 dlclose(lib);
124 static void *lsys_load(lua_State *L, const char *path, int seeglb) {
125 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
126 if (l_unlikely(lib == NULL))
127 lua_pushstring(L, dlerror());
128 return lib;
132 static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
133 lua_CFunction f = cast_func(dlsym(lib, sym));
134 if (l_unlikely(f == NULL))
135 lua_pushstring(L, dlerror());
136 return f;
139 /* }====================================================== */
143 #elif defined(LUA_DL_DLL) /* }{ */
145 ** {======================================================================
146 ** This is an implementation of loadlib for Windows using native functions.
147 ** =======================================================================
150 #include <windows.h>
154 ** optional flags for LoadLibraryEx
156 #if !defined(LUA_LLE_FLAGS)
157 #define LUA_LLE_FLAGS 0
158 #endif
161 #undef setprogdir
165 ** Replace in the path (on the top of the stack) any occurrence
166 ** of LUA_EXEC_DIR with the executable's path.
168 static void setprogdir(lua_State *L) {
169 char buff[MAX_PATH + 1];
170 char *lb;
171 DWORD nsize = sizeof(buff) / sizeof(char);
172 DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */
173 if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
174 luaL_error(L, "unable to get ModuleFileName");
175 else {
176 *lb = '\0'; /* cut name on the last '\\' to get the path */
177 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
178 lua_remove(L, -2); /* remove original string */
185 static void pusherror(lua_State *L) {
186 int error = GetLastError();
187 char buffer[128];
188 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
189 NULL, error, 0, buffer, sizeof(buffer) / sizeof(char), NULL))
190 lua_pushstring(L, buffer);
191 else
192 lua_pushfstring(L, "system error %d\n", error);
195 static void lsys_unloadlib(void *lib) {
196 FreeLibrary((HMODULE)lib);
200 static void *lsys_load(lua_State *L, const char *path, int seeglb) {
201 HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
202 (void)(seeglb); /* not used: symbols are 'global' by default */
203 if (lib == NULL) pusherror(L);
204 return lib;
208 static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
209 lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
210 if (f == NULL) pusherror(L);
211 return f;
214 /* }====================================================== */
217 #else /* }{ */
219 ** {======================================================
220 ** Fallback for other systems
221 ** =======================================================
224 #undef LIB_FAIL
225 #define LIB_FAIL "absent"
228 #define DLMSG "dynamic libraries not enabled; check your Lua installation"
231 static void lsys_unloadlib(void *lib) {
232 (void)(lib); /* not used */
236 static void *lsys_load(lua_State *L, const char *path, int seeglb) {
237 (void)(path);
238 (void)(seeglb); /* not used */
239 lua_pushliteral(L, DLMSG);
240 return NULL;
244 static lua_CFunction lsys_sym(lua_State *L, void *lib, const char *sym) {
245 (void)(lib);
246 (void)(sym); /* not used */
247 lua_pushliteral(L, DLMSG);
248 return NULL;
251 /* }====================================================== */
252 #endif /* } */
256 ** {==================================================================
257 ** Set Paths
258 ** ===================================================================
262 ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
263 ** variables that Lua check to set its paths.
265 #if !defined(LUA_PATH_VAR)
266 #define LUA_PATH_VAR "LUA_PATH"
267 #endif
269 #if !defined(LUA_CPATH_VAR)
270 #define LUA_CPATH_VAR "LUA_CPATH"
271 #endif
276 ** return registry.LUA_NOENV as a boolean
278 static int noenv(lua_State *L) {
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;
288 ** Set a path
290 static void setpath(lua_State *L, const char *fieldname,
291 const char *envname,
292 const char *dft) {
293 const char *dftmark;
294 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
295 const char *path = getenv(nver); /* try versioned name */
296 if (path == NULL) /* no versioned environment variable? */
297 path = getenv(envname); /* try unversioned name */
298 if (path == NULL || noenv(L)) /* no environment variable? */
299 lua_pushstring(L, dft); /* use default */
300 else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
301 lua_pushstring(L, path); /* nothing to change */
302 else { /* path contains a ";;": insert default path in its place */
303 size_t len = strlen(path);
304 luaL_Buffer b;
305 luaL_buffinit(L, &b);
306 if (path < dftmark) { /* is there a prefix before ';;'? */
307 luaL_addlstring(&b, path, dftmark - path); /* add it */
308 luaL_addchar(&b, *LUA_PATH_SEP);
310 luaL_addstring(&b, dft); /* add default */
311 if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
312 luaL_addchar(&b, *LUA_PATH_SEP);
313 luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
315 luaL_pushresult(&b);
317 setprogdir(L);
318 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
319 lua_pop(L, 1); /* pop versioned variable name ('nver') */
322 /* }================================================================== */
326 ** return registry.CLIBS[path]
328 static void *checkclib(lua_State *L, const char *path) {
329 void *plib;
330 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
331 lua_getfield(L, -1, path);
332 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
333 lua_pop(L, 2); /* pop CLIBS table and 'plib' */
334 return plib;
339 ** registry.CLIBS[path] = plib -- for queries
340 ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
342 static void addtoclib(lua_State *L, const char *path, void *plib) {
343 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
344 lua_pushlightuserdata(L, plib);
345 lua_pushvalue(L, -1);
346 lua_setfield(L, -3, path); /* CLIBS[path] = plib */
347 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
348 lua_pop(L, 1); /* pop CLIBS table */
353 ** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
354 ** handles in list CLIBS
356 static int gctm(lua_State *L) {
357 lua_Integer n = luaL_len(L, 1);
358 for (; n >= 1; n--) { /* for each handle, in reverse order */
359 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
360 lsys_unloadlib(lua_touserdata(L, -1));
361 lua_pop(L, 1); /* pop handle */
363 return 0;
368 /* error codes for 'lookforfunc' */
369 #define ERRLIB 1
370 #define ERRFUNC 2
373 ** Look for a C function named 'sym' in a dynamically loaded library
374 ** 'path'.
375 ** First, check whether the library is already loaded; if not, try
376 ** to load it.
377 ** Then, if 'sym' is '*', return true (as library has been loaded).
378 ** Otherwise, look for symbol 'sym' in the library and push a
379 ** C function with that symbol.
380 ** Return 0 and 'true' or a function in the stack; in case of
381 ** errors, return an error code and an error message in the stack.
383 static int lookforfunc(lua_State *L, const char *path, const char *sym) {
384 void *reg = checkclib(L, path); /* check loaded C libraries */
385 if (reg == NULL) { /* must load library? */
386 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
387 if (reg == NULL) return ERRLIB; /* unable to load library */
388 addtoclib(L, path, reg);
390 if (*sym == '*') { /* loading only library (no function)? */
391 lua_pushboolean(L, 1); /* return 'true' */
392 return 0; /* no errors */
393 } else {
394 lua_CFunction f = lsys_sym(L, reg, sym);
395 if (f == NULL)
396 return ERRFUNC; /* unable to find function */
397 lua_pushcfunction(L, f); /* else create new function */
398 return 0; /* no errors */
403 static int ll_loadlib(lua_State *L) {
404 const char *path = luaL_checkstring(L, 1);
405 const char *init = luaL_checkstring(L, 2);
406 int stat = lookforfunc(L, path, init);
407 if (l_likely(stat == 0)) /* no errors? */
408 return 1; /* return the loaded function */
409 else { /* error; error message is on stack top */
410 luaL_pushfail(L);
411 lua_insert(L, -2);
412 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
413 return 3; /* return fail, error message, and where */
420 ** {======================================================
421 ** 'require' function
422 ** =======================================================
426 static int readable(const char *filename) {
427 FILE *f = fopen(filename, "r"); /* try to open file */
428 if (f == NULL) return 0; /* open failed */
429 fclose(f);
430 return 1;
435 ** Get the next name in '*path' = 'name1;name2;name3;...', changing
436 ** the ending ';' to '\0' to create a zero-terminated string. Return
437 ** NULL when list ends.
439 static const char *getnextfilename(char **path, char *end) {
440 char *sep;
441 char *name = *path;
442 if (name == end)
443 return NULL; /* no more names */
444 else if (*name == '\0') { /* from previous iteration? */
445 *name = *LUA_PATH_SEP; /* restore separator */
446 name++; /* skip it */
448 sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
449 if (sep == NULL) /* separator not found? */
450 sep = end; /* name goes until the end */
451 *sep = '\0'; /* finish file name */
452 *path = sep; /* will start next search from here */
453 return name;
458 ** Given a path such as ";blabla.so;blublu.so", pushes the string
460 ** no file 'blabla.so'
461 ** no file 'blublu.so'
463 static void pusherrornotfound(lua_State *L, const char *path) {
464 luaL_Buffer b;
465 luaL_buffinit(L, &b);
466 luaL_addstring(&b, "no file '");
467 luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
468 luaL_addstring(&b, "'");
469 luaL_pushresult(&b);
473 static const char *searchpath(lua_State *L, const char *name,
474 const char *path,
475 const char *sep,
476 const char *dirsep) {
477 luaL_Buffer buff;
478 char *pathname; /* path with name inserted */
479 char *endpathname; /* its end */
480 const char *filename;
481 /* separator is non-empty and appears in 'name'? */
482 if (*sep != '\0' && strchr(name, *sep) != NULL)
483 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
484 luaL_buffinit(L, &buff);
485 /* add path to the buffer, replacing marks ('?') with the file name */
486 luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
487 luaL_addchar(&buff, '\0');
488 pathname = luaL_buffaddr(&buff); /* writable list of file names */
489 endpathname = pathname + luaL_bufflen(&buff) - 1;
490 while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
491 if (readable(filename)) /* does file exist and is readable? */
492 return lua_pushstring(L, filename); /* save and return name */
494 luaL_pushresult(&buff); /* push path to create error message */
495 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */
496 return NULL; /* not found */
500 static int ll_searchpath(lua_State *L) {
501 const char *f = searchpath(L, luaL_checkstring(L, 1),
502 luaL_checkstring(L, 2),
503 luaL_optstring(L, 3, "."),
504 luaL_optstring(L, 4, LUA_DIRSEP));
505 if (f != NULL) return 1;
506 else { /* error message is on top of the stack */
507 luaL_pushfail(L);
508 lua_insert(L, -2);
509 return 2; /* return fail + error message */
514 static const char *findfile(lua_State *L, const char *name,
515 const char *pname,
516 const char *dirsep) {
517 const char *path;
518 lua_getfield(L, lua_upvalueindex(1), pname);
519 path = lua_tostring(L, -1);
520 if (l_unlikely(path == NULL))
521 luaL_error(L, "'package.%s' must be a string", pname);
522 return searchpath(L, name, path, ".", dirsep);
526 static int checkload(lua_State *L, int stat, const char *filename) {
527 if (l_likely(stat)) { /* module loaded successfully? */
528 lua_pushstring(L, filename); /* will be 2nd argument to module */
529 return 2; /* return open function and file name */
530 } else
531 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
532 lua_tostring(L, 1), filename, lua_tostring(L, -1));
536 static int searcher_Lua(lua_State *L) {
537 const char *filename;
538 const char *name = luaL_checkstring(L, 1);
539 filename = findfile(L, name, "path", LUA_LSUBSEP);
540 if (filename == NULL) return 1; /* module not found in this path */
541 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
546 ** Try to find a load function for module 'modname' at file 'filename'.
547 ** First, change '.' to '_' in 'modname'; then, if 'modname' has
548 ** the form X-Y (that is, it has an "ignore mark"), build a function
549 ** name "luaopen_X" and look for it. (For compatibility, if that
550 ** fails, it also tries "luaopen_Y".) If there is no ignore mark,
551 ** look for a function named "luaopen_modname".
553 static int loadfunc(lua_State *L, const char *filename, const char *modname) {
554 const char *openfunc;
555 const char *mark;
556 modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
557 mark = strchr(modname, *LUA_IGMARK);
558 if (mark) {
559 int stat;
560 openfunc = lua_pushlstring(L, modname, mark - modname);
561 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
562 stat = lookforfunc(L, filename, openfunc);
563 if (stat != ERRFUNC) return stat;
564 modname = mark + 1; /* else go ahead and try old-style name */
566 openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
567 return lookforfunc(L, filename, openfunc);
571 static int searcher_C(lua_State *L) {
572 const char *name = luaL_checkstring(L, 1);
573 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
574 if (filename == NULL) return 1; /* module not found in this path */
575 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
579 static int searcher_Croot(lua_State *L) {
580 const char *filename;
581 const char *name = luaL_checkstring(L, 1);
582 const char *p = strchr(name, '.');
583 int stat;
584 if (p == NULL) return 0; /* is root */
585 lua_pushlstring(L, name, p - name);
586 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
587 if (filename == NULL) return 1; /* root not found */
588 if ((stat = loadfunc(L, filename, name)) != 0) {
589 if (stat != ERRFUNC)
590 return checkload(L, 0, filename); /* real error */
591 else { /* open function not found */
592 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
593 return 1;
596 lua_pushstring(L, filename); /* will be 2nd argument to module */
597 return 2;
601 static int searcher_preload(lua_State *L) {
602 const char *name = luaL_checkstring(L, 1);
603 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
604 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
605 lua_pushfstring(L, "no field package.preload['%s']", name);
606 return 1;
607 } else {
608 lua_pushliteral(L, ":preload:");
609 return 2;
614 static void findloader(lua_State *L, const char *name) {
615 int i;
616 luaL_Buffer msg; /* to build error message */
617 /* push 'package.searchers' to index 3 in the stack */
618 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
619 != LUA_TTABLE))
620 luaL_error(L, "'package.searchers' must be a table");
621 luaL_buffinit(L, &msg);
622 /* iterate over available searchers to find a loader */
623 for (i = 1; ; i++) {
624 luaL_addstring(&msg, "\n\t"); /* error-message prefix */
625 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
626 lua_pop(L, 1); /* remove nil */
627 luaL_buffsub(&msg, 2); /* remove prefix */
628 luaL_pushresult(&msg); /* create error message */
629 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
631 lua_pushstring(L, name);
632 lua_call(L, 1, 2); /* call it */
633 if (lua_isfunction(L, -2)) /* did it find a loader? */
634 return; /* module loader found */
635 else if (lua_isstring(L, -2)) { /* searcher returned error message? */
636 lua_pop(L, 1); /* remove extra return */
637 luaL_addvalue(&msg); /* concatenate error message */
638 } else { /* no error message */
639 lua_pop(L, 2); /* remove both returns */
640 luaL_buffsub(&msg, 2); /* remove prefix */
646 static int ll_require(lua_State *L) {
647 const char *name = luaL_checkstring(L, 1);
648 lua_settop(L, 1); /* LOADED table will be at index 2 */
649 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
650 lua_getfield(L, 2, name); /* LOADED[name] */
651 if (lua_toboolean(L, -1)) /* is it there? */
652 return 1; /* package is already loaded */
653 /* else must load package */
654 lua_pop(L, 1); /* remove 'getfield' result */
655 findloader(L, name);
656 lua_rotate(L, -2, 1); /* function <-> loader data */
657 lua_pushvalue(L, 1); /* name is 1st argument to module loader */
658 lua_pushvalue(L, -3); /* loader data is 2nd argument */
659 /* stack: ...; loader data; loader function; mod. name; loader data */
660 lua_call(L, 2, 1); /* run loader to load module */
661 /* stack: ...; loader data; result from loader */
662 if (!lua_isnil(L, -1)) /* non-nil return? */
663 lua_setfield(L, 2, name); /* LOADED[name] = returned value */
664 else
665 lua_pop(L, 1); /* pop nil */
666 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
667 lua_pushboolean(L, 1); /* use true as result */
668 lua_copy(L, -1, -2); /* replace loader result */
669 lua_setfield(L, 2, name); /* LOADED[name] = true */
671 lua_rotate(L, -2, 1); /* loader data <-> module result */
672 return 2; /* return module result and loader data */
675 /* }====================================================== */
680 static const luaL_Reg pk_funcs[] = {
681 {"loadlib", ll_loadlib},
682 {"searchpath", ll_searchpath},
683 /* placeholders */
684 {"preload", NULL},
685 {"cpath", NULL},
686 {"path", NULL},
687 {"searchers", NULL},
688 {"loaded", NULL},
689 {NULL, NULL}
693 static const luaL_Reg ll_funcs[] = {
694 {"require", ll_require},
695 {NULL, NULL}
699 static void createsearcherstable(lua_State *L) {
700 static const lua_CFunction searchers[] = {
701 searcher_preload,
702 searcher_Lua,
703 searcher_C,
704 searcher_Croot,
705 NULL
707 int i;
708 /* create 'searchers' table */
709 lua_createtable(L, sizeof(searchers) / sizeof(searchers[0]) - 1, 0);
710 /* fill it with predefined searchers */
711 for (i = 0; searchers[i] != NULL; i++) {
712 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
713 lua_pushcclosure(L, searchers[i], 1);
714 lua_rawseti(L, -2, i + 1);
716 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
721 ** create table CLIBS to keep track of loaded C libraries,
722 ** setting a finalizer to close all libraries when closing state.
724 static void createclibstable(lua_State *L) {
725 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
726 lua_createtable(L, 0, 1); /* create metatable for CLIBS */
727 lua_pushcfunction(L, gctm);
728 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
729 lua_setmetatable(L, -2);
733 LUAMOD_API int luaopen_package(lua_State *L) {
734 createclibstable(L);
735 luaL_newlib(L, pk_funcs); /* create 'package' table */
736 createsearcherstable(L);
737 /* set paths */
738 setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
739 setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
740 /* store config information */
741 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
742 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
743 lua_setfield(L, -2, "config");
744 /* set field 'loaded' */
745 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
746 lua_setfield(L, -2, "loaded");
747 /* set field 'preload' */
748 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
749 lua_setfield(L, -2, "preload");
750 lua_pushglobaltable(L);
751 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
752 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
753 lua_pop(L, 1); /* pop global table */
754 return 1; /* return 'package' table */