summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2012-12-13 15:04:29 +0000
committerTed Trask <ttrask01@yahoo.com>2012-12-13 15:04:29 +0000
commit4fb53c1fa8dd550d4f1b1654cb5fda3518385dc1 (patch)
tree4a075eba8153fd9a99416570a9b659aa1ef04e94 /lib
parent11327093bfa303006f3d8def04aa03f55e75d6f7 (diff)
downloadacf-core-4fb53c1fa8dd550d4f1b1654cb5fda3518385dc1.tar.bz2
acf-core-4fb53c1fa8dd550d4f1b1654cb5fda3518385dc1.tar.xz
Make modelfunctions.run_executable poll so it doesn't block on full pipe. Also add flag to add stderr to output.
Polling with posix.sleep is definitely not the most efficient way to do this, but it works for now. We can try to add better C library support in the future.
Diffstat (limited to 'lib')
-rw-r--r--lib/modelfunctions.lua26
1 files changed, 21 insertions, 5 deletions
diff --git a/lib/modelfunctions.lua b/lib/modelfunctions.lua
index 8981bdd..4f99780 100644
--- a/lib/modelfunctions.lua
+++ b/lib/modelfunctions.lua
@@ -239,24 +239,40 @@ end
-- args should be an array where args[1] is the executable
-- output will never be nil
-- errtxt will be nil for success and non-nil for failure
-run_executable = function(args)
+-- if include_err, then stderr will be prepended to stdout (if executable doesn't fail)
+run_executable = function(args, include_err)
local output = ""
local errtxt
local res, err = pcall(function()
-- For security, set the path
posix.setenv("PATH", "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin")
+ args.stdin = "/dev/null"
args.stdout = subprocess.PIPE
args.stderr = subprocess.PIPE
local proc, errmsg, errno = subprocess.popen(args)
if proc then
+ local out = {}
+ local err = {}
+ function readpipes()
+ local o = proc.stdout:read("*a")
+ if o ~= "" then out[#out+1] = o end
+ local e = proc.stderr:read("*a")
+ if e ~= "" then err[#err+1] = e end
+ end
+ while nil == proc:poll() do
+ readpipes()
+ posix.sleep(0)
+ end
+ readpipes()
proc:wait()
- output = (proc.stdout:read("*a")) or ""
proc.stdout:close()
- errtxt = (proc.stderr:read("*a"))
proc.stderr:close()
- if proc.exitcode == 0 and string.find(errtxt, "^%s*$") then
- errtxt = nil
+ output = table.concat(out, "") or ""
+ if proc.exitcode == 0 and include_err and #err > 0 then
+ output = table.concat(err, "")..output
+ elseif proc.exitcode ~= 0 then
+ errtxt = table.concat(err, "") or "Unknown error"
end
else
errtxt = errmsg or "Unknown failure"