]> git.e2factory.org Git - e2factory.git/commitdiff
Merge dofile_protected() into dofile2()
authorTobias Ulmer <tu@emlix.com>
Fri, 3 May 2013 11:07:03 +0000 (13:07 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 13:58:55 +0000 (14:58 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
generic/e2option.lua
local/e2tool.lua

index f2b7e011a7ea3175f2d1c8bf768f78ca11bfc41e..99e32251702c66d5a93b054af8f7c56ece469f8d 100644 (file)
@@ -867,7 +867,7 @@ function e2lib.read_global_config(e2_config_file)
         local rc = e2util.exists(path)
         if rc then
             e2lib.logf(3, "using global config file: %s", path)
-            rc, re = e2lib.dofile_protected(path, c, true)
+            rc, re = e2lib.dofile2(path, c, true)
             if not rc then
                 return false, re
             end
@@ -924,7 +924,7 @@ function e2lib.read_extension_config()
     c.extensions = function(x)
         c.data = x
     end
-    local rc, re = e2lib.dofile_protected(e2lib.globals.extension_config, c, true)
+    local rc, re = e2lib.dofile2(e2lib.globals.extension_config, c, true)
     if not rc then
         return false, e:cat(re)
     end
@@ -1173,57 +1173,37 @@ function e2lib.callcmd_log(cmd, loglevel)
     return rc, e
 end
 
---- Protected execution of Lua code.
--- Runs the code in the Lua file at path with a restricted global environment.
--- gtable contains a table with the initial global environment. If allownewdefs 
--- is given and true, then the code may define new global variables.
--- This function aborts on error.
--- XXX: This looks like a more restricted version of dofile2 with problematic
--- error handling. Merge the two and fix usage.
--- @param path Filename to load lua code from (string).
--- @param gtable Environment (table) that is used instead of the global _G.
--- @param allownewdefs Allow adding new definitions to gtable (boolean).
--- @return True on success.
--- @see dofile2
-function e2lib.dofile_protected(path, gtable, allownewdefs)
+--- Executes Lua code loaded from path.
+--@param path Filename to load lua code from (string).
+--@param gtable Environment (table) that is used instead of the global _G.
+--@param allownewdefs Boolean indicating whether new variables may be defined
+--                    and undefined ones read.
+--@return True on success, false on error.
+--@return Error object on failure.
+function e2lib.dofile2(path, gtable, allownewdefs)
+    local e = err.new("error loading config file: %s", path)
     local chunk, msg = loadfile(path)
     if not chunk then
-        return false, msg
+        return false, e:cat(msg)
     end
-    local t = gtable
-    -- t._G = t
+
     local function checkread(t, k)
         local x = rawget(t, k)
-        if x then return x
-        else e2lib.abort(path, ": attempt to reference undefined global variable '",
-            k, "'")
+        if x then
+            return x
+        else
+            e2lib.abort(path, ": attempt to reference undefined global variable '", k, "'")
         end
     end
+
     local function checkwrite(t, k, v)
         e2lib.abort(path, ": attempt to set new global variable `", k, "' to ", v)
     end
+
     if not allownewdefs then
-        setmetatable(t, { __newindex = checkwrite, __index = checkread })
-    end
-    setfenv(chunk, t)
-    local s, msg = pcall(chunk)
-    if not s then
-        e2lib.abort(msg)
+        setmetatable(gtable, { __newindex = checkwrite, __index = checkread })
     end
-    return true, nil
-end
 
---- Executes Lua code loaded from path.
---@param path Filename to load lua code from (string).
---@param gtable Environment (table) that is used instead of the global _G.
---@return True on success, false on error.
---@return Error object on failure.
-function e2lib.dofile2(path, gtable)
-    local e = err.new("error loading config file: %s", path)
-    local chunk, msg = loadfile(path)
-    if not chunk then
-        return false, e:cat(msg)
-    end
     setfenv(chunk, gtable)
     local s, msg = pcall(chunk)
     if not s then
index c482f19fa9541d9b585d35e7497ca05e15b2c80e..c04dcbc31401ff09e5d1ec2e023a96fd7723e1db 100644 (file)
@@ -229,7 +229,7 @@ local function userdefaultoptions(opts)
     end
 
     local e2rc = {}
-    local rc, e = e2lib.dofile_protected(file, { e2rc = function(t) e2rc = t end })
+    local rc, e = e2lib.dofile2(file, { e2rc = function(t) e2rc = t end }, false)
     if not rc then
         return false, e
     end
index 58024e7a4934177724cf19d989ceabe92bdd4b7d..764de1c838c2fcef72a829e5263d51f89a834089 100644 (file)
@@ -284,7 +284,7 @@ local function load_user_config(info, path, dest, index, var)
     local function func(table)
         dest[index] = table
     end
-    local rc, re = e2lib.dofile_protected(path, { [var] = func, env = info.env, string=string })
+    local rc, re = e2lib.dofile2(path, { [var] = func, env = info.env, string=string }, false)
     if not rc then
         return false, e:cat(re)
     end
@@ -357,7 +357,7 @@ local function load_user_config2(info, path, types)
         g[type] = f[type]                      -- and some config functions
     end
 
-    rc, re = e2lib.dofile2(path, g)
+    rc, re = e2lib.dofile2(path, g, true)
     if not rc then
         return nil, e:cat(re)
     end
@@ -836,7 +836,7 @@ local function load_env_config(info, file)
     g.e2env = info.env                    -- env as built up so far
     g.string = string                     -- string
     g.env = mergeenv
-    rc, re = e2lib.dofile2(path, g)
+    rc, re = e2lib.dofile2(path, g, true)
     if not rc then
         return false, e:cat(re)
     end