some code refactoring

This commit is contained in:
Данила Горнушко 2023-11-24 08:59:26 +03:00
parent 89cdbfd39c
commit 1efb4699d4
3 changed files with 17 additions and 14 deletions

View file

@ -924,14 +924,11 @@ uint32_t getMillis(void) {
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use * fails. If stdin_fd or stdout_fd are set to -1, the default is to use
* STDIN_FILENO and STDOUT_FILENO. * 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 /* Populate the linenoise state that we pass to functions implementing
* specific editing functionalities. */ * specific editing functionalities. */
l->in_completion = 0; l->in_completion = 0;
l->buf = buf; l->plen = strlen(l->prompt);
l->buflen = buflen;
l->prompt = prompt;
l->plen = strlen(prompt);
l->oldpos = l->pos = 0; l->oldpos = l->pos = 0;
l->len = 0; l->len = 0;
l->cols = getColumns(); l->cols = getColumns();
@ -948,7 +945,7 @@ int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const
if (!dumbmode) { if (!dumbmode) {
linenoiseHistoryAdd(""); linenoiseHistoryAdd("");
int pos1 = getCursorPosition(); int pos1 = getCursorPosition();
if (fwrite(prompt,l->plen,1,stdout) == -1) { if (fwrite(l->prompt,l->plen,1,stdout) == -1) {
xSemaphoreGive(stdout_taken_sem); xSemaphoreGive(stdout_taken_sem);
return -1; return -1;
} }
@ -958,7 +955,7 @@ int linenoiseEditStart(struct linenoiseState *l, char *buf, size_t buflen, const
l->plen = pos2 - pos1; l->plen = pos2 - pos1;
} }
} else { } else {
if (fwrite(prompt,l->plen,1,stdout) == -1) { if (fwrite(l->prompt,l->plen,1,stdout) == -1) {
xSemaphoreGive(stdout_taken_sem); xSemaphoreGive(stdout_taken_sem);
return -1; return -1;
} }
@ -1201,16 +1198,19 @@ void linenoiseEditStop(struct linenoiseState *l) {
* In many applications that are not event-drivern, we can just call * In many applications that are not event-drivern, we can just call
* the blocking linenoise API, wait for the user to complete the editing * the blocking linenoise API, wait for the user to complete the editing
* and return the buffer. */ * 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; struct linenoiseState l;
/* Editing without a buffer is invalid. */ /* Editing without a buffer is invalid. */
if (buflen == 0) { if (buf == NULL) {
errno = EINVAL; errno = EINVAL;
return NULL; return NULL;
} }
char *res; 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); while((res = linenoiseEditFeed(&l)) == linenoiseEditMore);
linenoiseEditStop(&l); linenoiseEditStop(&l);
return res; return res;
@ -1267,7 +1267,7 @@ int linenoiseProbe() {
/* The high level function that is the main API of the linenoise library. */ /* The high level function that is the main API of the linenoise library. */
char *linenoise(const char *prompt) { char *linenoise(const char *prompt) {
char *buf = calloc(1, max_cmdline_length); char *buf = calloc(1, max_cmdline_length);
char *retval = linenoiseBlockingEdit(buf,max_cmdline_length,prompt); char *retval = linenoiseBlockingEdit(buf, prompt);
free(buf); free(buf);
return retval; return retval;
} }

View file

@ -80,7 +80,7 @@ typedef struct linenoiseCompletions {
} linenoiseCompletions; } linenoiseCompletions;
/* Non blocking API. */ /* 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); char *linenoiseEditFeed(struct linenoiseState *l);
void linenoiseEditStop(struct linenoiseState *l); void linenoiseEditStop(struct linenoiseState *l);
void linenoiseHide(struct linenoiseState *l); void linenoiseHide(struct linenoiseState *l);

View file

@ -87,7 +87,10 @@ void console_task_interactive(void* arg) {
"Press TAB when typing command name to auto-complete.\n" "Press TAB when typing command name to auto-complete.\n"
"Ctrl+C will terminate the console environment.\n"); "Ctrl+C will terminate the console environment.\n");
get_prompt(prompt); 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) { while (true) {
line = linenoiseEditFeed(&ls); line = linenoiseEditFeed(&ls);
if (line == linenoiseEditMore) continue; 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 */ /* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line); linenoiseFree(line);
get_prompt(prompt); get_prompt(prompt);
linenoiseEditStart(&ls, buf, console_config.max_cmdline_length, prompt); linenoiseEditStart(&ls);
} }
ESP_LOGE(TAG, "Terminating console"); ESP_LOGE(TAG, "Terminating console");