Page 1 of 1

spawn function

Posted: Thu May 14, 2009 11:13 pm
by Quisar
Hi,

I write a spawn function for mudlet. I tried to add the diff but whatever extension I chose, I get an error. Tell me where I can put it, if you are interested.

The idea is to be able to spawn an external process from a script and to be able to communicate with it.

Here is an example script:

Code: Select all

catProcess = nil

function readCatProcess( a ) 
    echo(a)
end

function getCatProcess()
    if catProcess == nil or  not catProcess.isRunning() then
        if catProcess ~= nil then
            echo("Process did finish. Starting a new one!\n")
        end
        catProcess = spawn(readCatProcess, "/bin/cat")
    end
    return catProcess
end
with the following aliases:

Code: Select all

^cat  *(.*) -> getCatProcess().send(matches[2] .. "\n")

Code: Select all

^close -> getCatProcess().close()
The spawn method take as argument a callback function that will be call each time the process write something, and a list of string that is the process and its arguments.

It returns a process object that contains 3 functions:
send String -> () that send a string to the process
close () -> () that close the channel to the process (it does not end the process)
isRunning () -> bool that returns whether the process is still running or not.

Re: spawn function

Posted: Thu May 14, 2009 11:25 pm
by Heiko
tar/zip the diff and attach it to a posting. This will work.

This would be a useful function.

Re: spawn function

Posted: Thu May 14, 2009 11:26 pm
by Quisar
Here is the patch.

Re: spawn function

Posted: Thu May 14, 2009 11:29 pm
by Heiko
Thank you. New users need 3 approved postings before they are allowed to post freely. We have a problem with spam bots.

Re: spawn function

Posted: Thu May 14, 2009 11:33 pm
by Vadi
Thanks a ton!

You do know about http://www.lua.org/manual/5.1/manual.ht ... os.execute though? (yours is more fully featured though, so I very much agree with it still).

Re: spawn function

Posted: Thu May 14, 2009 11:38 pm
by Quisar
Vadi wrote:Thanks a ton!

You do know about http://www.lua.org/manual/5.1/manual.ht ... os.execute though? (yours is more fully featured though, so I very much agree with it still).
I did look at it, yes. But it does not allow communication and I need that.

I plan to use it to (try to) implement a mapper using an external application, and for that I really need bi-directionnal communications.

Re: spawn function

Posted: Thu May 14, 2009 11:46 pm
by Heiko
The patch is good. I'll commit it to git. Hope to see more ;)