2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
24 #include <../include/asterisk/ast_expr.h>
26 int global_lineno = 1;
27 int global_expr_count = 0;
28 int global_expr_max_size = 0;
29 int global_expr_tot_size = 0;
30 int global_warn_count = 0;
31 int global_OK_count = 0;
35 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
36 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
40 struct varz *global_varlist;
42 /* Our own version of ast_log, since the expr parser uses it. */
44 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
46 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
51 printf("LOG: lev:%d file:%s line:%d func: %s ",
52 level, file, line, function);
58 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
61 for (t= global_varlist; t; t = t->next) {
62 if (!strcmp(t->varname, varname)) {
69 void set_var(const char *varname, const char *varval)
71 struct varz *t = calloc(1,sizeof(struct varz));
74 strcpy(t->varname, varname);
75 strcpy(t->varval, varval);
76 t->next = global_varlist;
80 int check_expr(char *buffer, char *error_report)
88 for (cp=buffer;*cp;cp++) {
103 || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */
106 /* skip to the other end */
108 while (*cp && *cp != '"')
111 fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n",
116 if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) {
123 if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') {
126 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",
129 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",
130 global_lineno, *cp, *(cp+1));
131 strcat(error_report,tbuf);
142 int check_eval(char *buffer, char *error_report)
154 for (cp=buffer;*cp;cp++) {
155 if (*cp == '$' && *(cp+1) == '{') {
173 strncpy(varname,cp+2, xp-cp-2);
174 varname[xp-cp-2] = 0;
176 val = find_var(varname);
183 *ep++ = '5'; /* why not */
189 printf("Unterminated variable reference at line %d\n", global_lineno);
193 else if (*cp == '\\') {
194 /* braindead simple elim of backslash */
203 /* now, run the test */
204 result = ast_expr(evalbuf, s, sizeof(s));
206 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
209 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
215 void parse_file(const char *fname)
217 FILE *f = fopen(fname,"r");
218 FILE *l = fopen("expr2_log","w");
221 char buffer[30000]; /* I sure hope no expr gets this big! */
224 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n");
228 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
234 while ((c1 = fgetc(f)) != EOF) {
237 else if (c1 == '[') {
238 if (last_char == '$') {
243 char error_report[30000];
245 while ((c1 = fgetc(f)) != EOF) {
251 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
254 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
259 buffer[bufcount++] = c1;
262 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
265 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
269 buffer[bufcount] = 0;
271 global_expr_tot_size += bufcount;
273 if (bufcount > global_expr_max_size)
274 global_expr_max_size = bufcount;
276 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
278 /* print error report */
279 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
280 global_lineno, buffer);
281 fprintf(l, "%s", error_report);
284 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
288 retval = check_eval(buffer, error_report);
289 fprintf(l, "%s", error_report);
294 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",
298 global_expr_max_size,
299 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
306 main(int argc,char **argv)
312 printf("Hey-- give me a path to an extensions.conf file!\n");
316 for (argc1=2;argc1 < argc; argc1++) {
317 if ((eq = strchr(argv[argc1],'='))) {
319 set_var(argv[argc1],eq+1);
323 /* parse command args for x=y and set varz */