exec.h
Go to the documentation of this file.
#ifndef WIBBLE_SYS_EXEC_H
#define WIBBLE_SYS_EXEC_H
/*
* OO wrapper for execve
*
* Copyright (C) 2003 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/sys/childprocess.h>
namespace wibble {
namespace sys {
class Exec : public ChildProcess
{
protected:
virtual int main();
virtual void spawnChild();
public:
virtual ~Exec() {}
std::string pathname;
std::vector<std::string> args;
std::vector<std::string> env;
bool envFromParent;
bool searchInPath;
Exec(const std::string& pathname)
: pathname(pathname), envFromParent(true), searchInPath(false)
{
args.push_back(pathname);
}
void importEnv();
void exec();
};
class ShellCommand : public Exec
{
public:
ShellCommand(const std::string& cmd) :
#ifdef POSIX
Exec("/bin/sh")
#elif defined _WIN32
Exec("bash") // let's hope for the best...
#endif
{
args.push_back("-c");
args.push_back(cmd);
searchInPath = false;
envFromParent = true;
}
};
}
}
// vim:set ts=4 sw=4:
#endif
childprocess.h
wibble::sys::ChildProcess
Fork a child process.
Definition: childprocess.h:43
wibble::sys::Exec
Execute external commands, either forked as a ChildProcess or directly using exec().
Definition: exec.h:34
wibble::sys::Exec::env
std::vector< std::string > env
Custom environment for the child process, if envFromParent is false.
Definition: exec.h:64
wibble::sys::Exec::main
virtual int main()
Used to run the program as a child process, if one of the ChildProcess::fork functions is called.
Definition: exec.cpp:33
wibble::sys::Exec::spawnChild
virtual void spawnChild()
On Windows, it's impossible to fork(), but if you were to fork+exec, it's not all lost.
Definition: exec.cpp:49
wibble::sys::Exec::pathname
std::string pathname
Filename or pathname of the program to execute.
Definition: exec.h:52
wibble::sys::Exec::~Exec
virtual ~Exec()
Definition: exec.h:44
wibble::sys::Exec::exec
void exec()
exec the program, never returning if all goes well
Definition: exec.cpp:60
wibble::sys::Exec::envFromParent
bool envFromParent
True if the environment is to be taken from the parent, false if it is explicitly provided in env.
Definition: exec.h:70
wibble::sys::Exec::searchInPath
bool searchInPath
Set to true if the file is to be searched in the current $PATH.
Definition: exec.h:78
wibble::sys::Exec::args
std::vector< std::string > args
Arguments for the process to execute.
Definition: exec.h:59
wibble::sys::Exec::importEnv
void importEnv()
Import the current environment into env.
Definition: exec.cpp:43
wibble::sys::Exec::Exec
Exec(const std::string &pathname)
Create a new object that will execute program ‘program’.
Definition: exec.h:81
wibble::sys::ShellCommand
Execute a shell command using /bin/sh -c.
Definition: exec.h:98
wibble::sys::ShellCommand::ShellCommand
ShellCommand(const std::string &cmd)
Definition: exec.h:100
wibble
Definition: amorph.h:17