Skip to content

Commit

Permalink
code changes towards mode select
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcawood committed Dec 10, 2024
1 parent 64ee19a commit 482d030
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 23 deletions.
2 changes: 1 addition & 1 deletion rt/unload/mf/Core/C/1.0.lua
Original file line number Diff line number Diff line change
@@ -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"}}
Expand Down
4 changes: 3 additions & 1 deletion src/MName.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
30 changes: 20 additions & 10 deletions src/MainControl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,"\")"}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
153 changes: 142 additions & 11 deletions src/modfuncs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,46 @@ 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
end
return true
end



--------------------------------------------------------------------------
-- Validate a function with only string module names table.
-- @param cmdName The command which is getting its arguments validated.
Expand Down Expand Up @@ -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)"}

Expand Down Expand Up @@ -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(...)
Expand Down

0 comments on commit 482d030

Please sign in to comment.