From e39b351c443b613123d5e8ab310e2b4b8663e71c Mon Sep 17 00:00:00 2001 From: asterix Date: Fri, 21 Mar 2008 00:05:04 +0000 Subject: [PATCH] Add clear hook. Refactor. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1210 38d2e660-2303-0410-9eaa-f027e97ec537 --- mware/readline.c | 36 ++++++++++++++++++++++++++---------- mware/readline.h | 8 ++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/mware/readline.c b/mware/readline.c index a8a92810..4c3b3a81 100644 --- a/mware/readline.c +++ b/mware/readline.c @@ -75,6 +75,7 @@ #include #include +#include /// Enable compilation of the unit test code @@ -165,8 +166,14 @@ INLINE void rl_putc(const struct RLContext* ctx, char ch) static bool rl_getc(const struct RLContext* ctx, int* ch) { int c = ctx->get(ctx->get_param); + if (c == EOF) + { + if (ctx->clear) + ctx->clear(ctx->clear_param); + return false; + } if (c == 0x1B) { @@ -373,7 +380,7 @@ const char* rl_readline(struct RLContext* ctx) char ch; int c; - ASSERT(ctx->history - ctx->real_history + i < HISTORY_SIZE); + ASSERT(ctx->history - ctx->real_history + ctx->line_pos < HISTORY_SIZE); if (!rl_getc(ctx, &c)) return NULL; @@ -388,12 +395,17 @@ const char* rl_readline(struct RLContext* ctx) if (!ctx->match) return false; - complete_word(ctx, &i); + complete_word(ctx, &ctx->line_pos); continue; } if (c == '\r' || c == '\n') { + if (ctx->prompt) + rl_puts(ctx, ctx->prompt); + + // Terminate line + insert_chars(ctx, &ctx->line_pos, NULL, 0); rl_puts(ctx, "\r\n"); break; } @@ -402,9 +414,9 @@ const char* rl_readline(struct RLContext* ctx) // the start of the line if (c == '\b') { - if (ctx->history[i] != '\0') + if (ctx->history[ctx->line_pos] != '\0') { - --i; + --ctx->line_pos; rl_puts(ctx, "\b \b"); } continue; @@ -413,18 +425,22 @@ const char* rl_readline(struct RLContext* ctx) // 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, &i, &ch, 1)) + if (insert_chars(ctx, &ctx->line_pos, &ch, 1)) + { rl_putc(ctx, ch); + } else + { beep(ctx); + } } - ctx->history_pos = i+1; - while (ctx->history[i] != '\0') - --i; + ctx->history_pos = ctx->line_pos + 1; + while (ctx->history[ctx->line_pos] != '\0') + --ctx->line_pos; // Do not store empty lines in the history - if (i == ctx->history_pos - 1) + if (ctx->line_pos == ctx->history_pos - 1) ctx->history_pos -= 1; #if DEBUG_DUMP_HISTORY @@ -433,7 +449,7 @@ const char* rl_readline(struct RLContext* ctx) // Since the current pointer now points to the separator, we need // to return the first character - return &ctx->history[i+1]; + return &ctx->history[ctx->line_pos + 1]; } diff --git a/mware/readline.h b/mware/readline.h index a275ede2..9219bfd7 100644 --- a/mware/readline.h +++ b/mware/readline.h @@ -58,6 +58,7 @@ typedef int (*getc_hook)(void* user_data); typedef void (*putc_hook)(char ch, void* user_data); typedef const char* (*match_hook)(void* user_data, const char* word, int word_len); +typedef void (*clear_hook)(void* user_data); struct RLContext { @@ -70,11 +71,15 @@ struct RLContext match_hook match; void* match_param; + clear_hook clear; + void* clear_param; + const char* prompt; char real_history[HISTORY_SIZE]; char* history; size_t history_pos; + size_t line_pos; }; INLINE void rl_init_ctx(struct RLContext *ctx) @@ -99,6 +104,9 @@ INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param) { ctx->match = match; ctx->match_param = match_param; } +INLINE void rl_sethook_clear(struct RLContext* ctx, clear_hook clear, void* clear_param) +{ ctx->clear = clear; ctx->clear_param = clear_param; } + INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt) { ctx->prompt = prompt; } -- 2.25.1