spawn function

Post Reply
Quisar
Posts: 3
Joined: Thu May 14, 2009 11:03 pm

spawn function

Post by Quisar » Thu May 14, 2009 11:13 pm

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.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: spawn function

Post by Heiko » Thu May 14, 2009 11:25 pm

tar/zip the diff and attach it to a posting. This will work.

This would be a useful function.

Quisar
Posts: 3
Joined: Thu May 14, 2009 11:03 pm

Re: spawn function

Post by Quisar » Thu May 14, 2009 11:26 pm

Here is the patch.
Attachments
patch.mudlet.zip
spawn patch.
(3.88 KiB) Downloaded 197 times

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: spawn function

Post by Heiko » Thu May 14, 2009 11:29 pm

Thank you. New users need 3 approved postings before they are allowed to post freely. We have a problem with spam bots.

User avatar
Vadi
Posts: 4674
Joined: Sat Mar 14, 2009 3:13 pm

Re: spawn function

Post by Vadi » Thu May 14, 2009 11:33 pm

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).

Quisar
Posts: 3
Joined: Thu May 14, 2009 11:03 pm

Re: spawn function

Post by Quisar » Thu May 14, 2009 11:38 pm

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.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: spawn function

Post by Heiko » Thu May 14, 2009 11:46 pm

The patch is good. I'll commit it to git. Hope to see more ;)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests