[posix] fix ot-ctl CLI output missing (#5087)

An issue occurs when ot-ctl reads the CLI output in multiple chunks in
which the last line of a chunk does not ends with a '\r\n'. The last
line is then dropped incorrectly.

This commit fixes this issue:

- Output the last line (without \r\n) if it's not ">" (have to drop
  '>' because we can not determine whether or not to output '>')

- Output ">" if the first char is not ' ' but the previous promptState
  is 1 (meaning '>' was dropped previous but should have been
  printed).
This commit is contained in:
Simon Lin
2020-06-11 11:29:21 -07:00
committed by GitHub
parent 2c258a3f3e
commit 7f3013cb06
+14 -1
View File
@@ -40,6 +40,7 @@
#define OPENTHREAD_USE_READLINE (HAVE_LIBEDIT || HAVE_LIBREADLINE)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -295,10 +296,12 @@ int main(int argc, char *argv[])
}
else
{
size_t lineStart = 0;
ssize_t lineStart = 0;
for (ssize_t i = 0; i < rval; i++)
{
int prevPromptState = promptState;
if (FindPrompt(promptState, buffer[i]))
{
doneState = 0;
@@ -306,6 +309,10 @@ int main(int argc, char *argv[])
lineStart = i + 1;
continue;
}
else if (prevPromptState == 1 && i == 0)
{
VerifyOrExit(DoWrite(STDOUT_FILENO, ">", 1), ret = OT_EXIT_FAILURE);
}
if (buffer[i] == '\r' || buffer[i] == '\n')
{
@@ -320,6 +327,12 @@ int main(int argc, char *argv[])
ret = OT_EXIT_SUCCESS;
}
}
if (lineStart < rval && promptState != 1)
{
assert(promptState != 0 && promptState != 2);
VerifyOrExit(DoWrite(STDOUT_FILENO, buffer + lineStart, rval - lineStart), ret = OT_EXIT_FAILURE);
}
}
}
}