2 This file is part of Konsole, an X terminal.
3 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <sys/types.h>
33 #include <QtCore/QStringList>
36 #include <KStandardDirs>
43 using namespace Konsole
;
45 void Pty::setWindowSize(int lines
, int cols
)
47 _windowColumns
= cols
;
50 if (pty()->masterFd() >= 0)
51 pty()->setWinSize(lines
, cols
);
53 QSize
Pty::windowSize() const
55 return QSize(_windowColumns
,_windowLines
);
58 void Pty::setFlowControlEnabled(bool enable
)
62 if (pty()->masterFd() >= 0)
64 struct ::termios ttmode
;
65 pty()->tcGetAttr(&ttmode
);
67 ttmode
.c_iflag
&= ~(IXOFF
| IXON
);
69 ttmode
.c_iflag
|= (IXOFF
| IXON
);
70 if (!pty()->tcSetAttr(&ttmode
))
71 kWarning() << "Unable to set terminal attributes.";
74 bool Pty::flowControlEnabled() const
76 if (pty()->masterFd() >= 0)
78 struct ::termios ttmode
;
79 pty()->tcGetAttr(&ttmode
);
80 return ttmode
.c_iflag
& IXOFF
&&
81 ttmode
.c_iflag
& IXON
;
83 kWarning() << "Unable to get flow control status, terminal not connected.";
87 void Pty::setUtf8Mode(bool enable
)
89 #ifdef IUTF8 // XXX not a reasonable place to check it.
92 if (pty()->masterFd() >= 0)
94 struct ::termios ttmode
;
95 pty()->tcGetAttr(&ttmode
);
97 ttmode
.c_iflag
&= ~IUTF8
;
99 ttmode
.c_iflag
|= IUTF8
;
100 if (!pty()->tcSetAttr(&ttmode
))
101 kWarning() << "Unable to set terminal attributes.";
106 void Pty::setErase(char erase
)
110 if (pty()->masterFd() >= 0)
112 struct ::termios ttmode
;
113 pty()->tcGetAttr(&ttmode
);
114 ttmode
.c_cc
[VERASE
] = erase
;
115 if (!pty()->tcSetAttr(&ttmode
))
116 kWarning() << "Unable to set terminal attributes.";
120 char Pty::erase() const
122 if (pty()->masterFd() >= 0)
124 struct ::termios ttyAttributes
;
125 pty()->tcGetAttr(&ttyAttributes
);
126 return ttyAttributes
.c_cc
[VERASE
];
132 void Pty::addEnvironmentVariables(const QStringList
& environment
)
134 QListIterator
<QString
> iter(environment
);
135 while (iter
.hasNext())
137 QString pair
= iter
.next();
139 // split on the first '=' character
140 int pos
= pair
.indexOf('=');
144 QString variable
= pair
.left(pos
);
145 QString value
= pair
.mid(pos
+1);
147 setEnv(variable
,value
);
152 int Pty::start(const QString
& program
,
153 const QStringList
& programArguments
,
154 const QStringList
& environment
,
157 const QString
& dbusService
,
158 const QString
& dbusSession
)
162 // For historical reasons, the first argument in programArguments is the
163 // name of the program to execute, so create a list consisting of all
164 // but the first argument to pass to setProgram()
165 Q_ASSERT(programArguments
.count() >= 1);
166 setProgram(program
.toLatin1(),programArguments
.mid(1));
168 addEnvironmentVariables(environment
);
170 if ( !dbusService
.isEmpty() )
171 setEnv("KONSOLE_DBUS_SERVICE",dbusService
);
172 if ( !dbusSession
.isEmpty() )
173 setEnv("KONSOLE_DBUS_SESSION", dbusSession
);
175 setEnv("WINDOWID", QString::number(winid
));
177 // unless the LANGUAGE environment variable has been set explicitly
178 // set it to a null string
179 // this fixes the problem where KCatalog sets the LANGUAGE environment
180 // variable during the application's startup to something which
181 // differs from LANG,LC_* etc. and causes programs run from
182 // the terminal to display messages in the wrong language
184 // this can happen if LANG contains a language which KDE
185 // does not have a translation for
188 setEnv("LANGUAGE",QString(),false /* do not overwrite existing value if any */);
190 setUseUtmp(addToUtmp
);
192 struct ::termios ttmode
;
193 pty()->tcGetAttr(&ttmode
);
195 ttmode
.c_iflag
&= ~(IXOFF
| IXON
);
197 ttmode
.c_iflag
|= (IXOFF
| IXON
);
198 #ifdef IUTF8 // XXX not a reasonable place to check it.
200 ttmode
.c_iflag
&= ~IUTF8
;
202 ttmode
.c_iflag
|= IUTF8
;
206 ttmode
.c_cc
[VERASE
] = _eraseChar
;
208 if (!pty()->tcSetAttr(&ttmode
))
209 kWarning() << "Unable to set terminal attributes.";
211 pty()->setWinSize(_windowLines
, _windowColumns
);
215 if (!waitForStarted())
221 void Pty::setWriteable(bool writeable
)
223 KDE_struct_stat sbuf
;
224 KDE_stat(pty()->ttyName(), &sbuf
);
226 chmod(pty()->ttyName(), sbuf
.st_mode
| S_IWGRP
);
228 chmod(pty()->ttyName(), sbuf
.st_mode
& ~(S_IWGRP
|S_IWOTH
));
231 Pty::Pty(int masterFd
, QObject
* parent
)
232 : KPtyProcess(masterFd
,parent
)
236 Pty::Pty(QObject
* parent
)
237 : KPtyProcess(parent
)
249 connect(pty(), SIGNAL(readyRead()) , this , SLOT(dataReceived()));
250 setPtyChannels(KPtyProcess::AllChannels
);
257 void Pty::sendData(const char* data
, int length
)
262 if (!pty()->write(data
,length
))
264 kWarning() << "Pty::doSendJobs - Could not send input data to terminal process.";
269 void Pty::dataReceived()
271 QByteArray data
= pty()->readAll();
272 emit
receivedData(data
.constData(),data
.count());
275 void Pty::lockPty(bool lock
)
278 #warning "TODO: Support for locking the Pty"
286 int Pty::foregroundProcessGroup() const
288 int pid
= tcgetpgrp(pty()->masterFd());
298 void Pty::setupChildProcess()
300 KPtyProcess::setupChildProcess();
302 // reset all signal handlers
303 // this ensures that terminal applications respond to
304 // signals generated via key sequences such as Ctrl+C
305 // (which sends SIGINT)
306 struct sigaction action
;
307 sigemptyset(&action
.sa_mask
);
308 action
.sa_handler
= SIG_DFL
;
310 for (int signal
=1;signal
< NSIG
; signal
++)
311 sigaction(signal
,&action
,0L);