libzypp  12.16.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 
113  ExternalProgram();
114 
115  ExternalProgram (const Arguments &argv,
116  Stderr_Disposition stderr_disp = Normal_Stderr,
117  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
118  const Pathname& root = "");
119 
120  ExternalProgram (const Arguments &argv, const Environment & environment,
121  Stderr_Disposition stderr_disp = Normal_Stderr,
122  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
123  const Pathname& root = "");
124 
125  ExternalProgram (const char *const *argv,
126  Stderr_Disposition stderr_disp = Normal_Stderr,
127  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
128  const Pathname& root = "");
129 
130  ExternalProgram (const char *const *argv, const Environment & environment,
131  Stderr_Disposition stderr_disp = Normal_Stderr,
132  bool use_pty = false, int stderr_fd = -1, bool default_locale = false,
133  const Pathname& root = "");
134 
135  ExternalProgram (const char *binpath, const char *const *argv_1,
136  bool use_pty = false);
137 
138 
139  ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment,
140  bool use_pty = false);
141 
142 
144 
146  int close();
147 
151  bool kill();
152 
156  bool running();
157 
161  pid_t getpid() { return pid; }
162 
164  const std::string & command() const
165  { return _command; }
166 
176  const std::string & execError() const
177  { return _execError; }
178 
182  static void renumber_fd (int origfd, int newfd);
183 
184  public:
185 
204  std::ostream & operator>>( std::ostream & out_r );
205 
206  protected:
207  int checkStatus( int );
208 
209  private:
210 
215  bool use_pty;
216 
217  pid_t pid;
220  std::string _command;
222  std::string _execError;
223 
224  void start_program (const char *const *argv, const Environment & environment,
225  Stderr_Disposition stderr_disp = Normal_Stderr,
226  int stderr_fd = -1, bool default_locale = false,
227  const char* root = NULL);
228 
229  };
230 
231 
232  namespace _ExternalProgram
233  {
239  struct EarlyPipe
240  {
241  enum { R=0, W=1 };
242  EarlyPipe();
243  ~EarlyPipe();
244  void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } }
245  FILE * stderr() { return _stderr; }
246  protected:
247  FILE * _stderr;
248  int _fds[2];
249  };
250  }
251 
256  {
257  public:
259  : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
260  { _initStdErr(); }
261 
262  ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r )
263  : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] )
264  { _initStdErr(); }
265 
266  public:
269 
274  bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false );
275 
279  bool stderrGetline( std::string & retval_r, bool returnDelim_r = false )
280  { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); }
281 
282  private:
284  void _initStdErr()
285  { closeW(); }
286 
287  private:
288  std::string _buffer;
289  };
290 
291 } // namespace zypp
292 
293 #endif // ZYPP_EXTERNALPROGRAM_H