Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 26 May 2014 13:37:34 GMT
From:      pedrosouza@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r268635 - soc2014/pedrosouza/lua_loader/head/sys/boot/common
Message-ID:  <201405261337.s4QDbYL7079776@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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

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 <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 "../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;
+}
+
+int lua_perform(lua_State *L)
+{
+
+    int argc, ret;
+	char **argv;
+    int res = -1;
+    int n = lua_gettop(L);
+
+    if (n >= 1)
+    {
+	    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;
+}
+
+
+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 ((strcmp(filename, "/boot/loader.rc") == 0) ||
+	    (strcmp(filename, "/boot/boot.conf") == 0)) {
+		printf("Skipping loader.rc and boot.conf");
+		return (0);
+	}
+    */
+	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,
+};



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201405261337.s4QDbYL7079776>