Modules: Additional improvements to CLI completion.
[asterisk/asterisk.git] / main / cli.c
index 8af20b6..0896181 100644 (file)
@@ -38,8 +38,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_REGISTER_FILE()
-
 #include "asterisk/_private.h"
 #include "asterisk/paths.h"    /* use ast_config_AST_MODULE_DIR */
 #include <signal.h>
@@ -47,7 +45,6 @@ ASTERISK_REGISTER_FILE()
 #include <regex.h>
 #include <pwd.h>
 #include <grp.h>
-#include <editline/readline.h>
 
 #include "asterisk/cli.h"
 #include "asterisk/linkedlists.h"
@@ -63,6 +60,7 @@ ASTERISK_REGISTER_FILE()
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stasis_bridges.h"
 #include "asterisk/vector.h"
+#include "asterisk/stream.h"
 
 /*!
  * \brief List of restrictions per user.
@@ -225,28 +223,6 @@ static int cli_has_permissions(int uid, int gid, const char *command)
 
 static AST_RWLIST_HEAD_STATIC(helpers, ast_cli_entry);
 
-static char *complete_fn(const char *word, int state)
-{
-       char *c, *d;
-       char filename[PATH_MAX];
-
-       if (word[0] == '/')
-               ast_copy_string(filename, word, sizeof(filename));
-       else
-               snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_MODULE_DIR, word);
-
-       c = d = filename_completion_function(filename, state);
-
-       if (c && word[0] != '/')
-               c += (strlen(ast_config_AST_MODULE_DIR) + 1);
-       if (c)
-               c = ast_strdup(c);
-
-       ast_std_free(d);
-
-       return c;
-}
-
 static char *handle_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        /* "module load <mod>" */
@@ -259,12 +235,14 @@ static char *handle_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *
                return NULL;
 
        case CLI_GENERATE:
-               if (a->pos != e->args)
+               if (a->pos != e->args) {
                        return NULL;
-               return complete_fn(a->word, a->n);
+               }
+               return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, AST_MODULE_HELPER_LOAD);
        }
-       if (a->argc != e->args + 1)
+       if (a->argc != e->args + 1) {
                return CLI_SHOWUSAGE;
+       }
        if (ast_load_resource(a->argv[e->args])) {
                ast_cli(a->fd, "Unable to load module %s\n", a->argv[e->args]);
                return CLI_FAILURE;
@@ -287,7 +265,7 @@ static char *handle_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                return NULL;
 
        case CLI_GENERATE:
-               return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 1);
+               return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, AST_MODULE_HELPER_RELOAD);
        }
        if (a->argc == e->args) {
                ast_module_reload(NULL);
@@ -373,7 +351,7 @@ static char *complete_number(const char *partial, unsigned int min, unsigned int
        int i, count = 0;
        unsigned int prospective[2];
        unsigned int part = strtoul(partial, NULL, 10);
-       char next[12];
+       char next[13];
 
        if (part < min || part > max) {
                return NULL;
@@ -449,19 +427,11 @@ static char *handle_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args
        case CLI_INIT:
                e->command = "core set debug";
                e->usage =
-#if !defined(LOW_MEMORY)
                        "Usage: core set debug [atleast] <level> [module]\n"
-#else
-                       "Usage: core set debug [atleast] <level>\n"
-#endif
                        "       core set debug off\n"
                        "\n"
-#if !defined(LOW_MEMORY)
                        "       Sets level of debug messages to be displayed or\n"
                        "       sets a module name to display debug messages from.\n"
-#else
-                       "       Sets level of debug messages to be displayed.\n"
-#endif
                        "       0 or off means no messages should be displayed.\n"
                        "       Equivalent to -d[d[...]] on startup\n";
                return NULL;
@@ -489,13 +459,9 @@ static char *handle_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                        } else if (a->n == (22 - numbermatch) && a->pos == 3 && ast_strlen_zero(argv3)) {
                                return ast_strdup("atleast");
                        }
-#if !defined(LOW_MEMORY)
                } else if ((a->pos == 4 && !atleast && strcasecmp(argv3, "off") && strcasecmp(argv3, "channel"))
                        || (a->pos == 5 && atleast)) {
-                       const char *pos = S_OR(a->argv[a->pos], "");
-
-                       return ast_complete_source_filename(pos, a->n);
-#endif
+                       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, AST_MODULE_HELPER_RUNNING);
                }
                return NULL;
        }
@@ -746,7 +712,7 @@ static char *handle_unload(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                return NULL;
 
        case CLI_GENERATE:
-               return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
+               return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, AST_MODULE_HELPER_UNLOAD);
        }
        if (a->argc < e->args + 1)
                return CLI_SHOWUSAGE;
