Merged revisions 74317 via svnmerge from
[asterisk/asterisk.git] / utils / check_expr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <../include/asterisk/ast_expr.h>
25
26 static int global_lineno = 1;
27 static int global_expr_count=0;
28 static int global_expr_max_size=0;
29 static int global_expr_tot_size=0;
30 static int global_warn_count=0;
31 static int global_OK_count=0;
32
33 struct varz
34 {
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** */
37         struct varz *next;
38 };
39
40 struct varz *global_varlist;
41
42 /* Our own version of ast_log, since the expr parser uses it. */
43
44 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
45
46 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
47 {
48         va_list vars;
49         va_start(vars,fmt);
50         
51         printf("LOG: lev:%d file:%s  line:%d func: %s  ",
52                    level, file, line, function);
53         vprintf(fmt, vars);
54         fflush(stdout);
55         va_end(vars);
56 }
57 void ast_register_file_version(const char *file, const char *version);
58 void ast_unregister_file_version(const char *file);
59
60 char *find_var(const char *varname);
61 void set_var(const char *varname, const char *varval);
62 unsigned int check_expr(char* buffer, char* error_report);
63 int check_eval(char *buffer, char *error_report);
64 void parse_file(const char *fname);
65
66 void ast_register_file_version(const char *file, const char *version)
67 {
68 }
69
70 void ast_unregister_file_version(const char *file)
71 {
72 }
73
74 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
75 {
76         struct varz *t;
77         for (t= global_varlist; t; t = t->next) {
78                 if (!strcmp(t->varname, varname)) {
79                         return t->varval;
80                 }
81         }
82         return 0;
83 }
84
85 void set_var(const char *varname, const char *varval)
86 {
87         struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
88         if (!t)
89                 return;
90         strcpy(t->varname, varname);
91         strcpy(t->varval, varval);
92         t->next = global_varlist;
93         global_varlist = t;
94 }
95
96 unsigned int check_expr(char* buffer, char* error_report)
97 {
98         char* cp;
99         unsigned int warn_found = 0;
100
101         error_report[0] = 0;
102         
103         for (cp = buffer; *cp; ++cp)
104         {
105                 switch (*cp)
106                 {
107                         case '"':
108                                 /* skip to the other end */
109                                 while (*(++cp) && *cp != '"') ;
110
111                                 if (*cp == 0)
112                                 {
113                                         fprintf(stderr,
114                                                 "Trouble? Unterminated double quote found at line %d\n",
115                                                 global_lineno);
116                                 }
117                                 break;
118                                 
119                         case '>':
120                         case '<':
121                         case '!':
122                                 if (   (*(cp + 1) == '=')
123                                         && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
124                                 {
125                                         char msg[200];
126                                         snprintf(msg,
127                                                 sizeof(msg),
128                                                 "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",
129                                                 global_lineno, *cp, *(cp + 1));
130                                         strcat(error_report, msg);
131                                         ++global_warn_count;
132                                         ++warn_found;
133                                 }
134                                 break;
135                                 
136                         case '|':
137                         case '&':
138                         case '=':
139                         case '+':
140                         case '-':
141                         case '*':
142                         case '/':
143                         case '%':
144                         case '?':
145                         case ':':
146                                 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
147                                 {
148                                         char msg[200];
149                                         snprintf(msg,
150                                                 sizeof(msg),
151                                                 "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",
152                                                 global_lineno, *cp );
153                                         strcat(error_report, msg);
154                                         ++global_warn_count;
155                                         ++warn_found;
156                                 }
157                                 break;
158                 }
159         }
160
161         return warn_found;
162 }
163
164 struct ast_custom_function *ast_custom_function_find(const char *name);
165
166 struct ast_custom_function *ast_custom_function_find(const char *name)
167 {
168         return 0;
169 }
170
171 int check_eval(char *buffer, char *error_report)
172 {
173         char *cp, *ep;
174         char s[4096];
175         char evalbuf[80000];
176         int result;
177
178         error_report[0] = 0;
179         ep = evalbuf;
180
181         for (cp=buffer;*cp;cp++) {
182                 if (*cp == '$' && *(cp+1) == '{') {
183                         int brack_lev = 1;
184                         char *xp= cp+2;
185                         
186                         while (*xp) {
187                                 if (*xp == '{')
188                                         brack_lev++;
189                                 else if (*xp == '}')
190                                         brack_lev--;
191                                 
192                                 if (brack_lev == 0)
193                                         break;
194                                 xp++;
195                         }
196                         if (*xp == '}') {
197                                 char varname[200];
198                                 char *val;
199                                 
200                                 strncpy(varname,cp+2, xp-cp-2);
201                                 varname[xp-cp-2] = 0;
202                                 cp = xp;
203                                 val = find_var(varname);
204                                 if (val) {
205                                         char *z = val;
206                                         while (*z)
207                                                 *ep++ = *z++;
208                                 }
209                                 else {
210                                         *ep++ = '5';  /* why not */
211                                         *ep++ = '5';
212                                         *ep++ = '5';
213                                 }
214                         }
215                         else {
216                                 printf("Unterminated variable reference at line %d\n", global_lineno);
217                                 *ep++ = *cp;
218                         }
219                 }
220                 else if (*cp == '\\') {
221                         /* braindead simple elim of backslash */
222                         cp++;
223                         *ep++ = *cp;
224                 }
225                 else
226                         *ep++ = *cp;
227         }
228         *ep++ = 0;
229
230         /* now, run the test */
231         result = ast_expr(evalbuf, s, sizeof(s),NULL);
232         if (result) {
233                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
234                 return 1;
235         } else {
236                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
237                 return 1;
238         }
239 }
240
241
242 void parse_file(const char *fname)
243 {
244         FILE *f = fopen(fname,"r");
245         FILE *l = fopen("expr2_log","w");
246         int c1;
247         char last_char= 0;
248         char buffer[30000]; /* I sure hope no expr gets this big! */
249         
250         if (!f) {
251                 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
252                 exit(20);
253         }
254         if (!l) {
255                 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
256                 exit(21);
257         }
258         
259         global_lineno = 1;
260         
261         while ((c1 = fgetc(f)) != EOF) {
262                 if (c1 == '\n')
263                         global_lineno++;
264                 else if (c1 == '[') {
265                         if (last_char == '$') {
266                                 /* bingo, an expr */
267                                 int bracklev = 1;
268                                 int bufcount = 0;
269                                 int retval;
270                                 char error_report[30000];
271                                 
272                                 while ((c1 = fgetc(f)) != EOF) {
273                                         if (c1 == '[')
274                                                 bracklev++;
275                                         else if (c1 == ']')
276                                                 bracklev--;
277                                         if (c1 == '\n') {
278                                                 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
279                                                 fclose(f);
280                                                 fclose(l);
281                                                 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
282                                         }
283                                         
284                                         if (bracklev == 0)
285                                                 break;
286                                         buffer[bufcount++] = c1;
287                                 }
288                                 if (c1 == EOF) {
289                                         fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
290                                         fclose(f);
291                                         fclose(l);
292                                         printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
293                                         exit(22);
294                                 }
295                                 
296                                 buffer[bufcount] = 0;
297                                 /* update stats */
298                                 global_expr_tot_size += bufcount;
299                                 global_expr_count++;
300                                 if (bufcount > global_expr_max_size)
301                                         global_expr_max_size = bufcount;
302                                 
303                                 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
304                                 if (retval != 0) {
305                                         /* print error report */
306                                         printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", 
307                                                    global_lineno, buffer);
308                                         fprintf(l, "%s", error_report);
309                                 }
310                                 else {
311                                         printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
312                                         global_OK_count++;
313                                 }
314                                 error_report[0] = 0;
315                                 retval = check_eval(buffer, error_report);
316                                 fprintf(l, "%s", error_report);
317                         }
318                 }
319                 last_char = c1;
320         }
321         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",
322                    global_expr_count,
323                    global_OK_count,
324                    global_warn_count,
325                    global_expr_max_size,
326                    (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
327         
328         fclose(f);
329         fclose(l);
330 }
331
332
333 int main(int argc,char **argv)
334 {
335         int argc1;
336         char *eq;
337         
338         if (argc < 2) {
339                 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
340                 printf("              and run them thru the parser, looking for problems\n");
341                 printf("Hey-- give me a path to an extensions.conf file!\n");
342                 printf(" You can also follow the file path with a series of variable decls,\n");
343                 printf("     of the form, varname=value, each separated from the next by spaces.\n");
344                 printf("     (this might allow you to avoid division by zero messages, check that math\n");
345                 printf("      is being done correctly, etc.)\n");
346                 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
347                 printf("  you to possible problems where you might be expecting those operators as part of a string.\n");
348         printf("  (to include operators in a string, wrap with double quotes!)\n");
349                 
350                 exit(19);
351         }
352         global_varlist = 0;
353         for (argc1=2;argc1 < argc; argc1++) {
354                 if ((eq = strchr(argv[argc1],'='))) {
355                         *eq = 0;
356                         set_var(argv[argc1],eq+1);
357                 }
358         }
359
360         /* parse command args for x=y and set varz */
361         
362         parse_file(argv[1]);
363         return 0;
364 }