- normalize some loops

- simplify and reduce code by keeping track of return value
- replace some simple if/then sections with conditional expressions
(issue #6065)


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7639 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2005-12-26 21:38:50 +00:00
parent cf1e68e4e8
commit a4f1d606b7

95
pbx.c
View File

@@ -2541,12 +2541,13 @@ int pbx_set_autofallthrough(int newval)
*/ */
int ast_context_remove_include(const char *context, const char *include, const char *registrar) int ast_context_remove_include(const char *context, const char *include, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) return -1; if (ast_lock_contexts())
return -1;
/* walk contexts and search for the right one ...*/ /* walk contexts and search for the right one ...*/
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* we found one ... */ /* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
int ret; int ret;
@@ -2609,28 +2610,25 @@ int ast_context_remove_include2(struct ast_context *con, const char *include, co
*/ */
int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar) int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
int ret = -1; /* default error return */
if (ast_lock_contexts()) return -1; if (ast_lock_contexts())
return -1;
/* walk contexts and search for the right one ...*/ /* walk contexts and search for the right one ...*/
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* we found one ... */ /* we found one ... */
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
int ret;
/* remove switch from this context ... */ /* remove switch from this context ... */
ret = ast_context_remove_switch2(c, sw, data, registrar); ret = ast_context_remove_switch2(c, sw, data, registrar);
break;
ast_unlock_contexts();
/* ... return results */
return ret;
} }
} }
/* we can't find the right one context */ /* found or error */
ast_unlock_contexts(); ast_unlock_contexts();
return -1; return ret;
} }
/*! /*!
@@ -2677,26 +2675,25 @@ int ast_context_remove_switch2(struct ast_context *con, const char *sw, const ch
*/ */
int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar) int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
int ret = -1; /* default error return */
if (ast_lock_contexts()) return -1; if (ast_lock_contexts())
return -1;
/* walk contexts ... */ /* walk contexts ... */
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* ... search for the right one ... */ /* ... search for the right one ... */
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
/* ... remove extension ... */ /* ... remove extension ... */
int ret = ast_context_remove_extension2(c, extension, priority, ret = ast_context_remove_extension2(c, extension, priority,
registrar); registrar);
/* ... unlock contexts list and return */ break;
ast_unlock_contexts();
return ret;
} }
} }
/* found or error */
/* we can't find the right context */
ast_unlock_contexts(); ast_unlock_contexts();
return -1; return ret;
} }
/*! /*!
@@ -3234,7 +3231,8 @@ static char *complete_show_applications(char *line, char *word, int pos, int sta
static char *complete_show_dialplan_context(char *line, char *word, int pos, static char *complete_show_dialplan_context(char *line, char *word, int pos,
int state) int state)
{ {
struct ast_context *c; struct ast_context *c = NULL;
char *ret = NULL;
int which = 0; int which = 0;
int wordlen; int wordlen;
@@ -3251,22 +3249,21 @@ static char *complete_show_dialplan_context(char *line, char *word, int pos,
wordlen = strlen(word); wordlen = strlen(word);
/* ... walk through all contexts ... */ /* ... walk through all contexts ... */
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* ... word matches context name? yes? ... */ /* ... word matches context name? yes? ... */
if (!strncasecmp(word, ast_get_context_name(c), wordlen)) { if (!strncasecmp(word, ast_get_context_name(c), wordlen)) {
/* ... for serve? ... */ /* ... for serve? ... */
if (++which > state) { if (++which > state) {
/* ... yes, serve this context name ... */ /* ... yes, serve this context name ... */
char *ret = strdup(ast_get_context_name(c)); ret = strdup(ast_get_context_name(c));
ast_unlock_contexts(); break;
return ret;
} }
} }
} }
/* ... unlock and return */ /* ... unlock and return */
ast_unlock_contexts(); ast_unlock_contexts();
return NULL; return ret;
} }
struct dialplan_counters { struct dialplan_counters {
@@ -3279,7 +3276,7 @@ struct dialplan_counters {
static int show_dialplan_helper(int fd, char *context, char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, char *includes[]) static int show_dialplan_helper(int fd, char *context, char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, char *includes[])
{ {
struct ast_context *c; struct ast_context *c = NULL;
int res = 0, old_total_exten = dpc->total_exten; int res = 0, old_total_exten = dpc->total_exten;
/* try to lock contexts */ /* try to lock contexts */
@@ -3289,7 +3286,7 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
} }
/* walk all contexts ... */ /* walk all contexts ... */
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* show this context? */ /* show this context? */
if (!context || if (!context ||
!strcmp(ast_get_context_name(c), context)) { !strcmp(ast_get_context_name(c), context)) {
@@ -3728,7 +3725,7 @@ void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char
*/ */
int ast_context_add_include(const char *context, const char *include, const char *registrar) int ast_context_add_include(const char *context, const char *include, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) { if (ast_lock_contexts()) {
errno = EBUSY; errno = EBUSY;
@@ -3736,7 +3733,7 @@ int ast_context_add_include(const char *context, const char *include, const char
} }
/* walk contexts ... */ /* walk contexts ... */
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* ... search for the right one ... */ /* ... search for the right one ... */
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
int ret = ast_context_add_include2(c, include, registrar); int ret = ast_context_add_include2(c, include, registrar);
@@ -4164,7 +4161,7 @@ int ast_context_add_include2(struct ast_context *con, const char *value,
*/ */
int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar) int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) { if (ast_lock_contexts()) {
errno = EBUSY; errno = EBUSY;
@@ -4172,7 +4169,7 @@ int ast_context_add_switch(const char *context, const char *sw, const char *data
} }
/* walk contexts ... */ /* walk contexts ... */
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
/* ... search for the right one ... */ /* ... search for the right one ... */
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
int ret = ast_context_add_switch2(c, sw, data, eval, registrar); int ret = ast_context_add_switch2(c, sw, data, eval, registrar);
@@ -4276,14 +4273,14 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
*/ */
int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar) int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) { if (ast_lock_contexts()) {
errno = EBUSY; errno = EBUSY;
return -1; return -1;
} }
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
if (!strcmp(ast_get_context_name(c), context)) { if (!strcmp(ast_get_context_name(c), context)) {
int ret = ast_context_remove_ignorepat2(c, ignorepat, registrar); int ret = ast_context_remove_ignorepat2(c, ignorepat, registrar);
ast_unlock_contexts(); ast_unlock_contexts();
@@ -4332,14 +4329,14 @@ int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat
*/ */
int ast_context_add_ignorepat(const char *con, const char *value, const char *registrar) int ast_context_add_ignorepat(const char *con, const char *value, const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) { if (ast_lock_contexts()) {
errno = EBUSY; errno = EBUSY;
return -1; return -1;
} }
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
if (!strcmp(ast_get_context_name(c), con)) { if (!strcmp(ast_get_context_name(c), con)) {
int ret = ast_context_add_ignorepat2(c, value, registrar); int ret = ast_context_add_ignorepat2(c, value, registrar);
ast_unlock_contexts(); ast_unlock_contexts();
@@ -4409,14 +4406,14 @@ int ast_ignore_pattern(const char *context, const char *pattern)
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid,
const char *application, void *data, void (*datad)(void *), const char *registrar) const char *application, void *data, void (*datad)(void *), const char *registrar)
{ {
struct ast_context *c; struct ast_context *c = NULL;
if (ast_lock_contexts()) { if (ast_lock_contexts()) {
errno = EBUSY; errno = EBUSY;
return -1; return -1;
} }
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { while ( (c = ast_walk_contexts(c)) ) {
if (!strcmp(context, ast_get_context_name(c))) { if (!strcmp(context, ast_get_context_name(c))) {
int ret = ast_add_extension2(c, replace, extension, priority, label, callerid, int ret = ast_add_extension2(c, replace, extension, priority, label, callerid,
application, data, datad, registrar); application, data, datad, registrar);
@@ -6134,10 +6131,7 @@ const char *ast_get_switch_registrar(struct ast_sw *sw)
*/ */
struct ast_context *ast_walk_contexts(struct ast_context *con) struct ast_context *ast_walk_contexts(struct ast_context *con)
{ {
if (!con) return con ? con->next : contexts;
return contexts;
else
return con->next;
} }
struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
@@ -6161,10 +6155,7 @@ struct ast_sw *ast_walk_context_switches(struct ast_context *con,
struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten, struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
struct ast_exten *priority) struct ast_exten *priority)
{ {
if (!priority) return priority ? priority->peer : exten;
return exten;
else
return priority->peer;
} }
struct ast_include *ast_walk_context_includes(struct ast_context *con, struct ast_include *ast_walk_context_includes(struct ast_context *con,
@@ -6187,10 +6178,10 @@ struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con,
int ast_context_verify_includes(struct ast_context *con) int ast_context_verify_includes(struct ast_context *con)
{ {
struct ast_include *inc; struct ast_include *inc = NULL;
int res = 0; int res = 0;
for (inc = ast_walk_context_includes(con, NULL); inc; inc = ast_walk_context_includes(con, inc)) while ( (inc = ast_walk_context_includes(con, inc)) )
if (!ast_context_find(inc->rname)) { if (!ast_context_find(inc->rname)) {
res = -1; res = -1;
ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n", ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n",