00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef SYSTEM_CMD_H
00024 #define SYSTEM_CMD_H
00025
00026 #include <sys/poll.h>
00027 #include <stdio.h>
00028
00029 #include <string>
00030 #include <vector>
00031 #include <list>
00032
00033 using std::string;
00034
00035 namespace storage
00036 {
00037
00038 class OutputProcessor;
00039
00040 class SystemCmd
00041 {
00042 public:
00043 enum OutputStream { IDX_STDOUT, IDX_STDERR };
00044 SystemCmd( const char* Command_Cv );
00045 SystemCmd( const string& Command_Cv );
00046 SystemCmd();
00047 virtual ~SystemCmd();
00048 int execute( const string& Command_Cv );
00049 int executeBackground( const string& Command_Cv );
00050 int executeRestricted( const string& Command_Cv,
00051 unsigned long MaxTimeSec,
00052 unsigned long MaxLineOut,
00053 bool& ExceedTime, bool& ExceedLines);
00054 void setOutputHandler( void (*Handle_f)( void *, string, bool ),
00055 void * Par_p );
00056 void logOutput() const;
00057 void setOutputProcessor( OutputProcessor * proc )
00058 { output_proc = proc; }
00059 int select( string Reg_Cv, bool Invert_bv=false,
00060 unsigned Idx_ii=IDX_STDOUT );
00061 const string& stderr() const { return( *getString(IDX_STDERR)); }
00062 const string& stdout() const { return( *getString(IDX_STDOUT)); }
00063 const string& cmd() const { return( lastCmd ); }
00064 const string* getString( unsigned Idx_ii=IDX_STDOUT ) const;
00065 const string* getLine( unsigned Num_iv, bool Selected_bv=false,
00066 unsigned Idx_ii=IDX_STDOUT ) const;
00067 unsigned numLines( bool Selected_bv=false, unsigned Idx_ii=IDX_STDOUT ) const;
00068 void setCombine( const bool Combine_b=true );
00069 int retcode() const { return Ret_i; }
00070
00071 int getStdout( std::vector<string> &Ret_Cr, const bool Append_bv = false ) const
00072 { return placeOutput( IDX_STDOUT, Ret_Cr, Append_bv); }
00073 int getStderr( std::vector<string> &Ret_Cr, const bool Append_bv = false ) const
00074 { return placeOutput( IDX_STDERR, Ret_Cr, Append_bv); }
00075 int getStdout( std::list<string> &Ret_Cr, const bool Append_bv = false ) const
00076 { return placeOutput( IDX_STDOUT, Ret_Cr, Append_bv); }
00077 int getStderr( std::list<string> &Ret_Cr, const bool Append_bv = false ) const
00078 { return placeOutput( IDX_STDERR, Ret_Cr, Append_bv); }
00079
00083 static string quote(const string& str);
00084
00088 static string quote(const std::list<string>& strs);
00089
00090 static bool testmode;
00091
00092 protected:
00093
00094 int placeOutput( unsigned Which_iv, std::vector<string> &Ret_Cr, const bool Append_bv ) const;
00095 int placeOutput( unsigned Which_iv, std::list<string> &Ret_Cr, const bool Append_bv ) const;
00096
00097 void invalidate();
00098 void closeOpenFds();
00099 int doExecute( string Cmd_Cv );
00100 bool doWait( bool Hang_bv, int& Ret_ir );
00101 void checkOutput();
00102 void getUntilEOF( FILE* File_Cr, std::vector<string>& Lines_Cr,
00103 bool& NewLineSeen_br, bool Stderr_bv );
00104 void extractNewline( const char* Buf_ti, int Cnt_ii, bool& NewLineSeen_br,
00105 string& Text_Cr, std::vector<string>& Lines_Cr );
00106 void addLine( string Text_Cv, std::vector<string>& Lines_Cr );
00107 void init();
00108
00109 mutable string Text_aC[2];
00110 mutable bool Valid_ab[2];
00111 FILE* File_aC[2];
00112 std::vector<string> Lines_aC[2];
00113 std::vector<string*> SelLines_aC[2];
00114 bool NewLineSeen_ab[2];
00115 bool Combine_b;
00116 bool Background_b;
00117 string lastCmd;
00118 int Ret_i;
00119 int Pid_i;
00120 void (* OutputHandler_f)( void*, string, bool );
00121 void *HandlerPar_p;
00122 OutputProcessor* output_proc;
00123 struct pollfd pfds[2];
00124
00125 };
00126
00127
00128 inline string quote(const string& str)
00129 {
00130 return SystemCmd::quote(str);
00131 }
00132
00133 inline string quote(const std::list<string>& strs)
00134 {
00135 return SystemCmd::quote(strs);
00136 }
00137
00138 }
00139
00140 #endif