File indexing completed on 2024-12-01 06:51:47

0001 /***************************************************************************
0002                           crunningscript.h  -  running script
0003                              -------------------
0004     begin                : Ne dec 8 2002
0005     copyright            : (C) 2002-2009 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef CRUNNINGSCRIPT_H
0019 #define CRUNNINGSCRIPT_H
0020 
0021 #include <qobject.h>
0022 
0023 class cScript;
0024 class cUnixSocket;
0025 
0026 #include <QProcess>
0027 
0028 #define SERVEROUTPUT '1'
0029 #define USERCOMMAND '2'
0030 #define PROMPT '3'
0031 
0032 /**
0033 One currently running script.
0034   *@author Tomas Mecir
0035   */
0036 
0037 class cRunningScript : public QObject  {
0038    Q_OBJECT
0039 protected: 
0040   friend class cScript;
0041   friend class cRunningList;
0042   
0043   /** this object can only be created by cScript, ensuring that unauthorized
0044   classes don't mess it up (we rely on cScript heavily)
0045   Then it's registered with cRunningList, which takes care of deleting of
0046   this object. That's why we need cRunningScript as a friend too. */
0047   cRunningScript (cScript *s);
0048   ~cRunningScript ();
0049 
0050 public:
0051 
0052   /** send command to script, if we have this feature enabled. Type is
0053   used for advanced communication, it determines type of sent line;
0054   1 = server output, 2 = user command, 3 = prompt; more types coming later */
0055   void sendCommandToScript (const QString &command, char type = SERVEROUTPUT);
0056 
0057   /** Launch the script !!! */
0058   void launch (int fcState);
0059 
0060   /** get script name */
0061   const QString name () const;
0062   /** is the script running? */
0063   bool isRunning () const;
0064   /** try to terminate the script */
0065   void terminate ();
0066   /** kill the script */
0067   void kill ();
0068 
0069   int getId () const { return id; };
0070   /** set script ID; used by cRunningList */
0071   void setId (int _id) { id = _id; };
0072 
0073   /** is flow control enabled for this script? */
0074   bool flowControl () const { return flowcontrol; };
0075   /** has input actually been transmitted as a result of sendCommandToScript() ? */
0076   bool actuallySentCommand() const { return actuallySent; }
0077 signals:
0078   /** successfully sent last text to the script */
0079   void textSent ();
0080   /** text accepted by the class, will be sent now */
0081   void textAccepted ();
0082   /** command to be sent */
0083   void sendText (const QString &text);
0084   /** text to be displayed */
0085   void displayText (const QString &text);
0086   /** script finished normally, exit value is returnValue */
0087   void scriptFinished (cRunningScript *me, int returnValue);
0088   /** script was killed with a signal */
0089   void scriptKilled (cRunningScript *me);
0090   /** script couldn't be started */
0091   void scriptFailed (cRunningScript *me);
0092 protected slots:
0093   void processScriptStdOutput ();
0094   void processScriptStdError ();
0095   void finished (int, QProcess::ExitStatus);
0096   void failed (QProcess::ProcessError);
0097   void stdinReady();
0098 protected:
0099   void establishSocket (int sess);
0100   void processScriptOutput (const QByteArray &output, bool sendoutput);
0101   /** launch the script, after it has been waiting for flow control
0102   to synchronize */
0103   void doLaunch ();
0104   void cleanup ();
0105   void cleanupSend ();
0106 
0107   cUnixSocket *unixsocket;
0108   cScript *script;
0109   
0110   /** should we emit a signal when the process has ended? Set to false in
0111   destructor before deleting the QProcess object to prevent double deletion
0112   of this object (that would cause a crash) */
0113   bool dontSignal;
0114 
0115   /** script ID; this has nothing to do with its PID! */
0116   int id;
0117 
0118   int launchAfter;
0119   
0120   QString outLine, errLine;
0121   bool scriptDying;
0122 
0123   bool onlyifmatch;
0124 
0125   QString command;
0126   QStringList args;
0127 
0128   QProcess *process;
0129   bool sendusercommands;
0130   bool useadvcomm;
0131 
0132   /** should be use flow control? */
0133   bool flowcontrol;
0134 
0135   bool    actuallySent;   // Was data actually transmitted to this script's STDIN?
0136   bool    sendInProgress; // Is data being sent to this script's STDIN?
0137   QString stdinBuffer;    // Buffered data to send to script's STDIN
0138   QString stdinSending;   // Data in transit to script's STDIN
0139 };
0140 
0141 #endif