Skip to content

Commit

Permalink
Exit shell in case of Ctrl-D input with empty line (#15294)
Browse files Browse the repository at this point in the history
This commit adds a default EOT handler to the shell main loop. With
that, the shell app can be exited with Ctrl+D keyboard sequence.
  • Loading branch information
arkq authored and pull[bot] committed Oct 10, 2023
1 parent 22184e8 commit 5242001
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/lib/shell/MainLoopDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using chip::Shell::streamer_get;

namespace {

void ReadLine(char * buffer, size_t max)
ssize_t ReadLine(char * buffer, size_t max)
{
ssize_t read = 0;
bool done = false;
Expand All @@ -56,6 +56,15 @@ void ReadLine(char * buffer, size_t max)
*inptr = 0; // null terminate
done = true;
break;
case 0x04:
// Delete EOT character.
inptr -= 1;
// Stop the read loop if the input is still empty.
if (inptr == buffer - 1)
{
done = true;
}
break;
case 0x7F:
// delete backspace character + 1 more
inptr -= 2;
Expand Down Expand Up @@ -84,6 +93,9 @@ void ReadLine(char * buffer, size_t max)
read--;
}
}

// Return the length of the buffer including the terminating null byte.
return inptr - buffer;
}

bool IsSeparator(char ch)
Expand Down Expand Up @@ -186,7 +198,11 @@ void Engine::RunMainLoop()
while (true)
{
char * line = static_cast<char *>(Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE));
ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE);
if (ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE) == 0)
{
// Stop loop in case of empty read (Ctrl-D).
break;
}
#if CONFIG_DEVICE_LAYER
DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast<intptr_t>(line));
#else
Expand Down

0 comments on commit 5242001

Please sign in to comment.