C++98 compatible iterator erase
[ardour.git] / libs / pbd / system_exec.cc
index d92987bf218ef4ce9e1e5e4694dbcea6e5d0bf1b..351e4ee066ae118f2f69e0e64de6cf2b90715f42 100644 (file)
@@ -170,6 +170,7 @@ SystemExec::init ()
        stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
        stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
        stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
+       w_args = NULL;
 #endif
 }
 
@@ -199,6 +200,29 @@ SystemExec::SystemExec (std::string command, const std::map<char, std::string> s
        init ();
        make_argp_escaped(command, subs);
 
+#ifdef PLATFORM_WINDOWS
+       if (argp[0] && strlen (argp[0]) > 0) {
+               std::string wa = argp[0];
+               // only add quotes to command if required..
+               if (argp[0][0] != '"'
+                               && argp[0][strlen(argp[0])-1] != '"'
+                               && strchr(argp[0], ' ')) {
+                       wa = "\"";
+                       wa += argp[0];
+                       wa += "\"";
+               }
+               // ...but always quote all args
+               for (int i = 1; argp[i]; ++i) {
+                       std::string tmp (argp[i]);
+                       while (tmp.find("\"") != std::string::npos)
+                               tmp.replace(tmp.find("\""), 1, "\\\"");
+                       wa += " \"";
+                       wa += tmp;
+                       wa += '"';
+               }
+               w_args = strdup(wa.c_str());
+       }
+#else
        if (find_file (Searchpath (Glib::getenv ("PATH")), argp[0], cmd)) {
                // argp[0] exists in $PATH` - set it to the actual path where it was found
                free (argp[0]);
@@ -208,7 +232,7 @@ SystemExec::SystemExec (std::string command, const std::map<char, std::string> s
 
        // Glib::find_program_in_path () is only available in Glib >= 2.28
        // cmd = Glib::find_program_in_path (argp[0]);
-
+#endif
        make_envp();
 }
 
@@ -311,6 +335,22 @@ interposer_thread (void *arg) {
        return 0;
 }
 
+string
+SystemExec::to_s () const
+{
+#ifdef PLATFORM_WINDOWS
+       return string (w_args ? w_args : "");
+#else
+       stringstream out;
+       if (argp) {
+               for (int i = 0; argp[i]; ++i) {
+                       out << argp[i] << " ";
+               }
+       }
+       return out.str();
+#endif
+}
+
 #ifdef PLATFORM_WINDOWS /* Windows Process */
 
 /* HELPER FUNCTIONS */
@@ -530,6 +570,7 @@ SystemExec::output_interposer()
                ReadStdout(data, bytesRead);/* EMIT SIGNAL */
        }
        Terminated();/* EMIT SIGNAL */
+       pthread_exit(0);
 }
 
 void
@@ -896,6 +937,7 @@ SystemExec::output_interposer()
                ReadStdout(rv, r);/* EMIT SIGNAL */
        }
        Terminated();/* EMIT SIGNAL */
+       pthread_exit(0);
 }
 
 void