webmcp

changeset 435:f704f35923e2

Added a (currently unused) pure Lua version of extos.pfilter(...)
author jbe
date Sat Jan 16 04:44:16 2016 +0100 (2016-01-16)
parents c7a27cfd07d0
children 1dbbe4c62f08
files libraries/extos/pfilter.lua
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libraries/extos/pfilter.lua	Sat Jan 16 04:44:16 2016 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +--[[
     1.5 +
     1.6 +This file contains an alternative pfilter(...) implementation in pure Lua (utilizing moonbridge_io). This implementation is currently not used since extos already comes with a C implementation (extos.pfilter), which is independent of moonbridge_io and thus preferred to the implementation below.
     1.7 +
     1.8 +data_out,    -- string containing stdout data, or nil in case of error
     1.9 +data_err,    -- string containing error or stderr data
    1.10 +status =     -- exit code, or negative code in case of abnormal termination
    1.11 +pfilter(
    1.12 +  data_in,   -- string containing stdin data
    1.13 +  filename,  -- executable
    1.14 +  arg1,      -- first (non-zero) argument to executable
    1.15 +  arg2,      -- second argument to executable
    1.16 +  ...
    1.17 +)
    1.18 +
    1.19 +Executes the executable given by "filename", passing optional arguments. A given string may be fed into the program as stdin. On success 3 values are returned: A string containing all stdout data of the sub-process, a string containing all stderr data of the sub-process, and a status code. The status code is negative, if the program didn't terminate normally. By convention a status code of zero indicates success, while positive status codes indicate error conditions. If program execution was not possible at all, then nil is returned as first value and an error string as second value.
    1.20 +
    1.21 +--]]
    1.22 +
    1.23 +return function(data_in, ...)
    1.24 +  local process, errmsg = moonbridge_io.exec(...)
    1.25 +  if not process then return nil, errmsg end
    1.26 +  local read_fds = {[process.stdout] = true, [process.stderr] = true}
    1.27 +  local write_fds = {[process.stdin] = true}
    1.28 +  local function read(socket, chunks)
    1.29 +    if read_fds[socket] then
    1.30 +      local chunk, status = socket:read_nb()
    1.31 +      if not chunk then
    1.32 +        socket:close()
    1.33 +        read_fds[socket] = nil
    1.34 +      else
    1.35 +        chunks[#chunks+1] = chunk
    1.36 +        if status == "eof" then
    1.37 +          socket:close()
    1.38 +          read_fds[socket] = nil
    1.39 +        end
    1.40 +      end
    1.41 +    end
    1.42 +  end
    1.43 +  local function write(...)
    1.44 +    if write_fds[process.stdin] then
    1.45 +      local buffered = process.stdin:flush_nb(...)
    1.46 +      if not buffered or buffered == 0 then
    1.47 +        process.stdin:close()
    1.48 +        write_fds[process.stdin] = nil
    1.49 +      end
    1.50 +    end
    1.51 +  end
    1.52 +  write(data_in or "")
    1.53 +  local stdout_chunks, stderr_chunks = {}, {}
    1.54 +  while next(read_fds) or next(write_fds) do
    1.55 +    moonbridge_io.poll(read_fds, write_fds)
    1.56 +    read(process.stdout, stdout_chunks)
    1.57 +    read(process.stderr, stderr_chunks)
    1.58 +    write()
    1.59 +  end
    1.60 +  return
    1.61 +    table.concat(stdout_chunks),
    1.62 +    table.concat(stderr_chunks),
    1.63 +    process:wait()
    1.64 +end
    1.65 +

Impressum / About Us