From 575b10a27368078480454121383e815de7a2a6d5 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 20 Jun 2023 05:30:06 +0300 Subject: [PATCH] kpty: format and indent Signed-off-by: Ivailo Monev --- kpty/kpty.cpp | 13 ++--- kpty/kptydevice.cpp | 119 ++++++++++++++++++++----------------------- kpty/kptyprocess.cpp | 55 ++++++++++---------- kpty/kptyprocess.h | 7 ++- 4 files changed, 91 insertions(+), 103 deletions(-) diff --git a/kpty/kpty.cpp b/kpty/kpty.cpp index 8b8d2f05..63079f8f 100644 --- a/kpty/kpty.cpp +++ b/kpty/kpty.cpp @@ -125,12 +125,10 @@ KPty::~KPty() bool KPty::open() { - Q_D(KPty); - + Q_D(KPty); if (d->masterFd >= 0) { return true; } - d->ownMaster = true; // Find a master pty that we can open //////////////////////////////// @@ -222,30 +220,27 @@ bool KPty::open(int fd) kWarning(175) << "Attempting to open an already open pty"; return false; } - d->ownMaster = false; -# ifdef HAVE_PTSNAME_R +#ifdef HAVE_PTSNAME_R char ptsn[32]; ::memset(ptsn, '\0', sizeof(ptsn) * sizeof(char)); if (ptsname_r(fd, ptsn, sizeof(ptsn)) == 0) { d->ttyName = ptsn; -# else +#else char *ptsn = ptsname(fd); if (ptsn) { d->ttyName = ptsn; -# endif +#endif } else { kWarning(175) << "Failed to determine pty slave device for fd" << fd; return false; } - d->masterFd = fd; if (!openSlave()) { d->masterFd = -1; return false; } - return true; } diff --git a/kpty/kptydevice.cpp b/kpty/kptydevice.cpp index dfe6818b..36b86eb9 100644 --- a/kpty/kptydevice.cpp +++ b/kpty/kptydevice.cpp @@ -133,7 +133,6 @@ public: char *reserve(int bytes) { totalSize += bytes; - char *ptr; if (tail + bytes <= buffers.last().size()) { ptr = buffers.last().data() + tail; @@ -169,17 +168,19 @@ public: int start = head; QList::ConstIterator it = buffers.begin(); forever { - if (!maxLength) + if (!maxLength) { return index; - if (index == size()) + } + if (index == size()) { return -1; + } const QByteArray &buf = *it; ++it; - int len = qMin((it == buffers.end() ? tail : buf.size()) - start, - maxLength); + int len = qMin((it == buffers.end() ? tail : buf.size()) - start, maxLength); const char *ptr = buf.data() + start; - if (const char *rptr = (const char *)memchr(ptr, c, len)) + if (const char *rptr = (const char *)memchr(ptr, c, len)) { return index + (rptr - ptr) + 1; + } index += len; maxLength -= len; start = 0; @@ -240,7 +241,8 @@ static void qt_ignore_sigpipe() #define NO_INTR(ret,func) do { ret = func; } while (ret < 0 && errno == EINTR) -class KPtyDevicePrivate : public KPtyPrivate { +class KPtyDevicePrivate : public KPtyPrivate +{ Q_DECLARE_PUBLIC(KPtyDevice) public: @@ -307,7 +309,7 @@ bool KPtyDevicePrivate::_k_canRead() #endif // Useless block braces except in Solaris { - NO_INTR(readBytes, read(q->masterFd(), ptr, available)); + NO_INTR(readBytes, read(q->masterFd(), ptr, available)); } if (readBytes < 0) { readBuffer.unreserve(available); @@ -321,43 +323,38 @@ bool KPtyDevicePrivate::_k_canRead() readNotifier->setEnabled(false); emit q->readEof(); return false; - } else { - if (!emittedReadyRead) { - emittedReadyRead = true; - emit q->readyRead(); - emittedReadyRead = false; - } - return true; } + if (!emittedReadyRead) { + emittedReadyRead = true; + emit q->readyRead(); + emittedReadyRead = false; + } + return true; } bool KPtyDevicePrivate::_k_canWrite() { Q_Q(KPtyDevice); - writeNotifier->setEnabled(false); - if (writeBuffer.isEmpty()) + if (writeBuffer.isEmpty()) { return false; - + } qt_ignore_sigpipe(); int wroteBytes; - NO_INTR(wroteBytes, - write(q->masterFd(), - writeBuffer.readPointer(), writeBuffer.readSize())); + NO_INTR(wroteBytes, write(q->masterFd(), writeBuffer.readPointer(), writeBuffer.readSize())); if (wroteBytes < 0) { q->setErrorString(i18n("Error writing to PTY")); return false; } writeBuffer.free(wroteBytes); - if (!emittedBytesWritten) { emittedBytesWritten = true; emit q->bytesWritten(wroteBytes); emittedBytesWritten = false; } - - if (!writeBuffer.isEmpty()) + if (!writeBuffer.isEmpty()) { writeNotifier->setEnabled(true); + } return true; } @@ -410,10 +407,12 @@ bool KPtyDevicePrivate::doWait(int msecs, bool reading) FD_ZERO(&rfds); FD_ZERO(&wfds); - if (readNotifier->isEnabled()) + if (readNotifier->isEnabled()) { FD_SET(q->masterFd(), &rfds); - if (!writeBuffer.isEmpty()) + } + if (!writeBuffer.isEmpty()) { FD_SET(q->masterFd(), &wfds); + } #ifndef Q_OS_LINUX if (tvp) { @@ -425,25 +424,30 @@ bool KPtyDevicePrivate::doWait(int msecs, bool reading) #endif switch (select(q->masterFd() + 1, &rfds, &wfds, 0, tvp)) { - case -1: - if (errno == EINTR) + case -1: { + if (errno == EINTR) + break; + return false; + } + case 0: { + q->setErrorString(i18n("PTY operation timed out")); + return false; + } + default: { + if (FD_ISSET(q->masterFd(), &rfds)) { + bool canRead = _k_canRead(); + if (reading && canRead) { + return true; + } + } + if (FD_ISSET(q->masterFd(), &wfds)) { + bool canWrite = _k_canWrite(); + if (!reading) { + return canWrite; + } + } break; - return false; - case 0: - q->setErrorString(i18n("PTY operation timed out")); - return false; - default: - if (FD_ISSET(q->masterFd(), &rfds)) { - bool canRead = _k_canRead(); - if (reading && canRead) - return true; } - if (FD_ISSET(q->masterFd(), &wfds)) { - bool canWrite = _k_canWrite(); - if (!reading) - return canWrite; - } - break; } } return false; @@ -452,7 +456,6 @@ bool KPtyDevicePrivate::doWait(int msecs, bool reading) void KPtyDevicePrivate::finishOpen(QIODevice::OpenMode mode) { Q_Q(KPtyDevice); - q->QIODevice::open(mode); fcntl(q->masterFd(), F_SETFL, O_NONBLOCK); readBuffer.clear(); @@ -467,8 +470,8 @@ void KPtyDevicePrivate::finishOpen(QIODevice::OpenMode mode) // public member functions // ///////////////////////////// -KPtyDevice::KPtyDevice(QObject *parent) : - QIODevice(parent), +KPtyDevice::KPtyDevice(QObject *parent) + : QIODevice(parent), KPty(new KPtyDevicePrivate(this)) { } @@ -481,46 +484,37 @@ KPtyDevice::~KPtyDevice() bool KPtyDevice::open(OpenMode mode) { Q_D(KPtyDevice); - - if (masterFd() >= 0) + if (masterFd() >= 0) { return true; - + } if (!KPty::open()) { setErrorString(i18n("Error opening PTY")); return false; } - d->finishOpen(mode); - return true; } bool KPtyDevice::open(int fd, OpenMode mode) { Q_D(KPtyDevice); - if (!KPty::open(fd)) { setErrorString(i18n("Error opening PTY")); return false; } - d->finishOpen(mode); - return true; } void KPtyDevice::close() { Q_D(KPtyDevice); - - if (masterFd() < 0) + if (masterFd() < 0) { return; - + } delete d->readNotifier; delete d->writeNotifier; - QIODevice::close(); - KPty::close(); } @@ -532,19 +526,19 @@ bool KPtyDevice::isSequential() const bool KPtyDevice::canReadLine() const { Q_D(const KPtyDevice); - return QIODevice::canReadLine() || d->readBuffer.canReadLine(); + return (QIODevice::canReadLine() || d->readBuffer.canReadLine()); } bool KPtyDevice::atEnd() const { Q_D(const KPtyDevice); - return QIODevice::atEnd() && d->readBuffer.isEmpty(); + return (QIODevice::atEnd() && d->readBuffer.isEmpty()); } qint64 KPtyDevice::bytesAvailable() const { Q_D(const KPtyDevice); - return QIODevice::bytesAvailable() + d->readBuffer.size(); + return (QIODevice::bytesAvailable() + d->readBuffer.size()); } qint64 KPtyDevice::bytesToWrite() const @@ -596,7 +590,6 @@ qint64 KPtyDevice::writeData(const char *data, qint64 len) { Q_D(KPtyDevice); Q_ASSERT(len <= KMAXINT); - d->writeBuffer.write(data, len); d->writeNotifier->setEnabled(true); return len; diff --git a/kpty/kptyprocess.cpp b/kpty/kptyprocess.cpp index d77b3229..c9ea600d 100644 --- a/kpty/kptyprocess.cpp +++ b/kpty/kptyprocess.cpp @@ -33,54 +33,59 @@ // private data // ////////////////// -class KPtyProcessPrivate : public KProcessPrivate { +class KPtyProcessPrivate : public KProcessPrivate +{ public: - KPtyProcessPrivate() : - ptyChannels(KPtyProcess::NoChannels), + KPtyProcessPrivate() + : ptyChannels(KPtyProcess::NoChannels), addUtmp(false) { } void _k_onStateChanged(QProcess::ProcessState newState) { - if (newState == QProcess::NotRunning && addUtmp) + if (newState == QProcess::NotRunning && addUtmp) { pty->logout(); + } } KPtyDevice *pty; KPtyProcess::PtyChannels ptyChannels; - bool addUtmp : 1; + bool addUtmp; }; KPtyProcess::KPtyProcess(QObject *parent) : KProcess(new KPtyProcessPrivate, parent) { Q_D(KPtyProcess); - d->pty = new KPtyDevice(this); d->pty->open(); - connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), - SLOT(_k_onStateChanged(QProcess::ProcessState))); + connect( + this, SIGNAL(stateChanged(QProcess::ProcessState)), + SLOT(_k_onStateChanged(QProcess::ProcessState)) + ); } KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) : KProcess(new KPtyProcessPrivate, parent) { Q_D(KPtyProcess); - d->pty = new KPtyDevice(this); d->pty->open(ptyMasterFd); - connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), - SLOT(_k_onStateChanged(QProcess::ProcessState))); + connect( + this, SIGNAL(stateChanged(QProcess::ProcessState)), + SLOT(_k_onStateChanged(QProcess::ProcessState)) + ); } KPtyProcess::~KPtyProcess() { Q_D(KPtyProcess); - if (state() != QProcess::NotRunning && d->addUtmp) { - disconnect(SIGNAL(stateChanged(QProcess::ProcessState)), - this, SLOT(_k_onStateChanged(QProcess::ProcessState))); + disconnect( + SIGNAL(stateChanged(QProcess::ProcessState)), + this, SLOT(_k_onStateChanged(QProcess::ProcessState)) + ); d->pty->logout(); } delete d->pty; @@ -89,52 +94,48 @@ KPtyProcess::~KPtyProcess() void KPtyProcess::setPtyChannels(PtyChannels channels) { Q_D(KPtyProcess); - d->ptyChannels = channels; } KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const { Q_D(const KPtyProcess); - return d->ptyChannels; } void KPtyProcess::setUseUtmp(bool value) { Q_D(KPtyProcess); - d->addUtmp = value; } bool KPtyProcess::isUseUtmp() const { Q_D(const KPtyProcess); - return d->addUtmp; } KPtyDevice *KPtyProcess::pty() const { Q_D(const KPtyProcess); - return d->pty; } void KPtyProcess::setupChildProcess() { Q_D(KPtyProcess); - d->pty->setCTty(); if (d->addUtmp) d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().data(), qgetenv("DISPLAY")); - if (d->ptyChannels & StdinChannel) - dup2(d->pty->slaveFd(), 0); - if (d->ptyChannels & StdoutChannel) - dup2(d->pty->slaveFd(), 1); - if (d->ptyChannels & StderrChannel) - dup2(d->pty->slaveFd(), 2); - + if (d->ptyChannels & StdinChannel) { + ::dup2(d->pty->slaveFd(), 0); + } + if (d->ptyChannels & StdoutChannel) { + ::dup2(d->pty->slaveFd(), 1); + } + if (d->ptyChannels & StderrChannel) { + ::dup2(d->pty->slaveFd(), 2); + } KProcess::setupChildProcess(); } diff --git a/kpty/kptyprocess.h b/kpty/kptyprocess.h index ced7bbfe..baa85faa 100644 --- a/kpty/kptyprocess.h +++ b/kpty/kptyprocess.h @@ -27,7 +27,6 @@ #include "kpty_export.h" class KPtyDevice; - class KPtyProcessPrivate; /** @@ -65,7 +64,7 @@ public: /** * Constructor */ - explicit KPtyProcess(QObject *parent = 0); + explicit KPtyProcess(QObject *parent = nullptr); /** * Construct a process using an open pty master. @@ -74,7 +73,7 @@ public: * The process does not take ownership of the descriptor; * it will not be automatically closed at any point. */ - KPtyProcess(int ptyMasterFd, QObject *parent = 0); + KPtyProcess(int ptyMasterFd, QObject *parent = nullptr); /** * Destructor @@ -136,4 +135,4 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(KPtyProcess::PtyChannels) -#endif +#endif // KPTYPROCESS_H