Browse Source

Main+Readline: Do not leak history path std::string_view

master
Riyyi 2 years ago
parent
commit
aba70beeb3
  1. 15
      src/readline.cpp
  2. 4
      src/readline.h
  3. 2
      src/step2_eval.cpp

15
src/readline.cpp

@ -9,6 +9,8 @@
#include <readline/history.h> #include <readline/history.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/tilde.h> #include <readline/tilde.h>
#include <string>
#include <string_view>
#include "ruc/format/color.h" #include "ruc/format/color.h"
@ -18,7 +20,7 @@ namespace blaze {
Readline::Readline(bool pretty_print, std::string_view history_path) Readline::Readline(bool pretty_print, std::string_view history_path)
: m_pretty_print(pretty_print) : m_pretty_print(pretty_print)
, m_history_path(history_path) , m_history_path(tilde_expand(history_path.data()))
{ {
if (!pretty_print) { if (!pretty_print) {
m_prompt = "user> "; m_prompt = "user> ";
@ -28,9 +30,16 @@ Readline::Readline(bool pretty_print, std::string_view history_path)
m_prompt += format(" \033[1m"); m_prompt += format(" \033[1m");
} }
read_history(tilde_expand(history_path.data())); read_history(m_history_path);
} }
Readline::~Readline()
{
std::free(m_history_path);
}
// -----------------------------------------
bool Readline::get(std::string& output) bool Readline::get(std::string& output)
{ {
char* line = readline(m_prompt.c_str()); char* line = readline(m_prompt.c_str());
@ -40,7 +49,7 @@ bool Readline::get(std::string& output)
// Add input to in-memory history // Add input to in-memory history
add_history(line); add_history(line);
append_history(1, m_history_path.data()); append_history(1, m_history_path);
output = line; output = line;
std::free(line); std::free(line);

4
src/readline.h

@ -16,14 +16,14 @@ namespace blaze {
class Readline { class Readline {
public: public:
Readline(bool pretty_print, std::string_view history_path); Readline(bool pretty_print, std::string_view history_path);
virtual ~Readline() {} virtual ~Readline();
bool get(std::string& output); bool get(std::string& output);
private: private:
bool m_pretty_print { false }; bool m_pretty_print { false };
std::string m_prompt; std::string m_prompt;
std::string_view m_history_path; char* m_history_path;
}; };
} // namespace blaze } // namespace blaze

2
src/step2_eval.cpp

@ -104,5 +104,3 @@ auto main(int argc, char* argv[]) -> int
return 0; return 0;
} }
#endif #endif
// - Add AST node printing support to ruc::format

Loading…
Cancel
Save