annotate hgrunner.cpp @ 210:c5fceb3fe5b4

* OS/X: Add script to run external diff; add space around tab widget; textual fixes * Bail out of easyhg.py in uisetup rather than at extension load time if we can't load PyQt4 -- that way we find out about it in easyhg * Ensure editing the default path works even if hgrc doesn't yet exist; ensure path and branch are reset (to empty and default) if their respective files are absent
author Chris Cannam
date Wed, 05 Jan 2011 14:31:07 +0000
parents ff0d76dcb3b8
children 90e70a9024f3
rev   line source
Chris@57 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@57 2
Chris@57 3 /*
Chris@57 4 EasyMercurial
Chris@57 5
Chris@57 6 Based on HgExplorer by Jari Korhonen
Chris@57 7 Copyright (c) 2010 Jari Korhonen
Chris@57 8 Copyright (c) 2010 Chris Cannam
Chris@57 9 Copyright (c) 2010 Queen Mary, University of London
Chris@57 10
Chris@57 11 This program is free software; you can redistribute it and/or
Chris@57 12 modify it under the terms of the GNU General Public License as
Chris@57 13 published by the Free Software Foundation; either version 2 of the
Chris@57 14 License, or (at your option) any later version. See the file
Chris@57 15 COPYING included with this distribution for more information.
Chris@57 16 */
jtkorhonen@0 17
jtkorhonen@0 18 #include "hgrunner.h"
Chris@62 19 #include "common.h"
Chris@57 20 #include "debug.h"
Chris@50 21
Chris@50 22 #include <QPushButton>
Chris@50 23 #include <QListWidget>
Chris@50 24 #include <QDialog>
Chris@50 25 #include <QLabel>
Chris@50 26 #include <QVBoxLayout>
Chris@62 27 #include <QSettings>
Chris@75 28 #include <QInputDialog>
Chris@188 29 #include <QTemporaryFile>
Chris@161 30 #include <QDir>
jtkorhonen@0 31
Chris@43 32 #include <iostream>
Chris@75 33 #include <errno.h>
Chris@75 34 #include <stdio.h>
Chris@111 35 #include <stdlib.h>
jtkorhonen@0 36
Chris@76 37 #ifndef Q_OS_WIN32
Chris@111 38 #include <unistd.h>
Chris@113 39 #include <termios.h>
Chris@111 40 #include <fcntl.h>
Chris@80 41 #endif
Chris@76 42
Chris@172 43 HgRunner::HgRunner(QString myDirPath, QWidget * parent) :
Chris@172 44 QProgressBar(parent),
Chris@172 45 m_myDirPath(myDirPath)
jtkorhonen@0 46 {
Chris@113 47 m_proc = 0;
Chris@84 48
jtkorhonen@0 49 setTextVisible(false);
jtkorhonen@0 50 setVisible(false);
Chris@84 51 m_isRunning = false;
Chris@161 52
Chris@180 53 (void)findExtension();
Chris@180 54 (void)findHgBinaryName();
jtkorhonen@0 55 }
jtkorhonen@0 56
jtkorhonen@0 57 HgRunner::~HgRunner()
jtkorhonen@0 58 {
Chris@111 59 closeTerminal();
Chris@120 60 if (m_proc) {
Chris@120 61 m_proc->kill();
Chris@122 62 m_proc->deleteLater();
Chris@120 63 }
jtkorhonen@0 64 }
jtkorhonen@0 65
Chris@180 66 QString HgRunner::findExtension()
Chris@172 67 {
Chris@210 68 // Always unbundle the extension: even if it already exists (in
Chris@210 69 // case we're upgrading) and even if we're not going to use it (so
Chris@210 70 // that it's available in case someone wants to use it later,
Chris@210 71 // e.g. to fix a malfunctioning setup). But the path we actually
Chris@210 72 // prefer is the one in the settings first, if it exists; then the
Chris@189 73 // unbundled one; then anything in the path if for some reason
Chris@189 74 // unbundling failed
Chris@188 75
Chris@176 76 QSettings settings;
Chris@176 77 settings.beginGroup("Locations");
Chris@188 78
Chris@210 79 QString unbundled = unbundleExtension();
Chris@180 80 QString extpath = settings.value("extensionpath", "").toString();
Chris@189 81 if (extpath != "" && QFile(extpath).exists()) return extpath;
Chris@188 82
Chris@188 83 extpath = unbundled;
Chris@188 84 if (extpath == "") {
Chris@188 85 extpath = findInPath("easyhg.py", m_myDirPath, false);
Chris@188 86 if (extpath == "easyhg.py") {
Chris@188 87 extpath = "";
Chris@188 88 }
Chris@172 89 }
Chris@188 90
Chris@180 91 settings.setValue("extensionpath", extpath);
Chris@180 92 return extpath;
Chris@172 93 }
Chris@172 94
Chris@188 95 QString HgRunner::getUnbundledFileName()
Chris@188 96 {
Chris@188 97 QString home = QDir::homePath();
Chris@188 98 QString target = QString("%1/.easyhg").arg(home);
Chris@188 99 QString extpath = QString("%1/easyhg.py").arg(target);
Chris@188 100 return extpath;
Chris@188 101 }
Chris@188 102
Chris@180 103 QString HgRunner::unbundleExtension()
Chris@161 104 {
Chris@188 105 // Pull out the bundled Python file into a temporary file, and
Chris@188 106 // copy it to our known extension location, replacing the magic
Chris@188 107 // text NO_EASYHG_IMPORT_PATH with our installation location
Chris@188 108
Chris@161 109 QString bundled = ":easyhg.py";
Chris@188 110 QString unbundled = getUnbundledFileName();
Chris@188 111
Chris@188 112 QString target = QFileInfo(unbundled).path();
Chris@161 113 if (!QDir().mkpath(target)) {
Chris@161 114 DEBUG << "Failed to make unbundle path " << target << endl;
Chris@188 115 std::cerr << "Failed to make unbundle path " << target << std::endl;
Chris@180 116 return "";
Chris@161 117 }
Chris@188 118
Chris@161 119 QFile bf(bundled);
Chris@188 120 DEBUG << "unbundle: bundled file will be " << bundled << endl;
Chris@188 121 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
Chris@180 122 DEBUG << "Bundled extension is missing!" << endl;
Chris@180 123 return "";
Chris@180 124 }
Chris@188 125
Chris@188 126 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
Chris@188 127 tmpfile.setAutoRemove(false);
Chris@188 128 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
Chris@188 129 if (!tmpfile.open()) {
Chris@188 130 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
Chris@188 131 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
Chris@180 132 return "";
Chris@161 133 }
Chris@188 134
Chris@188 135 QString all = QString::fromUtf8(bf.readAll());
Chris@188 136 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
Chris@188 137 tmpfile.write(all.toUtf8());
Chris@188 138 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
Chris@188 139
Chris@188 140 tmpfile.close();
Chris@188 141
Chris@188 142 QFile ef(unbundled);
Chris@188 143 if (ef.exists()) {
Chris@188 144 DEBUG << "unbundle: removing old file " << unbundled << endl;
Chris@188 145 ef.remove();
Chris@188 146 }
Chris@188 147 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
Chris@188 148 if (!tmpfile.rename(unbundled)) {
Chris@188 149 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
Chris@188 150 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
Chris@188 151 return "";
Chris@188 152 }
Chris@188 153
Chris@188 154 DEBUG << "Unbundled extension to " << unbundled << endl;
Chris@188 155 return unbundled;
Chris@161 156 }
Chris@161 157
Chris@109 158 void HgRunner::requestAction(HgAction action)
Chris@109 159 {
Chris@109 160 DEBUG << "requestAction " << action.action << endl;
Chris@109 161 bool pushIt = true;
Chris@109 162 if (m_queue.empty()) {
Chris@109 163 if (action == m_currentAction) {
Chris@109 164 // this request is identical to the thing we're executing
Chris@109 165 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
Chris@109 166 pushIt = false;
Chris@109 167 }
Chris@109 168 } else {
Chris@109 169 HgAction last = m_queue.back();
Chris@109 170 if (action == last) {
Chris@109 171 // this request is identical to the previous thing we
Chris@109 172 // queued which we haven't executed yet
Chris@109 173 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
Chris@109 174 pushIt = false;
Chris@109 175 }
Chris@109 176 }
Chris@109 177 if (pushIt) m_queue.push_back(action);
Chris@109 178 checkQueue();
Chris@109 179 }
Chris@109 180
Chris@180 181 QString HgRunner::findHgBinaryName()
Chris@62 182 {
Chris@62 183 QSettings settings;
Chris@175 184 settings.beginGroup("Locations");
Chris@77 185 QString hg = settings.value("hgbinary", "").toString();
Chris@77 186 if (hg == "") {
Chris@172 187 hg = findInPath("hg", m_myDirPath, true);
Chris@77 188 }
Chris@77 189 if (hg != "hg") {
Chris@77 190 settings.setValue("hgbinary", hg);
Chris@77 191 }
Chris@62 192 return hg;
Chris@62 193 }
Chris@62 194
jtkorhonen@0 195 void HgRunner::started()
jtkorhonen@0 196 {
Chris@104 197 DEBUG << "started" << endl;
Chris@75 198 /*
Chris@104 199 m_proc->write("blah\n");
Chris@104 200 m_proc->write("blah\n");
Chris@104 201 m_proc -> closeWriteChannel();
Chris@75 202 */
jtkorhonen@0 203 }
jtkorhonen@0 204
Chris@84 205 void HgRunner::noteUsername(QString name)
Chris@75 206 {
Chris@84 207 m_userName = name;
Chris@75 208 }
Chris@75 209
Chris@84 210 void HgRunner::noteRealm(QString realm)
Chris@75 211 {
Chris@84 212 m_realm = realm;
Chris@84 213 }
Chris@84 214
Chris@84 215 void HgRunner::getUsername()
Chris@84 216 {
Chris@113 217 if (m_ptyFile) {
Chris@84 218 bool ok = false;
Chris@84 219 QString prompt = tr("User name:");
Chris@84 220 if (m_realm != "") {
Chris@84 221 prompt = tr("User name for \"%1\":").arg(m_realm);
Chris@84 222 }
Chris@84 223 QString pwd = QInputDialog::getText
Chris@84 224 (qobject_cast<QWidget *>(parent()),
Chris@84 225 tr("Enter user name"), prompt,
Chris@84 226 QLineEdit::Normal, QString(), &ok);
Chris@84 227 if (ok) {
Chris@113 228 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 229 m_ptyFile->flush();
Chris@84 230 return;
Chris@111 231 } else {
Chris@111 232 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
Chris@111 233 killCurrentCommand();
Chris@111 234 return;
Chris@84 235 }
Chris@84 236 }
Chris@84 237 // user cancelled or something went wrong
Chris@111 238 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
Chris@84 239 killCurrentCommand();
Chris@84 240 }
Chris@84 241
Chris@84 242 void HgRunner::getPassword()
Chris@84 243 {
Chris@113 244 if (m_ptyFile) {
Chris@84 245 bool ok = false;
Chris@84 246 QString prompt = tr("Password:");
Chris@84 247 if (m_userName != "") {
Chris@84 248 if (m_realm != "") {
Chris@84 249 prompt = tr("Password for \"%1\" at \"%2\":")
Chris@84 250 .arg(m_userName).arg(m_realm);
Chris@75 251 } else {
Chris@84 252 prompt = tr("Password for user \"%1\":")
Chris@84 253 .arg(m_userName);
Chris@75 254 }
Chris@75 255 }
Chris@84 256 QString pwd = QInputDialog::getText
Chris@84 257 (qobject_cast<QWidget *>(parent()),
Chris@84 258 tr("Enter password"), prompt,
Chris@84 259 QLineEdit::Password, QString(), &ok);
Chris@84 260 if (ok) {
Chris@113 261 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 262 m_ptyFile->flush();
Chris@84 263 return;
Chris@111 264 } else {
Chris@111 265 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
Chris@111 266 killCurrentCommand();
Chris@111 267 return;
Chris@84 268 }
Chris@75 269 }
Chris@84 270 // user cancelled or something went wrong
Chris@111 271 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
Chris@84 272 killCurrentCommand();
Chris@84 273 }
Chris@84 274
Chris@113 275 bool HgRunner::checkPrompts(QString chunk)
Chris@84 276 {
Chris@93 277 //DEBUG << "checkPrompts: " << chunk << endl;
Chris@84 278
Chris@128 279 if (!m_currentAction.mayBeInteractive()) return false;
Chris@128 280
Chris@84 281 QString text = chunk.trimmed();
Chris@84 282 QString lower = text.toLower();
Chris@84 283 if (lower.endsWith("password:")) {
Chris@84 284 getPassword();
Chris@113 285 return true;
Chris@84 286 }
Chris@128 287 if (lower.endsWith("user:") || lower.endsWith("username:")) {
Chris@84 288 getUsername();
Chris@113 289 return true;
Chris@84 290 }
Chris@128 291 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
Chris@84 292 if (userRe.indexIn(text) >= 0) {
Chris@128 293 noteUsername(userRe.cap(2));
Chris@84 294 }
Chris@84 295 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
Chris@84 296 if (realmRe.indexIn(text) >= 0) {
Chris@84 297 noteRealm(realmRe.cap(1));
Chris@84 298 }
Chris@113 299 return false;
Chris@84 300 }
Chris@84 301
Chris@110 302 void HgRunner::dataReadyStdout()
Chris@84 303 {
Chris@110 304 DEBUG << "dataReadyStdout" << endl;
Chris@110 305 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
Chris@113 306 if (!checkPrompts(chunk)) {
Chris@113 307 m_stdout += chunk;
Chris@113 308 }
Chris@110 309 }
Chris@110 310
Chris@110 311 void HgRunner::dataReadyStderr()
Chris@110 312 {
Chris@110 313 DEBUG << "dataReadyStderr" << endl;
Chris@110 314 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
Chris@113 315 DEBUG << chunk;
Chris@113 316 if (!checkPrompts(chunk)) {
Chris@113 317 m_stderr += chunk;
Chris@113 318 }
Chris@113 319 }
Chris@113 320
Chris@113 321 void HgRunner::dataReadyPty()
Chris@113 322 {
Chris@113 323 DEBUG << "dataReadyPty" << endl;
Chris@113 324 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
Chris@113 325 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
Chris@113 326 if (!checkPrompts(chunk)) {
Chris@113 327 m_stdout += chunk;
Chris@113 328 }
Chris@75 329 }
Chris@75 330
Chris@189 331 void HgRunner::error(QProcess::ProcessError)
Chris@189 332 {
Chris@189 333 finished(-1, QProcess::CrashExit);
Chris@189 334 }
Chris@189 335
jtkorhonen@0 336 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
jtkorhonen@0 337 {
Chris@109 338 // Save the current action and reset m_currentAction before we
Chris@109 339 // emit a signal to mark the completion; otherwise we may be
Chris@109 340 // resetting the action after a slot has already tried to set it
Chris@109 341 // to something else to start a new action
Chris@109 342
Chris@109 343 HgAction completedAction = m_currentAction;
Chris@109 344
Chris@84 345 m_isRunning = false;
Chris@109 346 m_currentAction = HgAction();
Chris@84 347
Chris@195 348 //closeProcInput();
Chris@189 349 m_proc->deleteLater();
Chris@113 350 m_proc = 0;
jtkorhonen@0 351
Chris@109 352 if (completedAction.action == ACT_NONE) {
Chris@109 353 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
Chris@62 354 } else {
Chris@113 355 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
Chris@113 356 DEBUG << "HgRunner::finished: Command completed successfully"
Chris@113 357 << endl;
Chris@124 358 // DEBUG << "stdout is " << m_stdout << endl;
Chris@113 359 emit commandCompleted(completedAction, m_stdout);
Chris@113 360 } else {
Chris@113 361 DEBUG << "HgRunner::finished: Command failed, exit code "
Chris@113 362 << procExitCode << ", exit status " << procExitStatus
Chris@113 363 << ", stderr follows" << endl;
Chris@113 364 DEBUG << m_stderr << endl;
Chris@113 365 emit commandFailed(completedAction, m_stderr);
Chris@113 366 }
jtkorhonen@0 367 }
Chris@109 368
Chris@109 369 checkQueue();
jtkorhonen@0 370 }
jtkorhonen@0 371
Chris@182 372 void HgRunner::killCurrentActions()
Chris@182 373 {
Chris@182 374 m_queue.clear();
Chris@182 375 killCurrentCommand();
Chris@182 376 }
Chris@182 377
Chris@62 378 void HgRunner::killCurrentCommand()
jtkorhonen@0 379 {
Chris@109 380 if (m_isRunning) {
Chris@113 381 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
Chris@113 382 m_proc->kill();
jtkorhonen@0 383 }
jtkorhonen@0 384 }
jtkorhonen@0 385
Chris@109 386 void HgRunner::checkQueue()
Chris@62 387 {
Chris@109 388 if (m_isRunning) {
Chris@109 389 return;
Chris@109 390 }
Chris@109 391 if (m_queue.empty()) {
Chris@109 392 hide();
Chris@109 393 return;
Chris@109 394 }
Chris@109 395 HgAction toRun = m_queue.front();
Chris@109 396 m_queue.pop_front();
Chris@109 397 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
Chris@109 398 startCommand(toRun);
Chris@109 399 }
Chris@109 400
Chris@109 401 void HgRunner::startCommand(HgAction action)
Chris@109 402 {
Chris@109 403 QString executable = action.executable;
Chris@109 404 bool interactive = false;
Chris@109 405 QStringList params = action.params;
Chris@109 406
Chris@109 407 if (executable == "") {
Chris@109 408 // This is a Hg command
Chris@180 409 executable = findHgBinaryName();
Chris@161 410
Chris@161 411 if (action.mayBeInteractive()) {
Chris@161 412 params.push_front("ui.interactive=true");
Chris@161 413 params.push_front("--config");
Chris@176 414
Chris@176 415 QSettings settings;
Chris@176 416 settings.beginGroup("General");
Chris@176 417 if (settings.value("useextension", true).toBool()) {
Chris@180 418 QString extpath = findExtension();
Chris@180 419 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
Chris@176 420 params.push_front("--config");
Chris@176 421 }
Chris@161 422 interactive = true;
Chris@161 423 }
Chris@161 424
Chris@161 425 //!!! want an option to use the mercurial_keyring extension as well
Chris@107 426 }
jtkorhonen@0 427
Chris@84 428 m_isRunning = true;
jtkorhonen@0 429 setRange(0, 0);
Chris@115 430 if (!action.shouldBeFast()) show();
Chris@110 431 m_stdout.clear();
Chris@110 432 m_stderr.clear();
Chris@84 433 m_realm = "";
Chris@84 434 m_userName = "";
jtkorhonen@0 435
Chris@113 436 m_proc = new QProcess;
Chris@113 437
Chris@177 438 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
Chris@177 439
Chris@177 440 #ifdef Q_OS_WIN32
Chris@177 441 if (m_myDirPath != "") {
Chris@177 442 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
Chris@172 443 }
Chris@172 444 #endif
Chris@172 445
Chris@113 446 env.insert("LANG", "en_US.utf8");
Chris@113 447 env.insert("LC_ALL", "en_US.utf8");
Chris@113 448 env.insert("HGPLAIN", "1");
Chris@113 449 m_proc->setProcessEnvironment(env);
Chris@113 450
Chris@113 451 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
Chris@189 452 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
Chris@189 453 this, SLOT(error(QProcess::ProcessError)));
Chris@113 454 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@113 455 this, SLOT(finished(int, QProcess::ExitStatus)));
Chris@113 456 connect(m_proc, SIGNAL(readyReadStandardOutput()),
Chris@113 457 this, SLOT(dataReadyStdout()));
Chris@113 458 connect(m_proc, SIGNAL(readyReadStandardError()),
Chris@113 459 this, SLOT(dataReadyStderr()));
Chris@113 460
Chris@109 461 if (!action.workingDir.isEmpty()) {
Chris@109 462 m_proc->setWorkingDirectory(action.workingDir);
jtkorhonen@0 463 }
jtkorhonen@0 464
Chris@107 465 if (interactive) {
Chris@111 466 openTerminal();
Chris@111 467 if (m_ptySlaveFilename != "") {
Chris@113 468 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
Chris@107 469 m_proc->setStandardInputFile(m_ptySlaveFilename);
Chris@114 470 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
Chris@113 471 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
Chris@107 472 }
Chris@84 473 }
Chris@84 474
Chris@109 475 QString cmdline = executable;
Chris@57 476 foreach (QString param, params) cmdline += " " + param;
Chris@64 477 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
Chris@109 478 << action.workingDir << endl;
Chris@43 479
Chris@109 480 m_currentAction = action;
Chris@109 481
Chris@113 482 // fill these out with what we actually ran
Chris@113 483 m_currentAction.executable = executable;
Chris@113 484 m_currentAction.params = params;
Chris@113 485
Chris@109 486 DEBUG << "set current action to " << m_currentAction.action << endl;
Chris@109 487
Chris@109 488 m_proc->start(executable, params);
Chris@84 489 }
Chris@84 490
Chris@84 491 void HgRunner::closeProcInput()
Chris@84 492 {
Chris@84 493 DEBUG << "closeProcInput" << endl;
Chris@84 494
Chris@84 495 m_proc->closeWriteChannel();
Chris@111 496 }
Chris@111 497
Chris@111 498 void HgRunner::openTerminal()
Chris@111 499 {
Chris@111 500 #ifndef Q_OS_WIN32
Chris@111 501 if (m_ptySlaveFilename != "") return; // already open
Chris@111 502 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
Chris@111 503 int master = posix_openpt(O_RDWR | O_NOCTTY);
Chris@111 504 if (master < 0) {
Chris@111 505 DEBUG << "openpt failed" << endl;
Chris@111 506 perror("openpt failed");
Chris@111 507 return;
Chris@111 508 }
Chris@113 509 struct termios t;
Chris@113 510 if (tcgetattr(master, &t)) {
Chris@113 511 DEBUG << "tcgetattr failed" << endl;
Chris@113 512 perror("tcgetattr failed");
Chris@113 513 }
Chris@113 514 cfmakeraw(&t);
Chris@113 515 if (tcsetattr(master, TCSANOW, &t)) {
Chris@113 516 DEBUG << "tcsetattr failed" << endl;
Chris@113 517 perror("tcsetattr failed");
Chris@113 518 }
Chris@111 519 if (grantpt(master)) {
Chris@111 520 perror("grantpt failed");
Chris@111 521 }
Chris@111 522 if (unlockpt(master)) {
Chris@111 523 perror("unlockpt failed");
Chris@111 524 }
Chris@111 525 char *slave = ptsname(master);
Chris@111 526 if (!slave) {
Chris@111 527 perror("ptsname failed");
Chris@111 528 ::close(master);
Chris@111 529 return;
Chris@111 530 }
Chris@111 531 m_ptyMasterFd = master;
Chris@113 532 m_ptyFile = new QFile();
Chris@113 533 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
Chris@113 534 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
Chris@113 535 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
Chris@113 536 }
Chris@111 537 m_ptySlaveFilename = slave;
Chris@111 538 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
Chris@111 539 << m_ptySlaveFilename << endl;
Chris@111 540 #endif
Chris@111 541 }
Chris@111 542
Chris@111 543 void HgRunner::closeTerminal()
Chris@111 544 {
Chris@84 545 #ifndef Q_OS_WIN32
Chris@84 546 if (m_ptySlaveFilename != "") {
Chris@113 547 delete m_ptyFile;
Chris@113 548 m_ptyFile = 0;
Chris@84 549 ::close(m_ptyMasterFd);
Chris@84 550 m_ptySlaveFilename = "";
Chris@84 551 }
Chris@84 552 #endif
jtkorhonen@0 553 }