2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
23 * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
25 * This module provides the capability to create aliases to other
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/module.h"
35 #include "asterisk/config.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/astobj2.h"
39 /*! Maximum number of buckets for CLI aliases */
40 #define MAX_ALIAS_BUCKETS 53
42 /*! Configuration file used for this application */
43 static const char config_file[] = "cli_aliases.conf";
46 struct ast_cli_entry cli_entry; /*!< Actual CLI structure used for this alias */
47 char *alias; /*!< CLI Alias */
48 char *real_cmd; /*!< Actual CLI command it is aliased to */
51 static struct ao2_container *cli_aliases;
53 /*! \brief Hashing function used for aliases */
54 static int alias_hash_cb(const void *obj, const int flags)
56 const struct cli_alias *alias = obj;
57 return ast_str_hash(alias->cli_entry.command);
60 /*! \brief Comparison function used for aliases */
61 static int alias_cmp_cb(void *obj, void *arg, int flags)
63 const struct cli_alias *alias0 = obj, *alias1 = arg;
65 return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
68 /*! \brief Destruction function used for aliases */
69 static void alias_destroy(void *obj)
71 struct cli_alias *alias = obj;
73 /* Unregister the CLI entry from the core */
74 ast_cli_unregister(&alias->cli_entry);
79 /*! \brief Function which passes through an aliased CLI command to the real one */
80 static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
82 struct cli_alias *alias;
83 struct cli_alias tmp = {
84 .cli_entry.command = e->command,
89 /* Try to find the alias based on the CLI entry */
90 if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
100 line += (strlen(alias->alias));
101 if (!strncmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
103 } else if (!ast_strlen_zero(a->word)) {
104 struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
105 ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
106 generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
108 generator = ast_cli_generator(alias->real_cmd, a->word, a->n);
114 /* If they gave us extra arguments we need to construct a string to pass in */
115 if (a->argc != e->args) {
116 struct ast_str *real_cmd = ast_str_alloca(2048);
119 ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
121 /* Add the additional arguments that have been passed in */
122 for (i = e->args + 1; i <= a->argc; i++) {
123 ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
126 ast_cli_command(a->fd, ast_str_buffer(real_cmd));
128 ast_cli_command(a->fd, alias->real_cmd);
136 /*! \brief CLI Command to display CLI Aliases */
137 static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
139 #define FORMAT "%-50.50s %-50.50s\n"
140 struct cli_alias *alias;
141 struct ao2_iterator i;
145 e->command = "cli show aliases";
147 "Usage: cli show aliases\n"
148 " Displays a list of aliased CLI commands.\n";
154 ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
156 i = ao2_iterator_init(cli_aliases, 0);
158 for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
159 ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
166 /*! \brief CLI commands to interact with things */
167 static struct ast_cli_entry cli_alias[] = {
168 AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
171 /*! \brief Function called to to see if an alias is marked for destruction, they always are! */
172 static int alias_marked(void *obj, void *arg, int flags)
177 /*! \brief Function called to load or reload the configuration file */
178 static void load_config(int reload)
180 struct ast_config *cfg = NULL;
181 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
182 struct cli_alias *alias;
183 struct ast_variable *v, *v1;
185 if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
186 ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
188 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
192 /* Destroy any existing CLI aliases */
194 ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE , alias_marked, NULL);
197 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
198 if (strcmp(v->name, "template")) {
199 ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
202 /* Read in those there CLI aliases */
203 for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
204 if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), alias_destroy))) {
207 alias->alias = ((char *) alias) + sizeof(*alias);
208 alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
209 strcpy(alias->alias, v1->name);
210 strcpy(alias->real_cmd, v1->value);
211 alias->cli_entry.handler = cli_alias_passthrough;
212 alias->cli_entry.command = alias->alias;
213 alias->cli_entry.usage = "Aliased CLI Command";
215 ast_cli_register(&alias->cli_entry);
216 ao2_link(cli_aliases, alias);
217 ast_verbose(VERBOSE_PREFIX_2 "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
222 ast_config_destroy(cfg);
227 /*! \brief Function called to reload the module */
228 static int reload_module(void)
234 /*! \brief Function called to unload the module */
235 static int unload_module(void)
237 ao2_ref(cli_aliases, -1);
239 ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
244 /*! \brief Function called to load the module */
245 static int load_module(void)
247 if (!(cli_aliases = ao2_container_alloc(MAX_ALIAS_BUCKETS, alias_hash_cb, alias_cmp_cb))) {
248 return AST_MODULE_LOAD_DECLINE;
253 ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
255 return AST_MODULE_LOAD_SUCCESS;
258 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases",
260 .unload = unload_module,
261 .reload = reload_module,