From 070bb7a8abcd787958b3ea07e77043dd404d653c Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Mon, 7 Oct 2013 21:16:42 +0100 Subject: Move system_exec from gtk2_ardour/ to libs/pbd/ --- libs/pbd/pbd/system_exec.h | 201 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 libs/pbd/pbd/system_exec.h (limited to 'libs/pbd/pbd/system_exec.h') diff --git a/libs/pbd/pbd/system_exec.h b/libs/pbd/pbd/system_exec.h new file mode 100644 index 0000000000..cf8518f547 --- /dev/null +++ b/libs/pbd/pbd/system_exec.h @@ -0,0 +1,201 @@ +/* + Copyright (C) 2010 Paul Davis + Author: Robin Gareus + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ +#ifndef __ardour_system_exec_h__ +#define __ardour_system_exec_h__ + +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif +#ifndef STDOUT_FILENO +#define STDOUT_FILENO 1 +#endif +#ifndef STDERR_FILENO +#define STDERR_FILENO 2 +#endif + +#include +#include +#include +#ifdef NOPBD /* outside ardour */ +#include +#include +#else +#include +#endif + +/** @class: SystemExec + * @brief execute an external command + * + * This class allows launche an external command-line application + * opening a full-duplex connection to its standard I/O. + * + * In Ardour context it is used to launch xjadeo and ffmpeg. + * + * The \ref write_to_stdin function provides for injecting data into STDIN + * of the child-application while output of the program to STDOUT/STDERR is + * forwarded using the \ref ReadStdout signal. + * \ref Terminated is sent if the child application exits. + * + */ +class SystemExec +{ + public: + /** prepare execution of a program with 'execve' + * + * This function takes over the existing environment variable and provides + * an easy way to speciy command-line arguments for the new process. + * + * Note: The argument parser does not interpret quotation-marks and splits + * arugments on whitespace. The argument string can be empty. + * The alternative constructor below allows to specify quoted parameters + * incl. whitespace. + * + * @param c program pathname that identifies the new process image file. + * @param a string of commandline-arguments to be passed to the new program. + */ + SystemExec (std::string c, std::string a = ""); + /** similar to \ref SystemExec but allows to specify custom arguments + * + * @param c program pathname that identifies the new process image file. + * @param a array of argument strings passed to the new program as 'argv'. + * it must be terminated by a null pointer (see the 'evecve' + * POSIX-C documentation for more information) + * The array must be dynamically allocated using malloc or strdup. + * Unless they're NULL, the array itself and each of its content + * memory is freed() in the destructor. + * + */ + SystemExec (std::string c, char ** a); + virtual ~SystemExec (); + + /** fork and execute the given program + * + * @param stderr_mode select what to do with program's standard error + * output: + * '0': keep STDERR; mix it with parent-process' STDERR + * '1': ignore STDERR of child-program + * '2': merge STDERR into STDOUT and send it with the + * ReadStdout signal. + * @return If the process is already running or was launched successfully + * the function returns zero (0). A negative number indicates an error. + */ + int start (int stderr_mode = 1); + /** kill running child-process + * + * if a child process exists trt to shut it down by closing its STDIN. + * if the program dies not react try SIGTERM and eventually SIGKILL + */ + void terminate (); + /** check if the child programm is (still) running. + * + * This function calls waitpid(WNOHANG) to check the state of the + * child-process. + * @return true if the program is (still) running. + */ + bool is_running (); + /** call the waitpid system-call with the pid of the child-program + * + * Basically what \ref terminate uses internally. + * + * This function is only useful if you want to control application + * termination yourself (eg timeouts or progress-dialog). + * @param option flags - see waitpid manual + * @return status info from waitpid call (not waitpid's return value) + * or -1 if the child-program is not running. + */ + int wait (int options=0); + /** closes both STDIN and STDOUT connections to/from + * the child-program. + * With the output-interposer thread gone, the program + * should terminate. + * used by \ref terminate() + */ + void close_stdin (); + /** write into child-program's STDIN + * @param d data to write + * @param len length of data to write, if it is 0 (zero), d.length() is + * used to determine the number of bytes to transmit. + * @return number of bytes written. + */ + int write_to_stdin (std::string d, size_t len=0); + + /** The ReadStdout signal is emitted when the application writes to STDOUT. + * it passes the written data and its length in bytes as arguments to the bound + * slot(s). + */ +#ifdef NOPBD /* outside ardour */ + sigc::signal ReadStdout; +#else + PBD::Signal2 ReadStdout; +#endif + + /** The Terminated signal is emitted when application terminates. */ +#ifdef NOPBD /* outside ardour */ + sigc::signal Terminated; +#else + PBD::Signal0 Terminated; +#endif + + /** interposer to emit signal for writes to STDOUT/ERR. + * + * Thread that reads the stdout of the forked + * process and signal-sends it to the main thread. + * It also emits the Terminated() signal once + * the the forked process closes it's stdout. + * + * Note: it's actually 'private' function but used + * by the internal pthread, which only has a pointer + * to this instance and thus can only access public fn. + */ + void output_interposer (); + + protected: + std::string cmd; ///< path to command - set when creating the class + int nicelevel; ///< process nice level - defaults to 0 + + void make_argp(std::string); + void make_envp(); + + char **argp; + char **envp; + + private: +#ifdef __WIN32__ + PROCESS_INFORMATION *pid; + HANDLE stdinP[2]; + HANDLE stdoutP[2]; + HANDLE stderrP[2]; + char *w_args; + void make_wargs(char **); +#else + pid_t pid; +#endif + pthread_mutex_t write_lock; + + int fdin; ///< file-descriptor for writing to child's STDIN. This variable is identical to pin[1] but also used as status check if the stdin pipe is open: <0 means closed. + int pok[2]; + int pin[2]; + int pout[2]; + + pthread_t thread_id_tt; + bool thread_active; +}; + +#endif /* __ardour_system_exec_h__ */ -- cgit v1.2.3 From e1562961c0bf87daa98417f2a660fbf8ebd04a35 Mon Sep 17 00:00:00 2001 From: Colin Fletcher Date: Thu, 10 Oct 2013 19:50:21 +0100 Subject: Add command-line parsing constructor to SystemExec Add a new constructor to SystemExec which takes a complete command-line and a map of substitutions for %, and parses the command line, taking account of some simple escape sequences and quoting. --- libs/pbd/pbd/system_exec.h | 22 +++++++++ libs/pbd/system_exec.cc | 113 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 121 insertions(+), 14 deletions(-) (limited to 'libs/pbd/pbd/system_exec.h') diff --git a/libs/pbd/pbd/system_exec.h b/libs/pbd/pbd/system_exec.h index cf8518f547..dc17ced2f6 100644 --- a/libs/pbd/pbd/system_exec.h +++ b/libs/pbd/pbd/system_exec.h @@ -33,6 +33,8 @@ #include #include #include +#include + #ifdef NOPBD /* outside ardour */ #include #include @@ -83,8 +85,26 @@ class SystemExec * */ SystemExec (std::string c, char ** a); + + /** similar to \ref SystemExec but expects a whole command line, and + * handles some simple escape sequences. + * + * @param command complete command-line to be executed + * @param subs a map of listing the % substitutions to + * be made. + * + * creates an argv array from the given command string, splitting into + * parameters at spaces. + * "\ " is non-splitting space, "\\" (and "\" at end of command) as "\", + * for "%", is looked up in subs and the corresponding string + * substituted. "%%" (and "%" at end of command) + * returns an argv array suitable for creating a new SystemExec with + */ + SystemExec (std::string command, const std::map subs); + virtual ~SystemExec (); + /** fork and execute the given program * * @param stderr_mode select what to do with program's standard error @@ -171,6 +191,7 @@ class SystemExec int nicelevel; ///< process nice level - defaults to 0 void make_argp(std::string); + void make_argp_escaped(std::string command, const std::map subs); void make_envp(); char **argp; @@ -187,6 +208,7 @@ class SystemExec #else pid_t pid; #endif + void init (); pthread_mutex_t write_lock; int fdin; ///< file-descriptor for writing to child's STDIN. This variable is identical to pin[1] but also used as status check if the stdin pipe is open: <0 means closed. diff --git a/libs/pbd/system_exec.cc b/libs/pbd/system_exec.cc index d26eedad6d..f270364660 100644 --- a/libs/pbd/system_exec.cc +++ b/libs/pbd/system_exec.cc @@ -144,9 +144,8 @@ static int close_allv(const int except_fds[]) { } #endif /* not on windows */ - -SystemExec::SystemExec (std::string c, std::string a) - : cmd(c) +void +SystemExec::init () { pthread_mutex_init(&write_lock, NULL); thread_active=false; @@ -154,12 +153,19 @@ SystemExec::SystemExec (std::string c, std::string a) pin[1] = -1; nicelevel = 0; envp = NULL; - argp = NULL; #ifdef __WIN32__ stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE; stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE; stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE; #endif +} + +SystemExec::SystemExec (std::string c, std::string a) + : cmd(c) +{ + init (); + + argp = NULL; make_envp(); make_argp(a); } @@ -167,21 +173,100 @@ SystemExec::SystemExec (std::string c, std::string a) SystemExec::SystemExec (std::string c, char **a) : cmd(c) , argp(a) { - pthread_mutex_init(&write_lock, NULL); - thread_active=false; - pid = 0; - pin[1] = -1; - nicelevel = 0; - envp = NULL; + init (); + #ifdef __WIN32__ - stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE; - stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE; - stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE; make_wargs(a); #endif make_envp(); } +SystemExec::SystemExec (std::string command, const std::map subs) +{ + init (); + make_argp_escaped(command, subs); + cmd = argp[0]; + // cmd = strdup(argp[0]); + make_envp(); +} + +void +SystemExec::make_argp_escaped(std::string command, const std::map subs) +{ + + int inquotes = 0; + int n = 0; + size_t i = 0; + std::string arg = ""; + + argp = (char **) malloc(sizeof(char *)); + + for (i = 0; i < command.length(); i++) { + char c = command.c_str()[i]; + if (inquotes) { + if (c == '"') { + inquotes = 0; + } else { + // still in quotes - just copy + arg += c; + } + } else switch (c) { + case '%' : + c = command.c_str()[++i]; + if (c == '%' || c == '\0') { + // "%%", "%" at end-of-string => "%" + arg += '%'; + } else { + // search subs for string to substitute for char + std::map::const_iterator s = subs.find(c); + if (s != subs.end()) { + // found substitution + arg += s->second; + } else { + // not a valid substitution, just copy + arg += '%'; + arg += c; + } + } + break; + case '\\': + c = command.c_str()[++i]; + switch (c) { + case ' ' : + case '"' : arg += c; break; // "\\", "\" at end-of-string => "\" + case '\0': + case '\\': arg += '\\'; break; + default : arg += '\\'; arg += c; break; + } + break; + case '"' : + inquotes = 1; + break; + case ' ' : + case '\t': + if (arg.length() > 0) { + // if there wasn't already a space or tab, start a new parameter + argp = (char **) realloc(argp, (n + 2) * sizeof(char *)); + argp[n++] = strdup (arg.c_str()); + arg = ""; + } + break; + default : + arg += c; + break; + } + } + argp[n] = NULL; + + char *p = argp[0]; + n = 0; + do { + std::cerr << "argv[" << n << "] == \"" << p << "\"" << std::endl; + p = argp[n++]; + } while (p); + +} + SystemExec::~SystemExec () { terminate (); @@ -502,7 +587,7 @@ SystemExec::make_argp(std::string args) { *cp2 = '\0'; argp[argn++] = strdup(cp1); cp1 = cp2 + 1; - argp = (char **) realloc(argp, (argn + 1) * sizeof(char *)); + argp = (char **) realloc(argp, (argn + 1) * sizeof(char *)); } } if (cp2 != cp1) { -- cgit v1.2.3