From owner-svn-src-projects@FreeBSD.ORG Wed Mar 25 06:05:45 2015 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 49DFEE4D; Wed, 25 Mar 2015 06:05:45 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 34BEA932; Wed, 25 Mar 2015 06:05:45 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t2P65iak057560; Wed, 25 Mar 2015 06:05:44 GMT (envelope-from rpaulo@FreeBSD.org) Received: (from rpaulo@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t2P65ixE057558; Wed, 25 Mar 2015 06:05:44 GMT (envelope-from rpaulo@FreeBSD.org) Message-Id: <201503250605.t2P65ixE057558@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: rpaulo set sender to rpaulo@FreeBSD.org using -f From: Rui Paulo Date: Wed, 25 Mar 2015 06:05:44 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r280487 - projects/lua-bootloader/sys/boot/common X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Mar 2015 06:05:45 -0000 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 + * 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 +__FBSDID("$FreeBSD$"); + +#include /* to pick up __FreeBSD_version */ +#include + +#include "bootstrap.h" +#include "interp.h" + +#define lua_c + +#include +#include +#include +#include +#include + + +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 +};