restructure a function reducing nesting depth.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@25717 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Luigi Rizzo 20 years ago
parent 57d8591989
commit 3a9bd2a478

137
pbx.c

@ -2396,13 +2396,12 @@ int ast_context_remove_include(const char *context, const char *include, const c
int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar) int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar)
{ {
struct ast_include *i, *pi = NULL; struct ast_include *i, *pi = NULL;
int ret = -1;
if (ast_mutex_lock(&con->lock)) ast_mutex_lock(&con->lock);
return -1;
/* walk includes */
for (i = con->includes; i; pi = i, i = i->next) {
/* find our include */ /* find our include */
for (i = con->includes; i; pi = i, i = i->next) {
if (!strcmp(i->name, include) && if (!strcmp(i->name, include) &&
(!registrar || !strcmp(i->registrar, registrar))) { (!registrar || !strcmp(i->registrar, registrar))) {
/* remove from list */ /* remove from list */
@ -2412,14 +2411,13 @@ int ast_context_remove_include2(struct ast_context *con, const char *include, co
con->includes = i->next; con->includes = i->next;
/* free include and return */ /* free include and return */
free(i); free(i);
ast_mutex_unlock(&con->lock); ret = 0;
return 0; break;
} }
} }
/* we can't find the right include */
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
return -1; return ret;
} }
/*! /*!
@ -2997,7 +2995,22 @@ struct dialplan_counters {
int extension_existence; int extension_existence;
}; };
static int show_dialplan_helper(int fd, char *context, char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, char *includes[]) /*! \brief helper function to print an extension */
static void print_ext(struct ast_exten *e, char * buf, int buflen)
{
int prio = ast_get_extension_priority(e);
if (prio == PRIORITY_HINT) {
snprintf(buf, buflen, "hint: %s",
ast_get_extension_app(e));
} else {
snprintf(buf, buflen, "%d. %s(%s)",
prio, ast_get_extension_app(e),
(char *)ast_get_extension_app_data(e));
}
}
/* XXX not verified */
static int show_dialplan_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, char *includes[])
{ {
struct ast_context *c = NULL; struct ast_context *c = NULL;
int res = 0, old_total_exten = dpc->total_exten; int res = 0, old_total_exten = dpc->total_exten;
@ -3006,22 +3019,24 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
/* walk all contexts ... */ /* walk all contexts ... */
while ( (c = ast_walk_contexts(c)) ) { while ( (c = ast_walk_contexts(c)) ) {
/* show this context? */
if (!context ||
!strcmp(ast_get_context_name(c), context)) {
/* XXX re-indent this block */
struct ast_exten *e; struct ast_exten *e;
struct ast_include *i; struct ast_include *i;
struct ast_ignorepat *ip; struct ast_ignorepat *ip;
struct ast_sw *sw;
char buf[256], buf2[256]; char buf[256], buf2[256];
int context_info_printed = 0; int context_info_printed = 0;
if (context && strcmp(ast_get_context_name(c), context))
continue; /* skip this one, name doesn't match */
dpc->context_existence = 1; dpc->context_existence = 1;
ast_lock_context(c); ast_lock_context(c);
/* are we looking for exten too? if yes, we print context /* are we looking for exten too? if yes, we print context
* if we our extension only * only if we find our extension.
* Otherwise print context even if empty ?
* XXX i am not sure how the rinclude is handled.
* I think it ought to go inside.
*/ */
if (!exten) { if (!exten) {
dpc->total_context++; dpc->total_context++;
@ -3031,91 +3046,58 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
} }
/* walk extensions ... */ /* walk extensions ... */
for (e = ast_walk_context_extensions(c, NULL); e; e = ast_walk_context_extensions(c, e)) { e = NULL;
while ( (e = ast_walk_context_extensions(c, e)) ) {
struct ast_exten *p; struct ast_exten *p;
int prio;
if (exten && !ast_extension_match(ast_get_extension_name(e), exten))
/* looking for extension? is this our extension? */ continue; /* skip, extension match failed */
if (exten &&
!ast_extension_match(ast_get_extension_name(e), exten))
{
/* we are looking for extension and it's not our
* extension, so skip to next extension */
continue;
}
dpc->extension_existence = 1; dpc->extension_existence = 1;
/* may we print context info? */ /* may we print context info? */
if (!context_info_printed) { if (!context_info_printed) {
dpc->total_context++; dpc->total_context++;
if (rinclude) { if (rinclude) { /* TODO Print more info about rinclude */
/* TODO Print more info about rinclude */
ast_cli(fd, "[ Included context '%s' created by '%s' ]\n", ast_cli(fd, "[ Included context '%s' created by '%s' ]\n",
ast_get_context_name(c), ast_get_context_name(c), ast_get_context_registrar(c));
ast_get_context_registrar(c));
} else { } else {
ast_cli(fd, "[ Context '%s' created by '%s' ]\n", ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
ast_get_context_name(c), ast_get_context_name(c), ast_get_context_registrar(c));
ast_get_context_registrar(c));
} }
context_info_printed = 1; context_info_printed = 1;
} }
dpc->total_prio++; dpc->total_prio++;
/* write extension name and first peer */ /* write extension name and first peer */
bzero(buf, sizeof(buf)); snprintf(buf, sizeof(buf), "'%s' =>", ast_get_extension_name(e));
snprintf(buf, sizeof(buf), "'%s' =>",
ast_get_extension_name(e));
prio = ast_get_extension_priority(e); print_ext(e, buf2, sizeof(buf2));
if (prio == PRIORITY_HINT) {
snprintf(buf2, sizeof(buf2),
"hint: %s",
ast_get_extension_app(e));
} else {
snprintf(buf2, sizeof(buf2),
"%d. %s(%s)",
prio,
ast_get_extension_app(e),
(char *)ast_get_extension_app_data(e));
}
ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2, ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2,
ast_get_extension_registrar(e)); ast_get_extension_registrar(e));
dpc->total_exten++; dpc->total_exten++;
/* walk next extension peers */ /* walk next extension peers */
for (p=ast_walk_extension_priorities(e, e); p; p=ast_walk_extension_priorities(e, p)) { p = e; /* skip the first one, we already got it */
while ( (p = ast_walk_extension_priorities(e, p)) ) {
const char *el = ast_get_extension_label(p);
dpc->total_prio++; dpc->total_prio++;
bzero((void *)buf2, sizeof(buf2)); if (el)
bzero((void *)buf, sizeof(buf)); snprintf(buf, sizeof(buf), " [%s]", el);
if (ast_get_extension_label(p)) else
snprintf(buf, sizeof(buf), " [%s]", ast_get_extension_label(p)); buf[0] = '\0';
prio = ast_get_extension_priority(p); print_ext(p, buf2, sizeof(buf2));
if (prio == PRIORITY_HINT) {
snprintf(buf2, sizeof(buf2),
"hint: %s",
ast_get_extension_app(p));
} else {
snprintf(buf2, sizeof(buf2),
"%d. %s(%s)",
prio,
ast_get_extension_app(p),
(char *)ast_get_extension_app_data(p));
}
ast_cli(fd," %-17s %-45s [%s]\n", ast_cli(fd," %-17s %-45s [%s]\n", buf, buf2,
buf, buf2,
ast_get_extension_registrar(p)); ast_get_extension_registrar(p));
} }
} }
/* walk included and write info ... */ /* walk included and write info ... */
for (i = ast_walk_context_includes(c, NULL); i; i = ast_walk_context_includes(c, i)) { i = NULL;
bzero(buf, sizeof(buf)); while ( (i = ast_walk_context_includes(c, i)) ) {
snprintf(buf, sizeof(buf), "'%s'", snprintf(buf, sizeof(buf), "'%s'", ast_get_include_name(i));
ast_get_include_name(i));
if (exten) { if (exten) {
/* Check all includes for the requested extension */ /* Check all includes for the requested extension */
if (includecount >= AST_PBX_MAX_STACK) { if (includecount >= AST_PBX_MAX_STACK) {
@ -3131,7 +3113,7 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
} }
if (!dupe) { if (!dupe) {
includes[includecount] = (char *)ast_get_include_name(i); includes[includecount] = (char *)ast_get_include_name(i);
show_dialplan_helper(fd, (char *)ast_get_include_name(i), exten, dpc, i, includecount + 1, includes); show_dialplan_helper(fd, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes);
} else { } else {
ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context); ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context);
} }
@ -3143,18 +3125,20 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
} }
/* walk ignore patterns and write info ... */ /* walk ignore patterns and write info ... */
for (ip = ast_walk_context_ignorepats(c, NULL); ip; ip = ast_walk_context_ignorepats(c, ip)) { ip = NULL;
while ( (ip = ast_walk_context_ignorepats(c, ip)) ) {
const char *ipname = ast_get_ignorepat_name(ip); const char *ipname = ast_get_ignorepat_name(ip);
char ignorepat[AST_MAX_EXTENSION]; char ignorepat[AST_MAX_EXTENSION];
snprintf(buf, sizeof(buf), "'%s'", ipname); snprintf(buf, sizeof(buf), "'%s'", ipname);
snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname); snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname);
if ((!exten) || ast_extension_match(ignorepat, exten)) { if (!exten || ast_extension_match(ignorepat, exten)) {
ast_cli(fd, " Ignore pattern => %-45s [%s]\n", ast_cli(fd, " Ignore pattern => %-45s [%s]\n",
buf, ast_get_ignorepat_registrar(ip)); buf, ast_get_ignorepat_registrar(ip));
} }
} }
if (!rinclude) { if (!rinclude) {
for (sw = ast_walk_context_switches(c, NULL); sw; sw = ast_walk_context_switches(c, sw)) { struct ast_sw *sw = NULL;
while ( (sw = ast_walk_context_switches(c, sw)) ) {
snprintf(buf, sizeof(buf), "'%s/%s'", snprintf(buf, sizeof(buf), "'%s/%s'",
ast_get_switch_name(sw), ast_get_switch_name(sw),
ast_get_switch_data(sw)); ast_get_switch_data(sw));
@ -3166,8 +3150,8 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
ast_unlock_context(c); ast_unlock_context(c);
/* if we print something in context, make an empty line */ /* if we print something in context, make an empty line */
if (context_info_printed) ast_cli(fd, "\r\n"); if (context_info_printed)
} ast_cli(fd, "\r\n");
} }
ast_unlock_contexts(); ast_unlock_contexts();
@ -3179,6 +3163,7 @@ static int handle_show_dialplan(int fd, int argc, char *argv[])
char *exten = NULL, *context = NULL; char *exten = NULL, *context = NULL;
/* Variables used for different counters */ /* Variables used for different counters */
struct dialplan_counters counters; struct dialplan_counters counters;
char *incstack[AST_PBX_MAX_STACK]; char *incstack[AST_PBX_MAX_STACK];
memset(&counters, 0, sizeof(counters)); memset(&counters, 0, sizeof(counters));

Loading…
Cancel
Save