Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

kprocess.h

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 //
00020 //  KPROCESS -- A class for handling child processes in KDE without
00021 //  having to take care of Un*x specific implementation details
00022 //
00023 //  version 0.3.1, Jan 8th 1998
00024 //
00025 //  (C) Christian Czezatke
00026 //  e9025461@student.tuwien.ac.at
00027 //
00028 
00029 #ifndef __kprocess_h__
00030 #define __kprocess_h__
00031 
00032 #include <sys/types.h> // for pid_t
00033 #include <sys/wait.h>
00034 #include <signal.h>
00035 #include <unistd.h>
00036 #include <qvaluelist.h>
00037 #include <qcstring.h>
00038 #include <qobject.h>
00039 
00040 class QSocketNotifier;
00041 class KProcessPrivate;
00042 
00146 class KProcess : public QObject
00147 {
00148   Q_OBJECT
00149 
00150 public:
00151 
00164   enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4,
00165                        AllOutput = 6, All = 7,
00166                        NoRead };
00167 
00171   enum RunMode {
00176        DontCare,
00180        NotifyOnExit,
00184        Block };
00185 
00189   KProcess();
00190 
00199   virtual ~KProcess();
00200 
00214   bool setExecutable(const QString& proc);
00215 
00216 
00228   KProcess &operator<<(const QString& arg);
00232   KProcess &operator<<(const char * arg);
00236   KProcess &operator<<(const QCString & arg);
00237 
00242   KProcess &operator<<(const QStringList& args);
00243 
00248   void clearArguments();
00249 
00272   virtual bool start(RunMode  runmode = NotifyOnExit,
00273     Communication comm = NoCommunication);
00274 
00281   virtual bool kill(int signo = SIGTERM);
00282 
00286   bool isRunning() const;
00287 
00297   pid_t pid() const;
00298 
00303   pid_t getPid() const { return pid(); }
00304 
00308   void suspend();
00309 
00313   void resume();
00314 
00322   bool normalExit() const;
00323 
00333   int  exitStatus() const;
00334 
00335 
00360   bool writeStdin(const char *buffer, int buflen);
00361 
00369   bool closeStdin();
00370 
00378   bool closeStdout();
00379 
00387   bool closeStderr();
00388 
00393   const QValueList<QCString> &args() { return arguments; }
00394 
00401   void setRunPrivileged(bool keepPrivileges);
00402 
00407   bool runPrivileged() const;
00408   
00413   void setEnvironment(const QString &name, const QString &value);
00414 
00420   void setWorkingDirectory(const QString &dir);
00421 
00429   void detach(); 
00430 
00431 signals:
00432 
00438   void processExited(KProcess *proc);
00439 
00440 
00455   void receivedStdout(KProcess *proc, char *buffer, int buflen);
00456 
00472   void receivedStdout(int fd, int &len);
00473 
00474 
00488   void receivedStderr(KProcess *proc, char *buffer, int buflen);
00489 
00495   void wroteStdin(KProcess *proc);
00496 
00497 
00498 protected slots:
00499 
00504   void slotChildOutput(int fdno);
00505 
00510   void slotChildError(int fdno);
00511   /*
00512     Slot functions for capturing stdout and stderr of the child
00513   */
00514 
00520   void slotSendData(int dummy);
00521 
00522 protected:
00523 
00528   void setupEnvironment();
00529 
00534   QValueList<QCString> arguments;
00539   RunMode run_mode;
00547   bool runs;
00548 
00556   pid_t pid_;
00557 
00565   int status;
00566 
00567 
00571   bool keepPrivs;
00572 
00573   /*
00574     Functions for setting up the sockets for communication.
00575     setupCommunication
00576     -- is called from "start" before "fork"ing.
00577     commSetupDoneP
00578     -- completes communication socket setup in the parent
00579     commSetupDoneC
00580     -- completes communication setup in the child process
00581     commClose
00582     -- frees all allocated communication resources in the parent
00583     after the process has exited
00584   */
00585 
00599   virtual int setupCommunication(Communication comm);
00600 
00612   virtual int commSetupDoneP();
00613 
00620   virtual int commSetupDoneC();
00621 
00622 
00629   virtual void processHasExited(int state);
00630 
00635   virtual void commClose();
00636 
00637 
00641   int out[2];
00642   int in[2];
00643   int err[2];
00644 
00648   QSocketNotifier *innot;
00649   QSocketNotifier *outnot;
00650   QSocketNotifier *errnot;
00651 
00656   Communication communication;
00657 
00663   int childOutput(int fdno);
00664 
00670   int childError(int fdno);
00671 
00672   // information about the data that has to be sent to the child:
00673 
00674   const char *input_data;  // the buffer holding the data
00675   int input_sent;    // # of bytes already transmitted
00676   int input_total;   // total length of input_data
00677 
00682   friend class KProcessController;
00683 
00684 
00685 private:
00686   // Disallow assignment and copy-construction
00687   KProcess( const KProcess& );
00688   KProcess& operator= ( const KProcess& );
00689 
00690 protected:
00691   virtual void virtual_hook( int id, void* data );
00692 private:
00693   KProcessPrivate *d;
00694 };
00695 
00696 class KShellProcessPrivate;
00697 
00727 class KShellProcess: public KProcess
00728 {
00729   Q_OBJECT
00730 
00731 public:
00732 
00740   KShellProcess(const char *shellname=0);
00741 
00745   ~KShellProcess();
00746 
00752   virtual bool start(RunMode  runmode = NotifyOnExit,
00753           Communication comm = NoCommunication);
00754 
00761   static QString quote(const QString &arg);
00762 
00763 private:
00764 
00769   QCString searchShell();
00770 
00775   bool isExecutable(const QCString &filename);
00776 
00777   QCString shell;
00778 
00779   // Disallow assignment and copy-construction
00780   KShellProcess( const KShellProcess& );
00781   KShellProcess& operator= ( const KShellProcess& );
00782 
00783 protected:
00784   virtual void virtual_hook( int id, void* data );
00785 private:
00786   KShellProcessPrivate *d;
00787 };
00788 
00789 
00790 
00791 #endif
00792 

Generated on Sat Jul 26 04:23:40 2003 by doxygen1.2.18