00001
00002
00003
00004 #ifndef _SystemCmd_h
00005 #define _SystemCmd_h
00006
00007 #include <sys/poll.h>
00008 #include <stdio.h>
00009
00010 #include <string>
00011 #include <vector>
00012 #include <list>
00013
00014 using std::string;
00015
00016 namespace storage
00017 {
00018
00019 class OutputProcessor;
00020
00021 class SystemCmd
00022 {
00023 public:
00024 enum OutputStream { IDX_STDOUT, IDX_STDERR };
00025 SystemCmd( const char* Command_Cv );
00026 SystemCmd( const string& Command_Cv );
00027 SystemCmd();
00028 virtual ~SystemCmd();
00029 int execute( const string& Command_Cv );
00030 int executeBackground( const string& Command_Cv );
00031 int executeRestricted( const string& Command_Cv,
00032 unsigned long MaxTimeSec,
00033 unsigned long MaxLineOut,
00034 bool& ExceedTime, bool& ExceedLines);
00035 void setOutputHandler( void (*Handle_f)( void *, string, bool ),
00036 void * Par_p );
00037 void logOutput();
00038 void setOutputProcessor( OutputProcessor * proc )
00039 { output_proc = proc; }
00040 int select( string Reg_Cv, bool Invert_bv=false,
00041 unsigned Idx_ii=IDX_STDOUT );
00042 const string& stderr() const { return( *getString(IDX_STDERR)); }
00043 const string& stdout() const { return( *getString(IDX_STDOUT)); }
00044 const string& cmd() const { return( lastCmd ); }
00045 const string* getString( unsigned Idx_ii=IDX_STDOUT ) const;
00046 const string* getLine( unsigned Num_iv, bool Selected_bv=false,
00047 unsigned Idx_ii=IDX_STDOUT ) const;
00048 unsigned numLines( bool Selected_bv=false, unsigned Idx_ii=IDX_STDOUT ) const;
00049 void setCombine( const bool Combine_b=true );
00050 int retcode() const { return Ret_i; }
00051
00052 int getStdout( std::vector<string> &Ret_Cr, const bool Append_bv = false ) const
00053 { return placeOutput( IDX_STDOUT, Ret_Cr, Append_bv); }
00054 int getStderr( std::vector<string> &Ret_Cr, const bool Append_bv = false ) const
00055 { return placeOutput( IDX_STDERR, Ret_Cr, Append_bv); }
00056 int getStdout( std::list<string> &Ret_Cr, const bool Append_bv = false ) const
00057 { return placeOutput( IDX_STDOUT, Ret_Cr, Append_bv); }
00058 int getStderr( std::list<string> &Ret_Cr, const bool Append_bv = false ) const
00059 { return placeOutput( IDX_STDERR, Ret_Cr, Append_bv); }
00060
00061 protected:
00062
00063 int placeOutput( unsigned Which_iv, std::vector<string> &Ret_Cr, const bool Append_bv ) const;
00064 int placeOutput( unsigned Which_iv, std::list<string> &Ret_Cr, const bool Append_bv ) const;
00065
00066 void invalidate();
00067 void closeOpenFds();
00068 int doExecute( string Cmd_Cv );
00069 bool doWait( bool Hang_bv, int& Ret_ir );
00070 void checkOutput();
00071 void getUntilEOF( FILE* File_Cr, std::vector<string>& Lines_Cr,
00072 bool& NewLineSeen_br, bool Stderr_bv );
00073 void extractNewline( const char* Buf_ti, int Cnt_ii, bool& NewLineSeen_br,
00074 string& Text_Cr, std::vector<string>& Lines_Cr );
00075 void addLine( string Text_Cv, std::vector<string>& Lines_Cr );
00076 void init();
00077
00078 mutable string Text_aC[2];
00079 mutable bool Valid_ab[2];
00080 FILE* File_aC[2];
00081 std::vector<string> Lines_aC[2];
00082 std::vector<string*> SelLines_aC[2];
00083 bool NewLineSeen_ab[2];
00084 bool Combine_b;
00085 bool Background_b;
00086 string lastCmd;
00087 int Ret_i;
00088 int Pid_i;
00089 void (* OutputHandler_f)( void*, string, bool );
00090 void *HandlerPar_p;
00091 OutputProcessor* output_proc;
00092 struct pollfd pfds[2];
00093 static int Nr_i;
00094 };
00095
00096 }
00097
00098 #endif