aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-05-03 16:47:31 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-05-03 16:47:31 +0200
commit373f6333efbeeeee28753adeb78094ef0807efa0 (patch)
treef4e38d078fe2d3bb9d35606c44c028a264df341a
parent4928f9ed62b62c3704bce093369ee476268b435f (diff)
downloadxK-373f6333efbeeeee28753adeb78094ef0807efa0.tar.gz
xK-373f6333efbeeeee28753adeb78094ef0807efa0.tar.xz
xK-373f6333efbeeeee28753adeb78094ef0807efa0.zip
degesch: refactor /help, fix segfault
Forgot to check if the item has a schema.
-rw-r--r--degesch.c76
1 files changed, 44 insertions, 32 deletions
diff --git a/degesch.c b/degesch.c
index b0a3e04..1d66a9d 100644
--- a/degesch.c
+++ b/degesch.c
@@ -4121,6 +4121,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)
{
if (!*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;
}