wibble  1.1
exec.h
Go to the documentation of this file.
1 #ifndef WIBBLE_SYS_EXEC_H
2 #define WIBBLE_SYS_EXEC_H
3 
4 /*
5  * OO wrapper for execve
6  *
7  * Copyright (C) 2003 Enrico Zini <enrico@debian.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
25 
26 namespace wibble {
27 namespace sys {
28 
33 class Exec : public ChildProcess
34 {
35 protected:
40  virtual int main();
41  virtual void spawnChild();
42 
43 public:
44  virtual ~Exec() {}
45 
52  std::string pathname;
53 
59  std::vector<std::string> args;
60 
64  std::vector<std::string> env;
65 
71 
79 
81  Exec(const std::string& pathname)
83  {
84  args.push_back(pathname);
85  }
86 
88  void importEnv();
89 
91  void exec();
92 };
93 
97 class ShellCommand : public Exec
98 {
99 public:
100  ShellCommand(const std::string& cmd) :
101 #ifdef POSIX
102  Exec("/bin/sh")
103 #elif defined _WIN32
104  Exec("bash") // let's hope for the best...
105 #endif
106  {
107  args.push_back("-c");
108  args.push_back(cmd);
109  searchInPath = false;
110  envFromParent = true;
111  }
112 };
113 
114 }
115 }
116 
117 // vim:set ts=4 sw=4:
118 #endif
Fork a child process.
Definition: childprocess.h:43
Execute external commands, either forked as a ChildProcess or directly using exec().
Definition: exec.h:34
std::vector< std::string > env
Custom environment for the child process, if envFromParent is false.
Definition: exec.h:64
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
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
std::string pathname
Filename or pathname of the program to execute.
Definition: exec.h:52
virtual ~Exec()
Definition: exec.h:44
void exec()
exec the program, never returning if all goes well
Definition: exec.cpp:60
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
bool searchInPath
Set to true if the file is to be searched in the current $PATH.
Definition: exec.h:78
std::vector< std::string > args
Arguments for the process to execute.
Definition: exec.h:59
void importEnv()
Import the current environment into env.
Definition: exec.cpp:43
Exec(const std::string &pathname)
Create a new object that will execute program ‘program’.
Definition: exec.h:81
Execute a shell command using /bin/sh -c.
Definition: exec.h:98
ShellCommand(const std::string &cmd)
Definition: exec.h:100
Definition: amorph.h:17