aboutsummaryrefslogtreecommitdiff
path: root/butl
diff options
context:
space:
mode:
Diffstat (limited to 'butl')
-rw-r--r--butl/process15
-rw-r--r--butl/process.cxx38
2 files changed, 52 insertions, 1 deletions
diff --git a/butl/process b/butl/process
index 96b161f..e1eea29 100644
--- a/butl/process
+++ b/butl/process
@@ -9,8 +9,9 @@
# include <sys/types.h> // pid_t
#endif
+#include <iosfwd>
#include <cassert>
-#include <cstdint> // uint32_t
+#include <cstdint> // uint32_t
#include <system_error>
#include <butl/path>
@@ -260,6 +261,18 @@ namespace butl
static process_path
try_path_search (const path&, bool, const dir_path& = dir_path ());
+ // Print process commmand line. If the number of elements is specified,
+ // then it will print the piped multi-process command line, if present.
+ // In this case, the expected format is as follows:
+ //
+ // name1 arg arg ... nullptr
+ // name2 arg arg ... nullptr
+ // ...
+ // nameN arg arg ... nullptr nullptr
+ //
+ static void
+ print (std::ostream&, const char* const args[], size_t n = 0);
+
public:
#ifndef _WIN32
using handle_type = pid_t;
diff --git a/butl/process.cxx b/butl/process.cxx
index e13a8cd..38948a1 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -37,6 +37,7 @@
#include <cassert>
#include <cstddef> // size_t
#include <cstring> // strlen(), strchr()
+#include <ostream>
#include <butl/utility> // casecmp()
#include <butl/fdstream> // fdnull(), fdclose()
@@ -120,6 +121,43 @@ namespace butl
return r;
}
+ void process::
+ print (ostream& o, const char* const args[], size_t n)
+ {
+ size_t m (0);
+ const char* const* p (args);
+ do
+ {
+ if (m != 0)
+ o << " |"; // Trailing space will be added inside the loop.
+
+ for (m++; *p != nullptr; p++, m++)
+ {
+ if (p != args)
+ o << ' ';
+
+ // Quote if empty or contains spaces.
+ //
+ bool q (**p == '\0' || strchr (*p, ' ') != nullptr);
+
+ if (q)
+ o << '"';
+
+ o << *p;
+
+ if (q)
+ o << '"';
+ }
+
+ if (m < n) // Can we examine the next element?
+ {
+ p++;
+ m++;
+ }
+
+ } while (*p != nullptr);
+ }
+
#ifndef _WIN32
static process_path