'io.popen' not implemented in MUSHclient

Posted by Andy.Liu on Tue 07 Mar 2017 10:31 AM — 9 posts, 34,467 views.

#0
It works on command line.

Thanks.

Code:
function callRemoteLua(keyword)
print(keyword)
local handler = io.popen("lua fengxianpoem.lua "..keyword)
local poem = handler:read("*all")
handler:close()
print("found answer:"..poem)
end



Run-time error
World: robotcat
Immediate execution
[string "Script file"]:920: 'io.popen' not implemented in MUSHclient
stack traceback:
[C]: in function 'popen'
[string "Script file"]:920: in function 'callRemoteLua'
[string "Script file"]:905: in function 'answerPoem4'
[string "触发器: "]:1: in main chunk
脚本错误处的上下文:
916 : end
917 :
918 : function callRemoteLua(keyword)
919 : print(keyword)
920*: local handler = io.popen("lua fengxianpoem.lua "..keyword)
921 : local poem = handler:read("*all")
922 : handler:close()
923 : print("found answer:"..poem)
924 : end
USA Global Moderator #1
https://www.mushclient.com/scripts/doc.php?lua=io.popen says
Quote:
io.popen is not supported under the version compiled into MUSHclient


Try this instead:


-- replacement for io.popen
-- or os.execute with output
function osexecute(cmd)
   local n = GetInfo(66).."osexecute_temp_file.txt" -- temp file for catching output
   cmd = cmd .. " > \""..n.."\""
   local err = os.execute(cmd)
   local messages = {}
   for line in io.lines(n) do
      table.insert(messages, line)
   end
   os.remove(n) -- remove temp file
   return err, messages
end


But wait!
Are you calling another Lua from inside Lua? Why on earth would you do that?
Why not just import fengxianpoem.lua and call its functions?
Amended on Tue 07 Mar 2017 11:50 AM by Fiendish
#2
Why I call another Lua script is because that I found that some dll only can be loaded in command line or console way.

So I can work around it by calling another Lua script which can call require("luaiconv") successfully.

I can get the output of another Lua which does many works for my logic and I just copy the result with simple function.

That's my purpose to call another Lua.

Thanks for your reply and I will try it later and back to you.
#3
I have tried but output nothing to the temp file.
Its size is zero.
But when I run the code in command line,it output correct result.
Maybe it is because of asynchronize http request.
I don't know the root cause.
Australia Forum Administrator #4
I've been trying to call luaiconv myself. Without success. Which characters sets are you translating from/to?

If you can require luaiconv from the command line you should be able to do it from your MUSHclient script. Maybe it isn't in your path or something like that.
#5
I want to translate text between gbk and utf8 .
The text from mud is encoded in GBK but I handle It in lua in utf8
It works in Windows command line
I have installed lua5.1 by full package with an installer
#6
I have put dlls into the same directory which lua script file stayed.
they are all in mushclient/scripts
#7
I tried to load luaiconv with require "luaiconv" in mushclient lua script.

but it cannot find luaiconv. luaiconv placed in mushclient/scripts
and another copy in mushclient root directory also.
It doesn't work


so I want to call another lua which running in command line.
#8
I have worked around this issue.

There are 2 luas to complete the mission:

1.demo.lua
First,call fengxianpoem to fetech some html page and parse it by keyword. and write the result into a local text file.

Second,read the text file for result.

Full path of file or lua is required for mushclient,otherwise it failed.

function getScriptFilePath(fileName)
local mushSupport = true
if mushSupport then
return GetInfo(66).."scripts\\"..fileName
else
return fileName
end
end

function callRemoteFengxianLua(keyword)
keyword = string.gsub(keyword,"。","")
keyword = string.gsub(keyword,",",",")
print(keyword)
--Use fullpath
local script = getScriptFilePath("fengxianpoem.lua")
local resultPath = getScriptFilePath("fengxianpoemresult.txt")
local luacmd = "C:\\Program\ Files\\Lua\\5.1\\lua"
--lua shell command
local err = os.execute("\""..luacmd.."\" "..script.." "..keyword.." "..resultPath)
print("os.execute result:"..err)
--read result
local fileHandle = assert(io.open(resultPath,"r"),"failed to open file")
if fileHandle then
local dataUTF8 = fileHandle:read("*a");
print(dataUTF8)
end
fileHandle:close()

end

2.Fengxianpoem.lua
Fetech some data and parse it by keyword,save the result into a text file.

local keyword = "You failed when you get these words"
local filePath --full file path for writing result
if #arg>1 then
filePath = arg[2]
keyword = arg[1]
--print("keyword:"..keyword)
--print("filePath:"..filePath)

-- answerPoem5 fetech http page and parse it to result text,and write it to file.
----***luaiconv**** is called in this function.This is what I want.
---The lua script is running in command line so it works like that in command line.
local result = answerPoem5(keyword)
if not result then
result = ""
end
local fileHandle = assert(io.open(filePath,"w+"),"failed to open file")
--delay()
if fileHandle then
print("result:"..result)
fileHandle:write(result)
--delay()
fileHandle:close()
end
end


Thanks for your support.

I don't get require "luaiconv.dll" work yet.
And have no idea now.