add pipestream ctor overload for vectors of string
[libi2ncommon] / src / pipestream.cpp
index c475a50..cd9347a 100644 (file)
@@ -24,17 +24,203 @@ on this file might be covered by the GNU General Public License.
     copyright            : (C) 2001 by Intra2net AG
  ***************************************************************************/
 
+#include <errno.h>
 #include <stdio.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
-#include <string>
 #include <streambuf>
 #include <istream>
 #include <ostream>
 #include <cstdio>
+#include <boost/foreach.hpp>
+#include <boost/shared_array.hpp>
 
 #include "exception.hxx"
+#include "stringfunc.hxx"
 #include "pipestream.hxx"
 
+/** @brief runs command and returns it's output as string
+ *  @param command the full command with all parameters
+ *  @param rescode struct containing the return code, if the program exited normally and so on
+ *  @returns the output (stdout) of the called program
+ */
+template <typename CmdT>
+std::string capture_exec(CmdT command, ExecResult &rescode)
+{
+    std::string output;
+
+    bool exit_set = false;
+    int exit_status_waitpid;
+
+    // set the results to false until we are sure we have proper values
+    rescode.normal_exit = false;
+    rescode.terminated_by_signal = false;
+
+    try
+    {
+        {
+            inpipestream ips(command);
+
+            ips.store_exit_status(&exit_set, &exit_status_waitpid);
+
+            char buffer[2048];
+            while (ips.good())
+            {
+                ips.read(buffer, sizeof(buffer));
+                output.append(buffer, ips.gcount());
+            }
+        }
+
+        // exit_status_waitpid only valid after destruction of the inpipestream
+
+        if (exit_set)
+        {
+            rescode.normal_exit = WIFEXITED(exit_status_waitpid);
+            if (rescode.normal_exit)
+                rescode.return_code = WEXITSTATUS(exit_status_waitpid);
+
+            rescode.terminated_by_signal = WIFSIGNALED(exit_status_waitpid);
+            if (rescode.terminated_by_signal)
+                rescode.signal = WTERMSIG(exit_status_waitpid);
+        }
+    }
+    catch (pipestream_error &e)
+    {
+        rescode.error_message = e.what();
+    }
+
+    return output;
+}
+
+std::string capture_exec (const std::string &command, ExecResult &res)
+{ return capture_exec<const std::string &>(command, res); }
+
+std::string capture_exec (const char *const *command, ExecResult &res)
+{ return capture_exec<const char *const *>(command, res); }
+
+std::string capture_exec (const std::vector<std::string> &command, ExecResult &res)
+{
+    return capture_exec<const std::vector<std::string> &>(command, res);
+}
+
+#define PIPE_CTOR_FAIL(where) \
+    do { \
+        throw EXCEPTION (pipestream_error, \
+                         std::string (where) + ": error " \
+                         + I2n::to_string (errno) \
+                         + " (" + std::string (strerror (errno)) + ")"); \
+    } while (0)
+
+static boost::shared_array <char *>
+mk_argv (const std::vector<std::string> &command)
+{
+    char **ret = NULL;
+
+    try {
+        ret = new char *[command.size () * sizeof (ret[0]) + 1];
+    } catch (std::bad_alloc &) {
+        return boost::shared_array<char *> ();
+    }
+
+    size_t cur = 0;
+    BOOST_FOREACH(const std::string &arg, command) {
+        /*
+         * Casting away constness is safe since the data is always
+         * kept alive until after exec().
+         */
+        ret [cur++] = const_cast<char *> (arg.c_str ());
+    }
+
+    ret [cur] = NULL;
+
+    return boost::shared_array<char *> (ret);
+}
+
+
+FILE *
+inpipebuf::init_without_shell (const char *const *argv) const
+{
+    FILE *pipeobj = NULL;
+    int pipefd [2];
+
+    errno = 0;
+    if (::pipe (pipefd) == -1) {
+        PIPE_CTOR_FAIL("pipe");
+    }
+
+    errno = 0;
+    pid_t childpid = fork ();
+    switch (childpid) {
+        case -1: {
+            PIPE_CTOR_FAIL("fork");
+            break;
+        }
+        case 0: {
+            close (pipefd [0]);
+
+            if (dup2 (pipefd[1], STDOUT_FILENO) == -1) {
+                PIPE_CTOR_FAIL("dup2");
+            }
+
+            if (dup2 (pipefd[1], STDERR_FILENO) == -1) {
+                PIPE_CTOR_FAIL("dup2");
+            }
+
+            errno = 0;
+            if (execve (argv [0], const_cast <char *const *>(argv), NULL) == -1) {
+                PIPE_CTOR_FAIL("exec");
+            }
+            break;
+        }
+        default: {
+            close (pipefd [1]);
+
+            errno = 0;
+            if ((pipeobj = fdopen (pipefd [0], "r")) == NULL) {
+                PIPE_CTOR_FAIL("fdopen");
+            }
+            break;
+        }
+    }
+
+    return pipeobj;
+}
+
+inpipebuf::inpipebuf(const char *const *command)
+    : pipe (NULL) /* brr: shadowing global ident */
+    , status_set (NULL)
+    , exit_status (NULL)
+{
+    if (command == NULL || command [0] == NULL) {
+        PIPE_CTOR_FAIL("command");
+    }
+
+    this->pipe = this->init_without_shell (command);
+
+    setg (&buffer, &buffer, &buffer);
+}
+
+inpipebuf::inpipebuf(const std::vector<std::string> &command)
+    : pipe (NULL) /* brr: shadowing global ident */
+    , status_set (NULL)
+    , exit_status (NULL)
+{
+    if (command.empty ()) {
+        PIPE_CTOR_FAIL("command");
+    }
+
+    const boost::shared_array <char *> argv = mk_argv (command);
+    if (!argv) {
+        PIPE_CTOR_FAIL("malloc");
+    }
+
+    this->pipe = this->init_without_shell (argv.get ());
+
+    setg (&buffer, &buffer, &buffer);
+}
+
 inpipebuf::inpipebuf(const std::string& command)
 {
     status_set = NULL;
@@ -88,43 +274,6 @@ inpipebuf::int_type inpipebuf::underflow()
     return traits_type::to_int_type(*gptr());
 }
 
-/** @brief runs command and returns it's output as string
- *  @param command the full command with all parameters
- *  @param exit_status the full exit status, use WEXITSTATUS to get the "regular" return code
- *  @returns the output (stderr) of the called program
- */
-std::string pipe_to_string(const std::string& command, std::string *error, int *_exit_status)
-{
-    std::string result;
-    bool exit_set;
-
-    try
-    {
-        inpipestream ips(command);
-
-        ips.store_exit_status(&exit_set, _exit_status);
-
-        char buffer[2048];
-        while (ips.good())
-        {
-            ips.read(buffer, sizeof(buffer));
-            result.append(buffer, ips.gcount());
-        }
-    }
-    catch (pipestream_error &e)
-    {
-        if (error)
-            *error=e.what();
-        return "";
-    }
-    catch(...)
-    {
-        throw;
-    }
-
-    return result;
-}
-
 outpipebuf::outpipebuf(const std::string& command)
 {
     status_set = NULL;