libzypp  11.13.5
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 
110  ExternalProgram();
111 
112  ExternalProgram (const Arguments &argv,
113  Stderr_Disposition stderr_disp = Normal_Stderr,
114  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
115  const Pathname& root = "");
116 
117  ExternalProgram (const Arguments &argv, const Environment & environment,
118  Stderr_Disposition stderr_disp = Normal_Stderr,
119  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
120  const Pathname& root = "");
121 
122  ExternalProgram (const char *const *argv,
123  Stderr_Disposition stderr_disp = Normal_Stderr,
124  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
125  const Pathname& root = "");
126 
127  ExternalProgram (const char *const *argv, const Environment & environment,
128  Stderr_Disposition stderr_disp = Normal_Stderr,
129  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
130  const Pathname& root = "");
131 
132  ExternalProgram (const char *binpath, const char *const *argv_1,
133  bool use_pty = false);
134 
135 
136  ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
137  bool use_pty = false);
138 
139 
141 
143  int close();
144 
148  bool kill();
149 
153  bool running();
154 
158  pid_t getpid() { return pid; }
159 
161  const std::string & command() const
162  { return _command; }
163 
173  const std::string & execError() const
174  { return _execError; }
175 
179  static void renumber_fd (int origfd, int newfd);
180 
181  public:
182 
201  std::ostream & operator>>( std::ostream & out_r );
202 
203  protected:
204  int checkStatus( int );
205 
206  private:
207 
212  bool use_pty;
213 
214  pid_t pid;
217  std::string _command;
219  std::string _execError;
220 
221  void start_program (const char *const *argv, const Environment & environment,
222  Stderr_Disposition stderr_disp = Normal_Stderr,
223  int stderr_fd = -1, bool default_locale = false,
224  const char* root = NULL);
225 
226  };
227 
228 
229  namespace _ExternalProgram
230  {
236  struct EarlyPipe
237  {
238  enum { R=0, W=1 };
239  EarlyPipe();
240  ~EarlyPipe();
241  void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
242  FILE * stderr() { return _stderr; }
243  protected:
244  FILE * _stderr;
245  int _fds[2];
246  };
247  }
248 
253  {
254  public:
256  : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
257  { _initStdErr(); }
258 
259  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r )
260  : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
261  { _initStdErr(); }
262 
263  public:
266 
271  bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
272 
276  bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
277  { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
278 
279  private:
281  void _initStdErr()
282  { closeW(); }
283 
284  private:
285  std::string _buffer;
286  };
287 
288 } // namespace zypp
289 
290 #endif // ZYPP_EXTERNALPROGRAM_H