webmcp

annotate libraries/extos/pfilter.lua @ 466:2751b6b81c23

Minor efficiency enhancement in <db_object>:try_save() method
author jbe
date Mon Nov 07 19:32:51 2016 +0100 (2016-11-07)
parents f704f35923e2
children 87c87be92154
rev   line source
jbe@435 1 --[[
jbe@435 2
jbe@435 3 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.
jbe@435 4
jbe@435 5 data_out, -- string containing stdout data, or nil in case of error
jbe@435 6 data_err, -- string containing error or stderr data
jbe@435 7 status = -- exit code, or negative code in case of abnormal termination
jbe@435 8 pfilter(
jbe@435 9 data_in, -- string containing stdin data
jbe@435 10 filename, -- executable
jbe@435 11 arg1, -- first (non-zero) argument to executable
jbe@435 12 arg2, -- second argument to executable
jbe@435 13 ...
jbe@435 14 )
jbe@435 15
jbe@435 16 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.
jbe@435 17
jbe@435 18 --]]
jbe@435 19
jbe@435 20 return function(data_in, ...)
jbe@435 21 local process, errmsg = moonbridge_io.exec(...)
jbe@435 22 if not process then return nil, errmsg end
jbe@435 23 local read_fds = {[process.stdout] = true, [process.stderr] = true}
jbe@435 24 local write_fds = {[process.stdin] = true}
jbe@435 25 local function read(socket, chunks)
jbe@435 26 if read_fds[socket] then
jbe@435 27 local chunk, status = socket:read_nb()
jbe@435 28 if not chunk then
jbe@435 29 socket:close()
jbe@435 30 read_fds[socket] = nil
jbe@435 31 else
jbe@435 32 chunks[#chunks+1] = chunk
jbe@435 33 if status == "eof" then
jbe@435 34 socket:close()
jbe@435 35 read_fds[socket] = nil
jbe@435 36 end
jbe@435 37 end
jbe@435 38 end
jbe@435 39 end
jbe@435 40 local function write(...)
jbe@435 41 if write_fds[process.stdin] then
jbe@435 42 local buffered = process.stdin:flush_nb(...)
jbe@435 43 if not buffered or buffered == 0 then
jbe@435 44 process.stdin:close()
jbe@435 45 write_fds[process.stdin] = nil
jbe@435 46 end
jbe@435 47 end
jbe@435 48 end
jbe@435 49 write(data_in or "")
jbe@435 50 local stdout_chunks, stderr_chunks = {}, {}
jbe@435 51 while next(read_fds) or next(write_fds) do
jbe@435 52 moonbridge_io.poll(read_fds, write_fds)
jbe@435 53 read(process.stdout, stdout_chunks)
jbe@435 54 read(process.stderr, stderr_chunks)
jbe@435 55 write()
jbe@435 56 end
jbe@435 57 return
jbe@435 58 table.concat(stdout_chunks),
jbe@435 59 table.concat(stderr_chunks),
jbe@435 60 process:wait()
jbe@435 61 end
jbe@435 62

Impressum / About Us