From 7be62ddeb0ae7cc5a45f33b1de0e864ff03cf8d6 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 26 Mar 2019 16:47:59 +0000 Subject: [PATCH] [Minor] Unify `--var` argument for rspamd and rspamadm --- src/rspamadm/rspamadm.c | 2 +- src/rspamd.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index 227cbc86b..281690132 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -60,7 +60,7 @@ static GOptionEntry entries[] = { {"list-commands", 'l', 0, G_OPTION_ARG_NONE, &list_commands, "List available commands", NULL}, {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&rspamadm_parse_ucl_var, - "Redefine UCL variable", NULL}, + "Redefine/define environment variable", NULL}, {"help", 'h', 0, G_OPTION_ARG_NONE, &show_help, "Show help", NULL}, {"version", 'V', 0, G_OPTION_ARG_NONE, &show_version, diff --git a/src/rspamd.c b/src/rspamd.c index 142915df9..631759f05 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -108,7 +108,10 @@ static GHashTable *listen_sockets = NULL; extern module_t *modules[]; extern worker_t *workers[]; -/* Commandline options */ +/* Command line options */ +static gboolean rspamd_parse_var (const gchar *option_name, + const gchar *value, gpointer data, + GError **error); static GOptionEntry entries[] = { { "config-test", 't', 0, G_OPTION_ARG_NONE, &config_test, @@ -141,9 +144,36 @@ static GOptionEntry entries[] = "controller password to store in the configuration file", NULL }, { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version, "Show version and exit", NULL }, + {"var", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer)&rspamd_parse_var, + "Redefine/define environment variable", NULL}, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; +static gboolean +rspamd_parse_var (const gchar *option_name, + const gchar *value, gpointer data, + GError **error) +{ + gchar *k, *v, *t; + + t = strchr (value, '='); + + if (t != NULL) { + k = g_strdup (value); + t = k + (t - value); + v = g_strdup (t + 1); + *t = '\0'; + + g_hash_table_insert (ucl_vars, k, v); + } + else { + g_set_error (error, g_quark_try_string ("main"), EINVAL, + "Bad variable format: %s", value); + return FALSE; + } + + return TRUE; +} static void read_cmd_line (gint *argc, gchar ***argv, struct rspamd_config *cfg) -- 2.39.5