6 #include <../include/asterisk/ast_expr.h>
9 int global_expr_count = 0;
10 int global_expr_max_size = 0;
11 int global_expr_tot_size = 0;
12 int global_warn_count = 0;
13 int global_OK_count = 0;
17 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
18 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
22 struct varz *global_varlist;
24 /* Our own version of ast_log, since the expr parser uses it. */
26 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
28 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
33 printf("LOG: lev:%d file:%s line:%d func: %s ",
34 level, file, line, function);
40 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
43 for (t= global_varlist; t; t = t->next) {
44 if (!strcmp(t->varname, varname)) {
51 void set_var(const char *varname, const char *varval)
53 struct varz *t = calloc(1,sizeof(struct varz));
54 strcpy(t->varname, varname);
55 strcpy(t->varval, varval);
56 t->next = global_varlist;
60 int check_expr(char *buffer, char *error_report)
68 for (cp=buffer;*cp;cp++) {
83 || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */
86 /* skip to the other end */
88 while (*cp && *cp != '"')
91 fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n",
96 if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) {
103 if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') {
106 sprintf(tbuf,"WARNING: line %d, '%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
109 sprintf(tbuf,"WARNING: line %d, '%c%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
110 global_lineno, *cp, *(cp+1));
111 strcat(error_report,tbuf);
122 int check_eval(char *buffer, char *error_report)
134 for (cp=buffer;*cp;cp++) {
135 if (*cp == '$' && *(cp+1) == '{') {
153 strncpy(varname,cp+2, xp-cp-2);
154 varname[xp-cp-2] = 0;
156 val = find_var(varname);
163 *ep++ = '5'; /* why not */
169 printf("Unterminated variable reference at line %d\n", global_lineno);
173 else if (*cp == '\\') {
174 /* braindead simple elim of backslash */
183 /* now, run the test */
184 result = ast_expr(evalbuf, s, sizeof(s));
186 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
189 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
195 void parse_file(const char *fname)
197 FILE *f = fopen(fname,"r");
198 FILE *l = fopen("expr2_log","w");
201 char buffer[30000]; /* I sure hope no expr gets this big! */
204 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n");
208 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
214 while ((c1 = fgetc(f)) != EOF) {
217 else if (c1 == '[') {
218 if (last_char == '$') {
223 char error_report[30000];
225 while ((c1 = fgetc(f)) != EOF) {
231 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
234 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
239 buffer[bufcount++] = c1;
242 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
245 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
249 buffer[bufcount] = 0;
251 global_expr_tot_size += bufcount;
253 if (bufcount > global_expr_max_size)
254 global_expr_max_size = bufcount;
256 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
258 /* print error report */
259 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
260 global_lineno, buffer);
261 fprintf(l, "%s", error_report);
264 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
268 retval = check_eval(buffer, error_report);
269 fprintf(l, "%s", error_report);
274 printf("Summary:\n Expressions detected: %d\n Expressions OK: %d\n Total # Warnings: %d\n Longest Expr: %d chars\n Ave expr len: %d chars\n",
278 global_expr_max_size,
279 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
286 main(int argc,char **argv)
292 printf("Hey-- give me a path to an extensions.conf file!\n");
296 for (argc1=2;argc1 < argc; argc1++) {
297 if ((eq = strchr(argv[argc1],'='))) {
299 set_var(argv[argc1],eq+1);
303 /* parse command args for x=y and set varz */