res_clialiases.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief CLI Aliases
  21. *
  22. * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  23. *
  24. * This module provides the capability to create aliases to other
  25. * CLI commands.
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/module.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/cli.h"
  35. #include "asterisk/astobj2.h"
  36. /*! Maximum number of buckets for CLI aliases */
  37. #define MAX_ALIAS_BUCKETS 53
  38. /*! Configuration file used for this application */
  39. static const char config_file[] = "cli_aliases.conf";
  40. struct cli_alias {
  41. struct ast_cli_entry cli_entry; /*!< Actual CLI structure used for this alias */
  42. char *alias; /*!< CLI Alias */
  43. char *real_cmd; /*!< Actual CLI command it is aliased to */
  44. };
  45. static struct ao2_container *cli_aliases;
  46. /*! \brief Hashing function used for aliases */
  47. static int alias_hash_cb(const void *obj, const int flags)
  48. {
  49. const struct cli_alias *alias = obj;
  50. return ast_str_hash(alias->cli_entry.command);
  51. }
  52. /*! \brief Comparison function used for aliases */
  53. static int alias_cmp_cb(void *obj, void *arg, int flags)
  54. {
  55. const struct cli_alias *alias0 = obj, *alias1 = arg;
  56. return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
  57. }
  58. /*! \brief Destruction function used for aliases */
  59. static void alias_destroy(void *obj)
  60. {
  61. struct cli_alias *alias = obj;
  62. /* Unregister the CLI entry from the core */
  63. ast_cli_unregister(&alias->cli_entry);
  64. return;
  65. }
  66. /*! \brief Function which passes through an aliased CLI command to the real one */
  67. static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  68. {
  69. struct cli_alias *alias;
  70. struct cli_alias tmp = {
  71. .cli_entry.command = e->command,
  72. };
  73. char *generator;
  74. const char *line;
  75. /* Try to find the alias based on the CLI entry */
  76. if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
  77. return 0;
  78. }
  79. switch (cmd) {
  80. case CLI_INIT:
  81. ao2_ref(alias, -1);
  82. return NULL;
  83. case CLI_GENERATE:
  84. line = a->line;
  85. line += (strlen(alias->alias));
  86. if (!strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
  87. generator = NULL;
  88. } else if (!ast_strlen_zero(a->word)) {
  89. struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
  90. ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
  91. generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
  92. } else {
  93. generator = ast_cli_generator(alias->real_cmd, a->word, a->n);
  94. }
  95. ao2_ref(alias, -1);
  96. return generator;
  97. }
  98. /* If they gave us extra arguments we need to construct a string to pass in */
  99. if (a->argc != e->args) {
  100. struct ast_str *real_cmd = ast_str_alloca(2048);
  101. int i;
  102. ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
  103. /* Add the additional arguments that have been passed in */
  104. for (i = e->args + 1; i <= a->argc; i++) {
  105. ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
  106. }
  107. ast_cli_command(a->fd, ast_str_buffer(real_cmd));
  108. } else {
  109. ast_cli_command(a->fd, alias->real_cmd);
  110. }
  111. ao2_ref(alias, -1);
  112. return CLI_SUCCESS;
  113. }
  114. /*! \brief CLI Command to display CLI Aliases */
  115. static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  116. {
  117. #define FORMAT "%-50.50s %-50.50s\n"
  118. struct cli_alias *alias;
  119. struct ao2_iterator i;
  120. switch (cmd) {
  121. case CLI_INIT:
  122. e->command = "cli show aliases";
  123. e->usage =
  124. "Usage: cli show aliases\n"
  125. " Displays a list of aliased CLI commands.\n";
  126. return NULL;
  127. case CLI_GENERATE:
  128. return NULL;
  129. }
  130. ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
  131. i = ao2_iterator_init(cli_aliases, 0);
  132. for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
  133. ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
  134. }
  135. ao2_iterator_destroy(&i);
  136. return CLI_SUCCESS;
  137. #undef FORMAT
  138. }
  139. /*! \brief CLI commands to interact with things */
  140. static struct ast_cli_entry cli_alias[] = {
  141. AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
  142. };
  143. /*! \brief Function called to load or reload the configuration file */
  144. static void load_config(int reload)
  145. {
  146. struct ast_config *cfg = NULL;
  147. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  148. struct cli_alias *alias;
  149. struct ast_variable *v, *v1;
  150. if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  151. ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
  152. return;
  153. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  154. return;
  155. }
  156. /* Destroy any existing CLI aliases */
  157. if (reload) {
  158. ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  159. }
  160. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  161. if (strcmp(v->name, "template")) {
  162. ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
  163. continue;
  164. }
  165. /* Read in those there CLI aliases */
  166. for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
  167. if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), alias_destroy))) {
  168. continue;
  169. }
  170. alias->alias = ((char *) alias) + sizeof(*alias);
  171. alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
  172. strcpy(alias->alias, v1->name);
  173. strcpy(alias->real_cmd, v1->value);
  174. alias->cli_entry.handler = cli_alias_passthrough;
  175. alias->cli_entry.command = alias->alias;
  176. alias->cli_entry.usage = "Aliased CLI Command\n";
  177. if (ast_cli_register(&alias->cli_entry)) {
  178. ao2_ref(alias, -1);
  179. continue;
  180. }
  181. ao2_link(cli_aliases, alias);
  182. ast_verbose(VERBOSE_PREFIX_2 "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
  183. ao2_ref(alias, -1);
  184. }
  185. }
  186. ast_config_destroy(cfg);
  187. return;
  188. }
  189. /*! \brief Function called to reload the module */
  190. static int reload_module(void)
  191. {
  192. load_config(1);
  193. return 0;
  194. }
  195. /*! \brief Function called to unload the module */
  196. static int unload_module(void)
  197. {
  198. ao2_ref(cli_aliases, -1);
  199. ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
  200. return 0;
  201. }
  202. /*! \brief Function called to load the module */
  203. static int load_module(void)
  204. {
  205. if (!(cli_aliases = ao2_container_alloc(MAX_ALIAS_BUCKETS, alias_hash_cb, alias_cmp_cb))) {
  206. return AST_MODULE_LOAD_DECLINE;
  207. }
  208. load_config(0);
  209. ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
  210. return AST_MODULE_LOAD_SUCCESS;
  211. }
  212. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases",
  213. .load = load_module,
  214. .unload = unload_module,
  215. .reload = reload_module,
  216. );