From 20d46fb63c0e65e9d2d7b6601b0ff2035fd54117 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 1 Jun 2018 14:52:30 +0100 Subject: [PATCH] [Minor] Add aliases support to rspamadm commands --- src/rspamadm/commands.c | 32 ++++++++++++++++++++++++++++++- src/rspamadm/rspamadm.c | 42 ++++++++++++++++++++++++++++++----------- src/rspamadm/rspamadm.h | 1 + 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/rspamadm/commands.c b/src/rspamadm/commands.c index 8d93a86da..83a0507ec 100644 --- a/src/rspamadm/commands.c +++ b/src/rspamadm/commands.c @@ -53,7 +53,8 @@ const struct rspamadm_command * rspamadm_search_command (const gchar *name, GPtrArray *all_commands) { const struct rspamadm_command *ret = NULL, *cmd; - guint i; + const gchar *alias; + guint i, j; if (name == NULL) { name = "help"; @@ -64,6 +65,13 @@ rspamadm_search_command (const gchar *name, GPtrArray *all_commands) ret = cmd; break; } + + PTR_ARRAY_FOREACH (cmd->aliases, j, alias) { + if (strcmp (name, alias) == 0) { + ret = cmd; + break; + } + } } return ret; @@ -208,6 +216,7 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) if (luaL_dofile (L, path) != 0) { msg_err ("cannot execute lua script %s: %s", path, lua_tostring (L, -1)); + lua_settop (L, 0); continue; } else { if (lua_type (L, -1) == LUA_TTABLE) { @@ -252,6 +261,24 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) lua_pop (L, 1); + lua_pushstring (L, "aliases"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TTABLE) { + lua_cmd->aliases = g_ptr_array_new_full ( + rspamd_lua_table_size (L, -1), + g_free); + + for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 2)) { + if (lua_isstring (L, -1)) { + g_ptr_array_add (lua_cmd->aliases, + g_strdup (lua_tostring (L, -1))); + } + } + } + + lua_pop (L, 1); + lua_pushvalue (L, -1); /* Reference table itself */ lua_cmd->command_data = GINT_TO_POINTER (luaL_ref (L, LUA_REGISTRYINDEX)); @@ -259,7 +286,10 @@ rspamadm_fill_lua_commands (lua_State *L, GPtrArray *dest) lua_cmd->run = rspamadm_lua_command_run; lua_cmd->help = rspamadm_lua_command_help; + g_ptr_array_add (dest, lua_cmd); } + + lua_settop (L, 0); } } diff --git a/src/rspamadm/rspamadm.c b/src/rspamadm/rspamadm.c index eff27c1e9..68dbdeb85 100644 --- a/src/rspamadm/rspamadm.c +++ b/src/rspamadm/rspamadm.c @@ -282,6 +282,28 @@ rspamdadm_commands_sort_func (gconstpointer a, gconstpointer b) return strcmp (cmda->name, cmdb->name); } +static gboolean +rspamadm_command_maybe_match_name (const gchar *cmd, const gchar *input) +{ + gsize clen, inplen; + + clen = strlen (cmd); + inplen = strlen (input); + + if (rspamd_strings_levenshtein_distance (cmd, clen, + input, inplen, 1) == 1) { + return TRUE; + } + else if ((clen > inplen && + rspamd_substring_search (cmd, clen, input, inplen) != -1) || + (inplen > clen && + rspamd_substring_search (input, inplen, cmd, clen) != -1)) { + return TRUE; + } + + return FALSE; +} + gint main (gint argc, gchar **argv, gchar **env) { @@ -427,20 +449,18 @@ main (gint argc, gchar **argv, gchar **env) /* Try fuzz search */ rspamd_fprintf (stderr, "Suggested commands:\n"); PTR_ARRAY_FOREACH (all_commands, i, cmd) { - gsize clen, inplen; + guint j; + const gchar *alias; - clen = strlen (cmd->name); - inplen = strlen (cmd_name); - - if (rspamd_strings_levenshtein_distance (cmd->name, clen, - cmd_name, inplen, 1) == 1) { + if (rspamadm_command_maybe_match_name (cmd->name, cmd_name)) { rspamd_fprintf (stderr, "%s\n", cmd->name); } - else if ((clen > inplen && - rspamd_substring_search (cmd->name, clen, cmd_name, inplen) != -1) || - (inplen > clen && - rspamd_substring_search (cmd_name, inplen, cmd->name, clen) != -1)) { - rspamd_fprintf (stderr, "%s\n", cmd->name); + else { + PTR_ARRAY_FOREACH (cmd->aliases, j, alias) { + if (rspamadm_command_maybe_match_name (alias, cmd_name)) { + rspamd_fprintf (stderr, "%s\n", alias); + } + } } } diff --git a/src/rspamadm/rspamadm.h b/src/rspamadm/rspamadm.h index 25f512595..3d9799dd5 100644 --- a/src/rspamadm/rspamadm.h +++ b/src/rspamadm/rspamadm.h @@ -43,6 +43,7 @@ struct rspamadm_command { rspamadm_help_func help; rspamadm_run_func run; rspamadm_lua_exports_func lua_subrs; + GPtrArray *aliases; gpointer command_data; /* Opaque data */ }; -- 2.39.5