1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
// Own
#include "Pty.h"
// System
#include <csignal>
#include <sys/ioctl.h> //ioctl() and TIOCSWINSZ
#include <termios.h>
// Qt
#include <QDebug>
#include <QStringList>
#include <qplatformdefs.h>
// KDE
#include <KPtyDevice>
#include <kpty_version.h>
using Konsole::Pty;
Pty::Pty(QObject *aParent)
: Pty(-1, aParent)
{
}
Pty::Pty(int masterFd, QObject *aParent)
: KPtyProcess(masterFd, aParent)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
// Must call parent class child process modifier, as it sets file descriptors ...etc
auto parentChildProcModifier = KPtyProcess::childProcessModifier();
setChildProcessModifier([parentChildProcModifier = std::move(parentChildProcModifier)]() {
if (parentChildProcModifier) {
parentChildProcModifier();
}
// reset all signal handlers
// this ensures that terminal applications respond to
// signals generated via key sequences such as Ctrl+C
// (which sends SIGINT)
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
for (int signal = 1; signal < NSIG; signal++) {
sigaction(signal, &action, nullptr);
}
});
#endif
_windowColumns = 0;
_windowLines = 0;
_windowWidth = 0;
_windowHeight = 0;
_eraseChar = 0;
_xonXoff = true;
_utf8 = true;
setEraseChar(_eraseChar);
setFlowControlEnabled(_xonXoff);
setUtf8Mode(_utf8);
setWindowSize(_windowColumns, _windowLines, _windowWidth, _windowHeight);
setUseUtmp(true);
setPtyChannels(KPtyProcess::AllChannels);
connect(pty(), &KPtyDevice::readyRead, this, &Konsole::Pty::dataReceived);
}
Pty::~Pty() = default;
void Pty::sendData(const QByteArray &data)
{
if (data.isEmpty()) {
return;
}
if (pty()->write(data) == -1) {
qDebug() << "Could not send input data to terminal process.";
return;
}
}
void Pty::dataReceived()
{
QByteArray data = pty()->readAll();
if (data.isEmpty()) {
return;
}
Q_EMIT receivedData(data.constData(), data.size());
}
void Pty::setWindowSize(int columns, int lines, int width, int height)
{
_windowColumns = columns;
_windowLines = lines;
_windowWidth = width;
_windowHeight = height;
if (pty()->masterFd() >= 0) {
#if KPTY_VERSION >= QT_VERSION_CHECK(5, 93, 0)
pty()->setWinSize(_windowLines, _windowColumns, _windowHeight, _windowWidth);
#else
struct winsize w;
w.ws_xpixel = _windowWidth;
w.ws_ypixel = _windowHeight;
w.ws_col = _windowColumns;
w.ws_row = _windowLines;
ioctl(pty()->masterFd(), TIOCSWINSZ, &w);
#endif
}
}
QSize Pty::windowSize() const
{
return {_windowColumns, _windowLines};
}
QSize Pty::pixelSize() const
{
return {_windowWidth, _windowHeight};
}
void Pty::setFlowControlEnabled(bool enable)
{
_xonXoff = enable;
if (pty()->masterFd() >= 0) {
struct ::termios ttmode;
pty()->tcGetAttr(&ttmode);
if (enable) {
ttmode.c_iflag |= (IXOFF | IXON);
} else {
ttmode.c_iflag &= ~(IXOFF | IXON);
}
if (!pty()->tcSetAttr(&ttmode)) {
qDebug() << "Unable to set terminal attributes.";
}
}
}
bool Pty::flowControlEnabled() const
{
if (pty()->masterFd() >= 0) {
struct ::termios ttmode;
pty()->tcGetAttr(&ttmode);
return ((ttmode.c_iflag & IXOFF) != 0U) && ((ttmode.c_iflag & IXON) != 0U);
} else {
qDebug() << "Unable to get flow control status, terminal not connected.";
return _xonXoff;
}
}
void Pty::setUtf8Mode(bool enable)
{
#if defined(IUTF8) // XXX not a reasonable place to check it.
_utf8 = enable;
if (pty()->masterFd() >= 0) {
struct ::termios ttmode;
pty()->tcGetAttr(&ttmode);
if (enable) {
ttmode.c_iflag |= IUTF8;
} else {
ttmode.c_iflag &= ~IUTF8;
}
if (!pty()->tcSetAttr(&ttmode)) {
qDebug() << "Unable to set terminal attributes.";
}
}
#else
Q_UNUSED(enable)
#endif
}
void Pty::setEraseChar(char eChar)
{
_eraseChar = eChar;
if (pty()->masterFd() >= 0) {
struct ::termios ttmode;
pty()->tcGetAttr(&ttmode);
ttmode.c_cc[VERASE] = eChar;
if (!pty()->tcSetAttr(&ttmode)) {
qDebug() << "Unable to set terminal attributes.";
}
}
}
char Pty::eraseChar() const
{
if (pty()->masterFd() >= 0) {
struct ::termios ttyAttributes;
pty()->tcGetAttr(&ttyAttributes);
return ttyAttributes.c_cc[VERASE];
} else {
qDebug() << "Unable to get erase char attribute, terminal not connected.";
return _eraseChar;
}
}
void Pty::setInitialWorkingDirectory(const QString &dir)
{
QString pwd = dir;
// remove trailing slash in the path when appropriate
// example: /usr/share/icons/ ==> /usr/share/icons
if (pwd.length() > 1 && pwd.endsWith(QLatin1Char('/'))) {
pwd.chop(1);
}
setWorkingDirectory(pwd);
// setting PWD to "." will cause problem for bash & zsh
if (pwd != QLatin1String(".")) {
setEnv(QStringLiteral("PWD"), pwd);
}
}
void Pty::addEnvironmentVariables(const QStringList &environmentVariables)
{
bool isTermEnvAdded = false;
for (const QString &pair : environmentVariables) {
// split on the first '=' character
const int separator = pair.indexOf(QLatin1Char('='));
if (separator >= 0) {
QString variable = pair.left(separator);
QString value = pair.mid(separator + 1);
setEnv(variable, value);
if (variable == QLatin1String("TERM")) {
isTermEnvAdded = true;
}
}
}
// extra safeguard to make sure $TERM is always set
if (!isTermEnvAdded) {
setEnv(QStringLiteral("TERM"), QStringLiteral("xterm-256color"));
}
}
int Pty::start(const QString &programName, const QStringList &programArguments, const QStringList &environmentList)
{
clearProgram();
setProgram(programName, programArguments);
addEnvironmentVariables(environmentList);
// unless the LANGUAGE environment variable has been set explicitly
// set it to a null string
// this fixes the problem where KCatalog sets the LANGUAGE environment
// variable during the application's startup to something which
// differs from LANG,LC_* etc. and causes programs run from
// the terminal to display messages in the wrong language
//
// this can happen if LANG contains a language which KDE
// does not have a translation for
//
// BR:149300
setEnv(QStringLiteral("LANGUAGE"), QString(), false /* do not overwrite existing value if any */);
KProcess::start();
if (waitForStarted()) {
return 0;
} else {
return -1;
}
}
void Pty::setWriteable(bool writeable)
{
QT_STATBUF sbuf;
if (QT_STAT(pty()->ttyName(), &sbuf) == 0) {
if (writeable) {
if (::chmod(pty()->ttyName(), sbuf.st_mode | S_IWGRP) < 0) {
qDebug() << "Could not set writeable on " << pty()->ttyName();
}
} else {
if (::chmod(pty()->ttyName(), sbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) {
qDebug() << "Could not unset writeable on " << pty()->ttyName();
}
}
} else {
qDebug() << "Could not stat " << pty()->ttyName();
}
}
void Pty::closePty()
{
pty()->close();
}
int Pty::foregroundProcessGroup() const
{
const int master_fd = pty()->masterFd();
if (master_fd >= 0) {
int foregroundPid = tcgetpgrp(master_fd);
if (foregroundPid != -1) {
return foregroundPid;
}
}
return 0;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void Pty::setupChildProcess()
{
KPtyProcess::setupChildProcess();
// reset all signal handlers
// this ensures that terminal applications respond to
// signals generated via key sequences such as Ctrl+C
// (which sends SIGINT)
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_DFL;
action.sa_flags = 0;
for (int signal = 1; signal < NSIG; signal++) {
sigaction(signal, &action, nullptr);
}
}
#endif
|