/ added parameter to change ec suppresion threshhold from config
[asterisk/asterisk.git] / cli.c
diff --git a/cli.c b/cli.c
index 16a05b0..70e3809 100755 (executable)
--- a/cli.c
+++ b/cli.c
@@ -3,7 +3,7 @@
  *
  * Standard Command Line Interface
  * 
- * Copyright (C) 1999-2004, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
+#include <ctype.h>
 /* For rl_filename_completion */
 #include "editline/readline/readline.h"
 /* For module directory */
@@ -46,7 +47,12 @@ void ast_cli(int fd, char *fmt, ...)
 
        va_list ap;
        va_start(ap, fmt);
+#ifdef SOLARIS
+        stuff = (char *)malloc(10240);
+        vsnprintf(stuff, 10240, fmt, ap);
+#else
        res = vasprintf(&stuff, fmt, ap);
+#endif
        va_end(ap);
        if (res == -1) {
                ast_log(LOG_ERROR, "Out of memory\n");
@@ -144,6 +150,7 @@ static int handle_set_verbose(int fd, int argc, char *argv[])
 {
        int val = 0;
        int oldval = 0;
+
        /* Has a hidden 'at least' argument */
        if ((argc != 3) && (argc != 4))
                return RESULT_SHOWUSAGE;
@@ -160,7 +167,7 @@ static int handle_set_verbose(int fd, int argc, char *argv[])
        if (oldval != option_verbose && option_verbose > 0)
                ast_cli(fd, "Verbosity was %d and is now %d\n", oldval, option_verbose);
        else if (oldval > 0 && option_verbose > 0)
-               ast_cli(fd, "Verbosity is atleast %d\n", option_verbose);
+               ast_cli(fd, "Verbosity is at least %d\n", option_verbose);
        else if (oldval > 0 && option_verbose == 0)
                ast_cli(fd, "Verbosity is now OFF\n");
        return RESULT_SUCCESS;
@@ -186,7 +193,7 @@ static int handle_set_debug(int fd, int argc, char *argv[])
        if (oldval != option_debug && option_debug > 0)
                ast_cli(fd, "Core debug was %d and is now %d\n", oldval, option_debug);
        else if (oldval > 0 && option_debug > 0)
-               ast_cli(fd, "Core debug is atleast %d\n", option_debug);
+               ast_cli(fd, "Core debug is at least %d\n", option_debug);
        else if (oldval > 0 && option_debug == 0)
                ast_cli(fd, "Core debug is now OFF\n");
        return RESULT_SUCCESS;
@@ -627,6 +634,7 @@ static int handle_showchan(int fd, int argc, char *argv[])
        struct ast_channel *c=NULL;
        struct timeval now;
        char buf[1024];
+       char cdrtime[256];
        long elapsed_seconds=0;
        int hour=0, min=0, sec=0;
        if (argc != 3)
@@ -640,7 +648,9 @@ static int handle_showchan(int fd, int argc, char *argv[])
                                hour = elapsed_seconds / 3600;
                                min = (elapsed_seconds % 3600) / 60;
                                sec = elapsed_seconds % 60;
-                       }
+                               snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
+                       } else
+                               strncpy(cdrtime, "N/A", sizeof(cdrtime) -1);
                        ast_cli(fd, 
        " -- General --\n"
        "           Name: %s\n"
@@ -658,7 +668,7 @@ static int handle_showchan(int fd, int argc, char *argv[])
        "      Frames in: %d%s\n"
        "     Frames out: %d%s\n"
        " Time to Hangup: %ld\n"
-       "   Elapsed Time: %dh%dm%ds\n"
+       "   Elapsed Time: %s\n"
        " --   PBX   --\n"
        "        Context: %s\n"
        "      Extension: %s\n"
@@ -667,7 +677,6 @@ static int handle_showchan(int fd, int argc, char *argv[])
        "   Pickup Group: %d\n"
        "    Application: %s\n"
        "           Data: %s\n"
-       "          Stack: %d\n"
        "    Blocking in: %s\n",
        c->name, c->type, c->uniqueid,
        (c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
@@ -675,10 +684,10 @@ static int handle_showchan(int fd, int argc, char *argv[])
        (c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
        c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
        c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
-       hour, min, sec, 
+       cdrtime,
        c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
        ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
-       c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
+       (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
                        if(pbx_builtin_serialize_variables(c,buf,sizeof(buf)))
                                ast_cli(fd,"Variables:\n%s\n",buf);
 
@@ -767,8 +776,8 @@ static struct ast_cli_entry builtins[] = {
        { { "reload", NULL }, handle_reload, "Reload configuration", reload_help, complete_mod_2 },
        { { "set", "debug", NULL }, handle_set_debug, "Set level of debug chattiness", set_debug_help },
        { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
-       { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
        { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch_3 },
+       { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
        { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
        { { "show", "modules", "like", NULL }, handle_modlist, "List modules and info", modlist_help, complete_mod_4 },
        { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
@@ -936,10 +945,10 @@ int ast_cli_register(struct ast_cli_entry *e)
 
 static int help_workhorse(int fd, char *match[])
 {
-       char fullcmd1[80];
-       char fullcmd2[80];
+       char fullcmd1[80] = "";
+       char fullcmd2[80] = "";
        char matchstr[80];
-       char *fullcmd;
+       char *fullcmd = NULL;
        struct ast_cli_entry *e, *e1, *e2;
        e1 = builtins;
        e2 = helpers;
@@ -1072,8 +1081,7 @@ normal:
 int ast_cli_generatornummatches(char *text, char *word)
 {
        int matches = 0, i = 0;
-       char *buf, *oldbuf = NULL;
-
+       char *buf = NULL, *oldbuf = NULL;
 
        while ( (buf = ast_cli_generator(text, word, i)) ) {
                if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
@@ -1108,7 +1116,7 @@ char **ast_cli_completion_matches(char *text, char *word)
        prevstr = match_list[1];
        max_equal = strlen(prevstr);
        for (; which <= matches; which++) {
-               for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
+               for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
                        continue;
                max_equal = i;
        }
@@ -1132,10 +1140,10 @@ static char *__ast_cli_generator(char *text, char *word, int state, int lock)
        int x;
        int matchnum=0;
        char *dup, *res;
-       char fullcmd1[80];
-       char fullcmd2[80];
+       char fullcmd1[80] = "";
+       char fullcmd2[80] = "";
        char matchstr[80];
-       char *fullcmd;
+       char *fullcmd = NULL;
 
        if ((dup = parse_args(text, &x, argv))) {
                join(matchstr, sizeof(matchstr), argv);
@@ -1163,19 +1171,19 @@ static char *__ast_cli_generator(char *text, char *word, int state, int lock)
                        }
                        if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
                                /* We contain the first part of one or more commands */
-                               matchnum++;
-                               if (matchnum > state) {
-                                       /* Now, what we're supposed to return is the next word... */
-                                       if (!ast_strlen_zero(word) && x>0) {
-                                               res = e->cmda[x-1];
-                                       } else {
-                                               res = e->cmda[x];
-                                       }
-                                       if (res) {
+                               /* Now, what we're supposed to return is the next word... */
+                               if (!ast_strlen_zero(word) && x>0) {
+                                       res = e->cmda[x-1];
+                               } else {
+                                       res = e->cmda[x];
+                               }
+                               if (res) {
+                                       matchnum++;
+                                       if (matchnum > state) {
                                                if (lock)
                                                        ast_mutex_unlock(&clilock);
                                                free(dup);
-                                               return res ? strdup(res) : NULL;
+                                               return strdup(res);
                                        }
                                }
                        }
@@ -1183,10 +1191,12 @@ static char *__ast_cli_generator(char *text, char *word, int state, int lock)
                                /* We have a command in its entirity within us -- theoretically only one
                                   command can have this occur */
                                fullcmd = e->generator(matchstr, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
-                               if (lock)
-                                       ast_mutex_unlock(&clilock);
-                               free(dup);
-                               return fullcmd;
+                               if (fullcmd) {
+                                       if (lock)
+                                               ast_mutex_unlock(&clilock);
+                                       free(dup);
+                                       return fullcmd;
+                               }
                        }
                        
                }