libzypp 8.13.6
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00013 #ifndef ZYPP_EXTERNALPROGRAM_H 00014 #define ZYPP_EXTERNALPROGRAM_H 00015 00016 #include <map> 00017 #include <string> 00018 #include <vector> 00019 00020 #include "zypp/base/ExternalDataSource.h" 00021 #include "zypp/Pathname.h" 00022 00023 namespace zypp { 00024 00056 class ExternalProgram : public zypp::externalprogram::ExternalDataSource 00057 { 00058 00059 public: 00060 00061 typedef std::vector<std::string> Arguments; 00062 00067 enum Stderr_Disposition { 00068 Normal_Stderr, 00069 Discard_Stderr, 00070 Stderr_To_Stdout, 00071 Stderr_To_FileDesc 00072 }; 00073 00074 00078 typedef std::map<std::string,std::string> Environment; 00079 00088 ExternalProgram (std::string commandline, 00089 Stderr_Disposition stderr_disp = Normal_Stderr, 00090 bool use_pty = false, int stderr_fd = -1, bool default_locale = false, 00091 const Pathname& root = ""); 00092 00108 ExternalProgram(); 00109 00110 ExternalProgram (const Arguments &argv, 00111 Stderr_Disposition stderr_disp = Normal_Stderr, 00112 bool use_pty = false, int stderr_fd = -1, bool default_locale = false, 00113 const Pathname& root = ""); 00114 00115 ExternalProgram (const Arguments &argv, const Environment & environment, 00116 Stderr_Disposition stderr_disp = Normal_Stderr, 00117 bool use_pty = false, int stderr_fd = -1, bool default_locale = false, 00118 const Pathname& root = ""); 00119 00120 ExternalProgram (const char *const *argv, 00121 Stderr_Disposition stderr_disp = Normal_Stderr, 00122 bool use_pty = false, int stderr_fd = -1, bool default_locale = false, 00123 const Pathname& root = ""); 00124 00125 ExternalProgram (const char *const *argv, const Environment & environment, 00126 Stderr_Disposition stderr_disp = Normal_Stderr, 00127 bool use_pty = false, int stderr_fd = -1, bool default_locale = false, 00128 const Pathname& root = ""); 00129 00130 ExternalProgram (const char *binpath, const char *const *argv_1, 00131 bool use_pty = false); 00132 00133 00134 ExternalProgram (const char *binpath, const char *const *argv_1, const Environment & environment, 00135 bool use_pty = false); 00136 00137 00138 ~ExternalProgram(); 00139 00141 int close(); 00142 00146 bool kill(); 00147 00151 bool running(); 00152 00156 pid_t getpid() { return pid; } 00157 00159 const std::string & command() const 00160 { return _command; } 00161 00171 const std::string & execError() const 00172 { return _execError; } 00173 00177 static void renumber_fd (int origfd, int newfd); 00178 00179 public: 00180 00199 std::ostream & operator>>( std::ostream & out_r ); 00200 00201 protected: 00202 int checkStatus( int ); 00203 00204 private: 00205 00210 bool use_pty; 00211 00212 pid_t pid; 00213 int _exitStatus; 00215 std::string _command; 00217 std::string _execError; 00218 00219 void start_program (const char *const *argv, const Environment & environment, 00220 Stderr_Disposition stderr_disp = Normal_Stderr, 00221 int stderr_fd = -1, bool default_locale = false, 00222 const char* root = NULL); 00223 00224 }; 00225 00226 00227 namespace _ExternalProgram 00228 { 00234 struct EarlyPipe 00235 { 00236 enum { R=0, W=1 }; 00237 EarlyPipe(); 00238 ~EarlyPipe(); 00239 void closeW() { if ( _fds[W] != -1 ) { ::close( _fds[W] ); _fds[W] = -1; } } 00240 FILE * stderr() { return _stderr; } 00241 protected: 00242 FILE * _stderr; 00243 int _fds[2]; 00244 }; 00245 } 00246 00250 class ExternalProgramWithStderr : private _ExternalProgram::EarlyPipe, public ExternalProgram 00251 { 00252 public: 00253 ExternalProgramWithStderr( const Arguments & argv_r ) 00254 : ExternalProgram( argv_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] ) 00255 { _initStdErr(); } 00256 00257 ExternalProgramWithStderr( const Arguments & argv_r, const Environment & environment_r ) 00258 : ExternalProgram( argv_r, environment_r, Stderr_To_FileDesc, /*use_pty*/false, _fds[W] ) 00259 { _initStdErr(); } 00260 00261 public: 00263 _ExternalProgram::EarlyPipe::stderr; 00264 00269 bool stderrGetUpTo( std::string & retval_r, const char delim_r, bool returnDelim_r = false ); 00270 00274 bool stderrGetline( std::string & retval_r, bool returnDelim_r = false ) 00275 { return stderrGetUpTo( retval_r, '\n', returnDelim_r ); } 00276 00277 private: 00279 void _initStdErr() 00280 { closeW(); } 00281 00282 private: 00283 std::string _buffer; 00284 }; 00285 00286 } // namespace zypp 00287 00288 #endif // ZYPP_EXTERNALPROGRAM_H