degesch: refactor /help, fix segfault

Forgot to check if the item has a schema.
This commit is contained in:
Přemysl Eric Janouch 2015-05-03 16:47:31 +02:00
parent 4928f9ed62
commit 373f6333ef
1 changed files with 44 additions and 32 deletions

View File

@ -4120,6 +4120,41 @@ g_command_handlers[] =
"<command>" },
};
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;
}