]> git.e2factory.org Git - e2factory.git/commitdiff
e2lib: add callcmd_stdout_stderr() taking two callbacks
authorTobias Ulmer <tu@emlix.com>
Wed, 15 Feb 2017 14:58:56 +0000 (15:58 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 15 Feb 2017 14:58:56 +0000 (15:58 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua

index d41cfdbb615b4e09f08e1eaac584e66f67f24494..9d2b2ee7e84bb6aabac6aaf6e113f1b2c8334387 100644 (file)
@@ -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.