process.hh
/* process.hh - fork/exec made userfriendly
* Copyright 2005-2008 Bas Wijnen <wijnen@debian.org>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_PROCESS_HH
#define SHEVEK_PROCESS_HH
#include <string>
#include "refbase.hh"
#include "fd.hh"
namespace shevek
{
class process : public shevek::refbase
{
public:
static Glib::RefPtr <process> create (std::string const &command,
std::list <std::string> &argv,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true);
static Glib::RefPtr <process> create (std::string const &command,
std::list <std::string> &argv,
std::list <std::string> const &envp,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true);
static Glib::RefPtr <process> shell (std::string const &command,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true,
std::string const &sh = "/bin/sh")
{
std::list <std::string> args;
args.push_back (sh);
args.push_back ("-c");
args.push_back (command);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
Glib::RefPtr <shevek::fd> in ();
Glib::RefPtr <shevek::fd> out ();
Glib::RefPtr <shevek::fd> err ();
pid_t pid ();
~process ();
static std::string run (std::string const &command, std::string const &sh);
static Glib::RefPtr <process> create (std::string const &command,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
std::string const &a5,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
args.push_back (a5);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
std::string const &a5,
std::string const &a6,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
args.push_back (a5);
args.push_back (a6);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
std::string const &a5,
std::string const &a6,
std::string const &a7,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
args.push_back (a5);
args.push_back (a6);
args.push_back (a7);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
std::string const &a5,
std::string const &a6,
std::string const &a7,
std::string const &a8,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
args.push_back (a5);
args.push_back (a6);
args.push_back (a7);
args.push_back (a8);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
static Glib::RefPtr <process> create (std::string const &command,
std::string const &a1,
std::string const &a2,
std::string const &a3,
std::string const &a4,
std::string const &a5,
std::string const &a6,
std::string const &a7,
std::string const &a8,
std::string const &a9,
bool pipe_stdin = true,
bool pipe_stdout = true,
bool pipe_stderr = true)
{
std::list <std::string> args;
args.push_back (command);
args.push_back (a1);
args.push_back (a2);
args.push_back (a3);
args.push_back (a4);
args.push_back (a5);
args.push_back (a6);
args.push_back (a7);
args.push_back (a8);
args.push_back (a9);
return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
}
private:
Glib::RefPtr <shevek::fd> m_in, m_out, m_err;
pid_t m_pid;
process (std::string const &command, char **argv, char **envp, bool pipe_stdin, bool pipe_stdout, bool pipe_stderr);
static char **make_pointers (std::list <std::string> const &source);
static void clean (char **pointers);
static char *dup (char const *str);
};
}
#endif
shevek::process::in
Glib::RefPtr< shevek::fd > in()
The standard input pipe, if it was requested.
shevek::refbase
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with three arguments.
Definition: process.hh:111
shevek::process::out
Glib::RefPtr< shevek::fd > out()
The standard output pipe, if it was requested.
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process without arguments.
Definition: process.hh:75
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, std::string const &a5, std::string const &a6, std::string const &a7, std::string const &a8, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with eight arguments.
Definition: process.hh:211
shevek
Definition: args.hh:52
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with one argument.
Definition: process.hh:85
shevek::process::pid
pid_t pid()
The process ID.
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, std::string const &a5, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with five arguments.
Definition: process.hh:145
shevek::process::run
static std::string run(std::string const &command, std::string const &sh)
Run a process and return its output.
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::list< std::string > &argv, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process from a filename and an argument list.
shevek::process::shell
static Glib::RefPtr< process > shell(std::string const &command, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true, std::string const &sh="/bin/sh")
Run a string with the shell.
Definition: process.hh:47
shevek::process
Create a process, optionally connection its standard in- and output streams to the calling program...
Definition: process.hh:28
shevek::process::err
Glib::RefPtr< shevek::fd > err()
The standard error pipe, if it was requested.
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, std::string const &a5, std::string const &a6, std::string const &a7, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with seven arguments.
Definition: process.hh:187
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, std::string const &a5, std::string const &a6, std::string const &a7, std::string const &a8, std::string const &a9, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with nine arguments.
Definition: process.hh:237
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, std::string const &a5, std::string const &a6, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with six arguments.
Definition: process.hh:165
shevek::process::~process
~process()
The destructor. This kills the process if it was still running.
shevek::args
Commandline and configuration file parsing helper.
Definition: args.hh:65
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, std::string const &a3, std::string const &a4, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with four arguments.
Definition: process.hh:127
shevek::process::create
static Glib::RefPtr< process > create(std::string const &command, std::string const &a1, std::string const &a2, bool pipe_stdin=true, bool pipe_stdout=true, bool pipe_stderr=true)
Create a process with two arguments.
Definition: process.hh:97