From 7f3013cb06fd3f61aed2d7820fb7c08e2fbc6be2 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Fri, 12 Jun 2020 02:29:21 +0800 Subject: [PATCH] [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). --- src/posix/client.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/posix/client.cpp b/src/posix/client.cpp index 2cb16b172..0002df2ee 100644 --- a/src/posix/client.cpp +++ b/src/posix/client.cpp @@ -40,6 +40,7 @@ #define OPENTHREAD_USE_READLINE (HAVE_LIBEDIT || HAVE_LIBREADLINE) +#include #include #include #include @@ -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); + } } } }