| 313 | } |
| 314 | |
| 315 | void STTY::findExternalTTY(QStringList terminalCommandLine) |
| 316 | { |
| 317 | #ifndef Q_OS_WIN |
| 318 | Q_ASSERT(!terminalCommandLine.empty()); |
| 319 | const auto appName = terminalCommandLine.takeFirst(); |
| 320 | |
| 321 | if (QStandardPaths::findExecutable(appName).isEmpty()) { |
| 322 | m_lastError = i18n("%1 is incorrect terminal name", appName); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | QTemporaryFile file; |
| 327 | if (!file.open()) { |
| 328 | m_lastError = i18n("Can't create a temporary file"); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | std::unique_ptr<QProcess, LaterDeleter> externalTerminal(new QProcess); |
| 333 | |
| 334 | terminalCommandLine << QStringLiteral("sh") << QStringLiteral("-c") |
| 335 | << QLatin1String("tty>") + file.fileName() |
| 336 | + QLatin1String(";exec<&-;exec>&-;while :;do sleep 3600;done"); |
| 337 | |
| 338 | externalTerminal->start(appName, terminalCommandLine); |
| 339 | |
| 340 | if (!externalTerminal->waitForStarted(500)) { |
| 341 | m_lastError = QLatin1String("Can't run terminal: ") + appName; |
| 342 | externalTerminal->terminate(); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | for (int i = 0; i < 800; i++) { |
| 347 | if (!file.bytesAvailable()) { |
| 348 | if (externalTerminal->state() == QProcess::NotRunning && externalTerminal->exitCode()) { |
| 349 | break; |
| 350 | } |
| 351 | QCoreApplication::processEvents(QEventLoop::AllEvents, 100); |
| 352 | usleep(8000); |
| 353 | } else { |
| 354 | qCDebug(DEBUGGERCOMMON) << "Received terminal output(tty)"; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | usleep(1000); |
| 360 | ttySlave = QString::fromUtf8(file.readAll().trimmed()); |
| 361 | |
| 362 | file.close(); |
| 363 | |
| 364 | if (ttySlave.isEmpty()) { |
| 365 | m_lastError = i18n( |
| 366 | "Can't receive %1 tty/pty. Check that %1 is actually a terminal and that it accepts these arguments: %2", |
| 367 | appName, KShell::joinArgs(terminalCommandLine)); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | if (externalTerminal->state() == QProcess::NotRunning) { |
| 372 | // This branch may be taken in case of inadequate external terminal command configuration. |