From 7373155c0a0826ffc3ec7c161dd2bd2f31dc8b6b Mon Sep 17 00:00:00 2001
From: Christian Kandeler <christian.kandeler@nokia.com>
Date: Fri, 1 Apr 2011 10:08:09 +0200
Subject: [PATCH] SSH: Implement pseudo terminal support.

Untested yet, because SSH tests are currently broken.
(They are fixed in 2.2 and will be merged back soon.)
---
 src/libs/utils/ssh/sshoutgoingpacket.cpp | 17 +++++++++++++++++
 src/libs/utils/ssh/sshoutgoingpacket_p.h |  4 ++++
 src/libs/utils/ssh/sshremoteprocess.cpp  | 20 +++++++++++++++++---
 src/libs/utils/ssh/sshremoteprocess.h    |  2 ++
 src/libs/utils/ssh/sshremoteprocess_p.h  |  4 ++++
 src/libs/utils/utils-lib.pri             |  1 +
 6 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/libs/utils/ssh/sshoutgoingpacket.cpp b/src/libs/utils/ssh/sshoutgoingpacket.cpp
index 52fc32d9b4b..7747c4332aa 100644
--- a/src/libs/utils/ssh/sshoutgoingpacket.cpp
+++ b/src/libs/utils/ssh/sshoutgoingpacket.cpp
@@ -154,6 +154,23 @@ void SshOutgoingPacket::generateEnvPacket(quint32 remoteChannel,
         .appendBool(false).appendString(var).appendString(value).finalize();
 }
 
