From 317b43e488f16a32630a30799d7a0ea82f8ac5eb Mon Sep 17 00:00:00 2001 From: Claus Fischer Date: Wed, 29 Nov 2017 14:42:39 +0100 Subject: [PATCH] rl_unintialize(): New function, free all memory after use Free all allocated internal memory and reset variables so rl_initialize() or readline() may be called again later. Signed-off-by: Claus Fischer Signed-off-by: Joachim Nilsson --- include/editline.h | 1 + src/editline.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/editline.h b/include/editline.h index cf5f56f..257a346 100644 --- a/include/editline.h +++ b/include/editline.h @@ -82,6 +82,7 @@ extern int el_hist_size; /* size of history scrollback buffer, default: extern void rl_initialize(void); extern void rl_reset_terminal(const char *terminal_name); +extern void rl_uninitialize(void); void rl_save_prompt(void); void rl_restore_prompt(void); diff --git a/src/editline.c b/src/editline.c index f1850a0..9b549db 100644 --- a/src/editline.c +++ b/src/editline.c @@ -1145,6 +1145,30 @@ void rl_initialize(void) if (el_outfd < 0) el_outfd = EL_STDOUT; } +void rl_uninitialize(void) +{ + int i; + + /* Uninitialize the history */ + if (H.Lines) { + for (i = 0; i < el_hist_size; i++) { + if (H.Lines[i]) + free(H.Lines[i]); + H.Lines[i] = 0; + } + free(H.Lines); + H.Lines = 0; + } + H.Size = 0; + H.Pos = 0; + + /* Uninitialize the line buffer */ + if (rl_line_buffer) + free(rl_line_buffer); + rl_line_buffer = NULL; + Length = 0; +} + static const char *rl_saved_prompt = NULL; void rl_save_prompt(void) {