Wed Apr 6 11:30:08 2011

Asterisk developer's documentation


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"

Go to the source code of this file.

Data Structures

struct  cli_alias

Defines

#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.
static void alias_destroy (void *obj)
 Destruction 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 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 void load_config (int reload)
 Function called to load or reload the configuration file.
static int load_module (void)
 Function called to load the module.
static int reload_module (void)
 Function called to reload the module.
static int unload_module (void)
 Function called to unload the module.

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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_cli_entry cli_alias []
 CLI commands to interact with things.
static struct ao2_containercli_aliases
static const char config_file [] = "cli_aliases.conf"


Detailed Description

CLI Aliases.

Author:
Joshua Colp <jcolp@digium.com> 
This module provides the capability to create aliases to other CLI commands.

Definition in file res_clialiases.c.


Define Documentation

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

#define MAX_ALIAS_BUCKETS   53

Maximum number of buckets for CLI aliases

Definition at line 40 of file res_clialiases.c.

Referenced by load_module().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 256 of file res_clialiases.c.

static void __unreg_module ( void   )  [static]

Definition at line 256 of file res_clialiases.c.

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

Comparison function used for aliases.

Definition at line 61 of file res_clialiases.c.

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

Referenced by load_module().

00062 {
00063    const struct cli_alias *alias0 = obj, *alias1 = arg;
00064 
00065    return (alias0->cli_entry.command == alias1->cli_entry.command ? CMP_MATCH | CMP_STOP : 0);
00066 }

static void alias_destroy ( void *  obj  )  [static]

Destruction function used for aliases.

Definition at line 69 of file res_clialiases.c.

References cli_alias::alias, and ast_cli_unregister().

Referenced by load_config().

00070 {
00071    struct cli_alias *alias = obj;
00072 
00073    /* Unregister the CLI entry from the core */
00074    ast_cli_unregister(&alias->cli_entry);
00075 
00076    return;
00077 }

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

Hashing function used for aliases.

Definition at line 54 of file res_clialiases.c.

References cli_alias::alias, and ast_str_hash().

Referenced by load_module().

00055 {
00056    const struct cli_alias *alias = obj;
00057    return ast_str_hash(alias->cli_entry.command);
00058 }

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 137 of file res_clialiases.c.

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

00138 {
00139 #define FORMAT "%-50.50s %-50.50s\n"
00140    struct cli_alias *alias;
00141    struct ao2_iterator i;
00142 
00143    switch (cmd) {
00144    case CLI_INIT:
00145       e->command = "cli show aliases";
00146       e->usage =
00147          "Usage: cli show aliases\n"
00148          "       Displays a list of aliased CLI commands.\n";
00149       return NULL;
00150    case CLI_GENERATE:
00151       return NULL;
00152    }
00153 
00154    ast_cli(a->fd, FORMAT, "Alias Command", "Real Command");
00155 
00156    i = ao2_iterator_init(cli_aliases, 0);
00157    for (; (alias = ao2_iterator_next(&i)); ao2_ref(alias, -1)) {
00158       ast_cli(a->fd, FORMAT, alias->alias, alias->real_cmd);
00159    }
00160    ao2_iterator_destroy(&i);
00161 
00162    return CLI_SUCCESS;
00163 #undef FORMAT
00164 }

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 80 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_command, ast_cli_generator(), ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_strlen_zero(), cli_aliases, 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, OBJ_POINTER, cli_alias::real_cmd, and ast_cli_args::word.

00081 {
00082    struct cli_alias *alias;
00083    struct cli_alias tmp = {
00084       .cli_entry.command = e->command,
00085    };
00086    char *generator;
00087    const char *line;
00088 
00089    /* Try to find the alias based on the CLI entry */
00090    if (!(alias = ao2_find(cli_aliases, &tmp, OBJ_POINTER))) {
00091       return 0;
00092    }
00093 
00094    switch (cmd) {
00095    case CLI_INIT:
00096       ao2_ref(alias, -1);
00097       return NULL;
00098    case CLI_GENERATE:
00099       line = a->line;
00100       line += (strlen(alias->alias));
00101       if (!strncasecmp(alias->alias, alias->real_cmd, strlen(alias->alias))) {
00102          generator = NULL;
00103       } else if (!ast_strlen_zero(a->word)) {
00104          struct ast_str *real_cmd = ast_str_alloca(strlen(alias->real_cmd) + strlen(line) + 1);
00105          ast_str_append(&real_cmd, 0, "%s%s", alias->real_cmd, line);
00106          generator = ast_cli_generator(ast_str_buffer(real_cmd), a->word, a->n);
00107       } else {
00108          generator = ast_cli_generator(alias->real_cmd, a->word, a->n);
00109       }
00110       ao2_ref(alias, -1);
00111       return generator;
00112    }
00113 
00114    /* If they gave us extra arguments we need to construct a string to pass in */
00115    if (a->argc != e->args) {
00116       struct ast_str *real_cmd = ast_str_alloca(2048);
00117       int i;
00118 
00119       ast_str_append(&real_cmd, 0, "%s", alias->real_cmd);
00120 
00121       /* Add the additional arguments that have been passed in */
00122       for (i = e->args + 1; i <= a->argc; i++) {
00123          ast_str_append(&real_cmd, 0, " %s", a->argv[i - 1]);
00124       }
00125 
00126       ast_cli_command(a->fd, ast_str_buffer(real_cmd));
00127    } else {
00128       ast_cli_command(a->fd, alias->real_cmd);
00129    }
00130 
00131    ao2_ref(alias, -1);
00132 
00133    return CLI_SUCCESS;
00134 }

