From 8400472f7033d6960ff90a138c7bfea884742a10 Mon Sep 17 00:00:00 2001 From: batt Date: Fri, 13 Nov 2009 18:18:10 +0000 Subject: [PATCH] Fix history handling. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3111 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/mware/readline.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/bertos/mware/readline.c b/bertos/mware/readline.c index d7c5aa8c..35afe50b 100644 --- a/bertos/mware/readline.c +++ b/bertos/mware/readline.c @@ -366,13 +366,6 @@ void rl_refresh(struct RLContext* ctx) const char* rl_readline(struct RLContext* ctx) { - size_t i = ctx->history_pos; - - if (ctx->prompt) - rl_puts(ctx, ctx->prompt); - - insert_chars(ctx, &i, NULL, 0); - while (1) { char ch; @@ -391,20 +384,12 @@ const char* rl_readline(struct RLContext* ctx) { // Ask the match hook if available if (!ctx->match) - return false; + return NULL; complete_word(ctx, &ctx->line_pos); continue; } - if (c == '\r' || c == '\n') - { - // Terminate line - insert_chars(ctx, &ctx->line_pos, NULL, 0); - rl_puts(ctx, "\r\n"); - break; - } - // Backspace cancels a character, or it is ignored if at // the start of the line if (c == '\b') @@ -417,17 +402,20 @@ const char* rl_readline(struct RLContext* ctx) continue; } + if (c == '\r' || c == '\n') + { + rl_puts(ctx, "\r\n"); + break; + } + + // Add a character to the buffer, if possible ch = (char)c; ASSERT2(ch == c, "a special key was not properly handled"); if (insert_chars(ctx, &ctx->line_pos, &ch, 1)) - { rl_putc(ctx, ch); - } else - { beep(ctx); - } } ctx->history_pos = ctx->line_pos + 1; @@ -442,9 +430,18 @@ const char* rl_readline(struct RLContext* ctx) dump_history(ctx); #endif + const char *buf = &ctx->history[ctx->line_pos + 1]; + + ctx->line_pos = ctx->history_pos; + + if (ctx->prompt) + rl_puts(ctx, ctx->prompt); + + insert_chars(ctx, &ctx->line_pos, NULL, 0); + // Since the current pointer now points to the separator, we need // to return the first character - return &ctx->history[ctx->line_pos + 1]; + return buf; } -- 2.25.1