| 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 Konsole, an X terminal. |
| 11 | Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de> |
| 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 <errno.h> |
| 37 | #include <termios.h> |
| 38 | #include <signal.h> |
| 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 QSize(_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 | |
| 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 |
| 183 | // |
| 184 | // this can happen if LANG contains a language which KDE |
| 185 | // does not have a translation for |
| 186 | // |
| 187 | // BR:149300 |
| 188 | setEnv(QLatin1String("LANGUAGE" ),QString(),false /* do not overwrite existing value if any */); |
| 189 | |
| 190 | setUseUtmp(addToUtmp); |
| 191 | |
| 192 | struct ::termios ttmode; |
| 193 | pty()->tcGetAttr(&ttmode); |
| 194 | if (!_xonXoff) |
| 195 | ttmode.c_iflag &= ~(IXOFF | IXON); |
| 196 | else |
| 197 | ttmode.c_iflag |= (IXOFF | IXON); |
| 198 | #ifdef IUTF8 // XXX not a reasonable place to check it. |
| 199 | if (!_utf8) |
| 200 | ttmode.c_iflag &= ~IUTF8; |
| 201 | else |
| 202 | ttmode.c_iflag |= IUTF8; |
| 203 | #endif |
| 204 | |
| 205 | if (_eraseChar != 0) |
| 206 | ttmode.c_cc[VERASE] = _eraseChar; |
| 207 | |
| 208 | if (!pty()->tcSetAttr(&ttmode)) |
| 209 | qWarning() << "Unable to set terminal attributes." ; |
| 210 | |
| 211 | pty()->setWinSize(_windowLines, _windowColumns); |
| 212 | |
| 213 | KProcess::start(); |
| 214 | |
| 215 | if (!waitForStarted()) |
| 216 | return -1; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | void Pty::setEmptyPTYProperties() |
| 222 | { |
| 223 | struct ::termios ttmode; |
| 224 | pty()->tcGetAttr(&ttmode); |
| 225 | if (!_xonXoff) |
| 226 | ttmode.c_iflag &= ~(IXOFF | IXON); |
| 227 | else |
| 228 | ttmode.c_iflag |= (IXOFF | IXON); |
| 229 | #ifdef IUTF8 // XXX not a reasonable place to check it. |
| 230 | if (!_utf8) |
| 231 | ttmode.c_iflag &= ~IUTF8; |
| 232 | else |
| 233 | ttmode.c_iflag |= IUTF8; |
| 234 | #endif |
| 235 | |
| 236 | if (_eraseChar != 0) |
| 237 | ttmode.c_cc[VERASE] = _eraseChar; |
| 238 | |
| 239 | if (!pty()->tcSetAttr(&ttmode)) |
| 240 | qWarning() << "Unable to set terminal attributes." ; |
| 241 | } |
| 242 | |
| 243 | void Pty::setWriteable(bool writeable) |
| 244 | { |
| 245 | struct stat sbuf; |
| 246 | stat(pty()->ttyName(), &sbuf); |
| 247 | if (writeable) |
| 248 | chmod(pty()->ttyName(), sbuf.st_mode | S_IWGRP); |
| 249 | else |
| 250 | chmod(pty()->ttyName(), sbuf.st_mode & ~(S_IWGRP|S_IWOTH)); |
| 251 | } |
| 252 | |
| 253 | Pty::Pty(int masterFd, QObject* parent) |
| 254 | : KPtyProcess(masterFd,parent) |
| 255 | { |
| 256 | init(); |
| 257 | } |
| 258 | Pty::Pty(QObject* parent) |
| 259 | : KPtyProcess(parent) |
| 260 | { |
| 261 | init(); |
| 262 | } |
| 263 | void Pty::init() |
| 264 | { |
| 265 | _windowColumns = 0; |
| 266 | _windowLines = 0; |
| 267 | _eraseChar = 0; |
| 268 | _xonXoff = true; |
| 269 | _utf8 =true; |
| 270 | |
| 271 | connect(pty(), SIGNAL(readyRead()) , this , SLOT(dataReceived())); |
| 272 | setPtyChannels(KPtyProcess::AllChannels); |
| 273 | } |
| 274 | |
| 275 | Pty::~Pty() |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | void Pty::sendData(const char* data, int length) |
| 280 | { |
| 281 | if (!length) |
| 282 | return; |
| 283 | |
| 284 | if (!pty()->write(data,length)) |
| 285 | { |
| 286 | qWarning() << "Pty::doSendJobs - Could not send input data to terminal process." ; |
| 287 | return; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void Pty::dataReceived() |
| 292 | { |
| 293 | QByteArray data = pty()->readAll(); |
| 294 | emit receivedData(data.constData(),data.count()); |
| 295 | } |
| 296 | |
| 297 | void Pty::lockPty(bool lock) |
| 298 | { |
| 299 | Q_UNUSED(lock); |
| 300 | |
| 301 | // TODO: Support for locking the Pty |
| 302 | //if (lock) |
| 303 | //suspend(); |
| 304 | //else |
| 305 | //resume(); |
| 306 | } |
| 307 | |
| 308 | int Pty::foregroundProcessGroup() const |
| 309 | { |
| 310 | int pid = tcgetpgrp(pty()->masterFd()); |
| 311 | |
| 312 | if ( pid != -1 ) |
| 313 | { |
| 314 | return pid; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | void Pty::setupChildProcess() |
| 321 | { |
| 322 | KPtyProcess::setupChildProcess(); |
| 323 | |
| 324 | // reset all signal handlers |
| 325 | // this ensures that terminal applications respond to |
| 326 | // signals generated via key sequences such as Ctrl+C |
| 327 | // (which sends SIGINT) |
| 328 | struct sigaction action; |
| 329 | sigset_t sigset; |
| 330 | sigemptyset(&action.sa_mask); |
| 331 | action.sa_handler = SIG_DFL; |
| 332 | action.sa_flags = 0; |
| 333 | for (int signal=1;signal < NSIG; signal++) { |
| 334 | sigaction(signal,&action,0L); |
| 335 | sigaddset(&sigset, signal); |
| 336 | } |
| 337 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); |
| 338 | } |
| 339 | |