repl cleanup

master
Daniel Kolesa 2016-10-05 21:33:59 +02:00
parent 7f1c6b03a8
commit da8120f5c8
4 changed files with 4 additions and 25 deletions

View File

@ -30,8 +30,7 @@ $(LIBCS_LIB): $(LIBCS_OBJ)
repl: $(LIBCS_LIB) tools/repl.cc tools/linenoise.cc tools/linenoise.hh repl: $(LIBCS_LIB) tools/repl.cc tools/linenoise.cc tools/linenoise.hh
$(CXX) $(CXXFLAGS) $(LIBCS_CXXFLAGS) $(LDFLAGS) \ $(CXX) $(CXXFLAGS) $(LIBCS_CXXFLAGS) $(LDFLAGS) \
-DCS_REPL_USE_LINENOISE -DCS_REPL_HAS_HINTS -DCS_REPL_HAS_COMPLETE \ -DCS_REPL_USE_LINENOISE tools/linenoise.cc tools/repl.cc -o repl $(LIBCS_LIB)
tools/linenoise.cc tools/repl.cc -o repl $(LIBCS_LIB)
clean: clean:
rm -f $(LIBCS_LIB) $(LIBCS_OBJ) repl rm -f $(LIBCS_LIB) $(LIBCS_OBJ) repl

View File

