cli.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Standard Command Line Interface
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/options.h>
  17. #include <asterisk/cli.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/channel.h>
  20. #include <asterisk/channel_pvt.h>
  21. #include <sys/signal.h>
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <pthread.h>
  26. /* For rl_filename_completion */
  27. #include "editline/readline/readline.h"
  28. /* For module directory */
  29. #include "asterisk.h"
  30. #include "build.h"
  31. #include "astconf.h"
  32. #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
  33. " on a " BUILD_MACHINE " running " BUILD_OS
  34. void ast_cli(int fd, char *fmt, ...)
  35. {
  36. char *stuff;
  37. va_list ap;
  38. va_start(ap, fmt);
  39. vasprintf(&stuff, fmt, ap);
  40. va_end(ap);
  41. write(fd, stuff, strlen(stuff));
  42. free(stuff);
  43. }
  44. ast_mutex_t clilock = AST_MUTEX_INITIALIZER;
  45. struct ast_cli_entry *helpers = NULL;
  46. static char load_help[] =
  47. "Usage: load <module name>\n"
  48. " Loads the specified module into Asterisk.\n";
  49. static char unload_help[] =
  50. "Usage: unload [-f|-h] <module name>\n"
  51. " Unloads the specified module from Asterisk. The -f\n"
  52. " option causes the module to be unloaded even if it is\n"
  53. " in use (may cause a crash) and the -h module causes the\n"
  54. " module to be unloaded even if the module says it cannot, \n"
  55. " which almost always will cause a crash.\n";
  56. static char help_help[] =
  57. "Usage: help [topic]\n"
  58. " When called with a topic as an argument, displays usage\n"
  59. " information on the given command. If called without a\n"
  60. " topic, it provides a list of commands.\n";
  61. static char chanlist_help[] =
  62. "Usage: show channels\n"
  63. " Lists currently defined channels and some information about\n"
  64. " them.\n";
  65. static char reload_help[] =
  66. "Usage: reload\n"
  67. " Reloads configuration files for all modules which support\n"
  68. " reloading.\n";
  69. static char set_verbose_help[] =
  70. "Usage: set verbose <level>\n"
  71. " Sets level of verbose messages to be displayed. 0 means\n"
  72. " no messages should be displayed.\n";
  73. static char softhangup_help[] =
  74. "Usage: soft hangup <channel>\n"
  75. " Request that a channel be hung up. The hangup takes effect\n"
  76. " the next time the driver reads or writes from the channel\n";
  77. static int handle_load(int fd, int argc, char *argv[])
  78. {
  79. if (argc != 2)
  80. return RESULT_SHOWUSAGE;
  81. if (ast_load_resource(argv[1])) {
  82. ast_cli(fd, "Unable to load module %s\n", argv[1]);
  83. return RESULT_FAILURE;
  84. }
  85. return RESULT_SUCCESS;
  86. }
  87. static int handle_reload(int fd, int argc, char *argv[])
  88. {
  89. if (argc != 1)
  90. return RESULT_SHOWUSAGE;
  91. ast_module_reload();
  92. return RESULT_SUCCESS;
  93. }
  94. static int handle_set_verbose(int fd, int argc, char *argv[])
  95. {
  96. int val;
  97. /* Has a hidden 'at least' argument */
  98. if ((argc != 3) && (argc != 4))
  99. return RESULT_SHOWUSAGE;
  100. if ((argc == 4) && strcasecmp(argv[2], "atleast"))
  101. return RESULT_SHOWUSAGE;
  102. if (argc == 3)
  103. option_verbose = atoi(argv[2]);
  104. else {
  105. val = atoi(argv[3]);
  106. if (val > option_verbose)
  107. option_verbose = val;
  108. }
  109. return RESULT_SUCCESS;
  110. }
  111. static int handle_unload(int fd, int argc, char *argv[])
  112. {
  113. int x;
  114. int force=AST_FORCE_SOFT;
  115. if (argc < 2)
  116. return RESULT_SHOWUSAGE;
  117. for (x=1;x<argc;x++) {
  118. if (argv[x][0] == '-') {
  119. switch(argv[x][1]) {
  120. case 'f':
  121. force = AST_FORCE_FIRM;
  122. break;
  123. case 'h':
  124. force = AST_FORCE_HARD;
  125. break;
  126. default:
  127. return RESULT_SHOWUSAGE;
  128. }
  129. } else if (x != argc - 1)
  130. return RESULT_SHOWUSAGE;
  131. else if (ast_unload_resource(argv[x], force)) {
  132. ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
  133. return RESULT_FAILURE;
  134. }
  135. }
  136. return RESULT_SUCCESS;
  137. }
  138. #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n"
  139. #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
  140. static ast_mutex_t climodentrylock = AST_MUTEX_INITIALIZER;
  141. static int climodentryfd = -1;
  142. static int modlist_modentry(char *module, char *description, int usecnt)
  143. {
  144. ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
  145. return 0;
  146. }
  147. static char modlist_help[] =
  148. "Usage: show modules\n"
  149. " Shows Asterisk modules currently in use, and usage "
  150. "statistics.\n";
  151. static char version_help[] =
  152. "Usage: show version\n"
  153. " Shows Asterisk version information.\n ";
  154. static char *format_uptimestr(time_t timeval)
  155. {
  156. int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
  157. char timestr[256];
  158. int pos = 0;
  159. #define SECOND (1)
  160. #define MIN (SECOND*60)
  161. #define HOUR (MIN*60)
  162. #define DAY (HOUR*24)
  163. #define WEEK (DAY*7)
  164. #define YEAR (DAY*365)
  165. if (timeval < 0)
  166. return NULL;
  167. if (timeval > YEAR) {
  168. years = (timeval / YEAR);
  169. timeval -= (years * YEAR);
  170. if (years > 1)
  171. pos += sprintf(timestr + pos, "%d years, ", years);
  172. else
  173. pos += sprintf(timestr + pos, "1 year, ");
  174. }
  175. if (timeval > WEEK) {
  176. weeks = (timeval / WEEK);
  177. timeval -= (weeks * WEEK);
  178. if (weeks > 1)
  179. pos += sprintf(timestr + pos, "%d weeks, ", weeks);
  180. else
  181. pos += sprintf(timestr + pos, "1 week, ");
  182. }
  183. if (timeval > DAY) {
  184. days = (timeval / DAY);
  185. timeval -= (days * DAY);
  186. if (days > 1)
  187. pos += sprintf(timestr + pos, "%d days, ", days);
  188. else
  189. pos += sprintf(timestr + pos, "1 day, ");
  190. }
  191. if (timeval > HOUR) {
  192. hours = (timeval / HOUR);
  193. timeval -= (hours * HOUR);
  194. if (hours > 1)
  195. pos += sprintf(timestr + pos, "%d hours, ", hours);
  196. else
  197. pos += sprintf(timestr + pos, "1 hour, ");
  198. }
  199. if (timeval > MIN) {
  200. mins = (timeval / MIN);
  201. timeval -= (mins * MIN);
  202. if (mins > 1)
  203. pos += sprintf(timestr + pos, "%d minutes, ", mins);
  204. else if (mins > 0)
  205. pos += sprintf(timestr + pos, "1 minute, ");
  206. }
  207. secs = timeval;
  208. if (secs > 0)
  209. pos += sprintf(timestr + pos, "%d seconds", secs);
  210. return timestr ? strdup(timestr) : NULL;
  211. }
  212. static int handle_showuptime(int fd, int argc, char *argv[])
  213. {
  214. time_t curtime, tmptime;
  215. char *timestr;
  216. time(&curtime);
  217. if (ast_startuptime) {
  218. tmptime = curtime - ast_startuptime;
  219. timestr = format_uptimestr(tmptime);
  220. if (timestr) {
  221. ast_cli(fd, "System uptime: %s\n", timestr);
  222. free(timestr);
  223. }
  224. }
  225. if (ast_lastreloadtime) {
  226. tmptime = curtime - ast_lastreloadtime;
  227. timestr = format_uptimestr(tmptime);
  228. if (timestr) {
  229. ast_cli(fd, "Last reload: %s\n", timestr);
  230. free(timestr);
  231. }
  232. }
  233. return RESULT_SUCCESS;
  234. }
  235. static int handle_modlist(int fd, int argc, char *argv[])
  236. {
  237. if (argc != 2)
  238. return RESULT_SHOWUSAGE;
  239. ast_mutex_lock(&climodentrylock);
  240. climodentryfd = fd;
  241. ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
  242. ast_update_module_list(modlist_modentry);
  243. climodentryfd = -1;
  244. ast_mutex_unlock(&climodentrylock);
  245. return RESULT_SUCCESS;
  246. }
  247. static int handle_version(int fd, int argc, char *argv[])
  248. {
  249. if (argc != 2)
  250. return RESULT_SHOWUSAGE;
  251. ast_cli(fd, "%s\n", VERSION_INFO);
  252. return RESULT_SUCCESS;
  253. }
  254. static int handle_chanlist(int fd, int argc, char *argv[])
  255. {
  256. #define FORMAT_STRING "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
  257. #define FORMAT_STRING2 "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
  258. struct ast_channel *c=NULL;
  259. int numchans = 0;
  260. if (argc != 2)
  261. return RESULT_SHOWUSAGE;
  262. c = ast_channel_walk(NULL);
  263. ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
  264. while(c) {
  265. ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
  266. c->appl ? c->appl : "(None)", c->data ? ( strlen(c->data) ? c->data : "(Empty)" ): "(None)");
  267. numchans++;
  268. c = ast_channel_walk(c);
  269. }
  270. ast_cli(fd, "%d active channel(s)\n", numchans);
  271. return RESULT_SUCCESS;
  272. }
  273. static char showchan_help[] =
  274. "Usage: show channel <channel>\n"
  275. " Shows lots of information about the specified channel.\n";
  276. static char debugchan_help[] =
  277. "Usage: debug channel <channel>\n"
  278. " Enables debugging on a specific channel.\n";
  279. static char nodebugchan_help[] =
  280. "Usage: no debug channel <channel>\n"
  281. " Disables debugging on a specific channel.\n";
  282. static char commandcomplete_help[] =
  283. "Usage: _command complete \"<line>\" text state\n"
  284. " This function is used internally to help with command completion and should.\n"
  285. " never be called by the user directly.\n";
  286. static char commandnummatches_help[] =
  287. "Usage: _command nummatches \"<line>\" text \n"
  288. " This function is used internally to help with command completion and should.\n"
  289. " never be called by the user directly.\n";
  290. static char commandmatchesarray_help[] =
  291. "Usage: _command matchesarray \"<line>\" text \n"
  292. " This function is used internally to help with command completion and should.\n"
  293. " never be called by the user directly.\n";
  294. static int handle_softhangup(int fd, int argc, char *argv[])
  295. {
  296. struct ast_channel *c=NULL;
  297. if (argc != 3)
  298. return RESULT_SHOWUSAGE;
  299. c = ast_channel_walk(NULL);
  300. while(c) {
  301. if (!strcasecmp(c->name, argv[2])) {
  302. ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
  303. ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
  304. break;
  305. }
  306. c = ast_channel_walk(c);
  307. }
  308. if (!c)
  309. ast_cli(fd, "%s is not a known channel\n", argv[2]);
  310. return RESULT_SUCCESS;
  311. }
  312. static char *__ast_cli_generator(char *text, char *word, int state, int lock);
  313. static int handle_commandmatchesarray(int fd, int argc, char *argv[])
  314. {
  315. char *buf;
  316. int buflen = 2048;
  317. int len = 0;
  318. char **matches;
  319. int x;
  320. if (argc != 4)
  321. return RESULT_SHOWUSAGE;
  322. buf = malloc(buflen);
  323. if (!buf)
  324. return RESULT_FAILURE;
  325. buf[len] = '\0';
  326. matches = ast_cli_completion_matches(argv[2], argv[3]);
  327. if (matches) {
  328. for (x=0; matches[x]; x++) {
  329. #if 0
  330. printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
  331. #endif
  332. if (len + strlen(matches[x]) >= buflen) {
  333. buflen += strlen(matches[x]) * 3;
  334. buf = realloc(buf, buflen);
  335. }
  336. len += sprintf( buf + len, "%s ", matches[x]);
  337. free(matches[x]);
  338. matches[x] = NULL;
  339. }
  340. free(matches);
  341. }
  342. #if 0
  343. printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
  344. #endif
  345. if (buf) {
  346. ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
  347. free(buf);
  348. } else
  349. ast_cli(fd, "NULL\n");
  350. return RESULT_SUCCESS;
  351. }
  352. static int handle_commandnummatches(int fd, int argc, char *argv[])
  353. {
  354. int matches = 0;
  355. if (argc != 4)
  356. return RESULT_SHOWUSAGE;
  357. matches = ast_cli_generatornummatches(argv[2], argv[3]);
  358. #if 0
  359. printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
  360. #endif
  361. ast_cli(fd, "%d", matches);
  362. return RESULT_SUCCESS;
  363. }
  364. static int handle_commandcomplete(int fd, int argc, char *argv[])
  365. {
  366. char *buf;
  367. #if 0
  368. printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
  369. #endif
  370. if (argc != 5)
  371. return RESULT_SHOWUSAGE;
  372. buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
  373. #if 0
  374. printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
  375. #endif
  376. if (buf) {
  377. ast_cli(fd, buf);
  378. free(buf);
  379. } else
  380. ast_cli(fd, "NULL\n");
  381. return RESULT_SUCCESS;
  382. }
  383. static int handle_debugchan(int fd, int argc, char *argv[])
  384. {
  385. struct ast_channel *c=NULL;
  386. if (argc != 3)
  387. return RESULT_SHOWUSAGE;
  388. c = ast_channel_walk(NULL);
  389. while(c) {
  390. if (!strcasecmp(c->name, argv[2])) {
  391. c->fin |= 0x80000000;
  392. c->fout |= 0x80000000;
  393. break;
  394. }
  395. c = ast_channel_walk(c);
  396. }
  397. if (c)
  398. ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
  399. else
  400. ast_cli(fd, "No such channel %s\n", argv[2]);
  401. return RESULT_SUCCESS;
  402. }
  403. static int handle_nodebugchan(int fd, int argc, char *argv[])
  404. {
  405. struct ast_channel *c=NULL;
  406. if (argc != 4)
  407. return RESULT_SHOWUSAGE;
  408. c = ast_channel_walk(NULL);
  409. while(c) {
  410. if (!strcasecmp(c->name, argv[3])) {
  411. c->fin &= 0x7fffffff;
  412. c->fout &= 0x7fffffff;
  413. break;
  414. }
  415. c = ast_channel_walk(c);
  416. }
  417. if (c)
  418. ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
  419. else
  420. ast_cli(fd, "No such channel %s\n", argv[2]);
  421. return RESULT_SUCCESS;
  422. }
  423. static int handle_showchan(int fd, int argc, char *argv[])
  424. {
  425. struct ast_channel *c=NULL;
  426. if (argc != 3)
  427. return RESULT_SHOWUSAGE;
  428. c = ast_channel_walk(NULL);
  429. while(c) {
  430. if (!strcasecmp(c->name, argv[2])) {
  431. ast_cli(fd,
  432. " -- General --\n"
  433. " Name: %s\n"
  434. " Type: %s\n"
  435. " UniqueID: %s\n"
  436. " Caller ID: %s\n"
  437. " DNID Digits: %s\n"
  438. " State: %s (%d)\n"
  439. " Rings: %d\n"
  440. " NativeFormat: %d\n"
  441. " WriteFormat: %d\n"
  442. " ReadFormat: %d\n"
  443. "1st File Descriptor: %d\n"
  444. " Frames in: %d%s\n"
  445. " Frames out: %d%s\n"
  446. " Time to Hangup: %ld\n"
  447. " -- PBX --\n"
  448. " Context: %s\n"
  449. " Extension: %s\n"
  450. " Priority: %d\n"
  451. " Call Group: %d\n"
  452. " Pickup Group: %d\n"
  453. " Application: %s\n"
  454. " Data: %s\n"
  455. " Stack: %d\n"
  456. " Blocking in: %s\n",
  457. c->name, c->type, c->uniqueid,
  458. (c->callerid ? c->callerid : "(N/A)"),
  459. (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
  460. c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
  461. c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
  462. c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
  463. ( c-> data ? (strlen(c->data) ? c->data : "(Empty)") : "(None)"),
  464. c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
  465. break;
  466. }
  467. c = ast_channel_walk(c);
  468. }
  469. if (!c)
  470. ast_cli(fd, "%s is not a known channel\n", argv[2]);
  471. return RESULT_SUCCESS;
  472. }
  473. static char *complete_ch(char *line, char *word, int pos, int state)
  474. {
  475. struct ast_channel *c;
  476. int which=0;
  477. c = ast_channel_walk(NULL);
  478. while(c) {
  479. if (!strncasecmp(word, c->name, strlen(word))) {
  480. if (++which > state)
  481. break;
  482. }
  483. c = ast_channel_walk(c);
  484. }
  485. return c ? strdup(c->name) : NULL;
  486. }
  487. static char *complete_fn(char *line, char *word, int pos, int state)
  488. {
  489. char *c;
  490. char filename[256];
  491. if (pos != 1)
  492. return NULL;
  493. if (word[0] == '/')
  494. strncpy(filename, word, sizeof(filename)-1);
  495. else
  496. snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
  497. c = (char*)filename_completion_function(filename, state);
  498. if (c && word[0] != '/')
  499. c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
  500. return c ? strdup(c) : c;
  501. }
  502. static int handle_help(int fd, int argc, char *argv[]);
  503. static struct ast_cli_entry builtins[] = {
  504. /* Keep alphabetized, with longer matches first (example: abcd before abc) */
  505. { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
  506. { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
  507. { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
  508. { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch },
  509. { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
  510. { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
  511. { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch },
  512. { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
  513. { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
  514. { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
  515. { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch },
  516. { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
  517. { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", modlist_help },
  518. { { "show", "version", NULL }, handle_version, "Display version info", version_help },
  519. { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch },
  520. { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
  521. { { NULL }, NULL, NULL, NULL }
  522. };
  523. static struct ast_cli_entry *find_cli(char *cmds[], int exact)
  524. {
  525. int x;
  526. int y;
  527. int match;
  528. struct ast_cli_entry *e=NULL;
  529. for (x=0;builtins[x].cmda[0];x++) {
  530. /* start optimistic */
  531. match = 1;
  532. for (y=0;match && cmds[y]; y++) {
  533. /* If there are no more words in the candidate command, then we're
  534. there. */
  535. if (!builtins[x].cmda[y] && !exact)
  536. break;
  537. /* If there are no more words in the command (and we're looking for
  538. an exact match) or there is a difference between the two words,
  539. then this is not a match */
  540. if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
  541. match = 0;
  542. }
  543. /* If more words are needed to complete the command then this is not
  544. a candidate (unless we're looking for a really inexact answer */
  545. if ((exact > -1) && builtins[x].cmda[y])
  546. match = 0;
  547. if (match)
  548. return &builtins[x];
  549. }
  550. for (e=helpers;e;e=e->next) {
  551. match = 1;
  552. for (y=0;match && cmds[y]; y++) {
  553. if (!e->cmda[y] && !exact)
  554. break;
  555. if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
  556. match = 0;
  557. }
  558. if ((exact > -1) && e->cmda[y])
  559. match = 0;
  560. if (match)
  561. break;
  562. }
  563. return e;
  564. }
  565. static void join(char *s, int len, char *w[])
  566. {
  567. int x;
  568. /* Join words into a string */
  569. strcpy(s, "");
  570. for (x=0;w[x];x++) {
  571. if (x)
  572. strncat(s, " ", len - strlen(s));
  573. strncat(s, w[x], len - strlen(s));
  574. }
  575. }
  576. static void join2(char *s, int len, char *w[])
  577. {
  578. int x;
  579. /* Join words into a string */
  580. strcpy(s, "");
  581. for (x=0;w[x];x++) {
  582. strncat(s, w[x], len - strlen(s));
  583. }
  584. }
  585. static char *find_best(char *argv[])
  586. {
  587. static char cmdline[80];
  588. int x;
  589. /* See how close we get, then print the */
  590. char *myargv[AST_MAX_CMD_LEN];
  591. for (x=0;x<AST_MAX_CMD_LEN;x++)
  592. myargv[x]=NULL;
  593. for (x=0;argv[x];x++) {
  594. myargv[x] = argv[x];
  595. if (!find_cli(myargv, -1))
  596. break;
  597. }
  598. join(cmdline, sizeof(cmdline), myargv);
  599. return cmdline;
  600. }
  601. int ast_cli_unregister(struct ast_cli_entry *e)
  602. {
  603. struct ast_cli_entry *cur, *l=NULL;
  604. ast_mutex_lock(&clilock);
  605. cur = helpers;
  606. while(cur) {
  607. if (e == cur) {
  608. if (e->inuse) {
  609. ast_log(LOG_WARNING, "Can't remove command that is in use\n");
  610. } else {
  611. /* Rewrite */
  612. if (l)
  613. l->next = e->next;
  614. else
  615. helpers = e->next;
  616. e->next = NULL;
  617. break;
  618. }
  619. }
  620. l = cur;
  621. cur = cur->next;
  622. }
  623. ast_mutex_unlock(&clilock);
  624. return 0;
  625. }
  626. int ast_cli_register(struct ast_cli_entry *e)
  627. {
  628. struct ast_cli_entry *cur, *l=NULL;
  629. char fulle[80] ="", fulltst[80] ="";
  630. static int len;
  631. ast_mutex_lock(&clilock);
  632. join2(fulle, sizeof(fulle), e->cmda);
  633. if (find_cli(e->cmda, -1)) {
  634. ast_mutex_unlock(&clilock);
  635. ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
  636. return -1;
  637. }
  638. cur = helpers;
  639. while(cur) {
  640. join2(fulltst, sizeof(fulltst), cur->cmda);
  641. len = strlen(fulltst);
  642. if (strlen(fulle) < len)
  643. len = strlen(fulle);
  644. if (strncasecmp(fulle, fulltst, len) < 0) {
  645. if (l) {
  646. e->next = l->next;
  647. l->next = e;
  648. } else {
  649. e->next = helpers;
  650. helpers = e;
  651. }
  652. break;
  653. }
  654. l = cur;
  655. cur = cur->next;
  656. }
  657. if (!cur) {
  658. if (l)
  659. l->next = e;
  660. else
  661. helpers = e;
  662. e->next = NULL;
  663. }
  664. ast_mutex_unlock(&clilock);
  665. return 0;
  666. }
  667. static int help_workhorse(int fd, char *match[])
  668. {
  669. char fullcmd1[80];
  670. char fullcmd2[80];
  671. char matchstr[80];
  672. char *fullcmd;
  673. struct ast_cli_entry *e, *e1, *e2;
  674. e1 = builtins;
  675. e2 = helpers;
  676. if (match)
  677. join(matchstr, sizeof(matchstr), match);
  678. while(e1->cmda[0] || e2) {
  679. if (e2)
  680. join(fullcmd2, sizeof(fullcmd2), e2->cmda);
  681. if (e1->cmda[0])
  682. join(fullcmd1, sizeof(fullcmd1), e1->cmda);
  683. if (!e1->cmda[0] ||
  684. (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
  685. /* Use e2 */
  686. e = e2;
  687. fullcmd = fullcmd2;
  688. /* Increment by going to next */
  689. e2 = e2->next;
  690. } else {
  691. /* Use e1 */
  692. e = e1;
  693. fullcmd = fullcmd1;
  694. e1++;
  695. }
  696. /* Hide commands that start with '_' */
  697. if (fullcmd[0] == '_')
  698. continue;
  699. if (match) {
  700. if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
  701. continue;
  702. }
  703. }
  704. ast_cli(fd, "%20.20s %s\n", fullcmd, e->summary);
  705. }
  706. return 0;
  707. }
  708. static int handle_help(int fd, int argc, char *argv[]) {
  709. struct ast_cli_entry *e;
  710. char fullcmd[80];
  711. if ((argc < 1))
  712. return RESULT_SHOWUSAGE;
  713. if (argc > 1) {
  714. e = find_cli(argv + 1, 1);
  715. if (e)
  716. ast_cli(fd, e->usage);
  717. else {
  718. if (find_cli(argv + 1, -1)) {
  719. return help_workhorse(fd, argv + 1);
  720. } else {
  721. join(fullcmd, sizeof(fullcmd), argv+1);
  722. ast_cli(fd, "No such command '%s'.\n", fullcmd);
  723. }
  724. }
  725. } else {
  726. return help_workhorse(fd, NULL);
  727. }
  728. return RESULT_SUCCESS;
  729. }
  730. static char *parse_args(char *s, int *max, char *argv[])
  731. {
  732. char *dup, *cur;
  733. int x=0;
  734. int quoted=0;
  735. int escaped=0;
  736. int whitespace=1;
  737. dup = strdup(s);
  738. if (dup) {
  739. cur = dup;
  740. while(*s) {
  741. switch(*s) {
  742. case '"':
  743. /* If it's escaped, put a literal quote */
  744. if (escaped)
  745. goto normal;
  746. else
  747. quoted = !quoted;
  748. if (quoted && whitespace) {
  749. /* If we're starting a quote, coming off white space start a new word, too */
  750. argv[x++] = cur;
  751. whitespace=0;
  752. }
  753. escaped = 0;
  754. break;
  755. case ' ':
  756. case '\t':
  757. if (!quoted && !escaped) {
  758. /* If we're not quoted, mark this as whitespace, and
  759. end the previous argument */
  760. whitespace = 1;
  761. *(cur++) = '\0';
  762. } else
  763. /* Otherwise, just treat it as anything else */
  764. goto normal;
  765. break;
  766. case '\\':
  767. /* If we're escaped, print a literal, otherwise enable escaping */
  768. if (escaped) {
  769. goto normal;
  770. } else {
  771. escaped=1;
  772. }
  773. break;
  774. default:
  775. normal:
  776. if (whitespace) {
  777. if (x >= AST_MAX_ARGS -1) {
  778. ast_log(LOG_WARNING, "Too many arguments, truncating\n");
  779. break;
  780. }
  781. /* Coming off of whitespace, start the next argument */
  782. argv[x++] = cur;
  783. whitespace=0;
  784. }
  785. *(cur++) = *s;
  786. escaped=0;
  787. }
  788. s++;
  789. }
  790. /* Null terminate */
  791. *(cur++) = '\0';
  792. argv[x] = NULL;
  793. *max = x;
  794. }
  795. return dup;
  796. }
  797. /* This returns the number of unique matches for the generator */
  798. int ast_cli_generatornummatches(char *text, char *word)
  799. {
  800. int matches = 0, i = 0;
  801. char *buf, *oldbuf = NULL;
  802. while ( (buf = ast_cli_generator(text, word, i)) ) {
  803. if (++i > 1 && strcmp(buf,oldbuf) == 0) {
  804. continue;
  805. }
  806. oldbuf = buf;
  807. matches++;
  808. }
  809. return matches;
  810. }
  811. char **ast_cli_completion_matches(char *text, char *word)
  812. {
  813. char **match_list = NULL, *retstr, *prevstr;
  814. size_t match_list_len, max_equal, which, i;
  815. int matches = 0;
  816. match_list_len = 1;
  817. while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
  818. if (matches + 1 >= match_list_len) {
  819. match_list_len <<= 1;
  820. match_list = realloc(match_list, match_list_len * sizeof(char *));
  821. }
  822. match_list[++matches] = retstr;
  823. }
  824. if (!match_list)
  825. return (char **) NULL;
  826. which = 2;
  827. prevstr = match_list[1];
  828. max_equal = strlen(prevstr);
  829. for (; which <= matches; which++) {
  830. for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
  831. continue;
  832. max_equal = i;
  833. }
  834. retstr = malloc(max_equal + 1);
  835. (void) strncpy(retstr, match_list[1], max_equal);
  836. retstr[max_equal] = '\0';
  837. match_list[0] = retstr;
  838. if (matches + 1 >= match_list_len)
  839. match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
  840. match_list[matches + 1] = (char *) NULL;
  841. return (match_list);
  842. }
  843. static char *__ast_cli_generator(char *text, char *word, int state, int lock)
  844. {
  845. char *argv[AST_MAX_ARGS];
  846. struct ast_cli_entry *e, *e1, *e2;
  847. int x;
  848. int matchnum=0;
  849. char *dup, *res;
  850. char fullcmd1[80];
  851. char fullcmd2[80];
  852. char matchstr[80];
  853. char *fullcmd;
  854. if ((dup = parse_args(text, &x, argv))) {
  855. join(matchstr, sizeof(matchstr), argv);
  856. if (lock)
  857. ast_mutex_lock(&clilock);
  858. e1 = builtins;
  859. e2 = helpers;
  860. while(e1->cmda[0] || e2) {
  861. if (e2)
  862. join(fullcmd2, sizeof(fullcmd2), e2->cmda);
  863. if (e1->cmda[0])
  864. join(fullcmd1, sizeof(fullcmd1), e1->cmda);
  865. if (!e1->cmda[0] ||
  866. (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
  867. /* Use e2 */
  868. e = e2;
  869. fullcmd = fullcmd2;
  870. /* Increment by going to next */
  871. e2 = e2->next;
  872. } else {
  873. /* Use e1 */
  874. e = e1;
  875. fullcmd = fullcmd1;
  876. e1++;
  877. }
  878. if ((fullcmd[0] != '_') && !strncasecmp(text, fullcmd, strlen(text))) {
  879. /* We contain the first part of one or more commands */
  880. matchnum++;
  881. if (matchnum > state) {
  882. /* Now, what we're supposed to return is the next word... */
  883. if (strlen(word) && x>0) {
  884. res = e->cmda[x-1];
  885. } else {
  886. res = e->cmda[x];
  887. }
  888. if (res) {
  889. if (lock)
  890. ast_mutex_unlock(&clilock);
  891. free(dup);
  892. return res ? strdup(res) : NULL;
  893. }
  894. }
  895. }
  896. if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
  897. /* We have a command in its entirity within us -- theoretically only one
  898. command can have this occur */
  899. fullcmd = e->generator(text, word, (strlen(word) ? (x - 1) : (x)), state);
  900. if (lock)
  901. ast_mutex_unlock(&clilock);
  902. free(dup);
  903. return fullcmd;
  904. }
  905. }
  906. if (lock)
  907. ast_mutex_unlock(&clilock);
  908. free(dup);
  909. }
  910. return NULL;
  911. }
  912. char *ast_cli_generator(char *text, char *word, int state)
  913. {
  914. return __ast_cli_generator(text, word, state, 1);
  915. }
  916. int ast_cli_command(int fd, char *s)
  917. {
  918. char *argv[AST_MAX_ARGS];
  919. struct ast_cli_entry *e;
  920. int x;
  921. char *dup;
  922. x = AST_MAX_ARGS;
  923. if ((dup = parse_args(s, &x, argv))) {
  924. /* We need at least one entry, or ignore */
  925. if (x > 0) {
  926. ast_mutex_lock(&clilock);
  927. e = find_cli(argv, 0);
  928. if (e)
  929. e->inuse++;
  930. ast_mutex_unlock(&clilock);
  931. if (e) {
  932. switch(e->handler(fd, x, argv)) {
  933. case RESULT_SHOWUSAGE:
  934. ast_cli(fd, e->usage);
  935. break;
  936. }
  937. } else
  938. ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
  939. if (e) {
  940. ast_mutex_lock(&clilock);
  941. e->inuse--;
  942. ast_mutex_unlock(&clilock);
  943. }
  944. }
  945. free(dup);
  946. } else {
  947. ast_log(LOG_WARNING, "Out of memory\n");
  948. return -1;
  949. }
  950. return 0;
  951. }