From 02f489ae1688fb02fabb69c6ae772b4116d9d192 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 17 Aug 2012 16:07:01 +0200 Subject: [PATCH] Rewrite userdefaultoptions() to give it a chance of working, fixes #1015 Signed-off-by: Tobias Ulmer --- generic/e2option.lua | 70 +++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/generic/e2option.lua b/generic/e2option.lua index d6ea094..2426a00 100644 --- a/generic/e2option.lua +++ b/generic/e2option.lua @@ -254,40 +254,76 @@ function e2option.parse(args) category) end - local function userdefaultoptions() + local function userdefaultoptions(opts) local home = e2lib.globals.homedir - if not home then return end + + if not home then + return + end + local file = home .. "/.e2/e2rc" if not e2util.exists(file) then return end + local e2rc = {} - local rc, e = e2lib.dofile_protected(file, - { e2rc = function(t) e2rc = t end }) + local rc, e = e2lib.dofile_protected(file, { e2rc = function(t) e2rc = t end }) if not rc then e2lib.abort(e) end - for _,p in pairs(e2rc) do - local n=p[1] - local v=p[2] - if options[n] then - if options[n].type == "flag" and v then - e2lib.abort("argument given for flag: " .. n) - elseif options[n].type == "option" and not v then - e2lib.abort("argument missing for option: " .. n) + + for _,tbl in pairs(e2rc) do + if type(tbl) ~= "table" then + e2lib.abort(string.format("could not parse user defaults.\n".. + "'%s' is not in the expected format.", file)) + end + + local opt=tbl[1] + local val=tbl[2] + + if type(opt) ~= "string" or string.len(opt) == 0 then + e2lib.abort(string.format("could not parse user defaults.\n".. + "'%s' has a malformed option", file)) + end + + opt = aliases[opt] or opt + + if not options[opt] then + e2lib.abort("unknown option in user defaults: " .. opt) + end + + if options[opt].type == "flag" and val then + e2lib.abort(string.format( + "user default option '%s' does not take an argument ", + opt)) + elseif options[opt].type == "option" and not val then + e2lib.abort( + "argument missing for user default option: " .. opt) + end + + if options[opt].proc then + if options[opt].type == "flag" then + opts[opt] = options[opt].proc() + else + opts[opt] = options[opt].proc(val) end - local proc = options[n].proc - proc(v) + elseif options[opt].default then + opts[opt] = options[opt].default else - e2lib.abort("unknown option in user defaults: " .. n) + e2lib.bomb("user default option has no effect") end end end defaultoptions() - userdefaultoptions() + local opts = {} local vals = {} - local opts={ arguments=vals } + -- arguments is a special option containing all additional arguments that + -- are not parsed. XXX: Remove this if possible. + opts["arguments"] = vals + + userdefaultoptions(opts) + local i = 1 while i <= #args do -- we may modify args local v = args[i] -- 2.39.5