libzypp  15.28.6
ExternalProgram.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #ifndef ZYPP_EXTERNALPROGRAM_H
14 #define ZYPP_EXTERNALPROGRAM_H
15 
16 #include <unistd.h>
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
23 #include "zypp/Pathname.h"
24 
25 namespace zypp {
26 
59  {
60 
61  public:
62 
63  typedef std::vector<std::string> Arguments;
64 
74  };
75 
76 
80  typedef std::map<std::string,std::string> Environment;
81 
90  ExternalProgram (std::string commandline,
91  Stderr_Disposition stderr_disp = Normal_Stderr,
92  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
93  const Pathname& root = "");
94 
117  ExternalProgram();
118 
119  ExternalProgram (const Arguments &argv,
120  Stderr_Disposition stderr_disp = Normal_Stderr,
121  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
122  const Pathname& root = "");
123 
124  ExternalProgram (const Arguments &argv, const Environment & environment,
125  Stderr_Disposition stderr_disp = Normal_Stderr,
126  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
127  const Pathname& root = "");
128 
129  ExternalProgram (const char *const *argv,
130  Stderr_Disposition stderr_disp = Normal_Stderr,
131  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
132  const Pathname& root = "");
133 
134  ExternalProgram (const char *const *argv, const Environment & environment,
135  Stderr_Disposition stderr_disp = Normal_Stderr,
136  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
137  const Pathname& root = "");
138 
139  ExternalProgram (const char *binpath, const char *const *argv_1,
140  bool use_pty = false);
141 
142 
143  ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
144  bool use_pty = false);
145 
146 
148 
150  int close();
151 
155  bool kill();
156 
160  bool running();
161 
165  pid_t getpid() { return pid; }
166 
168  const std::string & command() const
169  { return _command; }
170 
180  const std::string & execError() const
181  { return _execError; }
182 
186  static void renumber_fd (int origfd, int newfd);
187 
188  public:
189 
208  std::ostream & operator>>( std::ostream & out_r );
209 
210  protected:
211  int checkStatus( int );
212 
213  private:
214 
219  bool use_pty;
220 
221  pid_t pid;
224  std::string _command;
226  std::string _execError;
227 
228  void start_program (const char *const *argv, const Environment & environment,
229  Stderr_Disposition stderr_disp = Normal_Stderr,
230  int stderr_fd = -1, bool default_locale = false,
231  const char* root = NULL);
232 
233  };
234 
235 
236  namespace externalprogram
237  {
243  struct EarlyPipe
244  {
245  enum { R=0, W=1 };
246  EarlyPipe();
247  ~EarlyPipe();
248  void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
249  FILE * stderr() { return _stderr; }
250  protected:
251  FILE * _stderr;
252  int _fds[2];
253  };
254  } // namespace externalprogram
255 
260  {
261  public:
263  : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
264  { _initStdErr(); }
265 
266  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r )
267  : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
268  { _initStdErr(); }
269 
270  public:
273 
278  bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
279 
283  bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
284  { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
285 
286  private:
288  void _initStdErr()
289  { closeW(); }
290 
291  private:
292  std::string _buffer;
293  };
294 
295 } // namespace zypp
296 
297 #endif // ZYPP_EXTERNALPROGRAM_H
ExternalProgram()
Start an external program by giving the arguments as an arry of char *pointers.
Bidirectional stream to external data.
bool use_pty
Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
std::ostream & operator>>(std::ostream &out_r)
Redirect all command output to an ostream.
bool kill()
Kill the program.
void _initStdErr()
Close write end of the pipe (childs end).
ExternalProgram extended to offer reading programs stderr.
const std::string & command() const
The command we're executing.
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
void start_program(const char *const *argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const char *root=NULL)
bool running()
Return whether program is running.
std::vector< std::string > Arguments
ExternalProgramWithStderr(const Arguments &argv_r)
bool stderrGetline(std::string &retval_r, bool returnDelim_r=false)
Read next complete line from stderr (nonblocking).
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
ExternalProgramWithStderr(const Arguments &argv_r, const Environment &environment_r)
Stderr_Disposition
Define symbols for different policies on the handling of stderr.
std::map< std::string, std::string > Environment
For passing additional environment variables to set.
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
std::string _execError
Remember execution errors like failed fork/exec.
int close()
Wait for the progamm to complete.
std::string _command
Store the command we're executing.
static void renumber_fd(int origfd, int newfd)
origfd will be accessible as newfd and closed (unless they were equal)
pid_t getpid()
return pid
Helper providing pipe FDs for ExternalProgramWithStderr.