From: Tobias Ulmer Date: Wed, 15 Feb 2017 14:58:56 +0000 (+0100) Subject: e2lib: add callcmd_stdout_stderr() taking two callbacks X-Git-Tag: e2factory-2.3.17~22 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=42b0aafff39639854d4aee23fada62dda7b8e057;p=e2factory.git e2lib: add callcmd_stdout_stderr() taking two callbacks Signed-off-by: Tobias Ulmer --- diff --git a/generic/e2lib.lua b/generic/e2lib.lua index d41cfdb..9d2b2ee 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -1548,6 +1548,47 @@ function e2lib.callcmd_capture(cmd, capture, workdir, envdict) return rc end +--- Call command with stdin redirected to /dev/null and stderr/stdout +-- independently redirected to callback functions. Note that unlike +-- callcmd_capture(), the output will not necessarily appear in order at the +-- callbacks. +-- @param cmd Argument vector holding command and args. +-- @param outfn Callback for stdout. +-- @param errfn Callback for stderr. +-- @param workdir Optional working directory +-- @param envdict Optional environment dictionary. +-- @return Numeric return code of program or false on some errors. +-- @return Error object on failure. +function e2lib.callcmd_stdout_stderr(cmd, outfn, errfn, workdir, envdict) + assertIsTable(cmd) + assertIsFunction(outfn) + assertIsFunction(errfn) + assert(workdir == nil or type(workdir) == "string") + assert(envdict == nil or type(workdir) == "table") + + local rc, re, devnull + + devnull, re = eio.fopen("/dev/null", "r") + if not devnull then + return false, re + end + + local fdctv = { + { dup = eio.STDIN, istype = "readfo", file = devnull }, + { dup = eio.STDOUT, istype = "writefunc", callfn = outfn }, + { dup = eio.STDERR, istype = "writefunc", callfn = errfn }, + } + + rc, re = e2lib.callcmd(cmd, fdctv, workdir, envdict) + if not rc then + eio.fclose(devnull) + return false, re + end + + eio.fclose(devnull) + return rc +end + --- Call a command, log its output and catch the last lines for error reporting. -- See callcmd() for details. -- @param cmd Argument vector holding the command.