From 373f6333efbeeeee28753adeb78094ef0807efa0 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 3 May 2015 16:47:31 +0200 Subject: degesch: refactor /help, fix segfault Forgot to check if the item has a schema. --- degesch.c | 76 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/degesch.c b/degesch.c index b0a3e04..1d66a9d 100644 --- a/degesch.c +++ b/degesch.c @@ -4120,6 +4120,41 @@ g_command_handlers[] = "" }, }; +static bool +try_handle_command_help_option (struct app_context *ctx, const char *name) +{ + struct config_item_ *item = + config_item_get (ctx->config.root, name, NULL); + if (!item) + return false; + + struct config_schema *schema = item->schema; + if (!schema) + { + buffer_send_error (ctx, ctx->global_buffer, + "%s: %s", "This option has no schema", name); + return true; + } + + buffer_send_status (ctx, ctx->global_buffer, "%s", ""); + buffer_send_status (ctx, ctx->global_buffer, + "Option \"%s\":", name); + buffer_send_status (ctx, ctx->global_buffer, + " Description: %s", schema->comment); + buffer_send_status (ctx, ctx->global_buffer, + " Type: %s", config_item_type_name (schema->type)); + buffer_send_status (ctx, ctx->global_buffer, + " Default: %s", schema->default_ ? schema->default_ : "null"); + + struct str tmp; + str_init (&tmp); + config_item_write (item, false, &tmp); + buffer_send_status (ctx, ctx->global_buffer, + " Current value: %s", tmp.str); + str_free (&tmp); + return true; +} + static bool handle_command_help (struct app_context *ctx, char *arguments) { @@ -4140,43 +4175,20 @@ handle_command_help (struct app_context *ctx, char *arguments) for (size_t i = 0; i < N_ELEMENTS (g_command_handlers); i++) { struct command_handler *handler = &g_command_handlers[i]; - if (!strcasecmp_ascii (command, handler->name)) - { - buffer_send_status (ctx, ctx->global_buffer, "%s", ""); - buffer_send_status (ctx, ctx->global_buffer, "%s: %s", - handler->name, handler->description); - buffer_send_status (ctx, ctx->global_buffer, " Arguments: %s", - handler->usage); - return true; - } - } + if (strcasecmp_ascii (command, handler->name)) + continue; - struct config_item_ *item = - config_item_get (ctx->config.root, command, NULL); - if (item) - { - struct config_schema *schema = item->schema; buffer_send_status (ctx, ctx->global_buffer, "%s", ""); - buffer_send_status (ctx, ctx->global_buffer, - "Option \"%s\":", command); - buffer_send_status (ctx, ctx->global_buffer, - " Description: %s", schema->comment); - buffer_send_status (ctx, ctx->global_buffer, - " Type: %s", config_item_type_name (schema->type)); - buffer_send_status (ctx, ctx->global_buffer, - " Default: %s", schema->default_ ? schema->default_ : "null"); - - struct str tmp; - str_init (&tmp); - config_item_write (item, false, &tmp); - buffer_send_status (ctx, ctx->global_buffer, - " Current value: %s", tmp.str); - str_free (&tmp); + buffer_send_status (ctx, ctx->global_buffer, "%s: %s", + handler->name, handler->description); + buffer_send_status (ctx, ctx->global_buffer, " Arguments: %s", + handler->usage); return true; } - buffer_send_error (ctx, ctx->global_buffer, - "%s: %s", "No such command or option", command); + if (!try_handle_command_help_option (ctx, command)) + buffer_send_error (ctx, ctx->global_buffer, + "%s: %s", "No such command or option", command); return true; } -- cgit v1.2.3-70-g09d2