Skip to content

Commit d7b1531

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

File tree

11 files changed

+89
-67
lines changed

11 files changed

+89
-67
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: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ 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_;
19-
bool LineReaderManager::is_first_call_ = true;
20+
bool LineReaderManager::is_first_call_ = true;
2021

21-
char* LineReaderManager::my_readline(const char* prompt, const std::string& history_file) {
22+
char *LineReaderManager::my_readline(const char *prompt, const std::string &history_file)
23+
{
2224
if (is_first_call_) {
2325
reader_.history_load(history_file);
2426
is_first_call_ = false;
2527
}
2628

27-
char* line = reader_.input(prompt);
29+
char *line = (char *)reader_.input(prompt);
2830
if (line == nullptr) {
2931
return nullptr;
3032
}
@@ -40,20 +42,30 @@ char* LineReaderManager::my_readline(const char* prompt, const std::string& hist
4042
if (is_valid_input) {
4143
reader_.history_add(line);
4244
}
43-
45+
4446
return line;
4547
}
4648

47-
bool LineReaderManager::is_exit_command(const char* cmd, const std::string& history_file) {
48-
bool is_exit = 0 == strncasecmp("exit", cmd, 4) ||
49-
0 == strncasecmp("bye", cmd, 3) ||
50-
0 == strncasecmp("\\q", cmd, 2) ||
51-
0 == strncasecmp("interrupted", cmd, 11);
52-
49+
bool LineReaderManager::is_exit_command(const char *cmd, const std::string &history_file)
50+
{
51+
bool is_exit = 0 == strncasecmp("exit", cmd, 4) || 0 == strncasecmp("bye", cmd, 3) ||
52+
0 == strncasecmp("\\q", cmd, 2) || 0 == strncasecmp("interrupted", cmd, 11);
53+
5354
if (is_exit) {
5455
reader_.history_save(history_file);
5556
}
56-
57+
5758
return is_exit;
5859
}
59-
} // namespace common
60+
61+
void LineReaderManager::free_buffer(char *buffer)
62+
{
63+
if (buffer != nullptr) {
64+
#if USE_REPLXX
65+
delete[] buffer; // replxx uses new[]
66+
#else
67+
free(buffer); // linenoise uses malloc
68+
#endif
69+
}
70+
}
71+
} // namespace common

src/common/linereader/line_reader.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,44 @@ See the Mulan PSL v2 for more details. */
1616
#define COMMON_LINE_READER
1717

1818
#if USE_REPLXX
19-
#include "replxx.hxx"
20-
using LineReader = replxx::Replxx;
19+
#include "replxx.hxx"
20+
using LineReader = replxx::Replxx;
2121
#else
22-
#include "common/linereader/linenoise_reader.h"
23-
using LineReader = LinenoiseReader;
22+
#include "common/linereader/linenoise_reader.h"
23+
using LineReader = LinenoiseReader;
2424
#endif
2525

2626
namespace common {
27-
class LineReaderManager {
27+
class LineReaderManager
28+
{
2829
public:
2930
/**
3031
* @brief Read a line from input
3132
* @param prompt The prompt to display
3233
* @param history_file path/to/file
3334
* @return char* to input string or nullptr
3435
*/
35-
static char* my_readline(const char* prompt, const std::string& history_file);
36+
static char *my_readline(const char *prompt, const std::string &history_file);
3637

3738
/**
3839
* @brief Check if the command is an exit command
3940
* @param cmd The input command
4041
* @param history_file path/to/file
4142
* @return True if the command is an exit command
4243
*/
43-
static bool is_exit_command(const char* cmd, const std::string& history_file);
44+
static bool is_exit_command(const char *cmd, const std::string &history_file);
45+
46+
/**
47+
* @brief Free the buffer allocated by my_readline
48+
* @param buffer The buffer allocated by my_readline
49+
* @note dealing with alloc-dealloc-mismatch
50+
*/
51+
static void free_buffer(char *buffer);
4452

4553
private:
4654
static LineReader reader_;
47-
static bool is_first_call_;
55+
static bool is_first_call_;
4856
};
49-
} // namespace common
57+
} // namespace common
5058

51-
#endif // COMMON_LINE_READER
59+
#endif // COMMON_LINE_READER

src/common/linereader/linenoise_reader.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,41 @@ EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
88
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
See the Mulan PSL v2 for more details. */
1010

11-
//
11+
//
1212
// Created by Willaaaaaaa in 2025
1313
//
1414

1515
#include "common/linereader/linenoise_reader.h"
1616
#include "common/linereader/linenoise.h"
1717
#include "common/lang/string.h"
1818

19-
LinenoiseReader::LinenoiseReader() {
19+
LinenoiseReader::LinenoiseReader()
20+
{
2021
linenoiseHistorySetMaxLen(1000);
2122
linenoiseSetMultiLine(0);
2223
}
2324

24-
char* LinenoiseReader::input(const char* prompt) {
25-
return linenoise(prompt);
26-
}
25+
char *LinenoiseReader::input(const char *prompt) { return linenoise(prompt); }
2726

28-
bool LinenoiseReader::history_load(const std::string& history_file) {
27+
bool LinenoiseReader::history_load(const std::string &history_file)
28+
{
2929
history_file_ = history_file;
3030
return linenoiseHistoryLoad(history_file.c_str()) == 0;
3131
}
3232

33-
bool LinenoiseReader::history_save(const std::string& history_file) const {
33+
bool LinenoiseReader::history_save(const std::string &history_file) const
34+
{
3435
return linenoiseHistorySave(history_file.c_str()) == 0;
3536
}
3637

37-
bool LinenoiseReader::history_add(const char* line) {
38+
bool LinenoiseReader::history_add(const char *line)
39+
{
3840
if (line != nullptr && *line != '\0') {
3941
return linenoiseHistoryAdd(line) == 0;
4042
}
4143
return false;
4244
}
4345

44-
void LinenoiseReader::history_set_max_len(int len) {
45-
linenoiseHistorySetMaxLen(len);
46-
}
46+
void LinenoiseReader::history_set_max_len(int len) { linenoiseHistorySetMaxLen(len); }
4747

48-
void LinenoiseReader::clear_screen() {
49-
linenoiseClearScreen();
50-
}
48+
void LinenoiseReader::clear_screen() { linenoiseClearScreen(); }

src/common/linereader/linenoise_reader.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
88
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
See the Mulan PSL v2 for more details. */
1010

11-
//
11+
//
1212
// Created by Willaaaaaaa in 2025
1313
//
1414

@@ -21,45 +21,47 @@ See the Mulan PSL v2 for more details. */
2121
* @brief A wrapper class for linenoise lib
2222
* @details Support history memory...
2323
*/
24-
class LinenoiseReader {
24+
class LinenoiseReader
25+
{
2526
public:
2627
LinenoiseReader();
2728
~LinenoiseReader() = default;
28-
29+
2930
/**
30-
* @brief Read input with C string
31+
* @brief Read input with C++ string
3132
* @param prompt
3233
* @return char* to input string or nullptr
34+
// * @note set prompt to string to be consistent with replxx
3335
*/
34-
char* input(const char* prompt);
35-
36+
char *input(const char *prompt);
37+
3638
/**
3739
* @brief Load history records from the file
3840
* @param history_file path/to/history
3941
* @return Whether load success
4042
*/
41-
bool history_load(const std::string& history_file);
42-
43+
bool history_load(const std::string &history_file);
44+
4345
/**
4446
* @brief Save history records to the file
4547
* @param history_file path/to/history
4648
* @return Whether save success
4749
*/
48-
bool history_save(const std::string& history_file) const;
49-
50+
bool history_save(const std::string &history_file) const;
51+
5052
/**
5153
* @brief Add a single history record to the file
5254
* @param line the C string command to be recorded
5355
* @return Whether add success
5456
*/
55-
bool history_add(const char* line);
56-
57+
bool history_add(const char *line);
58+
5759
/**
5860
* @brief Set the maximum of history length
5961
* @param len length
6062
*/
6163
void history_set_max_len(int len);
62-
64+
6365
/**
6466
* @brief Clean the screen: Ctrl + L
6567
*/
@@ -69,4 +71,4 @@ class LinenoiseReader {
6971
std::string history_file_;
7072
};
7173

72-
#endif // COMMON_LINENOISE_WRAPPER_H
74+
#endif // COMMON_LINENOISE_WRAPPER_H

src/obclient/client.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ const std::string LINE_HISTORY_FILE = "./.obclient.history";
4343
'strncasecmp("exit", cmd, 4)' means that obclient read command string from terminal, truncate it to 4 chars from
4444
the beginning, then compare the result with 'exit', if they match, exit the obclient.
4545
*/
46-
bool is_exit_command(const char *cmd)
47-
{
48-
return LineReaderManager::is_exit_command(cmd, LINE_HISTORY_FILE);
49-
}
46+
bool is_exit_command(const char *cmd) { return LineReaderManager::is_exit_command(cmd, LINE_HISTORY_FILE); }
5047

5148
int init_unix_sock(const char *unix_sock_path)
5249
{
@@ -145,13 +142,13 @@ int main(int argc, char *argv[])
145142

146143
while ((input_command = LineReaderManager::my_readline(prompt_str, LINE_HISTORY_FILE)) != nullptr) {
147144
if (common::is_blank(input_command)) {
148-
free(input_command);
145+
LineReaderManager::free_buffer(input_command);
149146
input_command = nullptr;
150147
continue;
151148
}
152149

153150
if (is_exit_command(input_command)) {
154-
free(input_command);
151+
LineReaderManager::free_buffer(input_command);
155152
input_command = nullptr;
156153
break;
157154
}
@@ -165,7 +162,7 @@ int main(int argc, char *argv[])
165162
fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno));
166163
exit(1);
167164
}
168-
free(input_command);
165+
LineReaderManager::free_buffer(input_command);
169166
input_command = nullptr;
170167

171168
memset(send_buf, 0, sizeof(send_buf));
@@ -197,7 +194,7 @@ int main(int argc, char *argv[])
197194
}
198195

199196
if (input_command != nullptr) {
200-
free(input_command);
197+
LineReaderManager::free_buffer(input_command);
201198
input_command = nullptr;
202199
}
203200
close(sockfd);

src/oblsm/client/cliutil/parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ using common::LineReaderManager;
2222
namespace oceanbase {
2323
std::string my_readline(const std::string &prompt)
2424
{
25-
char* line = LineReaderManager::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
25+
char *line = LineReaderManager::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
2626
if (line == nullptr) {
2727
return "";
2828
}
29-
29+
3030
std::string result = line;
3131
free(line);
3232
return result;

src/oblsm/client/cliutil/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ See the Mulan PSL v2 for more details. */
2828
#define MAX_MEM_BUFFER_SIZE 8192
2929
namespace oceanbase {
3030

31-
inline const string LINE_HISTORY_FILE = "./.oblsm_cli.history";
31+
inline const string LINE_HISTORY_FILE = "./.oblsm_cli.history";
3232

3333
std::string my_readline(const string &prompt);
3434

src/oblsm/client/ob_lsm_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ int main(int, char **)
120120
static time_t previous_history_save_time = 0;
121121

122122
for (; !quit;) {
123-
char* command_input = LineReaderManager::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
123+
char *command_input = LineReaderManager::my_readline(prompt.c_str(), LINE_HISTORY_FILE);
124124

125125
if (command_input == nullptr) {
126126
continue;
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()) {

0 commit comments

Comments
 (0)