xref: /freebsd/libexec/flua/linit_flua.c (revision f35ccf46c7c6cb7d26994ec5dc5926ea53be0116)
1506f3640SKyle Evans /*
2506f3640SKyle Evans ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $
3506f3640SKyle Evans ** Initialization of libraries for lua.c and other clients
4506f3640SKyle Evans ** See Copyright Notice in lua.h
5506f3640SKyle Evans */
6506f3640SKyle Evans 
7506f3640SKyle Evans 
8506f3640SKyle Evans #define linit_c
9506f3640SKyle Evans #define LUA_LIB
10506f3640SKyle Evans 
11506f3640SKyle Evans /*
12506f3640SKyle Evans ** If you embed Lua in your program and need to open the standard
13506f3640SKyle Evans ** libraries, call luaL_openlibs in your program. If you need a
14506f3640SKyle Evans ** different set of libraries, copy this file to your project and edit
15506f3640SKyle Evans ** it to suit your needs.
16506f3640SKyle Evans **
17506f3640SKyle Evans ** You can also *preload* libraries, so that a later 'require' can
18506f3640SKyle Evans ** open the library, which is already linked to the application.
19506f3640SKyle Evans ** For that, do the following code:
20506f3640SKyle Evans **
21506f3640SKyle Evans **  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
22506f3640SKyle Evans **  lua_pushcfunction(L, luaopen_modname);
23506f3640SKyle Evans **  lua_setfield(L, -2, modname);
24506f3640SKyle Evans **  lua_pop(L, 1);  // remove PRELOAD table
25506f3640SKyle Evans */
26506f3640SKyle Evans 
27506f3640SKyle Evans #include "lprefix.h"
28506f3640SKyle Evans 
29506f3640SKyle Evans 
30506f3640SKyle Evans #include <stddef.h>
31506f3640SKyle Evans 
32506f3640SKyle Evans #include "lua.h"
33506f3640SKyle Evans 
34506f3640SKyle Evans #include "lualib.h"
35506f3640SKyle Evans #include "lauxlib.h"
36506f3640SKyle Evans #include "lfs.h"
37506f3640SKyle Evans #include "lposix.h"
381f31e00eSBaptiste Daroussin #include "lfbsd.h"
39506f3640SKyle Evans 
40506f3640SKyle Evans /*
41506f3640SKyle Evans ** these libs are loaded by lua.c and are readily available to any Lua
42506f3640SKyle Evans ** program
43506f3640SKyle Evans */
44506f3640SKyle Evans static const luaL_Reg loadedlibs[] = {
45506f3640SKyle Evans   {"_G", luaopen_base},
46506f3640SKyle Evans   {LUA_LOADLIBNAME, luaopen_package},
47506f3640SKyle Evans   {LUA_COLIBNAME, luaopen_coroutine},
48506f3640SKyle Evans   {LUA_TABLIBNAME, luaopen_table},
49506f3640SKyle Evans   {LUA_IOLIBNAME, luaopen_io},
50506f3640SKyle Evans   {LUA_OSLIBNAME, luaopen_os},
51506f3640SKyle Evans   {LUA_STRLIBNAME, luaopen_string},
52506f3640SKyle Evans   {LUA_MATHLIBNAME, luaopen_math},
53506f3640SKyle Evans   {LUA_UTF8LIBNAME, luaopen_utf8},
54506f3640SKyle Evans   {LUA_DBLIBNAME, luaopen_debug},
55506f3640SKyle Evans #if defined(LUA_COMPAT_BITLIB)
56506f3640SKyle Evans   {LUA_BITLIBNAME, luaopen_bit32},
57506f3640SKyle Evans #endif
58506f3640SKyle Evans   /* FreeBSD Extensions */
59506f3640SKyle Evans   {"lfs", luaopen_lfs},
60*f35ccf46SStefan Eßer   {"posix.fnmatch", luaopen_posix_fnmatch},
61c2caf3b3SKyle Evans   {"posix.libgen", luaopen_posix_libgen},
62c2caf3b3SKyle Evans   {"posix.stdlib", luaopen_posix_stdlib},
63405e3338SEd Maste   {"posix.sys.stat", luaopen_posix_sys_stat},
641726db7aSMark Johnston   {"posix.sys.utsname", luaopen_posix_sys_utsname},
65c2caf3b3SKyle Evans   {"posix.sys.wait", luaopen_posix_sys_wait},
66506f3640SKyle Evans   {"posix.unistd", luaopen_posix_unistd},
671f31e00eSBaptiste Daroussin   {"fbsd", luaopen_fbsd},
68506f3640SKyle Evans   {NULL, NULL}
69506f3640SKyle Evans };
70506f3640SKyle Evans 
71506f3640SKyle Evans 
72506f3640SKyle Evans LUALIB_API void luaL_openlibs (lua_State *L) {
73506f3640SKyle Evans   const luaL_Reg *lib;
74506f3640SKyle Evans   /* "require" functions from 'loadedlibs' and set results to global table */
75506f3640SKyle Evans   for (lib = loadedlibs; lib->func; lib++) {
76506f3640SKyle Evans     luaL_requiref(L, lib->name, lib->func, 1);
77506f3640SKyle Evans     lua_pop(L, 1);  /* remove lib */
78506f3640SKyle Evans   }
79506f3640SKyle Evans }
80506f3640SKyle Evans 
81