From fed2892ee1963455199899ffe4e7769f764a67d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sat, 10 Oct 2020 00:31:10 +0200 Subject: Readline: add trivial OpenRPC support So far hidden under a switch and only for this frontend. --- json-rpc-shell.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 6 deletions(-) diff --git a/json-rpc-shell.c b/json-rpc-shell.c index 0c0bde7..4fd50a4 100644 --- a/json-rpc-shell.c +++ b/json-rpc-shell.c @@ -268,6 +268,8 @@ input_rl_on_startup (void) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +static char **app_readline_completion (const char *text, int start, int end); + static void input_rl_start (struct input *input, const char *program_name) { @@ -282,6 +284,8 @@ input_rl_start (struct input *input, const char *program_name) rl_catch_sigwinch = false; rl_change_environment = false; + rl_attempted_completion_function = app_readline_completion; + hard_assert (self->prompt != NULL); rl_callback_handler_install (self->prompt, input_rl_on_input); @@ -663,6 +667,8 @@ input_el_start (struct input *input, const char *program_name) "run-editor", "Run editor to edit line", input_el_on_run_editor); el_set (self->editline, EL_BIND, "M-e", "run-editor", NULL); + // TODO: implement method name completion for editline (see degesch) + // Source the user's defaults file el_source (self->editline, NULL); @@ -940,12 +946,14 @@ static struct app_context struct backend *backend; ///< Our current backend char *editor_filename; ///< File for input line editor + struct str_map methods; ///< Methods detected via OpenRPC struct config config; ///< Program configuration enum color_mode color_mode; ///< Colour output mode bool compact; ///< Whether to not pretty print bool verbose; ///< Print requests bool trust_all; ///< Don't verify peer certificates + bool openrpc; ///< OpenRPC method name completion bool null_as_id; ///< JSON null is used as an ID int64_t next_id; ///< Next autogenerated ID @@ -2891,13 +2899,18 @@ maybe_print_verbose (struct app_context *ctx, intptr_t attribute, char *term = iconv_xstrdup (ctx->term_from_utf8, utf8, len, NULL); if (!term) - print_error ("%s: %s", "verbose", "character conversion failed"); - else { - print_attributed (ctx, stdout, attribute, "%s", term); - fputs ("\n", stdout); - free (term); + print_error ("%s: %s", "verbose", "character conversion failed"); + return; } + + ctx->input->vtable->hide (ctx->input); + + print_attributed (ctx, stdout, attribute, "%s", term); + fputs ("\n", stdout); + free (term); + + ctx->input->vtable->show (ctx->input); } static struct error * @@ -3081,7 +3094,90 @@ fail: free (input); } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// --- OpenRPC information extraction ------------------------------------------ + +static void +init_openrpc (struct app_context *ctx) +{ + if (!ctx->openrpc) + return; + + json_t *id = json_integer (ctx->next_id++); + struct str buf = str_make (); + struct error *e = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf); + json_decref (id); + + // Just optimistically punch through, I don't have time for this shit + json_error_t error; + json_t *response = NULL, *result = NULL, *value = NULL; + if (!e && !(response = json_loadb (buf.str, buf.len, 0, &error))) + error_set (&e, "parse failure: %s", error.text); + else if (!(result = json_object_get (response, "result")) + || !(result = json_object_get (result, "methods"))) + error_set (&e, "unsupported"); + else + { + const char *name = NULL; + for (size_t i = 0; (value = json_array_get (result, i)); i++) + if ((value = json_object_get (value, "name")) + && (name = json_string_value (value))) + str_map_set (&ctx->methods, name, (void *) 1); + } + json_decref (response); + if (e) + { + print_error ("OpenRPC: %s", e->message); + error_free (e); + } + str_free (&buf); +} + +// --- GNU Readline user actions ----------------------------------------------- + +#ifdef HAVE_READLINE + +static char * +app_readline_complete (const char *text, int state) +{ + static struct str_map_iter iter; + if (!state) + iter = str_map_iter_make (&g_ctx.methods); + + char *input; + size_t len; + if (!(input = iconv_xstrdup (g_ctx.term_to_utf8, (char *) text, -1, &len))) + { + print_error ("character conversion failed for `%s'", "user input"); + return NULL; + } + + char *match = NULL; + while (str_map_iter_next (&iter) + && (strncasecmp_ascii (input, iter.link->key, len - 1 /* XXX */) + || !(match = iconv_xstrdup (g_ctx.term_from_utf8, + iter.link->key, iter.link->key_length + 1, NULL)))) + ; + free (input); + return match; +} + +static char ** +app_readline_completion (const char *text, int start, int end) +{ + (void) end; + + // Only customize matches for the first token, which is the method name + if (start) + return NULL; + + // Don't iterate over filenames and stuff in this case + rl_attempted_completion_over = true; + return rl_completion_matches (text, app_readline_complete); +} + +#endif // HAVE_READLINE + +// --- Main program ------------------------------------------------------------ // The ability to use an external editor on the input line has been shamelessly // copypasted from degesch with minor changes only. @@ -3366,6 +3462,8 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv, "colorize output: never, always, or auto" }, { 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" }, { 'o', "origin", "O", 0, "set the HTTP Origin header" }, + // So far you have to explicitly enable this rather than disable + { 'O', "openrpc", NULL, 0, "method name completion using OpenRPC" }, { 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" }, { 'v', "verbose", NULL, 0, "print raw requests and responses" }, { 'w', "write-default-cfg", "FILENAME", @@ -3395,6 +3493,7 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv, exit (EXIT_SUCCESS); case 'o': *origin = optarg; break; + case 'O': ctx->openrpc = true; break; case 'n': ctx->null_as_id = true; break; case 'c': ctx->compact = true; break; case 't': ctx->trust_all = true; break; @@ -3452,6 +3551,7 @@ main (int argc, char *argv[]) g_ctx.input->on_input = process_input; g_ctx.input->on_run_editor = run_editor; + g_ctx.methods = str_map_make (NULL); init_colors (&g_ctx); load_configuration (&g_ctx); @@ -3531,6 +3631,7 @@ main (int argc, char *argv[]) g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME); ev_set_userdata (EV_DEFAULT_ &g_ctx); + init_openrpc (&g_ctx); ev_run (EV_DEFAULT_ 0); // User has terminated the program, let's save the history and clean up @@ -3553,6 +3654,7 @@ main (int argc, char *argv[]) iconv_close (g_ctx.term_from_utf8); iconv_close (g_ctx.term_to_utf8); + str_map_free (&g_ctx.methods); config_free (&g_ctx.config); free_terminal (); ev_loop_destroy (EV_DEFAULT); -- cgit v1.2.3