#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 // constants
// //
// commands and entry related constants
#define MAX_COMMAND_LENGTH 24
#define AUTOCOMPLETE_KEY '\t'
#define SHOW_STACK_SEPARATOR "> "
#define PROMPT "rpn> "
// show formats // show formats
#define MPFR_FORMAT_BEG "%." #define MPFR_FORMAT_BEG "%."
#define MPFR_FORMAT_STD "Rg" #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) if (prog.run(s_global_stack, s_global_heap) == ret_good_bye)
break; break;
else 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 // run it
ret = prog.run(s_global_stack, s_global_heap); 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 // interactive entry and decoding
static ret_value entry(program& prog) static ret_value entry(program& prog)
{ {
char* buf; char* entry;
ret_value ret; ret_value ret;
// declare completion fn (bound to '\t' by default) // declare completion fn (bound to '\t' by default)
rl_completion_entry_function = entry_completion_generator; rl_completion_entry_function = entry_completion_generator;
// get user entry // get user entry
buf = readline(prompt); entry = readline(PROMPT);
if (buf != NULL) if (entry != NULL)
{ {
// parse it // parse it
ret = parse(buf, prog); ret = parse(entry, prog);
// keep history // keep history
if (buf[0]!=0) add_history(entry);
add_history(buf);
} }
else else
ret = ret_internal; ret = ret_internal;
//TODO free(entry);
free(buf);
return ret;
} }
private: private:
@ -480,13 +480,13 @@ static char* entry_completion_generator(const char* text, int state)
/* If no names matched, then return NULL. */ /* If no names matched, then return NULL. */
return (char*)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)); int len = strlen(src);
if (r != NULL) char* dst = (char*)malloc(len+1);
strcpy(r, s); if (dst != NULL)
return r; strcpy(dst, src);
return dst;
} }

View file

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

View file

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