From 482d030658b7ad80c627ab0d025ca7fafa1f8c07 Mon Sep 17 00:00:00 2001 From: Matthew Cawood Date: Tue, 10 Dec 2024 15:02:17 -0600 Subject: [PATCH] code changes towards mode select --- rt/unload/mf/Core/C/1.0.lua | 2 +- src/MName.lua | 4 +- src/MainControl.lua | 30 ++++--- src/modfuncs.lua | 153 +++++++++++++++++++++++++++++++++--- 4 files changed, 166 insertions(+), 23 deletions(-) diff --git a/rt/unload/mf/Core/C/1.0.lua b/rt/unload/mf/Core/C/1.0.lua index 8fa18bd4e..f4505d01f 100644 --- a/rt/unload/mf/Core/C/1.0.lua +++ b/rt/unload/mf/Core/C/1.0.lua @@ -1,4 +1,4 @@ -setenv{"A", "A", mode={"load"}} +setenv{"FOO", "BAR", mode={"load"}} setenv{"A", "B", mode={"unload"}} pushenv{"STACK_VAR", "load_value", mode={"load"}} pushenv{"STACK_VAR", "unload_value", mode={"unload"}} diff --git a/src/MName.lua b/src/MName.lua index 600aa929e..732b5a3a8 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) +function M.new(self, sType, name, action, is, ie, mode) --dbg.start{"Mname:new(",sType,")"} local exact_match = cosmic:value("LMOD_EXACT_MATCH") @@ -143,6 +143,8 @@ function M.new(self, sType, name, action, is, ie) o.__userName = (name or ""):trim():gsub("/+$",""):gsub("%.lua$","") end + o.__mode = mode + --dbg.fini("MName:new") return o end diff --git a/src/MainControl.lua b/src/MainControl.lua index 3df0841f9..bde7aa4e7 100644 --- a/src/MainControl.lua +++ b/src/MainControl.lua @@ -312,8 +312,10 @@ 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, name, value, respect) - name = (name or ""):trim() +function M.setenv(self, table) --name, value, respect) + local name = (table[1] or ""):trim() + local value = table[2] + local respect = table[3] or nil dbg.start{"MainControl:setenv(\"",name,"\", \"",value,"\", \"", respect,"\")"} @@ -342,8 +344,10 @@ end ------------------------------------------------------------------- -- Set an environment variable. -- This function just sets the name with value in the current env. -function M.setenv_env(self, name, value, respect) - name = (name or ""):trim() +function M.setenv_env(self, table) --name, value, respect) + local name = (table[1] or ""):trim() + local value = table[2] + local respect = table[3] or nil dbg.start{"MainControl:setenv_env(\"",name,"\", \"",value,"\", \"", respect,"\")"} posix.setenv(name, value, true) @@ -357,8 +361,12 @@ 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, name, value, respect) - name = (name or ""):trim() +function M.unsetenv(self, table) --name, value, respect) + + local name = (table[1] or ""):trim() + local value = table[2] + local respect = table[3] or nil + dbg.start{"MainControl:unsetenv(\"",name,"\", \"",value,"\")"} l_check_for_valid_name("unsetenv",name) @@ -393,8 +401,9 @@ end -- @param self A MainControl object. -- @param name the environment variable name. -- @param value the environment variable value. -function M.pushenv(self, name, value) - name = (name or ""):trim() +function M.pushenv(self, table) --name, value) + local name = table[1]:trim() + local value = table[2] dbg.start{"MainControl:pushenv(\"",name,"\", \"",value,"\")"} l_check_for_valid_name("pushenv",name) @@ -449,8 +458,9 @@ end -- @param self A MainControl object. -- @param name the environment variable name. -- @param value the environment variable value. -function M.popenv(self, name, value) - name = (name or ""):trim() +function M.popenv(self, table) --name, value) + local name = (table[1] or ""):trim() + local value = table[2] dbg.start{"MainControl:popenv(\"",name,"\", \"",value,"\")"} l_check_for_valid_name("popenv",name) diff --git a/src/modfuncs.lua b/src/modfuncs.lua index 5269dc66d..f4593e704 100644 --- a/src/modfuncs.lua +++ b/src/modfuncs.lua @@ -139,18 +139,37 @@ 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] +--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 + for i = 1, n -1 do + local v = table[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] + local v = table[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 @@ -158,6 +177,8 @@ local function l_validateArgsWithValue(cmdName, ...) return true end + + -------------------------------------------------------------------------- -- Validate a function with only string module names table. -- @param cmdName The command which is getting its arguments validated. @@ -187,16 +208,105 @@ 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 +-- first_elem seperated from args for type test +function list_2_Tbl(MCP, mcp, first_elem, ...) + dbg.start{"list_2_Tbl(",l_concatTbl({first_elem, ...},", "),")"} + + local my_mcp = nil + local t = nil + local action = nil + if ( type(first_elem) == "table" )then + t = first_elem + t.kind = "table" + local my_mode = mode() + local modeA = t.mode or {} + for i = 1,#modeA do + if (my_mode == modeA[i]) then + action = true + my_mcp = MCP + break + end + end + + else + t = pack(first_elem, ...) + t.kind = "list" + my_mcp = mcp + action = true + end + + if ( not action ) then my_mcp = nil end + dbg.print{"OutputTable: ", t, "\n"} + dbg.fini("list_2_Tbl") + return my_mcp, t +end + +function inTable(tbl, val) + for _, v in ipairs(tbl) do + if v == val then + return true + end + end + return false +end + + + 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(), ...)) + -- Convert arguments into a table, similar to how setenv is handled + local argTable + local mcp_old = mcp + mcp, argTable = list_2_Tbl(MCP, mcp, ...) + if (not mcp) then + mcp = mcp_old + dbg.fini("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)) + + mcp = mcp_old dbg.fini("load_module") return b end + function mgrload(required, active) dbg.start{"mgrload(",required,", activeA)"} @@ -299,16 +409,37 @@ function pushenv(...) end -------------------------------------------------------------------------- +-- Set the value of environment variable. +--function setenv(...) +-- dbg.start{"setenv(",l_concatTbl({...},", "),")"} +-- if (not l_validateArgsWithValue("setenv",...)) then return end + +-- mcp:setenv(...) +-- dbg.fini("setenv") +-- return +--end + -- Set the value of environment variable. function setenv(...) dbg.start{"setenv(",l_concatTbl({...},", "),")"} - if (not l_validateArgsWithValue("setenv",...)) then return end - mcp:setenv(...) + local table + local mcp_old = mcp + mcp, table = list_2_Tbl(MCP, mcp, ...) + if ( not mcp ) then + mcp = mcp_old + return + end + + if (not l_validateArgsWithValue("setenv", table)) then return end + + mcp:setenv(table) + mcp = mcp_old dbg.fini("setenv") return end + -------------------------------------------------------------------------- -- Unset the value of environment variable. function unsetenv(...)