rename pipe_to_string() to capture_exec() and improve it's interface
[libi2ncommon] / src / pipestream.cpp
index c475a50..b29ec70 100644 (file)
@@ -25,6 +25,7 @@ on this file might be covered by the GNU General Public License.
  ***************************************************************************/
 
 #include <stdio.h>
+#include <sys/wait.h>
 
 #include <string>
 #include <streambuf>
@@ -35,6 +36,58 @@ on this file might be covered by the GNU General Public License.
 #include "exception.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
+ */
+std::string capture_exec(const std::string& command, ExecResult &rescode)
+{
+    std::string output;
+
+    bool exit_set;
+    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;
+}
+
 inpipebuf::inpipebuf(const std::string& command)
 {
     status_set = NULL;
@@ -88,43 +141,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;