Skip to content

Commit 2535640

Browse files
arkqpull[bot]
authored andcommitted
Exit shell in case of Ctrl-D input with empty line (#15294)
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.
1 parent a1ae59f commit 2535640

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/lib/shell/MainLoopDefault.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using chip::Shell::streamer_get;
3131

3232
namespace {
3333

34-
void ReadLine(char * buffer, size_t max)
34+
ssize_t ReadLine(char * buffer, size_t max)
3535
{
3636
ssize_t read = 0;
3737
bool done = false;
@@ -56,6 +56,15 @@ void ReadLine(char * buffer, size_t max)
5656
*inptr = 0; // null terminate
5757
done = true;
5858
break;
59+
case 0x04:
60+
// Delete EOT character.
61+
inptr -= 1;
62+
// Stop the read loop if the input is still empty.
63+
if (inptr == buffer - 1)
64+
{
65+
done = true;
66+
}
67+
break;
5968
case 0x7F:
6069
// delete backspace character + 1 more
6170
inptr -= 2;
@@ -84,6 +93,9 @@ void ReadLine(char * buffer, size_t max)
8493
read--;
8594
}
8695
}
96+
97+
// Return the length of the buffer including the terminating null byte.
98+
return inptr - buffer;
8799
}
88100

89101
bool IsSeparator(char ch)
@@ -186,7 +198,11 @@ void Engine::RunMainLoop()
186198
while (true)
187199
{
188200
char * line = static_cast<char *>(Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE));
189-
ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE);
201+
if (ReadLine(line, CHIP_SHELL_MAX_LINE_SIZE) == 0)
202+
{
203+
// Stop loop in case of empty read (Ctrl-D).
204+
break;
205+
}
190206
#if CONFIG_DEVICE_LAYER
191207
DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLine, reinterpret_cast<intptr_t>(line));
192208
#else

0 commit comments

Comments
 (0)