Wed Jan 8 2020 09:49:49

Asterisk developer's documentation


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 /*** MODULEINFO
30  <support_level>core</support_level>
31  ***/
32 
33 #include "asterisk.h"
34 
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 407205 $")
36 
37 #include "asterisk/module.h"
38 #include "asterisk/config.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/astobj2.h"
41 
42 /*! Maximum number of buckets for CLI aliases */
43 #define MAX_ALIAS_BUCKETS 53
44 
45 /*! Configuration file used for this application */
46 static const char config_file[] = "cli_aliases.conf";
47 
48 struct cli_alias {
49  struct ast_cli_entry cli_entry; /*!< Actual CLI structure used for this alias */
50  char *alias; /*!< CLI Alias */
51  char *real_cmd; /*!< Actual CLI command it is aliased to */
52 };
53 
54 static struct ao2_container *cli_aliases;
55 
56 /*! \brief Hashing function used for aliases */
57 static int alias_hash_cb(const void *obj, const int flags)
58 {
59  const struct cli_alias *alias = obj;
60  return ast_str_hash(alias->cli_entry.command);
61 }
62 
63 /*! \brief Comparison function used for aliases */
64 static int alias_cmp_cb(void *obj, void *arg, int flags)
65 {
66  const struct cli_alias *alias0 = obj, *alias1 = arg;
67 
68  return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
69 }
70 
71 /*! \brief Callback for unregistering an alias */
72 static int alias_unregister_cb(void *obj, void *arg, int flags)
73 {
74  struct cli_alias *alias = obj;
75 
76  /* Unregister the CLI entry from the core */
78 
79  /* We can determine if this worked or not by looking at the cli_entry itself */
80  return !alias->cli_entry.command ? CMP_MATCH : 0;
81 }
82 
83 /*! \brief Callback for finding an alias based on name */
84 static int alias_name_cb(void *obj, void *arg, int flags)
85 {
86  struct cli_alias *alias = obj;
87  char *name = arg;
88 
89  return !strcmp(alias->alias, name) ? CMP_MATCH | CMP_STOP : 0;
90 }
91 
92 /*! \brief Function which passes through an aliased CLI command to the real one */
93 static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
94 {
95  struct cli_alias *alias;
96  struct cli_alias tmp = {
98  };
99  char *generator;
100  const char *line;
101 
102  /* Try to find the alias based on the CLI entry */
103  if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
104  return 0;
105  }
106 
107  switch (cmd) {
108  case CLI_INIT:
109  ao2_ref(alias, -1);
110  return NULL;
111  case CLI_GENERATE:
112  line = a->line;
113  line += (strlen(alias->alias));
114  if (!strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
115  generator = NULL;
116  } else if (!ast_strlen_zero(a->word)) {
117  struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
118  ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
119  generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
120  } else {
121  generator = ast_cli_generator(alias->real_cmd, a->word, a->n);
122  }
123  ao2_ref(alias, -1);
124  return generator;
125  }
126 
127  /* If they gave us extra arguments we need to construct a string to pass in */
128  if (a->argc != e->args) {
129  struct ast_str *real_cmd = ast_str_alloca(2048);
130  int i;
131 
132  ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
133 
134  /* Add the additional arguments that have been passed in */
135  for (i = e->args + 1; i <= a->argc; i++) {
136  ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
137  }
138 
139  ast_cli_command(a->fd, ast_str_buffer(real_cmd));
140  } else {
141  ast_cli_command(a->fd, alias->real_cmd);
142  }
143 
144  ao2_ref(alias, -1);
145 
146  return CLI_SUCCESS;
147 }
148 
149 /*! \brief CLI Command to display CLI Aliases */
150 static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
151 {
152 #define FORMAT "%-50.50s %-50.50s\n"
153  struct cli_alias *alias;
154  struct ao2_iterator i;
155 
156  switch (cmd) {
157  case CLI_INIT:
158  e->command = "cli show aliases";
159  e->usage =
160  "Usage: cli show aliases\n"
161  " Displays a list of aliased CLI commands.\n";
162  return NULL;
163  case CLI_GENERATE:
164  return NULL;
165  }
166 
167  ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
168 
169  i = ao2_iterator_init(cli_aliases, 0);
170  for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
171  ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
172  }
174 
175  return CLI_SUCCESS;
176 #undef FORMAT
177 }
178 
179 /*! \brief CLI commands to interact with things */
180 static struct ast_cli_entry cli_alias[] = {
181  AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
182 };
183 
184 /*! \brief Function called to load or reload the configuration file */
185 static void load_config(int reload)
186 {
187  struct ast_config *cfg = NULL;
188  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
189  struct cli_alias *alias;
190  struct ast_variable *v, *v1;
191 
192  if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
193  ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
194  return;
195  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
196  return;
197  }
198 
199  /* Destroy any existing CLI aliases */
200  if (reload) {
202  }
203 
204  for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
205  if (strcmp(v->name, "template")) {
206  ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
207  continue;
208  }
209  /* Read in those there CLI aliases */
210  for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
211  struct cli_alias *existing = ao2_callback(cli_aliases, 0, alias_name_cb, (char*)v1->name);
212 
213  if (existing) {
214  ast_log(LOG_WARNING, "Alias '%s' could not be unregistered and has been retained\n",
215  existing->alias);
216  ao2_ref(existing, -1);
217  continue;
218  }
219 
220  if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), NULL))) {
221  continue;
222  }
223  alias->alias = ((char *) alias) + sizeof(*alias);
224  alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
225  strcpy(alias->alias, v1->name);
226  strcpy(alias->real_cmd, v1->value);
228  alias->cli_entry.command = alias->alias;
229  alias->cli_entry.usage = "Aliased CLI Command\n";
230 
231  if (ast_cli_register(&alias->cli_entry)) {
232  ao2_ref(alias, -1);
233  continue;
234  }
235  ao2_link(cli_aliases, alias);
236  ast_verbose(VERBOSE_PREFIX_2 "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
237  ao2_ref(alias, -1);
238  }
239  }
240 
241  ast_config_destroy(cfg);
242 
243  return;
244 }
245 
246 /*! \brief Function called to reload the module */
247 static int reload_module(void)
248 {
249  load_config(1);
250  return 0;
251 }
252 
253 /*! \brief Function called to unload the module */
254 static int unload_module(void)
255 {
257 
258  if (ao2_container_count(cli_aliases)) {
259  ast_log(LOG_ERROR, "Could not unregister all CLI aliases\n");
260  return -1;
261  }
262 
263  ao2_ref(cli_aliases, -1);
264 
265  ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
266 
267  return 0;
268 }
269 
270 /*! \brief Function called to load the module */
271 static int load_module(void)
272 {
275  }
276 
277  load_config(0);
278 
279  ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
280 
282 }
283 
285  .load = load_module,
286  .unload = unload_module,
288  );
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static void load_config(int reload)
Function called to load or reload the configuration file.
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_link(arg1, arg2)
Definition: astobj2.h:785
static char * alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI Command to display CLI Aliases.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Definition: astobj2.c:470
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * alias
static int alias_name_cb(void *obj, void *arg, int flags)
Callback for finding an alias based on name.
int ast_cli_register(struct ast_cli_entry *e)
Registers a command or an array of commands.
Definition: cli.c:2159
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
#define ao2_iterator_next(arg1)
Definition: astobj2.h:1126
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: cli.c:2153
descriptor for a cli entry.
Definition: cli.h:165
const int argc
Definition: cli.h:154
#define LOG_WARNING
Definition: logger.h:144
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:910
void ast_verbose(const char *fmt,...)
Definition: logger.c:1568
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
Definition: cli.h:146
Configuration File Parser.
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:900
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
Create an iterator for a container.
Definition: astobj2.c:818
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
#define ast_str_alloca(init_len)
Definition: strings.h:608
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
const char * line
Definition: cli.h:156
static int unload_module(void)
Function called to unload the module.
int args
This gets set in ast_cli_register()
Definition: cli.h:179
static const char config_file[]
static int alias_cmp_cb(void *obj, void *arg, int flags)
Comparison function used for aliases.
const char * value
Definition: config.h:79
#define VERBOSE_PREFIX_2
Definition: logger.h:42
const int fd
Definition: cli.h:153
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const int n
Definition: cli.h:159
#define ao2_ref(o, delta)
Definition: astobj2.h:472
const char * name
Definition: config.h:77
ast_cli_command
calling arguments for new-style handlers.
Definition: cli.h:145
const char *const * argv
Definition: cli.h:155
static int reload(void)
Definition: app_amd.c:497
static int alias_unregister_cb(void *obj, void *arg, int flags)
Callback for unregistering an alias.
#define MAX_ALIAS_BUCKETS
#define LOG_ERROR
Definition: logger.h:155
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
char *(* handler)(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: cli.h:181
static struct ast_generator generator
Definition: app_fax.c:361
char * real_cmd
struct ast_cli_entry cli_entry
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
#define ao2_find(arg1, arg2, arg3)
Definition: astobj2.h:964
static int reload_module(void)
Function called to reload the module.
static const char name[]
char * command
Definition: cli.h:180
const char * word
Definition: cli.h:157
static int load_module(void)
Function called to load the module.
char * ast_cli_generator(const char *, const char *, int)
Readline madness Useful for readline, that&#39;s about it.
Definition: cli.c:2525
void ao2_iterator_destroy(struct ao2_iterator *i)
Destroy a container iterator.
Definition: astobj2.c:833
Structure used to handle boolean flags.
Definition: utils.h:200
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1053
Standard Command Line Interface.
#define ao2_container_alloc(arg1, arg2, arg3)
Definition: astobj2.h:734
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
#define FORMAT
struct ast_variable * next
Definition: config.h:82
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static int alias_hash_cb(const void *obj, const int flags)
Hashing function used for aliases.
static struct ao2_container * cli_aliases
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.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define CONFIG_STATUS_FILEUNCHANGED
Definition: config.h:51
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:949