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";
ret = cmd;
break;
}
+
+ PTR_ARRAY_FOREACH (cmd->aliases, j, alias) {
+ if (strcmp (name, alias) == 0) {
+ ret = cmd;
+ break;
+ }
+ }
}
return ret;
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) {
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));
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);
}
}
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)
{
/* 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);
+ }
+ }
}
}