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., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef __kprocess_h__
00021 #define __kprocess_h__
00022 
00023 #include <sys/types.h> // for pid_t
00024 #include <sys/wait.h>
00025 #include <signal.h>
00026 #include <unistd.h>
00027 #include <qvaluelist.h>
00028 #include <qcstring.h>
00029 #include <qobject.h>
00030 #include "kdelibs_export.h"
00031 
00032 class QSocketNotifier;
00033 class KProcessPrivate;
00034 class KPty;
00035 
00124 class KDECORE_EXPORT KProcess : public QObject
00125 {
00126   Q_OBJECT
00127 
00128 public:
00129 
00151   enum Communication {
00152        NoCommunication = 0,
00153        Stdin = 1, Stdout = 2, Stderr = 4,
00154        AllOutput = 6, All = 7,
00155        NoRead = 8,
00156        CTtyOnly = NoRead,
00157        MergedStderr = 16
00158   };
00159 
00163   enum RunMode {
00168        DontCare,
00172        NotifyOnExit,
00176        Block,
00181        OwnGroup
00182   };
00183 
00188   KProcess( QObject* parent, const char *name = 0 );
00189  // KDE4 merge with the above
00193   KProcess();
00194 
00203   virtual ~KProcess();
00204 
00216   bool setExecutable(const QString& proc) KDE_DEPRECATED;
00217 
00218 
00232   KProcess &operator<<(const QString& arg);
00236   KProcess &operator<<(const char * arg);
00242   KProcess &operator<<(const QCString & arg);
00243 
00250   KProcess &operator<<(const QStringList& args);
00251 
00256   void clearArguments();
00257 
00284   virtual bool start(RunMode  runmode = NotifyOnExit,
00285     Communication comm = NoCommunication);
00286 
00293   virtual bool kill(int signo = SIGTERM);
00294 
00299   bool isRunning() const;
00300 
00311   pid_t pid() const;
00312 
00317   KDE_DEPRECATED pid_t getPid() const { return pid(); }
00318 
00322   void suspend();
00323 
00327   void resume();
00328 
00337   bool wait(int timeout = -1);
00338 
00345   bool normalExit() const;
00346 
00355   bool signalled() const;
00356 
00366   bool coreDumped() const;
00367 
00374   int exitStatus() const;
00375 
00384   int exitSignal() const;
00385 
00414   bool writeStdin(const char *buffer, int buflen);
00415 
00422   bool closeStdin();
00423 
00431   bool closeStdout();
00432 
00440   bool closeStderr();
00441 
00450   bool closePty();
00451 
00458   void closeAll();
00459 
00464   const QValueList<QCString> &args() /* const */ { return arguments; }
00465 
00475   void setRunPrivileged(bool keepPrivileges);
00476 
00482   bool runPrivileged() const;
00483 
00490   void setEnvironment(const QString &name, const QString &value);
00491 
00498   void setWorkingDirectory(const QString &dir);
00499 
00516   void setUseShell(bool useShell, const char *shell = 0);
00517 
00527   static QString quote(const QString &arg);
00528 
00536   void detach();
00537 
00538 #ifdef Q_OS_UNIX
00539 
00550   void setUsePty(Communication comm, bool addUtmp);
00551 
00559   KPty *pty() const;
00560 #endif
00561 
00565   enum { PrioLowest = 20, PrioLow = 10, PrioLower = 5, PrioNormal = 0,
00566     PrioHigher = -5, PrioHigh = -10, PrioHighest = -19 };
00567 
00574   bool setPriority(int prio);
00575 
00576 signals:
00583   void processExited(KProcess *proc);
00584 
00585 
00604   void receivedStdout(KProcess *proc, char *buffer, int buflen);
00605 
00624   void receivedStdout(int fd, int &len); // KDE4: change, broken API
00625 
00626 
00641   void receivedStderr(KProcess *proc, char *buffer, int buflen);
00642 
00649   void wroteStdin(KProcess *proc);
00650 
00651 
00652 protected slots:
00653 
00659   void slotChildOutput(int fdno);
00660 
00666   void slotChildError(int fdno);
00667 
00674   void slotSendData(int dummy); // KDE 4: remove dummy
00675 
00676 protected:
00677 
00682   void setupEnvironment();
00683 
00688   QValueList<QCString> arguments;
00693   RunMode run_mode;
00700   bool runs;
00701 
00709   pid_t pid_;
00710 
00718   int status;
00719 
00720 
00726   bool keepPrivs;
00727 
00740   virtual int setupCommunication(Communication comm);
00741 
00754   virtual int commSetupDoneP();
00755 
00761   virtual int commSetupDoneC();
00762 
00763 
00770   virtual void processHasExited(int state);
00771 
00797   virtual void commClose();
00798 
00799   /* KDE 4 - commClose will be changed to perform cleanup only in all cases *
00800    * If @p notfd is -1, all data immediately available from the
00801    *  communication links should be processed.
00802    * If @p notfd is not -1, the communication links should be monitored
00803    *  for data until the file handle @p notfd becomes ready for reading.
00804    */
00805 //  virtual void commDrain(int notfd);
00806 
00812   void setBinaryExecutable(const char *filename);
00813 
00817   int out[2];
00821   int in[2];
00825   int err[2];
00826 
00830   QSocketNotifier *innot;
00834   QSocketNotifier *outnot;
00838   QSocketNotifier *errnot;
00839 
00844   Communication communication;
00845 
00851   int childOutput(int fdno);
00852 
00858   int childError(int fdno);
00859 
00863   const char *input_data;
00867   int input_sent;
00871   int input_total;
00872 
00877   friend class KProcessController;
00878 
00879 protected:
00880   virtual void virtual_hook( int id, void* data );
00881 private:
00882   KProcessPrivate *d;
00883 };
00884 
00885 class KShellProcessPrivate;
00886 
00896 class KDECORE_EXPORT KShellProcess: public KProcess
00897 {
00898   Q_OBJECT
00899 
00900 public:
00901 
00907   KShellProcess(const char *shellname=0);
00908 
00912   ~KShellProcess();
00913 
00914   virtual bool start(RunMode  runmode = NotifyOnExit,
00915           Communication comm = NoCommunication);
00916 
00917   static QString quote(const QString &arg);
00918 
00919 private:
00920   QCString shell;
00921 
00922 protected:
00923   virtual void virtual_hook( int id, void* data );
00924 private:
00925   KShellProcessPrivate *d;
00926 };
00927 
00928 
00929 
00930 #endif
00931 
KDE Home | KDE Accessibility Home | Description of Access Keys