annotate src/hgrunner.cpp @ 444:66ec8b2ee946

Provide auth key and file information to easyhg extension
author Chris Cannam
date Tue, 28 Jun 2011 15:59:13 +0100
parents 005633eed862
children 856da063d76e
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@244 8 Copyright (c) 2011 Chris Cannam
Chris@244 9 Copyright (c) 2011 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@240 21 #include "settingsdialog.h"
Chris@50 22
Chris@50 23 #include <QPushButton>
Chris@50 24 #include <QListWidget>
Chris@50 25 #include <QDialog>
Chris@50 26 #include <QLabel>
Chris@50 27 #include <QVBoxLayout>
Chris@62 28 #include <QSettings>
Chris@75 29 #include <QInputDialog>
Chris@444 30 #include <QDesktopServices>
Chris@188 31 #include <QTemporaryFile>
Chris@161 32 #include <QDir>
jtkorhonen@0 33
Chris@43 34 #include <iostream>
Chris@75 35 #include <errno.h>
Chris@75 36 #include <stdio.h>
Chris@111 37 #include <stdlib.h>
jtkorhonen@0 38
Chris@76 39 #ifndef Q_OS_WIN32
Chris@111 40 #include <unistd.h>
Chris@113 41 #include <termios.h>
Chris@111 42 #include <fcntl.h>
Chris@80 43 #endif
Chris@76 44
Chris@172 45 HgRunner::HgRunner(QString myDirPath, QWidget * parent) :
Chris@172 46 QProgressBar(parent),
Chris@172 47 m_myDirPath(myDirPath)
jtkorhonen@0 48 {
Chris@113 49 m_proc = 0;
Chris@84 50
Chris@239 51 // Always unbundle the extension: even if it already exists (in
Chris@239 52 // case we're upgrading) and even if we're not going to use it (so
Chris@239 53 // that it's available in case someone wants to use it later,
Chris@239 54 // e.g. to fix a malfunctioning setup). But the path we actually
Chris@239 55 // prefer is the one in the settings first, if it exists; then the
Chris@239 56 // unbundled one; then anything in the path if for some reason
Chris@239 57 // unbundling failed
Chris@239 58 unbundleExtension();
Chris@239 59
jtkorhonen@0 60 setTextVisible(false);
jtkorhonen@0 61 setVisible(false);
Chris@84 62 m_isRunning = false;
jtkorhonen@0 63 }
jtkorhonen@0 64
jtkorhonen@0 65 HgRunner::~HgRunner()
jtkorhonen@0 66 {
Chris@111 67 closeTerminal();
Chris@120 68 if (m_proc) {
Chris@120 69 m_proc->kill();
Chris@122 70 m_proc->deleteLater();
Chris@120 71 }
Chris@444 72 if (m_authFilePath != "") {
Chris@444 73 QFile(m_authFilePath).remove();
Chris@444 74 }
Chris@444 75 //!!! and remove any other misc auth file paths...
jtkorhonen@0 76 }
jtkorhonen@0 77
Chris@188 78 QString HgRunner::getUnbundledFileName()
Chris@188 79 {
Chris@240 80 return SettingsDialog::getUnbundledExtensionFileName();
Chris@188 81 }
Chris@188 82
Chris@180 83 QString HgRunner::unbundleExtension()
Chris@161 84 {
Chris@188 85 // Pull out the bundled Python file into a temporary file, and
Chris@188 86 // copy it to our known extension location, replacing the magic
Chris@188 87 // text NO_EASYHG_IMPORT_PATH with our installation location
Chris@188 88
Chris@161 89 QString bundled = ":easyhg.py";
Chris@188 90 QString unbundled = getUnbundledFileName();
Chris@188 91
Chris@188 92 QString target = QFileInfo(unbundled).path();
Chris@161 93 if (!QDir().mkpath(target)) {
Chris@161 94 DEBUG << "Failed to make unbundle path " << target << endl;
Chris@188 95 std::cerr << "Failed to make unbundle path " << target << std::endl;
Chris@180 96 return "";
Chris@161 97 }
Chris@188 98
Chris@161 99 QFile bf(bundled);
Chris@188 100 DEBUG << "unbundle: bundled file will be " << bundled << endl;
Chris@188 101 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
Chris@180 102 DEBUG << "Bundled extension is missing!" << endl;
Chris@180 103 return "";
Chris@180 104 }
Chris@188 105
Chris@188 106 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
Chris@188 107 tmpfile.setAutoRemove(false);
Chris@188 108 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
Chris@188 109 if (!tmpfile.open()) {
Chris@188 110 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
Chris@188 111 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
Chris@180 112 return "";
Chris@161 113 }
Chris@188 114
Chris@188 115 QString all = QString::fromUtf8(bf.readAll());
Chris@188 116 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
Chris@188 117 tmpfile.write(all.toUtf8());
Chris@188 118 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
Chris@188 119
Chris@188 120 tmpfile.close();
Chris@188 121
Chris@188 122 QFile ef(unbundled);
Chris@188 123 if (ef.exists()) {
Chris@188 124 DEBUG << "unbundle: removing old file " << unbundled << endl;
Chris@188 125 ef.remove();
Chris@188 126 }
Chris@188 127 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
Chris@188 128 if (!tmpfile.rename(unbundled)) {
Chris@188 129 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
Chris@188 130 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
Chris@188 131 return "";
Chris@188 132 }
Chris@188 133
Chris@188 134 DEBUG << "Unbundled extension to " << unbundled << endl;
Chris@188 135 return unbundled;
Chris@161 136 }
Chris@161 137
Chris@109 138 void HgRunner::requestAction(HgAction action)
Chris@109 139 {
Chris@109 140 DEBUG << "requestAction " << action.action << endl;
Chris@109 141 bool pushIt = true;
Chris@109 142 if (m_queue.empty()) {
Chris@109 143 if (action == m_currentAction) {
Chris@109 144 // this request is identical to the thing we're executing
Chris@109 145 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
Chris@109 146 pushIt = false;
Chris@109 147 }
Chris@109 148 } else {
Chris@109 149 HgAction last = m_queue.back();
Chris@109 150 if (action == last) {
Chris@109 151 // this request is identical to the previous thing we
Chris@109 152 // queued which we haven't executed yet
Chris@109 153 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
Chris@109 154 pushIt = false;
Chris@109 155 }
Chris@109 156 }
Chris@109 157 if (pushIt) m_queue.push_back(action);
Chris@109 158 checkQueue();
Chris@109 159 }
Chris@109 160
Chris@239 161 QString HgRunner::getHgBinaryName()
Chris@62 162 {
Chris@62 163 QSettings settings;
Chris@175 164 settings.beginGroup("Locations");
Chris@239 165 return settings.value("hgbinary", "").toString();
Chris@62 166 }
Chris@62 167
chris@406 168 QString HgRunner::getSshBinaryName()
chris@406 169 {
chris@406 170 QSettings settings;
chris@406 171 settings.beginGroup("Locations");
chris@406 172 return settings.value("sshbinary", "").toString();
chris@406 173 }
chris@406 174
Chris@239 175 QString HgRunner::getExtensionLocation()
Chris@239 176 {
Chris@239 177 QSettings settings;
Chris@239 178 settings.beginGroup("Locations");
Chris@239 179 QString extpath = settings.value("extensionpath", "").toString();
Chris@239 180 if (extpath != "" && QFile(extpath).exists()) return extpath;
Chris@239 181 return "";
Chris@239 182 }
Chris@239 183
jtkorhonen@0 184 void HgRunner::started()
jtkorhonen@0 185 {
Chris@104 186 DEBUG << "started" << endl;
Chris@75 187 /*
Chris@104 188 m_proc->write("blah\n");
Chris@104 189 m_proc->write("blah\n");
Chris@104 190 m_proc -> closeWriteChannel();
Chris@75 191 */
jtkorhonen@0 192 }
jtkorhonen@0 193
Chris@84 194 void HgRunner::noteUsername(QString name)
Chris@75 195 {
Chris@84 196 m_userName = name;
Chris@75 197 }
Chris@75 198
Chris@84 199 void HgRunner::noteRealm(QString realm)
Chris@75 200 {
Chris@84 201 m_realm = realm;
Chris@84 202 }
Chris@84 203
Chris@84 204 void HgRunner::getUsername()
Chris@84 205 {
Chris@113 206 if (m_ptyFile) {
Chris@84 207 bool ok = false;
Chris@84 208 QString prompt = tr("User name:");
Chris@84 209 if (m_realm != "") {
Chris@84 210 prompt = tr("User name for \"%1\":").arg(m_realm);
Chris@84 211 }
Chris@84 212 QString pwd = QInputDialog::getText
Chris@84 213 (qobject_cast<QWidget *>(parent()),
Chris@84 214 tr("Enter user name"), prompt,
Chris@84 215 QLineEdit::Normal, QString(), &ok);
Chris@84 216 if (ok) {
Chris@113 217 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 218 m_ptyFile->flush();
Chris@84 219 return;
Chris@111 220 } else {
Chris@111 221 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
Chris@111 222 killCurrentCommand();
Chris@111 223 return;
Chris@84 224 }
Chris@84 225 }
Chris@84 226 // user cancelled or something went wrong
Chris@111 227 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
Chris@84 228 killCurrentCommand();
Chris@84 229 }
Chris@84 230
Chris@84 231 void HgRunner::getPassword()
Chris@84 232 {
Chris@113 233 if (m_ptyFile) {
Chris@84 234 bool ok = false;
Chris@84 235 QString prompt = tr("Password:");
Chris@84 236 if (m_userName != "") {
Chris@84 237 if (m_realm != "") {
Chris@84 238 prompt = tr("Password for \"%1\" at \"%2\":")
Chris@84 239 .arg(m_userName).arg(m_realm);
Chris@75 240 } else {
Chris@84 241 prompt = tr("Password for user \"%1\":")
Chris@84 242 .arg(m_userName);
Chris@75 243 }
Chris@75 244 }
Chris@84 245 QString pwd = QInputDialog::getText
Chris@84 246 (qobject_cast<QWidget *>(parent()),
Chris@84 247 tr("Enter password"), prompt,
Chris@84 248 QLineEdit::Password, QString(), &ok);
Chris@84 249 if (ok) {
Chris@113 250 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 251 m_ptyFile->flush();
Chris@84 252 return;
Chris@111 253 } else {
Chris@111 254 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
Chris@111 255 killCurrentCommand();
Chris@111 256 return;
Chris@84 257 }
Chris@75 258 }
Chris@84 259 // user cancelled or something went wrong
Chris@111 260 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
Chris@84 261 killCurrentCommand();
Chris@84 262 }
Chris@84 263
Chris@113 264 bool HgRunner::checkPrompts(QString chunk)
Chris@84 265 {
Chris@93 266 //DEBUG << "checkPrompts: " << chunk << endl;
Chris@84 267
Chris@128 268 if (!m_currentAction.mayBeInteractive()) return false;
Chris@128 269
Chris@84 270 QString text = chunk.trimmed();
Chris@84 271 QString lower = text.toLower();
Chris@84 272 if (lower.endsWith("password:")) {
Chris@84 273 getPassword();
Chris@113 274 return true;
Chris@84 275 }
Chris@128 276 if (lower.endsWith("user:") || lower.endsWith("username:")) {
Chris@84 277 getUsername();
Chris@113 278 return true;
Chris@84 279 }
Chris@128 280 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
Chris@84 281 if (userRe.indexIn(text) >= 0) {
Chris@128 282 noteUsername(userRe.cap(2));
Chris@84 283 }
Chris@84 284 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
Chris@84 285 if (realmRe.indexIn(text) >= 0) {
Chris@84 286 noteRealm(realmRe.cap(1));
Chris@84 287 }
Chris@113 288 return false;
Chris@84 289 }
Chris@84 290
Chris@110 291 void HgRunner::dataReadyStdout()
Chris@84 292 {
Chris@110 293 DEBUG << "dataReadyStdout" << endl;
Chris@408 294 if (!m_proc) return;
Chris@110 295 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
Chris@113 296 if (!checkPrompts(chunk)) {
Chris@113 297 m_stdout += chunk;
Chris@113 298 }
Chris@110 299 }
Chris@110 300
Chris@110 301 void HgRunner::dataReadyStderr()
Chris@110 302 {
Chris@110 303 DEBUG << "dataReadyStderr" << endl;
Chris@408 304 if (!m_proc) return;
Chris@110 305 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
Chris@113 306 DEBUG << chunk;
Chris@113 307 if (!checkPrompts(chunk)) {
Chris@113 308 m_stderr += chunk;
Chris@113 309 }
Chris@113 310 }
Chris@113 311
Chris@113 312 void HgRunner::dataReadyPty()
Chris@113 313 {
Chris@113 314 DEBUG << "dataReadyPty" << endl;
Chris@113 315 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
Chris@113 316 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
Chris@113 317 if (!checkPrompts(chunk)) {
Chris@113 318 m_stdout += chunk;
Chris@113 319 }
Chris@75 320 }
Chris@75 321
Chris@189 322 void HgRunner::error(QProcess::ProcessError)
Chris@189 323 {
Chris@189 324 finished(-1, QProcess::CrashExit);
Chris@189 325 }
Chris@189 326
jtkorhonen@0 327 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
jtkorhonen@0 328 {
chris@385 329 if (!m_proc) return;
chris@385 330
chris@385 331 // Save the current action and reset m_currentAction before we
Chris@109 332 // emit a signal to mark the completion; otherwise we may be
Chris@109 333 // resetting the action after a slot has already tried to set it
Chris@109 334 // to something else to start a new action
Chris@109 335
Chris@109 336 HgAction completedAction = m_currentAction;
Chris@109 337
Chris@84 338 m_isRunning = false;
Chris@109 339 m_currentAction = HgAction();
Chris@84 340
Chris@195 341 //closeProcInput();
Chris@189 342 m_proc->deleteLater();
Chris@113 343 m_proc = 0;
jtkorhonen@0 344
Chris@109 345 if (completedAction.action == ACT_NONE) {
Chris@109 346 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
Chris@62 347 } else {
Chris@113 348 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
Chris@113 349 DEBUG << "HgRunner::finished: Command completed successfully"
Chris@113 350 << endl;
Chris@124 351 // DEBUG << "stdout is " << m_stdout << endl;
Chris@113 352 emit commandCompleted(completedAction, m_stdout);
Chris@113 353 } else {
Chris@113 354 DEBUG << "HgRunner::finished: Command failed, exit code "
Chris@113 355 << procExitCode << ", exit status " << procExitStatus
Chris@113 356 << ", stderr follows" << endl;
Chris@113 357 DEBUG << m_stderr << endl;
Chris@113 358 emit commandFailed(completedAction, m_stderr);
Chris@113 359 }
jtkorhonen@0 360 }
Chris@109 361
Chris@109 362 checkQueue();
jtkorhonen@0 363 }
jtkorhonen@0 364
Chris@182 365 void HgRunner::killCurrentActions()
Chris@182 366 {
Chris@182 367 m_queue.clear();
Chris@182 368 killCurrentCommand();
Chris@182 369 }
Chris@182 370
Chris@62 371 void HgRunner::killCurrentCommand()
jtkorhonen@0 372 {
Chris@109 373 if (m_isRunning) {
Chris@113 374 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
chris@385 375 if (m_proc) m_proc->kill();
jtkorhonen@0 376 }
jtkorhonen@0 377 }
jtkorhonen@0 378
Chris@109 379 void HgRunner::checkQueue()
Chris@62 380 {
Chris@109 381 if (m_isRunning) {
Chris@109 382 return;
Chris@109 383 }
Chris@109 384 if (m_queue.empty()) {
Chris@109 385 hide();
Chris@109 386 return;
Chris@109 387 }
Chris@109 388 HgAction toRun = m_queue.front();
Chris@109 389 m_queue.pop_front();
Chris@109 390 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
Chris@109 391 startCommand(toRun);
Chris@109 392 }
Chris@109 393
Chris@444 394 QStringList HgRunner::addExtensionOptions(QStringList params)
Chris@444 395 {
Chris@444 396 QString extpath = getExtensionLocation();
Chris@444 397 if (extpath == "") {
Chris@444 398 DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
Chris@444 399 return params;
Chris@444 400 }
Chris@444 401
Chris@444 402 if (m_authFilePath == "") {
Chris@444 403
Chris@444 404 QByteArray key = randomKey();
Chris@444 405 if (key == QByteArray()) {
Chris@444 406 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
Chris@444 407 return params;
Chris@444 408 }
Chris@444 409 QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
Chris@444 410
Chris@444 411 QByteArray fileExt = randomKey();
Chris@444 412 if (fileExt == QByteArray()) {
Chris@444 413 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
Chris@444 414 return params;
Chris@444 415 }
Chris@444 416 QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
Chris@444 417 .replace('+', '-').replace('/', '.');
Chris@444 418 QString path = QDesktopServices::storageLocation
Chris@444 419 (QDesktopServices::CacheLocation);
Chris@444 420 QDir().mkpath(path);
Chris@444 421 if (path == "") {
Chris@444 422 DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
Chris@444 423 return params;
Chris@444 424 }
Chris@444 425
Chris@444 426 m_authKey = key16;
Chris@444 427 m_authFilePath = QString("%1/easyhg_%2.dat").arg(path).arg(fileExt16);
Chris@444 428 }
Chris@444 429
Chris@444 430 params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
Chris@444 431 params.push_front("--config");
Chris@444 432
Chris@444 433 params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
Chris@444 434 params.push_front("--config");
Chris@444 435
Chris@444 436 // Looks like this one must be without quotes, even though the SSH
Chris@444 437 // one above only works on Windows if it has quotes (at least where
Chris@444 438 // there is a space in the path). Odd
Chris@444 439 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
Chris@444 440 params.push_front("--config");
Chris@444 441
Chris@444 442 return params;
Chris@444 443 }
Chris@444 444
Chris@109 445 void HgRunner::startCommand(HgAction action)
Chris@109 446 {
Chris@109 447 QString executable = action.executable;
Chris@109 448 bool interactive = false;
Chris@109 449 QStringList params = action.params;
Chris@109 450
Chris@340 451 if (action.workingDir.isEmpty()) {
Chris@340 452 // We require a working directory, never just operate in pwd
Chris@340 453 emit commandFailed(action, "EasyMercurial: No working directory supplied, will not run Mercurial command without one");
Chris@340 454 return;
Chris@340 455 }
Chris@340 456
Chris@257 457 QSettings settings;
Chris@257 458 settings.beginGroup("General");
Chris@257 459
Chris@109 460 if (executable == "") {
Chris@109 461 // This is a Hg command
Chris@239 462 executable = getHgBinaryName();
chris@406 463 if (executable == "") executable = "hg";
chris@406 464
chris@406 465 QString ssh = getSshBinaryName();
chris@406 466 if (ssh != "") {
chris@406 467 params.push_front(QString("ui.ssh=\"%1\"").arg(ssh));
chris@406 468 params.push_front("--config");
chris@406 469 }
Chris@161 470
Chris@161 471 if (action.mayBeInteractive()) {
Chris@161 472 params.push_front("ui.interactive=true");
Chris@161 473 params.push_front("--config");
Chris@176 474 if (settings.value("useextension", true).toBool()) {
Chris@444 475 params = addExtensionOptions(params);
Chris@176 476 }
Chris@161 477 interactive = true;
Chris@161 478 }
Chris@161 479
Chris@161 480 //!!! want an option to use the mercurial_keyring extension as well
Chris@107 481 }
jtkorhonen@0 482
Chris@84 483 m_isRunning = true;
jtkorhonen@0 484 setRange(0, 0);
Chris@115 485 if (!action.shouldBeFast()) show();
Chris@110 486 m_stdout.clear();
Chris@110 487 m_stderr.clear();
Chris@84 488 m_realm = "";
Chris@84 489 m_userName = "";
jtkorhonen@0 490
Chris@113 491 m_proc = new QProcess;
Chris@113 492
Chris@177 493 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
Chris@177 494
Chris@177 495 #ifdef Q_OS_WIN32
Chris@257 496 // On Win32 we like to bundle Hg and other executables with EasyHg
Chris@177 497 if (m_myDirPath != "") {
Chris@177 498 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
Chris@172 499 }
Chris@172 500 #endif
Chris@172 501
Chris@257 502 #ifdef Q_OS_MAC
Chris@321 503 if (settings.value("python32", false).toBool()) {
Chris@261 504 env.insert("VERSIONER_PYTHON_PREFER_32_BIT", "1");
Chris@257 505 }
Chris@257 506 #endif
Chris@257 507
Chris@113 508 env.insert("LANG", "en_US.utf8");
Chris@113 509 env.insert("LC_ALL", "en_US.utf8");
Chris@408 510 env.insert("HGENCODING", "utf8");
Chris@113 511 env.insert("HGPLAIN", "1");
Chris@113 512 m_proc->setProcessEnvironment(env);
Chris@113 513
Chris@113 514 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
Chris@189 515 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
Chris@189 516 this, SLOT(error(QProcess::ProcessError)));
Chris@113 517 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@113 518 this, SLOT(finished(int, QProcess::ExitStatus)));
Chris@113 519 connect(m_proc, SIGNAL(readyReadStandardOutput()),
Chris@113 520 this, SLOT(dataReadyStdout()));
Chris@113 521 connect(m_proc, SIGNAL(readyReadStandardError()),
Chris@113 522 this, SLOT(dataReadyStderr()));
Chris@113 523
Chris@340 524 m_proc->setWorkingDirectory(action.workingDir);
jtkorhonen@0 525
Chris@107 526 if (interactive) {
Chris@111 527 openTerminal();
Chris@111 528 if (m_ptySlaveFilename != "") {
Chris@113 529 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
Chris@107 530 m_proc->setStandardInputFile(m_ptySlaveFilename);
Chris@114 531 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
Chris@113 532 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
Chris@107 533 }
Chris@84 534 }
Chris@84 535
Chris@109 536 QString cmdline = executable;
Chris@57 537 foreach (QString param, params) cmdline += " " + param;
Chris@64 538 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
Chris@109 539 << action.workingDir << endl;
Chris@43 540
Chris@109 541 m_currentAction = action;
Chris@109 542
Chris@113 543 // fill these out with what we actually ran
Chris@113 544 m_currentAction.executable = executable;
Chris@113 545 m_currentAction.params = params;
Chris@113 546
Chris@109 547 DEBUG << "set current action to " << m_currentAction.action << endl;
Chris@109 548
Chris@238 549 emit commandStarting(action);
Chris@238 550
Chris@109 551 m_proc->start(executable, params);
Chris@84 552 }
Chris@84 553
Chris@84 554 void HgRunner::closeProcInput()
Chris@84 555 {
Chris@84 556 DEBUG << "closeProcInput" << endl;
Chris@84 557
chris@385 558 if (m_proc) m_proc->closeWriteChannel();
Chris@111 559 }
Chris@111 560
Chris@111 561 void HgRunner::openTerminal()
Chris@111 562 {
Chris@111 563 #ifndef Q_OS_WIN32
Chris@111 564 if (m_ptySlaveFilename != "") return; // already open
Chris@111 565 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
Chris@111 566 int master = posix_openpt(O_RDWR | O_NOCTTY);
Chris@111 567 if (master < 0) {
Chris@111 568 DEBUG << "openpt failed" << endl;
Chris@111 569 perror("openpt failed");
Chris@111 570 return;
Chris@111 571 }
Chris@113 572 struct termios t;
Chris@113 573 if (tcgetattr(master, &t)) {
Chris@113 574 DEBUG << "tcgetattr failed" << endl;
Chris@113 575 perror("tcgetattr failed");
Chris@113 576 }
Chris@113 577 cfmakeraw(&t);
Chris@113 578 if (tcsetattr(master, TCSANOW, &t)) {
Chris@113 579 DEBUG << "tcsetattr failed" << endl;
Chris@113 580 perror("tcsetattr failed");
Chris@113 581 }
Chris@111 582 if (grantpt(master)) {
Chris@111 583 perror("grantpt failed");
Chris@111 584 }
Chris@111 585 if (unlockpt(master)) {
Chris@111 586 perror("unlockpt failed");
Chris@111 587 }
Chris@111 588 char *slave = ptsname(master);
Chris@111 589 if (!slave) {
Chris@111 590 perror("ptsname failed");
Chris@111 591 ::close(master);
Chris@111 592 return;
Chris@111 593 }
Chris@111 594 m_ptyMasterFd = master;
Chris@113 595 m_ptyFile = new QFile();
Chris@113 596 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
Chris@113 597 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
Chris@113 598 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
Chris@113 599 }
Chris@111 600 m_ptySlaveFilename = slave;
Chris@111 601 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
Chris@111 602 << m_ptySlaveFilename << endl;
Chris@111 603 #endif
Chris@111 604 }
Chris@111 605
Chris@111 606 void HgRunner::closeTerminal()
Chris@111 607 {
Chris@84 608 #ifndef Q_OS_WIN32
Chris@84 609 if (m_ptySlaveFilename != "") {
Chris@113 610 delete m_ptyFile;
Chris@113 611 m_ptyFile = 0;
Chris@84 612 ::close(m_ptyMasterFd);
Chris@84 613 m_ptySlaveFilename = "";
Chris@84 614 }
Chris@84 615 #endif
jtkorhonen@0 616 }