Debian Codegraph @

lib/Pty.cpp

raw symbols SCIP
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 <[email protected]>
6 *
7 */
8
9 /*
10 This file is part of Konsole, an X terminal.
11 Copyright 1997,1998 by Lars Doelle <[email protected]>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 02110-1301 USA.
27 */
28
29 // Own
30 #include "Pty.h"
31
32 // System
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <cerrno>
37 #include <termios.h>
38 #include <csignal>
39
40 // Qt
41 #include <QStringList>
42 #include <QtDebug>
43
44 #include "kpty.h"
45 #include "kptydevice.h"
46
47 using namespace Konsole;
48
49 void Pty::setWindowSize(int lines, int cols)
50 {
51 _windowColumns = cols;
52 _windowLines = lines;
53
54 if (pty()->masterFd() >= 0)
55 pty()->setWinSize(lines, cols);
56 }
57 QSize Pty::windowSize() const
58 {
59 return {_windowColumns,_windowLines};
60 }
61
62 void Pty::setFlowControlEnabled(bool enable)
63 {
64 _xonXoff = enable;
65
66 if (pty()->masterFd() >= 0)
67 {
68 struct ::termios ttmode;
69 pty()->tcGetAttr(&ttmode);
70 if (!enable)
71 ttmode.c_iflag &= ~(IXOFF | IXON);
72 else
73 ttmode.c_iflag |= (IXOFF | IXON);
74 if (!pty()->tcSetAttr(&ttmode))
75 qWarning() << "Unable to set terminal attributes.";
76 }
77 }
78 bool Pty::flowControlEnabled() const
79 {
80 if (pty()->masterFd() >= 0)
81 {
82 struct ::termios ttmode;
83 pty()->tcGetAttr(&ttmode);
84 return ttmode.c_iflag & IXOFF &&
85 ttmode.c_iflag & IXON;
86 }
87 qWarning() << "Unable to get flow control status, terminal not connected.";
88 return false;
89 }
90
91 void Pty::setUtf8Mode(bool enable)
92 {
93 #ifdef IUTF8 // XXX not a reasonable place to check it.
94 _utf8 = enable;
95
96 if (pty()->masterFd() >= 0)
97 {
98 struct ::termios ttmode;
99 pty()->tcGetAttr(&ttmode);
100 if (!enable)
101 ttmode.c_iflag &= ~IUTF8;
102 else
103 ttmode.c_iflag |= IUTF8;
104 if (!pty()->tcSetAttr(&ttmode))
105 qWarning() << "Unable to set terminal attributes.";
106 }
107 #endif
108 }
109
110 void Pty::setErase(char erase)
111 {
112 _eraseChar = erase;
113
114 if (pty()->masterFd() >= 0)
115 {
116 struct ::termios ttmode;
117 pty()->tcGetAttr(&ttmode);
118 ttmode.c_cc[VERASE] = erase;
119 if (!pty()->tcSetAttr(&ttmode))
120 qWarning() << "Unable to set terminal attributes.";
121 }
122 }
123
124 char Pty::erase() const
125 {
126 if (pty()->masterFd() >= 0)
127 {
128 struct ::termios ttyAttributes;
129 pty()->tcGetAttr(&ttyAttributes);
130 return ttyAttributes.c_cc[VERASE];
131 }
132
133 return _eraseChar;
134 }
135
136 void Pty::addEnvironmentVariables(const QStringList& environment)
137 {
138 QListIterator<QString> iter(environment);
139 while (iter.hasNext())
140 {
141 QString pair = iter.next();
142
143 // split on the first '=' character
144 int pos = pair.indexOf(QLatin1Char('='));
145
146 if ( pos >= 0 )
147 {
148 QString variable = pair.left(pos);
149 QString value = pair.mid(pos+1);
150
151 setEnv(variable,value);
152 }
153 }
154 }
155
156 int Pty::start(const QString& program,
157 const QStringList& programArguments,
158 const QStringList& environment,
159 ulong winid,
160 bool addToUtmp
161 //const QString& dbusService,
162 //const QString& dbusSession
163 )
164 {
165 clearProgram();
166
167 // For historical reasons, the first argument in programArguments is the
168 // name of the program to execute, so create a list consisting of all
169 // but the first argument to pass to setProgram()
170 Q_ASSERT(programArguments.count() >= 1);
171 setProgram(program, programArguments.mid(1));
172
173 addEnvironmentVariables(environment);
174
175 setEnv(QLatin1String("WINDOWID"), QString::number(winid));
176 setEnv(QLatin1String("COLORTERM"), QLatin1String("truecolor"));
177
178 // unless the LANGUAGE environment variable has been set explicitly
179 // set it to a null string
180 // this fixes the problem where KCatalog sets the LANGUAGE environment
181 // variable during the application's startup to something which
182 // differs from LANG,LC_* etc. and causes programs run from
183 // the terminal to display messages in the wrong language
184 //
185 // this can happen if LANG contains a language which KDE
186 // does not have a translation for
187 //
188 // BR:149300
189 setEnv(QLatin1String("LANGUAGE"),QString(),false /* do not overwrite existing value if any */);
190
191 setUseUtmp(addToUtmp);
192
193 struct ::termios ttmode;
194 pty()->tcGetAttr(&ttmode);
195 if (!_xonXoff)
196 ttmode.c_iflag &= ~(IXOFF | IXON);
197 else
198 ttmode.c_iflag |= (IXOFF | IXON);
199 #ifdef IUTF8 // XXX not a reasonable place to check it.
200 if (!_utf8)
201 ttmode.c_iflag &= ~IUTF8;
202 else
203 ttmode.c_iflag |= IUTF8;
204 #endif
205
206 if (_eraseChar != 0)
207 ttmode.c_cc[VERASE] = _eraseChar;
208
209 if (!pty()->tcSetAttr(&ttmode))
210 qWarning() << "Unable to set terminal attributes.";
211
212 pty()->setWinSize(_windowLines, _windowColumns);
213
214 KProcess::start();
215
216 if (!waitForStarted())
217 return -1;
218
219 return 0;
220 }
221
222 void Pty::setEmptyPTYProperties()
223 {
224 struct ::termios ttmode;
225 pty()->tcGetAttr(&ttmode);
226 if (!_xonXoff)
227 ttmode.c_iflag &= ~(IXOFF | IXON);
228 else
229 ttmode.c_iflag |= (IXOFF | IXON);
230 #ifdef IUTF8 // XXX not a reasonable place to check it.
231 if (!_utf8)
232 ttmode.c_iflag &= ~IUTF8;
233 else
234 ttmode.c_iflag |= IUTF8;
235 #endif
236
237 if (_eraseChar != 0)
238 ttmode.c_cc[VERASE] = _eraseChar;
239
240 if (!pty()->tcSetAttr(&ttmode))
241 qWarning() << "Unable to set terminal attributes.";
242 }
243
244 void Pty::setWriteable(bool writeable)
245 {
246 struct stat sbuf;
247 stat(pty()->ttyName(), &sbuf);
248 if (writeable)
249 chmod(pty()->ttyName(), sbuf.st_mode | S_IWGRP);
250 else
251 chmod(pty()->ttyName(), sbuf.st_mode & ~(S_IWGRP|S_IWOTH));
252 }
253
254 Pty::Pty(int masterFd, QObject* parent)
255 : KPtyProcess(masterFd,parent)
256 {
257 init();
258 }
259 Pty::Pty(QObject* parent)
260 : KPtyProcess(parent)
261 {
262 init();
263 }
264 void Pty::init()
265 {
266 _windowColumns = 0;
267 _windowLines = 0;
268 _eraseChar = 0;
269 _xonXoff = true;
270 _utf8 =true;
271
272 connect(pty(), SIGNAL(readyRead()) , this , SLOT(dataReceived()));
273 setPtyChannels(KPtyProcess::AllChannels);
274 }
275
276 Pty::~Pty()
277 {
278 }
279
280 void Pty::sendData(const char* data, int length)
281 {
282 if (!length)
283 return;
284
285 if (!pty()->write(data,length))
286 {
287 qWarning() << "Pty::doSendJobs - Could not send input data to terminal process.";
288 return;
289 }
290 }
291
292 void Pty::dataReceived()
293 {
294 QByteArray data = pty()->readAll();
295 emit receivedData(data.constData(),data.count());
296 }
297
298 void Pty::lockPty(bool lock)
299 {
300 Q_UNUSED(lock);
301
302 // TODO: Support for locking the Pty
303 //if (lock)
304 //suspend();
305 //else
306 //resume();
307 }
308
309 int Pty::foregroundProcessGroup() const
310 {
311 int pid = tcgetpgrp(pty()->masterFd());
312
313 if ( pid != -1 )
314 {
315 return pid;
316 }
317
318 return 0;
319 }
320
321 void Pty::setupChildProcess()
322 {
323 KPtyProcess::setupChildProcess();
324
325 // reset all signal handlers
326 // this ensures that terminal applications respond to
327 // signals generated via key sequences such as Ctrl+C
328 // (which sends SIGINT)
329 struct sigaction action;
330 sigset_t sigset;
331 sigemptyset(&action.sa_mask);
332 sigemptyset(&sigset);
333 action.sa_handler = SIG_DFL;
334 action.sa_flags = 0;
335 for (int signal=1;signal < NSIG; signal++) {
336 sigaction(signal,&action,nullptr);
337 sigaddset(&sigset, signal);
338 }
339 sigprocmask(SIG_UNBLOCK, &sigset, nullptr);
340 }