@@ -902,10 +868,11 @@ static char *handle_modlist(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
                return NULL;
 
        case CLI_GENERATE:
-               if (a->pos == e->args)
-                       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, 0);
-               else
+               if (a->pos == e->args) {
+                       return ast_module_helper(a->line, a->word, a->pos, a->n, a->pos, AST_MODULE_HELPER_LOADED);
+               } else {
                        return NULL;
+               }
        }
        /* all the above return, so we proceed with the handler.
         * we are guaranteed to have argc >= e->args
@@ -930,6 +897,7 @@ static char *handle_modlist(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
 
 static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
+       static const char * const completions[] = { "seconds", NULL };
        struct timeval curtime = ast_tvnow();
        int showuptime, printsec;
 
@@ -937,7 +905,7 @@ static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_a
        case CLI_INIT:
                e->command = "core show calls [uptime]";
                e->usage =
-                       "Usage: core show calls [uptime] [seconds]\n"
+                       "Usage: core show calls [uptime [seconds]]\n"
                        "       Lists number of currently active calls and total number of calls\n"
                        "       processed through PBX since last restart. If 'uptime' is specified\n"
                        "       the system uptime is also displayed. If 'seconds' is specified in\n"
@@ -947,7 +915,7 @@ static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_a
        case CLI_GENERATE:
                if (a->pos != e->args)
                        return NULL;
-               return a->n == 0  ? ast_strdup("seconds") : NULL;
+               return ast_cli_complete(a->word, completions, a->n);
        }
 
        /* regular handler */
@@ -1043,7 +1011,7 @@ static char *handle_chanlist(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
        it_chans = ao2_iterator_init(channels, 0);
        for (; (msg = ao2_iterator_next(&it_chans)); ao2_ref(msg, -1)) {
                struct ast_channel_snapshot *cs = stasis_message_data(msg);
-               char durbuf[10] = "-";
+               char durbuf[16] = "-";
 
                if (!count) {
                        if ((concise || verbose)  && !ast_tvzero(cs->creationtime)) {
@@ -1117,7 +1085,9 @@ static char *handle_chanlist(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
 
 static char *handle_softhangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       struct ast_channel *c=NULL;
+       struct ast_channel *c = NULL;
+       static const char * const completions[] = { "all", NULL };
+       char *complete;
 
        switch (cmd) {
        case CLI_INIT:
@@ -1130,7 +1100,14 @@ static char *handle_softhangup(struct ast_cli_entry *e, int cmd, struct ast_cli_
                        "       will see the hangup request.\n";
                return NULL;
        case CLI_GENERATE:
-               return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
+               if (a->pos != e->args) {
+                       return NULL;
+               }
+               complete = ast_cli_complete(a->word, completions, a->n);
+               if (!complete) {
+                       complete = ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
+               }
+               return complete;
        }
 
        if (a->argc != 4) {
@@ -1442,6 +1419,8 @@ static int channel_set_debug(void *obj, void *arg, void *data, int flags)
 static char *handle_core_set_debug_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ast_channel *c = NULL;
+       static const char * const completions_all[] = { "all", NULL };
+       static const char * const completions_off[] = { "off", NULL };
        struct channel_set_debug_args args = {
                .fd = a->fd,
        };
@@ -1454,10 +1433,15 @@ static char *handle_core_set_debug_channel(struct ast_cli_entry *e, int cmd, str
                        "       Enables/disables debugging on all or on a specific channel.\n";
                return NULL;
        case CLI_GENERATE:
-               /* XXX remember to handle the optional "off" */
-               if (a->pos != e->args)
-                       return NULL;
-               return a->n == 0 ? ast_strdup("all") : ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
+               if (a->pos == 4) {
+                       char *complete = ast_cli_complete(a->word, completions_all, a->n);
+                       if (!complete) {
+                               complete = ast_complete_channels(a->line, a->word, a->pos, a->n - 1, e->args);
+                       }
+                       return complete;
+               } else if (a->pos == 5) {
+                       return ast_cli_complete(a->word, completions_off, a->n);
+               }
        }
 
        if (cmd == (CLI_HANDLER + 1000)) {
@@ -1539,6 +1523,7 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
        struct ast_bridge *bridge;
        ast_callid callid;
        char callid_buf[32];
+       int stream_num;
 
        switch (cmd) {
        case CLI_INIT:
@@ -1665,6 +1650,7 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
                S_OR(ast_channel_data(chan), "(Empty)"),
                S_OR(callid_buf, "(None)")
                );
+
        ast_str_append(&output, 0, "      Variables:\n");
 
        AST_LIST_TRAVERSE(ast_channel_varshead(chan), var, entries) {
@@ -1676,6 +1662,24 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
                ast_str_append(&output, 0, "  CDR Variables:\n%s\n", ast_str_buffer(obuf));
        }
 
+       ast_str_append(&output, 0, " -- Streams --\n");
+       for (stream_num = 0; stream_num < ast_stream_topology_get_count(ast_channel_get_stream_topology(chan)); stream_num++) {
+               struct ast_stream *stream = ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), stream_num);
+
+               ast_str_append(&output, 0,
+                       "Name: %s\n"
+                       "    Type: %s\n"
+                       "    State: %s\n"
+                       "    Group: %d\n"
+                       "    Formats: %s\n",
+                       ast_stream_get_name(stream),
+                       ast_codec_media_type2str(ast_stream_get_type(stream)),
+                       ast_stream_state2str(ast_stream_get_state(stream)),
+                       ast_stream_get_group(stream),
+                       ast_format_cap_get_names(ast_stream_get_formats(stream), &codec_buf)
+                       );
+       }
+
        ast_channel_unlock(chan);
 
        ast_cli(a->fd, "%s", ast_str_buffer(output));