From 1efb4699d48f5b2b28c3132ceecf86d0518e681b 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 08:59:26 +0300 Subject: [PATCH] some code refactoring --- components/console/linenoise/linenoise.c | 22 +++++++++++----------- components/console/linenoise/linenoise.h | 2 +- main/console.c | 7 +++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index 925b705..d57bddd 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -924,14 +924,11 @@ uint32_t getMillis(void) { * fails. If stdin_fd or stdout_fd are set to -1, the default is to use * STDIN_FILENO and STDOUT_FILENO. */ -int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const char *prompt) { +int linenoiseEditStart(struct linenoiseState *l) { /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ l->in_completion = 0; - l->buf = buf; - l->buflen = buflen; - l->prompt = prompt; - l->plen = strlen(prompt); + l->plen = strlen(l->prompt); l->oldpos = l->pos = 0; l->len = 0; l->cols = getColumns(); @@ -948,7 +945,7 @@ int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const if (!dumbmode) { linenoiseHistoryAdd(""); int pos1 = getCursorPosition(); - if (fwrite(prompt,l->plen,1,stdout) == -1) { + if (fwrite(l->prompt,l->plen,1,stdout) == -1) { xSemaphoreGive(stdout_taken_sem); return -1; } @@ -958,7 +955,7 @@ int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const l->plen = pos2 - pos1; } } else { - if (fwrite(prompt,l->plen,1,stdout) == -1) { + if (fwrite(l->prompt,l->plen,1,stdout) == -1) { xSemaphoreGive(stdout_taken_sem); return -1; } @@ -1201,16 +1198,19 @@ 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, size_t buflen, const char *prompt) +static char *linenoiseBlockingEdit(char *buf, const char *prompt) { struct linenoiseState l; /* Editing without a buffer is invalid. */ - if (buflen == 0) { + if (buf == NULL) { errno = EINVAL; return NULL; } char *res; - linenoiseEditStart(&l,buf,buflen,prompt); + l.buf = buf; + l.buflen = max_cmdline_length; + l.prompt = prompt; + linenoiseEditStart(&l); while((res = linenoiseEditFeed(&l)) == linenoiseEditMore); linenoiseEditStop(&l); return res; @@ -1267,7 +1267,7 @@ int linenoiseProbe() { /* The high level function that is the main API of the linenoise library. */ char *linenoise(const char *prompt) { char *buf = calloc(1, max_cmdline_length); - char *retval = linenoiseBlockingEdit(buf,max_cmdline_length,prompt); + char *retval = linenoiseBlockingEdit(buf, prompt); free(buf); return retval; } diff --git a/components/console/linenoise/linenoise.h b/components/console/linenoise/linenoise.h index 7022361..0b99d16 100644 --- a/components/console/linenoise/linenoise.h +++ b/components/console/linenoise/linenoise.h @@ -80,7 +80,7 @@ typedef struct linenoiseCompletions { } linenoiseCompletions; /* Non blocking API. */ -int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const char *prompt); +int linenoiseEditStart(struct linenoiseState *l); char *linenoiseEditFeed(struct linenoiseState *l); void linenoiseEditStop(struct linenoiseState *l); void linenoiseHide(struct linenoiseState *l); diff --git a/main/console.c b/main/console.c index ed8ce48..ec9174f 100644 --- a/main/console.c +++ b/main/console.c @@ -87,7 +87,10 @@ void console_task_interactive(void* arg) { "Press TAB when typing command name to auto-complete.\n" "Ctrl+C will terminate the console environment.\n"); get_prompt(prompt); - linenoiseEditStart(&ls, buf, console_config.max_cmdline_length, prompt); + ls.buflen = console_config.max_cmdline_length; + ls.buf = buf; + ls.prompt = prompt; + linenoiseEditStart(&ls); while (true) { line = linenoiseEditFeed(&ls); if (line == linenoiseEditMore) continue; @@ -117,7 +120,7 @@ void console_task_interactive(void* arg) { /* linenoise allocates line buffer on the heap, so need to free it */ linenoiseFree(line); get_prompt(prompt); - linenoiseEditStart(&ls, buf, console_config.max_cmdline_length, prompt); + linenoiseEditStart(&ls); } ESP_LOGE(TAG, "Terminating console");