From eb47c611c83fe618b279ba1d6e30c14f6465db03 Mon Sep 17 00:00:00 2001 From: Matthew Cawood Date: Tue, 10 Dec 2024 16:36:11 -0600 Subject: [PATCH] mode select working --- rt/unload/mf/Core/B/2.0.lua | 2 +- rt/unload/mf/Core/C/1.0.lua | 2 +- src/MName.lua | 6 +-- src/MainControl.lua | 41 ++++++++++++----- src/modfuncs.lua | 89 ++++++++++++++----------------------- 5 files changed, 69 insertions(+), 71 deletions(-) diff --git a/rt/unload/mf/Core/B/2.0.lua b/rt/unload/mf/Core/B/2.0.lua index f8a8ef833..257e384c7 100644 --- a/rt/unload/mf/Core/B/2.0.lua +++ b/rt/unload/mf/Core/B/2.0.lua @@ -1,5 +1,5 @@ setenv("TOTO","set_in_B/2.0") if (mode() == "unload") then - LmodError("Error in unload of B/2.0") + LmodError("Sucessfully raised mode error in unload of B/2.0") end diff --git a/rt/unload/mf/Core/C/1.0.lua b/rt/unload/mf/Core/C/1.0.lua index f4505d01f..c22a6108d 100644 --- a/rt/unload/mf/Core/C/1.0.lua +++ b/rt/unload/mf/Core/C/1.0.lua @@ -11,7 +11,7 @@ append_path{"PATH_TEST", "/last", mode={"load"}} append_path{"PATH_TEST", "/unload_last", mode={"unload"}} -- Test remove during specific modes -remove_path{"PATH_TEST", "/to_remove", mode={"unload"}} +--remove_path{"PATH_TEST", "/to_remove", mode={"unload"}} --setenv{"AA", "X", mode={"load", "unload"}} diff --git a/src/MName.lua b/src/MName.lua index 732b5a3a8..4f7717eed 100644 --- a/src/MName.lua +++ b/src/MName.lua @@ -66,7 +66,7 @@ local s_rangeFuncT = { ["<="] = {func = l_lessthan_equal, name = "<="}, } -function M.new(self, sType, name, action, is, ie, mode) +function M.new(self, sType, name, action, is, ie) --dbg.start{"Mname:new(",sType,")"} local exact_match = cosmic:value("LMOD_EXACT_MATCH") @@ -143,8 +143,6 @@ function M.new(self, sType, name, action, is, ie, mode) o.__userName = (name or ""):trim():gsub("/+$",""):gsub("%.lua$","") end - o.__mode = mode - --dbg.fini("MName:new") return o end @@ -168,6 +166,8 @@ end -- @param sType The type which can be "entryT", "load", "mt" -- @return An array of MName objects. function M.buildA(self,sType, ...) + + local argA = pack(...) local a = {} diff --git a/src/MainControl.lua b/src/MainControl.lua index bde7aa4e7..5eaffdc4e 100644 --- a/src/MainControl.lua +++ b/src/MainControl.lua @@ -312,10 +312,22 @@ end -- @param name the environment variable name. -- @param value the environment variable value. -- @param respect If true, then respect the old value. -function M.setenv(self, table) --name, value, respect) - local name = (table[1] or ""):trim() - local value = table[2] - local respect = table[3] or nil +function M.setenv(self, ...) --name, value, respect) + local name, value, respect + + local firstArg = select(1, ...) + if type(firstArg) == "table" then + -- Format 2: arguments were passed as a table + local t = firstArg + name = (t[1] or ""):trim() + value = t[2] + respect = t[3] or nil + else + -- Format 1: arguments were passed as individual parameters + name, value, respect = ... + name = (name or ""):trim() + end + dbg.start{"MainControl:setenv(\"",name,"\", \"",value,"\", \"", respect,"\")"} @@ -340,7 +352,6 @@ function M.setenv(self, table) --name, value, respect) dbg.fini("MainControl:setenv") end - ------------------------------------------------------------------- -- Set an environment variable. -- This function just sets the name with value in the current env. @@ -361,11 +372,21 @@ end -- @param name the environment variable name. -- @param value the environment variable value. -- @param respect If true, then respect the old value. -function M.unsetenv(self, table) --name, value, respect) - - local name = (table[1] or ""):trim() - local value = table[2] - local respect = table[3] or nil +function M.unsetenv(self, ...) --name, value, respect) + local name, value, respect + + local firstArg = select(1, ...) + if type(firstArg) == "table" then + -- Format 2: arguments were passed as a table + local t = firstArg + name = (t[1] or ""):trim() + value = t[2] + respect = t[3] or nil + else + -- Format 1: arguments were passed as individual parameters + name, value, respect = ... + name = (name or ""):trim() + end dbg.start{"MainControl:unsetenv(\"",name,"\", \"",value,"\")"} diff --git a/src/modfuncs.lua b/src/modfuncs.lua index f4593e704..eb2cec233 100644 --- a/src/modfuncs.lua +++ b/src/modfuncs.lua @@ -140,24 +140,6 @@ end -- Validate a function with only string module names table. -- @param cmdName The command which is getting its arguments validated. --local function l_validateArgsWithValue(cmdName, ...) --- local argA = pack(...) - --- for i = 1, argA.n -1 do --- local v = argA[i] --- if (type(v) ~= "string") then --- mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName} --- return false --- end --- end - --- local v = argA[argA.n] --- if (type(v) ~= "string" and type(v) ~= "number" and type(v) ~= "boolean") then --- mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName} --- return false --- end --- return true ---end - local function l_validateArgsWithValue(cmdName, table) local n = table.n or #table @@ -205,18 +187,6 @@ local function l_validateModules(cmdName, ...) return allGood end --------------------------------------------------------------------------- --- The load function. It can be found in the following forms: --- "load('name'); load('name/1.2'); load(atleast('name','3.2'))", ---function load_module(...) --- dbg.start{"load_module(",l_concatTbl({...},", "),")"} --- if (not l_validateModules("load",...)) then return {} end - --- dbg.print{"mcp:name(): ",mcp:name(),"\n"} --- local b = mcp:load_usr(MName:buildA(mcp:MNameType(), ...)) --- dbg.fini("load_module") --- return b ---end -------------------------------------------------------------------------- -- Convert all function arguments to table form @@ -263,11 +233,14 @@ function inTable(tbl, val) end +-------------------------------------------------------------------------- +-- The load function. It can be found in the following forms: +-- "load('name'); load('name/1.2'); load(atleast('name','3.2'))", +--function load_module(...) function load_module(...) dbg.start{"load_module(",l_concatTbl({...},", "),")"} - -- Convert arguments into a table, similar to how setenv is handled local argTable local mcp_old = mcp mcp, argTable = list_2_Tbl(MCP, mcp, ...) @@ -277,29 +250,13 @@ function load_module(...) return {} end - -- If a mode selector is provided, check if current evaluation mode matches - if argTable.mode then - local currentMode = mcp._mode - if (not inTable(argTable.mode, currentMode)) then - -- If the current mode is not allowed, skip this operation - mcp = mcp_old - dbg.fini("load_module") - return {} - end - end - - -- Validate the modules as before if (not l_validateModules("load", table.unpack(argTable))) then mcp = mcp_old dbg.fini("load_module") return {} end - dbg.print{"mcp:name(): ", mcp:name(), "\n"} - - -- Pass mode information along to MName:buildA - -- We assume MName:buildA is modified to accept a mode parameter and forward it to M.new - local b = mcp:load_usr(MName:buildA(mcp:MNameType(), argTable, argTable.mode)) + local b = mcp:load_usr(MName:buildA(mcp:MNameType(), table.unpack(argTable))) mcp = mcp_old dbg.fini("load_module") @@ -423,17 +380,17 @@ end function setenv(...) dbg.start{"setenv(",l_concatTbl({...},", "),")"} - local table + local argTable local mcp_old = mcp - mcp, table = list_2_Tbl(MCP, mcp, ...) + mcp, argTable = list_2_Tbl(MCP, mcp, ...) if ( not mcp ) then mcp = mcp_old return end - if (not l_validateArgsWithValue("setenv", table)) then return end + if (not l_validateArgsWithValue("setenv", argTable)) then return end - mcp:setenv(table) + mcp:setenv(table.unpack(argTable)) mcp = mcp_old dbg.fini("setenv") return @@ -444,9 +401,19 @@ end -- Unset the value of environment variable. function unsetenv(...) dbg.start{"unsetenv(",l_concatTbl({...},", "),")"} - if (not l_validateArgsWithValue("unsetenv",...)) then return end - mcp:unsetenv(...) + local argTable + local mcp_old = mcp + mcp, argTable = list_2_Tbl(MCP, mcp, ...) + if ( not mcp ) then + mcp = mcp_old + return + end + + if (not l_validateArgsWithValue("unsetenv", argTable)) then return end + + mcp:unsetenv(table.unpack(argTable)) + mcp = mcp_old dbg.fini("unsetenv") return end @@ -958,8 +925,18 @@ end -- not loaded. The reverse of an unload is a no-op. function unload(...) dbg.start{"unload(",l_concatTbl({...},", "),")"} - if (not l_validateModules("unload",...)) then return {} end - local b = unload_internal(MName:buildA("mt",...)) + + local argTable + local mcp_old = mcp + mcp, argTable = list_2_Tbl(MCP, mcp, ...) + if (not mcp) then + mcp = mcp_old + dbg.fini("unload_module") + return {} + end + + if (not l_validateModules("unload", table.unpack(argTable))) then return {} end + local b = unload_internal(MName:buildA("mt",table.unpack(argTable))) dbg.fini("unload") return b end