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[])
114 return RESULT_SHOWUSAGE;
117 ast_module_reload(argv[x]);
119 ast_module_reload(NULL);
120 return RESULT_SUCCESS;
123 static int handle_set_verbose(int fd, int argc, char *argv[])
126 /* Has a hidden 'at least' argument */
127 if ((argc != 3) && (argc != 4))
128 return RESULT_SHOWUSAGE;
129 if ((argc == 4) && strcasecmp(argv[2], "atleast"))
130 return RESULT_SHOWUSAGE;
132 option_verbose = atoi(argv[2]);
135 if (val > option_verbose)
136 option_verbose = val;
138 return RESULT_SUCCESS;
141 static int handle_unload(int fd, int argc, char *argv[])
144 int force=AST_FORCE_SOFT;
146 return RESULT_SHOWUSAGE;
147 for (x=1;x<argc;x++) {
148 if (argv[x][0] == '-') {
151 force = AST_FORCE_FIRM;
154 force = AST_FORCE_HARD;
157 return RESULT_SHOWUSAGE;
159 } else if (x != argc - 1)
160 return RESULT_SHOWUSAGE;
161 else if (ast_unload_resource(argv[x], force)) {
162 ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
163 return RESULT_FAILURE;
166 return RESULT_SUCCESS;
169 #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n"
170 #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
172 AST_MUTEX_DEFINE_STATIC(climodentrylock);
173 static int climodentryfd = -1;
175 static int modlist_modentry(char *module, char *description, int usecnt)
177 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
181 static char modlist_help[] =
182 "Usage: show modules\n"
183 " Shows Asterisk modules currently in use, and usage "
186 static char version_help[] =
187 "Usage: show version\n"
188 " Shows Asterisk version information.\n ";
190 static char *format_uptimestr(time_t timeval)
192 int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
193 char timestr[256]="";
198 #define MINUTE (SECOND*60)
199 #define HOUR (MINUTE*60)
200 #define DAY (HOUR*24)
202 #define YEAR (DAY*365)
203 #define ESS(x) ((x == 1) ? "" : "s")
205 maxbytes = sizeof(timestr);
208 if (timeval > YEAR) {
209 years = (timeval / YEAR);
210 timeval -= (years * YEAR);
212 snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
213 bytes = strlen(timestr + offset);
218 if (timeval > WEEK) {
219 weeks = (timeval / WEEK);
220 timeval -= (weeks * WEEK);
222 snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
223 bytes = strlen(timestr + offset);
229 days = (timeval / DAY);
230 timeval -= (days * DAY);
232 snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
233 bytes = strlen(timestr + offset);
238 if (timeval > HOUR) {
239 hours = (timeval / HOUR);
240 timeval -= (hours * HOUR);
242 snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
243 bytes = strlen(timestr + offset);
248 if (timeval > MINUTE) {
249 mins = (timeval / MINUTE);
250 timeval -= (mins * MINUTE);
252 snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
253 bytes = strlen(timestr + offset);
261 snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
264 return timestr ? strdup(timestr) : NULL;
267 static int handle_showuptime(int fd, int argc, char *argv[])
269 time_t curtime, tmptime;
273 if (ast_startuptime) {
274 tmptime = curtime - ast_startuptime;
275 timestr = format_uptimestr(tmptime);
277 ast_cli(fd, "System uptime: %s\n", timestr);
281 if (ast_lastreloadtime) {
282 tmptime = curtime - ast_lastreloadtime;
283 timestr = format_uptimestr(tmptime);
285 ast_cli(fd, "Last reload: %s\n", timestr);
289 return RESULT_SUCCESS;
292 static int handle_modlist(int fd, int argc, char *argv[])
295 return RESULT_SHOWUSAGE;
296 ast_mutex_lock(&climodentrylock);
298 ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
299 ast_update_module_list(modlist_modentry);
301 ast_mutex_unlock(&climodentrylock);
302 return RESULT_SUCCESS;
305 static int handle_version(int fd, int argc, char *argv[])
308 return RESULT_SHOWUSAGE;
309 ast_cli(fd, "%s\n", VERSION_INFO);
310 return RESULT_SUCCESS;
312 static int handle_chanlist(int fd, int argc, char *argv[])
314 #define FORMAT_STRING "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
315 #define FORMAT_STRING2 "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
316 #define CONCISE_FORMAT_STRING "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"
318 struct ast_channel *c=NULL;
321 if (argc < 2 || argc > 3)
322 return RESULT_SHOWUSAGE;
324 concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
325 c = ast_channel_walk_locked(NULL);
327 ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
330 ast_cli(fd, CONCISE_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 : "" ): "",
332 (c->callerid && !ast_strlen_zero(c->callerid)) ? c->callerid : "",
333 (c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
335 ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
336 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "(Empty)" ): "(None)");
339 ast_mutex_unlock(&c->lock);
340 c = ast_channel_walk_locked(c);
343 ast_cli(fd, "%d active channel(s)\n", numchans);
344 return RESULT_SUCCESS;
347 static char showchan_help[] =
348 "Usage: show channel <channel>\n"
349 " Shows lots of information about the specified channel.\n";
351 static char debugchan_help[] =
352 "Usage: debug channel <channel>\n"
353 " Enables debugging on a specific channel.\n";
355 static char nodebugchan_help[] =
356 "Usage: no debug channel <channel>\n"
357 " Disables debugging on a specific channel.\n";
359 static char commandcomplete_help[] =
360 "Usage: _command complete \"<line>\" text state\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 commandnummatches_help[] =
365 "Usage: _command nummatches \"<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 char commandmatchesarray_help[] =
370 "Usage: _command matchesarray \"<line>\" text \n"
371 " This function is used internally to help with command completion and should.\n"
372 " never be called by the user directly.\n";
374 static int handle_softhangup(int fd, int argc, char *argv[])
376 struct ast_channel *c=NULL;
378 return RESULT_SHOWUSAGE;
379 c = ast_channel_walk_locked(NULL);
381 if (!strcasecmp(c->name, argv[2])) {
382 ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
383 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
384 ast_mutex_unlock(&c->lock);
387 ast_mutex_unlock(&c->lock);
388 c = ast_channel_walk_locked(c);
391 ast_cli(fd, "%s is not a known channel\n", argv[2]);
392 return RESULT_SUCCESS;
395 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
397 static int handle_commandmatchesarray(int fd, int argc, char *argv[])
406 return RESULT_SHOWUSAGE;
407 buf = malloc(buflen);
409 return RESULT_FAILURE;
411 matches = ast_cli_completion_matches(argv[2], argv[3]);
413 for (x=0; matches[x]; x++) {
415 printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
417 if (len + strlen(matches[x]) >= buflen) {
418 buflen += strlen(matches[x]) * 3;
419 buf = realloc(buf, buflen);
421 len += sprintf( buf + len, "%s ", matches[x]);
428 printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
432 ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
435 ast_cli(fd, "NULL\n");
437 return RESULT_SUCCESS;
442 static int handle_commandnummatches(int fd, int argc, char *argv[])
447 return RESULT_SHOWUSAGE;
449 matches = ast_cli_generatornummatches(argv[2], argv[3]);
452 printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
454 ast_cli(fd, "%d", matches);
456 return RESULT_SUCCESS;
459 static int handle_commandcomplete(int fd, int argc, char *argv[])
463 printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
466 return RESULT_SHOWUSAGE;
467 buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
469 printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
475 ast_cli(fd, "NULL\n");
476 return RESULT_SUCCESS;
479 static int handle_debugchan(int fd, int argc, char *argv[])
481 struct ast_channel *c=NULL;
483 return RESULT_SHOWUSAGE;
484 c = ast_channel_walk_locked(NULL);
486 if (!strcasecmp(c->name, argv[2])) {
487 c->fin |= 0x80000000;
488 c->fout |= 0x80000000;
491 ast_mutex_unlock(&c->lock);
492 c = ast_channel_walk_locked(c);
495 ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
496 ast_mutex_unlock(&c->lock);
499 ast_cli(fd, "No such channel %s\n", argv[2]);
500 return RESULT_SUCCESS;
503 static int handle_nodebugchan(int fd, int argc, char *argv[])
505 struct ast_channel *c=NULL;
507 return RESULT_SHOWUSAGE;
508 c = ast_channel_walk_locked(NULL);
510 if (!strcasecmp(c->name, argv[3])) {
511 c->fin &= 0x7fffffff;
512 c->fout &= 0x7fffffff;
515 ast_mutex_unlock(&c->lock);
516 c = ast_channel_walk_locked(c);
519 ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
520 ast_mutex_unlock(&c->lock);
522 ast_cli(fd, "No such channel %s\n", argv[2]);
523 return RESULT_SUCCESS;
528 static int handle_showchan(int fd, int argc, char *argv[])
530 struct ast_channel *c=NULL;
532 long elapsed_seconds=0;
534 return RESULT_SHOWUSAGE;
535 gettimeofday(&now, NULL);
536 c = ast_channel_walk_locked(NULL);
538 if (!strcasecmp(c->name, argv[2])) {
540 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
551 " NativeFormat: %d\n"
554 "1st File Descriptor: %d\n"
556 " Frames out: %d%s\n"
557 " Time to Hangup: %ld\n"
558 "Elapsed Seconds: %ld\n"
564 " Pickup Group: %d\n"
568 " Blocking in: %s\n",
569 c->name, c->type, c->uniqueid,
570 (c->callerid ? c->callerid : "(N/A)"),
571 (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
572 c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
573 c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
574 (long)elapsed_seconds,
575 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
576 ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
577 c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
578 ast_mutex_unlock(&c->lock);
581 ast_mutex_unlock(&c->lock);
582 c = ast_channel_walk_locked(c);
585 ast_cli(fd, "%s is not a known channel\n", argv[2]);
586 return RESULT_SUCCESS;
589 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
591 struct ast_channel *c;
596 c = ast_channel_walk_locked(NULL);
598 if (!strncasecmp(word, c->name, strlen(word))) {
602 ast_mutex_unlock(&c->lock);
603 c = ast_channel_walk_locked(c);
606 ret = strdup(c->name);
607 ast_mutex_unlock(&c->lock);
613 static char *complete_ch_3(char *line, char *word, int pos, int state)
615 return complete_ch_helper(line, word, pos, state, 2);
618 static char *complete_ch_4(char *line, char *word, int pos, int state)
620 return complete_ch_helper(line, word, pos, state, 3);
623 static char *complete_fn(char *line, char *word, int pos, int state)
630 strncpy(filename, word, sizeof(filename)-1);
632 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
633 c = (char*)filename_completion_function(filename, state);
634 if (c && word[0] != '/')
635 c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
636 return c ? strdup(c) : c;
639 static int handle_help(int fd, int argc, char *argv[]);
641 static struct ast_cli_entry builtins[] = {
642 /* Keep alphabetized, with longer matches first (example: abcd before abc) */
643 { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
644 { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
645 { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
646 { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch_3 },
647 { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
648 { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
649 { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch_4 },
650 { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
651 { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
652 { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
653 { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch_3 },
654 { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
655 { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
656 { { "show", "version", NULL }, handle_version, "Display version info", version_help },
657 { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch_3 },
658 { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
659 { { NULL }, NULL, NULL, NULL }
662 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
667 struct ast_cli_entry *e=NULL;
668 for (x=0;builtins[x].cmda[0];x++) {
669 /* start optimistic */
671 for (y=0;match && cmds[y]; y++) {
672 /* If there are no more words in the candidate command, then we're
674 if (!builtins[x].cmda[y] && !exact)
676 /* If there are no more words in the command (and we're looking for
677 an exact match) or there is a difference between the two words,
678 then this is not a match */
679 if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
682 /* If more words are needed to complete the command then this is not
683 a candidate (unless we're looking for a really inexact answer */
684 if ((exact > -1) && builtins[x].cmda[y])
689 for (e=helpers;e;e=e->next) {
691 for (y=0;match && cmds[y]; y++) {
692 if (!e->cmda[y] && !exact)
694 if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
697 if ((exact > -1) && e->cmda[y])
705 static void join(char *dest, size_t destsize, char *w[])
708 /* Join words into a string */
709 if (!dest || destsize < 1) {
715 strncat(dest, " ", destsize - strlen(dest) - 1);
716 strncat(dest, w[x], destsize - strlen(dest) - 1);
720 static void join2(char *dest, size_t destsize, char *w[])
723 /* Join words into a string */
724 if (!dest || destsize < 1) {
729 strncat(dest, w[x], destsize - strlen(dest) - 1);
733 static char *find_best(char *argv[])
735 static char cmdline[80];
737 /* See how close we get, then print the */
738 char *myargv[AST_MAX_CMD_LEN];
739 for (x=0;x<AST_MAX_CMD_LEN;x++)
741 for (x=0;argv[x];x++) {
743 if (!find_cli(myargv, -1))
746 join(cmdline, sizeof(cmdline), myargv);
750 int ast_cli_unregister(struct ast_cli_entry *e)
752 struct ast_cli_entry *cur, *l=NULL;
753 ast_mutex_lock(&clilock);
758 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
772 ast_mutex_unlock(&clilock);
776 int ast_cli_register(struct ast_cli_entry *e)
778 struct ast_cli_entry *cur, *l=NULL;
779 char fulle[80] ="", fulltst[80] ="";
781 ast_mutex_lock(&clilock);
782 join2(fulle, sizeof(fulle), e->cmda);
783 if (find_cli(e->cmda, -1)) {
784 ast_mutex_unlock(&clilock);
785 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
790 join2(fulltst, sizeof(fulltst), cur->cmda);
791 len = strlen(fulltst);
792 if (strlen(fulle) < len)
794 if (strncasecmp(fulle, fulltst, len) < 0) {
814 ast_mutex_unlock(&clilock);
818 static int help_workhorse(int fd, char *match[])
824 struct ast_cli_entry *e, *e1, *e2;
828 join(matchstr, sizeof(matchstr), match);
829 while(e1->cmda[0] || e2) {
831 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
833 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
835 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
839 /* Increment by going to next */
847 /* Hide commands that start with '_' */
848 if (fullcmd[0] == '_')
851 if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
855 ast_cli(fd, "%25.25s %s\n", fullcmd, e->summary);
860 static int handle_help(int fd, int argc, char *argv[]) {
861 struct ast_cli_entry *e;
864 return RESULT_SHOWUSAGE;
866 e = find_cli(argv + 1, 1);
868 ast_cli(fd, e->usage);
870 if (find_cli(argv + 1, -1)) {
871 return help_workhorse(fd, argv + 1);
873 join(fullcmd, sizeof(fullcmd), argv+1);
874 ast_cli(fd, "No such command '%s'.\n", fullcmd);
878 return help_workhorse(fd, NULL);
880 return RESULT_SUCCESS;
883 static char *parse_args(char *s, int *max, char *argv[])
897 /* If it's escaped, put a literal quote */
902 if (quoted && whitespace) {
903 /* If we're starting a quote, coming off white space start a new word, too */
911 if (!quoted && !escaped) {
912 /* If we're not quoted, mark this as whitespace, and
913 end the previous argument */
917 /* Otherwise, just treat it as anything else */
921 /* If we're escaped, print a literal, otherwise enable escaping */
931 if (x >= AST_MAX_ARGS -1) {
932 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
935 /* Coming off of whitespace, start the next argument */
952 /* This returns the number of unique matches for the generator */
953 int ast_cli_generatornummatches(char *text, char *word)
955 int matches = 0, i = 0;
956 char *buf, *oldbuf = NULL;
959 while ( (buf = ast_cli_generator(text, word, i)) ) {
960 if (++i > 1 && strcmp(buf,oldbuf) == 0) {
970 char **ast_cli_completion_matches(char *text, char *word)
972 char **match_list = NULL, *retstr, *prevstr;
973 size_t match_list_len, max_equal, which, i;
977 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
978 if (matches + 1 >= match_list_len) {
979 match_list_len <<= 1;
980 match_list = realloc(match_list, match_list_len * sizeof(char *));
982 match_list[++matches] = retstr;
986 return (char **) NULL;
989 prevstr = match_list[1];
990 max_equal = strlen(prevstr);
991 for (; which <= matches; which++) {
992 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
997 retstr = malloc(max_equal + 1);
998 (void) strncpy(retstr, match_list[1], max_equal);
999 retstr[max_equal] = '\0';
1000 match_list[0] = retstr;
1002 if (matches + 1 >= match_list_len)
1003 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
1004 match_list[matches + 1] = (char *) NULL;
1006 return (match_list);
1009 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
1011 char *argv[AST_MAX_ARGS];
1012 struct ast_cli_entry *e, *e1, *e2;
1021 if ((dup = parse_args(text, &x, argv))) {
1022 join(matchstr, sizeof(matchstr), argv);
1024 ast_mutex_lock(&clilock);
1027 while(e1->cmda[0] || e2) {
1029 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
1031 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
1033 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
1037 /* Increment by going to next */
1045 if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
1046 /* We contain the first part of one or more commands */
1048 if (matchnum > state) {
1049 /* Now, what we're supposed to return is the next word... */
1050 if (!ast_strlen_zero(word) && x>0) {
1057 ast_mutex_unlock(&clilock);
1059 return res ? strdup(res) : NULL;
1063 if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
1064 /* We have a command in its entirity within us -- theoretically only one
1065 command can have this occur */
1066 fullcmd = e->generator(matchstr, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
1068 ast_mutex_unlock(&clilock);
1075 ast_mutex_unlock(&clilock);
1081 char *ast_cli_generator(char *text, char *word, int state)
1083 return __ast_cli_generator(text, word, state, 1);
1086 int ast_cli_command(int fd, char *s)
1088 char *argv[AST_MAX_ARGS];
1089 struct ast_cli_entry *e;
1093 if ((dup = parse_args(s, &x, argv))) {
1094 /* We need at least one entry, or ignore */
1096 ast_mutex_lock(&clilock);
1097 e = find_cli(argv, 0);
1100 ast_mutex_unlock(&clilock);
1102 switch(e->handler(fd, x, argv)) {
1103 case RESULT_SHOWUSAGE:
1104 ast_cli(fd, e->usage);
1108 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
1110 ast_mutex_lock(&clilock);
1112 ast_mutex_unlock(&clilock);
1117 ast_log(LOG_WARNING, "Out of memory\n");