2 * Asterisk -- A telephony toolkit for Linux.
4 * Standard Command Line Interface
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <asterisk/cli.h>
19 #include <asterisk/module.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/channel_pvt.h>
22 #include <asterisk/manager.h>
23 #include <asterisk/utils.h>
24 #include <asterisk/lock.h>
25 #include <sys/signal.h>
29 /* For rl_filename_completion */
30 #include "editline/readline/readline.h"
31 /* For module directory */
36 #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
37 " on a " BUILD_MACHINE " running " BUILD_OS
39 void ast_cli(int fd, char *fmt, ...)
46 res = vasprintf(&stuff, fmt, ap);
49 ast_log(LOG_ERROR, "Out of memory\n");
51 ast_carefulwrite(fd, stuff, strlen(stuff), 100);
56 AST_MUTEX_DEFINE_STATIC(clilock);
58 struct ast_cli_entry *helpers = NULL;
60 static char load_help[] =
61 "Usage: load <module name>\n"
62 " Loads the specified module into Asterisk.\n";
64 static char unload_help[] =
65 "Usage: unload [-f|-h] <module name>\n"
66 " Unloads the specified module from Asterisk. The -f\n"
67 " option causes the module to be unloaded even if it is\n"
68 " in use (may cause a crash) and the -h module causes the\n"
69 " module to be unloaded even if the module says it cannot, \n"
70 " which almost always will cause a crash.\n";
72 static char help_help[] =
73 "Usage: help [topic]\n"
74 " When called with a topic as an argument, displays usage\n"
75 " information on the given command. If called without a\n"
76 " topic, it provides a list of commands.\n";
78 static char chanlist_help[] =
79 "Usage: show channels [concise]\n"
80 " Lists currently defined channels and some information about\n"
81 " them. If 'concise' is specified, format is abridged and in\n"
82 " a more easily machine parsable format\n";
84 static char reload_help[] =
86 " Reloads configuration files for all modules which support\n"
89 static char set_verbose_help[] =
90 "Usage: set verbose <level>\n"
91 " Sets level of verbose messages to be displayed. 0 means\n"
92 " no messages should be displayed.\n";
94 static char softhangup_help[] =
95 "Usage: soft hangup <channel>\n"
96 " Request that a channel be hung up. The hangup takes effect\n"
97 " the next time the driver reads or writes from the channel\n";
99 static int handle_load(int fd, int argc, char *argv[])
102 return RESULT_SHOWUSAGE;
103 if (ast_load_resource(argv[1])) {
104 ast_cli(fd, "Unable to load module %s\n", argv[1]);
105 return RESULT_FAILURE;
107 return RESULT_SUCCESS;
110 static int handle_reload(int fd, int argc, char *argv[])
113 return RESULT_SHOWUSAGE;
115 return RESULT_SUCCESS;
118 static int handle_set_verbose(int fd, int argc, char *argv[])
121 /* Has a hidden 'at least' argument */
122 if ((argc != 3) && (argc != 4))
123 return RESULT_SHOWUSAGE;
124 if ((argc == 4) && strcasecmp(argv[2], "atleast"))
125 return RESULT_SHOWUSAGE;
127 option_verbose = atoi(argv[2]);
130 if (val > option_verbose)
131 option_verbose = val;
133 return RESULT_SUCCESS;
136 static int handle_unload(int fd, int argc, char *argv[])
139 int force=AST_FORCE_SOFT;
141 return RESULT_SHOWUSAGE;
142 for (x=1;x<argc;x++) {
143 if (argv[x][0] == '-') {
146 force = AST_FORCE_FIRM;
149 force = AST_FORCE_HARD;
152 return RESULT_SHOWUSAGE;
154 } else if (x != argc - 1)
155 return RESULT_SHOWUSAGE;
156 else if (ast_unload_resource(argv[x], force)) {
157 ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
158 return RESULT_FAILURE;
161 return RESULT_SUCCESS;
164 #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n"
165 #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
167 AST_MUTEX_DEFINE_STATIC(climodentrylock);
168 static int climodentryfd = -1;
170 static int modlist_modentry(char *module, char *description, int usecnt)
172 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
176 static char modlist_help[] =
177 "Usage: show modules\n"
178 " Shows Asterisk modules currently in use, and usage "
181 static char version_help[] =
182 "Usage: show version\n"
183 " Shows Asterisk version information.\n ";
185 static char *format_uptimestr(time_t timeval)
187 int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
188 char timestr[256]="";
193 #define MINUTE (SECOND*60)
194 #define HOUR (MINUTE*60)
195 #define DAY (HOUR*24)
197 #define YEAR (DAY*365)
198 #define ESS(x) ((x == 1) ? "" : "s")
200 maxbytes = sizeof(timestr);
203 if (timeval > YEAR) {
204 years = (timeval / YEAR);
205 timeval -= (years * YEAR);
207 snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
208 bytes = strlen(timestr + offset);
213 if (timeval > WEEK) {
214 weeks = (timeval / WEEK);
215 timeval -= (weeks * WEEK);
217 snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
218 bytes = strlen(timestr + offset);
224 days = (timeval / DAY);
225 timeval -= (days * DAY);
227 snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
228 bytes = strlen(timestr + offset);
233 if (timeval > HOUR) {
234 hours = (timeval / HOUR);
235 timeval -= (hours * HOUR);
237 snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
238 bytes = strlen(timestr + offset);
243 if (timeval > MINUTE) {
244 mins = (timeval / MINUTE);
245 timeval -= (mins * MINUTE);
247 snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
248 bytes = strlen(timestr + offset);
256 snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
259 return timestr ? strdup(timestr) : NULL;
262 static int handle_showuptime(int fd, int argc, char *argv[])
264 time_t curtime, tmptime;
268 if (ast_startuptime) {
269 tmptime = curtime - ast_startuptime;
270 timestr = format_uptimestr(tmptime);
272 ast_cli(fd, "System uptime: %s\n", timestr);
276 if (ast_lastreloadtime) {
277 tmptime = curtime - ast_lastreloadtime;
278 timestr = format_uptimestr(tmptime);
280 ast_cli(fd, "Last reload: %s\n", timestr);
284 return RESULT_SUCCESS;
287 static int handle_modlist(int fd, int argc, char *argv[])
290 return RESULT_SHOWUSAGE;
291 ast_mutex_lock(&climodentrylock);
293 ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
294 ast_update_module_list(modlist_modentry);
296 ast_mutex_unlock(&climodentrylock);
297 return RESULT_SUCCESS;
300 static int handle_version(int fd, int argc, char *argv[])
303 return RESULT_SHOWUSAGE;
304 ast_cli(fd, "%s\n", VERSION_INFO);
305 return RESULT_SUCCESS;
307 static int handle_chanlist(int fd, int argc, char *argv[])
309 #define FORMAT_STRING "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
310 #define FORMAT_STRING2 "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
311 #define CONCISE_FORMAT_STRING "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"
313 struct ast_channel *c=NULL;
316 if (argc < 2 || argc > 3)
317 return RESULT_SHOWUSAGE;
319 concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
320 c = ast_channel_walk_locked(NULL);
322 ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
325 ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
326 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
327 (c->callerid && !ast_strlen_zero(c->callerid)) ? c->callerid : "",
328 (c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
330 ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
331 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "(Empty)" ): "(None)");
334 ast_mutex_unlock(&c->lock);
335 c = ast_channel_walk_locked(c);
338 ast_cli(fd, "%d active channel(s)\n", numchans);
339 return RESULT_SUCCESS;
342 static char showchan_help[] =
343 "Usage: show channel <channel>\n"
344 " Shows lots of information about the specified channel.\n";
346 static char debugchan_help[] =
347 "Usage: debug channel <channel>\n"
348 " Enables debugging on a specific channel.\n";
350 static char nodebugchan_help[] =
351 "Usage: no debug channel <channel>\n"
352 " Disables debugging on a specific channel.\n";
354 static char commandcomplete_help[] =
355 "Usage: _command complete \"<line>\" text state\n"
356 " This function is used internally to help with command completion and should.\n"
357 " never be called by the user directly.\n";
359 static char commandnummatches_help[] =
360 "Usage: _command nummatches \"<line>\" text \n"
361 " This function is used internally to help with command completion and should.\n"
362 " never be called by the user directly.\n";
364 static char commandmatchesarray_help[] =
365 "Usage: _command matchesarray \"<line>\" text \n"
366 " This function is used internally to help with command completion and should.\n"
367 " never be called by the user directly.\n";
369 static int handle_softhangup(int fd, int argc, char *argv[])
371 struct ast_channel *c=NULL;
373 return RESULT_SHOWUSAGE;
374 c = ast_channel_walk_locked(NULL);
376 if (!strcasecmp(c->name, argv[2])) {
377 ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
378 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
379 ast_mutex_unlock(&c->lock);
382 ast_mutex_unlock(&c->lock);
383 c = ast_channel_walk_locked(c);
386 ast_cli(fd, "%s is not a known channel\n", argv[2]);
387 return RESULT_SUCCESS;
390 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
392 static int handle_commandmatchesarray(int fd, int argc, char *argv[])
401 return RESULT_SHOWUSAGE;
402 buf = malloc(buflen);
404 return RESULT_FAILURE;
406 matches = ast_cli_completion_matches(argv[2], argv[3]);
408 for (x=0; matches[x]; x++) {
410 printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
412 if (len + strlen(matches[x]) >= buflen) {
413 buflen += strlen(matches[x]) * 3;
414 buf = realloc(buf, buflen);
416 len += sprintf( buf + len, "%s ", matches[x]);
423 printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
427 ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
430 ast_cli(fd, "NULL\n");
432 return RESULT_SUCCESS;
437 static int handle_commandnummatches(int fd, int argc, char *argv[])
442 return RESULT_SHOWUSAGE;
444 matches = ast_cli_generatornummatches(argv[2], argv[3]);
447 printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
449 ast_cli(fd, "%d", matches);
451 return RESULT_SUCCESS;
454 static int handle_commandcomplete(int fd, int argc, char *argv[])
458 printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
461 return RESULT_SHOWUSAGE;
462 buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
464 printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
470 ast_cli(fd, "NULL\n");
471 return RESULT_SUCCESS;
474 static int handle_debugchan(int fd, int argc, char *argv[])
476 struct ast_channel *c=NULL;
478 return RESULT_SHOWUSAGE;
479 c = ast_channel_walk_locked(NULL);
481 if (!strcasecmp(c->name, argv[2])) {
482 c->fin |= 0x80000000;
483 c->fout |= 0x80000000;
486 ast_mutex_unlock(&c->lock);
487 c = ast_channel_walk_locked(c);
490 ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
491 ast_mutex_unlock(&c->lock);
494 ast_cli(fd, "No such channel %s\n", argv[2]);
495 return RESULT_SUCCESS;
498 static int handle_nodebugchan(int fd, int argc, char *argv[])
500 struct ast_channel *c=NULL;
502 return RESULT_SHOWUSAGE;
503 c = ast_channel_walk_locked(NULL);
505 if (!strcasecmp(c->name, argv[3])) {
506 c->fin &= 0x7fffffff;
507 c->fout &= 0x7fffffff;
510 ast_mutex_unlock(&c->lock);
511 c = ast_channel_walk_locked(c);
514 ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
515 ast_mutex_unlock(&c->lock);
517 ast_cli(fd, "No such channel %s\n", argv[2]);
518 return RESULT_SUCCESS;
523 static int handle_showchan(int fd, int argc, char *argv[])
525 struct ast_channel *c=NULL;
527 return RESULT_SHOWUSAGE;
528 c = ast_channel_walk_locked(NULL);
530 if (!strcasecmp(c->name, argv[2])) {
540 " NativeFormat: %d\n"
543 "1st File Descriptor: %d\n"
545 " Frames out: %d%s\n"
546 " Time to Hangup: %ld\n"
552 " Pickup Group: %d\n"
556 " Blocking in: %s\n",
557 c->name, c->type, c->uniqueid,
558 (c->callerid ? c->callerid : "(N/A)"),
559 (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
560 c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
561 c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
562 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
563 ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
564 c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
565 ast_mutex_unlock(&c->lock);
568 ast_mutex_unlock(&c->lock);
569 c = ast_channel_walk_locked(c);
572 ast_cli(fd, "%s is not a known channel\n", argv[2]);
573 return RESULT_SUCCESS;
576 static char *complete_ch(char *line, char *word, int pos, int state)
578 struct ast_channel *c;
581 c = ast_channel_walk_locked(NULL);
583 if (!strncasecmp(word, c->name, strlen(word))) {
587 ast_mutex_unlock(&c->lock);
588 c = ast_channel_walk_locked(c);
591 ret = strdup(c->name);
592 ast_mutex_unlock(&c->lock);
598 static char *complete_fn(char *line, char *word, int pos, int state)
605 strncpy(filename, word, sizeof(filename)-1);
607 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
608 c = (char*)filename_completion_function(filename, state);
609 if (c && word[0] != '/')
610 c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
611 return c ? strdup(c) : c;
614 static int handle_help(int fd, int argc, char *argv[]);
616 static struct ast_cli_entry builtins[] = {
617 /* Keep alphabetized, with longer matches first (example: abcd before abc) */
618 { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
619 { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
620 { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
621 { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch },
622 { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
623 { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
624 { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch },
625 { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
626 { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
627 { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
628 { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch },
629 { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
630 { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
631 { { "show", "version", NULL }, handle_version, "Display version info", version_help },
632 { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch },
633 { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
634 { { NULL }, NULL, NULL, NULL }
637 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
642 struct ast_cli_entry *e=NULL;
643 for (x=0;builtins[x].cmda[0];x++) {
644 /* start optimistic */
646 for (y=0;match && cmds[y]; y++) {
647 /* If there are no more words in the candidate command, then we're
649 if (!builtins[x].cmda[y] && !exact)
651 /* If there are no more words in the command (and we're looking for
652 an exact match) or there is a difference between the two words,
653 then this is not a match */
654 if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
657 /* If more words are needed to complete the command then this is not
658 a candidate (unless we're looking for a really inexact answer */
659 if ((exact > -1) && builtins[x].cmda[y])
664 for (e=helpers;e;e=e->next) {
666 for (y=0;match && cmds[y]; y++) {
667 if (!e->cmda[y] && !exact)
669 if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
672 if ((exact > -1) && e->cmda[y])
680 static void join(char *dest, size_t destsize, char *w[])
683 /* Join words into a string */
684 if (!dest || destsize < 1) {
690 strncat(dest, " ", destsize - strlen(dest) - 1);
691 strncat(dest, w[x], destsize - strlen(dest) - 1);
695 static void join2(char *dest, size_t destsize, char *w[])
698 /* Join words into a string */
699 if (!dest || destsize < 1) {
704 strncat(dest, w[x], destsize - strlen(dest) - 1);
708 static char *find_best(char *argv[])
710 static char cmdline[80];
712 /* See how close we get, then print the */
713 char *myargv[AST_MAX_CMD_LEN];
714 for (x=0;x<AST_MAX_CMD_LEN;x++)
716 for (x=0;argv[x];x++) {
718 if (!find_cli(myargv, -1))
721 join(cmdline, sizeof(cmdline), myargv);
725 int ast_cli_unregister(struct ast_cli_entry *e)
727 struct ast_cli_entry *cur, *l=NULL;
728 ast_mutex_lock(&clilock);
733 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
747 ast_mutex_unlock(&clilock);
751 int ast_cli_register(struct ast_cli_entry *e)
753 struct ast_cli_entry *cur, *l=NULL;
754 char fulle[80] ="", fulltst[80] ="";
756 ast_mutex_lock(&clilock);
757 join2(fulle, sizeof(fulle), e->cmda);
758 if (find_cli(e->cmda, -1)) {
759 ast_mutex_unlock(&clilock);
760 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
765 join2(fulltst, sizeof(fulltst), cur->cmda);
766 len = strlen(fulltst);
767 if (strlen(fulle) < len)
769 if (strncasecmp(fulle, fulltst, len) < 0) {
789 ast_mutex_unlock(&clilock);
793 static int help_workhorse(int fd, char *match[])
799 struct ast_cli_entry *e, *e1, *e2;
803 join(matchstr, sizeof(matchstr), match);
804 while(e1->cmda[0] || e2) {
806 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
808 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
810 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
814 /* Increment by going to next */
822 /* Hide commands that start with '_' */
823 if (fullcmd[0] == '_')
826 if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
830 ast_cli(fd, "%25.25s %s\n", fullcmd, e->summary);
835 static int handle_help(int fd, int argc, char *argv[]) {
836 struct ast_cli_entry *e;
839 return RESULT_SHOWUSAGE;
841 e = find_cli(argv + 1, 1);
843 ast_cli(fd, e->usage);
845 if (find_cli(argv + 1, -1)) {
846 return help_workhorse(fd, argv + 1);
848 join(fullcmd, sizeof(fullcmd), argv+1);
849 ast_cli(fd, "No such command '%s'.\n", fullcmd);
853 return help_workhorse(fd, NULL);
855 return RESULT_SUCCESS;
858 static char *parse_args(char *s, int *max, char *argv[])
872 /* If it's escaped, put a literal quote */
877 if (quoted && whitespace) {
878 /* If we're starting a quote, coming off white space start a new word, too */
886 if (!quoted && !escaped) {
887 /* If we're not quoted, mark this as whitespace, and
888 end the previous argument */
892 /* Otherwise, just treat it as anything else */
896 /* If we're escaped, print a literal, otherwise enable escaping */
906 if (x >= AST_MAX_ARGS -1) {
907 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
910 /* Coming off of whitespace, start the next argument */
927 /* This returns the number of unique matches for the generator */
928 int ast_cli_generatornummatches(char *text, char *word)
930 int matches = 0, i = 0;
931 char *buf, *oldbuf = NULL;
934 while ( (buf = ast_cli_generator(text, word, i)) ) {
935 if (++i > 1 && strcmp(buf,oldbuf) == 0) {
945 char **ast_cli_completion_matches(char *text, char *word)
947 char **match_list = NULL, *retstr, *prevstr;
948 size_t match_list_len, max_equal, which, i;
952 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
953 if (matches + 1 >= match_list_len) {
954 match_list_len <<= 1;
955 match_list = realloc(match_list, match_list_len * sizeof(char *));
957 match_list[++matches] = retstr;
961 return (char **) NULL;
964 prevstr = match_list[1];
965 max_equal = strlen(prevstr);
966 for (; which <= matches; which++) {
967 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
972 retstr = malloc(max_equal + 1);
973 (void) strncpy(retstr, match_list[1], max_equal);
974 retstr[max_equal] = '\0';
975 match_list[0] = retstr;
977 if (matches + 1 >= match_list_len)
978 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
979 match_list[matches + 1] = (char *) NULL;
984 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
986 char *argv[AST_MAX_ARGS];
987 struct ast_cli_entry *e, *e1, *e2;
996 if ((dup = parse_args(text, &x, argv))) {
997 join(matchstr, sizeof(matchstr), argv);
999 ast_mutex_lock(&clilock);
1002 while(e1->cmda[0] || e2) {
1004 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
1006 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
1008 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
1012 /* Increment by going to next */
1020 if ((fullcmd[0] != '_') && !strncasecmp(text, fullcmd, strlen(text))) {
1021 /* We contain the first part of one or more commands */
1023 if (matchnum > state) {
1024 /* Now, what we're supposed to return is the next word... */
1025 if (!ast_strlen_zero(word) && x>0) {
1032 ast_mutex_unlock(&clilock);
1034 return res ? strdup(res) : NULL;
1038 if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
1039 /* We have a command in its entirity within us -- theoretically only one
1040 command can have this occur */
1041 fullcmd = e->generator(text, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
1043 ast_mutex_unlock(&clilock);
1050 ast_mutex_unlock(&clilock);
1056 char *ast_cli_generator(char *text, char *word, int state)
1058 return __ast_cli_generator(text, word, state, 1);
1061 int ast_cli_command(int fd, char *s)
1063 char *argv[AST_MAX_ARGS];
1064 struct ast_cli_entry *e;
1068 if ((dup = parse_args(s, &x, argv))) {
1069 /* We need at least one entry, or ignore */
1071 ast_mutex_lock(&clilock);
1072 e = find_cli(argv, 0);
1075 ast_mutex_unlock(&clilock);
1077 switch(e->handler(fd, x, argv)) {
1078 case RESULT_SHOWUSAGE:
1079 ast_cli(fd, e->usage);
1083 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
1085 ast_mutex_lock(&clilock);
1087 ast_mutex_unlock(&clilock);
1092 ast_log(LOG_WARNING, "Out of memory\n");