From ece41b04e3d5edeaf747665ab91d24b02eb7c882 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 3 Apr 2020 00:16:21 +0200 Subject: [PATCH] console: make empty line behavior run-time configurable --- components/console/Kconfig | 10 ---------- components/console/linenoise/linenoise.c | 12 +++++++----- components/console/linenoise/linenoise.h | 3 +++ docs/en/api-reference/system/console.rst | 3 +++ examples/system/console/main/console_example_main.c | 8 +++++--- 5 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 components/console/Kconfig diff --git a/components/console/Kconfig b/components/console/Kconfig deleted file mode 100644 index a153b490de..0000000000 --- a/components/console/Kconfig +++ /dev/null @@ -1,10 +0,0 @@ -menu "Linenoise" - - config ESP_LINENOISE_RETURN_ZERO_STRING - bool "Return 0 length strings" - default n - help - If enabled linenoise will return 0 length strings if the user presses enter with out typing - a command, if disabled NULL will be returned instead. - -endmenu \ No newline at end of file diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 0cf86fc0c2..91dc9e3d2e 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -116,7 +116,6 @@ #include #include #include "linenoise.h" -#include "sdkconfig.h" #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 @@ -130,6 +129,7 @@ static int dumbmode = 0; /* Dumb mode where line editing is disabled. Off by def static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; static int history_len = 0; static char **history = NULL; +static bool allow_empty = true; /* The linenoiseState structure represents the state during line editing. * We pass this state to functions implementing specific editing @@ -888,6 +888,10 @@ static int linenoiseEdit(char *buf, size_t buflen, const char *prompt) return l.len; } +void linenoiseAllowEmpty(bool val) { + allow_empty = val; +} + int linenoiseProbe(void) { /* Switch to non-blocking mode */ int flags = fcntl(STDIN_FILENO, F_GETFL); @@ -980,13 +984,11 @@ char *linenoise(const char *prompt) { } else { count = linenoiseDumb(buf, LINENOISE_MAX_LINE, prompt); } -#ifdef CONFIG_ESP_LINENOISE_RETURN_ZERO_STRING - if (count >= 0) { -#else if (count > 0) { -#endif sanitize(buf); count = strlen(buf); + } else if (count == 0 && allow_empty) { + /* will return an empty (0-length) string */ } else { free(buf); return NULL; diff --git a/components/console/linenoise/linenoise.h b/components/console/linenoise/linenoise.h index 04c6f8b0cc..58756a5230 100644 --- a/components/console/linenoise/linenoise.h +++ b/components/console/linenoise/linenoise.h @@ -43,6 +43,8 @@ extern "C" { #endif +#include + typedef struct linenoiseCompletions { size_t len; char **cvec; @@ -68,6 +70,7 @@ void linenoiseClearScreen(void); void linenoiseSetMultiLine(int ml); void linenoiseSetDumbMode(int set); void linenoisePrintKeyCodes(void); +void linenoiseAllowEmpty(bool); #ifdef __cplusplus } diff --git a/docs/en/api-reference/system/console.rst b/docs/en/api-reference/system/console.rst index d256a517e1..273bf4a886 100644 --- a/docs/en/api-reference/system/console.rst +++ b/docs/en/api-reference/system/console.rst @@ -36,6 +36,9 @@ Linenoise library does not need explicit initialization. However, some configura :cpp:func:`linenoiseSetMultiLine` Switch between single line and multi line editing modes. In single line mode, if the length of the command exceeds the width of the terminal, the command text is scrolled within the line to show the end of the text. In this case the beginning of the text is hidden. Single line needs less data to be sent to refresh screen on each key press, so exhibits less glitching compared to the multi line mode. On the flip side, editing commands and copying command text from terminal in single line mode is harder. Default is single line mode. +:cpp:func:`linenoiseAllowEmpty` + Set whether linenoise library will return a zero-length string (if ``true``) or ``NULL`` (if ``false``) for empty lines. By default, zero-length strings are returned. + Main loop ^^^^^^^^^ diff --git a/examples/system/console/main/console_example_main.c b/examples/system/console/main/console_example_main.c index 0292b7fd72..c91d79509f 100644 --- a/examples/system/console/main/console_example_main.c +++ b/examples/system/console/main/console_example_main.c @@ -112,6 +112,9 @@ static void initialize_console(void) /* Set command history size */ linenoiseHistorySetMaxLen(100); + /* Don't return empty lines */ + linenoiseAllowEmpty(false); + #if CONFIG_STORE_HISTORY /* Load command history from filesystem */ linenoiseHistoryLoad(HISTORY_PATH); @@ -174,7 +177,7 @@ void app_main(void) break; } /* Add the command to the history if not empty*/ - if(strlen(line) > 0) { + if (strlen(line) > 0) { linenoiseHistoryAdd(line); #if CONFIG_STORE_HISTORY /* Save command history to filesystem */ @@ -197,8 +200,7 @@ void app_main(void) /* linenoise allocates line buffer on the heap, so need to free it */ linenoiseFree(line); } - + ESP_LOGE(TAG, "Error or end-of-input, terminating console"); esp_console_deinit(); - vTaskDelete(NULL); /* terminate app_main */ }