From e077d2609cb454a52d736d87518a7d3432a307e8 Mon Sep 17 00:00:00 2001
From: ck <qt-info@nokia.com>
Date: Fri, 9 Jul 2010 08:53:35 +0200
Subject: [PATCH] Maemo: Fix end-of-process detection.

The remote terminal likes to insert backspaces just for fun.
---
 .../qt-maemo/maemosshthread.cpp               | 24 +++++++++++++++++--
 .../qt-maemo/maemosshthread.h                 |  1 +
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
index caef4b19e3e..d0b022f37f3 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
@@ -123,7 +123,8 @@ void MaemoSshRunner::initState()
 
 void MaemoSshRunner::handleRemoteOutput(const QByteArray &curOutput)
 {
-    const QByteArray output = m_potentialEndMarkerPrefix + curOutput;
+    const QByteArray output
+        = filterTerminalControlChars(m_potentialEndMarkerPrefix + curOutput);
 
     // Wait for a prompt before sending the command.
     if (!m_promptEncountered) {
@@ -184,7 +185,26 @@ void MaemoSshRunner::handleRemoteOutput(const QByteArray &curOutput)
     if (m_endMarkerCount == 2)
         stop();
 
-    m_potentialEndMarkerPrefix = output.right(EndMarker.count() - 1);
+    m_potentialEndMarkerPrefix = output.right(EndMarker.count());
+}
+
+QByteArray MaemoSshRunner::filterTerminalControlChars(const QByteArray &data)
+{
+    QByteArray filteredData;
+    for (int i = 0; i < data.size(); ++i) {
+        if (data.at(i) == '\b') {
+            if (filteredData.isEmpty()) {
+                qWarning("Failed to filter terminal control characters, "
+                    "remote output may not appear.");
+            } else {
+                filteredData.remove(filteredData.count() - 1, 1);
+            }
+        } else {
+            filteredData.append(data.at(i));
+        }
+    }
+
+    return filteredData;
 }
 
 const QByteArray MaemoSshRunner::EndMarker(QString(QChar(0x13a0)).toUtf8());
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
index 788bc058211..85fc79208dc 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
@@ -98,6 +98,7 @@ private:
     virtual bool runInternal();
     Q_SLOT void handleRemoteOutput(const QByteArray &output);
     void initState();
+    QByteArray filterTerminalControlChars(const QByteArray &data);
 
     static const QByteArray EndMarker;
 
-- 
GitLab