From 63c8a794793e2118912ee4b10579f60008549f21 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Tue, 13 Oct 2020 21:06:03 +0200 Subject: Factor out init_backend() The main() function is still way too long. --- json-rpc-shell.c | 58 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/json-rpc-shell.c b/json-rpc-shell.c index adcf310..44e2598 100644 --- a/json-rpc-shell.c +++ b/json-rpc-shell.c @@ -3536,7 +3536,7 @@ init_watchers (struct app_context *ctx) static void parse_program_arguments (struct app_context *ctx, int argc, char **argv, - char **origin, char **endpoint) + const char **origin, const char **endpoint) { static const struct opt opts[] = { @@ -3618,27 +3618,9 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv, opt_handler_free (&oh); } -int -main (int argc, char *argv[]) +static void +init_backend (struct app_context *ctx, const char *origin, const char *endpoint) { - g_ctx.config = config_make (); - register_config_modules (&g_ctx); - config_load (&g_ctx.config, config_item_object ()); - - char *origin = NULL; - char *endpoint = NULL; - parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint); - - g_ctx.input = input_new (); - g_ctx.input->user_data = &g_ctx; - g_ctx.input->on_input = process_input; - g_ctx.input->on_run_editor = run_editor; - g_ctx.input->complete_start_word = complete_method_name; - - g_ctx.methods = str_map_make (NULL); - init_colors (&g_ctx); - load_configuration (&g_ctx); - struct http_parser_url url; if (http_parser_parse_url (endpoint, strlen (endpoint), false, &url)) exit_fatal ("invalid endpoint address"); @@ -3649,23 +3631,45 @@ main (int argc, char *argv[]) url.field_data[UF_SCHEMA].off, url.field_data[UF_SCHEMA].len); - // TODO: try to avoid the need to pass application context to backends + // TODO: try to avoid the need to pass the application context to backends if (!strcasecmp_ascii (url_schema, "http") || !strcasecmp_ascii (url_schema, "https")) - g_ctx.backend = backend_curl_new (&g_ctx, endpoint); + ctx->backend = backend_curl_new (ctx, endpoint); else if (!strcasecmp_ascii (url_schema, "ws") || !strcasecmp_ascii (url_schema, "wss")) - g_ctx.backend = backend_ws_new (&g_ctx, endpoint, &url); + ctx->backend = backend_ws_new (ctx, endpoint, &url); else exit_fatal ("unsupported protocol"); free (url_schema); if (origin) { - origin = xstrdup_printf ("Origin: %s", origin); - g_ctx.backend->vtable->add_header (g_ctx.backend, origin); + char *header = xstrdup_printf ("Origin: %s", origin); + g_ctx.backend->vtable->add_header (g_ctx.backend, header); + free (header); } - free (origin); +} + +int +main (int argc, char *argv[]) +{ + g_ctx.config = config_make (); + register_config_modules (&g_ctx); + config_load (&g_ctx.config, config_item_object ()); + + const char *origin = NULL, *endpoint = NULL; + parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint); + + g_ctx.input = input_new (); + g_ctx.input->user_data = &g_ctx; + g_ctx.input->on_input = process_input; + g_ctx.input->on_run_editor = run_editor; + g_ctx.input->complete_start_word = complete_method_name; + + g_ctx.methods = str_map_make (NULL); + init_colors (&g_ctx); + load_configuration (&g_ctx); + init_backend (&g_ctx, origin, endpoint); // We only need to convert to and from the terminal encoding setlocale (LC_CTYPE, ""); -- cgit v1.2.3-70-g09d2