Autocompletion and history with ReadLine

This commit is contained in:
Louis 2015-02-10 18:20:48 +01:00
parent 4eb7c132ca
commit b9d240478b
3 changed files with 18 additions and 22 deletions

View file

@ -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

View file

@ -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. */

View file

@ -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; (i<sizeof(_keywords)/sizeof(_keywords[0])) && (_keywords[i].type != cmd_max); i++)
{
if ((_keywords[i].name.size()>0) && (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;