+void SshOutgoingPacket::generatePtyRequestPacket(quint32 remoteChannel,
+    const SshPseudoTerminal &terminal)
+{
+    init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel)
+        .appendString("pty-req").appendBool(false)
+        .appendString(terminal.termType).appendInt(terminal.columnCount)
+        .appendInt(terminal.rowCount);
+    QByteArray modeString;
+    for (SshPseudoTerminal::ModeMap::ConstIterator it = terminal.modes.constBegin();
+         it != terminal.modes.constEnd(); ++it) {
+        modeString += encodeInt(static_cast<quint8>(it.key()));
+        modeString += encodeInt(it.value());
+    }
+    modeString += encodeInt(static_cast<quint8>(0)); // TTY_OP_END
+    appendString(modeString).finalize();
+}
+
 void SshOutgoingPacket::generateExecPacket(quint32 remoteChannel,
     const QByteArray &command)
 {
diff --git a/src/libs/utils/ssh/sshoutgoingpacket_p.h b/src/libs/utils/ssh/sshoutgoingpacket_p.h
index 22adf6d2e7c..e27c89e5185 100644
--- a/src/libs/utils/ssh/sshoutgoingpacket_p.h
+++ b/src/libs/utils/ssh/sshoutgoingpacket_p.h
@@ -36,6 +36,8 @@
 
 #include "sshpacket_p.h"
 
+#include "sshpseudoterminal.h"
+
 namespace Utils {
 namespace Internal {
 
@@ -65,6 +67,8 @@ public:
         quint32 maxPacketSize);
     void generateEnvPacket(quint32 remoteChannel, const QByteArray &var,
         const QByteArray &value);
+    void generatePtyRequestPacket(quint32 remoteChannel,
+        const SshPseudoTerminal &terminal);
     void generateExecPacket(quint32 remoteChannel, const QByteArray &command);
     void generateSftpPacket(quint32 remoteChannel);
     void generateWindowAdjustPacket(quint32 remoteChannel, quint32 bytesToAdd);
diff --git a/src/libs/utils/ssh/sshremoteprocess.cpp b/src/libs/utils/ssh/sshremoteprocess.cpp
index 8f0620b2b16..91626dd3476 100644
--- a/src/libs/utils/ssh/sshremoteprocess.cpp
+++ b/src/libs/utils/ssh/sshremoteprocess.cpp
@@ -39,6 +39,8 @@
 
 #include <botan/exceptn.h>
 
+#include <utils/qtcassert.h>
+
 #include <QtCore/QTimer>
 
 /*!
@@ -57,8 +59,8 @@
 
     Therefore, the only sensible use case for calling closeChannel() is to
     get rid of an SshRemoteProces object before the process is actually started.
-    Note that the process does not have a terminal, so you can't use it
-    for applications that require one.
+    If the process needs a pseudo terminal, you can request one
+    via requestTerminal() before calling start().
  */
 
 namespace Utils {
@@ -105,6 +107,13 @@ void SshRemoteProcess::addToEnvironment(const QByteArray &var, const QByteArray
         d->m_env << qMakePair(var, value); // Cached locally and sent on start()
 }
 
+void SshRemoteProcess::requestTerminal(const SshPseudoTerminal &terminal)
+{
+    QTC_ASSERT(d->channelState() == Internal::SshRemoteProcessPrivate::Inactive, return);
+    d->m_useTerminal = true;
+    d->m_terminal = terminal;
+}
+
 void SshRemoteProcess::start()
 {
     if (d->channelState() == Internal::SshRemoteProcessPrivate::Inactive) {
@@ -154,7 +163,8 @@ namespace Internal {
 SshRemoteProcessPrivate::SshRemoteProcessPrivate(const QByteArray &command,
     quint32 channelId, SshSendFacility &sendFacility, SshRemoteProcess *proc)
     : AbstractSshChannel(channelId, sendFacility), m_procState(NotYetStarted),
-      m_wasRunning(false), m_exitCode(0), m_command(command), m_proc(proc)
+      m_wasRunning(false), m_exitCode(0), m_command(command),
+      m_useTerminal(false), m_proc(proc)
 {
 }
 
@@ -189,6 +199,10 @@ void SshRemoteProcessPrivate::handleOpenSuccessInternal()
            envVar.second);
    }
 
+   if (m_useTerminal) {
+       // TODO: Encode m_terminal
+   }
+
    m_sendFacility.sendExecPacket(remoteChannel(), m_command);
    setProcState(ExecRequested);
    m_timeoutTimer->start(ReplyTimeout);
diff --git a/src/libs/utils/ssh/sshremoteprocess.h b/src/libs/utils/ssh/sshremoteprocess.h
index df81336b07e..31ece89e63d 100644
--- a/src/libs/utils/ssh/sshremoteprocess.h
+++ b/src/libs/utils/ssh/sshremoteprocess.h
@@ -44,6 +44,7 @@ class QByteArray;
 QT_END_NAMESPACE
 
 namespace Utils {
+class SshPseudoTerminal;
 namespace Internal {
 class SshChannelManager;
 class SshRemoteProcessPrivate;
@@ -84,6 +85,7 @@ public:
      */
     void addToEnvironment(const QByteArray &var, const QByteArray &value);
 
+    void requestTerminal(const SshPseudoTerminal &terminal);
     void start();
     void closeChannel();
 
diff --git a/src/libs/utils/ssh/sshremoteprocess_p.h b/src/libs/utils/ssh/sshremoteprocess_p.h
index d667bde2f04..0c87e684119 100644
--- a/src/libs/utils/ssh/sshremoteprocess_p.h
+++ b/src/libs/utils/ssh/sshremoteprocess_p.h
@@ -34,6 +34,8 @@
 #ifndef SSHREMOTEPROCESS_P_H
 #define SSHREMOTEPROCESS_P_H
 
+#include "sshpseudoterminal.h"
+
 #include "sshchannel_p.h"
 
 #include <QtCore/QList>
@@ -88,6 +90,8 @@ private:
 
     typedef QPair<QByteArray, QByteArray> EnvVar;
     QList<EnvVar> m_env;
+    bool m_useTerminal;
+    SshPseudoTerminal m_terminal;
 
     SshRemoteProcess *m_proc;
 };
diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri
index 420e299e39b..4d4b3d3f956 100644
--- a/src/libs/utils/utils-lib.pri
+++ b/src/libs/utils/utils-lib.pri
@@ -172,6 +172,7 @@ HEADERS += $$PWD/environment.h \
     $$PWD/ssh/sftpchannel_p.h \
     $$PWD/ssh/sshremoteprocessrunner.h \
     $$PWD/ssh/sshconnectionmanager.h \
+    $$PWD/ssh/sshpseudoterminal.h \
     $$PWD/statuslabel.h
 
 FORMS += $$PWD/filewizardpage.ui \
-- 
GitLab