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@172
|
86 }
|
Chris@188
|
87
|
Chris@180
|
88 settings.setValue("extensionpath", extpath);
|
Chris@180
|
89 return extpath;
|
Chris@172
|
90 }
|
Chris@172
|
91
|
Chris@188
|
92 QString HgRunner::getUnbundledFileName()
|
Chris@188
|
93 {
|
Chris@188
|
94 QString home = QDir::homePath();
|
Chris@188
|
95 QString target = QString("%1/.easyhg").arg(home);
|
Chris@188
|
96 QString extpath = QString("%1/easyhg.py").arg(target);
|
Chris@188
|
97 return extpath;
|
Chris@188
|
98 }
|
Chris@188
|
99
|
Chris@180
|
100 QString HgRunner::unbundleExtension()
|
Chris@161
|
101 {
|
Chris@188
|
102 // Pull out the bundled Python file into a temporary file, and
|
Chris@188
|
103 // copy it to our known extension location, replacing the magic
|
Chris@188
|
104 // text NO_EASYHG_IMPORT_PATH with our installation location
|
Chris@188
|
105
|
Chris@161
|
106 QString bundled = ":easyhg.py";
|
Chris@188
|
107 QString unbundled = getUnbundledFileName();
|
Chris@188
|
108
|
Chris@188
|
109 QString target = QFileInfo(unbundled).path();
|
Chris@161
|
110 if (!QDir().mkpath(target)) {
|
Chris@161
|
111 DEBUG << "Failed to make unbundle path " << target << endl;
|
Chris@188
|
112 std::cerr << "Failed to make unbundle path " << target << std::endl;
|
Chris@180
|
113 return "";
|
Chris@161
|
114 }
|
Chris@188
|
115
|
Chris@161
|
116 QFile bf(bundled);
|
Chris@188
|
117 DEBUG << "unbundle: bundled file will be " << bundled << endl;
|
Chris@188
|
118 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
|
Chris@180
|
119 DEBUG << "Bundled extension is missing!" << endl;
|
Chris@180
|
120 return "";
|
Chris@180
|
121 }
|
Chris@188
|
122
|
Chris@188
|
123 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
|
Chris@188
|
124 tmpfile.setAutoRemove(false);
|
Chris@188
|
125 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
|
Chris@188
|
126 if (!tmpfile.open()) {
|
Chris@188
|
127 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
|
Chris@188
|
128 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
|
Chris@180
|
129 return "";
|
Chris@161
|
130 }
|
Chris@188
|
131
|
Chris@188
|
132 QString all = QString::fromUtf8(bf.readAll());
|
Chris@188
|
133 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
|
Chris@188
|
134 tmpfile.write(all.toUtf8());
|
Chris@188
|
135 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
|
Chris@188
|
136
|
Chris@188
|
137 tmpfile.close();
|
Chris@188
|
138
|
Chris@188
|
139 QFile ef(unbundled);
|
Chris@188
|
140 if (ef.exists()) {
|
Chris@188
|
141 DEBUG << "unbundle: removing old file " << unbundled << endl;
|
Chris@188
|
142 ef.remove();
|
Chris@188
|
143 }
|
Chris@188
|
144 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
|
Chris@188
|
145 if (!tmpfile.rename(unbundled)) {
|
Chris@188
|
146 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
|
Chris@188
|
147 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
|
Chris@188
|
148 return "";
|
Chris@188
|
149 }
|
Chris@188
|
150
|
Chris@188
|
151 DEBUG << "Unbundled extension to " << unbundled << endl;
|
Chris@188
|
152 return unbundled;
|
Chris@161
|
153 }
|
Chris@161
|
154
|
Chris@109
|
155 void HgRunner::requestAction(HgAction action)
|
Chris@109
|
156 {
|
Chris@109
|
157 DEBUG << "requestAction " << action.action << endl;
|
Chris@109
|
158 bool pushIt = true;
|
Chris@109
|
159 if (m_queue.empty()) {
|
Chris@109
|
160 if (action == m_currentAction) {
|
Chris@109
|
161 // this request is identical to the thing we're executing
|
Chris@109
|
162 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
|
Chris@109
|
163 pushIt = false;
|
Chris@109
|
164 }
|
Chris@109
|
165 } else {
|
Chris@109
|
166 HgAction last = m_queue.back();
|
Chris@109
|
167 if (action == last) {
|
Chris@109
|
168 // this request is identical to the previous thing we
|
Chris@109
|
169 // queued which we haven't executed yet
|
Chris@109
|
170 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
|
Chris@109
|
171 pushIt = false;
|
Chris@109
|
172 }
|
Chris@109
|
173 }
|
Chris@109
|
174 if (pushIt) m_queue.push_back(action);
|
Chris@109
|
175 checkQueue();
|
Chris@109
|
176 }
|
Chris@109
|
177
|
Chris@180
|
178 QString HgRunner::findHgBinaryName()
|
Chris@62
|
179 {
|
Chris@62
|
180 QSettings settings;
|
Chris@175
|
181 settings.beginGroup("Locations");
|
Chris@77
|
182 QString hg = settings.value("hgbinary", "").toString();
|
Chris@77
|
183 if (hg == "") {
|
Chris@172
|
184 hg = findInPath("hg", m_myDirPath, true);
|
Chris@77
|
185 }
|
Chris@213
|
186 if (hg != "") {
|
Chris@77
|
187 settings.setValue("hgbinary", hg);
|
Chris@77
|
188 }
|
Chris@62
|
189 return hg;
|
Chris@62
|
190 }
|
Chris@62
|
191
|
jtkorhonen@0
|
192 void HgRunner::started()
|
jtkorhonen@0
|
193 {
|
Chris@104
|
194 DEBUG << "started" << endl;
|
Chris@75
|
195 /*
|
Chris@104
|
196 m_proc->write("blah\n");
|
Chris@104
|
197 m_proc->write("blah\n");
|
Chris@104
|
198 m_proc -> closeWriteChannel();
|
Chris@75
|
199 */
|
jtkorhonen@0
|
200 }
|
jtkorhonen@0
|
201
|
Chris@84
|
202 void HgRunner::noteUsername(QString name)
|
Chris@75
|
203 {
|
Chris@84
|
204 m_userName = name;
|
Chris@75
|
205 }
|
Chris@75
|
206
|
Chris@84
|
207 void HgRunner::noteRealm(QString realm)
|
Chris@75
|
208 {
|
Chris@84
|
209 m_realm = realm;
|
Chris@84
|
210 }
|
Chris@84
|
211
|
Chris@84
|
212 void HgRunner::getUsername()
|
Chris@84
|
213 {
|
Chris@113
|
214 if (m_ptyFile) {
|
Chris@84
|
215 bool ok = false;
|
Chris@84
|
216 QString prompt = tr("User name:");
|
Chris@84
|
217 if (m_realm != "") {
|
Chris@84
|
218 prompt = tr("User name for \"%1\":").arg(m_realm);
|
Chris@84
|
219 }
|
Chris@84
|
220 QString pwd = QInputDialog::getText
|
Chris@84
|
221 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
222 tr("Enter user name"), prompt,
|
Chris@84
|
223 QLineEdit::Normal, QString(), &ok);
|
Chris@84
|
224 if (ok) {
|
Chris@113
|
225 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
226 m_ptyFile->flush();
|
Chris@84
|
227 return;
|
Chris@111
|
228 } else {
|
Chris@111
|
229 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
|
Chris@111
|
230 killCurrentCommand();
|
Chris@111
|
231 return;
|
Chris@84
|
232 }
|
Chris@84
|
233 }
|
Chris@84
|
234 // user cancelled or something went wrong
|
Chris@111
|
235 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
|
Chris@84
|
236 killCurrentCommand();
|
Chris@84
|
237 }
|
Chris@84
|
238
|
Chris@84
|
239 void HgRunner::getPassword()
|
Chris@84
|
240 {
|
Chris@113
|
241 if (m_ptyFile) {
|
Chris@84
|
242 bool ok = false;
|
Chris@84
|
243 QString prompt = tr("Password:");
|
Chris@84
|
244 if (m_userName != "") {
|
Chris@84
|
245 if (m_realm != "") {
|
Chris@84
|
246 prompt = tr("Password for \"%1\" at \"%2\":")
|
Chris@84
|
247 .arg(m_userName).arg(m_realm);
|
Chris@75
|
248 } else {
|
Chris@84
|
249 prompt = tr("Password for user \"%1\":")
|
Chris@84
|
250 .arg(m_userName);
|
Chris@75
|
251 }
|
Chris@75
|
252 }
|
Chris@84
|
253 QString pwd = QInputDialog::getText
|
Chris@84
|
254 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
255 tr("Enter password"), prompt,
|
Chris@84
|
256 QLineEdit::Password, QString(), &ok);
|
Chris@84
|
257 if (ok) {
|
Chris@113
|
258 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
259 m_ptyFile->flush();
|
Chris@84
|
260 return;
|
Chris@111
|
261 } else {
|
Chris@111
|
262 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
|
Chris@111
|
263 killCurrentCommand();
|
Chris@111
|
264 return;
|
Chris@84
|
265 }
|
Chris@75
|
266 }
|
Chris@84
|
267 // user cancelled or something went wrong
|
Chris@111
|
268 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
|
Chris@84
|
269 killCurrentCommand();
|
Chris@84
|
270 }
|
Chris@84
|
271
|
Chris@113
|
272 bool HgRunner::checkPrompts(QString chunk)
|
Chris@84
|
273 {
|
Chris@93
|
274 //DEBUG << "checkPrompts: " << chunk << endl;
|
Chris@84
|
275
|
Chris@128
|
276 if (!m_currentAction.mayBeInteractive()) return false;
|
Chris@128
|
277
|
Chris@84
|
278 QString text = chunk.trimmed();
|
Chris@84
|
279 QString lower = text.toLower();
|
Chris@84
|
280 if (lower.endsWith("password:")) {
|
Chris@84
|
281 getPassword();
|
Chris@113
|
282 return true;
|
Chris@84
|
283 }
|
Chris@128
|
284 if (lower.endsWith("user:") || lower.endsWith("username:")) {
|
Chris@84
|
285 getUsername();
|
Chris@113
|
286 return true;
|
Chris@84
|
287 }
|
Chris@128
|
288 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
|
Chris@84
|
289 if (userRe.indexIn(text) >= 0) {
|
Chris@128
|
290 noteUsername(userRe.cap(2));
|
Chris@84
|
291 }
|
Chris@84
|
292 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
|
Chris@84
|
293 if (realmRe.indexIn(text) >= 0) {
|
Chris@84
|
294 noteRealm(realmRe.cap(1));
|
Chris@84
|
295 }
|
Chris@113
|
296 return false;
|
Chris@84
|
297 }
|
Chris@84
|
298
|
Chris@110
|
299 void HgRunner::dataReadyStdout()
|
Chris@84
|
300 {
|
Chris@110
|
301 DEBUG << "dataReadyStdout" << endl;
|
Chris@110
|
302 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
|
Chris@113
|
303 if (!checkPrompts(chunk)) {
|
Chris@113
|
304 m_stdout += chunk;
|
Chris@113
|
305 }
|
Chris@110
|
306 }
|
Chris@110
|
307
|
Chris@110
|
308 void HgRunner::dataReadyStderr()
|
Chris@110
|
309 {
|
Chris@110
|
310 DEBUG << "dataReadyStderr" << endl;
|
Chris@110
|
311 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
|
Chris@113
|
312 DEBUG << chunk;
|
Chris@113
|
313 if (!checkPrompts(chunk)) {
|
Chris@113
|
314 m_stderr += chunk;
|
Chris@113
|
315 }
|
Chris@113
|
316 }
|
Chris@113
|
317
|
Chris@113
|
318 void HgRunner::dataReadyPty()
|
Chris@113
|
319 {
|
Chris@113
|
320 DEBUG << "dataReadyPty" << endl;
|
Chris@113
|
321 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
|
Chris@113
|
322 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
|
Chris@113
|
323 if (!checkPrompts(chunk)) {
|
Chris@113
|
324 m_stdout += chunk;
|
Chris@113
|
325 }
|
Chris@75
|
326 }
|
Chris@75
|
327
|
Chris@189
|
328 void HgRunner::error(QProcess::ProcessError)
|
Chris@189
|
329 {
|
Chris@189
|
330 finished(-1, QProcess::CrashExit);
|
Chris@189
|
331 }
|
Chris@189
|
332
|
jtkorhonen@0
|
333 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
334 {
|
Chris@109
|
335 // Save the current action and reset m_currentAction before we
|
Chris@109
|
336 // emit a signal to mark the completion; otherwise we may be
|
Chris@109
|
337 // resetting the action after a slot has already tried to set it
|
Chris@109
|
338 // to something else to start a new action
|
Chris@109
|
339
|
Chris@109
|
340 HgAction completedAction = m_currentAction;
|
Chris@109
|
341
|
Chris@84
|
342 m_isRunning = false;
|
Chris@109
|
343 m_currentAction = HgAction();
|
Chris@84
|
344
|
Chris@195
|
345 //closeProcInput();
|
Chris@189
|
346 m_proc->deleteLater();
|
Chris@113
|
347 m_proc = 0;
|
jtkorhonen@0
|
348
|
Chris@109
|
349 if (completedAction.action == ACT_NONE) {
|
Chris@109
|
350 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
|
Chris@62
|
351 } else {
|
Chris@113
|
352 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@113
|
353 DEBUG << "HgRunner::finished: Command completed successfully"
|
Chris@113
|
354 << endl;
|
Chris@124
|
355 // DEBUG << "stdout is " << m_stdout << endl;
|
Chris@113
|
356 emit commandCompleted(completedAction, m_stdout);
|
Chris@113
|
357 } else {
|
Chris@113
|
358 DEBUG << "HgRunner::finished: Command failed, exit code "
|
Chris@113
|
359 << procExitCode << ", exit status " << procExitStatus
|
Chris@113
|
360 << ", stderr follows" << endl;
|
Chris@113
|
361 DEBUG << m_stderr << endl;
|
Chris@113
|
362 emit commandFailed(completedAction, m_stderr);
|
Chris@113
|
363 }
|
jtkorhonen@0
|
364 }
|
Chris@109
|
365
|
Chris@109
|
366 checkQueue();
|
jtkorhonen@0
|
367 }
|
jtkorhonen@0
|
368
|
Chris@182
|
369 void HgRunner::killCurrentActions()
|
Chris@182
|
370 {
|
Chris@182
|
371 m_queue.clear();
|
Chris@182
|
372 killCurrentCommand();
|
Chris@182
|
373 }
|
Chris@182
|
374
|
Chris@62
|
375 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
376 {
|
Chris@109
|
377 if (m_isRunning) {
|
Chris@113
|
378 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
|
Chris@113
|
379 m_proc->kill();
|
jtkorhonen@0
|
380 }
|
jtkorhonen@0
|
381 }
|
jtkorhonen@0
|
382
|
Chris@109
|
383 void HgRunner::checkQueue()
|
Chris@62
|
384 {
|
Chris@109
|
385 if (m_isRunning) {
|
Chris@109
|
386 return;
|
Chris@109
|
387 }
|
Chris@109
|
388 if (m_queue.empty()) {
|
Chris@109
|
389 hide();
|
Chris@109
|
390 return;
|
Chris@109
|
391 }
|
Chris@109
|
392 HgAction toRun = m_queue.front();
|
Chris@109
|
393 m_queue.pop_front();
|
Chris@109
|
394 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
|
Chris@109
|
395 startCommand(toRun);
|
Chris@109
|
396 }
|
Chris@109
|
397
|
Chris@109
|
398 void HgRunner::startCommand(HgAction action)
|
Chris@109
|
399 {
|
Chris@109
|
400 QString executable = action.executable;
|
Chris@109
|
401 bool interactive = false;
|
Chris@109
|
402 QStringList params = action.params;
|
Chris@109
|
403
|
Chris@109
|
404 if (executable == "") {
|
Chris@109
|
405 // This is a Hg command
|
Chris@180
|
406 executable = findHgBinaryName();
|
Chris@161
|
407
|
Chris@161
|
408 if (action.mayBeInteractive()) {
|
Chris@161
|
409 params.push_front("ui.interactive=true");
|
Chris@161
|
410 params.push_front("--config");
|
Chris@176
|
411
|
Chris@176
|
412 QSettings settings;
|
Chris@176
|
413 settings.beginGroup("General");
|
Chris@176
|
414 if (settings.value("useextension", true).toBool()) {
|
Chris@180
|
415 QString extpath = findExtension();
|
Chris@180
|
416 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
|
Chris@176
|
417 params.push_front("--config");
|
Chris@176
|
418 }
|
Chris@161
|
419 interactive = true;
|
Chris@161
|
420 }
|
Chris@161
|
421
|
Chris@161
|
422 //!!! want an option to use the mercurial_keyring extension as well
|
Chris@107
|
423 }
|
jtkorhonen@0
|
424
|
Chris@84
|
425 m_isRunning = true;
|
jtkorhonen@0
|
426 setRange(0, 0);
|
Chris@115
|
427 if (!action.shouldBeFast()) show();
|
Chris@110
|
428 m_stdout.clear();
|
Chris@110
|
429 m_stderr.clear();
|
Chris@84
|
430 m_realm = "";
|
Chris@84
|
431 m_userName = "";
|
jtkorhonen@0
|
432
|
Chris@113
|
433 m_proc = new QProcess;
|
Chris@113
|
434
|
Chris@177
|
435 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
Chris@177
|
436
|
Chris@177
|
437 #ifdef Q_OS_WIN32
|
Chris@177
|
438 if (m_myDirPath != "") {
|
Chris@177
|
439 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
|
Chris@172
|
440 }
|
Chris@172
|
441 #endif
|
Chris@172
|
442
|
Chris@113
|
443 env.insert("LANG", "en_US.utf8");
|
Chris@113
|
444 env.insert("LC_ALL", "en_US.utf8");
|
Chris@113
|
445 env.insert("HGPLAIN", "1");
|
Chris@113
|
446 m_proc->setProcessEnvironment(env);
|
Chris@113
|
447
|
Chris@113
|
448 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@189
|
449 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
|
Chris@189
|
450 this, SLOT(error(QProcess::ProcessError)));
|
Chris@113
|
451 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@113
|
452 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@113
|
453 connect(m_proc, SIGNAL(readyReadStandardOutput()),
|
Chris@113
|
454 this, SLOT(dataReadyStdout()));
|
Chris@113
|
455 connect(m_proc, SIGNAL(readyReadStandardError()),
|
Chris@113
|
456 this, SLOT(dataReadyStderr()));
|
Chris@113
|
457
|
Chris@109
|
458 if (!action.workingDir.isEmpty()) {
|
Chris@109
|
459 m_proc->setWorkingDirectory(action.workingDir);
|
jtkorhonen@0
|
460 }
|
jtkorhonen@0
|
461
|
Chris@107
|
462 if (interactive) {
|
Chris@111
|
463 openTerminal();
|
Chris@111
|
464 if (m_ptySlaveFilename != "") {
|
Chris@113
|
465 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
|
Chris@107
|
466 m_proc->setStandardInputFile(m_ptySlaveFilename);
|
Chris@114
|
467 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
|
Chris@113
|
468 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
|
Chris@107
|
469 }
|
Chris@84
|
470 }
|
Chris@84
|
471
|
Chris@109
|
472 QString cmdline = executable;
|
Chris@57
|
473 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
474 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@109
|
475 << action.workingDir << endl;
|
Chris@43
|
476
|
Chris@109
|
477 m_currentAction = action;
|
Chris@109
|
478
|
Chris@113
|
479 // fill these out with what we actually ran
|
Chris@113
|
480 m_currentAction.executable = executable;
|
Chris@113
|
481 m_currentAction.params = params;
|
Chris@113
|
482
|
Chris@109
|
483 DEBUG << "set current action to " << m_currentAction.action << endl;
|
Chris@109
|
484
|
Chris@109
|
485 m_proc->start(executable, params);
|
Chris@84
|
486 }
|
Chris@84
|
487
|
Chris@84
|
488 void HgRunner::closeProcInput()
|
Chris@84
|
489 {
|
Chris@84
|
490 DEBUG << "closeProcInput" << endl;
|
Chris@84
|
491
|
Chris@84
|
492 m_proc->closeWriteChannel();
|
Chris@111
|
493 }
|
Chris@111
|
494
|
Chris@111
|
495 void HgRunner::openTerminal()
|
Chris@111
|
496 {
|
Chris@111
|
497 #ifndef Q_OS_WIN32
|
Chris@111
|
498 if (m_ptySlaveFilename != "") return; // already open
|
Chris@111
|
499 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
|
Chris@111
|
500 int master = posix_openpt(O_RDWR | O_NOCTTY);
|
Chris@111
|
501 if (master < 0) {
|
Chris@111
|
502 DEBUG << "openpt failed" << endl;
|
Chris@111
|
503 perror("openpt failed");
|
Chris@111
|
504 return;
|
Chris@111
|
505 }
|
Chris@113
|
506 struct termios t;
|
Chris@113
|
507 if (tcgetattr(master, &t)) {
|
Chris@113
|
508 DEBUG << "tcgetattr failed" << endl;
|
Chris@113
|
509 perror("tcgetattr failed");
|
Chris@113
|
510 }
|
Chris@113
|
511 cfmakeraw(&t);
|
Chris@113
|
512 if (tcsetattr(master, TCSANOW, &t)) {
|
Chris@113
|
513 DEBUG << "tcsetattr failed" << endl;
|
Chris@113
|
514 perror("tcsetattr failed");
|
Chris@113
|
515 }
|
Chris@111
|
516 if (grantpt(master)) {
|
Chris@111
|
517 perror("grantpt failed");
|
Chris@111
|
518 }
|
Chris@111
|
519 if (unlockpt(master)) {
|
Chris@111
|
520 perror("unlockpt failed");
|
Chris@111
|
521 }
|
Chris@111
|
522 char *slave = ptsname(master);
|
Chris@111
|
523 if (!slave) {
|
Chris@111
|
524 perror("ptsname failed");
|
Chris@111
|
525 ::close(master);
|
Chris@111
|
526 return;
|
Chris@111
|
527 }
|
Chris@111
|
528 m_ptyMasterFd = master;
|
Chris@113
|
529 m_ptyFile = new QFile();
|
Chris@113
|
530 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
|
Chris@113
|
531 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
|
Chris@113
|
532 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
|
Chris@113
|
533 }
|
Chris@111
|
534 m_ptySlaveFilename = slave;
|
Chris@111
|
535 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
|
Chris@111
|
536 << m_ptySlaveFilename << endl;
|
Chris@111
|
537 #endif
|
Chris@111
|
538 }
|
Chris@111
|
539
|
Chris@111
|
540 void HgRunner::closeTerminal()
|
Chris@111
|
541 {
|
Chris@84
|
542 #ifndef Q_OS_WIN32
|
Chris@84
|
543 if (m_ptySlaveFilename != "") {
|
Chris@113
|
544 delete m_ptyFile;
|
Chris@113
|
545 m_ptyFile = 0;
|
Chris@84
|
546 ::close(m_ptyMasterFd);
|
Chris@84
|
547 m_ptySlaveFilename = "";
|
Chris@84
|
548 }
|
Chris@84
|
549 #endif
|
jtkorhonen@0
|
550 }
|