From 236f6315e0b30c7f2126f19aa37fc2e5c39e9fb1 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 28 Dec 2015 17:09:34 +0000 Subject: [PATCH] Add simple routine to get configuration help --- src/lua/lua_util.c | 2 +- src/rspamadm/CMakeLists.txt | 1 + src/rspamadm/commands.c | 2 + src/rspamadm/confighelp.c | 152 ++++++++++++++++++++++++++++++++++++ src/rspamadm/control.c | 6 +- 5 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 src/rspamadm/confighelp.c diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 7f89234c6..3cae28211 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -225,7 +225,7 @@ lua_util_config_from_ucl (lua_State *L) cfg->rcl_obj = obj; cfg->cache = rspamd_symbols_cache_new (cfg); - top = rspamd_rcl_config_init (); + top = rspamd_rcl_config_init (cfg); if (!rspamd_rcl_parse (top, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) { msg_err_config ("rcl parse error: %s", err->message); diff --git a/src/rspamadm/CMakeLists.txt b/src/rspamadm/CMakeLists.txt index d57325f0e..258ed454b 100644 --- a/src/rspamadm/CMakeLists.txt +++ b/src/rspamadm/CMakeLists.txt @@ -6,6 +6,7 @@ SET(RSPAMADMSRC rspamadm.c fuzzy_merge.c configdump.c control.c + confighelp.c ${CMAKE_BINARY_DIR}/src/workers.c ${CMAKE_BINARY_DIR}/src/modules.c ${CMAKE_SOURCE_DIR}/src/controller.c diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c index b2e2c87f5..589e15440 100644 --- a/src/rspamadm/commands.c +++ b/src/rspamadm/commands.c @@ -29,6 +29,7 @@ extern struct rspamadm_command configtest_command; extern struct rspamadm_command fuzzy_merge_command; extern struct rspamadm_command configdump_command; extern struct rspamadm_command control_command; +extern struct rspamadm_command confighelp_command; const struct rspamadm_command *commands[] = { &help_command, @@ -38,6 +39,7 @@ const struct rspamadm_command *commands[] = { &fuzzy_merge_command, &configdump_command, &control_command, + &confighelp_command, NULL }; diff --git a/src/rspamadm/confighelp.c b/src/rspamadm/confighelp.c new file mode 100644 index 000000000..d4ead3ef0 --- /dev/null +++ b/src/rspamadm/confighelp.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2015, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "rspamadm.h" +#include "cfg_file.h" +#include "cfg_rcl.h" +#include "rspamd.h" +#include "lua/lua_common.h" + +static gchar *config = NULL; +static gboolean json = FALSE; +static gboolean compact = FALSE; +extern struct rspamd_main *rspamd_main; +/* Defined in modules.c */ +extern module_t *modules[]; +extern worker_t *workers[]; + +static void rspamadm_confighelp (gint argc, gchar **argv); + +static const char *rspamadm_confighelp_help (gboolean full_help); + +struct rspamadm_command confighelp_command = { + .name = "confighelp", + .flags = 0, + .help = rspamadm_confighelp_help, + .run = rspamadm_confighelp +}; + +static GOptionEntry entries[] = { + {"json", 'j', 0, G_OPTION_ARG_NONE, &json, + "Output json", NULL}, + {"compact", 'c', 0, G_OPTION_ARG_NONE, &compact, + "Output compacted", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} +}; + +static const char * +rspamadm_confighelp_help (gboolean full_help) +{ + const char *help_str; + + if (full_help) { + help_str = "Shows help for the specified configuration options\n\n" + "Usage: rspamadm confighelp [option[, option...]]\n" + "Where options are:\n\n" + "--help: shows available options and commands"; + } + else { + help_str = "Shows help for configuration options"; + } + + return help_str; +} + +static void +rspamadm_confighelp_show (const ucl_object_t *obj) +{ + rspamd_fstring_t *out; + + out = rspamd_fstring_new (); + + if (json) { + rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON, &out); + } + else if (compact) { + rspamd_ucl_emit_fstring (obj, UCL_EMIT_JSON_COMPACT, &out); + } + else { + /* TODO: add lua helper for output */ + rspamd_ucl_emit_fstring (obj, UCL_EMIT_CONFIG, &out); + } + + rspamd_fprintf (stdout, "%V", out); + + rspamd_fstring_free (out); +} + +static void +rspamadm_confighelp (gint argc, gchar **argv) +{ + struct rspamd_rcl_section *top; + struct rspamd_config *cfg; + const ucl_object_t *doc_obj; + GOptionContext *context; + GError *error = NULL; + gint i = 1, ret = 0; + + context = g_option_context_new ( + "confighelp - displays help for the configuration options"); + g_option_context_set_summary (context, + "Summary:\n Rspamd administration utility version " + RVERSION + "\n Release id: " + RID); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_set_ignore_unknown_options (context, TRUE); + + if (!g_option_context_parse (context, &argc, &argv, &error)) { + rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); + g_error_free (error); + exit (1); + } + + cfg = rspamd_config_new (); + + top = rspamd_rcl_config_init (cfg); + + if (argc > 1) { + while (argc > 1) { + doc_obj = ucl_lookup_path (cfg->doc_strings, argv[i]); + + if (doc_obj != NULL) { + rspamadm_confighelp_show (doc_obj); + } + else { + rspamd_fprintf (stderr, "Cannot find help for %s\n", argv[i]); + ret = EXIT_FAILURE; + } + + i++; + argc--; + } + } + else { + /* Show all documentation strings */ + rspamadm_confighelp_show (cfg->doc_strings); + } + + exit (ret); +} diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c index 2b0332414..16170196e 100644 --- a/src/rspamadm/control.c +++ b/src/rspamadm/control.c @@ -35,9 +35,9 @@ #include "fuzzy_stat.lua.h" static gchar *control_path = RSPAMD_DBDIR "/rspamd.sock"; -gboolean json = FALSE; -gboolean compact = FALSE; -gdouble timeout = 1.0; +static gboolean json = FALSE; +static gboolean compact = FALSE; +static gdouble timeout = 1.0; static void rspamadm_control (gint argc, gchar **argv); static const char *rspamadm_control_help (gboolean full_help); -- 2.39.5