Index: pbx/pbx_ael.c =================================================================== RCS file: /usr/cvsroot/asterisk/pbx/pbx_ael.c,v retrieving revision 1.6 diff -u -r1.6 pbx_ael.c --- pbx/pbx_ael.c 15 Jul 2005 16:32:44 -0000 1.6 +++ pbx/pbx_ael.c 28 Jul 2005 05:59:25 -0000 @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "asterisk.h" @@ -1121,12 +1123,12 @@ static int ast_ael_compile(struct ast_context **local_contexts, const char *filename) { char *rfilename; - char *buf, *tbuf; - int bufsiz; + char *buf, *bline, *eline; FILE *f; char *c; char *token; int lineno=0; + struct stat sfilename; if (filename[0] == '/') rfilename = (char *)filename; @@ -1140,43 +1142,44 @@ ast_log(LOG_WARNING, "Unable to open '%s': %s\n", rfilename, strerror(errno)); return -1; } - buf = malloc(4096); + + stat(filename, &sfilename); + + buf = malloc(sfilename.st_size + 1); if (!buf) { ast_log(LOG_WARNING, "Out of memory!\n"); fclose(f); return -1; } - buf[0] = 0; - bufsiz = 4096; - while(!feof(f)) { - if (strlen(buf) - bufsiz < 2048) { - bufsiz += 4096; - tbuf = realloc(buf, bufsiz); - if (tbuf) { - buf = tbuf; - } else { - free(buf); - ast_log(LOG_WARNING, "Out of memory!\n"); - fclose(f); - } + fread(buf, sfilename.st_size, 1, f); + buf[sfilename.st_size] = '\0'; + + for (bline=buf, eline=index(buf, '\n'); eline; bline=eline + 1, eline=index(bline, '\n')) { + char *endline = eline; + + /* Eliminate newlines and trailing spaces */ + while (*endline < 33) { + *endline = '\0'; + endline--; } - if (fgets(buf + strlen(buf), bufsiz - strlen(buf), f)) { - lineno++; - while(*buf && buf[strlen(buf) - 1] < 33) - buf[strlen(buf) - 1] = '\0'; - c = strstr(buf, "//"); - if (c) - *c = '\0'; - if (*buf) { - if (aeldebug & DEBUG_READ) - ast_verbose("Newly composed line '%s'\n", buf); - while((token = grab_token(buf, filename, lineno))) { - handle_root_token(local_contexts, token, 0, filename, lineno); - free(token); - } + + lineno++; + + /* Eliminate comments */ + c = strstr(bline, "//"); + if (c) + *c = '\0'; + + /* If there's a line left, process it */ + if (*bline) { + if (aeldebug & DEBUG_READ) + ast_verbose("Newly composed line '%s'\n", bline); + while((token = grab_token(bline, filename, lineno))) { + handle_root_token(local_contexts, token, 0, filename, lineno); + free(token); } } - }; + } free(buf); fclose(f); return 0;