From b9d240478b3cb11c07af26b0d8a9a5c70ca49c36 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 10 Feb 2015 18:20:48 +0100 Subject: [PATCH] Autocompletion and history with ReadLine --- Makefile.am | 2 +- src/parse.h | 31 +++++++++++++------------------ src/rpn.cpp | 7 ++++--- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Makefile.am b/Makefile.am index 51250dc..716d150 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,6 +16,6 @@ rpn_SOURCES = src/rpn.cpp \ src/rpn-trig.h \ src/stack.h -rpn_LDFLAGS = -lreadline +rpn_LDFLAGS = -Wl,--no-as-needed -lreadline dist_noinst_SCRIPTS = autogen.sh diff --git a/src/parse.h b/src/parse.h index cdb65f6..70025ac 100644 --- a/src/parse.h +++ b/src/parse.h @@ -78,7 +78,7 @@ static bool get_keyword(const string& entry, object*& obj, unsigned int& obj_siz bool ret = false; // could be a command - if (get_fn(entry, fn, type) == ret_ok) + if (get_fn(entry.c_str(), fn, type) == ret_ok) { if (type == cmd_keyword) { @@ -299,14 +299,10 @@ static ret_value entry(program& prog) static char** entry_completion(const char* text, int start, int end) { - char** matches; - - matches = (char**)NULL; + char** matches = NULL; if (start == 0) matches = rl_completion_matches((char*)text, &entry_completion_generator); - else - rl_bind_key('\t',rl_abort); return matches; } @@ -314,9 +310,8 @@ static char** entry_completion(const char* text, int start, int end) static char* entry_completion_generator(const char* text, int state) { static int list_index, len, too_far; - char* name; - if (!state) + if (state == 0) { list_index = 0; too_far = 0; @@ -324,20 +319,20 @@ static char* entry_completion_generator(const char* text, int state) } while(too_far == 0) - { - if (_keywords[list_index].fn == NULL) + { + list_index++; + if (_keywords[list_index].fn != NULL) { + // compare list entry with text, return if match + if (strncmp(_keywords[list_index].name, text, len) == 0) + return entry_completion_dupstr(_keywords[list_index].name); + } + else + { + // fn=NULL and size=0 = last entry in list -> go out if (_keywords[list_index].comment.size() == 0) too_far = 1; - break; } - - list_index++; - - // mpo ! ne pas stocker c_str - name = (char*)_keywords[list_index].name.c_str(); - if (strncmp(name, text, len) == 0) - return entry_completion_dupstr(name); } /* If no names matched, then return NULL. */ diff --git a/src/rpn.cpp b/src/rpn.cpp index bdc1ac5..2a7a566 100644 --- a/src/rpn.cpp +++ b/src/rpn.cpp @@ -520,17 +520,18 @@ public: struct keyword_t { cmd_type_t type; - string name; + char name[24]; program_fn_t fn; string comment; }; static keyword_t _keywords[g_max_commands]; - static ret_value get_fn(const string& fn_name, program_fn_t& fn, cmd_type_t& type) + static ret_value get_fn(const char* fn_name, program_fn_t& fn, cmd_type_t& type) { for(unsigned int i=0; (i0) && (fn_name == _keywords[i].name)) + if ((strnlen(_keywords[i].name, sizeof(_keywords[i].name))>0) + && (strncmp(fn_name, _keywords[i].name, sizeof(_keywords[i].name)) == 0)) { fn = _keywords[i].fn; type = _keywords[i].type;