Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
res_clialiases.c File Reference

CLI Aliases. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/astobj2.h"
Include dependency graph for res_clialiases.c:

Go to the source code of this file.

Data Structures

struct  cli_alias
 

Macros

#define FORMAT   "%-50.50s %-50.50s\n"
 
#define MAX_ALIAS_BUCKETS   53
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int alias_cmp_cb (void *obj, void *arg, int flags)
 Comparison function used for aliases. More...
 
static int alias_hash_cb (const void *obj, const int flags)
 Hashing function used for aliases. More...
 
static int alias_name_cb (void *obj, void *arg, int flags)
 Callback for finding an alias based on name. More...
 
static char * alias_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI Command to display CLI Aliases. More...
 
static int alias_unregister_cb (void *obj, void *arg, int flags)
 Callback for unregistering an alias. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
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. More...
 
static void load_config (int reload)
 Function called to load or reload the configuration file. More...
 
static int load_module (void)
 Load the module. More...
 
static int reload_module (void)
 Function called to reload the module. More...
 
static int unload_module (void)
 Function called to unload the module. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "CLI Aliases" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_cli_entry cli_alias []
 CLI commands to interact with things. More...
 
static struct ao2_containercli_aliases
 
static const char config_file [] = "cli_aliases.conf"
 

Detailed Description

CLI Aliases.

Author
Joshua Colp <[email protected]> 

This module provides the capability to create aliases to other CLI commands.

Definition in file res_clialiases.c.

Macro Definition Documentation

◆ FORMAT

#define FORMAT   "%-50.50s %-50.50s\n"

Referenced by alias_show().

◆ MAX_ALIAS_BUCKETS

#define MAX_ALIAS_BUCKETS   53

Maximum number of buckets for CLI aliases

Definition at line 50 of file res_clialiases.c.

Referenced by load_module().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 303 of file res_clialiases.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 303 of file res_clialiases.c.

◆ alias_cmp_cb()

static int alias_cmp_cb ( void *  obj,
void *  arg,
int  flags 
)
static

Comparison function used for aliases.

Definition at line 71 of file res_clialiases.c.

References cli_alias::cli_entry, CMP_MATCH, CMP_STOP, and ast_cli_entry::command.

Referenced by load_module().

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 }
struct ast_cli_entry cli_entry
char * command
Definition: cli.h:186

◆ alias_hash_cb()

static int alias_hash_cb ( const void *  obj,
const int  flags 
)
static

Hashing function used for aliases.

Definition at line 64 of file res_clialiases.c.

References cli_alias::alias, ast_str_hash(), cli_alias::cli_entry, and ast_cli_entry::command.

Referenced by load_module().

65 {
66  const struct cli_alias *alias = obj;
67  return ast_str_hash(alias->cli_entry.command);
68 }
char * alias
struct ast_cli_entry cli_entry
char * command
Definition: cli.h:186
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206

◆ alias_name_cb()

static int alias_name_cb ( void *  obj,
void *  arg,
int  flags 
)
static

Callback for finding an alias based on name.

Definition at line 91 of file res_clialiases.c.

References cli_alias::alias, CMP_MATCH, CMP_STOP, and name.

Referenced by load_config().

92 {
93  struct cli_alias *alias = obj;
94  char *name = arg;
95 
96  return !strcmp(alias->alias, name) ? CMP_MATCH | CMP_STOP : 0;
97 }
char * alias
static const char name[]
Definition: cdr_mysql.c:74

◆ alias_show()

static char* alias_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

CLI Command to display CLI Aliases.

Definition at line 153 of file res_clialiases.c.

References cli_alias::alias, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, FORMAT, NULL, cli_alias::real_cmd, and ast_cli_entry::usage.

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 }
char * alias
Definition: cli.h:152
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const int fd
Definition: cli.h:159
#define ao2_ref(o, delta)
Definition: astobj2.h:464
char * real_cmd
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
#define FORMAT
static struct ao2_container * cli_aliases
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ alias_unregister_cb()

static int alias_unregister_cb ( void *  obj,
void *  arg,
int  flags 
)
static

Callback for unregistering an alias.

Definition at line 79 of file res_clialiases.c.

References cli_alias::alias, ast_cli_unregister(), cli_alias::cli_entry, CMP_MATCH, and ast_cli_entry::command.

Referenced by load_config(), and unload_module().

80 {
81  struct cli_alias *alias = obj;
82 
83  /* Unregister the CLI entry from the core */
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 }
char * alias
int ast_cli_unregister(struct ast_cli_entry *e)
Unregisters a command or an array of commands.
Definition: main/cli.c:2397
struct ast_cli_entry cli_entry
char * command
Definition: cli.h:186

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 303 of file res_clialiases.c.

◆ cli_alias_passthrough()

static char* cli_alias_passthrough ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Function which passes through an aliased CLI command to the real one.

Definition at line 100 of file res_clialiases.c.

References cli_alias::alias, ao2_find, ao2_ref, ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli_generator(), ast_str_alloca, ast_str_append(), ast_str_buffer(), cli_alias::cli_entry, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, generator, ast_cli_args::line, ast_cli_args::n, NULL, OBJ_POINTER, cli_alias::real_cmd, and ast_cli_args::word.

