From 71f1700b034fd6d19c8c01f59198670f557ad990 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Sat, 27 May 2017 22:19:17 +0200 Subject: [PATCH] #84: debugged readline artefact, refactored constants --- src/constant.h | 6 ++++++ src/main.cpp | 4 ++-- src/parse.hpp | 28 ++++++++++++++-------------- src/program.hpp | 9 ++++----- src/version.h | 5 ----- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/constant.h b/src/constant.h index 9082a92..e0478d6 100644 --- a/src/constant.h +++ b/src/constant.h @@ -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" diff --git a/src/main.cpp b/src/main.cpp index 00aae22..78ed2d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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); } } diff --git a/src/parse.hpp b/src/parse.hpp index af13e23..c2573ae 100644 --- a/src/parse.hpp +++ b/src/parse.hpp @@ -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; } diff --git a/src/program.hpp b/src/program.hpp index 5f4257c..0927c52 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -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; }; diff --git a/src/version.h b/src/version.h index 3decced..9908968 100644 --- a/src/version.h +++ b/src/version.h @@ -3,9 +3,6 @@ static const char version[] = "2.0 RC1"; static const char uname[] = "rpn v2.0 RC1, (c) 2013 , 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 "> ";