OK, I finally solve my problem, with the following code (maybe it could be optimized):
Lua Code:
function .executeCmd(cmdStr, isMacroBoolean, noPrintErrorBoolean) -- return "", or error string
local cmdStr = cmdStr or ""
local tbl = {strsplit("\n", cmdStr)}
local function executeString(str) -- return "", or error string
local str = str or ""
local func, errorMessage = loadstring(str)
if (not func) then
return "slash command LUA error #59000."
else
local success, errorMessage = pcall(func)
if (not success) then
return "slash command error #59001."
else
return ""
end
end
end
local resultStr = ""
for i, strLine in ipairs(tbl) do
if (resultStr ~= "") then break end
if (isMacroBoolean) then
--------------------------- macro ("/" needed)
if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
if (string.sub(strLine, 1, 5) == "/run ") then
strLine = string.sub(strLine, 6)
resultStr = executeString(strLine)
elseif (string.sub(strLine, 1, 8) == "/script ") then
strLine = string.sub(strLine, 9)
resultStr = executeString(strLine)
else
DEFAULT_CHAT_FRAME.editBox:SetText(strLine) -- sent to Chat Box
ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox,0)
end
else
--------------------------- command ("/" not valid)
if (string.sub(strLine, 1, 1) == "/") then strLine = string.sub(strLine, 2) end -- delete char 1
resultStr = executeString(strLine)
end
end
if ((resultStr ~= "") and (not noPrintErrorBoolean)) then print(resultStr) end
return resultStr
end