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 #include <lutils.h> 43 #include <lfs.h> 44 45 struct interp_lua_softc { 46 lua_State *luap; 47 }; 48 49 static struct interp_lua_softc lua_softc; 50 51 #ifdef LUA_DEBUG 52 #define LDBG(...) do { \ 53 printf("%s(%d): ", __func__, __LINE__); \ 54 printf(__VA_ARGS__); \ 55 printf("\n"); \ 56 } while (0) 57 #else 58 #define LDBG(...) 59 #endif 60 61 62 static void * 63 interp_lua_realloc(void *ud __unused, void *ptr, size_t osize __unused, size_t nsize) 64 { 65 66 if (nsize == 0) { 67 free(ptr); 68 return NULL; 69 } 70 return realloc(ptr, nsize); 71 } 72 73 /* 74 * The libraries commented out below either lack the proper 75 * support from libsa, or they are unlikely to be useful 76 * in the bootloader, so have been commented out. 77 */ 78 static const luaL_Reg loadedlibs[] = { 79 {"_G", luaopen_base}, 80 {LUA_LOADLIBNAME, luaopen_package}, 81 // {LUA_COLIBNAME, luaopen_coroutine}, 82 // {LUA_TABLIBNAME, luaopen_table}, 83 {LUA_STRLIBNAME, luaopen_string}, 84 // {LUA_IOLIBNAME, luaopen_io}, 85 // {LUA_OSLIBNAME, luaopen_os}, 86 // {LUA_MATHLIBNAME, luaopen_math}, 87 // {LUA_UTF8LIBNAME, luaopen_utf8}, 88 // {LUA_DBLIBNAME, luaopen_debug}, 89 {"io", luaopen_io}, 90 {"lfs", luaopen_lfs}, 91 {"loader", luaopen_loader}, 92 {NULL, NULL} 93 }; 94 95 void 96 interp_init(void) 97 { 98 lua_State *luap; 99 struct interp_lua_softc *softc = &lua_softc; 100 const char *filename; 101 const luaL_Reg *lib; 102 103 setenv("script.lang", "lua", 1); 104 LDBG("creating context"); 105 106 luap = lua_newstate(interp_lua_realloc, NULL); 107 if (luap == NULL) { 108 printf("problem initializing the Lua interpreter\n"); 109 abort(); 110 } 111 softc->luap = luap; 112 113 /* "require" functions from 'loadedlibs' and set results to global table */ 114 for (lib = loadedlibs; lib->func; lib++) { 115 luaL_requiref(luap, lib->name, lib->func, 1); 116 lua_pop(luap, 1); /* remove lib */ 117 } 118 119 filename = "/boot/lua/loader.lua"; 120 if (interp_include(filename) != 0) { 121 const char *errstr = lua_tostring(luap, -1); 122 errstr = errstr == NULL ? "unknown" : errstr; 123 printf("Startup errorr in %s:\nLUA ERROR: %s.\n", filename, errstr); 124 lua_pop(luap, 1); 125 } 126 } 127 128 int 129 interp_run(const char *line) 130 { 131 int argc; 132 char **argv; 133 lua_State *luap; 134 struct interp_lua_softc *softc = &lua_softc; 135 int status; 136 137 luap = softc->luap; 138 LDBG("executing line..."); 139 if ((status = luaL_dostring(luap, line)) != 0) { 140 /* 141 * If we could not parse the line as Lua syntax, 142 * try parsing it as a loader command. 143 */ 144 lua_pop(luap, 1); 145 if (parse(&argc, &argv, line) == 0) { 146 status = interp_builtin_cmd(argc, argv); 147 if (status != CMD_OK) 148 printf("Command failed\n"); 149 free(argv); 150 } else { 151 printf("Failed to parse \'%s\'\n", line); 152 status = -1; 153 } 154 } 155 156 return (status == 0 ? CMD_OK : CMD_ERROR); 157 } 158 159 int 160 interp_include(const char *filename) 161 { 162 struct interp_lua_softc *softc = &lua_softc; 163 164 LDBG("loading file %s", filename); 165 166 return (luaL_dofile(softc->luap, filename)); 167 } 168