static void load_config ( int  reload  )  [static]

Function called to load or reload the configuration file.

Definition at line 172 of file res_clialiases.c.

References cli_alias::alias, alias_destroy(), ao2_alloc, ao2_callback, ast_config_load, ast_log(), ast_variable_browse(), cli_alias, cli_aliases, CONFIG_FLAG_FILEUNCHANGED, config_flags, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, LOG_WARNING, ast_variable::name, ast_variable::next, OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, and ast_variable::value.

00173 {
00174    struct ast_config *cfg = NULL;
00175    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00176    struct cli_alias *alias;
00177    struct ast_variable *v, *v1;
00178 
00179    if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
00180       ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
00181       return;
00182    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00183       return;
00184    }
00185 
00186    /* Destroy any existing CLI aliases */
00187    if (reload) {
00188       ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
00189    }
00190 
00191    for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
00192       if (strcmp(v->name, "template")) {
00193          ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
00194          continue;
00195       }
00196       /* Read in those there CLI aliases */
00197       for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
00198          if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), alias_destroy))) {
00199             continue;
00200          }
00201          alias->alias = ((char *) alias) + sizeof(*alias);
00202          alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
00203          strcpy(alias->alias, v1->name);
00204          strcpy(alias->real_cmd, v1->value);
00205          alias->cli_entry.handler = cli_alias_passthrough;
00206          alias->cli_entry.command = alias->alias;
00207          alias->cli_entry.usage = "Aliased CLI Command\n";
00208 
00209          ast_cli_register(&alias->cli_entry);
00210          ao2_link(cli_aliases, alias);
00211          ast_verbose(VERBOSE_PREFIX_2 "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
00212          ao2_ref(alias, -1);
00213       }
00214    }
00215 
00216    ast_config_destroy(cfg);
00217 
00218    return;
00219 }

static int load_module ( void   )  [static]

Function called to load the module.

Definition at line 239 of file res_clialiases.c.

References alias_cmp_cb(), alias_hash_cb(), ao2_container_alloc, ARRAY_LEN, ast_cli_register_multiple(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, cli_aliases, load_config(), and MAX_ALIAS_BUCKETS.

00240 {
00241    if (!(cli_aliases = ao2_container_alloc(MAX_ALIAS_BUCKETS, alias_hash_cb, alias_cmp_cb))) {
00242       return AST_MODULE_LOAD_DECLINE;
00243    }
00244 
00245    load_config(0);
00246 
00247    ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
00248 
00249    return AST_MODULE_LOAD_SUCCESS;
00250 }

static int reload_module ( void   )  [static]

Function called to reload the module.

Definition at line 222 of file res_clialiases.c.

References load_config().

Referenced by load_module().

00223 {
00224    load_config(1);
00225    return 0;
00226 }

static int unload_module ( void   )  [static]

Function called to unload the module.

Definition at line 229 of file res_clialiases.c.

References ao2_ref, ARRAY_LEN, ast_cli_unregister_multiple(), and cli_aliases.

00230 {
00231    ao2_ref(cli_aliases, -1);
00232 
00233    ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
00234 
00235    return 0;
00236 }


Variable Documentation

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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload_module, } [static]

Definition at line 256 of file res_clialiases.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 256 of file res_clialiases.c.

struct ast_cli_entry cli_alias[] [static]

Initial value:

 {
   { .handler =  alias_show , .summary =  "Show CLI command aliases" ,__VA_ARGS__ },
}
CLI commands to interact with things.

Definition at line 167 of file res_clialiases.c.

Referenced by load_config().

struct ao2_container* cli_aliases [static]

Definition at line 51 of file res_clialiases.c.

Referenced by alias_show(), cli_alias_passthrough(), load_config(), load_module(), and unload_module().

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

Configuration file used for this application

Definition at line 43 of file res_clialiases.c.


Generated on Wed Apr 6 11:30:08 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7