config: Fix indentation and missing curlies in config_text_file_load().

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@415288 65c4cc65-6c06-0410-ace0-fbb531ad65f3
changes/97/197/1
Richard Mudgett 11 years ago
parent 61b0be0600
commit b0abea6028

@ -1687,9 +1687,14 @@ static struct ast_config *config_text_file_load(const char *database, const char
/*! Growable string buffer */ /*! Growable string buffer */
struct ast_str *comment_buffer = NULL; /*!< this will be a comment collector.*/ struct ast_str *comment_buffer = NULL; /*!< this will be a comment collector.*/
struct ast_str *lline_buffer = NULL; /*!< A buffer for stuff behind the ; */ struct ast_str *lline_buffer = NULL; /*!< A buffer for stuff behind the ; */
#ifdef AST_INCLUDE_GLOB
int glob_ret;
glob_t globbuf;
#endif
if (cfg) if (cfg) {
cat = ast_config_get_current_category(cfg); cat = ast_config_get_current_category(cfg);
}
if (filename[0] == '/') { if (filename[0] == '/') {
ast_copy_string(fn, filename, sizeof(fn)); ast_copy_string(fn, filename, sizeof(fn));
@ -1699,8 +1704,9 @@ static struct ast_config *config_text_file_load(const char *database, const char
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) { if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
comment_buffer = ast_str_create(CB_SIZE); comment_buffer = ast_str_create(CB_SIZE);
if (comment_buffer) if (comment_buffer) {
lline_buffer = ast_str_create(CB_SIZE); lline_buffer = ast_str_create(CB_SIZE);
}
if (!lline_buffer) { if (!lline_buffer) {
ast_free(comment_buffer); ast_free(comment_buffer);
ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n"); ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
@ -1708,273 +1714,284 @@ static struct ast_config *config_text_file_load(const char *database, const char
} }
} }
#ifdef AST_INCLUDE_GLOB #ifdef AST_INCLUDE_GLOB
{ globbuf.gl_offs = 0; /* initialize it to silence gcc */
int glob_ret; glob_ret = glob(fn, MY_GLOB_FLAGS, NULL, &globbuf);
glob_t globbuf; if (glob_ret == GLOB_NOSPACE) {
ast_log(LOG_WARNING,
globbuf.gl_offs = 0; /* initialize it to silence gcc */ "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
glob_ret = glob(fn, MY_GLOB_FLAGS, NULL, &globbuf); } else if (glob_ret == GLOB_ABORTED) {
if (glob_ret == GLOB_NOSPACE) { ast_log(LOG_WARNING,
ast_log(LOG_WARNING, "Glob Expansion of pattern '%s' failed: Read error\n", fn);
"Glob Expansion of pattern '%s' failed: Not enough memory\n", fn); } else {
} else if (glob_ret == GLOB_ABORTED) { /* loop over expanded files */
ast_log(LOG_WARNING, int i;
"Glob Expansion of pattern '%s' failed: Read error\n", fn);
} else {
/* loop over expanded files */
int i;
if (!cfg && (globbuf.gl_pathc != 1 || strcmp(fn, globbuf.gl_pathv[0]))) {
/*
* We just want a file changed answer and since we cannot
* tell if a file was deleted with wildcard matching we will
* assume that something has always changed. Also without
* a lot of refactoring we couldn't check more than one file
* for changes in the glob loop anyway.
*/
globfree(&globbuf);
ast_free(comment_buffer);
ast_free(lline_buffer);
return NULL;
}
for (i=0; i<globbuf.gl_pathc; i++) {
ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
#endif
/*
* The following is not a loop, but just a convenient way to define a block
* (using do { } while(0) ), and be able to exit from it with 'continue'
* or 'break' in case of errors. Nice trick.
*/
do {
if (stat(fn, &statbuf)) {
if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
config_cache_remove(fn, who_asked);
}
continue;
}
if (!S_ISREG(statbuf.st_mode)) { if (!cfg && (globbuf.gl_pathc != 1 || strcmp(fn, globbuf.gl_pathv[0]))) {
ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn); /*
if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) { * We just want a file changed answer and since we cannot
config_cache_remove(fn, who_asked); * tell if a file was deleted with wildcard matching we will
} * assume that something has always changed. Also without
continue; * a lot of refactoring we couldn't check more than one file
* for changes in the glob loop anyway.
*/
globfree(&globbuf);
ast_free(comment_buffer);
ast_free(lline_buffer);
return NULL;
} }
for (i=0; i<globbuf.gl_pathc; i++) {
if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) { ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
/* Find our cached entry for this configuration file */ #endif
AST_LIST_LOCK(&cfmtime_head); /*
AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) { * The following is not a loop, but just a convenient way to define a block
if (!strcmp(cfmtime->filename, fn) && !strcmp(cfmtime->who_asked, who_asked)) * (using do { } while(0) ), and be able to exit from it with 'continue'
break; * or 'break' in case of errors. Nice trick.
} */
if (!cfmtime) { do {
cfmtime = cfmtime_new(fn, who_asked); if (stat(fn, &statbuf)) {
if (!cfmtime) { if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
AST_LIST_UNLOCK(&cfmtime_head); config_cache_remove(fn, who_asked);
}
continue; continue;
} }
/* Note that the file mtime is initialized to 0, i.e. 1970 */
AST_LIST_INSERT_SORTALPHA(&cfmtime_head, cfmtime, list, filename);
}
}
if (cfmtime if (!S_ISREG(statbuf.st_mode)) {
&& !cfmtime->has_exec ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
&& !cfmstat_cmp(cfmtime, &statbuf) if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
&& ast_test_flag(&flags, CONFIG_FLAG_FILEUNCHANGED)) { config_cache_remove(fn, who_asked);
int unchanged = 1; }
continue;
}
/* File is unchanged, what about the (cached) includes (if any)? */ if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) { /* Find our cached entry for this configuration file */
if (!config_text_file_load(NULL, NULL, cfinclude->include, AST_LIST_LOCK(&cfmtime_head);
NULL, flags, "", who_asked)) { AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
/* One change is enough to short-circuit and reload the whole shebang */ if (!strcmp(cfmtime->filename, fn) && !strcmp(cfmtime->who_asked, who_asked)) {
unchanged = 0; break;
break; }
}
if (!cfmtime) {
cfmtime = cfmtime_new(fn, who_asked);
if (!cfmtime) {
AST_LIST_UNLOCK(&cfmtime_head);
continue;
}
/* Note that the file mtime is initialized to 0, i.e. 1970 */
AST_LIST_INSERT_SORTALPHA(&cfmtime_head, cfmtime, list, filename);
}
} }
}
if (unchanged) { if (cfmtime
AST_LIST_UNLOCK(&cfmtime_head); && !cfmtime->has_exec
&& !cfmstat_cmp(cfmtime, &statbuf)
&& ast_test_flag(&flags, CONFIG_FLAG_FILEUNCHANGED)) {
int unchanged = 1;
/* File is unchanged, what about the (cached) includes (if any)? */
AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
if (!config_text_file_load(NULL, NULL, cfinclude->include,
NULL, flags, "", who_asked)) {
/* One change is enough to short-circuit and reload the whole shebang */
unchanged = 0;
break;
}
}
if (unchanged) {
AST_LIST_UNLOCK(&cfmtime_head);
#ifdef AST_INCLUDE_GLOB #ifdef AST_INCLUDE_GLOB
globfree(&globbuf); globfree(&globbuf);
#endif #endif
ast_free(comment_buffer); ast_free(comment_buffer);
ast_free(lline_buffer); ast_free(lline_buffer);
return CONFIG_STATUS_FILEUNCHANGED; return CONFIG_STATUS_FILEUNCHANGED;
} }
} }
/* If cfg is NULL, then we just want a file changed answer. */
if (cfg == NULL) {
if (cfmtime) {
AST_LIST_UNLOCK(&cfmtime_head);
}
continue;
}
if (cfmtime) {
/* Forget about what we thought we knew about this file's includes. */
cfmtime->has_exec = 0;
config_cache_flush_includes(cfmtime);
cfmstat_save(cfmtime, &statbuf);
AST_LIST_UNLOCK(&cfmtime_head);
}
if (!(f = fopen(fn, "r"))) { /* If cfg is NULL, then we just want a file changed answer. */
ast_debug(1, "No file to parse: %s\n", fn); if (cfg == NULL) {
ast_verb(2, "Parsing '%s': Not found (%s)\n", fn, strerror(errno)); if (cfmtime) {
continue; AST_LIST_UNLOCK(&cfmtime_head);
}
count++;
/* If we get to this point, then we're loading regardless */
ast_clear_flag(&flags, CONFIG_FLAG_FILEUNCHANGED);
ast_debug(1, "Parsing %s\n", fn);
ast_verb(2, "Parsing '%s': Found\n", fn);
while (!feof(f)) {
lineno++;
if (fgets(buf, sizeof(buf), f)) {
/* Skip lines that are too long */
if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
ast_log(LOG_WARNING, "Line %d too long, skipping. It begins with: %.32s...\n", lineno, buf);
while (fgets(buf, sizeof(buf), f)) {
if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
break;
}
} }
continue; continue;
} }
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && lline_buffer && ast_str_strlen(lline_buffer)) { if (cfmtime) {
CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */ /* Forget about what we thought we knew about this file's includes. */
ast_str_reset(lline_buffer); /* erase the lline buffer */ cfmtime->has_exec = 0;
} config_cache_flush_includes(cfmtime);
new_buf = buf; cfmstat_save(cfmtime, &statbuf);
if (comment) AST_LIST_UNLOCK(&cfmtime_head);
process_buf = NULL; }
else
process_buf = buf;
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer) && (ast_strlen_zero(buf) || strlen(buf) == strspn(buf," \t\n\r"))) { if (!(f = fopen(fn, "r"))) {
/* blank line? really? Can we add it to an existing comment and maybe preserve inter- and post- comment spacing? */ ast_debug(1, "No file to parse: %s\n", fn);
CB_ADD(&comment_buffer, "\n"); /* add a newline to the comment buffer */ ast_verb(2, "Parsing '%s': Not found (%s)\n", fn, strerror(errno));
continue; /* go get a new line, then */ continue;
} }
count++;
/* If we get to this point, then we're loading regardless */
ast_clear_flag(&flags, CONFIG_FLAG_FILEUNCHANGED);
ast_debug(1, "Parsing %s\n", fn);
ast_verb(2, "Parsing '%s': Found\n", fn);
while (!feof(f)) {
lineno++;
if (fgets(buf, sizeof(buf), f)) {
/* Skip lines that are too long */
if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
ast_log(LOG_WARNING, "Line %d too long, skipping. It begins with: %.32s...\n", lineno, buf);
while (fgets(buf, sizeof(buf), f)) {
if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
break;
}
}
continue;
}
while ((comment_p = strchr(new_buf, COMMENT_META))) { if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)
if ((comment_p > new_buf) && (*(comment_p - 1) == '\\')) { && lline_buffer
/* Escaped semicolons aren't comments. */ && ast_str_strlen(lline_buffer)) {
new_buf = comment_p; CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
/* write over the \ and bring the null terminator with us */ ast_str_reset(lline_buffer); /* erase the lline buffer */
memmove(comment_p - 1, comment_p, strlen(comment_p) + 1); }
} else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
/* Meta-Comment start detected ";--" */ new_buf = buf;
if (comment < MAX_NESTED_COMMENTS) { if (comment) {
*comment_p = '\0'; process_buf = NULL;
new_buf = comment_p + 3;
comment++;
nest[comment-1] = lineno;
} else { } else {
ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS); process_buf = buf;
} }
} else if ((comment_p >= new_buf + 2) &&
(*(comment_p - 1) == COMMENT_TAG) &&
(*(comment_p - 2) == COMMENT_TAG)) {
/* Meta-Comment end detected "--;" */
comment--;
new_buf = comment_p + 1;
if (!comment) {
/* Back to non-comment now */
if (process_buf) {
/* Actually have to move what's left over the top, then continue */
char *oldptr;
oldptr = process_buf + strlen(process_buf);
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
CB_ADD(&comment_buffer, ";");
CB_ADD_LEN(&comment_buffer, oldptr+1, new_buf-oldptr-1);
}
memmove(oldptr, new_buf, strlen(new_buf) + 1); if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)
new_buf = oldptr; && comment_buffer
} else && ast_str_strlen(comment_buffer)
process_buf = new_buf; && (ast_strlen_zero(buf) || strlen(buf) == strspn(buf," \t\n\r"))) {
/* blank line? really? Can we add it to an existing comment and maybe preserve inter- and post- comment spacing? */
CB_ADD(&comment_buffer, "\n"); /* add a newline to the comment buffer */
continue; /* go get a new line, then */
} }
} else {
if (!comment) { while ((comment_p = strchr(new_buf, COMMENT_META))) {
/* If ; is found, and we are not nested in a comment, if ((comment_p > new_buf) && (*(comment_p - 1) == '\\')) {
we immediately stop all comment processing */ /* Escaped semicolons aren't comments. */
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) { new_buf = comment_p;
CB_ADD(&lline_buffer, comment_p); /* write over the \ and bring the null terminator with us */
memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
} else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
/* Meta-Comment start detected ";--" */
if (comment < MAX_NESTED_COMMENTS) {
*comment_p = '\0';
new_buf = comment_p + 3;
comment++;
nest[comment-1] = lineno;
} else {
ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
}
} else if ((comment_p >= new_buf + 2) &&
(*(comment_p - 1) == COMMENT_TAG) &&
(*(comment_p - 2) == COMMENT_TAG)) {
/* Meta-Comment end detected "--;" */
comment--;
new_buf = comment_p + 1;
if (!comment) {
/* Back to non-comment now */
if (process_buf) {
/* Actually have to move what's left over the top, then continue */
char *oldptr;
oldptr = process_buf + strlen(process_buf);
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
CB_ADD(&comment_buffer, ";");
CB_ADD_LEN(&comment_buffer, oldptr+1, new_buf-oldptr-1);
}
memmove(oldptr, new_buf, strlen(new_buf) + 1);
new_buf = oldptr;
} else {
process_buf = new_buf;
}
}
} else {
if (!comment) {
/* If ; is found, and we are not nested in a comment,
we immediately stop all comment processing */
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
CB_ADD(&lline_buffer, comment_p);
}
*comment_p = '\0';
new_buf = comment_p;
} else {
new_buf = comment_p + 1;
}
} }
*comment_p = '\0'; }
new_buf = comment_p; if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment && !process_buf ) {
} else CB_ADD(&comment_buffer, buf); /* the whole line is a comment, store it */
new_buf = comment_p + 1; }
}
}
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment && !process_buf ) {
CB_ADD(&comment_buffer, buf); /* the whole line is a comment, store it */
}
if (process_buf) { if (process_buf) {
char *buffer = ast_strip(process_buf); char *buffer = ast_strip(process_buf);
if (!ast_strlen_zero(buffer)) {
if (process_text_line(cfg, &cat, buffer, lineno, fn, flags, comment_buffer, lline_buffer, suggested_include_file, &last_cat, &last_var, who_asked)) { if (!ast_strlen_zero(buffer)) {
cfg = CONFIG_STATUS_FILEINVALID; if (process_text_line(cfg, &cat, buffer, lineno, fn,
break; flags, comment_buffer, lline_buffer,
suggested_include_file, &last_cat, &last_var,
who_asked)) {
cfg = CONFIG_STATUS_FILEINVALID;
break;
}
}
} }
} }
} }
} /* end of file-- anything in a comment buffer? */
} if (last_cat) {
/* end of file-- anything in a comment buffer? */ if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
if (last_cat) { if (lline_buffer && ast_str_strlen(lline_buffer)) {
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) { CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
if (lline_buffer && ast_str_strlen(lline_buffer)) { ast_str_reset(lline_buffer); /* erase the lline buffer */
CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */ }
ast_str_reset(lline_buffer); /* erase the lline buffer */ last_cat->trailing = ALLOC_COMMENT(comment_buffer);
}
} else if (last_var) {
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
if (lline_buffer && ast_str_strlen(lline_buffer)) {
CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
ast_str_reset(lline_buffer); /* erase the lline buffer */
}
last_var->trailing = ALLOC_COMMENT(comment_buffer);
}
} else {
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
ast_debug(1, "Nothing to attach comments to, discarded: %s\n", ast_str_buffer(comment_buffer));
}
} }
last_cat->trailing = ALLOC_COMMENT(comment_buffer); if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
} CB_RESET(comment_buffer, lline_buffer);
} else if (last_var) {
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
if (lline_buffer && ast_str_strlen(lline_buffer)) {
CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
ast_str_reset(lline_buffer); /* erase the lline buffer */
} }
last_var->trailing = ALLOC_COMMENT(comment_buffer);
}
} else {
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
ast_debug(1, "Nothing to attach comments to, discarded: %s\n", ast_str_buffer(comment_buffer));
}
}
if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
CB_RESET(comment_buffer, lline_buffer);
fclose(f); fclose(f);
} while (0); } while (0);
if (comment) { if (comment) {
ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]); ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
} }
#ifdef AST_INCLUDE_GLOB #ifdef AST_INCLUDE_GLOB
if (cfg == NULL || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) { if (cfg == NULL || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
break; break;
}
}
globfree(&globbuf);
} }
} }
globfree(&globbuf);
}
#endif #endif
ast_free(comment_buffer); ast_free(comment_buffer);
ast_free(lline_buffer); ast_free(lline_buffer);
if (count == 0) if (count == 0) {
return NULL; return NULL;
}
return cfg; return cfg;
} }

Loading…
Cancel
Save