1 | /* |
---|
2 | * This file is a part of QTerminal - http://gitorious.org/qterminal |
---|
3 | * |
---|
4 | * This file was un-linked from KDE and modified |
---|
5 | * by Maxim Bourmistrov <maxim@unixconn.com> |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | This file is part of the KDE libraries |
---|
11 | |
---|
12 | Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org> |
---|
13 | |
---|
14 | This library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU Library General Public |
---|
16 | License as published by the Free Software Foundation; either |
---|
17 | version 2 of the License, or (at your option) any later version. |
---|
18 | |
---|
19 | This library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | Library General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU Library General Public License |
---|
25 | along with this library; see the file COPYING.LIB. If not, write to |
---|
26 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
27 | Boston, MA 02110-1301, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef KPTYPROCESS_H |
---|
31 | #define KPTYPROCESS_H |
---|
32 | |
---|
33 | #include "kprocess.h" |
---|
34 | #include "kptydevice.h" |
---|
35 | |
---|
36 | #include <csignal> |
---|
37 | |
---|
38 | class KPtyDevice; |
---|
39 | |
---|
40 | class KPtyProcessPrivate; |
---|
41 | |
---|
42 | /** |
---|
43 | * This class extends KProcess by support for PTYs (pseudo TTYs). |
---|
44 | * |
---|
45 | * The PTY is opened as soon as the class is instantiated. Verify that |
---|
46 | * it was opened successfully by checking that pty()->masterFd() is not -1. |
---|
47 | * |
---|
48 | * The PTY is always made the process' controlling TTY. |
---|
49 | * Utmp registration and connecting the stdio handles to the PTY are optional. |
---|
50 | * |
---|
51 | * No attempt to integrate with QProcess' waitFor*() functions was made, |
---|
52 | * for it is impossible. Note that execute() does not work with the PTY, too. |
---|
53 | * Use the PTY device's waitFor*() functions or use it asynchronously. |
---|
54 | * |
---|
55 | * @author Oswald Buddenhagen <ossi@kde.org> |
---|
56 | */ |
---|
57 | class KPtyProcess : public KProcess |
---|
58 | { |
---|
59 | Q_OBJECT |
---|
60 | Q_DECLARE_PRIVATE(KPtyProcess) |
---|
61 | |
---|
62 | public: |
---|
63 | enum PtyChannelFlag { |
---|
64 | NoChannels = 0, /**< The PTY is not connected to any channel. */ |
---|
65 | StdinChannel = 1, /**< Connect PTY to stdin. */ |
---|
66 | StdoutChannel = 2, /**< Connect PTY to stdout. */ |
---|
67 | StderrChannel = 4, /**< Connect PTY to stderr. */ |
---|
68 | AllOutputChannels = 6, /**< Connect PTY to all output channels. */ |
---|
69 | AllChannels = 7 /**< Connect PTY to all channels. */ |
---|
70 | }; |
---|
71 | |
---|
72 | Q_DECLARE_FLAGS(PtyChannels, PtyChannelFlag) |
---|
73 | |
---|
74 | /** |
---|
75 | * Constructor |
---|
76 | */ |
---|
77 | explicit KPtyProcess(QObject *parent = nullptr); |
---|
78 | |
---|
79 | /** |
---|
80 | * Construct a process using an open pty master. |
---|
81 | * |
---|
82 | * @param ptyMasterFd an open pty master file descriptor. |
---|
83 | * The process does not take ownership of the descriptor; |
---|
84 | * it will not be automatically closed at any point. |
---|
85 | */ |
---|
86 | KPtyProcess(int ptyMasterFd, QObject *parent = nullptr); |
---|
87 | |
---|
88 | /** |
---|
89 | * Destructor |
---|
90 | */ |
---|
91 | ~KPtyProcess() override; |
---|
92 | |
---|
93 | /** |
---|
94 | * Set to which channels the PTY should be assigned. |
---|
95 | * |
---|
96 | * This function must be called before starting the process. |
---|
97 | * |
---|
98 | * @param channels the output channel handling mode |
---|
99 | */ |
---|
100 | void setPtyChannels(PtyChannels channels); |
---|
101 | |
---|
102 | bool isRunning() const |
---|
103 | { |
---|
104 | bool rval; |
---|
105 | (processId() > 0) ? rval= true : rval= false; |
---|
106 | return rval; |
---|
107 | |
---|
108 | } |
---|
109 | /** |
---|
110 | * Query to which channels the PTY is assigned. |
---|
111 | * |
---|
112 | * @return the output channel handling mode |
---|
113 | */ |
---|
114 | PtyChannels ptyChannels() const; |
---|
115 | |
---|
116 | /** |
---|
117 | * Set whether to register the process as a TTY login in utmp. |
---|
118 | * |
---|
119 | * Utmp is disabled by default. |
---|
120 | * It should enabled for interactively fed processes, like terminal |
---|
121 | * emulations. |
---|
122 | * |
---|
123 | * This function must be called before starting the process. |
---|
124 | * |
---|
125 | * @param value whether to register in utmp. |
---|
126 | */ |
---|
127 | void setUseUtmp(bool value); |
---|
128 | |
---|
129 | /** |
---|
130 | * Get whether to register the process as a TTY login in utmp. |
---|
131 | * |
---|
132 | * @return whether to register in utmp |
---|
133 | */ |
---|
134 | bool isUseUtmp() const; |
---|
135 | |
---|
136 | /** |
---|
137 | * Get the PTY device of this process. |
---|
138 | * |
---|
139 | * @return the PTY device |
---|
140 | */ |
---|
141 | KPtyDevice *pty() const; |
---|
142 | |
---|
143 | protected: |
---|
144 | /** |
---|
145 | * @reimp |
---|
146 | */ |
---|
147 | void setupChildProcess() override; |
---|
148 | |
---|
149 | private: |
---|
150 | Q_PRIVATE_SLOT(d_func(), void _k_onStateChanged(QProcess::ProcessState)) |
---|
151 | }; |
---|
152 | |
---|
153 | |
---|
154 | ////////////////// |
---|
155 | // private data // |
---|
156 | ////////////////// |
---|
157 | |
---|
158 | class KPtyProcessPrivate : public KProcessPrivate { |
---|
159 | public: |
---|
160 | KPtyProcessPrivate() : |
---|
161 | ptyChannels(KPtyProcess::NoChannels), |
---|
162 | addUtmp(false) |
---|
163 | { |
---|
164 | } |
---|
165 | |
---|
166 | void _k_onStateChanged(QProcess::ProcessState newState) |
---|
167 | { |
---|
168 | if (newState == QProcess::NotRunning && addUtmp) |
---|
169 | pty->logout(); |
---|
170 | } |
---|
171 | |
---|
172 | KPtyDevice *pty; |
---|
173 | KPtyProcess::PtyChannels ptyChannels; |
---|
174 | bool addUtmp : 1; |
---|
175 | }; |
---|
176 | |
---|
177 | Q_DECLARE_OPERATORS_FOR_FLAGS(KPtyProcess::PtyChannels) |
---|
178 | |
---|
179 | #endif |
---|