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");
52 ast_carefulwrite(fd, stuff, strlen(stuff), 100);
57 AST_MUTEX_DEFINE_STATIC(clilock);
59 struct ast_cli_entry *helpers = NULL;
61 static char load_help[] =
62 "Usage: load <module name>\n"
63 " Loads the specified module into Asterisk.\n";
65 static char unload_help[] =
66 "Usage: unload [-f|-h] <module name>\n"
67 " Unloads the specified module from Asterisk. The -f\n"
68 " option causes the module to be unloaded even if it is\n"
69 " in use (may cause a crash) and the -h module causes the\n"
70 " module to be unloaded even if the module says it cannot, \n"
71 " which almost always will cause a crash.\n";
73 static char help_help[] =
74 "Usage: help [topic]\n"
75 " When called with a topic as an argument, displays usage\n"
76 " information on the given command. If called without a\n"
77 " topic, it provides a list of commands.\n";
79 static char chanlist_help[] =
80 "Usage: show channels [concise]\n"
81 " Lists currently defined channels and some information about\n"
82 " them. If 'concise' is specified, format is abridged and in\n"
83 " a more easily machine parsable format\n";
85 static char reload_help[] =
87 " Reloads configuration files for all modules which support\n"
90 static char set_verbose_help[] =
91 "Usage: set verbose <level>\n"
92 " Sets level of verbose messages to be displayed. 0 means\n"
93 " no messages should be displayed.\n";
95 static char softhangup_help[] =
96 "Usage: soft hangup <channel>\n"
97 " Request that a channel be hung up. The hangup takes effect\n"
98 " the next time the driver reads or writes from the channel\n";
100 static int handle_load(int fd, int argc, char *argv[])
103 return RESULT_SHOWUSAGE;
104 if (ast_load_resource(argv[1])) {
105 ast_cli(fd, "Unable to load module %s\n", argv[1]);
106 return RESULT_FAILURE;
108 return RESULT_SUCCESS;
111 static int handle_reload(int fd, int argc, char *argv[])
114 return RESULT_SHOWUSAGE;
116 return RESULT_SUCCESS;
119 static int handle_set_verbose(int fd, int argc, char *argv[])
122 /* Has a hidden 'at least' argument */
123 if ((argc != 3) && (argc != 4))
124 return RESULT_SHOWUSAGE;
125 if ((argc == 4) && strcasecmp(argv[2], "atleast"))
126 return RESULT_SHOWUSAGE;
128 option_verbose = atoi(argv[2]);
131 if (val > option_verbose)
132 option_verbose = val;
134 return RESULT_SUCCESS;
137 static int handle_unload(int fd, int argc, char *argv[])
140 int force=AST_FORCE_SOFT;
142 return RESULT_SHOWUSAGE;
143 for (x=1;x<argc;x++) {
144 if (argv[x][0] == '-') {
147 force = AST_FORCE_FIRM;
150 force = AST_FORCE_HARD;
153 return RESULT_SHOWUSAGE;
155 } else if (x != argc - 1)
156 return RESULT_SHOWUSAGE;
157 else if (ast_unload_resource(argv[x], force)) {
158 ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
159 return RESULT_FAILURE;
162 return RESULT_SUCCESS;
165 #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n"
166 #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
168 AST_MUTEX_DEFINE_STATIC(climodentrylock);
169 static int climodentryfd = -1;
171 static int modlist_modentry(char *module, char *description, int usecnt)
173 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
177 static char modlist_help[] =
178 "Usage: show modules\n"
179 " Shows Asterisk modules currently in use, and usage "
182 static char version_help[] =
183 "Usage: show version\n"
184 " Shows Asterisk version information.\n ";
186 static char *format_uptimestr(time_t timeval)
188 int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
189 char timestr[256]="";
194 #define MINUTE (SECOND*60)
195 #define HOUR (MINUTE*60)
196 #define DAY (HOUR*24)
198 #define YEAR (DAY*365)
199 #define ESS(x) ((x == 1) ? "" : "s")
201 maxbytes = sizeof(timestr);
204 if (timeval > YEAR) {
205 years = (timeval / YEAR);
206 timeval -= (years * YEAR);
208 snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
209 bytes = strlen(timestr + offset);
214 if (timeval > WEEK) {
215 weeks = (timeval / WEEK);
216 timeval -= (weeks * WEEK);
218 snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
219 bytes = strlen(timestr + offset);
225 days = (timeval / DAY);
226 timeval -= (days * DAY);
228 snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
229 bytes = strlen(timestr + offset);
234 if (timeval > HOUR) {
235 hours = (timeval / HOUR);
236 timeval -= (hours * HOUR);
238 snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
239 bytes = strlen(timestr + offset);
244 if (timeval > MINUTE) {
245 mins = (timeval / MINUTE);
246 timeval -= (mins * MINUTE);
248 snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
249 bytes = strlen(timestr + offset);
257 snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
260 return timestr ? strdup(timestr) : NULL;
263 static int handle_showuptime(int fd, int argc, char *argv[])
265 time_t curtime, tmptime;
269 if (ast_startuptime) {
270 tmptime = curtime - ast_startuptime;
271 timestr = format_uptimestr(tmptime);
273 ast_cli(fd, "System uptime: %s\n", timestr);
277 if (ast_lastreloadtime) {
278 tmptime = curtime - ast_lastreloadtime;
279 timestr = format_uptimestr(tmptime);
281 ast_cli(fd, "Last reload: %s\n", timestr);
285 return RESULT_SUCCESS;
288 static int handle_modlist(int fd, int argc, char *argv[])
291 return RESULT_SHOWUSAGE;
292 ast_mutex_lock(&climodentrylock);
294 ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
295 ast_update_module_list(modlist_modentry);
297 ast_mutex_unlock(&climodentrylock);
298 return RESULT_SUCCESS;
301 static int handle_version(int fd, int argc, char *argv[])
304 return RESULT_SHOWUSAGE;
305 ast_cli(fd, "%s\n", VERSION_INFO);
306 return RESULT_SUCCESS;
308 static int handle_chanlist(int fd, int argc, char *argv[])
310 #define FORMAT_STRING "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
311 #define FORMAT_STRING2 "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
312 #define CONCISE_FORMAT_STRING "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"
314 struct ast_channel *c=NULL;
317 if (argc < 2 || argc > 3)
318 return RESULT_SHOWUSAGE;
320 concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
321 c = ast_channel_walk_locked(NULL);
323 ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
326 ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
327 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
328 (c->callerid && !ast_strlen_zero(c->callerid)) ? c->callerid : "",
329 (c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
331 ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
332 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "(Empty)" ): "(None)");
335 ast_mutex_unlock(&c->lock);
336 c = ast_channel_walk_locked(c);
339 ast_cli(fd, "%d active channel(s)\n", numchans);
340 return RESULT_SUCCESS;
343 static char showchan_help[] =
344 "Usage: show channel <channel>\n"
345 " Shows lots of information about the specified channel.\n";
347 static char debugchan_help[] =
348 "Usage: debug channel <channel>\n"
349 " Enables debugging on a specific channel.\n";
351 static char nodebugchan_help[] =
352 "Usage: no debug channel <channel>\n"
353 " Disables debugging on a specific channel.\n";
355 static char commandcomplete_help[] =
356 "Usage: _command complete \"<line>\" text state\n"
357 " This function is used internally to help with command completion and should.\n"
358 " never be called by the user directly.\n";
360 static char commandnummatches_help[] =
361 "Usage: _command nummatches \"<line>\" text \n"
362 " This function is used internally to help with command completion and should.\n"
363 " never be called by the user directly.\n";
365 static char commandmatchesarray_help[] =
366 "Usage: _command matchesarray \"<line>\" text \n"
367 " This function is used internally to help with command completion and should.\n"
368 " never be called by the user directly.\n";
370 static int handle_softhangup(int fd, int argc, char *argv[])
372 struct ast_channel *c=NULL;
374 return RESULT_SHOWUSAGE;
375 c = ast_channel_walk_locked(NULL);
377 if (!strcasecmp(c->name, argv[2])) {
378 ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
379 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
380 ast_mutex_unlock(&c->lock);
383 ast_mutex_unlock(&c->lock);
384 c = ast_channel_walk_locked(c);
387 ast_cli(fd, "%s is not a known channel\n", argv[2]);
388 return RESULT_SUCCESS;
391 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
393 static int handle_commandmatchesarray(int fd, int argc, char *argv[])
402 return RESULT_SHOWUSAGE;
403 buf = malloc(buflen);
405 return RESULT_FAILURE;
407 matches = ast_cli_completion_matches(argv[2], argv[3]);
409 for (x=0; matches[x]; x++) {
411 printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
413 if (len + strlen(matches[x]) >= buflen) {
414 buflen += strlen(matches[x]) * 3;
415 buf = realloc(buf, buflen);
417 len += sprintf( buf + len, "%s ", matches[x]);
424 printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
428 ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
431 ast_cli(fd, "NULL\n");
433 return RESULT_SUCCESS;
438 static int handle_commandnummatches(int fd, int argc, char *argv[])
443 return RESULT_SHOWUSAGE;
445 matches = ast_cli_generatornummatches(argv[2], argv[3]);
448 printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
450 ast_cli(fd, "%d", matches);
452 return RESULT_SUCCESS;
455 static int handle_commandcomplete(int fd, int argc, char *argv[])
459 printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
462 return RESULT_SHOWUSAGE;
463 buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
465 printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
471 ast_cli(fd, "NULL\n");
472 return RESULT_SUCCESS;
475 static int handle_debugchan(int fd, int argc, char *argv[])
477 struct ast_channel *c=NULL;
479 return RESULT_SHOWUSAGE;
480 c = ast_channel_walk_locked(NULL);
482 if (!strcasecmp(c->name, argv[2])) {
483 c->fin |= 0x80000000;
484 c->fout |= 0x80000000;
487 ast_mutex_unlock(&c->lock);
488 c = ast_channel_walk_locked(c);
491 ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
492 ast_mutex_unlock(&c->lock);
495 ast_cli(fd, "No such channel %s\n", argv[2]);
496 return RESULT_SUCCESS;
499 static int handle_nodebugchan(int fd, int argc, char *argv[])
501 struct ast_channel *c=NULL;
503 return RESULT_SHOWUSAGE;
504 c = ast_channel_walk_locked(NULL);
506 if (!strcasecmp(c->name, argv[3])) {
507 c->fin &= 0x7fffffff;
508 c->fout &= 0x7fffffff;
511 ast_mutex_unlock(&c->lock);
512 c = ast_channel_walk_locked(c);
515 ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
516 ast_mutex_unlock(&c->lock);
518 ast_cli(fd, "No such channel %s\n", argv[2]);
519 return RESULT_SUCCESS;
524 static int handle_showchan(int fd, int argc, char *argv[])
526 struct ast_channel *c=NULL;
528 return RESULT_SHOWUSAGE;
529 c = ast_channel_walk_locked(NULL);
531 if (!strcasecmp(c->name, argv[2])) {
541 " NativeFormat: %d\n"
544 "1st File Descriptor: %d\n"
546 " Frames out: %d%s\n"
547 " Time to Hangup: %ld\n"
553 " Pickup Group: %d\n"
557 " Blocking in: %s\n",
558 c->name, c->type, c->uniqueid,
559 (c->callerid ? c->callerid : "(N/A)"),
560 (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
561 c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
562 c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
563 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
564 ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
565 c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
566 ast_mutex_unlock(&c->lock);
569 ast_mutex_unlock(&c->lock);
570 c = ast_channel_walk_locked(c);
573 ast_cli(fd, "%s is not a known channel\n", argv[2]);
574 return RESULT_SUCCESS;
577 static char *complete_ch(char *line, char *word, int pos, int state)
579 struct ast_channel *c;
582 c = ast_channel_walk_locked(NULL);
584 if (!strncasecmp(word, c->name, strlen(word))) {
588 ast_mutex_unlock(&c->lock);
589 c = ast_channel_walk_locked(c);
592 ret = strdup(c->name);
593 ast_mutex_unlock(&c->lock);
599 static char *complete_fn(char *line, char *word, int pos, int state)
606 strncpy(filename, word, sizeof(filename)-1);
608 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
609 c = (char*)filename_completion_function(filename, state);
610 if (c && word[0] != '/')
611 c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
612 return c ? strdup(c) : c;
615 static int handle_help(int fd, int argc, char *argv[]);
617 static struct ast_cli_entry builtins[] = {
618 /* Keep alphabetized, with longer matches first (example: abcd before abc) */
619 { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
620 { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
621 { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
622 { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch },
623 { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
624 { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
625 { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch },
626 { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
627 { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
628 { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
629 { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch },
630 { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
631 { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
632 { { "show", "version", NULL }, handle_version, "Display version info", version_help },
633 { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch },
634 { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
635 { { NULL }, NULL, NULL, NULL }
638 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
643 struct ast_cli_entry *e=NULL;
644 for (x=0;builtins[x].cmda[0];x++) {
645 /* start optimistic */
647 for (y=0;match && cmds[y]; y++) {
648 /* If there are no more words in the candidate command, then we're
650 if (!builtins[x].cmda[y] && !exact)
652 /* If there are no more words in the command (and we're looking for
653 an exact match) or there is a difference between the two words,
654 then this is not a match */
655 if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
658 /* If more words are needed to complete the command then this is not
659 a candidate (unless we're looking for a really inexact answer */
660 if ((exact > -1) && builtins[x].cmda[y])
665 for (e=helpers;e;e=e->next) {
667 for (y=0;match && cmds[y]; y++) {
668 if (!e->cmda[y] && !exact)
670 if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
673 if ((exact > -1) && e->cmda[y])
681 static void join(char *dest, size_t destsize, char *w[])
684 /* Join words into a string */
685 if (!dest || destsize < 1) {
691 strncat(dest, " ", destsize - strlen(dest) - 1);
692 strncat(dest, w[x], destsize - strlen(dest) - 1);
696 static void join2(char *dest, size_t destsize, char *w[])
699 /* Join words into a string */
700 if (!dest || destsize < 1) {
705 strncat(dest, w[x], destsize - strlen(dest) - 1);
709 static char *find_best(char *argv[])
711 static char cmdline[80];
713 /* See how close we get, then print the */
714 char *myargv[AST_MAX_CMD_LEN];
715 for (x=0;x<AST_MAX_CMD_LEN;x++)
717 for (x=0;argv[x];x++) {
719 if (!find_cli(myargv, -1))
722 join(cmdline, sizeof(cmdline), myargv);
726 int ast_cli_unregister(struct ast_cli_entry *e)
728 struct ast_cli_entry *cur, *l=NULL;
729 ast_mutex_lock(&clilock);
734 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
748 ast_mutex_unlock(&clilock);
752 int ast_cli_register(struct ast_cli_entry *e)
754 struct ast_cli_entry *cur, *l=NULL;
755 char fulle[80] ="", fulltst[80] ="";
757 ast_mutex_lock(&clilock);
758 join2(fulle, sizeof(fulle), e->cmda);
759 if (find_cli(e->cmda, -1)) {
760 ast_mutex_unlock(&clilock);
761 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
766 join2(fulltst, sizeof(fulltst), cur->cmda);
767 len = strlen(fulltst);
768 if (strlen(fulle) < len)
770 if (strncasecmp(fulle, fulltst, len) < 0) {
790 ast_mutex_unlock(&clilock);
794 static int help_workhorse(int fd, char *match[])
800 struct ast_cli_entry *e, *e1, *e2;
804 join(matchstr, sizeof(matchstr), match);
805 while(e1->cmda[0] || e2) {
807 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
809 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
811 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
815 /* Increment by going to next */
823 /* Hide commands that start with '_' */
824 if (fullcmd[0] == '_')
827 if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
831 ast_cli(fd, "%25.25s %s\n", fullcmd, e->summary);
836 static int handle_help(int fd, int argc, char *argv[]) {
837 struct ast_cli_entry *e;
840 return RESULT_SHOWUSAGE;
842 e = find_cli(argv + 1, 1);
844 ast_cli(fd, e->usage);
846 if (find_cli(argv + 1, -1)) {
847 return help_workhorse(fd, argv + 1);
849 join(fullcmd, sizeof(fullcmd), argv+1);
850 ast_cli(fd, "No such command '%s'.\n", fullcmd);
854 return help_workhorse(fd, NULL);
856 return RESULT_SUCCESS;
859 static char *parse_args(char *s, int *max, char *argv[])
873 /* If it's escaped, put a literal quote */
878 if (quoted && whitespace) {
879 /* If we're starting a quote, coming off white space start a new word, too */
887 if (!quoted && !escaped) {
888 /* If we're not quoted, mark this as whitespace, and
889 end the previous argument */
893 /* Otherwise, just treat it as anything else */
897 /* If we're escaped, print a literal, otherwise enable escaping */
907 if (x >= AST_MAX_ARGS -1) {
908 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
911 /* Coming off of whitespace, start the next argument */
928 /* This returns the number of unique matches for the generator */
929 int ast_cli_generatornummatches(char *text, char *word)
931 int matches = 0, i = 0;
932 char *buf, *oldbuf = NULL;
935 while ( (buf = ast_cli_generator(text, word, i)) ) {
936 if (++i > 1 && strcmp(buf,oldbuf) == 0) {
946 char **ast_cli_completion_matches(char *text, char *word)
948 char **match_list = NULL, *retstr, *prevstr;
949 size_t match_list_len, max_equal, which, i;
953 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
954 if (matches + 1 >= match_list_len) {
955 match_list_len <<= 1;
956 match_list = realloc(match_list, match_list_len * sizeof(char *));
958 match_list[++matches] = retstr;
962 return (char **) NULL;
965 prevstr = match_list[1];
966 max_equal = strlen(prevstr);
967 for (; which <= matches; which++) {
968 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
973 retstr = malloc(max_equal + 1);
974 (void) strncpy(retstr, match_list[1], max_equal);
975 retstr[max_equal] = '\0';
976 match_list[0] = retstr;
978 if (matches + 1 >= match_list_len)
979 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
980 match_list[matches + 1] = (char *) NULL;
985 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
987 char *argv[AST_MAX_ARGS];
988 struct ast_cli_entry *e, *e1, *e2;
997 if ((dup = parse_args(text, &x, argv))) {
998 join(matchstr, sizeof(matchstr), argv);
1000 ast_mutex_lock(&clilock);
1003 while(e1->cmda[0] || e2) {
1005 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
1007 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
1009 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
1013 /* Increment by going to next */
1021 if ((fullcmd[0] != '_') && !strncasecmp(text, fullcmd, strlen(text))) {
1022 /* We contain the first part of one or more commands */
1024 if (matchnum > state) {
1025 /* Now, what we're supposed to return is the next word... */
1026 if (!ast_strlen_zero(word) && x>0) {
1033 ast_mutex_unlock(&clilock);
1035 return res ? strdup(res) : NULL;
1039 if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
1040 /* We have a command in its entirity within us -- theoretically only one
1041 command can have this occur */
1042 fullcmd = e->generator(text, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
1044 ast_mutex_unlock(&clilock);
1051 ast_mutex_unlock(&clilock);
1057 char *ast_cli_generator(char *text, char *word, int state)
1059 return __ast_cli_generator(text, word, state, 1);
1062 int ast_cli_command(int fd, char *s)
1064 char *argv[AST_MAX_ARGS];
1065 struct ast_cli_entry *e;
1069 if ((dup = parse_args(s, &x, argv))) {
1070 /* We need at least one entry, or ignore */
1072 ast_mutex_lock(&clilock);
1073 e = find_cli(argv, 0);
1076 ast_mutex_unlock(&clilock);
1078 switch(e->handler(fd, x, argv)) {
1079 case RESULT_SHOWUSAGE:
1080 ast_cli(fd, e->usage);
1084 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
1086 ast_mutex_lock(&clilock);
1088 ast_mutex_unlock(&clilock);
1093 ast_log(LOG_WARNING, "Out of memory\n");