Skip to content

Commit

Permalink
Simplify STDIN I/O and remove carriage returns
Browse files Browse the repository at this point in the history
  • Loading branch information
mortbopet committed Jan 30, 2022
1 parent 7878f8f commit c1e6e11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 16 additions & 15 deletions src/syscall/systemio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c1e6e11

Please sign in to comment.