From c1e6e11c2dc1c77167b3020d23414f3abd4c1ee5 Mon Sep 17 00:00:00 2001 From: Morten Borup Petersen Date: Sun, 30 Jan 2022 17:26:12 +0100 Subject: [PATCH] Simplify STDIN I/O and remove carriage returns --- src/console.cpp | 4 ++-- src/syscall/systemio.h | 31 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 5c2e47726..86532a0c6 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -78,8 +78,8 @@ void Console::keyPressEvent(QKeyEvent* e) { const QString text = e->text(); // Buffer managing if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { - // Return is interpreted as \r\n instead of the default \r - m_buffer += "\r\n"; + // Return is interpreted as \n instead of the default \r + m_buffer += "\n"; // Flush buffer to output emit sendData(m_buffer.toLocal8Bit()); diff --git a/src/syscall/systemio.h b/src/syscall/systemio.h index dd32df88e..48f03a3ef 100644 --- a/src/syscall/systemio.h +++ b/src/syscall/systemio.h @@ -326,24 +326,25 @@ class SystemIO : public QObject { if (fd == STDIN) { // systemIO might be called from non-gui thread, so be threadsafe in interacting with the ui. postToGUIThread([=] { SystemIOStatusManager::setStatusTimed("Waiting for user input...", 99999999); }); - while (myBuffer.size() == 0) { + while (myBuffer.size() < lengthRequested) { // Lock the stdio objects and try to read from stdio. If no data is present, wait until so. FileIOData::s_stdioMutex.lock(); - while (myBuffer.size() == 0) { - /** We spin on a wait condition with a timeout. The timeout is required to ensure that we may - * observe any abort flags (ie. if execution is stopped while waiting for IO */ - const bool dataInStdinStrm = FileIOData::s_stdinBufferEmpty.wait(&FileIOData::s_stdioMutex, 100); - if (s_abortSyscall) { - FileIOData::s_stdioMutex.unlock(); - s_abortSyscall = false; - SystemIOStatusManager::clearStatus(); - return -1; - } - if (dataInStdinStrm) { - myBuffer = InputStream.read(lengthRequested).toUtf8(); - } + if (s_abortSyscall) { + FileIOData::s_stdioMutex.unlock(); + s_abortSyscall = false; + postToGUIThread([=] { SystemIOStatusManager::clearStatus(); }); + return -1; } + auto readData = InputStream.read(lengthRequested).toUtf8(); + myBuffer.append(readData); + lengthRequested -= readData.length(); + + /** We spin on a wait condition with a timeout. The timeout is required to ensure that we may + * observe any abort flags (ie. if execution is stopped while waiting for IO */ + FileIOData::s_stdinBufferEmpty.wait(&FileIOData::s_stdioMutex, 100); FileIOData::s_stdioMutex.unlock(); + if (myBuffer.endsWith('\n')) + break; } } else { // Reads up to lengthRequested bytes of data from this Input stream into an array of bytes. @@ -416,8 +417,8 @@ public slots: void putStdInData(const QByteArray& data) { FileIOData::s_stdioMutex.lock(); FileIOData::s_stdinBuffer.append(data); - FileIOData::s_stdioMutex.unlock(); FileIOData::s_stdinBufferEmpty.wakeAll(); + FileIOData::s_stdioMutex.unlock(); } private: