+
+void Application::add_option(const std::string& arg)
+{
+ _options.push_back(std::make_pair(arg, std::string()));
+}
+
+void Application::add_option(const std::string& name, const std::string& value)
+{
+ _options.push_back(std::make_pair(name, value));
+}
+
+Application::Pipe::Pipe()
+{
+ _fd[0]= -1;
+ _fd[1]= -1;
+ _open[0]= false;
+ _open[1]= false;
+}
+
+void Application::Pipe::reset()
+{
+ close(READ);
+ close(WRITE);
+
+ int ret;
+ if ((ret= pipe(_fd)) < 0)
+ {
+ throw strerror(ret);
+ }
+ _open[0]= true;
+ _open[1]= true;
+
+ {
+ ret= fcntl(_fd[0], F_GETFL, 0);
+ if (ret == -1)
+ {
+ Error << "fcntl(F_GETFL) " << strerror(ret);
+ throw strerror(ret);
+ }
+
+ ret= fcntl(_fd[0], F_SETFL, ret | O_NONBLOCK);
+ if (ret == -1)
+ {
+ Error << "fcntl(F_SETFL) " << strerror(ret);
+ throw strerror(ret);
+ }
+ }
+}
+
+Application::Pipe::~Pipe()
+{
+ close(READ);
+ close(WRITE);
+}
+
+void Application::Pipe::dup_for_spawn(const close_t& arg, posix_spawn_file_actions_t& file_actions, const int newfildes)
+{
+ int type= int(arg);
+
+ int ret;
+ if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _fd[type], newfildes )) < 0)
+ {
+ Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
+ throw strerror(ret);
+ }
+
+ if ((ret= posix_spawn_file_actions_addclose(&file_actions, _fd[type])) < 0)
+ {
+ Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
+ throw strerror(ret);
+ }
+}
+
+void Application::Pipe::close(const close_t& arg)
+{
+ int type= int(arg);
+
+ if (_open[type])
+ {
+ int ret;
+ if ((ret= ::close(_fd[type])) < 0)
+ {
+ Error << "close(" << strerror(ret) << ")";
+ }
+ _open[type]= false;
+ }
+}
+
+void Application::create_argv(const char *args[])
+{
+ _argc= 2 +_use_libtool ? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
+
+ if (_use_libtool)
+ {
+ _argc+= 2; // +2 for libtool --mode=execute
+ }
+
+ /*
+ valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
+ */
+ if (_use_valgrind)
+ {
+ _argc+= 7;
+ }
+ else if (_use_gdb) // gdb
+ {
+ _argc+= 1;
+ }
+
+ for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
+ {
+ _argc++;
+ if ((*iter).second.empty() == false)
+ {
+ _argc++;
+ }
+ }
+
+ if (args)
+ {
+ for (const char **ptr= args; *ptr; ++ptr)
+ {
+ _argc++;
+ }
+ }
+
+ delete_argv();
+ built_argv= new char * [_argc];
+
+ size_t x= 0;
+ if (_use_libtool)
+ {
+ assert(libtool());
+ built_argv[x++]= strdup(libtool());
+ built_argv[x++]= strdup("--mode=execute");
+ }
+
+ if (_use_valgrind)
+ {
+ /*
+ valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
+ */
+ built_argv[x++]= strdup("valgrind");
+ built_argv[x++]= strdup("--error-exitcode=1");
+ built_argv[x++]= strdup("--leak-check=yes");
+ built_argv[x++]= strdup("--show-reachable=yes");
+ built_argv[x++]= strdup("--track-fds=yes");
+ built_argv[x++]= strdup("--malloc-fill=A5");
+ built_argv[x++]= strdup("--free-fill=DE");
+ }
+ else if (_use_gdb)
+ {
+ built_argv[x++]= strdup("gdb");
+ }
+
+ built_argv[x++]= strdup(_exectuble_with_path.c_str());
+
+ for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
+ {
+ built_argv[x++]= strdup((*iter).first.c_str());
+ if ((*iter).second.empty() == false)
+ {
+ built_argv[x++]= strdup((*iter).second.c_str());
+ }
+ }
+
+ if (args)
+ {
+ for (const char **ptr= args; *ptr; ++ptr)
+ {
+ built_argv[x++]= strdup(*ptr);
+ }
+ }
+ built_argv[_argc -1]= NULL;
+}
+
+std::string Application::print()
+{
+ return print_argv(built_argv, _argc);
+}
+
+std::string Application::arguments()
+{
+ std::stringstream arg_buffer;
+
+ for (size_t x= 1 + _use_libtool ? 2 : 0;
+ x < _argc and built_argv[x];
+ x++)
+ {
+ arg_buffer << built_argv[x] << " ";
+ }
+
+ return arg_buffer.str();
+}
+
+void Application::delete_argv()
+{
+ if (built_argv == NULL)
+ {
+ return;
+ }
+
+ for (size_t x= 0; x < _argc; x++)
+ {
+ if (built_argv[x])
+ {
+ ::free(built_argv[x]);
+ }
+ }
+ delete[] built_argv;
+ built_argv= NULL;
+ _argc= 0;
+}
+
+
+int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
+{
+ Application app(command, use_libtool);
+
+ Application::error_t ret= app.run(args);
+
+ if (ret != Application::SUCCESS)
+ {
+ return int(ret);
+ }
+
+ return int(app.wait());
+}
+
+const char *gearmand_binary()
+{
+ return GEARMAND_BINARY;
+}
+
+} // namespace exec_cmdline