Skip to content

Commit 6457eb0

Browse files
committed
[WIP] try to support both replxx and linenoise
1 parent ea0f7cf commit 6457eb0

File tree

8 files changed

+35
-11
lines changed

8 files changed

+35
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ IF (EXISTS /usr/local/lib64)
158158
LINK_DIRECTORIES (/usr/local/lib64)
159159
ENDIF ()
160160

161-
INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/deps ${PROJECT_SOURCE_DIR}/src /usr/local/include)
161+
INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/deps ${PROJECT_SOURCE_DIR}/deps/3rd/usr/local/include ${PROJECT_SOURCE_DIR}/src /usr/local/include)
162162

163163
IF(WITH_UNIT_TESTS)
164164
IF (ENABLE_COVERAGE)

src/common/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ IF(USE_MUSL_LIBC)
1919
TARGET_LINK_LIBRARIES(common execinfo)
2020
ENDIF(USE_MUSL_LIBC)
2121

22+
IF(USE_REPLXX)
23+
TARGET_LINK_LIBRARIES(common replxx::replxx)
24+
MESSAGE(STATUS "common uses replxx")
25+
ENDIF()
26+
2227
# 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下
2328
SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1)
2429

src/common/linereader/line_reader.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the Mulan PSL v2 for more details. */
1313
//
1414

1515
#include "common/linereader/line_reader.h"
16+
#include "common/lang/string.h"
1617

1718
namespace common {
1819
LineReader LineReaderManager::reader_;
@@ -24,7 +25,7 @@ char* LineReaderManager::my_readline(const char* prompt, const std::string& hist
2425
is_first_call_ = false;
2526
}
2627

27-
char* line = reader_.input(prompt);
28+
char* line = (char*)reader_.input(prompt);
2829
if (line == nullptr) {
2930
return nullptr;
3031
}
@@ -56,4 +57,14 @@ bool LineReaderManager::is_exit_command(const char* cmd, const std::string& hist
5657

5758
return is_exit;
5859
}
60+
61+
void LineReaderManager::free_buffer(char* buffer) {
62+
if (buffer != nullptr) {
63+
#if USE_REPLXX
64+
delete[] buffer; // replxx uses new[]
65+
#else
66+
free(buffer); // linenoise uses malloc
67+
#endif
68+
}
69+
}
5970
} // namespace common

src/common/linereader/line_reader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class LineReaderManager {
4242
*/
4343
static bool is_exit_command(const char* cmd, const std::string& history_file);
4444

45+
/**
46+
* @brief Free the buffer allocated by my_readline
47+
* @param buffer The buffer allocated by my_readline
48+
* @note dealing with alloc-dealloc-mismatch
49+
*/
50+
static void free_buffer(char* buffer);
51+
4552
private:
4653
static LineReader reader_;
4754
static bool is_first_call_;

src/common/linereader/linenoise_reader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ class LinenoiseReader {
2727
~LinenoiseReader() = default;
2828

2929
/**
30-
* @brief Read input with C string
30+
* @brief Read input with C++ string
3131
* @param prompt
3232
* @return char* to input string or nullptr
33+
// * @note set prompt to string to be consistent with replxx
3334
*/
3435
char* input(const char* prompt);
3536

src/obclient/client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ int main(int argc, char *argv[])
145145

146146
while ((input_command = LineReaderManager::my_readline(prompt_str, LINE_HISTORY_FILE)) != nullptr) {
147147
if (common::is_blank(input_command)) {
148-
free(input_command);
148+
LineReaderManager::free_buffer(input_command);
149149
input_command = nullptr;
150150
continue;
151151
}
152152

153153
if (is_exit_command(input_command)) {
154-
free(input_command);
154+
LineReaderManager::free_buffer(input_command);
155155
input_command = nullptr;
156156
break;
157157
}
@@ -165,7 +165,7 @@ int main(int argc, char *argv[])
165165
fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno));
166166
exit(1);
167167
}
168-
free(input_command);
168+
LineReaderManager::free_buffer(input_command);
169169
input_command = nullptr;
170170

171171
memset(send_buf, 0, sizeof(send_buf));
@@ -197,7 +197,7 @@ int main(int argc, char *argv[])
197197
}
198198

199199
if (input_command != nullptr) {
200-
free(input_command);
200+
LineReaderManager::free_buffer(input_command);
201201
input_command = nullptr;
202202
}
203203
close(sockfd);

src/oblsm/client/ob_lsm_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int main(int, char **)
127127
}
128128

129129
std::string command = command_input;
130-
free(command_input);
130+
LineReaderManager::free_buffer(command_input);
131131
command_input = nullptr;
132132

133133
if (!command.empty()) {

src/observer/net/cli_communicator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ RC CliCommunicator::read_event(SessionEvent *&event)
6666
}
6767

6868
if (common::is_blank(command)) {
69-
free(command);
69+
LineReaderManager::free_buffer(command);
7070
return RC::SUCCESS;
7171
}
7272

7373
if (LineReaderManager::is_exit_command(command, LINE_HISTORY_FILE)) {
74-
free(command);
74+
LineReaderManager::free_buffer(command);
7575
exit_ = true;
7676
return RC::SUCCESS;
7777
}
7878

7979
event = new SessionEvent(this);
8080
event->set_query(string(command));
81-
free(command);
81+
LineReaderManager::free_buffer(command);
8282
return RC::SUCCESS;
8383
}
8484

0 commit comments

Comments
 (0)