From 619c2b383cb90ee1c141a14edc0f5cfa7c05f35f Mon Sep 17 00:00:00 2001 From: Huang Qi <757509347@qq.com> Date: Fri, 11 Dec 2020 19:48:35 +0800 Subject: [PATCH] product-mini/nuttx: Use readline for repl mode (#469) The EOL of NuttX is configurable (CR/LF/CRLF), the implementation of getline in NuttX need CR or LF as line delimiter(CRLF not supported). Then we should use readline for better compatibility. Signed-off-by: Huang Qi Co-authored-by: Huang Qi --- product-mini/platforms/nuttx/main.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/product-mini/platforms/nuttx/main.c b/product-mini/platforms/nuttx/main.c index 2467cf8e..0ddf7c12 100644 --- a/product-mini/platforms/nuttx/main.c +++ b/product-mini/platforms/nuttx/main.c @@ -10,6 +10,8 @@ #include #include +#include + #include "bh_platform.h" #include "bh_read_file.h" #include "wasm_export.h" @@ -119,13 +121,20 @@ split_string(char *str, int *count) static void * app_instance_repl(wasm_module_inst_t module_inst) { - char *cmd = NULL; - size_t len = 0; + size_t len = 128; + char *cmd = malloc(len); ssize_t n; - while ((printf("webassembly> "), n = getline(&cmd, &len, stdin)) != -1) { + if (NULL == cmd) { + LOG_ERROR("Wasm repl cmd buffer alloc failed.\n"); + return NULL; + } + + while ( + (printf("webassembly> "), fflush(stdout), n = std_readline(cmd, len)) + != -1) { bh_assert(n > 0); - if (cmd[n - 1] == '\n') { + if ((cmd[n - 1] == '\n') || (cmd[n - 1] == '\r')) { if (n == 1) continue; else