yast2-storage

SystemCmd.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) [2004-2009] Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
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 #include <boost/noncopyable.hpp>
00033 
00034 
00035 namespace storage
00036 {
00037     using std::string;
00038     using std::vector;
00039 
00040 class OutputProcessor;
00041 
00042 class SystemCmd : boost::noncopyable
00043     {
00044     public:
00045 
00046         enum OutputStream { IDX_STDOUT, IDX_STDERR };
00047 
00048         SystemCmd(const string& Command_Cv);
00049         SystemCmd();
00050 
00051         virtual ~SystemCmd();
00052 
00053         int execute(const string& Command_Cv);
00054         int executeBackground(const string& Command_Cv);
00055         int executeRestricted(const string& Command_Cv,
00056                               unsigned long MaxTimeSec, unsigned long MaxLineOut,
00057                               bool& ExceedTime, bool& ExceedLines);
00058 
00059         void setOutputProcessor(OutputProcessor* proc) { output_proc = proc; }
00060 
00061         const vector<string>& stdout() const { return Lines_aC[IDX_STDOUT]; }
00062         const vector<string>& stderr() const { return Lines_aC[IDX_STDERR]; }
00063 
00064         string cmd() const { return lastCmd; }
00065         int retcode() const { return Ret_i; }
00066 
00067         int select(const string& Reg_Cv, OutputStream Idx_ii = IDX_STDOUT);
00068         unsigned numLines(bool Selected_bv = false, OutputStream Idx_ii = IDX_STDOUT) const;
00069         string getLine(unsigned Num_iv, bool Selected_bv = false, OutputStream Idx_ii = IDX_STDOUT) const;
00070 
00071         void setCombine(bool combine = true);
00072 
00073         static void setTestmode(bool testmode = true);
00074 
00078         static string quote(const string& str);
00079 
00083         static string quote(const std::list<string>& strs);
00084 
00085     protected:
00086 
00087         void invalidate();
00088         void closeOpenFds() const;
00089         int doExecute(const string& Cmd_Cv);
00090         bool doWait(bool Hang_bv, int& Ret_ir);
00091         void checkOutput();
00092         void getUntilEOF(FILE* File_Cr, std::vector<string>& Lines_Cr,
00093                          bool& NewLineSeen_br, bool Stderr_bv);
00094         void extractNewline(const string& Buf_ti, int Cnt_ii, bool& NewLineSeen_br,
00095                             string& Text_Cr, std::vector<string>& Lines_Cr);
00096         void addLine(const string& Text_Cv, std::vector<string>& Lines_Cr);
00097         void init();
00098 
00099         void logOutput() const;
00100 
00101         FILE* File_aC[2];
00102         std::vector<string> Lines_aC[2];
00103         std::vector<string*> SelLines_aC[2];
00104         bool NewLineSeen_ab[2];
00105         bool Combine_b;
00106         bool Background_b;
00107         string lastCmd;
00108         int Ret_i;
00109         int Pid_i;
00110         OutputProcessor* output_proc;
00111         struct pollfd pfds[2];
00112 
00113         static bool testmode;
00114 
00115         static const unsigned line_limit = 50;
00116     };
00117 
00118 
00119     inline string quote(const string& str)
00120     {
00121         return SystemCmd::quote(str);
00122     }
00123 
00124     inline string quote(const std::list<string>& strs)
00125     {
00126         return SystemCmd::quote(strs);
00127     }
00128 
00129 }
00130 
00131 #endif