2#include <fc/thread/thread.hpp>
11# include <readline/readline.h>
12# include <readline/history.h>
16# if RL_VERSION_MAJOR < 6
18# pragma message("You have an old version of readline installed that might not support some of the features we need")
19# pragma message("Readline support will not be compiled in")
21# warning "You have an old version of readline installed that might not support some of the features we need"
22# warning "Readline support will not be compiled in"
31namespace fc {
namespace rpc {
33static std::vector<std::string>& cli_commands()
35 static std::vector<std::string>* cmds =
new std::vector<std::string>();
41 if( _run_complete.valid() )
65 _run_complete = fc::async( [&](){ run(); } );
70 _run_complete.cancel();
81 _result_formatters[method] = formatter;
91 while( !_run_complete.canceled() )
98 getline( _prompt.c_str(), line );
100 catch (
const fc::eof_exception& e )
104 std::cout << line <<
"\n";
107 if( args.size() == 0 )
110 const string& method = args[0].get_string();
113 auto itr = _result_formatters.find( method );
114 if( itr == _result_formatters.end() )
119 std::cout << itr->second( result, args ) <<
"\n";
121 catch (
const std::bad_alloc& )
125 catch (
const boost::interprocess::bad_alloc& )
133 catch (
const std::exception& e )
135 std::cout << e.what() <<
"\n";
144 r = (
char*) malloc ((strlen (
s) + 1));
151 static int list_index,
len;
159 auto& cmd = cli_commands();
161 while( list_index < cmd.size() )
163 name = cmd[list_index].c_str();
166 if (strncmp (
name, text,
len) == 0)
171 return ((
char *)NULL);
175static char** cli_completion(
const char * text ,
int start,
int end)
178 matches = (
char **)NULL;
182 matches = rl_completion_matches ((
char*)text, &
my_generator);
184 rl_bind_key(
'\t',rl_abort);
197 if( isatty( fileno( stdin ) ) )
204 if( _isatty( _fileno( stdin ) ) )
207 rl_attempted_completion_function = cli_completion;
209 static fc::thread getline_thread(
"getline");
210 getline_thread.async( [&](){
211 char* line_read =
nullptr;
213 line_read = readline(prompt.c_str());
214 if( line_read ==
nullptr )
216 rl_bind_key(
'\t', rl_complete );
218 add_history(line_read);
228 fc::getline( fc::cin, line );
std::vector< std::string > get_method_names(api_id_type local_api_id=0) const
variant receive_call(api_id_type api_id, const string &method_name, const variants &args=variants()) const
Used to generate a useful error report when an exception is thrown.
std::string to_detail_string(log_level ll=log_level::all) const
static variants variants_from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
static string to_pretty_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
virtual variant send_callback(uint64_t callback_id, variants args=variants())
virtual void getline(const fc::string &prompt, fc::string &line)
virtual variant send_call(api_id_type api_id, string method_name, variants args=variants())
void format_result(const string &method, std::function< string(variant, const variants &)> formatter)
virtual void send_notice(uint64_t callback_id, variants args=variants())
void set_prompt(const string &prompt)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
char * my_generator(const char *text, int state)
char * dupstr(const char *s)
std::vector< fc::variant > variants
unsigned __int64 uint64_t