From 36f2e7ab9fe1eb1a0171d89436592e17c4af7f48 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 23 Oct 2018 15:47:46 +0200
Subject: [PATCH] Improve prompt handling

---
 alib2cli/src/lexer/Lexer.h |  4 ++++
 aql2/src/prompt/Prompt.cpp | 19 ++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/alib2cli/src/lexer/Lexer.h b/alib2cli/src/lexer/Lexer.h
index 9fad02d97f..cf57b93c42 100644
--- a/alib2cli/src/lexer/Lexer.h
+++ b/alib2cli/src/lexer/Lexer.h
@@ -121,6 +121,10 @@ public:
 	}
 
 	Token nextToken ( );
+
+	const std::string & getLine ( ) const {
+		return m_line;
+	}
 };
 
 } /* cli */
diff --git a/aql2/src/prompt/Prompt.cpp b/aql2/src/prompt/Prompt.cpp
index 763eb583a2..31bcdb496f 100644
--- a/aql2/src/prompt/Prompt.cpp
+++ b/aql2/src/prompt/Prompt.cpp
@@ -49,12 +49,11 @@ cli::Command::Result Prompt::run ( ) {
 		char * line = readline ( m_prefix.c_str ( ) );
 
 		if ( ! line )
-			continue;
+			break;
 
 		char * s = stripwhite ( line );
 
 		if ( * s ) {
-			add_history ( s );
 			state = execute_line ( std::string ( s ) );
 		}
 
@@ -64,9 +63,23 @@ cli::Command::Result Prompt::run ( ) {
 	return state;
 }
 
+class HistoryRegister {
+	cli::Lexer & m_lexer;
+
+public:
+	HistoryRegister ( cli::Lexer & lexer ) : m_lexer ( lexer ) {
+	}
+
+	~HistoryRegister ( ) {
+		add_history ( m_lexer.getLine ( ).c_str ( ) );
+	}
+};
+
 cli::Command::Result Prompt::execute_line ( std::string line ) {
 	try {
-		cli::Parser parser = cli::Parser ( cli::Lexer ( line ) );
+		cli::Lexer lexer ( line );
+		HistoryRegister historyRegister ( lexer );
+		cli::Parser parser = cli::Parser ( lexer );
 		return parser.parse ( )->run ( m_environment );
 	} catch ( const exception::CommonException & exception ) {
 		factory::XmlDataFactory::toStdout ( exception );
-- 
GitLab