socsvn commit: r270552 - soc2014/pedrosouza/lua_loader/head/sys/boot/lua

pedrosouza at FreeBSD.org pedrosouza at FreeBSD.org
Mon Jul 7 17:04:52 UTC 2014


Author: pedrosouza
Date: Mon Jul  7 17:04:51 2014
New Revision: 270552
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=270552

Log:
  Added file io & .conf file parse

Added:
  soc2014/pedrosouza/lua_loader/head/sys/boot/lua/config.lua
Modified:
  soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c

Added: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/config.lua
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/config.lua	Mon Jul  7 17:04:51 2014	(r270552)
@@ -0,0 +1,63 @@
+config = {};
+
+pattern_table = {
+    [1] = {str = "(%w+)_load%s*=%s*\"([%w%s%p]*)%s*(.*)\"", process = function(k, v) print("Processed(load) -> "..k..":"..v); end },       --module_load="value"
+    [2] = {str = "(%w+)_name%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(name) -> "..k..":"..v); end },       --module_name="value"
+    [3] = {str = "(%w+)_type%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(type) -> "..k..":"..v); end },       --module_type="value"
+    [4] = {str = "(%w+)_flags%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(flags) -> "..k..":"..v); end },      --module_flags="value"
+    [5] = {str = "(%w+)_before%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(before) -> "..k..":"..v); end }      --module_before="value"
+    [6] = {str = "(%w+)_after%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(after) -> "..k..":"..v); end },      --module_after="value"
+    [7] = {str = "(%w+)_error%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(error) -> "..k..":"..v); end }       --module_error="value"
+    [8] = {str = "(%w+)%s*=%s*\"([%w%s%p]*)\"%s*(.*)", process = function(k, v) print("Processed(environment) -> "..k..":"..v); end }             --env_var="value"
+};
+
+function config.isValidComment(c)
+    local s = string.match(c, "%s*#.*");
+    if s == nil then return false; end
+    return true;
+end
+
+function config.load(name)
+    local f = io.open(name);
+    if f == nil then
+        print("Failed to open config : " .. name);
+        return false;
+    end
+    
+    local text;
+    local r;
+    
+    text, r = io.read(f);
+    
+    if text == nil then
+        print("Failed to read confif : " .. name);
+        return false;
+    end
+    
+    local n = 1;
+    
+    for line in string.gmatch(text, "([^\n]+)") do
+    
+        local found = false;
+        
+        for i, v in ipairs(pattern_table) do
+            local k, v, c = string.match(line, v.str);
+            if k ~= nil then
+                found = true;
+                
+                if config.isValidComment(c) then
+                    v.process(k, v);
+                else
+                    print("Malformed line ("..n.."):"..line);
+                end
+                
+                break;
+            end
+        end
+        
+        if found == false then
+            print("Malformed line ("..n.."):"..line);
+        end
+        
+    end
+end
\ No newline at end of file

Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c
==============================================================================
--- soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c	Mon Jul  7 16:55:07 2014	(r270551)
+++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c	Mon Jul  7 17:04:51 2014	(r270552)
@@ -66,45 +66,6 @@
 }
 
 int
-lua_strchar(lua_State *L)
-{
-	const char	*str;
-	int		i, j;
-	int		n = lua_gettop(L);
-
-	if (n != 2) return 0;
-	str = lua_tostring(L, 1);
-	if (str == NULL) return 0;
-	i = strlen(str);
-	j = (int)lua_tonumber(L, 2);
-	if ( j >= 0 && j < i)
-	{
-		lua_pushnumber(L, str[j]);
-		return 1;
-	}
-	return 0;
-}
-
-int
-lua_charstr(lua_State *L)
-{
-	int	n = lua_gettop(L);
-	char	buf[129];
-
-	if (n < 1) return 0;
-	n = n > 128 ? 128 : n;
-
-	buf[n] = 0;
-	while (n-- > 0)
-	{
-		buf[n] = ((int)lua_tonumber(L, n+1)) & 0xFF;
-	}
-
-	lua_pushstring(L, buf);
-	return 1;
-}
-
-int
 lua_delay(lua_State *L)
 {
 	int	n = lua_gettop(L);
@@ -116,7 +77,8 @@
 	return 0;
 }
 
-int lua_getenv(lua_State *L)
+int
+lua_getenv(lua_State *L)
 {
 	char	*ev;
 	int	n = lua_gettop(L);
@@ -171,7 +133,7 @@
 
 	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;
 }
@@ -226,7 +188,8 @@
 	return 0;
 }
 
-int lua_include(lua_State *L)
+int
+lua_include(lua_State *L)
 {
 	const char	*str;
 
@@ -240,6 +203,91 @@
 	return 1;
 }
 
+int
+lua_openfile(lua_State *L)
+{
+	const char	*str;
+	int		fd;
+	int		r;
+	struct stat	st;
+
+	if (lua_gettop(L) != 1)
+	{
+		lua_pushnil(L);
+		return 1;
+	}
+	str = lua_tostring(L, 1);
+
+	FILE * f = fopen(str, "r");
+	if (f != NULL)
+	{
+		FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
+		*ptr = f;
+	} else
+		lua_pushnil(L);
+	return 1;
+}
+
+int
+lua_closefile(lua_State *L)
+{
+	FILE ** f;
+	if (lua_gettop(L) != 1)
+	{
+		lua_pushboolean(L, 0);
+		return 1;
+	}
+	
+	f = (FILE**)lua_touserdata(L, 1);
+	if (f != NULL && *f != NULL)
+	{
+		lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
+		*f = NULL;
+	} else
+		lua_pushboolean(L, 0);
+
+	return 1;
+}
+
+int
+lua_readfile(lua_State *L)
+{
+	FILE	**f;
+	size_t	size, r;
+	char * buf;
+
+	if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
+	{
+		lua_pushnil(L);
+		lua_pushnumber(L, 0);
+		return 2;
+	}
+	
+	f = (FILE**)lua_touserdata(L, 1);
+
+	if (f == NULL || *f == NULL)
+	{
+		lua_pushnil(L);
+		lua_pushnumber(L, 0);
+		return 2;
+	}
+
+	if (lua_gettop(L) == 2)
+	{
+		size = (size_t)lua_tonumber(L, 2);
+	} else
+		size = (*f)->size;
+
+
+	buf = (char*)malloc(size);
+	r = fread(buf, 1, size, *f);
+	lua_pushlstring(L, buf, r);
+	free(buf);
+	lua_pushnumber(L, r);
+
+	return 2;
+}
+
 void 
 lregister(lua_State *L, const char *tableName, const char *funcName, int (*funcPointer)(lua_State *))
 {
@@ -270,10 +318,11 @@
 			{lua_delay, "loader", "delay"},
 			{lua_include, "loader", "include"},
 			{lua_getenv, "loader", "getenv"},
-			{lua_strchar, "string", "byte"},
-			{lua_charstr, "string", "char"},
 			{lua_getchar, "io", "getchar"},
 			{lua_gets, "io", "gets"},
+			{lua_openfile, "io", "open"},
+			{lua_closefile, "io", "close"},
+			{lua_readfile, "io", "read"},
 			{NULL, NULL, NULL},
 			};
 
@@ -294,4 +343,4 @@
 		}
 		++f;
 	}
-}
\ No newline at end of file
+}


More information about the svn-soc-all mailing list