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