Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Jun 2014 18:01:25 GMT
From:      pedrosouza@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r269632 - in soc2014/pedrosouza/lua_loader/head/sys/boot: common lua
Message-ID:  <201406161801.s5GI1PYA045218@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pedrosouza
Date: Mon Jun 16 18:01:24 2014
New Revision: 269632
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=269632

Log:
  Added lua_include function

Modified:
  soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c
  soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c
  soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h

Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c
==============================================================================
--- soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c	Mon Jun 16 17:58:03 2014	(r269631)
+++ soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c	Mon Jun 16 18:01:24 2014	(r269632)
@@ -85,7 +85,7 @@
 	softc = data;
 	luap = softc->luap;
 
-	if (do_string(luap, line, strlen(line)) != 0)
+	if (ldo_string(luap, line, strlen(line)) != 0)
 		printf("[LUA]Failed to execure \'%s\'\n", line);
 
 	return (0);
@@ -95,53 +95,10 @@
 interp_lua_incl(void *ctx, const char *filename)
 {
 	struct interp_lua_softc *softc;
-	struct stat		st;
-	int			fd, r;
-	char			*buf;
-	const char		*errstr;
 
 	softc = ctx;
 
-	fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		printf("Failed to open file %s\n", filename);
-		return 1;
-	}
-
-	r = fstat(fd, &st);
-
-	if (r != 0) {
-		printf("Failed to retrieve file stat!\n");
-		close(fd);
-		return 1;
-	}
-
-	buf = malloc(st.st_size);
-	if (buf == NULL) {
-		printf("Failed to alloc buf!\n");
-		close(fd);
-		return 1;
-	}
-
-	r = read(fd, buf, st.st_size);
-	if (r != st.st_size) {
-		printf("Failed to read file (%d/%d)!\n", r, (unsigned int)st.st_size);
-		free(buf);
-		close(fd);
-		return 1;
-	}
-
-	if (do_string(softc->luap, buf, st.st_size) != 0) {
-		errstr = lua_tostring(softc->luap, -1);
-		errstr = errstr == NULL ? "unknown" : errstr;
-		printf("Failed to run %s file with error: %s.\n", filename, errstr);
-		lua_pop(softc->luap, 1);
-	}
-
-	free(buf);
-	close(fd);
-
-	return 0;
+	return ldo_file(softc->luap, filename);
 }
 
 

Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c
==============================================================================
--- soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c	Mon Jun 16 17:58:03 2014	(r269631)
+++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c	Mon Jun 16 18:01:24 2014	(r269632)
@@ -154,18 +154,79 @@
 
 
 int
-do_string(lua_State *L, const char * str, size_t size)
+ldo_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_load(L, read_chunk, &ds, "do_string__", 0);
 	res = lua_pcall(L, 0, LUA_MULTRET, 0);
 	return res;
 }
 
+int
+ldo_file(lua_State *L, const char *filename)
+{
+	struct stat		st;
+	int			fd, r;
+	char			*buf;
+	const char		*errstr;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		printf("Failed to open file %s\n", filename);
+		return 1;
+	}
 
+	r = fstat(fd, &st);
+
+	if (r != 0) {
+		printf("Failed to retrieve file stat!\n");
+		close(fd);
+		return 1;
+	}
+
+	buf = malloc(st.st_size);
+	if (buf == NULL) {
+		printf("Failed to alloc buf!\n");
+		close(fd);
+		return 1;
+	}
+
+	r = read(fd, buf, st.st_size);
+	if (r != st.st_size) {
+		printf("Failed to read file (%d/%d)!\n", r, (unsigned int)st.st_size);
+		free(buf);
+		close(fd);
+		return 1;
+	}
+
+	if (ldo_string(L, buf, st.st_size) != 0) {
+		errstr = lua_tostring(L, -1);
+		errstr = errstr == NULL ? "unknown" : errstr;
+		printf("Failed to run %s file with error: %s.\n", filename, errstr);
+		lua_pop(L, 1);
+	}
+
+	free(buf);
+	close(fd);
+
+	return 0;
+}
+
+int lua_include(lua_State * L)
+{
+	const char * str;
+	if (lua_gettop(L) != 1)
+	{
+		lua_pushboolean(L, 0);
+		return 1;
+	}
+	str = lua_tostring(L, 1);
+	lua_pushboolean(L, (ldo_file(L, str) == 0));
+	return 1;
+}
 
 void 
 lregister(lua_State * L, const char * tableName, const char * funcName, int (*funcPointer)(lua_State *))
@@ -195,6 +256,7 @@
 utils_func reg_funcs[] = { {lua_print, NULL, "print"},
 			{lua_perform, "loader", "perform"},
 			{lua_delay, "loader", "delay"},
+			{lua_include, "loader", "include"},
 			{lua_strchar, "string", "byte"},
 			{lua_charstr, "string", "char"},
 			{lua_getchar, "io", "getchar"},
@@ -218,4 +280,4 @@
 		}
 		++f;
 	}
-}
\ No newline at end of file
+}

Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h
==============================================================================
--- soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h	Mon Jun 16 17:58:03 2014	(r269631)
+++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h	Mon Jun 16 18:01:24 2014	(r269632)
@@ -36,7 +36,9 @@
 
 void * lua_realloc(void *ud, void *ptr, size_t osize, size_t nsize);
 
-int do_string(lua_State *L, const char * str, size_t size);
+int ldo_string(lua_State *L, const char * str, size_t size);
+
+int ldo_file(lua_State *L, const char * filename);
 
 void lregister(const char * tname, const char * fname, int (*fptr)(lua_State *));
 



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