From ba2c37c9c807ef34b604d204421fcb82f278d532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=BE?= Date: Fri, 24 Nov 2023 09:09:15 +0300 Subject: [PATCH] Changed some API --- components/console/esp_console_repl.c | 2 +- components/console/linenoise/linenoise.c | 24 +++++++++++++----------- components/console/linenoise/linenoise.h | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/components/console/esp_console_repl.c b/components/console/esp_console_repl.c index 123572d..e971371 100644 --- a/components/console/esp_console_repl.c +++ b/components/console/esp_console_repl.c @@ -516,7 +516,7 @@ static void esp_console_repl_task(void *args) linenoiseSetMaxLineLen(repl_com->max_cmdline_length); while (repl_com->state == CONSOLE_REPL_STATE_START) { - char *line = linenoise(repl_com->prompt); + char *line = linenoise(repl_com->prompt, NULL); if (line == NULL) { ESP_LOGD(TAG, "empty line"); /* Ignore empty lines */ diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index d57bddd..90db2a7 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -1198,21 +1198,18 @@ void linenoiseEditStop(struct linenoiseState *l) { * In many applications that are not event-drivern, we can just call * the blocking linenoise API, wait for the user to complete the editing * and return the buffer. */ -static char *linenoiseBlockingEdit(char *buf, const char *prompt) +static char *linenoiseBlockingEdit(struct linenoiseState *l) { - struct linenoiseState l; /* Editing without a buffer is invalid. */ - if (buf == NULL) { + if (l->buf == NULL) { errno = EINVAL; return NULL; } char *res; - l.buf = buf; - l.buflen = max_cmdline_length; - l.prompt = prompt; - linenoiseEditStart(&l); - while((res = linenoiseEditFeed(&l)) == linenoiseEditMore); - linenoiseEditStop(&l); + l->buflen = max_cmdline_length; + linenoiseEditStart(l); + while((res = linenoiseEditFeed(l)) == linenoiseEditMore); + linenoiseEditStop(l); return res; } @@ -1265,9 +1262,14 @@ int linenoiseProbe() { } /* The high level function that is the main API of the linenoise library. */ -char *linenoise(const char *prompt) { +// passing ls_to_pass is optional, you can just provide NULL +char *linenoise(const char *prompt, struct linenoiseState **ls_to_pass) { + struct linenoiseState l; + if (ls_to_pass != NULL) *ls_to_pass = &l; char *buf = calloc(1, max_cmdline_length); - char *retval = linenoiseBlockingEdit(buf, prompt); + l.prompt = prompt; + l.buf = buf; + char *retval = linenoiseBlockingEdit(&l); free(buf); return retval; } diff --git a/components/console/linenoise/linenoise.h b/components/console/linenoise/linenoise.h index 0b99d16..ac915c3 100644 --- a/components/console/linenoise/linenoise.h +++ b/components/console/linenoise/linenoise.h @@ -87,7 +87,7 @@ void linenoiseHide(struct linenoiseState *l); void linenoiseShow(struct linenoiseState *l); /* Blocking API. */ -char *linenoise(const char *prompt); +char *linenoise(const char *prompt, struct linenoiseState **ls_to_pass); void linenoiseFree(void *ptr); /* Completion API. */