@ -15,7 +15,6 @@
static CsState *ln_cs = nullptr; static CsState *ln_cs = nullptr;
#ifdef CS_REPL_HAS_COMPLETE
static void ln_complete(char const *buf, linenoiseCompletions *lc) { static void ln_complete(char const *buf, linenoiseCompletions *lc) {
ostd::ConstCharRange cmd = get_complete_cmd(buf); ostd::ConstCharRange cmd = get_complete_cmd(buf);
for (auto id: ln_cs->get_idents()) { for (auto id: ln_cs->get_idents()) {
@ -31,9 +30,7 @@ static void ln_complete(char const *buf, linenoiseCompletions *lc) {
} }
} }
} }
#endif /* CS_REPL_HAS_COMPLETE */
#ifdef CS_REPL_HAS_HINTS
static char *ln_hint(char const *buf, int *color, int *bold) { static char *ln_hint(char const *buf, int *color, int *bold) {
CsCommand *cmd = get_hint_cmd(*ln_cs, buf); CsCommand *cmd = get_hint_cmd(*ln_cs, buf);
if (!cmd) { if (!cmd) {
@ -52,19 +49,14 @@ static char *ln_hint(char const *buf, int *color, int *bold) {
static void ln_hint_free(void *hint) { static void ln_hint_free(void *hint) {
delete[] static_cast<char *>(hint); delete[] static_cast<char *>(hint);
} }
#endif /* CS_REPL_HAS_HINTS */
static void init_lineedit(CsState &cs, ostd::ConstCharRange) { static void init_lineedit(CsState &cs, ostd::ConstCharRange) {
/* sensible default history size */ /* sensible default history size */
linenoiseHistorySetMaxLen(1000); linenoiseHistorySetMaxLen(1000);
ln_cs = &cs; ln_cs = &cs;
#ifdef CS_REPL_HAS_COMPLETE
linenoiseSetCompletionCallback(ln_complete); linenoiseSetCompletionCallback(ln_complete);
#endif
#ifdef CS_REPL_HAS_HINTS
linenoiseSetHintsCallback(ln_hint); linenoiseSetHintsCallback(ln_hint);
linenoiseSetFreeHintsCallback(ln_hint_free); linenoiseSetFreeHintsCallback(ln_hint_free);
#endif
} }
static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) { static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) {

View File

@ -13,7 +13,6 @@
static CsState *rd_cs = nullptr; static CsState *rd_cs = nullptr;
#ifdef CS_REPL_HAS_COMPLETE
static char *ln_complete_list(char const *buf, int state) { static char *ln_complete_list(char const *buf, int state) {
static ostd::ConstCharRange cmd; static ostd::ConstCharRange cmd;
static ostd::PointerRange<CsIdent *> itr; static ostd::PointerRange<CsIdent *> itr;
@ -45,9 +44,7 @@ static char **ln_complete(char const *buf, int, int) {
rl_attempted_completion_over = 1; rl_attempted_completion_over = 1;
return rl_completion_matches(buf, ln_complete_list); return rl_completion_matches(buf, ln_complete_list);
} }
#endif
#ifdef CS_REPL_HAS_HINTS
void ln_hint() { void ln_hint() {
CsCommand *cmd = get_hint_cmd(*rd_cs, rl_line_buffer); CsCommand *cmd = get_hint_cmd(*rd_cs, rl_line_buffer);
if (!cmd) { if (!cmd) {
@ -64,16 +61,11 @@ void ln_hint() {
rl_redisplay(); rl_redisplay();
rl_replace_line(old.data(), 0); rl_replace_line(old.data(), 0);
} }
#endif
static void init_lineedit(CsState &cs, ostd::ConstCharRange) { static void init_lineedit(CsState &cs, ostd::ConstCharRange) {
rd_cs = &cs; rd_cs = &cs;
#ifdef CS_REPL_HAS_COMPLETE
rl_attempted_completion_function = ln_complete; rl_attempted_completion_function = ln_complete;
#endif
#ifdef CS_REPL_HAS_HINTS
rl_redisplay_function = ln_hint; rl_redisplay_function = ln_hint;
#endif
} }
static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) { static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) {

View File

@ -27,8 +27,7 @@ static bool stdin_is_tty() {
/* line editing support */ /* line editing support */
#ifdef CS_REPL_HAS_COMPLETE static inline ostd::ConstCharRange get_complete_cmd(ostd::ConstCharRange buf) {
static ostd::ConstCharRange get_complete_cmd(ostd::ConstCharRange buf) {
ostd::ConstCharRange not_allowed = "\"/;()[] \t\r\n\0"; ostd::ConstCharRange not_allowed = "\"/;()[] \t\r\n\0";
ostd::ConstCharRange found = ostd::find_one_of(buf, not_allowed); ostd::ConstCharRange found = ostd::find_one_of(buf, not_allowed);
while (!found.empty()) { while (!found.empty()) {
@ -38,9 +37,7 @@ static ostd::ConstCharRange get_complete_cmd(ostd::ConstCharRange buf) {
} }
return buf; return buf;
} }
#endif /* CS_REPL_HAS_COMPLETE */
#ifdef CS_REPL_HAS_HINTS
static inline ostd::ConstCharRange get_arg_type(char arg) { static inline ostd::ConstCharRange get_arg_type(char arg) {
switch (arg) { switch (arg) {
case 'i': case 'i':
@ -73,7 +70,7 @@ static inline ostd::ConstCharRange get_arg_type(char arg) {
return "illegal"; return "illegal";
} }
static void fill_cmd_args(ostd::String &writer, ostd::ConstCharRange args) { static inline void fill_cmd_args(ostd::String &writer, ostd::ConstCharRange args) {
char variadic = '\0'; char variadic = '\0';
int nrep = 0; int nrep = 0;
if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) { if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) {
@ -130,7 +127,7 @@ static void fill_cmd_args(ostd::String &writer, ostd::ConstCharRange args) {
} }
} }
static CsCommand *get_hint_cmd(CsState &cs, ostd::ConstCharRange buf) { static inline CsCommand *get_hint_cmd(CsState &cs, ostd::ConstCharRange buf) {
ostd::ConstCharRange nextchars = "([;"; ostd::ConstCharRange nextchars = "([;";
auto lp = ostd::find_one_of(buf, nextchars); auto lp = ostd::find_one_of(buf, nextchars);
if (!lp.empty()) { if (!lp.empty()) {
@ -153,7 +150,6 @@ static CsCommand *get_hint_cmd(CsState &cs, ostd::ConstCharRange buf) {
} }
return nullptr; return nullptr;
} }
#endif /* CS_REPL_HAS_HINTS */
#include "edit_linenoise.hh" #include "edit_linenoise.hh"
#include "edit_readline.hh" #include "edit_readline.hh"