libshevek
process.hh
1 /* process.hh - fork/exec made userfriendly
2  * Copyright 2005-2008 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_PROCESS_HH
19 #define SHEVEK_PROCESS_HH
20 
21 #include <string>
22 #include "refbase.hh"
23 #include "fd.hh"
24 
25 namespace shevek
26 {
28  class process : public shevek::refbase
29  {
30  public:
32  static Glib::RefPtr <process> create (std::string const &command,
33  std::list <std::string> &argv,
34  bool pipe_stdin = true,
35  bool pipe_stdout = true,
36  bool pipe_stderr = true);
38  static Glib::RefPtr <process> create (std::string const &command,
39  std::list <std::string> &argv,
40  std::list <std::string> const &envp,
41  bool pipe_stdin = true,
42  bool pipe_stdout = true,
43  bool pipe_stderr = true);
45 
47  static Glib::RefPtr <process> shell (std::string const &command,
48  bool pipe_stdin = true,
49  bool pipe_stdout = true,
50  bool pipe_stderr = true,
51  std::string const &sh = "/bin/sh")
52  {
53  std::list <std::string> args;
54  args.push_back (sh);
55  args.push_back ("-c");
56  args.push_back (command);
57  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
58  }
60  Glib::RefPtr <shevek::fd> in ();
62  Glib::RefPtr <shevek::fd> out ();
64  Glib::RefPtr <shevek::fd> err ();
66  pid_t pid ();
68  ~process ();
70 
73  static std::string run (std::string const &command, std::string const &sh);
75  static Glib::RefPtr <process> create (std::string const &command,
76  bool pipe_stdin = true,
77  bool pipe_stdout = true,
78  bool pipe_stderr = true)
79  {
80  std::list <std::string> args;
81  args.push_back (command);
82  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
83  }
85  static Glib::RefPtr <process> create (std::string const &command,
86  std::string const &a1,
87  bool pipe_stdin = true,
88  bool pipe_stdout = true,
89  bool pipe_stderr = true)
90  {
91  std::list <std::string> args;
92  args.push_back (command);
93  args.push_back (a1);
94  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
95  }
97  static Glib::RefPtr <process> create (std::string const &command,
98  std::string const &a1,
99  std::string const &a2,
100  bool pipe_stdin = true,
101  bool pipe_stdout = true,
102  bool pipe_stderr = true)
103  {
104  std::list <std::string> args;
105  args.push_back (command);
106  args.push_back (a1);
107  args.push_back (a2);
108  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
109  }
111  static Glib::RefPtr <process> create (std::string const &command,
112  std::string const &a1,
113  std::string const &a2,
114  std::string const &a3,
115  bool pipe_stdin = true,
116  bool pipe_stdout = true,
117  bool pipe_stderr = true)
118  {
119  std::list <std::string> args;
120  args.push_back (command);
121  args.push_back (a1);
122  args.push_back (a2);
123  args.push_back (a3);
124  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
125  }
127  static Glib::RefPtr <process> create (std::string const &command,
128  std::string const &a1,
129  std::string const &a2,
130  std::string const &a3,
131  std::string const &a4,
132  bool pipe_stdin = true,
133  bool pipe_stdout = true,
134  bool pipe_stderr = true)
135  {
136  std::list <std::string> args;
137  args.push_back (command);
138  args.push_back (a1);
139  args.push_back (a2);
140  args.push_back (a3);
141  args.push_back (a4);
142  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
143  }
145  static Glib::RefPtr <process> create (std::string const &command,
146  std::string const &a1,
147  std::string const &a2,
148  std::string const &a3,
149  std::string const &a4,
150  std::string const &a5,
151  bool pipe_stdin = true,
152  bool pipe_stdout = true,
153  bool pipe_stderr = true)
154  {
155  std::list <std::string> args;
156  args.push_back (command);
157  args.push_back (a1);
158  args.push_back (a2);
159  args.push_back (a3);
160  args.push_back (a4);
161  args.push_back (a5);
162  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
163  }
165  static Glib::RefPtr <process> create (std::string const &command,
166  std::string const &a1,
167  std::string const &a2,
168  std::string const &a3,
169  std::string const &a4,
170  std::string const &a5,
171  std::string const &a6,
172  bool pipe_stdin = true,
173  bool pipe_stdout = true,
174  bool pipe_stderr = true)
175  {
176  std::list <std::string> args;
177  args.push_back (command);
178  args.push_back (a1);
179  args.push_back (a2);
180  args.push_back (a3);
181  args.push_back (a4);
182  args.push_back (a5);
183  args.push_back (a6);
184  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
185  }
187  static Glib::RefPtr <process> create (std::string const &command,
188  std::string const &a1,
189  std::string const &a2,
190  std::string const &a3,
191  std::string const &a4,
192  std::string const &a5,
193  std::string const &a6,
194  std::string const &a7,
195  bool pipe_stdin = true,
196  bool pipe_stdout = true,
197  bool pipe_stderr = true)
198  {
199  std::list <std::string> args;
200  args.push_back (command);
201  args.push_back (a1);
202  args.push_back (a2);
203  args.push_back (a3);
204  args.push_back (a4);
205  args.push_back (a5);
206  args.push_back (a6);
207  args.push_back (a7);
208  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
209  }
211  static Glib::RefPtr <process> create (std::string const &command,
212  std::string const &a1,
213  std::string const &a2,
214  std::string const &a3,
215  std::string const &a4,
216  std::string const &a5,
217  std::string const &a6,
218  std::string const &a7,
219  std::string const &a8,
220  bool pipe_stdin = true,
221  bool pipe_stdout = true,
222  bool pipe_stderr = true)
223  {
224  std::list <std::string> args;
225  args.push_back (command);
226  args.push_back (a1);
227  args.push_back (a2);
228  args.push_back (a3);
229  args.push_back (a4);
230  args.push_back (a5);
231  args.push_back (a6);
232  args.push_back (a7);
233  args.push_back (a8);
234  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
235  }
237  static Glib::RefPtr <process> create (std::string const &command,
238  std::string const &a1,
239  std::string const &a2,
240  std::string const &a3,
241  std::string const &a4,
242  std::string const &a5,
243  std::string const &a6,
244  std::string const &a7,
245  std::string const &a8,
246  std::string const &a9,
247  bool pipe_stdin = true,
248  bool pipe_stdout = true,
249  bool pipe_stderr = true)
250  {
251  std::list <std::string> args;
252  args.push_back (command);
253  args.push_back (a1);
254  args.push_back (a2);
255  args.push_back (a3);
256  args.push_back (a4);
257  args.push_back (a5);
258  args.push_back (a6);
259  args.push_back (a7);
260  args.push_back (a8);
261  args.push_back (a9);
262  return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr);
263  }
264  private:
265  Glib::RefPtr <shevek::fd> m_in, m_out, m_err;
266  pid_t m_pid;
267  process (std::string const &command, char **argv, char **envp, bool pipe_stdin, bool pipe_stdout, bool pipe_stderr);
268  static char **make_pointers (std::list <std::string> const &source);
269  static void clean (char **pointers);
270  static char *dup (char const *str);
271  };
272 }
273 
274 #endif
Glib::RefPtr< shevek::fd > in()
The standard input pipe, if it was requested.
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
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
Glib::RefPtr< shevek::fd > out()
The standard output pipe, if it was requested.
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
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
Definition: args.hh:52
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
pid_t pid()
The process ID.
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
static std::string run(std::string const &command, std::string const &sh)
Run a process and return its output.
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.
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
Create a process, optionally connection its standard in- and output streams to the calling program...
Definition: process.hh:28
Glib::RefPtr< shevek::fd > err()
The standard error pipe, if it was requested.
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
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
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
~process()
The destructor. This kills the process if it was still running.
Commandline and configuration file parsing helper.
Definition: args.hh:65
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
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