lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

I'd like to accumulate stdio to a buffer.

Let me explain:

I have a Lua server, which is waiting for various tasks to do (reading
and sourcing files, executing commands, …). He is listening on Unix
demain sockets.

>From a client, i type things like this:
source foo.file
exec for k,v in pairs(foo_buffer) do print(k,v) end

The «exec» function is this one:

function exec(client,command)
  local chunk,err_msg = load(command)

  if chunk then
    _, err_msg = pcall(chunk)
  end

  if err_msg then
    client:send(err_msg)
  end
end


As you can see, all outputs to stdio will be done on the server-side.
(only errors will be send to client)

I'd like to redirect stdout to a buffer, and sending this buffer to the
client.

I could do something like this:

  if chunk then
    curr_stdout = io.output()
    handle = io.output("/tmp/foo")
    _, err_msg = pcall(chunk)
    
    -- here, reading /tmp/foo to buffer
    -- and then…
    
    client:send(buffer)
    io.output(curr_stdout)
    io.close(handle)
  end


I'm sure there's another way to do this.

Thank you.