xref: /freebsd/stand/common/interp_lua.c (revision 25ecdc7d52770caf1c9b44b5ec11f468f6b636f3)
1 /*-
2  * Copyright (c) 2011 Wojciech A. Koszek <wkoszek@FreeBSD.org>
3  * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <stand.h>
32 #include "bootstrap.h"
33 
34 #define lua_c
35 
36 #include "lstd.h"
37 
38 #include <lua.h>
39 #include <ldebug.h>
40 #include <lauxlib.h>
41 #include <lualib.h>
42 
43 #include <lerrno.h>
44 #include <lfs.h>
45 #include <lutils.h>
46 
47 struct interp_lua_softc {
48 	lua_State	*luap;
49 };
50 
51 static struct interp_lua_softc lua_softc;
52 
53 #ifdef LUA_DEBUG
54 #define	LDBG(...)	do {			\
55 	printf("%s(%d): ", __func__, __LINE__);	\
56 	printf(__VA_ARGS__);			\
57 	printf("\n");				\
58 } while (0)
59 #else
60 #define	LDBG(...)
61 #endif
62 
63 #define	LOADER_LUA	LUA_PATH "/loader.lua"
64 
65 INTERP_DEFINE("lua");
66 
67 static void *
68 interp_lua_realloc(void *ud __unused, void *ptr, size_t osize __unused, size_t nsize)
69 {
70 
71 	if (nsize == 0) {
72 		free(ptr);
73 		return NULL;
74 	}
75 	return realloc(ptr, nsize);
76 }
77 
78 /*
79  * The libraries commented out below either lack the proper
80  * support from libsa, or they are unlikely to be useful
81  * in the bootloader, so have been commented out.
82  */
83 static const luaL_Reg loadedlibs[] = {
84   {"_G", luaopen_base},
85   {LUA_LOADLIBNAME, luaopen_package},
86 //  {LUA_COLIBNAME, luaopen_coroutine},
87 //  {LUA_TABLIBNAME, luaopen_table},
88   {LUA_STRLIBNAME, luaopen_string},
89 //  {LUA_IOLIBNAME, luaopen_io},
90 //  {LUA_OSLIBNAME, luaopen_os},
91 //  {LUA_MATHLIBNAME, luaopen_math},
92 //  {LUA_UTF8LIBNAME, luaopen_utf8},
93 //  {LUA_DBLIBNAME, luaopen_debug},
94   {"errno", luaopen_errno},
95   {"io", luaopen_io},
96   {"lfs", luaopen_lfs},
97   {"loader", luaopen_loader},
98   {"pager", luaopen_pager},
99   {NULL, NULL}
100 };
101 
102 void
103 interp_init(void)
104 {
105 	lua_State *luap;
106 	struct interp_lua_softc	*softc = &lua_softc;
107 	const char *filename;
108 	const luaL_Reg *lib;
109 
110 	setenv("script.lang", "lua", 1);
111 	LDBG("creating context");
112 
113 	luap = lua_newstate(interp_lua_realloc, NULL);
114 	if (luap == NULL) {
115 		printf("problem initializing the Lua interpreter\n");
116 		abort();
117 	}
118 	softc->luap = luap;
119 
120 	/* "require" functions from 'loadedlibs' and set results to global table */
121 	for (lib = loadedlibs; lib->func; lib++) {
122 		luaL_requiref(luap, lib->name, lib->func, 1);
123 		lua_pop(luap, 1);  /* remove lib */
124 	}
125 
126 	filename = LOADER_LUA;
127 	if (interp_include(filename) != 0) {
128 		const char *errstr = lua_tostring(luap, -1);
129 		errstr = errstr == NULL ? "unknown" : errstr;
130 		printf("ERROR: %s.\n", errstr);
131 		lua_pop(luap, 1);
132 		setenv("autoboot_delay", "NO", 1);
133 	}
134 }
135 
136 int
137 interp_run(const char *line)
138 {
139 	int	argc, nargc;
140 	char	**argv;
141 	lua_State *luap;
142 	struct interp_lua_softc	*softc = &lua_softc;
143 	int status, ret;
144 
145 	luap = softc->luap;
146 	LDBG("executing line...");
147 	if ((status = luaL_dostring(luap, line)) != 0) {
148 		lua_pop(luap, 1);
149 		/*
150 		 * The line wasn't executable as lua; run it through parse to
151 		 * to get consistent parsing of command line arguments, then
152 		 * run it through cli_execute. If that fails, then we'll try it
153 		 * as a builtin.
154 		 */
155 		command_errmsg = NULL;
156 		if (parse(&argc, &argv, line) == 0) {
157 			lua_getglobal(luap, "cli_execute");
158 			for (nargc = 0; nargc < argc; ++nargc) {
159 				lua_pushstring(luap, argv[nargc]);
160 			}
161 			status = lua_pcall(luap, argc, 1, 0);
162 			ret = lua_tointeger(luap, 1);
163 			lua_pop(luap, 1);
164 			if (status != 0 || ret != 0) {
165 				/*
166 				 * Lua cli_execute will pass the function back
167 				 * through loader.command, which is a proxy to
168 				 * interp_builtin_cmd. If we failed to interpret
169 				 * the command, though, then there's a chance
170 				 * that didn't happen. Call interp_builtin_cmd
171 				 * directly if our lua_pcall was not successful.
172 				 */
173 				status = interp_builtin_cmd(argc, argv);
174 			}
175 			if (status != 0) {
176 				if (command_errmsg != NULL)
177 					printf("%s\n", command_errmsg);
178 				else
179 					printf("Command failed\n");
180 				status = CMD_ERROR;
181 			}
182 			free(argv);
183 		} else {
184 			printf("Failed to parse \'%s\'\n", line);
185 			status = CMD_ERROR;
186 		}
187 	}
188 
189 	return (status == 0 ? CMD_OK : CMD_ERROR);
190 }
191 
192 int
193 interp_include(const char *filename)
194 {
195 	struct interp_lua_softc	*softc = &lua_softc;
196 
197 	LDBG("loading file %s", filename);
198 
199 	return (luaL_dofile(softc->luap, filename));
200 }
201