Asterisk - The Open Source Telephony Project GIT-master-f36a736
res_clialiases.c
Go to the documentation of this file.
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
19/*! \file
20 *
21 * \brief CLI Aliases
22 *
23 * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
24 *
25 * This module provides the capability to create aliases to other
26 * CLI commands.
27 */
28
29/*! \li \ref res_clialiases.c uses the configuration file \ref cli_aliases.conf
30 * \addtogroup configuration_file Configuration Files
31 */
32
33/*!
34 * \page cli_aliases.conf cli_aliases.conf
35 * \verbinclude cli_aliases.conf.sample
36 */
37
38/*** MODULEINFO
39 <support_level>core</support_level>
40 ***/
41
42#include "asterisk.h"
43
44#include "asterisk/module.h"
45#include "asterisk/config.h"
46#include "asterisk/cli.h"
47#include "asterisk/astobj2.h"
48
49/*! Maximum number of buckets for CLI aliases */
50#define MAX_ALIAS_BUCKETS 53
51
52/*! Configuration file used for this application */
53static const char config_file[] = "cli_aliases.conf";
54
55struct cli_alias {
56 struct ast_cli_entry cli_entry; /*!< Actual CLI structure used for this alias */
57 char *alias; /*!< CLI Alias */
58 char *real_cmd; /*!< Actual CLI command it is aliased to */
59};
60
62
63/*! \brief Hashing function used for aliases */
64static int alias_hash_cb(const void *obj, const int flags)
65{
66 const struct cli_alias *alias = obj;
67 return ast_str_hash(alias->cli_entry.command);
68}
69
70/*! \brief Comparison function used for aliases */
71static int alias_cmp_cb(void *obj, void *arg, int flags)
72{
73 const struct cli_alias *alias0 = obj, *alias1 = arg;
74
75 return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
76}
77
78/*! \brief Callback for unregistering an alias */
79static int alias_unregister_cb(void *obj, void *arg, int flags)
80{
81 struct cli_alias *alias = obj;
82
83 /* Unregister the CLI entry from the core */
84 ast_cli_unregister(&alias->cli_entry);
85
86 /* We can determine if this worked or not by looking at the cli_entry itself */
87 return !alias->cli_entry.command ? CMP_MATCH : 0;
88}
89
90/*! \brief Callback for finding an alias based on name */
91static int alias_name_cb(void *obj, void *arg, int flags)
92{
93 struct cli_alias *alias = obj;
94 char *name = arg;
95
96 return !strcmp(alias->alias, name) ? CMP_MATCH | CMP_STOP : 0;
97}
98
99/*! \brief Function which passes through an aliased CLI command to the real one */
100static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
101{
102 struct cli_alias *alias;
103 struct cli_alias tmp = {
104 .cli_entry.command = e->command,
105 };
106 char *generator = NULL;
107 const char *line;
108
109 /* Try to find the alias based on the CLI entry */
111 return 0;
112 }
113
114 switch (cmd) {
115 case CLI_INIT:
116 ao2_ref(alias, -1);
117 return NULL;
118 case CLI_GENERATE:
119 line = a->line;
120 line += (strlen(alias->alias));
121 if (strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
122 struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
123 ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
124 generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
125 }
126 ao2_ref(alias, -1);
127 return generator;
128 }
129
130 /* If they gave us extra arguments we need to construct a string to pass in */
131 if (a->argc != e->args) {
132 struct ast_str *real_cmd = ast_str_alloca(2048);
133 int i;
134
135 ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
136
137 /* Add the additional arguments that have been passed in */
138 for (i = e->args + 1; i <= a->argc; i++) {
139 ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
140 }
141
142 ast_cli_command(a->fd, ast_str_buffer(real_cmd));
143 } else {
144 ast_cli_command(a->fd, alias->real_cmd);
145 }
146
147 ao2_ref(alias, -1);
148
149 return CLI_SUCCESS;
150}
151
152/*! \brief CLI Command to display CLI Aliases */
153static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
154{
155#define FORMAT "%-50.50s %-50.50s\n"
156 struct cli_alias *alias;
157 struct ao2_iterator i;
158
159 switch (cmd) {
160 case CLI_INIT:
161 e->command = "cli show aliases";
162 e->usage =
163 "Usage: cli show aliases\n"
164 " Displays a list of aliased CLI commands.\n";
165 return NULL;
166 case CLI_GENERATE:
167 return NULL;
168 }
169
170 ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
171
173 for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
174 ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
175 }
177
178 return CLI_SUCCESS;
179#undef FORMAT
180}
181
182/*! \brief CLI commands to interact with things */
183static struct ast_cli_entry cli_alias[] = {
184 AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
185};
186
187/*! \brief Function called to load or reload the configuration file */
188static void load_config(int reload)
189{
190 struct ast_config *cfg = NULL;
191 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
192 struct cli_alias *alias;
193 struct ast_variable *v, *v1;
194
195 if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
196 ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
197 return;
198 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
199 return;
200 }
201
202 /* Destroy any existing CLI aliases */
203 if (reload) {
205 }
206
207 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
208 if (strcmp(v->name, "template")) {
209 ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
210 continue;
211 }
212 /* Read in those there CLI aliases */
213 for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
214 struct cli_alias *existing = ao2_callback(cli_aliases, 0, alias_name_cb, (char*)v1->name);
215
216 if (existing) {
217 ast_log(LOG_WARNING, "Alias '%s' could not be unregistered and has been retained\n",
218 existing->alias);
219 ao2_ref(existing, -1);
220 continue;
221 }
222
223 if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), NULL))) {
224 continue;
225 }
226 alias->alias = ((char *) alias) + sizeof(*alias);
227 alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
228 strcpy(alias->alias, v1->name);
229 strcpy(alias->real_cmd, v1->value);
230 alias->cli_entry.handler = cli_alias_passthrough;
231 alias->cli_entry.command = alias->alias;
232 alias->cli_entry.usage = "Aliased CLI Command\n";
233
234 if (ast_cli_register(&alias->cli_entry)) {
235 ao2_ref(alias, -1);
236 continue;
237 }
239 ast_verb(5, "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
240 ao2_ref(alias, -1);
241 }
242 }
243
245
246 return;
247}
248
249/*! \brief Function called to reload the module */
250static int reload_module(void)
251{
252 load_config(1);
253 return 0;
254}
255
256/*! \brief Function called to unload the module */
257static int unload_module(void)
258{
260
262 ast_log(LOG_ERROR, "Could not unregister all CLI aliases\n");
263 return -1;
264 }
265
266 ao2_ref(cli_aliases, -1);
267
269
270 return 0;
271}
272
273/*!
274 * \brief Load the module
275 *
276 * Module loading including tests for configuration or dependencies.
277 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
278 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
279 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
280 * configuration file or other non-critical problem return
281 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
282 */
283static int load_module(void)
284{
287 if (!cli_aliases) {
289 }
290
291 load_config(0);
292
294
296}
297
299 .support_level = AST_MODULE_SUPPORT_CORE,
300 .load = load_module,
301 .unload = unload_module,
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
#define OBJ_POINTER
Definition: astobj2.h:1150
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
@ OBJ_UNLINK
Definition: astobj2.h:1039
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
static int tmp()
Definition: bt_open.c:389
Standard Command Line Interface.
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2439
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
char * ast_cli_generator(const char *, const char *, int)
Readline madness Useful for readline, that's about it.
Definition: main/cli.c:2959
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
ast_cli_command
calling arguments for new-style handlers.
Definition: cli.h:151
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static const char name[]
Definition: format_mp3.c:68
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_FILEUNCHANGED
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int alias_cmp_cb(void *obj, void *arg, int flags)
Comparison function used for aliases.
static int alias_hash_cb(const void *obj, const int flags)
Hashing function used for aliases.
static char * alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI Command to display CLI Aliases.
static int reload_module(void)
Function called to reload the module.
static int alias_name_cb(void *obj, void *arg, int flags)
Callback for finding an alias based on name.
static void load_config(int reload)
Function called to load or reload the configuration file.
static struct ao2_container * cli_aliases
#define MAX_ALIAS_BUCKETS
static const char config_file[]
static int load_module(void)
Load the module.
#define FORMAT
static int unload_module(void)
Function called to unload the module.
static char * cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Function which passes through an aliased CLI command to the real one.
static int alias_unregister_cb(void *obj, void *arg, int flags)
Callback for unregistering an alias.
static int reload(void)
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
#define ast_str_alloca(init_len)
Definition: strings.h:848
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
descriptor for a cli entry.
Definition: cli.h:171
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Structure used to handle boolean flags.
Definition: utils.h:199
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
struct ast_cli_entry cli_entry
char * real_cmd
char * alias
static struct test_val a
#define ARRAY_LEN(a)
Definition: utils.h:666