From owner-svn-soc-all@FreeBSD.ORG Mon Jun 2 22:07:45 2014 Return-Path: Delivered-To: svn-soc-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 3B2DC5DF; Mon, 2 Jun 2014 22:07:45 +0000 (UTC) Received: from freebsd.czest.pl (freebsd.czest.pl [212.87.224.105]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 85FE229F3; Mon, 2 Jun 2014 22:07:44 +0000 (UTC) Received-SPF: pass (freebsd.czest.pl: domain of wkoszek@freebsd.czest.pl designates 212.87.224.105 as permitted sender) receiver=freebsd.czest.pl; client-ip=212.87.224.105; helo=freebsd.czest.pl; envelope-from=wkoszek@freebsd.czest.pl; x-software=spfmilter 0.97 http://www.acme.com/software/spfmilter/ with libspf-unknown; Received: from freebsd.czest.pl (freebsd.czest.pl [212.87.224.105]) by freebsd.czest.pl (8.14.5/8.14.5) with ESMTP id s52M3jWu005022; Mon, 2 Jun 2014 22:03:45 GMT (envelope-from wkoszek@freebsd.czest.pl) Received: (from wkoszek@localhost) by freebsd.czest.pl (8.14.5/8.14.5/Submit) id s52M3jHs005021; Mon, 2 Jun 2014 22:03:45 GMT (envelope-from wkoszek) Date: Mon, 2 Jun 2014 22:03:45 +0000 From: "Wojciech A. Koszek" To: pedrosouza@freebsd.org Subject: Re: socsvn commit: r268635 - soc2014/pedrosouza/lua_loader/head/sys/boot/common Message-ID: <20140602220344.GB2587@FreeBSD.org> References: <201405261337.s4QDbYL7079776@socsvn.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <201405261337.s4QDbYL7079776@socsvn.freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Status: No, score=-0.4 required=5.0 tests=RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on freebsd.czest.pl X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.7 (freebsd.czest.pl [212.87.224.105]); Mon, 02 Jun 2014 22:03:49 +0000 (UTC) Cc: svn-soc-all@freebsd.org X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Jun 2014 22:07:45 -0000 On Mon, May 26, 2014 at 01:37:34PM +0000, pedrosouza@freebsd.org wrote: > Author: pedrosouza > Date: Mon May 26 13:37:34 2014 > New Revision: 268635 > URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=268635 > > Log: > Added missing file intep_lua.c Pedro, My comments below. > > Added: > soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c > > Added: soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c > ============================================================================== > --- /dev/null 00:00:00 1970 (empty, because file is newly added) > +++ soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c Mon May 26 13:37:34 2014 (r268635) > @@ -0,0 +1,203 @@ > +#include Please put our copyright in here. > +__FBSDID("$FreeBSD$"); > + > +#include /* to pick up __FreeBSD_version */ > +#include > + > + > +#include "bootstrap.h" > +#include "interp.h" > + > +#define lua_c > + > +#include "../lua/src/lua.h" > +#include "../lua/src/ldebug.h" > + > +struct interp_lua_softc { > + lua_State *luap; > +}; > +struct interp_lua_softc lua_softc = { 0 }; > + > +#define LDBG(...) do { \ > + printf("%s(%d): ", __func__, __LINE__); \ > + printf(__VA_ARGS__); \ > + printf("\n"); \ > +} while (0) > + > + > +int lua_print(lua_State *L) > +{ > + int i; > + int n = lua_gettop(L); > + for (i = 1; i <= n; ++i) > + printf("%s", lua_tostring(L, i)); > + return 0; > +} This comment touches the whole file: something is wrong with formatting. We'd appreciate if you could look at it a bit more and maybe align the formatting accross the file. > + > +int lua_perform(lua_State *L) > +{ > + > + int argc, ret; > + char **argv; ..like here.. > + int res = -1; > + int n = lua_gettop(L); > + > + if (n >= 1) > + { ..or here.. > + parse(&argc, &argv, lua_tostring(L, 1)); > + res = perform(argc, argv); > + } > + lua_pushnumber(L, res); > + > + return 1; > +} > + > +void * lua_realloc(void *ud, void *ptr, size_t osize, size_t nsize) > +{ > + (void)ud; (void)osize; /* not used */ > + if (nsize == 0) { > + free(ptr); > + return NULL; > + } > + else > + return realloc(ptr, nsize); > +} > + > +typedef struct data_chunk > +{ > + void * data; > + size_t size; > +} data_chunk; > + > +const char * read_chunk(lua_State *L, void * chunk, size_t *sz) > +{ > + data_chunk * ds = (data_chunk *)chunk; > + if (ds->size == 0) return NULL; > + *sz = ds->size; > + ds->size = 0; > + return (const char*)ds->data; > +} I don't seem to recall my code had these functions. Can you quickly summarize what they do? > + > + > +int do_string(lua_State *L, const char * str, size_t size) > +{ > + int res; > + data_chunk ds; > + ds.data = (void*)str; > + ds.size = size; > + res = lua_load(L, read_chunk, &ds, "do_string___", 0); > + res = lua_pcall(L, 0, LUA_MULTRET, 0); > + return res; > +} > + > + > +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_newstate(lua_realloc, NULL); > + if (luap == NULL) { > + LDBG("problem with initializing Lua interpreter\n"); > + } > + softc->luap = luap; > + lua_register(luap, "print", lua_print); > + lua_register(luap, "perform", lua_perform); > + > +} > + > +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 (do_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) > +{ > + lua_State *luap; > + struct interp_lua_softc *softc; > + struct stat st; > + char *filebuf, *errstr; > + int fd, filebufoff, i, rleft, rread; > + > + printf("[Lua] Including file %s.\n", filename); > + /* If you could look into using #ifdef for commenting out, this would be good. > + if ((strcmp(filename, "/boot/loader.rc") == 0) || > + (strcmp(filename, "/boot/boot.conf") == 0)) { > + printf("Skipping loader.rc and boot.conf"); > + return (0); > + } There was a small glitch here: I think the current implementation is that if there's no Forth interpreter, the rc/conf (which are in Forth) will get passed to the loader as well. I'd keep it that way in case somebody takes advantage of this bug, but we could comment this part out. > + */ > + softc = ctx; > + luap = softc->luap; > + /* > + > + fd = open(filename, O_RDONLY); > + if (fd < 0) { > + printf("Couldn't open file %s\n", filename); > + return (1); > + } > + i = stat(filename, &st); > + assert(i == 0); > + filebuf = malloc(st.st_size); > + assert(filebuf != NULL); > + memset(filebuf, 0, st.st_size); > + */ > + /* > + * XX: Investigate stat() vs logic problem. I'm getting > + * more bytes that the file really has. > + */ > + > + /* > + rleft = st.st_size - 1; > + filebufoff = 0; > + for (;;) { > + rread = read(fd, filebuf + filebufoff, rleft); > + assert(rread >= 0); > + rleft -= rread; > + filebufoff += rread; > + if (rread == 0 || rleft <= 0) { > + break; > + } > + } > + close(fd); > + i = LUA_DOSTRING(luap, filebuf); > + free(filebuf); > + if ((i != 0) && (lua_isnil(luap, -1) == 0)) { > + errstr = lua_tostring(luap, -1); > + if (errstr == NULL) { > + errstr = "internal error; errstr must be string"; > + } > + printf("Problem with script execution:\n\n"); > + printf("\t'%s'\n\n", errstr); > + lua_pop(luap, 1); > + } > + */ > + return (0); > +} > + > + > +struct interp boot_interp_lua = { > + .init = interp_lua_init, > + .run = interp_lua_run, > + .incl = interp_lua_incl, > + .context = &lua_softc, > +}; Other than that it looks good. -- Wojciech A. Koszek wkoszek@FreeBSD.czest.pl http://FreeBSD.czest.pl/~wkoszek/