1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt3Support module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef Q3PROCESS_H
43 #define Q3PROCESS_H
44 
45 #include <QtCore/qobject.h>
46 #include <QtCore/qstringlist.h>
47 #include <QtCore/qdir.h>
48 
49 QT_BEGIN_HEADER
50 
51 QT_BEGIN_NAMESPACE
52 
53 QT_MODULE(Qt3SupportLight)
54 
55 #ifndef QT_NO_PROCESS
56 
57 class Q3ProcessPrivate;
58 class Q3Membuf;
59 
60 class Q_COMPAT_EXPORT Q3Process : public QObject
61 {
62     Q_OBJECT
63 public:
64     Q3Process( QObject *parent=0, const char *name=0 );
65     Q3Process( const QString& arg0, QObject *parent=0, const char *name=0 );
66     Q3Process( const QStringList& args, QObject *parent=0, const char *name=0 );
67     ~Q3Process();
68 
69     // set and get the arguments and working directory
70     QStringList arguments() const;
71     void clearArguments();
72     virtual void setArguments( const QStringList& args );
73     virtual void addArgument( const QString& arg );
74 #ifndef QT_NO_DIR
75     QDir workingDirectory() const;
76     virtual void setWorkingDirectory( const QDir& dir );
77 #endif
78 
79     // set and get the comms wanted
80     enum Communication { Stdin=0x01, Stdout=0x02, Stderr=0x04, DupStderr=0x08 };
81     void setCommunication( int c );
82     int communication() const;
83 
84     // start the execution
85     virtual bool start( QStringList *env=0 );
86     virtual bool launch( const QString& buf, QStringList *env=0  );
87     virtual bool launch( const QByteArray& buf, QStringList *env=0  );
88 
89     // inquire the status
90     bool isRunning() const;
91     bool normalExit() const;
92     int exitStatus() const;
93 
94     // reading
95     virtual QByteArray readStdout();
96     virtual QByteArray readStderr();
97     bool canReadLineStdout() const;
98     bool canReadLineStderr() const;
99     virtual QString readLineStdout();
100     virtual QString readLineStderr();
101 
102     // get platform dependent process information
103 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
104     typedef void* PID;
105 #else
106     typedef Q_LONG PID;
107 #endif
108     PID processIdentifier();
109 
110     void flushStdin();
111 
112 Q_SIGNALS:
113     void readyReadStdout();
114     void readyReadStderr();
115     void processExited();
116     void wroteToStdin();
117     void launchFinished();
118 
119 public Q_SLOTS:
120     // end the execution
121     void tryTerminate() const;
122     void kill() const;
123 
124     // input
125     virtual void writeToStdin( const QByteArray& buf );
126     virtual void writeToStdin( const QString& buf );
127     virtual void closeStdin();
128 
129 protected: // ### or private?
130     void connectNotify( const char * signal );
131     void disconnectNotify( const char * signal );
132 private:
133     void setIoRedirection( bool value );
134     void setNotifyOnExit( bool value );
135     void setWroteStdinConnected( bool value );
136 
137     void init();
138     void reset();
139 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
140     uint readStddev( Qt::HANDLE dev, char *buf, uint bytes );
141 #endif
142     Q3Membuf* membufStdout();
143     Q3Membuf* membufStderr();
144 
145 private Q_SLOTS:
146     void socketRead( int fd );
147     void socketWrite( int fd );
148     void timeout();
149     void closeStdinLaunch();
150 
151 private:
152     Q3ProcessPrivate *d;
153 #ifndef QT_NO_DIR
154     QDir        workingDir;
155 #endif
156     QStringList _arguments;
157 
158     int  exitStat; // exit status
159     bool exitNormal; // normal exit?
160     bool ioRedirection; // automatically set be (dis)connectNotify
161     bool notifyOnExit; // automatically set be (dis)connectNotify
162     bool wroteToStdinConnected; // automatically set be (dis)connectNotify
163 
164     bool readStdoutCalled;
165     bool readStderrCalled;
166     int comms;
167 
168     friend class Q3ProcessPrivate;
169 #if defined(Q_OS_UNIX)
170     friend class Q3ProcessManager;
171     friend class QProc;
172 #endif
173 
174 #if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator=
175     Q3Process( const Q3Process & );
176     Q3Process &operator=( const Q3Process & );
177 #endif
178 };
179 
180 #endif // QT_NO_PROCESS
181 
182 QT_END_NAMESPACE
183 
184 QT_END_HEADER
185 
186 #endif // Q3PROCESS_H
187