00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <unistd.h>
00015 #include <stdlib.h>
00016 #include <asterisk/logger.h>
00017 #include <asterisk/options.h>
00018 #include <asterisk/cli.h>
00019 #include <asterisk/module.h>
00020 #include <asterisk/channel.h>
00021 #include <asterisk/channel_pvt.h>
00022 #include <asterisk/manager.h>
00023 #include <asterisk/utils.h>
00024 #include <asterisk/lock.h>
00025 #include <sys/signal.h>
00026 #include <stdio.h>
00027 #include <signal.h>
00028 #include <string.h>
00029
00030 #include "readline/readline.h"
00031
00032 #include "asterisk.h"
00033 #include "build.h"
00034 #include "astconf.h"
00035
00036 #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
00037 " on a " BUILD_MACHINE " running " BUILD_OS
00038
00039 void ast_cli(int fd, char *fmt, ...)
00040 {
00041 char *stuff;
00042 int res = 0;
00043
00044 va_list ap;
00045 va_start(ap, fmt);
00046 res = vasprintf(&stuff, fmt, ap);
00047 va_end(ap);
00048 if (res == -1) {
00049 ast_log(LOG_ERROR, "Out of memory\n");
00050 } else {
00051 ast_carefulwrite(fd, stuff, strlen(stuff), 100);
00052 free(stuff);
00053 }
00054 }
00055
00056 AST_MUTEX_DEFINE_STATIC(clilock);
00057
00058 struct ast_cli_entry *helpers = NULL;
00059
00060 static char load_help[] =
00061 "Usage: load <module name>\n"
00062 " Loads the specified module into Asterisk.\n";
00063
00064 static char unload_help[] =
00065 "Usage: unload [-f|-h] <module name>\n"
00066 " Unloads the specified module from Asterisk. The -f\n"
00067 " option causes the module to be unloaded even if it is\n"
00068 " in use (may cause a crash) and the -h module causes the\n"
00069 " module to be unloaded even if the module says it cannot, \n"
00070 " which almost always will cause a crash.\n";
00071
00072 static char help_help[] =
00073 "Usage: help [topic]\n"
00074 " When called with a topic as an argument, displays usage\n"
00075 " information on the given command. If called without a\n"
00076 " topic, it provides a list of commands.\n";
00077
00078 static char chanlist_help[] =
00079 "Usage: show channels [concise]\n"
00080 " Lists currently defined channels and some information about\n"
00081 " them. If 'concise' is specified, format is abridged and in\n"
00082 " a more easily machine parsable format\n";
00083
00084 static char reload_help[] =
00085 "Usage: reload [module ...]\n"
00086 " Reloads configuration files for all listed modules which support\n"
00087 " reloading, or for all supported modules if none are listed.\n";
00088
00089 static char set_verbose_help[] =
00090 "Usage: set verbose <level>\n"
00091 " Sets level of verbose messages to be displayed. 0 means\n"
00092 " no messages should be displayed. Equivalent to -v[v[v...]]\n"
00093 " on startup\n";
00094
00095 static char set_debug_help[] =
00096 "Usage: set debug <level>\n"
00097 " Sets level of core debug messages to be displayed. 0 means\n"
00098 " no messages should be displayed. Equivalent to -d[d[d...]]\n"
00099 " on startup.\n";
00100
00101 static char softhangup_help[] =
00102 "Usage: soft hangup <channel>\n"
00103 " Request that a channel be hung up. The hangup takes effect\n"
00104 " the next time the driver reads or writes from the channel\n";
00105
00106 static int handle_load(int fd, int argc, char *argv[])
00107 {
00108 if (argc != 2)
00109 return RESULT_SHOWUSAGE;
00110 if (ast_load_resource(argv[1])) {
00111 ast_cli(fd, "Unable to load module %s\n", argv[1]);
00112 return RESULT_FAILURE;
00113 }
00114 return RESULT_SUCCESS;
00115 }
00116
00117 static int handle_reload(int fd, int argc, char *argv[])
00118 {
00119 int x;
00120 if (argc < 1)
00121 return RESULT_SHOWUSAGE;
00122 if (argc > 1) {
00123 for (x=1;x<argc;x++)
00124 ast_module_reload(argv[x]);
00125 } else
00126 ast_module_reload(NULL);
00127 return RESULT_SUCCESS;
00128 }
00129
00130 static int handle_set_verbose(int fd, int argc, char *argv[])
00131 {
00132 int val = 0;
00133 int oldval = 0;
00134
00135 if ((argc != 3) && (argc != 4))
00136 return RESULT_SHOWUSAGE;
00137 if ((argc == 4) && strcasecmp(argv[2], "atleast"))
00138 return RESULT_SHOWUSAGE;
00139 oldval = option_verbose;
00140 if (argc == 3)
00141 option_verbose = atoi(argv[2]);
00142 else {
00143 val = atoi(argv[3]);
00144 if (val > option_verbose)
00145 option_verbose = val;
00146 }
00147 if (oldval != option_verbose && option_verbose > 0)
00148 ast_cli(fd, "Verbosity was %d and is now %d\n", oldval, option_verbose);
00149 else if (oldval > 0 && option_verbose > 0)
00150 ast_cli(fd, "Verbosity is atleast %d\n", option_verbose);
00151 else if (oldval > 0 && option_debug == 0)
00152 ast_cli(fd, "Verbosity is now OFF\n");
00153 return RESULT_SUCCESS;
00154 }
00155
00156 static int handle_set_debug(int fd, int argc, char *argv[])
00157 {
00158 int val = 0;
00159 int oldval = 0;
00160
00161 if ((argc != 3) && (argc != 4))
00162 return RESULT_SHOWUSAGE;
00163 if ((argc == 4) && strcasecmp(argv[2], "atleast"))
00164 return RESULT_SHOWUSAGE;
00165 oldval = option_debug;
00166 if (argc == 3)
00167 option_debug = atoi(argv[2]);
00168 else {
00169 val = atoi(argv[3]);
00170 if (val > option_debug)
00171 option_debug = val;
00172 }
00173 if (oldval != option_debug && option_debug > 0)
00174 ast_cli(fd, "Core debug was %d and is now %d\n", oldval, option_debug);
00175 else if (oldval > 0 && option_debug > 0)
00176 ast_cli(fd, "Core debug is atleast %d\n", option_debug);
00177 else if (oldval > 0 && option_debug == 0)
00178 ast_cli(fd, "Core debug is now OFF\n");
00179 return RESULT_SUCCESS;
00180 }
00181
00182 static int handle_unload(int fd, int argc, char *argv[])
00183 {
00184 int x;
00185 int force=AST_FORCE_SOFT;
00186 if (argc < 2)
00187 return RESULT_SHOWUSAGE;
00188 for (x=1;x<argc;x++) {
00189 if (argv[x][0] == '-') {
00190 switch(argv[x][1]) {
00191 case 'f':
00192 force = AST_FORCE_FIRM;
00193 break;
00194 case 'h':
00195 force = AST_FORCE_HARD;
00196 break;
00197 default:
00198 return RESULT_SHOWUSAGE;
00199 }
00200 } else if (x != argc - 1)
00201 return RESULT_SHOWUSAGE;
00202 else if (ast_unload_resource(argv[x], force)) {
00203 ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
00204 return RESULT_FAILURE;
00205 }
00206 }
00207 return RESULT_SUCCESS;
00208 }
00209
00210 #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n"
00211 #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
00212
00213 AST_MUTEX_DEFINE_STATIC(climodentrylock);
00214 static int climodentryfd = -1;
00215
00216 static int modlist_modentry(char *module, char *description, int usecnt)
00217 {
00218 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
00219 return 0;
00220 }
00221
00222 static char modlist_help[] =
00223 "Usage: show modules\n"
00224 " Shows Asterisk modules currently in use, and usage "
00225 "statistics.\n";
00226
00227 static char version_help[] =
00228 "Usage: show version\n"
00229 " Shows Asterisk version information.\n ";
00230
00231 static char *format_uptimestr(time_t timeval)
00232 {
00233 int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
00234 char timestr[256]="";
00235 int bytes = 0;
00236 int maxbytes = 0;
00237 int offset = 0;
00238 #define SECOND (1)
00239 #define MINUTE (SECOND*60)
00240 #define HOUR (MINUTE*60)
00241 #define DAY (HOUR*24)
00242 #define WEEK (DAY*7)
00243 #define YEAR (DAY*365)
00244 #define ESS(x) ((x == 1) ? "" : "s")
00245
00246 maxbytes = sizeof(timestr);
00247 if (timeval < 0)
00248 return NULL;
00249 if (timeval > YEAR) {
00250 years = (timeval / YEAR);
00251 timeval -= (years * YEAR);
00252 if (years > 0) {
00253 snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
00254 bytes = strlen(timestr + offset);
00255 offset += bytes;
00256 maxbytes -= bytes;
00257 }
00258 }
00259 if (timeval > WEEK) {
00260 weeks = (timeval / WEEK);
00261 timeval -= (weeks * WEEK);
00262 if (weeks > 0) {
00263 snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
00264 bytes = strlen(timestr + offset);
00265 offset += bytes;
00266 maxbytes -= bytes;
00267 }
00268 }
00269 if (timeval > DAY) {
00270 days = (timeval / DAY);
00271 timeval -= (days * DAY);
00272 if (days > 0) {
00273 snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
00274 bytes = strlen(timestr + offset);
00275 offset += bytes;
00276 maxbytes -= bytes;
00277 }
00278 }
00279 if (timeval > HOUR) {
00280 hours = (timeval / HOUR);
00281 timeval -= (hours * HOUR);
00282 if (hours > 0) {
00283 snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
00284 bytes = strlen(timestr + offset);
00285 offset += bytes;
00286 maxbytes -= bytes;
00287 }
00288 }
00289 if (timeval > MINUTE) {
00290 mins = (timeval / MINUTE);
00291 timeval -= (mins * MINUTE);
00292 if (mins > 0) {
00293 snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
00294 bytes = strlen(timestr + offset);
00295 offset += bytes;
00296 maxbytes -= bytes;
00297 }
00298 }
00299 secs = timeval;
00300
00301 if (secs > 0) {
00302 snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
00303 }
00304
00305 return timestr ? strdup(timestr) : NULL;
00306 }
00307
00308 static int handle_showuptime(int fd, int argc, char *argv[])
00309 {
00310 time_t curtime, tmptime;
00311 char *timestr;
00312
00313 time(&curtime);
00314 if (ast_startuptime) {
00315 tmptime = curtime - ast_startuptime;
00316 timestr = format_uptimestr(tmptime);
00317 if (timestr) {
00318 ast_cli(fd, "System uptime: %s\n", timestr);
00319 free(timestr);
00320 }
00321 }
00322 if (ast_lastreloadtime) {
00323 tmptime = curtime - ast_lastreloadtime;
00324 timestr = format_uptimestr(tmptime);
00325 if (timestr) {
00326 ast_cli(fd, "Last reload: %s\n", timestr);
00327 free(timestr);
00328 }
00329 }
00330 return RESULT_SUCCESS;
00331 }
00332
00333 static int handle_modlist(int fd, int argc, char *argv[])
00334 {
00335 if (argc != 2)
00336 return RESULT_SHOWUSAGE;
00337 ast_mutex_lock(&climodentrylock);
00338 climodentryfd = fd;
00339 ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
00340 ast_update_module_list(modlist_modentry);
00341 climodentryfd = -1;
00342 ast_mutex_unlock(&climodentrylock);
00343 return RESULT_SUCCESS;
00344 }
00345
00346 static int handle_version(int fd, int argc, char *argv[])
00347 {
00348 if (argc != 2)
00349 return RESULT_SHOWUSAGE;
00350 ast_cli(fd, "%s\n", VERSION_INFO);
00351 return RESULT_SUCCESS;
00352 }
00353 static int handle_chanlist(int fd, int argc, char *argv[])
00354 {
00355 #define FORMAT_STRING "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
00356 #define FORMAT_STRING2 "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
00357 #define CONCISE_FORMAT_STRING "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"
00358
00359 struct ast_channel *c=NULL;
00360 int numchans = 0;
00361 int concise = 0;
00362 if (argc < 2 || argc > 3)
00363 return RESULT_SHOWUSAGE;
00364
00365 concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
00366 c = ast_channel_walk_locked(NULL);
00367 if(!concise)
00368 ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
00369 while(c) {
00370 if(concise)
00371 ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00372 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
00373 (c->callerid && !ast_strlen_zero(c->callerid)) ? c->callerid : "",
00374 (c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
00375 else
00376 ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
00377 c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "(Empty)" ): "(None)");
00378
00379 numchans++;
00380 ast_mutex_unlock(&c->lock);
00381 c = ast_channel_walk_locked(c);
00382 }
00383 if(!concise)
00384 ast_cli(fd, "%d active channel(s)\n", numchans);
00385 return RESULT_SUCCESS;
00386 }
00387
00388 static char showchan_help[] =
00389 "Usage: show channel <channel>\n"
00390 " Shows lots of information about the specified channel.\n";
00391
00392 static char debugchan_help[] =
00393 "Usage: debug channel <channel>\n"
00394 " Enables debugging on a specific channel.\n";
00395
00396 static char nodebugchan_help[] =
00397 "Usage: no debug channel <channel>\n"
00398 " Disables debugging on a specific channel.\n";
00399
00400 static char commandcomplete_help[] =
00401 "Usage: _command complete \"<line>\" text state\n"
00402 " This function is used internally to help with command completion and should.\n"
00403 " never be called by the user directly.\n";
00404
00405 static char commandnummatches_help[] =
00406 "Usage: _command nummatches \"<line>\" text \n"
00407 " This function is used internally to help with command completion and should.\n"
00408 " never be called by the user directly.\n";
00409
00410 static char commandmatchesarray_help[] =
00411 "Usage: _command matchesarray \"<line>\" text \n"
00412 " This function is used internally to help with command completion and should.\n"
00413 " never be called by the user directly.\n";
00414
00415 static int handle_softhangup(int fd, int argc, char *argv[])
00416 {
00417 struct ast_channel *c=NULL;
00418 if (argc != 3)
00419 return RESULT_SHOWUSAGE;
00420 c = ast_channel_walk_locked(NULL);
00421 while(c) {
00422 if (!strcasecmp(c->name, argv[2])) {
00423 ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
00424 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
00425 ast_mutex_unlock(&c->lock);
00426 break;
00427 }
00428 ast_mutex_unlock(&c->lock);
00429 c = ast_channel_walk_locked(c);
00430 }
00431 if (!c)
00432 ast_cli(fd, "%s is not a known channel\n", argv[2]);
00433 return RESULT_SUCCESS;
00434 }
00435
00436 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
00437
00438 static int handle_commandmatchesarray(int fd, int argc, char *argv[])
00439 {
00440 char *buf;
00441 int buflen = 2048;
00442 int len = 0;
00443 char **matches;
00444 int x;
00445
00446 if (argc != 4)
00447 return RESULT_SHOWUSAGE;
00448 buf = malloc(buflen);
00449 if (!buf)
00450 return RESULT_FAILURE;
00451 buf[len] = '\0';
00452 matches = ast_cli_completion_matches(argv[2], argv[3]);
00453 if (matches) {
00454 for (x=0; matches[x]; x++) {
00455 #if 0
00456 printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
00457 #endif
00458 if (len + strlen(matches[x]) >= buflen) {
00459 buflen += strlen(matches[x]) * 3;
00460 buf = realloc(buf, buflen);
00461 }
00462 len += sprintf( buf + len, "%s ", matches[x]);
00463 free(matches[x]);
00464 matches[x] = NULL;
00465 }
00466 free(matches);
00467 }
00468 #if 0
00469 printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
00470 #endif
00471
00472 if (buf) {
00473 ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
00474 free(buf);
00475 } else
00476 ast_cli(fd, "NULL\n");
00477
00478 return RESULT_SUCCESS;
00479 }
00480
00481
00482
00483 static int handle_commandnummatches(int fd, int argc, char *argv[])
00484 {
00485 int matches = 0;
00486
00487 if (argc != 4)
00488 return RESULT_SHOWUSAGE;
00489
00490 matches = ast_cli_generatornummatches(argv[2], argv[3]);
00491
00492 #if 0
00493 printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
00494 #endif
00495 ast_cli(fd, "%d", matches);
00496
00497 return RESULT_SUCCESS;
00498 }
00499
00500 static int handle_commandcomplete(int fd, int argc, char *argv[])
00501 {
00502 char *buf;
00503 #if 0
00504 printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
00505 #endif
00506 if (argc != 5)
00507 return RESULT_SHOWUSAGE;
00508 buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
00509 #if 0
00510 printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
00511 #endif
00512 if (buf) {
00513 ast_cli(fd, buf);
00514 free(buf);
00515 } else
00516 ast_cli(fd, "NULL\n");
00517 return RESULT_SUCCESS;
00518 }
00519
00520 static int handle_debugchan(int fd, int argc, char *argv[])
00521 {
00522 struct ast_channel *c=NULL;
00523 if (argc != 3)
00524 return RESULT_SHOWUSAGE;
00525 c = ast_channel_walk_locked(NULL);
00526 while(c) {
00527 if (!strcasecmp(c->name, argv[2])) {
00528 c->fin |= 0x80000000;
00529 c->fout |= 0x80000000;
00530 break;
00531 }
00532 ast_mutex_unlock(&c->lock);
00533 c = ast_channel_walk_locked(c);
00534 }
00535 if (c) {
00536 ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
00537 ast_mutex_unlock(&c->lock);
00538 }
00539 else
00540 ast_cli(fd, "No such channel %s\n", argv[2]);
00541 return RESULT_SUCCESS;
00542 }
00543
00544 static int handle_nodebugchan(int fd, int argc, char *argv[])
00545 {
00546 struct ast_channel *c=NULL;
00547 if (argc != 4)
00548 return RESULT_SHOWUSAGE;
00549 c = ast_channel_walk_locked(NULL);
00550 while(c) {
00551 if (!strcasecmp(c->name, argv[3])) {
00552 c->fin &= 0x7fffffff;
00553 c->fout &= 0x7fffffff;
00554 break;
00555 }
00556 ast_mutex_unlock(&c->lock);
00557 c = ast_channel_walk_locked(c);
00558 }
00559 if (c) {
00560 ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
00561 ast_mutex_unlock(&c->lock);
00562 } else
00563 ast_cli(fd, "No such channel %s\n", argv[2]);
00564 return RESULT_SUCCESS;
00565 }
00566
00567
00568
00569 static int handle_showchan(int fd, int argc, char *argv[])
00570 {
00571 struct ast_channel *c=NULL;
00572 struct timeval now;
00573 long elapsed_seconds=0;
00574 int hour=0, min=0, sec=0;
00575 if (argc != 3)
00576 return RESULT_SHOWUSAGE;
00577 gettimeofday(&now, NULL);
00578 c = ast_channel_walk_locked(NULL);
00579 while(c) {
00580 if (!strcasecmp(c->name, argv[2])) {
00581 if(c->cdr) {
00582 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
00583 hour = elapsed_seconds / 3600;
00584 min = (elapsed_seconds % 3600) / 60;
00585 sec = elapsed_seconds % 60;
00586 }
00587 ast_cli(fd,
00588 " -- General --\n"
00589 " Name: %s\n"
00590 " Type: %s\n"
00591 " UniqueID: %s\n"
00592 " Caller ID: %s\n"
00593 " DNID Digits: %s\n"
00594 " State: %s (%d)\n"
00595 " Rings: %d\n"
00596 " NativeFormat: %d\n"
00597 " WriteFormat: %d\n"
00598 " ReadFormat: %d\n"
00599 "1st File Descriptor: %d\n"
00600 " Frames in: %d%s\n"
00601 " Frames out: %d%s\n"
00602 " Time to Hangup: %ld\n"
00603 " Elapsed Time: %dh%dm%ds\n"
00604 " -- PBX --\n"
00605 " Context: %s\n"
00606 " Extension: %s\n"
00607 " Priority: %d\n"
00608 " Call Group: %d\n"
00609 " Pickup Group: %d\n"
00610 " Application: %s\n"
00611 " Data: %s\n"
00612 " Stack: %d\n"
00613 " Blocking in: %s\n",
00614 c->name, c->type, c->uniqueid,
00615 (c->callerid ? c->callerid : "(N/A)"),
00616 (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
00617 c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
00618 c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
00619 hour, min, sec,
00620 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
00621 ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
00622 c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
00623 ast_mutex_unlock(&c->lock);
00624 break;
00625 }
00626 ast_mutex_unlock(&c->lock);
00627 c = ast_channel_walk_locked(c);
00628 }
00629 if (!c)
00630 ast_cli(fd, "%s is not a known channel\n", argv[2]);
00631 return RESULT_SUCCESS;
00632 }
00633
00634 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
00635 {
00636 struct ast_channel *c;
00637 int which=0;
00638 char *ret;
00639 if (pos != rpos)
00640 return NULL;
00641 c = ast_channel_walk_locked(NULL);
00642 while(c) {
00643 if (!strncasecmp(word, c->name, strlen(word))) {
00644 if (++which > state)
00645 break;
00646 }
00647 ast_mutex_unlock(&c->lock);
00648 c = ast_channel_walk_locked(c);
00649 }
00650 if (c) {
00651 ret = strdup(c->name);
00652 ast_mutex_unlock(&c->lock);
00653 } else
00654 ret = NULL;
00655 return ret;
00656 }
00657
00658 static char *complete_ch_3(char *line, char *word, int pos, int state)
00659 {
00660 return complete_ch_helper(line, word, pos, state, 2);
00661 }
00662
00663 static char *complete_ch_4(char *line, char *word, int pos, int state)
00664 {
00665 return complete_ch_helper(line, word, pos, state, 3);
00666 }
00667
00668 static char *complete_fn(char *line, char *word, int pos, int state)
00669 {
00670 char *c;
00671 char filename[256];
00672 if (pos != 1)
00673 return NULL;
00674 if (word[0] == '/')
00675 strncpy(filename, word, sizeof(filename)-1);
00676 else
00677 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
00678 c = (char*)filename_completion_function(filename, state);
00679 if (c && word[0] != '/')
00680 c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
00681 return c ? strdup(c) : c;
00682 }
00683
00684 static int handle_help(int fd, int argc, char *argv[]);
00685
00686 static struct ast_cli_entry builtins[] = {
00687
00688 { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
00689 { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
00690 { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
00691 { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch_3 },
00692 { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
00693 { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
00694 { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch_4 },
00695 { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
00696 { { "set", "debug", NULL }, handle_set_debug, "Set level of debug chattiness", set_debug_help },
00697 { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
00698 { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
00699 { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch_3 },
00700 { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
00701 { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
00702 { { "show", "version", NULL }, handle_version, "Display version info", version_help },
00703 { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch_3 },
00704 { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
00705 { { NULL }, NULL, NULL, NULL }
00706 };
00707
00708 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
00709 {
00710 int x;
00711 int y;
00712 int match;
00713 struct ast_cli_entry *e=NULL;
00714 for (x=0;builtins[x].cmda[0];x++) {
00715
00716 match = 1;
00717 for (y=0;match && cmds[y]; y++) {
00718
00719
00720 if (!builtins[x].cmda[y] && !exact)
00721 break;
00722
00723
00724
00725 if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
00726 match = 0;
00727 }
00728
00729
00730 if ((exact > -1) && builtins[x].cmda[y])
00731 match = 0;
00732 if (match)
00733 return &builtins[x];
00734 }
00735 for (e=helpers;e;e=e->next) {
00736 match = 1;
00737 for (y=0;match && cmds[y]; y++) {
00738 if (!e->cmda[y] && !exact)
00739 break;
00740 if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
00741 match = 0;
00742 }
00743 if ((exact > -1) && e->cmda[y])
00744 match = 0;
00745 if (match)
00746 break;
00747 }
00748 return e;
00749 }
00750
00751 static void join(char *dest, size_t destsize, char *w[])
00752 {
00753 int x;
00754
00755 if (!dest || destsize < 1) {
00756 return;
00757 }
00758 dest[0] = '\0';
00759 for (x=0;w[x];x++) {
00760 if (x)
00761 strncat(dest, " ", destsize - strlen(dest) - 1);
00762 strncat(dest, w[x], destsize - strlen(dest) - 1);
00763 }
00764 }
00765
00766 static void join2(char *dest, size_t destsize, char *w[])
00767 {
00768 int x;
00769
00770 if (!dest || destsize < 1) {
00771 return;
00772 }
00773 dest[0] = '\0';
00774 for (x=0;w[x];x++) {
00775 strncat(dest, w[x], destsize - strlen(dest) - 1);
00776 }
00777 }
00778
00779 static char *find_best(char *argv[])
00780 {
00781 static char cmdline[80];
00782 int x;
00783
00784 char *myargv[AST_MAX_CMD_LEN];
00785 for (x=0;x<AST_MAX_CMD_LEN;x++)
00786 myargv[x]=NULL;
00787 for (x=0;argv[x];x++) {
00788 myargv[x] = argv[x];
00789 if (!find_cli(myargv, -1))
00790 break;
00791 }
00792 join(cmdline, sizeof(cmdline), myargv);
00793 return cmdline;
00794 }
00795
00796 int ast_cli_unregister(struct ast_cli_entry *e)
00797 {
00798 struct ast_cli_entry *cur, *l=NULL;
00799 ast_mutex_lock(&clilock);
00800 cur = helpers;
00801 while(cur) {
00802 if (e == cur) {
00803 if (e->inuse) {
00804 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
00805 } else {
00806
00807 if (l)
00808 l->next = e->next;
00809 else
00810 helpers = e->next;
00811 e->next = NULL;
00812 break;
00813 }
00814 }
00815 l = cur;
00816 cur = cur->next;
00817 }
00818 ast_mutex_unlock(&clilock);
00819 return 0;
00820 }
00821
00822 int ast_cli_register(struct ast_cli_entry *e)
00823 {
00824 struct ast_cli_entry *cur, *l=NULL;
00825 char fulle[80] ="", fulltst[80] ="";
00826 static int len;
00827 ast_mutex_lock(&clilock);
00828 join2(fulle, sizeof(fulle), e->cmda);
00829 if (find_cli(e->cmda, -1)) {
00830 ast_mutex_unlock(&clilock);
00831 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
00832 return -1;
00833 }
00834 cur = helpers;
00835 while(cur) {
00836 join2(fulltst, sizeof(fulltst), cur->cmda);
00837 len = strlen(fulltst);
00838 if (strlen(fulle) < len)
00839 len = strlen(fulle);
00840 if (strncasecmp(fulle, fulltst, len) < 0) {
00841 if (l) {
00842 e->next = l->next;
00843 l->next = e;
00844 } else {
00845 e->next = helpers;
00846 helpers = e;
00847 }
00848 break;
00849 }
00850 l = cur;
00851 cur = cur->next;
00852 }
00853 if (!cur) {
00854 if (l)
00855 l->next = e;
00856 else
00857 helpers = e;
00858 e->next = NULL;
00859 }
00860 ast_mutex_unlock(&clilock);
00861 return 0;
00862 }
00863
00864 static int help_workhorse(int fd, char *match[])
00865 {
00866 char fullcmd1[80];
00867 char fullcmd2[80];
00868 char matchstr[80];
00869 char *fullcmd;
00870 struct ast_cli_entry *e, *e1, *e2;
00871 e1 = builtins;
00872 e2 = helpers;
00873 if (match)
00874 join(matchstr, sizeof(matchstr), match);
00875 while(e1->cmda[0] || e2) {
00876 if (e2)
00877 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
00878 if (e1->cmda[0])
00879 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
00880 if (!e1->cmda[0] ||
00881 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
00882
00883 e = e2;
00884 fullcmd = fullcmd2;
00885
00886 e2 = e2->next;
00887 } else {
00888
00889 e = e1;
00890 fullcmd = fullcmd1;
00891 e1++;
00892 }
00893
00894 if (fullcmd[0] == '_')
00895 continue;
00896 if (match) {
00897 if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
00898 continue;
00899 }
00900 }
00901 ast_cli(fd, "%25.25s %s\n", fullcmd, e->summary);
00902 }
00903 return 0;
00904 }
00905
00906 static int handle_help(int fd, int argc, char *argv[]) {
00907 struct ast_cli_entry *e;
00908 char fullcmd[80];
00909 if ((argc < 1))
00910 return RESULT_SHOWUSAGE;
00911 if (argc > 1) {
00912 e = find_cli(argv + 1, 1);
00913 if (e)
00914 ast_cli(fd, e->usage);
00915 else {
00916 if (find_cli(argv + 1, -1)) {
00917 return help_workhorse(fd, argv + 1);
00918 } else {
00919 join(fullcmd, sizeof(fullcmd), argv+1);
00920 ast_cli(fd, "No such command '%s'.\n", fullcmd);
00921 }
00922 }
00923 } else {
00924 return help_workhorse(fd, NULL);
00925 }
00926 return RESULT_SUCCESS;
00927 }
00928
00929 static char *parse_args(char *s, int *max, char *argv[])
00930 {
00931 char *dup, *cur;
00932 int x=0;
00933 int quoted=0;
00934 int escaped=0;
00935 int whitespace=1;
00936
00937 dup = strdup(s);
00938 if (dup) {
00939 cur = dup;
00940 while(*s) {
00941 switch(*s) {
00942 case '"':
00943
00944 if (escaped)
00945 goto normal;
00946 else
00947 quoted = !quoted;
00948 if (quoted && whitespace) {
00949
00950 argv[x++] = cur;
00951 whitespace=0;
00952 }
00953 escaped = 0;
00954 break;
00955 case ' ':
00956 case '\t':
00957 if (!quoted && !escaped) {
00958
00959
00960 whitespace = 1;
00961 *(cur++) = '\0';
00962 } else
00963
00964 goto normal;
00965 break;
00966 case '\\':
00967
00968 if (escaped) {
00969 goto normal;
00970 } else {
00971 escaped=1;
00972 }
00973 break;
00974 default:
00975 normal:
00976 if (whitespace) {
00977 if (x >= AST_MAX_ARGS -1) {
00978 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
00979 break;
00980 }
00981
00982 argv[x++] = cur;
00983 whitespace=0;
00984 }
00985 *(cur++) = *s;
00986 escaped=0;
00987 }
00988 s++;
00989 }
00990
00991 *(cur++) = '\0';
00992 argv[x] = NULL;
00993 *max = x;
00994 }
00995 return dup;
00996 }
00997
00998
00999 int ast_cli_generatornummatches(char *text, char *word)
01000 {
01001 int matches = 0, i = 0;
01002 char *buf, *oldbuf = NULL;
01003
01004
01005 while ( (buf = ast_cli_generator(text, word, i)) ) {
01006 if (++i > 1 && strcmp(buf,oldbuf) == 0) {
01007 continue;
01008 }
01009 oldbuf = buf;
01010 matches++;
01011 }
01012
01013 return matches;
01014 }
01015
01016 char **ast_cli_completion_matches(char *text, char *word)
01017 {
01018 char **match_list = NULL, *retstr, *prevstr;
01019 size_t match_list_len, max_equal, which, i;
01020 int matches = 0;
01021
01022 match_list_len = 1;
01023 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
01024 if (matches + 1 >= match_list_len) {
01025 match_list_len <<= 1;
01026 match_list = realloc(match_list, match_list_len * sizeof(char *));
01027 }
01028 match_list[++matches] = retstr;
01029 }
01030
01031 if (!match_list)
01032 return (char **) NULL;
01033
01034 which = 2;
01035 prevstr = match_list[1];
01036 max_equal = strlen(prevstr);
01037 for (; which <= matches; which++) {
01038 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
01039 continue;
01040 max_equal = i;
01041 }
01042
01043 retstr = malloc(max_equal + 1);
01044 (void) strncpy(retstr, match_list[1], max_equal);
01045 retstr[max_equal] = '\0';
01046 match_list[0] = retstr;
01047
01048 if (matches + 1 >= match_list_len)
01049 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
01050 match_list[matches + 1] = (char *) NULL;
01051
01052 return (match_list);
01053 }
01054
01055 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
01056 {
01057 char *argv[AST_MAX_ARGS];
01058 struct ast_cli_entry *e, *e1, *e2;
01059 int x;
01060 int matchnum=0;
01061 char *dup, *res;
01062 char fullcmd1[80];
01063 char fullcmd2[80];
01064 char matchstr[80];
01065 char *fullcmd;
01066
01067 if ((dup = parse_args(text, &x, argv))) {
01068 join(matchstr, sizeof(matchstr), argv);
01069 if (lock)
01070 ast_mutex_lock(&clilock);
01071 e1 = builtins;
01072 e2 = helpers;
01073 while(e1->cmda[0] || e2) {
01074 if (e2)
01075 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
01076 if (e1->cmda[0])
01077 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
01078 if (!e1->cmda[0] ||
01079 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
01080
01081 e = e2;
01082 fullcmd = fullcmd2;
01083
01084 e2 = e2->next;
01085 } else {
01086
01087 e = e1;
01088 fullcmd = fullcmd1;
01089 e1++;
01090 }
01091 if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
01092
01093 matchnum++;
01094 if (matchnum > state) {
01095
01096 if (!ast_strlen_zero(word) && x>0) {
01097 res = e->cmda[x-1];
01098 } else {
01099 res = e->cmda[x];
01100 }
01101 if (res) {
01102 if (lock)
01103 ast_mutex_unlock(&clilock);
01104 free(dup);
01105 return res ? strdup(res) : NULL;
01106 }
01107 }
01108 }
01109 if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
01110
01111
01112 fullcmd = e->generator(matchstr, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
01113 if (lock)
01114 ast_mutex_unlock(&clilock);
01115 free(dup);
01116 return fullcmd;
01117 }
01118
01119 }
01120 if (lock)
01121 ast_mutex_unlock(&clilock);
01122 free(dup);
01123 }
01124 return NULL;
01125 }
01126
01127 char *ast_cli_generator(char *text, char *word, int state)
01128 {
01129 return __ast_cli_generator(text, word, state, 1);
01130 }
01131
01132 int ast_cli_command(int fd, char *s)
01133 {
01134 char *argv[AST_MAX_ARGS];
01135 struct ast_cli_entry *e;
01136 int x;
01137 char *dup;
01138 x = AST_MAX_ARGS;
01139 if ((dup = parse_args(s, &x, argv))) {
01140
01141 if (x > 0) {
01142 ast_mutex_lock(&clilock);
01143 e = find_cli(argv, 0);
01144 if (e)
01145 e->inuse++;
01146 ast_mutex_unlock(&clilock);
01147 if (e) {
01148 switch(e->handler(fd, x, argv)) {
01149 case RESULT_SHOWUSAGE:
01150 ast_cli(fd, e->usage);
01151 break;
01152 }
01153 } else
01154 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
01155 if (e) {
01156 ast_mutex_lock(&clilock);
01157 e->inuse--;
01158 ast_mutex_unlock(&clilock);
01159 }
01160 }
01161 free(dup);
01162 } else {
01163 ast_log(LOG_WARNING, "Out of memory\n");
01164 return -1;
01165 }
01166 return 0;
01167 }