add option to forward environment to pipestream
[libi2ncommon] / src / pipestream.hxx
index d13f312..8668df3 100644 (file)
@@ -29,12 +29,15 @@ on this file might be covered by the GNU General Public License.
 
 #include <stdio.h>
 
+#include <cstring>
 #include <string>
 #include <streambuf>
 #include <istream>
 #include <ostream>
 #include <vector>
 
+#include <stringfunc.hxx>
+
 struct ExecResult
 {
     /** if the program exited normally and returned a return code */
@@ -51,12 +54,27 @@ struct ExecResult
 
     /** errormessage if we have one */
     std::string error_message;
+
+    inline std::string format (void) const
+    {
+        return std::string ("(")
+            + "(normal_exit " + (this->normal_exit ? "T" : "F") + ") "
+              "(return_code '" + I2n::to_string ((int)this->return_code) + "') "
+              "(signal " + (this->terminated_by_signal
+                            ? strsignal (this->signal)
+                            : "<nil>") + "))"
+            ;
+    };
 };
 typedef struct ExecResult ExecResult;
 
 std::string capture_exec(const std::string& command, ExecResult &rescode);
-std::string capture_exec(const char *const *command, ExecResult &rescode);
-std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode);
+std::string capture_exec(const char *const *command, ExecResult &rescode,
+                         const bool out=true, const bool err=false,
+                         const bool path=false, const bool env=false);
+std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode,
+                         const bool out=true, const bool err=false,
+                         const bool path=false, const bool env=false);
 
 inline std::string capture_exec (const std::string &command)
 {
@@ -91,15 +109,19 @@ protected:
     char buffer;
 
     FILE *pipe;
+    pid_t pid;
 
     // "callback" variables for destructor to store exit status
     bool *status_set;
     int *exit_status;
 
 public:
-    inpipebuf(const std::string& command);
-    inpipebuf(const char *const *command);
-    inpipebuf(const std::vector<std::string> &command);
+    inpipebuf(const std::string& command,
+              const bool out, const bool err, const bool path, const bool env);
+    inpipebuf(const char *const *command,
+              const bool out, const bool err, const bool path, const bool env);
+    inpipebuf(const std::vector<std::string> &command,
+              const bool out, const bool err, const bool path, const bool env);
 
     ~inpipebuf();
 
@@ -109,7 +131,10 @@ protected:
     virtual int_type underflow();
 
 private:
-    FILE *init_without_shell (const char *const *argv) const;
+    std::pair <pid_t, FILE *>
+    init_without_shell (const char *const *argv,
+                        const bool out, const bool err,
+                        const bool path, const bool env) const;
 };
 
 /** @brief stream around inpipebuf -- see comment there */
@@ -119,15 +144,22 @@ protected:
     inpipebuf buf;
 
 public:
-    inpipestream(const std::string& command)
-            : std::istream(&buf), buf(command)
+    inpipestream(const std::string& command,
+                 const bool out=true, const bool err=false,
+                 const bool path=false, const bool env=false)
+            : std::istream(&buf), buf(command, out, err, path, env)
     {}
-    inpipestream(const char *const command[])
-            : std::istream(&buf), buf(command)
+
+    inpipestream(const char *const command[],
+                 const bool out=true, const bool err=false,
+                 const bool path=false, const bool env=false)
+            : std::istream(&buf), buf(command, out, err, path, env)
     {}
 
-    inpipestream(const std::vector<std::string> &command)
-            : std::istream(&buf), buf(command)
+    inpipestream(const std::vector<std::string> &command,
+                 const bool out=true, const bool err=false,
+                 const bool path=false, const bool env=false)
+            : std::istream(&buf), buf(command, out, err, path, env)
     {}
 
     void store_exit_status(bool *_status_set, int *_exit_status)