Focus: correctly handle delayed newline

Previously, a newline that wasn't read with the rest of the command in
a single event would get appended to the command buffer instead of
remaining in the "peek" buffer, so the command dispatch would never
fire.

Stash a space delimiter instead of storing it in the buffer, only to
overwrite it later.

Fix an off-by-one error that could cause a buffered command to not be
null-terminated. This probably didn't cause a problem in practice,
because Focus plugins mostly did a `strcmp` against a fixed string that
is smaller than the command buffer size.

Signed-off-by: Taylor Yu <tlyu@mit.edu>
pull/1226/head
Taylor Yu 2 years ago
parent 35aa03b63d
commit af3dc61710

@ -33,6 +33,7 @@ namespace kaleidoscope {
namespace plugin { namespace plugin {
EventHandlerResult FocusSerial::afterEachCycle() { EventHandlerResult FocusSerial::afterEachCycle() {
int c;
// GD32 doesn't currently autoflush the very last packet. So manually flush here // GD32 doesn't currently autoflush the very last packet. So manually flush here
Runtime.serialPort().flush(); Runtime.serialPort().flush();
// If the serial buffer is empty, we don't have any work to do // If the serial buffer is empty, we don't have any work to do
@ -41,33 +42,28 @@ EventHandlerResult FocusSerial::afterEachCycle() {
} }
do { do {
command_[buf_cursor_++] = Runtime.serialPort().read(); // If there's a newline pending, don't read it
} while (command_[buf_cursor_ - 1] != SEPARATOR && buf_cursor_ < sizeof(command_) && Runtime.serialPort().available() && (Runtime.serialPort().peek() != NEWLINE)); if (Runtime.serialPort().peek() == NEWLINE) {
break;
}
// If there was no command, there's nothing to do c = Runtime.serialPort().read();
if (command_[0] == '\0') { // Don't store the separator; just stash it
buf_cursor_ = 0; if (c == SEPARATOR) {
memset(command_, 0, sizeof(command_)); break;
return EventHandlerResult::OK; }
} command_[buf_cursor_++] = c;
} while (buf_cursor_ < (sizeof(command_) - 1) && Runtime.serialPort().available());
if ((command_[buf_cursor_ - 1] != SEPARATOR) && (Runtime.serialPort().peek() != NEWLINE) && buf_cursor_ < sizeof(command_)) { if ((c != SEPARATOR) && (Runtime.serialPort().peek() != NEWLINE) && buf_cursor_ < (sizeof(command_) - 1)) {
// We don't have enough command to work with yet. // We don't have enough command to work with yet.
// Let's leave the buffer around for another cycle // Let's leave the buffer around for another cycle
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
// If this was a command with a space-delimited payload,
// strip the space delimiter off
if ((command_[buf_cursor_ - 1] == SEPARATOR)) {
command_[buf_cursor_ - 1] = '\0';
}
// Then process the command // Then process the command
Runtime.onFocusEvent(command_); Runtime.onFocusEvent(command_);
while (Runtime.serialPort().available()) { while (Runtime.serialPort().available()) {
char c = Runtime.serialPort().read(); c = Runtime.serialPort().read();
if (c == NEWLINE) { if (c == NEWLINE) {
// newline serves as an end-of-command marker // newline serves as an end-of-command marker
// don't drain the buffer past there // don't drain the buffer past there

Loading…
Cancel
Save