00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 252850 $")
00033
00034 #include "asterisk/module.h"
00035 #include "asterisk/config.h"
00036 #include "asterisk/cli.h"
00037 #include "asterisk/astobj2.h"
00038
00039
00040 #define MAX_ALIAS_BUCKETS 53
00041
00042
00043 static const char config_file[] = "cli_aliases.conf";
00044
00045 struct cli_alias {
00046 struct ast_cli_entry cli_entry;
00047 char *alias;
00048 char *real_cmd;
00049 };
00050
00051 static struct ao2_container *cli_aliases;
00052
00053
00054 static int alias_hash_cb(const void *obj, const int flags)
00055 {
00056 const struct cli_alias *alias = obj;
00057 return ast_str_hash(alias->cli_entry.command);
00058 }
00059
00060
00061 static int alias_cmp_cb(void *obj, void *arg, int flags)
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 }
00067
00068
00069 static void alias_destroy(void *obj)
00070 {
00071 struct cli_alias *alias = obj;
00072
00073
00074 ast_cli_unregister(&alias->cli_entry);
00075
00076 return;
00077 }
00078
00079
00080 static char *cli_alias_passthrough(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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
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
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
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 }
00135
00136
00137 static char *alias_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
00165
00166
00167 static struct ast_cli_entry cli_alias[] = {
00168 AST_CLI_DEFINE(alias_show, "Show CLI command aliases"),
00169 };
00170
00171
00172 static int alias_marked(void *obj, void *arg, int flags)
00173 {
00174 return CMP_MATCH;
00175 }
00176
00177
00178 static void load_config(int reload)
00179 {
00180 struct ast_config *cfg = NULL;
00181 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00182 struct cli_alias *alias;
00183 struct ast_variable *v, *v1;
00184
00185 if (!(cfg = ast_config_load(config_file, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
00186 ast_log(LOG_ERROR, "res_clialiases configuration file '%s' not found\n", config_file);
00187 return;
00188 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
00189 return;
00190 }
00191
00192
00193 if (reload) {
00194 ao2_callback(cli_aliases, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE , alias_marked, NULL);
00195 }
00196
00197 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
00198 if (strcmp(v->name, "template")) {
00199 ast_log(LOG_WARNING, "%s is not a correct option in [%s]\n", v->name, "general");
00200 continue;
00201 }
00202
00203 for (v1 = ast_variable_browse(cfg, v->value); v1; v1 = v1->next) {
00204 if (!(alias = ao2_alloc((sizeof(*alias) + strlen(v1->name) + strlen(v1->value) + 2), alias_destroy))) {
00205 continue;
00206 }
00207 alias->alias = ((char *) alias) + sizeof(*alias);
00208 alias->real_cmd = ((char *) alias->alias) + strlen(v1->name) + 1;
00209 strcpy(alias->alias, v1->name);
00210 strcpy(alias->real_cmd, v1->value);
00211 alias->cli_entry.handler = cli_alias_passthrough;
00212 alias->cli_entry.command = alias->alias;
00213 alias->cli_entry.usage = "Aliased CLI Command\n";
00214
00215 ast_cli_register(&alias->cli_entry);
00216 ao2_link(cli_aliases, alias);
00217 ast_verbose(VERBOSE_PREFIX_2 "Aliased CLI command '%s' to '%s'\n", v1->name, v1->value);
00218 ao2_ref(alias, -1);
00219 }
00220 }
00221
00222 ast_config_destroy(cfg);
00223
00224 return;
00225 }
00226
00227
00228 static int reload_module(void)
00229 {
00230 load_config(1);
00231 return 0;
00232 }
00233
00234
00235 static int unload_module(void)
00236 {
00237 ao2_ref(cli_aliases, -1);
00238
00239 ast_cli_unregister_multiple(cli_alias, ARRAY_LEN(cli_alias));
00240
00241 return 0;
00242 }
00243
00244
00245 static int load_module(void)
00246 {
00247 if (!(cli_aliases = ao2_container_alloc(MAX_ALIAS_BUCKETS, alias_hash_cb, alias_cmp_cb))) {
00248 return AST_MODULE_LOAD_DECLINE;
00249 }
00250
00251 load_config(0);
00252
00253 ast_cli_register_multiple(cli_alias, ARRAY_LEN(cli_alias));
00254
00255 return AST_MODULE_LOAD_SUCCESS;
00256 }
00257
00258 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "CLI Aliases",
00259 .load = load_module,
00260 .unload = unload_module,
00261 .reload = reload_module,
00262 );