Referenced by load_config().

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 */
110  if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
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 }
char * alias
#define OBJ_POINTER
Definition: astobj2.h:1154
const int argc
Definition: cli.h:160
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static int tmp()
Definition: bt_open.c:389
Definition: cli.h:152
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:1091
#define ast_str_alloca(init_len)
Definition: strings.h:800
#define NULL
Definition: resample.c:96
const char * line
Definition: cli.h:162
int args
This gets set in ast_cli_register()
Definition: cli.h:185
const int fd
Definition: cli.h:159
const int n
Definition: cli.h:165
#define ao2_ref(o, delta)
Definition: astobj2.h:464
ast_cli_command
calling arguments for new-style handlers.
Definition: cli.h:151
const char *const * argv
Definition: cli.h:161
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static struct ast_generator generator
Definition: app_fax.c:359
char * real_cmd
char * command
Definition: cli.h:186
const char * word
Definition: cli.h:163
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
char * ast_cli_generator(const char *, const char *, int)
Readline madness Useful for readline, that&#39;s about it.
Definition: main/cli.c:2917
#define CLI_SUCCESS
Definition: cli.h:44
static struct ao2_container * cli_aliases

◆ load_config()

static void load_config ( int  reload)
static

Function called to load or reload the configuration file.

Definition at line 188 of file res_clialiases.c.

References cli_alias::alias, alias_name_cb(), alias_unregister_cb(), ao2_alloc, ao2_callback, ao2_link, ao2_ref, ast_cli_register, ast_config_destroy(), ast_config_load, ast_log, ast_variable_browse(), ast_verb, cli_alias_passthrough(), cli_alias::cli_entry, ast_cli_entry::command, config_file, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, ast_cli_entry::handler, LOG_ERROR, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, cli_alias::real_cmd, ast_cli_entry::usage, and ast_variable::value.

Referenced by load_module(), and reload_module().

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);
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  }
238  ao2_link(cli_aliases, alias);
239  ast_verb(2, "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
240  ao2_ref(alias, -1);
241  }
242  }
243 
244  ast_config_destroy(cfg);
245 
246  return;
247 }
struct ast_variable * next
char * alias
static int alias_name_cb(void *obj, void *arg, int flags)
Callback for finding an alias based on name.
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
#define LOG_WARNING
Definition: logger.h:274
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define CONFIG_STATUS_FILEINVALID
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
#define ast_verb(level,...)
Definition: logger.h:463
#define ast_cli_register(e)
Registers a command or an array of commands.
Definition: cli.h:256
static const char config_file[]
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
char *(* handler)(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: cli.h:187
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEUNCHANGED
static int alias_unregister_cb(void *obj, void *arg, int flags)
Callback for unregistering an alias.
#define LOG_ERROR
Definition: logger.h:285
char * real_cmd
struct ast_cli_entry cli_entry
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
char * command
Definition: cli.h:186
static int reload(void)
Definition: cdr_mysql.c:741
Structure used to handle boolean flags.
Definition: utils.h:199
const char * usage
Definition: cli.h:177
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 ao2_link(container, obj)
Definition: astobj2.h:1549

◆ load_module()

static int load_module ( void  )
static

Load the module.

Module loading including tests for configuration or dependencies. This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE, or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails tests return AST_MODULE_LOAD_FAILURE. If the module can not load the configuration file or other non-critical problem return AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.

Definition at line 283 of file res_clialiases.c.

References alias_cmp_cb(), alias_hash_cb(), AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ARRAY_LEN, ast_cli_register_multiple, AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, load_config(), MAX_ALIAS_BUCKETS, NULL, reload(), reload_module(), and unload_module().

284 {
287  if (!cli_aliases) {
289  }
290 
291  load_config(0);
292 
294 
296 }
static void load_config(int reload)
Function called to load or reload the configuration file.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define NULL
Definition: resample.c:96
static int alias_cmp_cb(void *obj, void *arg, int flags)
Comparison function used for aliases.
#define MAX_ALIAS_BUCKETS
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int alias_hash_cb(const void *obj, const int flags)
Hashing function used for aliases.
static struct ao2_container * cli_aliases

◆ reload_module()

static int reload_module ( void  )
static

Function called to reload the module.

Definition at line 250 of file res_clialiases.c.

References load_config().

Referenced by load_module().

251 {
252  load_config(1);
253  return 0;
254 }
static void load_config(int reload)
Function called to load or reload the configuration file.

◆ unload_module()

static int unload_module ( void  )
static

Function called to unload the module.

Definition at line 257 of file res_clialiases.c.

References alias_unregister_cb(), ao2_callback, ao2_container_count(), ao2_ref, ARRAY_LEN, ast_cli_unregister_multiple(), ast_log, LOG_ERROR, NULL, OBJ_MULTIPLE, OBJ_NODATA, and OBJ_UNLINK.

Referenced by load_module().

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 }
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static int alias_unregister_cb(void *obj, void *arg, int flags)
Callback for unregistering an alias.
#define LOG_ERROR
Definition: logger.h:285
static struct ao2_container * cli_aliases

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "CLI Aliases" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, }
static

Definition at line 303 of file res_clialiases.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 303 of file res_clialiases.c.

◆ cli_alias

struct ast_cli_entry cli_alias[]
static
Initial value:
= {
{ .handler = alias_show , .summary = "Show CLI command aliases" ,},
}
static char * alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI Command to display CLI Aliases.

CLI commands to interact with things.

Definition at line 183 of file res_clialiases.c.

◆ cli_aliases

struct ao2_container* cli_aliases
static

Definition at line 61 of file res_clialiases.c.

◆ config_file

const char config_file[] = "cli_aliases.conf"
static

Configuration file used for this application

Definition at line 53 of file res_clialiases.c.

Referenced by load_config().