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 INTERP_DEFINE("lua"); 64 65 static void * 66 interp_lua_realloc(void *ud __unused, void *ptr, size_t osize __unused, size_t nsize) 67 { 68 69 if (nsize == 0) { 70 free(ptr); 71 return NULL; 72 } 73 return realloc(ptr, nsize); 74 } 75 76 /* 77 * The libraries commented out below either lack the proper 78 * support from libsa, or they are unlikely to be useful 79 * in the bootloader, so have been commented out. 80 */ 81 static const luaL_Reg loadedlibs[] = { 82 {"_G", luaopen_base}, 83 {LUA_LOADLIBNAME, luaopen_package}, 84 // {LUA_COLIBNAME, luaopen_coroutine}, 85 // {LUA_TABLIBNAME, luaopen_table}, 86 {LUA_STRLIBNAME, luaopen_string}, 87 // {LUA_IOLIBNAME, luaopen_io}, 88 // {LUA_OSLIBNAME, luaopen_os}, 89 // {LUA_MATHLIBNAME, luaopen_math}, 90 // {LUA_UTF8LIBNAME, luaopen_utf8}, 91 // {LUA_DBLIBNAME, luaopen_debug}, 92 {"errno", luaopen_errno}, 93 {"io", luaopen_io}, 94 {"lfs", luaopen_lfs}, 95 {"loader", luaopen_loader}, 96 {NULL, NULL} 97 }; 98 99 void 100 interp_init(void) 101 { 102 lua_State *luap; 103 struct interp_lua_softc *softc = &lua_softc; 104 const char *filename; 105 const luaL_Reg *lib; 106 107 setenv("script.lang", "lua", 1); 108 LDBG("creating context"); 109 110 luap = lua_newstate(interp_lua_realloc, NULL); 111 if (luap == NULL) { 112 printf("problem initializing the Lua interpreter\n"); 113 abort(); 114 } 115 softc->luap = luap; 116 117 /* "require" functions from 'loadedlibs' and set results to global table */ 118 for (lib = loadedlibs; lib->func; lib++) { 119 luaL_requiref(luap, lib->name, lib->func, 1); 120 lua_pop(luap, 1); /* remove lib */ 121 } 122 123 filename = "/boot/lua/loader.lua"; 124 if (interp_include(filename) != 0) { 125 const char *errstr = lua_tostring(luap, -1); 126 errstr = errstr == NULL ? "unknown" : errstr; 127 printf("Startup error in %s:\nLUA ERROR: %s.\n", filename, errstr); 128 lua_pop(luap, 1); 129 } 130 } 131 132 int 133 interp_run(const char *line) 134 { 135 int argc, nargc; 136 char **argv; 137 lua_State *luap; 138 struct interp_lua_softc *softc = &lua_softc; 139 int status, ret; 140 141 luap = softc->luap; 142 LDBG("executing line..."); 143 if ((status = luaL_dostring(luap, line)) != 0) { 144 lua_pop(luap, 1); 145 /* 146 * The line wasn't executable as lua; run it through parse to 147 * to get consistent parsing of command line arguments, then 148 * run it through cli_execute. If that fails, then we'll try it 149 * as a builtin. 150 */ 151 command_errmsg = NULL; 152 if (parse(&argc, &argv, line) == 0) { 153 lua_getglobal(luap, "cli_execute"); 154 for (nargc = 0; nargc < argc; ++nargc) { 155 lua_pushstring(luap, argv[nargc]); 156 } 157 status = lua_pcall(luap, argc, 1, 0); 158 ret = lua_tointeger(luap, 1); 159 lua_pop(luap, 1); 160 if (status != 0 || ret != 0) { 161 /* 162 * Lua cli_execute will pass the function back 163 * through loader.command, which is a proxy to 164 * interp_builtin_cmd. If we failed to interpret 165 * the command, though, then there's a chance 166 * that didn't happen. Call interp_builtin_cmd 167 * directly if our lua_pcall was not successful. 168 */ 169 status = interp_builtin_cmd(argc, argv); 170 } 171 if (status != 0) { 172 if (command_errmsg != NULL) 173 printf("%s\n", command_errmsg); 174 else 175 printf("Command failed\n"); 176 status = CMD_ERROR; 177 } 178 free(argv); 179 } else { 180 printf("Failed to parse \'%s\'\n", line); 181 status = CMD_ERROR; 182 } 183 } 184 185 return (status == 0 ? CMD_OK : CMD_ERROR); 186 } 187 188 int 189 interp_include(const char *filename) 190 { 191 struct interp_lua_softc *softc = &lua_softc; 192 193 LDBG("loading file %s", filename); 194 195 return (luaL_dofile(softc->luap, filename)); 196 } 197