xref: /freebsd/libexec/flua/linit_flua.c (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1 /* $FreeBSD$ */
2 /*
3 ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $
4 ** Initialization of libraries for lua.c and other clients
5 ** See Copyright Notice in lua.h
6 */
7 
8 
9 #define linit_c
10 #define LUA_LIB
11 
12 /*
13 ** If you embed Lua in your program and need to open the standard
14 ** libraries, call luaL_openlibs in your program. If you need a
15 ** different set of libraries, copy this file to your project and edit
16 ** it to suit your needs.
17 **
18 ** You can also *preload* libraries, so that a later 'require' can
19 ** open the library, which is already linked to the application.
20 ** For that, do the following code:
21 **
22 **  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
23 **  lua_pushcfunction(L, luaopen_modname);
24 **  lua_setfield(L, -2, modname);
25 **  lua_pop(L, 1);  // remove PRELOAD table
26 */
27 
28 #include "lprefix.h"
29 
30 
31 #include <stddef.h>
32 
33 #include "lua.h"
34 
35 #include "lualib.h"
36 #include "lauxlib.h"
37 #include "lfs.h"
38 #include "lposix.h"
39 #include "lua_ucl.h"
40 
41 /*
42 ** these libs are loaded by lua.c and are readily available to any Lua
43 ** program
44 */
45 static const luaL_Reg loadedlibs[] = {
46   {"_G", luaopen_base},
47   {LUA_LOADLIBNAME, luaopen_package},
48   {LUA_COLIBNAME, luaopen_coroutine},
49   {LUA_TABLIBNAME, luaopen_table},
50   {LUA_IOLIBNAME, luaopen_io},
51   {LUA_OSLIBNAME, luaopen_os},
52   {LUA_STRLIBNAME, luaopen_string},
53   {LUA_MATHLIBNAME, luaopen_math},
54   {LUA_UTF8LIBNAME, luaopen_utf8},
55   {LUA_DBLIBNAME, luaopen_debug},
56 #if defined(LUA_COMPAT_BITLIB)
57   {LUA_BITLIBNAME, luaopen_bit32},
58 #endif
59   /* FreeBSD Extensions */
60   {"lfs", luaopen_lfs},
61   {"posix.sys.stat", luaopen_posix_sys_stat},
62   {"posix.unistd", luaopen_posix_unistd},
63   {"ucl", luaopen_ucl},
64   {NULL, NULL}
65 };
66 
67 
68 LUALIB_API void luaL_openlibs (lua_State *L) {
69   const luaL_Reg *lib;
70   /* "require" functions from 'loadedlibs' and set results to global table */
71   for (lib = loadedlibs; lib->func; lib++) {
72     luaL_requiref(L, lib->name, lib->func, 1);
73     lua_pop(L, 1);  /* remove lib */
74   }
75 }
76 
77