Date: Wed, 25 Mar 2015 06:05:44 +0000 (UTC) From: Rui Paulo <rpaulo@FreeBSD.org> To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r280487 - projects/lua-bootloader/sys/boot/common Message-ID: <201503250605.t2P65ixE057558@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: rpaulo Date: Wed Mar 25 06:05:43 2015 New Revision: 280487 URL: https://svnweb.freebsd.org/changeset/base/280487 Log: Add Pedro Souza's interp_lua.c. Added: projects/lua-bootloader/sys/boot/common/interp_lua.c (contents, props changed) Modified: projects/lua-bootloader/sys/boot/common/Makefile.inc Modified: projects/lua-bootloader/sys/boot/common/Makefile.inc ============================================================================== --- projects/lua-bootloader/sys/boot/common/Makefile.inc Wed Mar 25 06:04:39 2015 (r280486) +++ projects/lua-bootloader/sys/boot/common/Makefile.inc Wed Mar 25 06:05:43 2015 (r280487) @@ -68,6 +68,11 @@ MAN+= ../forth/menusets.4th.8 MAN+= ../forth/version.4th.8 .endif +# Lua interpreter +.if defined(BOOT_LUA) +SRCS+= interp_lua.c +.endif + .if defined(BOOT_PROMPT_123) CFLAGS+= -DBOOT_PROMPT_123 .endif Added: projects/lua-bootloader/sys/boot/common/interp_lua.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/lua-bootloader/sys/boot/common/interp_lua.c Wed Mar 25 06:05:43 2015 (r280487) @@ -0,0 +1,123 @@ +/*- + * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> /* to pick up __FreeBSD_version */ +#include <machine/stdarg.h> + +#include "bootstrap.h" +#include "interp.h" + +#define lua_c + +#include <src/lua.h> +#include <src/ldebug.h> +#include <src/lauxlib.h> +#include <src/lualib.h> +#include <lutils.h> + + +struct interp_lua_softc { + lua_State *luap; +}; +static struct interp_lua_softc lua_softc = { 0 }; + +#define LDBG(...) do { \ + printf("%s(%d): ", __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ +} while (0) + + +void +interp_lua_init(void *ctx) +{ + lua_State *luap; + struct bootblk_command **cmdp; + struct interp_lua_softc *softc; + struct env_var *ev; + const char *name_str, *val_str; + char buf[16]; + + softc = ctx; + luap = lua_create(); + if (luap == NULL) { + LDBG("problem with initializing Lua interpreter\n"); + } + softc->luap = luap; + register_utils(luap); + luaopen_base(luap); + luaL_requiref(luap, LUA_STRLIBNAME, luaopen_string, 1); + lua_pop(luap, 1); +} + +int +interp_lua_run(void *data, const char *line) +{ + lua_State *luap; + struct interp_lua_softc *softc; + int argc, ret; + char **argv; + + softc = data; + luap = softc->luap; + + if (ldo_string(luap, line, strlen(line)) != 0) + printf("[LUA]Failed to execure \'%s\'\n", line); + + return (0); +} + +int +interp_lua_incl(void *ctx, const char *filename) +{ + struct interp_lua_softc *softc; + + softc = ctx; + + return (ldo_file(softc->luap, filename)); +} + +/* + * To avoid conflicts lua uses loader.lua instead of + * loader.rc/boot.conf to load its configurations. + */ +int +interp_lua_load_config(void *ctx) +{ + return interp_lua_incl(ctx, "/boot/loader.lua"); +} + + +struct interp boot_interp_lua = { + .init = interp_lua_init, + .run = interp_lua_run, + .incl = interp_lua_incl, + .load_configs = interp_lua_load_config, + .context = &lua_softc +};
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201503250605.t2P65ixE057558>