#84: debugged readline artefact, refactored constants

This commit is contained in:
Louis Rubet 2017-05-27 22:19:17 +02:00
parent 977bee3a7a
commit 71f1700b03
5 changed files with 26 additions and 26 deletions

View file

@ -25,6 +25,12 @@
// constants
//
// commands and entry related constants
#define MAX_COMMAND_LENGTH 24
#define AUTOCOMPLETE_KEY '\t'
#define SHOW_STACK_SEPARATOR "> "
#define PROMPT "rpn> "
// show formats
#define MPFR_FORMAT_BEG "%."
#define MPFR_FORMAT_STD "Rg"

View file

@ -27,7 +27,7 @@ int main(int argc, char* argv[])
if (prog.run(s_global_stack, s_global_heap) == ret_good_bye)
break;
else
program::show_stack(s_global_stack);
program::show_stack(s_global_stack, false);
}
}
}
@ -53,7 +53,7 @@ int main(int argc, char* argv[])
// run it
ret = prog.run(s_global_stack, s_global_heap);
program::show_stack(s_global_stack, separator);
program::show_stack(s_global_stack);
}
}

View file

@ -31,28 +31,28 @@ static ret_value parse(const char* entry, program& prog)
// interactive entry and decoding
static ret_value entry(program& prog)
{
char* buf;
char* entry;
ret_value ret;
// declare completion fn (bound to '\t' by default)
rl_completion_entry_function = entry_completion_generator;
// get user entry
buf = readline(prompt);
if (buf != NULL)
entry = readline(PROMPT);
if (entry != NULL)
{
// parse it
ret = parse(buf, prog);
ret = parse(entry, prog);
// keep history
if (buf[0]!=0)
add_history(buf);
add_history(entry);
}
else
ret = ret_internal;
//TODO
free(buf);
free(entry);
return ret;
}
private:
@ -480,13 +480,13 @@ static char* entry_completion_generator(const char* text, int state)
/* If no names matched, then return NULL. */
return (char*)NULL;
}
static char* entry_completion_dupstr(char* s)
static char* entry_completion_dupstr(const char* src)
{
char* r = (char*)malloc((strlen(s)+1));
if (r != NULL)
strcpy(r, s);
return r;
int len = strlen(src);
char* dst = (char*)malloc(len+1);
if (dst != NULL)
strcpy(dst, src);
return dst;
}

View file

@ -380,7 +380,7 @@ public:
ret_value get_err(void) { return _err; }
static void show_stack(stack& st, const string& separator = g_show_stack_separator)
static void show_stack(stack& st, bool show_separator = true)
{
if (st.size() == 1)
{
@ -389,11 +389,10 @@ public:
}
else
{
bool show_sep = (! separator.empty());
for (int i = st.size()-1; i>=0; i--)
{
if (show_sep)
printf("%d%s", i+1, separator.c_str());
if (show_separator)
printf("%d%s", i+1, SHOW_STACK_SEPARATOR);
((object*)st[i])->show();
printf("\n");
}
@ -457,7 +456,7 @@ private:
struct keyword_t
{
cmd_type_t type;
char name[24];
char name[MAX_COMMAND_LENGTH];
program_fn_t fn;
string comment;
};

View file

@ -3,9 +3,6 @@ static const char version[] = "2.0 RC1";
static const char uname[] =
"rpn v2.0 RC1, (c) 2013 <louis@rubet.fr>, GNU LGPL v3";
static const char g_cursor[] = "> ";
static const string g_show_stack_separator = "> ";
// description
static const char description[] =
ATTR_BOLD "R" ATTR_OFF "everse "
@ -19,5 +16,3 @@ static const char description[] =
static const char syntax[] =
ATTR_BOLD "Syntax" ATTR_OFF ": rpn [command]\n"
"with optional command = list of commands";
static const char prompt[] = ATTR_BOLD "rpn" ATTR_OFF "> ";