kpty: format and indent

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-20 05:30:06 +03:00
parent 3aaef7077e
commit 575b10a273
4 changed files with 91 additions and 103 deletions

View file

@ -126,11 +126,9 @@ KPty::~KPty()
bool KPty::open()
{
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;
}

View file

@ -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<QByteArray>::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:
@ -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;
}
}
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,34 +424,38 @@ bool KPtyDevicePrivate::doWait(int msecs, bool reading)
#endif
switch (select(q->masterFd() + 1, &rfds, &wfds, 0, tvp)) {
case -1:
case -1: {
if (errno == EINTR)
break;
return false;
case 0:
}
case 0: {
q->setErrorString(i18n("PTY operation timed out"));
return false;
default:
}
default: {
if (FD_ISSET(q->masterFd(), &rfds)) {
bool canRead = _k_canRead();
if (reading && canRead)
if (reading && canRead) {
return true;
}
}
if (FD_ISSET(q->masterFd(), &wfds)) {
bool canWrite = _k_canWrite();
if (!reading)
if (!reading) {
return canWrite;
}
}
break;
}
}
}
return false;
}
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;

View file

@ -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();
}

View file

@ -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